/* ═══════════════════════════════════════════════════════════
   RAINBOW SORT — rainbow-sort.css
   @layer: reset → tokens → base → layout → components → animations → utilities
   ═══════════════════════════════════════════════════════════ */
@layer reset, tokens, base, layout, components, animations, utilities;

/* ── TOKENS ──────────────────────────────────────────────── */
@layer tokens {
  :root {
    /* Game colours — saturated, toddler-perfect */
    --red:    #FF1744;
    --orange: #FF6D00;
    --yellow: #FFD600;
    --green:  #00C853;
    --blue:   #2979FF;
    --purple: #AA00FF;
    --pink:   #FF4081;

    /* UI / background */
    --bg:          #FFFBF0;          /* warm cream */
    --bg-dots:     #F5EDD8;
    --hud-bg:      #FFF8E7;
    --hud-border:  #E8D5A3;
    --hud-shadow:  0 4px 20px rgba(0,0,0,0.12);

    /* Blob */
    --blob-stroke:   4px;
    --blob-min:      72px;
    --blob-max:      96px;

    /* Bucket */
    --bucket-w:     100px;
    --bucket-h:     110px;
    --bucket-stroke: 5px;
    --bucket-radius: 16px;

    /* Spacing */
    --space-1:  4px;
    --space-2:  8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 20px;
    --space-6: 24px;

    /* HUD */
    --hud-h: 72px;

    /* Typography */
    --font: 'Arial Rounded MT Bold', 'Nunito', Arial, sans-serif;

    /* Easing */
    --ease-bounce:  cubic-bezier(0.34, 1.56, 0.64, 1);
    --ease-out:     cubic-bezier(0.22, 1, 0.36, 1);
    --ease-wiggle:  cubic-bezier(0.36, 0.07, 0.19, 0.97);

    /* Z-index */
    --z-bg:       0;
    --z-buckets:  2;
    --z-blobs:    4;
    --z-dragging: 8;
    --z-hud:     10;
    --z-overlay: 20;
    --z-confetti:30;
    --z-skip:   999;
  }
}

/* ── RESET ───────────────────────────────────────────────── */
@layer reset {
  *, *::before, *::after {
    margin: 0; padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
  }
  html, body { width: 100%; height: 100%; overflow: hidden; -webkit-text-size-adjust: 100%; }
  button { font: inherit; cursor: pointer; border: none; background: none; -webkit-appearance: none; }
  a      { color: inherit; text-decoration: none; }
}

/* ── BASE ────────────────────────────────────────────────── */
@layer base {
  body {
    font-family: var(--font);
    background-color: var(--bg);
    /* Polka-dot pattern using radial gradients */
    background-image:
      radial-gradient(circle, var(--bg-dots) 2px, transparent 2px);
    background-size: 32px 32px;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
    overflow: hidden;
  }

  :focus-visible {
    outline: 4px solid var(--yellow);
    outline-offset: 4px;
    border-radius: 12px;
  }
}

/* ── LAYOUT ──────────────────────────────────────────────── */
@layer layout {

  .hud {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: var(--hud-h);
    z-index: var(--z-hud);
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: 0 var(--space-4);
    background: var(--hud-bg);
    border-bottom: 3px solid var(--hud-border);
    box-shadow: var(--hud-shadow);
  }

  .game-area {
    position: fixed;
    top: var(--hud-h);
    left: 0; right: 0; bottom: 0;
    z-index: var(--z-bg);
    overflow: hidden;
  }

  /* Blobs scatter in the upper 55% of the game area */
  .blob-layer {
    position: absolute;
    top: 0; left: 0; right: 0;
    bottom: 45%;
  }

  /* Buckets sit in the lower 38% */
  .bucket-layer {
    position: absolute;
    left: 0; right: 0;
    bottom: 0;
    height: 42%;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    gap: clamp(8px, 3vw, 24px);
    padding-bottom: var(--space-5);
  }

  .confetti-layer {
    position: fixed; inset: 0;
    pointer-events: none;
    z-index: var(--z-confetti);
    overflow: hidden;
  }
}

