/* ─────────────────────────────────────────────────────────────
   Tap Tile! — stylesheet
   Mobile-first, portrait. Everything scales off --tile, which
   render.js recomputes whenever the board is laid out.

   Responsive strategy — one HTML tree, four shapes:
     · phone portrait   → the base rules below (single column)
     · short / landscape→ game splits into board | sidebar,
                          home splits into brand | controls
     · tablet / desktop → the column is centred in a framed panel
                          and capped so tiles never balloon
   Sizes that a person perceives (type, tiles, chrome) use clamp()
   so the in-between widths look deliberate rather than snapped.
   ───────────────────────────────────────────────────────────── */

:root {
  --bg-0:        #0b0e26;
  --bg-1:        #171b45;
  --bg-2:        #241a52;
  --panel:       rgba(255, 255, 255, 0.07);
  --panel-line:  rgba(255, 255, 255, 0.14);
  --ink:         #f3f2ff;
  --ink-dim:     rgba(243, 242, 255, 0.62);
  --accent:      #ffcb45;
  --accent-2:    #ff7ab8;
  --good:        #4ee39a;
  --bad:         #ff5c72;
  --tile-face:   linear-gradient(160deg, #fffdf6, #ecebfb 62%, #d7d5f0);
  --tile-edge:   #b9b5dd;
  --radius:      18px;
  --tile:        48px;
  --safe-b:      env(safe-area-inset-bottom, 0px);
  --app-w:       560px;   /* widened in landscape, capped on desktop */
  --pad-x:       10px;    /* screen side padding, tightened on tiny phones */
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  font-family: "Nunito", "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: var(--bg-0);
  user-select: none;
  touch-action: manipulation;
}

/* Ambient background wash */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  background:
    radial-gradient(1100px 620px at 12% -8%,  #3b2a86 0%, transparent 60%),
    radial-gradient(900px  560px at 92% 12%,  #1b4d7a 0%, transparent 58%),
    radial-gradient(1000px 700px at 50% 108%, #5a2668 0%, transparent 62%),
    linear-gradient(180deg, var(--bg-1), var(--bg-0) 55%, var(--bg-2));
  z-index: -1;
}

/* The play column. `dvh` keeps it pinned to what is actually visible while a
   mobile browser's toolbars slide in and out; the `100%` above it is the
   fallback for engines that don't know the unit. */
#app {
  position: relative;
  height: 100%;
  height: 100dvh;
  width: 100%;
  max-width: var(--app-w);
  margin: 0 auto;
}

/* ── screens ─────────────────────────────────────────────── */

.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  padding: max(10px, env(safe-area-inset-top, 0px))
           max(var(--pad-x), env(safe-area-inset-right, 0px))
           calc(8px + var(--safe-b))
           max(var(--pad-x), env(safe-area-inset-left, 0px));
}
.screen.is-active { display: flex; }

/* ── home ────────────────────────────────────────────────── */

/* Title-screen backdrop. Referenced straight from the stylesheet, so the
   relative path resolves against css/ — no absolute-URL dance needed here,
   unlike the per-level scenes that travel through a custom property. */
#screen-home {
  background:
    url("../assets/scenes/home.svg") center / cover no-repeat,
    linear-gradient(180deg, #231150 0%, #4a1c68 55%, #c04f6c 100%);
  /* Last-resort escape hatch: on a screen too short for even the compact
     landscape layout, the menu scrolls instead of being clipped.
     Both axes must be named. Setting only overflow-y promotes the other
     axis from `visible` to `auto` per spec, which hands the decorative
     halo behind the wordmark (.title::before, 132% wide) a horizontal
     scrollbar it never asked for. */
  overflow: hidden auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.28) transparent;
}

/* The default track is a light slab, which reads as a rendering fault in a
   dark UI. Applies to the menu and to any sheet tall enough to scroll. */
#screen-home::-webkit-scrollbar,
.modal-root::-webkit-scrollbar,
.modal::-webkit-scrollbar { width: 6px; height: 6px; }
#screen-home::-webkit-scrollbar-track,
.modal-root::-webkit-scrollbar-track,
.modal::-webkit-scrollbar-track { background: transparent; }
#screen-home::-webkit-scrollbar-thumb,
.modal-root::-webkit-scrollbar-thumb,
.modal::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.28);
  border-radius: 99px;
}

