/* ═══════════════════════════════════════════════════
   BALLOON POP — balloon-pop.css
   Layer order: reset → tokens → base → layout →
                components → animations → utilities
   ═══════════════════════════════════════════════════ */
@layer reset, tokens, base, layout, components, animations, utilities;

/* ── TOKENS ────────────────────────────────────────── */
@layer tokens {
  :root {
    /* Palette */
    --color-bg-top: #87ceeb;
    --color-bg-bottom: #b0e8ff;
    --color-ground: #5daa30;
    --color-ground-dark: #3d8020;
    --color-grass: #6ec236;
    --color-ink: #2d2d2d;
    --color-white: #ffffff;
    --color-hud-bg: rgba(255, 255, 255, 0.22);
    --color-hud-border: rgba(255, 255, 255, 0.5);

    /* Balloon colours */
    --balloon-1: #ff4d4d;
    --balloon-2: #ffd600;
    --balloon-3: #2979ff;
    --balloon-4: #ff4081;
    --balloon-5: #76ff03;
    --balloon-6: #d500f9;
    --balloon-7: #ff6d00;
    --balloon-8: #00e5ff;
    --balloon-9: #ff1493;
    --balloon-10: #aeea00;

    /* Celebration / UI accents */
    --color-yellow: #ffd93d;
    --color-coral: #ff6b6b;
    --color-mint: #6bcb77;
    --color-orange: #ff9a3c;
    --color-purple: #c77dff;

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

    --text-xs: 0.7rem;
    --text-sm: 0.85rem;
    --text-base: 1rem;
    --text-lg: 1.2rem;
    --text-hud: 2.4rem;

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

    /* Balloon geometry */
    --balloon-w: 90px;
    --balloon-h: 115px;
    --balloon-min-w: 80px;
    --balloon-min-h: 100px;

    /* HUD */
    --hud-height: 70px;
    --ground-height: 58px;

    /* Borders & radius */
    --radius-pill: 50px;
    --radius-sm: 8px;

    /* Shadows */
    --shadow-hud: 0 4px 20px rgba(0, 0, 0, 0.12);
    --shadow-btn: 0 3px 0 rgba(0, 0, 0, 0.25);

    /* Easing */
    --ease-bounce: cubic-bezier(0.34, 1.56, 0.64, 1);
    --ease-pop: cubic-bezier(0.22, 1, 0.36, 1);

    /* Duration */
    --dur-fast: 0.15s;
    --dur-base: 0.25s;
    --dur-slow: 0.5s;

    /* Z-index scale */
    --z-bg: 0;
    --z-balloon: 10;
    --z-ground: 15;
    --z-hud: 20;
    --z-particle: 30;
    --z-banner: 40;
    --z-celebration: 50;
    --z-flash: 60;
    --z-skip: 999;
  }
}

/* ── RESET ─────────────────────────────────────────── */
@layer reset {
  *,
  *::before,
  *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }

  html,
  body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    -webkit-text-size-adjust: 100%;
  }

  /* Kill the dark tap-highlight overlay on mobile browsers */
  * {
    -webkit-tap-highlight-color: transparent;
  }

  a {
    color: inherit;
    text-decoration: none;
  }
  img {
    display: block;
    max-width: 100%;
  }
}

/* ── BASE ──────────────────────────────────────────── */
@layer base {
  body {
    font-family: var(--font-display);
    background: linear-gradient(
      180deg,
      var(--color-bg-top) 0%,
      var(--color-bg-bottom) 100%
    );
    color: var(--color-ink);
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    /* fun custom cursor on desktop */
    cursor:
      url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='32' height='32' viewBox='0 0 32 32'%3E%3Ccircle cx='16' cy='16' r='12' fill='%23FFD93D' stroke='%232D2D2D' stroke-width='2.5'/%3E%3C/svg%3E")
        16 16,
      pointer;
  }

  /* Keyboard focus ring */
  :focus-visible {
    outline: 4px solid var(--color-yellow);
    outline-offset: 3px;
    border-radius: var(--radius-sm);
  }
}

/* ── LAYOUT ────────────────────────────────────────── */
@layer layout {
  .game-area {
    position: fixed;
    inset: 0;
    overflow: hidden;
  }

  .hud {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--hud-height);
    z-index: var(--z-hud);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--space-4);
    background: var(--color-hud-bg);
    border-bottom: 2px solid var(--color-hud-border);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    gap: var(--space-3);
  }

  .ground {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--ground-height);
    z-index: var(--z-ground);
    background: linear-gradient(
      180deg,
      var(--color-ground) 0%,
      var(--color-ground-dark) 100%
    );
    border-radius: 60% 60% 0 0 / 18px 18px 0 0;
  }

  /* Grass tufts via pseudo */
  .ground::after {
    content: "";
    position: absolute;
    top: 6px;
    left: 0;
    right: 0;
    height: 7px;
    background: repeating-linear-gradient(
      90deg,
      var(--color-grass) 0 28px,
      transparent 28px 56px
    );
    border-radius: 4px;
  }
}