/* ── COMPONENTS ──────────────────────────────────────────── */
@layer components {

  /* Skip link */
  .skip-link {
    position: fixed; top: -200%; left: 16px; z-index: var(--z-skip);
    background: var(--yellow); color: #111; font-weight: 800;
    padding: 8px 20px; border-radius: 40px; border: 3px solid #111;
    transition: top 100ms;
  }
  .skip-link:focus { top: 12px; }

  /* ── HUD elements ── */
  .hud-btn {
    display: flex; align-items: center; justify-content: center;
    min-width: 52px; min-height: 52px;
    border-radius: 50%;
    background: var(--bg);
    border: 3px solid var(--hud-border);
    font-size: 1.3rem; color: #555;
    flex-shrink: 0;
    box-shadow: 0 3px 8px rgba(0,0,0,0.10);
    transition: transform 120ms var(--ease-bounce);
  }
  .hud-btn:hover  { transform: scale(1.10); }
  .hud-btn:active { transform: scale(0.88); }

  .round-pips {
    display: flex;
    gap: 8px;
    flex: 1;
    justify-content: center;
  }

  .round-pip {
    width: 16px; height: 16px;
    border-radius: 50%;
    background: #D9CFBA;
    border: 2px solid #C4B89A;
    transition: background 300ms, transform 300ms var(--ease-bounce);
  }
  .round-pip.done {
    background: var(--yellow);
    border-color: #E8A000;
    transform: scale(1.15);
  }
  .round-pip.active {
    background: var(--orange);
    border-color: #CC5500;
    transform: scale(1.25);
    animation: pipPulse 1.2s ease-in-out infinite;
  }

  .score-chip {
    display: flex; align-items: center; gap: 6px;
    background: var(--bg);
    border: 3px solid var(--hud-border);
    border-radius: 40px;
    padding: 6px 14px;
    font-size: 1.1rem;
    box-shadow: 0 3px 8px rgba(0,0,0,0.10);
    flex-shrink: 0;
  }
  .score-num {
    font-size: 1.3rem;
    font-weight: 900;
    color: #333;
    min-width: 24px;
    text-align: center;
  }

  /* ── BLOB ── */
  .blob {
    position: absolute;
    width:  var(--blob-size, 80px);
    height: var(--blob-size, 80px);
    border-radius: var(--blob-radius, 60% 40% 55% 45% / 45% 55% 45% 55%);
    background: var(--blob-color, #FF1744);
    border: var(--blob-stroke) solid rgba(0,0,0,0.18);
    box-shadow:
      inset 0 -6px 12px rgba(0,0,0,0.14),
      inset 0  4px  8px rgba(255,255,255,0.35),
      0 6px 18px rgba(0,0,0,0.20);
    cursor: grab;
    touch-action: none;
    /* wiggle while idle */
    animation: blobWiggle var(--wiggle-dur, 2.4s) var(--wiggle-delay, 0s) ease-in-out infinite;
    transition: transform 120ms var(--ease-bounce),
                box-shadow 120ms,
                opacity 200ms;
    /* emoji / face inside */
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: calc(var(--blob-size, 80px) * 0.42);
    will-change: transform;
  }

  .blob:hover { transform: scale(1.08); }

  .blob.dragging {
    cursor: grabbing;
    z-index: var(--z-dragging);
    animation: none;                       /* stop wiggle while held */
    transform: scale(1.18) rotate(var(--drag-rot, 4deg));
    box-shadow:
      inset 0 -6px 12px rgba(0,0,0,0.14),
      inset 0  4px  8px rgba(255,255,255,0.35),
      0 16px 40px rgba(0,0,0,0.30);
    opacity: 0.92;
  }

  .blob.wrong {
    animation: blobReject 0.45s var(--ease-wiggle) forwards;
  }

  .blob.matched {
    animation: blobMatch 0.5s var(--ease-bounce) forwards;
    pointer-events: none;
  }

  /* ── BUCKET ── */
  .bucket {
    position: relative;
    width:  var(--bucket-w);
    height: var(--bucket-h);
    flex-shrink: 0;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    cursor: default;
    transition: transform 160ms var(--ease-bounce);
    /* min touch target */
    min-width: 80px;
  }

  /* The bucket body — trapezoid shape via clip-path */
  .bucket-body {
    position: absolute;
    inset: 0;
    background: var(--bucket-color, #FF1744);
    /* Trapezoid: wider at top, narrower at bottom */
    clip-path: polygon(8% 0%, 92% 0%, 82% 100%, 18% 100%);
    border-radius: var(--bucket-radius);
    box-shadow:
      inset 0 8px 16px rgba(255,255,255,0.25),
      inset 0 -6px 12px rgba(0,0,0,0.18),
      0 6px 20px rgba(0,0,0,0.20);
    transition: filter 200ms;
  }

  /* Thick top rim */
  .bucket-rim {
    position: absolute;
    top: -8px; left: -6px; right: -6px;
    height: 20px;
    background: var(--bucket-color, #FF1744);
    filter: brightness(0.82);
    border-radius: 10px;
    box-shadow: 0 3px 8px rgba(0,0,0,0.22);
  }

  /* Count badge inside bucket */
  .bucket-count {
    position: absolute;
    bottom: 10px;
    font-size: 1.4rem;
    font-weight: 900;
    color: rgba(255,255,255,0.90);
    text-shadow: 0 2px 4px rgba(0,0,0,0.30);
    z-index: 1;
    pointer-events: none;
    transition: transform 200ms var(--ease-bounce);
  }

  /* Highlight when a matching blob hovers over */
  .bucket.highlight .bucket-body {
    filter: brightness(1.12) saturate(1.2);
  }
  .bucket.highlight {
    transform: scale(1.08);
  }

  /* Wrong colour flash */
  .bucket.wrong-flash .bucket-body {
    animation: bucketWrongFlash 0.35s ease forwards;
  }

  /* ── OVERLAYS ── */
  .overlay {
    position: fixed; inset: 0;
    z-index: var(--z-overlay);
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    opacity: 0;
  }
  .overlay.show {
    pointer-events: auto;
    animation: overlayIn 0.5s var(--ease-bounce) forwards;
  }
  .overlay.hide {
    animation: overlayOut 0.35s var(--ease-out) forwards;
  }

  .overlay-inner {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-4);
  }

  .overlay-emoji, .win-emoji {
    font-size: clamp(5rem, 20vw, 9rem);
    filter: drop-shadow(0 8px 24px rgba(0,0,0,0.18));
    animation: overlayEmoji 0.7s var(--ease-bounce) 0.1s both;
  }

  .overlay-stars, .win-stars {
    font-size: clamp(2rem, 8vw, 3.5rem);
    letter-spacing: 8px;
    animation: overlayEmoji 0.5s var(--ease-bounce) 0.3s both;
  }

  /* ── CONFETTI ── */
  .confetti-piece {
    position: absolute;
    top: -30px;
    opacity: 0;
    animation: confettiFall var(--cf-dur, 2.8s) var(--cf-delay, 0s) ease-in forwards;
  }
  .confetti-piece.circle  { border-radius: 50%; }
  .confetti-piece.diamond { border-radius: 2px; }

  /* ── DROP ZONE indicator ring (drawn by JS as overlay on bucket) ── */
  .drop-zone-ring {
    position: absolute;
    inset: -10px;
    border-radius: 50%;
    border: 4px dashed rgba(255,255,255,0.70);
    pointer-events: none;
    animation: ringPulse 1s ease-in-out infinite;
  }
}

/* ── ANIMATIONS ──────────────────────────────────────────── */
@layer animations {

  /* Blob idle wiggle */
  @keyframes blobWiggle {
    0%,100% { border-radius: 60% 40% 55% 45% / 45% 55% 45% 55%; transform: rotate(-2deg) scale(1);   }
    25%      { border-radius: 45% 55% 40% 60% / 60% 40% 60% 40%; transform: rotate( 2deg) scale(1.02);}
    50%      { border-radius: 55% 45% 60% 40% / 40% 60% 40% 60%; transform: rotate(-1deg) scale(0.98);}
    75%      { border-radius: 40% 60% 45% 55% / 55% 45% 55% 45%; transform: rotate( 3deg) scale(1.01);}
  }

  /* Blob rejected — shakes left/right */
  @keyframes blobReject {
    0%         { transform: translateX(0)    rotate(0deg)   scale(1); }
    15%        { transform: translateX(-16px) rotate(-6deg) scale(1.05); }
    35%        { transform: translateX( 16px) rotate( 6deg) scale(1.05); }
    55%        { transform: translateX(-10px) rotate(-3deg) scale(1.02); }
    75%        { transform: translateX(  8px) rotate( 3deg) scale(1.01); }
    100%       { transform: translateX(0)    rotate(0deg)   scale(1); }
  }

  /* Blob matched — shrinks into bucket */
  @keyframes blobMatch {
    0%   { transform: scale(1.2); opacity: 1; }
    60%  { transform: scale(0.6); opacity: 0.8; }
    100% { transform: scale(0);   opacity: 0; }
  }

  /* Bucket wrong-colour flash */
  @keyframes bucketWrongFlash {
    0%,100% { filter: brightness(0.82); }
    30%     { filter: brightness(1.4) saturate(0.4); }
  }

  /* Bucket drop-zone ring pulse */
  @keyframes ringPulse {
    0%,100% { opacity: 0.5; transform: scale(1); }
    50%     { opacity: 1;   transform: scale(1.05); }
  }

  /* Round pip pulse */
  @keyframes pipPulse {
    0%,100% { transform: scale(1.25); }
    50%     { transform: scale(1.40); }
  }

  /* Overlay in */
  @keyframes overlayIn {
    0%   { opacity: 0; }
    100% { opacity: 1; }
  }
  @keyframes overlayOut {
    0%   { opacity: 1; }
    100% { opacity: 0; }
  }

  /* Overlay emoji bounce */
  @keyframes overlayEmoji {
    0%   { transform: scale(0.3) rotate(-15deg); opacity: 0; }
    60%  { transform: scale(1.2) rotate(5deg);   opacity: 1; }
    80%  { transform: scale(0.95) rotate(-2deg); }
    100% { transform: scale(1)   rotate(0deg);   opacity: 1; }
  }

  /* Confetti fall */
  @keyframes confettiFall {
    0%   { opacity: 1; transform: translateY(0)   rotate(0deg)              scale(1); }
    70%  { opacity: 1; }
    100% { opacity: 0; transform: translateY(calc(100vh + 60px)) rotate(var(--cf-spin, 540deg)) scale(0.5); }
  }

  /* Score number pop */
  @keyframes scorePop {
    0%   { transform: scale(1); }
    40%  { transform: scale(1.6); }
    100% { transform: scale(1); }
  }

  /* Reduced motion */
  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
      animation-duration: 0.01ms !important;
      animation-iteration-count: 1 !important;
      transition-duration: 0.01ms !important;
    }
  }
}

/* ── UTILITIES ───────────────────────────────────────────── */
@layer utilities {
  .sr-only {
    position: absolute; width: 1px; height: 1px;
    padding: 0; margin: -1px; overflow: hidden;
    clip: rect(0,0,0,0); white-space: nowrap; border: 0;
  }
}