/* Tiles drifting slowly behind the menu, to stop the screen feeling static. */
.home-drift {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 0;
}
.home-drift span {
  position: absolute;
  display: grid;
  place-items: center;
  border-radius: 22%;
  background: var(--tile-face);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.45);
  opacity: 0.16;
  animation: drift 17s ease-in-out infinite;
}
.home-drift span:nth-child(1) { left:  6%; top: 14%; width: 62px; height: 62px; font-size: 30px; animation-duration: 15s; }
.home-drift span:nth-child(2) { left: 78%; top: 20%; width: 48px; height: 48px; font-size: 24px; animation-duration: 19s; animation-delay: -4s; }
.home-drift span:nth-child(3) { left: 14%; top: 64%; width: 54px; height: 54px; font-size: 27px; animation-duration: 21s; animation-delay: -8s; }
.home-drift span:nth-child(4) { left: 82%; top: 72%; width: 58px; height: 58px; font-size: 29px; animation-duration: 16s; animation-delay: -2s; }
.home-drift span:nth-child(5) { left: 44%; top: 86%; width: 44px; height: 44px; font-size: 22px; animation-duration: 23s; animation-delay: -11s; }
.home-drift span:nth-child(6) { left: 62%; top:  8%; width: 40px; height: 40px; font-size: 20px; animation-duration: 18s; animation-delay: -6s; }

@keyframes drift {
  0%, 100% { transform: translate(0, 0) rotate(-8deg); }
  33%      { transform: translate(14px, -26px) rotate(6deg); }
  66%      { transform: translate(-10px, -12px) rotate(-3deg); }
}

.home-inner {
  position: relative;   /* above the drifting tiles */
  z-index: 1;
  margin: auto;
  width: 100%;
  text-align: center;
  padding: 16px;
}

.logo { display: flex; justify-content: center; gap: 10px; margin-bottom: 14px; }

.logo-tile {
  width: clamp(46px, 14vw, 66px);
  height: clamp(46px, 14vw, 66px);
  display: grid; place-items: center;
  font-size: clamp(22px, 6.8vw, 32px);
  border-radius: 16px;
  background: var(--tile-face);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.9),
              0 6px 0 var(--tile-edge),
              0 14px 28px rgba(0, 0, 0, 0.5),
              0 0 30px rgba(255, 203, 69, 0.28);
  animation: bob 2.6s ease-in-out infinite;
}
.logo-tile.t2 { animation-delay: 0.22s; }
.logo-tile.t3 { animation-delay: 0.44s; }

@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(-3deg); }
  50%      { transform: translateY(-11px) rotate(3deg); }
}

.title {
  position: relative;
  margin: 6px 0 4px;
  font-size: clamp(40px, 12vw, 60px);
  font-weight: 900;
  letter-spacing: -1.5px;
  background: linear-gradient(180deg, #fff 10%, #ffe9a8 45%, var(--accent) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 4px 0 rgba(0, 0, 0, 0.35))
          drop-shadow(0 0 26px rgba(255, 203, 69, 0.45));
}

/* Warm halo sitting behind the wordmark. */
.title::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 132%;
  height: 220%;
  transform: translate(-50%, -50%);
  background: radial-gradient(closest-side, rgba(255, 190, 90, 0.3), transparent 72%);
  z-index: -1;
  pointer-events: none;
}

.tagline {
  margin: 0 0 clamp(14px, 4vh, 26px);
  color: rgba(255, 240, 214, 0.78);
  font-size: clamp(13px, 3.8vw, 16px);
  font-weight: 700;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.55);
}

.home-stats {
  display: flex;
  gap: 10px;
  margin-bottom: clamp(14px, 4vh, 26px);
}
/* Darker, glassier chips — the title backdrop is a lit scene, so the
   light-translucent panel used elsewhere would wash out against it. */
.stat {
  flex: 1;
  padding: 12px 6px;
  border-radius: 16px;
  background: rgba(12, 8, 34, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.18);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.22),
              0 6px 18px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(8px);
}
.stat-val { display: block; font-size: clamp(19px, 5.6vw, 26px); font-weight: 900; color: var(--accent); }
.stat-key { display: block; font-size: 11px; letter-spacing: 1.2px; text-transform: uppercase; color: var(--ink-dim); }

.home-row { display: flex; gap: 10px; margin-top: 12px; }
.home-row .btn { flex: 1; }

/* ── buttons ─────────────────────────────────────────────── */