/* ── COMPONENTS ────────────────────────────────────── */
@layer components {
  /* Skip link */
  .skip-link {
    position: fixed;
    top: -100%;
    left: var(--space-4);
    z-index: var(--z-skip);
    background: var(--color-yellow);
    color: var(--color-ink);
    font-family: var(--font-display);
    font-size: var(--text-base);
    font-weight: 800;
    padding: var(--space-2) var(--space-5);
    border: 3px solid var(--color-ink);
    border-radius: var(--radius-pill);
    box-shadow: var(--shadow-btn);
    transition: top var(--dur-fast);
  }

  .skip-link:focus {
    top: var(--space-3);
  }

  /* Back button */
  .back-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 48px;
    min-height: 48px;
    background: var(--color-white);
    border: 3px solid var(--color-ink);
    border-radius: var(--radius-pill);
    font-size: 1.4rem;
    font-weight: 900;
    box-shadow: var(--shadow-btn);
    transition:
      transform var(--dur-fast) var(--ease-bounce),
      box-shadow var(--dur-fast);
    flex-shrink: 0;
  }

  .back-btn:hover,
  .back-btn:focus-visible {
    transform: scale(1.1);
    box-shadow: 0 5px 0 rgba(0, 0, 0, 0.25);
  }

  /* HUD score + level chips */
  .hud-score,
  .hud-level {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    background: var(--color-white);
    border: 3px solid var(--color-ink);
    border-radius: var(--radius-pill);
    padding: var(--space-1) var(--space-4);
    box-shadow: var(--shadow-btn);
    min-width: 90px;
    justify-content: center;
  }

  .hud-emoji {
    font-size: 1.6rem;
    line-height: 1;
  }

  .hud-num {
    font-size: var(--text-hud);
    font-weight: 900;
    line-height: 1;
    color: var(--color-ink);
    min-width: 2ch;
    text-align: center;
    transition: transform var(--dur-fast) var(--ease-bounce);
  }

  /* Balloon */
  .balloon {
    position: absolute;
    display: flex;
    flex-direction: column;
    align-items: center;
    cursor: pointer;
    z-index: var(--z-balloon);
    /* none = pass all touch points to JS, enabling multi-finger popping */
    touch-action: none;
    /* animation set dynamically in JS via inline style */
  }

  .balloon:focus-visible {
    outline: 4px solid var(--color-yellow);
    outline-offset: 6px;
    border-radius: 50%;
  }

  /* Prevent browsers darkening the balloon body on tap */
  .balloon:active {
    opacity: 1;
  }

  .balloon-body {
    /* width/height/background set inline per balloon */
    border-radius: 50% 50% 45% 45% / 55% 55% 45% 45%;
    position: relative;
    box-shadow:
      inset -10px -7px 0 rgba(0, 0, 0, 0.1),
      inset 9px 7px 0 rgba(255, 255, 255, 0.28),
      0 6px 18px rgba(0, 0, 0, 0.18);
    flex-shrink: 0;
    min-width: var(--balloon-min-w);
    min-height: var(--balloon-min-h);
  }

  /* Shine */
  .balloon-body::before {
    content: "";
    position: absolute;
    top: 12px;
    left: 14px;
    width: 20px;
    height: 32px;
    background: rgba(255, 255, 255, 0.38);
    border-radius: 50%;
    transform: rotate(-30deg);
  }

  .balloon-knot {
    /* background set inline */
    width: 13px;
    height: 13px;
    border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
    margin-top: -2px;
    flex-shrink: 0;
  }

  .balloon-string {
    width: 2px;
    height: 52px;
    flex-shrink: 0;
    background: repeating-linear-gradient(
      180deg,
      rgba(0, 0, 0, 0.28) 0 4px,
      transparent 4px 7px
    );
    border-radius: 1px;
  }

  /* Level-up banner */
  .level-banner {
    position: fixed;
    top: calc(var(--hud-height) + var(--space-4));
    left: 50%;
    transform: translateX(-50%) translateY(-20px);
    z-index: var(--z-banner);
    background: var(--color-yellow);
    color: var(--color-ink);
    font-size: 1.5rem;
    font-weight: 900;
    padding: var(--space-3) var(--space-6);
    border: 3px solid var(--color-ink);
    border-radius: var(--radius-pill);
    box-shadow: 4px 4px 0 var(--color-ink);
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: none;
  }

  .level-banner.show {
    animation: bannerPop 2s var(--ease-bounce) forwards;
  }

  /* Confetti particle */
  .confetti {
    position: fixed;
    border-radius: 50%;
    pointer-events: none;
    z-index: var(--z-particle);
    animation: confettiFly var(--dur, 0.65s) ease-out forwards;
  }

  /* Pop burst emoji */
  .pop-burst {
    position: fixed;
    font-size: 2.8rem;
    pointer-events: none;
    z-index: var(--z-particle);
    animation: popBurst 0.65s ease-out forwards;
    transform: translate(-50%, -50%);
  }

  /* Celebration overlay */
  .celebration {
    position: fixed;
    inset: 0;
    z-index: var(--z-celebration);
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    opacity: 0;
  }

  .celebration.active {
    animation: celebFade 1.6s ease-out forwards;
  }

  .celeb-star {
    font-size: 9rem;
    animation: none;
    filter: drop-shadow(0 0 30px rgba(255, 220, 0, 0.9));
  }

  .celebration.active .celeb-star {
    animation: celebStar 1.6s var(--ease-bounce) forwards;
  }

  /* Rainbow flash */
  .flash {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: var(--z-flash);
    opacity: 0;
  }

  .flash.active {
    animation: flashRainbow 1.5s ease-out forwards;
  }
}