.btn {
  display: block;
  width: 100%;
  padding: clamp(12px, 1.8vh, 16px) 18px;
  border: 0;
  border-radius: 16px;
  font: inherit;
  font-weight: 800;
  font-size: clamp(15px, 4.2vw, 18px);
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--panel-line);
  cursor: pointer;
  transition: transform 0.09s ease, filter 0.15s ease;
}
.btn:active { transform: translateY(2px) scale(0.985); }

.btn-primary {
  color: #2b1c00;
  background: linear-gradient(180deg, #ffe08a, var(--accent));
  border: 0;
  box-shadow: 0 5px 0 #c9982a, 0 12px 24px rgba(0, 0, 0, 0.34);
}
.btn-primary:active { box-shadow: 0 2px 0 #c9982a, 0 6px 14px rgba(0, 0, 0, 0.3); }

.btn-play { font-size: clamp(18px, 5vw, 22px); padding: clamp(14px, 2.2vh, 18px); }
.btn.disabled { opacity: 0.45; }

/* Mouse and trackpad users get a hover state; touch devices never match
   this, so no tile is left stuck in a "hovered" look after a tap. */
@media (hover: hover) and (pointer: fine) {
  .btn:hover { filter: brightness(1.09); }
  .icon-btn:hover, .power:hover, .shop-item:not(.disabled):hover, .toggle:hover {
    filter: brightness(1.14);
    border-color: rgba(255, 255, 255, 0.4);
  }
  /* Brightness only — a transform here would out-rank the :active press
     state defined above it and swallow the tap feedback. */
  .tile:not(.covered):hover { filter: brightness(1.07); }
}

.icon-btn {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  font-size: 30px; line-height: 1;
  border-radius: 12px;
  border: 1px solid var(--panel-line);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  padding: 0 0 4px;
}

/* Mute toggle. The chevron in .icon-btn is a glyph that sits high in its box
   and needs the bottom padding; an emoji does not, so it is reset here. */
.sound-btn {
  font-size: 19px;
  padding: 0;
  flex: 0 0 auto;
}
.sound-btn.is-off { opacity: 0.55; }

/* ── volume slider ───────────────────────────────────────── */

.slider-row {
  padding: 12px 14px 14px;
  margin-bottom: 9px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  text-align: left;
  transition: opacity 0.2s ease;
}
.slider-row.is-off { opacity: 0.45; }   /* muted: the level still shows, greyed */
.slider-key {
  display: block;
  margin-bottom: 8px;
  font-weight: 800;
  font-size: 15px;
}
.slider-key b { float: right; color: var(--accent); font-variant-numeric: tabular-nums; }

.slider-row input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 99px;
  background: rgba(0, 0, 0, 0.35);
  outline: none;
  cursor: pointer;
}
.slider-row input[type="range"]:disabled { cursor: default; }
/* The two thumb rules cannot be combined — an unknown pseudo-element voids
   the whole selector list, so each engine needs its own declaration. */
.slider-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: linear-gradient(180deg, #ffe08a, var(--accent));
  border: 0;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
  cursor: pointer;
}
.slider-row input[type="range"]::-moz-range-thumb {
  width: 22px; height: 22px;
  border-radius: 50%;
  background: linear-gradient(180deg, #ffe08a, var(--accent));
  border: 0;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
  cursor: pointer;
}

/* ── themed scene (game screen) ──────────────────────────────
   Each world supplies --sky-1/2/3 and --sky-glow; the scenery is
   built from those with layered gradients, so a new theme is data,
   not new artwork. Panels flip to dark-translucent here because the
   backdrops are bright.
   ────────────────────────────────────────────────────────── */

#screen-game {
  --sky-1: #cdeeff;
  --sky-2: #79c2e8;
  --sky-3: #2f6f9e;
  --sky-glow: rgba(255, 255, 255, 0.55);
  --theme-accent: #7fd8ff;
  --panel: rgba(6, 16, 34, 0.46);
  --panel-line: rgba(255, 255, 255, 0.26);
}

/* The whole scene paints as the screen's own background, so it layers above
   the app-wide backdrop on body::before without needing negative z-indexes
   (which would slide it behind that backdrop and disappear).

   Topmost layer is the illustrated SVG for the world, sized to cover. The
   gradient stack stays underneath as a fallback, so a missing or slow scene
   file still leaves a correctly-coloured screen rather than a black one. */
#screen-game {
  background:
    var(--scene, none) center / cover no-repeat,
    radial-gradient(78% 34% at 26% 12%, var(--sky-glow), transparent 62%),
    linear-gradient(180deg, var(--sky-1) 0%, var(--sky-2) 52%, var(--sky-3) 100%);
}

/* ── HUD ─────────────────────────────────────────────────── */

.hud {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 2px 8px;
}
.hud-block {
  flex: 1 1 60px;   /* the three blocks share whatever the back button leaves */
  min-width: 0;
  text-align: center;
  padding: 5px 4px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
}
.hud-key { display: block; font-size: 9px; letter-spacing: 1.3px; text-transform: uppercase; color: var(--ink-dim); }
.hud-val { display: block; font-size: clamp(15px, 4.4vw, 19px); font-weight: 900; }

/* Final ten seconds of a timed round. */
.hud-val.urgent {
  color: var(--bad);
  animation: clockPulse 0.9s ease-in-out infinite;
}
@keyframes clockPulse {
  0%, 100% { transform: scale(1);    opacity: 1; }
  50%      { transform: scale(1.14); opacity: 0.72; }
}

.progress {
  height: 6px;
  border-radius: 99px;
  background: rgba(0, 0, 0, 0.32);
  overflow: hidden;
  margin-bottom: 6px;
}
.progress-fill {
  height: 100%;
  width: 0;
  border-radius: 99px;
  background: linear-gradient(90deg, var(--good), var(--accent));
  transition: width 0.25s ease;
}

/* ── board ───────────────────────────────────────────────── */

.board {
  position: relative;
  flex: 1;
  min-height: 0;
  margin: 2px 0 8px;
}

.tile {
  position: absolute;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 22%;
  background: var(--tile-face);
  box-shadow:
    0 calc(2px + var(--depth, 0) * 1px) 0 var(--tile-edge),
    0 calc(5px + var(--depth, 0) * 2px) calc(9px + var(--depth, 0) * 3px) rgba(0, 0, 0, 0.4);
  cursor: pointer;
  font-family: inherit;
  transition: transform 0.12s ease, filter 0.18s ease, box-shadow 0.18s ease;
  will-change: transform;
}
.tile .face {
  line-height: 1;
  pointer-events: none;
  transition: opacity 0.1s ease, filter 0.2s ease;
}

.tile:not(.covered):active { transform: scale(0.9); }

/* Tappable tiles are bright and saturated; locked ones turn to cold slate
   with a muted face. Two clearly different materials, not one dimmed —
   that is what makes a dense board readable at a glance. */
.tile:not(.covered) {
  background: var(--tile-face);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.9),
    0 calc(2px + var(--depth, 0) * 1px) 0 var(--tile-edge),
    0 calc(5px + var(--depth, 0) * 2px) calc(9px + var(--depth, 0) * 3px) rgba(0, 0, 0, 0.42);
}

.tile.covered {
  background: linear-gradient(160deg, #a6b8ce 0%, #8497b4 55%, #6b7f9f 100%);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.28),
    0 calc(2px + var(--depth, 0) * 1px) 0 #56688a,
    0 calc(4px + var(--depth, 0) * 2px) calc(8px + var(--depth, 0) * 2px) rgba(0, 0, 0, 0.34);
  cursor: default;
  pointer-events: none;
}
.tile.covered .face {
  filter: grayscale(0.45) brightness(0.66) contrast(1.08);
}

/* Gold halo on a highlighted tile, as in the reference art. */
.tile.hint {
  animation: hintPulse 0.55s ease 3;
  z-index: 9000 !important;
}
@keyframes hintPulse {
  0%, 100% {
    transform: scale(1);
    box-shadow: 0 4px 0 var(--tile-edge),
                0 0 0 0 rgba(255, 203, 69, 0.85),
                0 0 18px 4px rgba(255, 203, 69, 0.8);
  }
  50% {
    transform: scale(1.16);
    box-shadow: 0 4px 0 var(--tile-edge),
                0 0 0 14px rgba(255, 203, 69, 0),
                0 0 26px 8px rgba(255, 214, 110, 0.95);
  }
}

.tile.nope { animation: nope 0.28s ease; }
@keyframes nope {
  0%, 100% { transform: translateX(0); }
  25%      { transform: translateX(-4px) rotate(-2deg); }
  75%      { transform: translateX(4px) rotate(2deg); }
}

.tile.flip .face { opacity: 0; }
.tile.flip { transform: rotateY(80deg) scale(0.92); }

/* ── tutorial coach ──────────────────────────────────────── */

.coach {
  max-height: 0;
  margin: 0 2px;
  padding: 0 14px;
  overflow: hidden;
  border-radius: 14px;
  /* Dark chip: the scenic backdrops are bright, so a translucent light
     panel would leave the text nearly invisible. */
  background: rgba(6, 16, 34, 0.62);
  border: 1px solid rgba(255, 203, 69, 0);
  color: #ffdf95;
  backdrop-filter: blur(4px);
  font-size: 14px;
  font-weight: 800;
  line-height: 1.35;
  text-align: center;
  opacity: 0;
  transition: max-height 0.3s ease, padding 0.3s ease, opacity 0.3s ease, border-color 0.3s ease;
}
.coach.show {
  max-height: 72px;
  padding: 10px 14px;
  border-color: rgba(255, 203, 69, 0.45);
  opacity: 1;
}

/* The pointing hand. Lives in #fx, so it never intercepts a tap. */
.coach-hand {
  position: fixed;
  font-size: 40px;
  line-height: 1;
  opacity: 0;
  transform-origin: 20% 0%;
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.6));
  transition: left 0.35s cubic-bezier(0.3, 0.9, 0.4, 1),
              top  0.35s cubic-bezier(0.3, 0.9, 0.4, 1),
              opacity 0.25s ease;
  z-index: 520;
}
.coach-hand.show { opacity: 1; animation: handTap 1.35s ease-in-out infinite; }