/* ── ANIMATIONS ────────────────────────────────────── */
@layer animations {
  @keyframes floatUp {
    from {
      transform: translateY(0);
    }
    to {
      transform: translateY(calc(-100vh - 220px));
    }
  }

  @keyframes wobble {
    from {
      margin-left: calc(var(--wobble, 16px) * -1);
    }
    to {
      margin-left: var(--wobble, 16px);
    }
  }

  @keyframes confettiFly {
    0% {
      opacity: 0;
      transform: translate(0, 0) scale(0);
    }
    15% {
      opacity: 1;
      transform: translate(calc(var(--tx, 0) * 0.15), calc(var(--ty, 0) * 0.15))
        scale(1);
    }
    100% {
      opacity: 0;
      transform: translate(var(--tx, 0), var(--ty, 0)) scale(0.3);
    }
  }

  @keyframes popBurst {
    0% {
      opacity: 0;
      transform: translate(-50%, -50%) scale(0.5);
    }
    20% {
      opacity: 1;
      transform: translate(-50%, -50%) scale(1.3);
    }
    100% {
      opacity: 0;
      transform: translate(-50%, -240%) scale(0.6);
    }
  }

  @keyframes bannerPop {
    0% {
      opacity: 0;
      transform: translateX(-50%) translateY(-20px) scale(0.7);
    }
    15% {
      opacity: 1;
      transform: translateX(-50%) translateY(0) scale(1.05);
    }
    25% {
      transform: translateX(-50%) translateY(0) scale(1);
    }
    75% {
      opacity: 1;
      transform: translateX(-50%) translateY(0) scale(1);
    }
    100% {
      opacity: 0;
      transform: translateX(-50%) translateY(-16px) scale(0.9);
    }
  }

  @keyframes celebFade {
    0% {
      opacity: 0;
    }
    15% {
      opacity: 1;
    }
    75% {
      opacity: 1;
    }
    100% {
      opacity: 0;
    }
  }

  @keyframes celebStar {
    0% {
      transform: scale(0.4) rotate(-15deg);
    }
    30% {
      transform: scale(1.15) rotate(8deg);
    }
    55% {
      transform: scale(0.95) rotate(-4deg);
    }
    70% {
      transform: scale(1) rotate(0deg);
    }
    100% {
      transform: scale(1.1) rotate(5deg);
    }
  }

  @keyframes flashRainbow {
    0% {
      opacity: 0;
      background: #ff4d4d;
    }
    8% {
      opacity: 0.45;
      background: #ff4d4d;
    }
    25% {
      opacity: 0.4;
      background: #ffd600;
    }
    42% {
      opacity: 0.35;
      background: #76ff03;
    }
    58% {
      opacity: 0.32;
      background: #2979ff;
    }
    75% {
      opacity: 0.28;
      background: #d500f9;
    }
    100% {
      opacity: 0;
      background: #ff4081;
    }
  }

  @keyframes scorePop {
    0% {
      transform: scale(1);
    }
    40% {
      transform: scale(1.55);
    }
    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;
  }
}