@keyframes handTap {
  0%, 100% { transform: translate(0, 0) scale(1); }
  35%      { transform: translate(-3px, -13px) scale(1.1); }
  55%      { transform: translate(0, 2px) scale(0.9); }
  75%      { transform: translate(0, 0) scale(1); }
}

/* ── tray ────────────────────────────────────────────────── */

/* A container so the tray's contents size off the tray's own width rather
   than the viewport's — the two stop agreeing the moment the play column is
   capped on desktop or moved into the landscape sidebar. */
.tray-wrap { padding: 0 2px; container-type: inline-size; }

.tray-note {
  max-height: 0;
  width: fit-content;
  margin: 0 auto;
  overflow: hidden;
  border-radius: 99px;
  font-size: 13px;
  font-weight: 800;
  text-align: center;
  color: var(--accent);
  background: rgba(6, 16, 34, 0.7);
  opacity: 0;
  transition: max-height 0.22s ease, opacity 0.22s ease, padding 0.22s ease;
}
.tray-note.show { max-height: 32px; padding: 5px 14px; margin-top: 6px; opacity: 1; }
.tray-note.danger { color: var(--bad); animation: notePulse 0.9s ease-in-out infinite; }

@keyframes notePulse { 50% { opacity: 0.5; } }

.tray {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  padding: 7px;
  border-radius: var(--radius);
  background: rgba(6, 16, 34, 0.5);
  border: 2px solid var(--panel-line);
  box-shadow: inset 0 3px 14px rgba(0, 0, 0, 0.5),
              0 6px 18px rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(4px);
  transition: border-color 0.25s ease, box-shadow 0.25s ease;
}
/* Two slots left — amber nudge. */
.tray.warn {
  border-color: rgba(255, 203, 69, 0.8);
  box-shadow: inset 0 3px 14px rgba(0, 0, 0, 0.5), 0 0 18px rgba(255, 203, 69, 0.3);
}
/* One slot left — red alarm. */
.tray.danger {
  border-color: rgba(255, 92, 114, 0.85);
  box-shadow: inset 0 3px 14px rgba(0, 0, 0, 0.5), 0 0 22px rgba(255, 92, 114, 0.4);
  animation: trayPulse 1.1s ease-in-out infinite;
}
@keyframes trayPulse { 50% { border-color: rgba(255, 92, 114, 0.35); } }

.slot {
  position: relative;
  aspect-ratio: 1;
  border-radius: 14%;
  background: rgba(255, 255, 255, 0.05);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.07);
  transition: box-shadow 0.25s ease, background 0.25s ease;
}

/* The final empty slots get an outline of their own, so "two left" is
   readable at a glance without counting tiles. */
.tray.warn .slot.last-chance {
  background: rgba(255, 203, 69, 0.10);
  box-shadow: inset 0 0 0 2px rgba(255, 203, 69, 0.75);
  animation: slotPulse 1.2s ease-in-out infinite;
}
.tray.danger .slot.last-chance {
  background: rgba(255, 92, 114, 0.14);
  box-shadow: inset 0 0 0 2px rgba(255, 92, 114, 0.9);
  animation: slotPulse 0.8s ease-in-out infinite;
}
@keyframes slotPulse { 50% { opacity: 0.45; } }

.tray-tile {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  font-size: min(6.2vw, 30px);   /* fallback for no container-query support */
  border-radius: 22%;
  animation: dropIn 0.16s ease;
}
/* Seven slots plus gaps and padding leaves each one ≈12% of the tray, so a
   face at ~6.4cqw fills it at any column width. */
@supports (container-type: inline-size) {
  .tray-tile { font-size: clamp(13px, 6.4cqw, 34px); }
}
@keyframes dropIn { from { transform: scale(0.6); opacity: 0; } }

.tray-tile.pending { visibility: hidden; }

.tray-tile.popping {
  animation: pop 0.24s ease forwards;
  z-index: 5;
}
@keyframes pop {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.3) rotate(6deg); filter: brightness(1.5); }
  100% { transform: scale(0.1) rotate(-14deg); opacity: 0; }
}

/* ── power-ups ───────────────────────────────────────────── */

.powerups {
  display: flex;
  gap: 8px;
  padding: 10px 2px 2px;
}
.power {
  position: relative;
  flex: 1;
  min-width: 0;
  padding: clamp(9px, 1.5vh, 13px) 0;
  font-size: clamp(19px, 5.6vw, 24px);
  border-radius: 15px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  color: var(--ink);
  cursor: pointer;
  transition: transform 0.09s ease;
}
.power:active { transform: translateY(2px) scale(0.97); }
.power b {
  position: absolute;
  top: -6px; right: -4px;
  min-width: 21px;
  padding: 1px 5px;
  font-size: 11px;
  font-weight: 900;
  color: #2b1c00;
  background: var(--accent);
  border-radius: 99px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
}
.power b.inf { background: var(--good); }

/* ── FX layer ────────────────────────────────────────────── */

#fx {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 400;
}

.tile.flying {
  position: fixed;
  transition: left 190ms cubic-bezier(0.4, 0.1, 0.3, 1),
              top  190ms cubic-bezier(0.4, 0.1, 0.3, 1),
              width 190ms ease, height 190ms ease, font-size 190ms ease;
  z-index: 500;
}

.float-score {
  position: fixed;
  transform: translate(-50%, 0);
  font-size: 20px;
  font-weight: 900;
  color: var(--accent);
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.6);
  animation: floatUp 0.9s ease forwards;
}
@keyframes floatUp {
  0%   { opacity: 0; transform: translate(-50%, 6px) scale(0.8); }
  25%  { opacity: 1; transform: translate(-50%, -10px) scale(1.1); }
  100% { opacity: 0; transform: translate(-50%, -56px) scale(1); }
}

.combo-pop {
  position: absolute;
  left: 50%;
  top: 40%;
  transform: translate(-50%, -50%);
  font-size: 34px;
  font-weight: 900;
  letter-spacing: -1px;
  color: var(--accent-2);
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.4);
  opacity: 0;
  pointer-events: none;
  z-index: 300;
}
.combo-pop.show { animation: comboIn 0.85s ease forwards; }
@keyframes comboIn {
  0%   { opacity: 0; transform: translate(-50%, -50%) scale(0.4) rotate(-8deg); }
  30%  { opacity: 1; transform: translate(-50%, -50%) scale(1.15) rotate(3deg); }
  70%  { opacity: 1; transform: translate(-50%, -60%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -90%) scale(0.9); }
}

/* ── modals ──────────────────────────────────────────────── */

.modal-root {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  padding: clamp(12px, 4vw, 20px);
  background: rgba(6, 8, 26, 0.72);
  backdrop-filter: blur(6px);
  z-index: 600;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.modal-root.show { display: grid; }

/* A sheet taller than the window scrolls inside itself — on a landscape
   phone the win sheet is easily taller than the whole viewport. */
.modal {
  display: none;
  width: 100%;
  max-width: 400px;
  max-height: 100%;
  overflow: hidden auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.28) transparent;
}
.modal.show { display: block; animation: sheetIn 0.28s cubic-bezier(0.2, 1.1, 0.35, 1); }
@keyframes sheetIn { from { opacity: 0; transform: translateY(26px) scale(0.94); } }

.sheet {
  padding: clamp(18px, 4vh, 26px) clamp(14px, 4vw, 20px) 20px;
  text-align: center;
  border-radius: 26px;
  background: linear-gradient(180deg, #2a2560, #1a1743);
  border: 1px solid var(--panel-line);
  box-shadow: 0 26px 60px rgba(0, 0, 0, 0.6);
}
.sheet h2 { margin: 0 0 6px; font-size: clamp(22px, 6.4vw, 28px); font-weight: 900; }
.sheet-sub { margin: 0 0 18px; color: var(--ink-dim); font-size: 14px; font-weight: 600; }
.sheet .btn + .btn { margin-top: 9px; }

.burst { font-size: 54px; line-height: 1; animation: bob 2s ease-in-out infinite; }
.burst.sad { animation: none; }

.stars { display: flex; justify-content: center; gap: 8px; margin: 12px 0 16px; }
.stars i {
  font-size: 40px;
  font-style: normal;
  color: rgba(255, 255, 255, 0.16);
  transition: transform 0.3s ease, color 0.3s ease;
}
.stars i.on { color: var(--accent); text-shadow: 0 0 18px rgba(255, 203, 69, 0.6); transform: scale(1.12); }
.stars i.on:nth-child(2) { transition-delay: 0.12s; }
.stars i.on:nth-child(3) { transition-delay: 0.24s; }

.reward-row { display: flex; gap: 8px; margin-bottom: 18px; }
.reward {
  flex: 1;
  padding: 10px 4px;
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.24);
}
.reward span { display: block; font-size: 19px; font-weight: 900; }
.reward small { display: block; font-size: 10px; letter-spacing: 1px; text-transform: uppercase; color: var(--ink-dim); }

/* ── shop ────────────────────────────────────────────────── */

.shop-list { display: grid; gap: 9px; margin-bottom: 16px; }
.shop-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px;
  text-align: left;
  border-radius: 16px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}
.shop-item.disabled { opacity: 0.45; }
.shop-ico { font-size: 26px; }
.shop-name { flex: 1; font-weight: 800; font-size: 16px; }
.shop-name small { display: block; font-weight: 600; font-size: 11px; color: var(--ink-dim); }
.shop-cost { font-weight: 900; color: var(--accent); }

/* ── settings ────────────────────────────────────────────── */

.toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 14px;
  margin-bottom: 9px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  font-weight: 800;
  cursor: pointer;
}
.toggle input { width: 20px; height: 20px; accent-color: var(--accent); }

/* ── toast ───────────────────────────────────────────────── */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(90px + var(--safe-b));
  transform: translate(-50%, 14px);
  max-width: 88%;
  padding: 11px 18px;
  border-radius: 99px;
  background: rgba(10, 12, 32, 0.94);
  border: 1px solid var(--panel-line);
  font-size: 14px;
  font-weight: 700;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 700;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ═══════════════ responsive breakpoints ═══════════════════
   Ordered narrow → wide so later, more specific rules win.
   ────────────────────────────────────────────────────────── */

/* ── very small phones (iPhone SE, 320–360 px) ───────────── */

@media (max-width: 360px) {
  :root { --pad-x: 7px; }
  .hud { gap: 5px; }
  .icon-btn { width: 38px; height: 38px; font-size: 27px; }
  .tray { gap: 3px; padding: 5px; }
  .powerups { gap: 6px; }
  .home-stats, .home-row, .reward-row { gap: 6px; }
  .stat { padding: 10px 4px; }
  .shop-ico { font-size: 22px; }
  .shop-name { font-size: 15px; }
}

/* ── tall phones: room to breathe ────────────────────────── */

@media (min-height: 800px) {
  .powerups { padding-top: 14px; }
  .coach.show { max-height: 84px; }
}

/* ── short screens, any orientation ──────────────────────── */

@media (max-height: 620px) {
  .logo { margin-bottom: 8px; }
  .title { margin: 2px 0; }
  .burst { font-size: 40px; }
  .stars { margin: 8px 0 12px; }
  .stars i { font-size: 32px; }
  .sheet .btn + .btn { margin-top: 7px; }
  .toast { bottom: calc(56px + var(--safe-b)); }
}

/* ── landscape: side-by-side instead of one tall column ────
   The board takes the height it can get and the chrome moves into a
   sidebar, so a phone held sideways still gets a square-ish play area
   rather than a letterbox slot. Pure layout — every coordinate in
   render.js comes from getBoundingClientRect, so nothing in JS cares.
   ────────────────────────────────────────────────────────── */

@media (orientation: landscape) and (max-height: 700px) {
  :root { --app-w: 1024px; }

  #screen-game.is-active {
    display: grid;
    /* The sidebar is clamped so it stays a sidebar on a wide window rather
       than growing into a second half of the screen. */
    grid-template-columns: minmax(0, 1fr) clamp(250px, 34%, 360px);
    grid-template-rows: auto auto auto auto 1fr;
    grid-template-areas:
      "board hud"
      "board prog"
      "board tray"
      "board coach"
      "board powers";
    column-gap: 12px;
  }
  #screen-game .hud       { grid-area: hud; padding-bottom: 6px; }
  #screen-game .progress  { grid-area: prog; }
  #screen-game .tray-wrap { grid-area: tray; }
  #screen-game .coach     { grid-area: coach; }
  #screen-game .board     { grid-area: board; margin: 0; }
  #screen-game .powerups  { grid-area: powers; align-self: end; padding-top: 8px; }

  .coach.show { max-height: 64px; font-size: 13px; }

  /* Recentre the combo burst over the board column, not the whole screen —
     the same clamp as the sidebar, so the two can't drift apart. */
  .combo-pop {
    left: calc((100% - clamp(250px, 34%, 360px)) / 2);
    font-size: 28px;
  }

  /* Home: brand on the left, controls on the right. */
  .home-inner {
    display: grid;
    grid-template-columns: 1fr minmax(220px, 1fr);
    grid-template-areas:
      "logo  stats"
      "title play"
      "tag   row";
    align-items: center;
    column-gap: 22px;
    padding: 10px 16px;
  }
  .home-inner .logo      { grid-area: logo; margin-bottom: 0; }
  .home-inner .title     { grid-area: title; }
  .home-inner .tagline   { grid-area: tag; margin-bottom: 0; }
  .home-inner .home-stats{ grid-area: stats; margin-bottom: 0; }
  .home-inner .btn-play  { grid-area: play; }
  .home-inner .home-row  { grid-area: row; margin-top: 0; }
  .title { font-size: clamp(32px, 7vw, 52px); }
  .logo-tile { width: clamp(40px, 7vw, 56px); height: clamp(40px, 7vw, 56px); font-size: clamp(20px, 3.4vw, 28px); }

  /* A sideways phone is barely 350 px tall, so the sheets tighten up until
     the primary button lands above the fold rather than behind a scroll. */
  .modal { max-width: 460px; }
  .sheet { padding: 14px 16px; }
  .sheet h2 { font-size: 21px; }
  .sheet-sub { margin-bottom: 12px; }
  .burst { font-size: 30px; }
  .stars { margin: 4px 0 10px; }
  .stars i { font-size: 26px; }
  .reward-row { margin-bottom: 10px; }
  .reward { padding: 7px 4px; }
  .reward span { font-size: 16px; }
  .reward small { font-size: 9px; }
  .sheet .btn { padding: 10px 14px; font-size: 15px; }
  .sheet .btn-play, .sheet .btn + .btn { margin-top: 6px; }
  .shop-item { padding: 9px 10px; }
  .shop-list { gap: 7px; margin-bottom: 12px; }
  .toggle { padding: 10px 12px; margin-bottom: 7px; }
}

/* ── tablet & desktop: centre the column in a framed panel ─
   The game is portrait by design, so widening it further would only
   stretch the artwork. Instead the column is capped in both axes and
   presented as a panel, which also stops tiles from ballooning on a
   very tall monitor.
   ────────────────────────────────────────────────────────── */

@media (min-width: 720px) and (min-height: 701px) {
  :root { --app-w: 600px; }

  body { display: grid; place-items: center; }

  #app {
    height: 100%;
    height: min(100dvh - 40px, 940px);
    border-radius: 30px;
    overflow: hidden;
    border: 1px solid rgba(255, 255, 255, 0.15);
    box-shadow: 0 30px 90px rgba(0, 0, 0, 0.6),
                inset 0 1px 0 rgba(255, 255, 255, 0.12);
  }

  /* No notch to dodge inside a windowed panel. */
  .screen { padding: 16px 16px 14px; }
  .toast { bottom: 40px; }
}

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