/* ===========================================================================
   Number Merge — board, blocks, motion
   Blocks are lit objects sitting in a machined tray, not flat rectangles.
   That reading is carried by three things: a top inner highlight, a solid
   bottom edge in the block's own dark shade, and a cast shadow onto the tray.
   =========================================================================== */

*, *::before, *::after { box-sizing: border-box; }

/* Any rule that sets `display` beats the `hidden` attribute's UA default, so
   an element toggled with `el.hidden = true` stays stubbornly visible. Several
   controls here are flex, so make the attribute authoritative. */
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--room);
  color: var(--ink);
  font-family: var(--font-display);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  /* A faint pool of light behind the board: the room is lit by the game. */
  /* Two pools rather than one: the tall one reaches up into the space above
     the board, which otherwise read as a blank page instead of a dark room. */
  background-image:
    radial-gradient(90% 72% at 50% 84%, var(--glow) 0%, transparent 70%),
    radial-gradient(120% 62% at 50% 76%, var(--glow) 0%, transparent 62%),
    linear-gradient(to bottom, var(--room) 0%, var(--room-deep) 100%);
  background-attachment: fixed;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}

/* --- App frame ------------------------------------------------------------ */

.app {
  position: relative;   /* the toast anchors inside the frame */
  /* --cell is written by render.js on resize (px); --step derives from it.
     Declared here rather than on .board so the staging block, which is a
     sibling of the board, lines up with the same column grid. */
  --cell: 3rem;
  --step: calc(var(--cell) + var(--gap));

  display: flex;
  flex-direction: column;
  gap: var(--s-4);
  min-height: 100dvh;
  max-width: 30rem;
  /* Reach. The cabinet used to sit hard on the bottom of the frame, and on a
     tall phone the thumb's natural resting line is about one block higher
     than that (in play, level with the second row). So the whole anchored
     group is lifted by exactly one cell, using the same --cell the board is
     built from, so it stays one block at any width. Only where there is the
     headroom to spare: a short screen is already full, and the desktop frame
     is centred as a whole and does not have a thumb to reach it. */
  /* Paint-only clip. The tray's under-glow is an absolutely positioned
     pseudo-element that reaches below the board, and on a short phone that
     reach extended the document by 28px and made the page scroll. `clip`
     stops that without becoming a scroll container, and leaves the fixed
     2048 bloom (which is not clipped by ancestors) exactly as it is. */
  overflow: clip;
  margin-inline: auto;
  padding:
    calc(env(safe-area-inset-top) + var(--s-8))
    calc(env(safe-area-inset-right) + var(--s-10))
    calc(env(safe-area-inset-bottom) + var(--s-8))
    calc(env(safe-area-inset-left) + var(--s-10));
}

/* The staging area holds the waiting block just above the board. It is capped
   rather than greedy — the board should sit thumb-low, but a screen that is
   two-thirds empty reads as unfinished rather than as breathing room. */
.stage {
  position: relative;
  flex: 0 0 auto;
  height: clamp(4.75rem, 15vh, 8.5rem);
  display: grid;
  place-items: end center;
}

/* Spare height collects above the tools row, not below it, so undo/hold/next
   ride down next to the board and stay inside thumb reach. */
/* The whole instrument panel is one bottom-anchored cabinet: score, ladder,
   queue, staging area, board, footer. Anchoring the TOOLS row instead (the
   first version) kept the board in thumb reach but left the score orphaned at
   the top with all the viewport's slack dumped between it and Next. The slack
   now sits above the score, where it reads as headroom rather than as a hole
   in the UI. Thumb reach is unchanged: nothing below the score has moved. */
.hud { margin-top: auto; }

.stage__rail { position: absolute; inset: 0; pointer-events: none; }

/* --- Board ---------------------------------------------------------------- */

.board-wrap { position: relative; flex: 0 0 auto; }
/* The tray sits in the light it casts: one soft pool beneath the board in the
   palette's own glow, so the cabinet reads as lit from within rather than cut
   out and pasted onto black. Static, one blurred ellipse, costs nothing. */
.board-wrap::before {
  content: '';
  position: absolute;
  inset: -10% -8% -16%;
  z-index: -1;
  border-radius: 50%;
  background: var(--glow);
  filter: blur(28px);
  pointer-events: none;
}

/* --- The 2048 moment ------------------------------------------------------
   Every other effect lights a cell. This one lights the room: a single slow
   bloom in the new block's own colour, rising from the board and gone in just
   over a second. It is the number the game is named after, so it is the one
   moment allowed to reach past the tray.

   A real element rather than a pseudo, driven from render.js with the Web
   Animations API: the colour is dynamic (the new block's own tier), a second
   2048 inside the same second must restart it rather than stack, and WAAPI
   gives both of those without a class dance or a cascade to argue with. */
.bloom {
  position: fixed;
  inset: 0;
  z-index: 3;
  pointer-events: none;
  background: radial-gradient(70% 55% at 50% 72%, var(--bloom, transparent) 0%, transparent 70%);
  opacity: 0;
}

.board {
  position: relative;
  aspect-ratio: var(--cols) / var(--rows);
  width: 100%;
  padding: var(--gap);
  border-radius: var(--radius-tray);
  background: var(--tray);
  /* A machined well: the floor falls away under an inner shadow, a dark
     hairline cuts the edge, and a narrow bezel in the cell colour stands
     proud of the room with its top face in shade and its bottom lip catching
     the light. The edge is drawn by the object, not by an outline. */
  box-shadow: var(--tray-well);
  touch-action: none;
  outline: none;
}

/* Keyboard players only. See main.js: a dialog's own autofocus counts as
   keyboard focus in Chrome and carries over to the board, which put a
   permanent teal ring round the tray for touch players. */
html[data-input="keys"] .board:focus-visible { box-shadow: var(--tray-well), 0 0 0 calc(var(--bezel) + 3px) var(--accent); }

/* Alternating column stripes — the source game does this and it genuinely
   helps you aim, so it stays. */
.board__stripes {
  position: absolute;
  inset: var(--gap);
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  gap: var(--gap);
  border-radius: calc(var(--radius-tray) - var(--gap));
  overflow: hidden;
}
/* --d (0 to 1) is this lane's own pressure, written by render.js from how
   close its stack is to the lip. Registered so it can ease rather than jump. */
@property --d { syntax: '<number>'; inherits: false; initial-value: 0; }
.board__stripes i {
  background-color: var(--surface-cell);
  /* Two layers: the lip of this lane heating to a hard line, and a wash
     falling a cell and a half below it. Both scale with the lane's own level. */
  background-image:
    linear-gradient(to bottom,
      oklch(0.66 0.21 25 / calc(1.0 * var(--d, 0))) 0, oklch(0.66 0.21 25 / calc(1.0 * var(--d, 0))) 2px, transparent 2px),
    linear-gradient(to bottom,
      oklch(0.63 0.20 25 / calc(0.44 * var(--d, 0))) 0%,
      oklch(0.63 0.20 25 / calc(0.15 * var(--d, 0))) 20%,
      transparent 38%);
  border-radius: var(--radius-sm);
  transition: --d var(--dur-slow) var(--ease-out-quint);
}
.board__stripes i:nth-child(even) { background-color: var(--surface-cell-alt); }

/* Pressure used to be a red wash over the whole board, which turned the tray
   brown as soon as any stack was tall and said nothing about where the
   trouble was. It now lives in each lane (above). This element is kept only
   so the board's markup is unchanged; --danger-level is still written by
   render.js as the worst lane's level, for anything that wants it. */
.board__danger { display: none; }

/* Aim guide for the targeted column. */
.board__aim {
  position: absolute;
  top: var(--gap);
  left: var(--gap);
  width: var(--cell);
  height: calc(100% - var(--gap) * 2);
  border-radius: var(--radius-sm);
  background: linear-gradient(to bottom,
    oklch(1 0 0 / 0.10) 0%, oklch(1 0 0 / 0.03) 55%, oklch(1 0 0 / 0.09) 100%);
  box-shadow: inset 0 0 0 1.5px oklch(1 0 0 / 0.16);
  transform: translate3d(calc(var(--aim-col, 0) * var(--step)), 0, 0);
  transition: transform var(--dur-fast) var(--ease-out-quint), opacity var(--dur-fast) linear;
  opacity: 0;
  pointer-events: none;
}
.board.is-aiming .board__aim { opacity: 1; }
.board.is-blocked .board__aim {
  background: var(--danger-soft);
  box-shadow: inset 0 0 0 1.5px oklch(0.63 0.20 25 / 0.55);
}

/* Landing preview: an outline on the exact cell the waiting block will end up
   in. For a plain block that is the lowest empty cell; for a Phantom it is the
   floor, because a Phantom slides in underneath. Showing the destination is a
   far stronger signal than decorating the block itself — you cannot miss an
   outline sitting at the bottom of a stacked column. */
.board__ghost {
  position: absolute;
  top: var(--gap);
  left: var(--gap);
  width: var(--cell);
  height: var(--cell);
  border-radius: var(--radius-block);
  transform: translate3d(calc(var(--ghost-col, 0) * var(--step)),
                         calc(var(--ghost-row, 0) * var(--step)), 0);
  box-shadow: inset 0 0 0 2px oklch(1 0 0 / 0.30);
  transition: transform var(--dur-fast) var(--ease-out-quint), opacity var(--dur-fast) linear;
  opacity: 0;
  pointer-events: none;
  z-index: var(--z-board);
}
.board.is-aiming .board__ghost { opacity: 1; }
.board.is-blocked .board__ghost { opacity: 0; }

/* A Phantom's destination is announced loudly: its own colour, a dashed rim
   and a slow pulse, so "this one goes to the bottom" reads at a glance. */
.board.is-phantom-aim .board__ghost {
  /* A Phantom's destination is the floor, which is nearly always already
     occupied — it pushes that block up. So this one has to draw OVER the
     stack, not under it, or the whole signal is hidden behind a block. */
  z-index: var(--z-fx);
  box-shadow: inset 0 0 0 3px var(--t-phantom), 0 0 calc(var(--cell) * 0.5) var(--t-phantom);
  outline: 2px dashed oklch(1 0 0 / 0.6);
  outline-offset: calc(var(--cell) * -0.16);
  animation: ghostPulse 1.5s ease-in-out infinite;
}
@keyframes ghostPulse { 0%, 100% { opacity: 0.55; } 50% { opacity: 1; } }

.board__blocks { position: absolute; inset: var(--gap); }

/* --- Blocks --------------------------------------------------------------- */

.block {
  position: absolute;
  top: 0; left: 0;
  width: var(--cell);
  height: var(--cell);
  transform: translate3d(calc(var(--col) * var(--step)), calc(var(--row) * var(--step)), 0);
  z-index: var(--z-block);
  will-change: transform;
}

/* Falling is the only place an accelerating curve is right. Duration is set
   per-block from JS because it is distance-proportional. */
.block.is-falling { transition: transform var(--fall-dur, 200ms) var(--ease-fall); }

.block__face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: var(--radius-block);
  background: var(--block);
  box-shadow:
    inset 0 1px 0 oklch(1 0 0 / 0.45),                        /* crisp reflection at the lip */
    inset 0 calc(var(--cell) * 0.055) 0 oklch(1 0 0 / 0.22),  /* top light  */
    inset 1px 0 0 oklch(1 0 0 / 0.10),                        /* bevel, lit side   */
    inset -1px 0 0 oklch(0 0 0 / 0.16),                       /* bevel, shade side */
    inset 0 calc(var(--cell) * -0.06) 0 var(--block-dk),      /* base shade */
    0 calc(var(--cell) * 0.05) 0 var(--block-dk),             /* thickness  */
    0 calc(var(--cell) * 0.09) calc(var(--cell) * 0.16) oklch(0 0 0 / 0.45);
  transform-origin: 50% 100%;
}
/* The fill itself deepens toward the base, the way light falls off through a
   thick cast block. Progressive: without color-mix the flat fill above stands. */
@supports (background: color-mix(in oklch, red, blue)) {
  .block[data-kind="number"] .block__face {
    background: linear-gradient(to bottom,
      var(--block) 0%,
      var(--block) 50%,
      color-mix(in oklch, var(--block) 78%, var(--block-dk)) 100%);
  }
}

/* --- Acrylic ----------------------------------------------------------------
   The face was a flat fill under a hard top line, which reads as painted
   plastic. A backlit acrylic keycap has two lights on it: the room's, from
   above, which lands as a soft specular sweep across the top of a convex
   face, and its own, from behind, which comes up through the material and
   lifts the centre and base. Both are white at low alpha over the tier
   colour, so every palette keeps its hue and nothing needs the relative
   colour syntax. One static pseudo-element per block: no blur, no
   backdrop-filter, no transparency to muddy the colour, nothing that costs
   a frame across twenty-five tiles.

   The Stone is excluded on purpose: it is dead matte, and a highlight would
   promise it does something. The Phantom's face is a hollow ring, so there
   is no surface for light to sit on. Everything else, including the
   near-black Bomb and Vortex, takes it and reads as obsidian. */
.block__face::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  background:
    radial-gradient(30% 24% at 26% 14%,
      oklch(1 0 0 / 0.42) 0%,
      transparent 72%),
    linear-gradient(168deg,
      oklch(1 0 0 / 0.30) 0%,
      oklch(1 0 0 / 0.13) 30%,
      transparent 50%),
    radial-gradient(75% 58% at 50% 104%,
      oklch(1 0 0 / 0.13) 0%,
      transparent 68%);
}
.block[data-kind="phantom"] .block__face::before { display: none; }
/* The Stone gets no light at all. Its surface layer is aggregate instead: a
   scatter of pits and pale flecks, so it reads as poured concrete rather than
   as a greyed-out button. Sized in cell fractions so it holds at any scale. */
.block[data-kind="stone"] .block__face::before {
  background:
    radial-gradient(circle at 22% 28%, oklch(0 0 0 / 0.30) 0 4%, transparent 5%),
    radial-gradient(circle at 71% 19%, oklch(1 0 0 / 0.16) 0 3%, transparent 4%),
    radial-gradient(circle at 83% 58%, oklch(0 0 0 / 0.26) 0 5%, transparent 6%),
    radial-gradient(circle at 36% 74%, oklch(1 0 0 / 0.12) 0 3.5%, transparent 4.5%),
    radial-gradient(circle at 58% 84%, oklch(0 0 0 / 0.22) 0 3%, transparent 4%),
    radial-gradient(circle at 14% 62%, oklch(1 0 0 / 0.10) 0 2.5%, transparent 3.5%),
    radial-gradient(circle at 52% 12%, oklch(0 0 0 / 0.20) 0 2.5%, transparent 3.5%),
    radial-gradient(circle at 90% 86%, oklch(1 0 0 / 0.10) 0 2.5%, transparent 3.5%),
    linear-gradient(155deg, oklch(1 0 0 / 0.07) 0%, transparent 45%, oklch(0 0 0 / 0.14) 100%);
}
/* The numeral and glyph sit on the glass, not under it. */
.block__num, .block__glyph, .block__badge { position: relative; z-index: 1; }

.block__num {
  font-weight: 800;
  font-size: calc(var(--cell) * var(--num-scale, 0.46));
  letter-spacing: var(--tracking-tight);
  color: oklch(1 0 0);
  text-shadow: 0 calc(var(--cell) * 0.022) 0 var(--block-dk);
  line-height: 1;
  user-select: none;
  font-variant-numeric: tabular-nums;
}

/* Digit-count sizing: 2 and 2048 both need to look optically right. */
.block[data-len="1"] { --num-scale: 0.50; }
.block[data-len="2"] { --num-scale: 0.42; }
.block[data-len="3"] { --num-scale: 0.32; }
.block[data-len="4"] { --num-scale: 0.25; }

/* Tier colours. --block / --block-dk are consumed by the rules above. */
.block[data-tier="2"]    { --block: var(--t2);    --block-dk: var(--t2-dk); }
.block[data-tier="4"]    { --block: var(--t4);    --block-dk: var(--t4-dk); }
.block[data-tier="8"]    { --block: var(--t8);    --block-dk: var(--t8-dk); }
.block[data-tier="16"]   { --block: var(--t16);   --block-dk: var(--t16-dk); }
.block[data-tier="32"]   { --block: var(--t32);   --block-dk: var(--t32-dk); }
.block[data-tier="64"]   { --block: var(--t64);   --block-dk: var(--t64-dk); }
.block[data-tier="128"]  { --block: var(--t128);  --block-dk: var(--t128-dk); }
.block[data-tier="256"]  { --block: var(--t256);  --block-dk: var(--t256-dk); }
.block[data-tier="512"]  { --block: var(--t512);  --block-dk: var(--t512-dk); }
.block[data-tier="1024"] { --block: var(--t1024); --block-dk: var(--t1024-dk); }
.block[data-tier="2048"] { --block: var(--t2048); --block-dk: var(--t2048-dk); }
.block[data-tier="4096"] { --block: var(--t4096); --block-dk: var(--t4096-dk); }
.block[data-tier="8192"] { --block: var(--t8192); --block-dk: var(--t8192-dk); }
.block[data-tier="max"]  { --block: var(--tmax);  --block-dk: var(--tmax-dk); }

/* --- Special blocks (Loaded mode) ------------------------------------------
   These carry a glyph instead of a numeral. Each is styled to announce what it
   does before the player has learnt the rules. */

.block__glyph {
  width: calc(var(--cell) * 0.5);
  height: calc(var(--cell) * 0.5);
  display: block;
}

.block[data-kind="wild"]  { --block: var(--t-wild);  --block-dk: var(--t-wild-dk); }
.block[data-kind="bomb"]  { --block: var(--t-bomb);  --block-dk: var(--t-bomb-dk); }
.block[data-kind="stone"] { --block: var(--t-stone); --block-dk: var(--t-stone-dk); }

/* The wild is the prize: bright, lilac, and lit from within. */
.block[data-kind="wild"] .block__face {
  box-shadow:
    inset 0 calc(var(--cell) * 0.055) 0 oklch(1 0 0 / 0.45),
    inset 0 calc(var(--cell) * -0.06) 0 var(--block-dk),
    0 calc(var(--cell) * 0.05) 0 var(--block-dk),
    0 calc(var(--cell) * 0.09) calc(var(--cell) * 0.16) oklch(0 0 0 / 0.45),
    0 0 calc(var(--cell) * 0.55) var(--t-wild);
}
.block[data-kind="wild"] .block__glyph { fill: var(--t-wild-dk); }

/* The bomb is dark and hot — a ring of danger round a near-black body. */
.block[data-kind="bomb"] .block__face {
  box-shadow:
    inset 0 0 0 calc(var(--cell) * 0.045) var(--danger),
    inset 0 calc(var(--cell) * -0.05) 0 var(--block-dk),
    0 calc(var(--cell) * 0.05) 0 var(--block-dk),
    0 calc(var(--cell) * 0.09) calc(var(--cell) * 0.16) oklch(0 0 0 / 0.5),
    0 0 calc(var(--cell) * 0.4) oklch(0.63 0.20 25 / 0.5);
  animation: fuseLit 1.5s ease-in-out infinite;
}
.block[data-kind="bomb"] .block__glyph { fill: var(--danger); }
@keyframes fuseLit { 0%, 100% { filter: brightness(1); } 50% { filter: brightness(1.35); } }

/* The stone is inert: matte, no top highlight, no glow. It should look like
   something that will never do anything for you. */
.block[data-kind="stone"] .block__face {
  box-shadow:
    inset 0 1px 0 oklch(1 0 0 / 0.10),                         /* a chiselled top arris, not a highlight */
    inset 0 calc(var(--cell) * -0.055) 0 var(--block-dk),
    0 calc(var(--cell) * 0.05) 0 var(--block-dk),
    0 calc(var(--cell) * 0.06) calc(var(--cell) * 0.1) oklch(0 0 0 / 0.4);
}
.block[data-kind="stone"] .block__glyph { fill: oklch(0 0 0 / 0.34); filter: drop-shadow(0 1px 0 oklch(1 0 0 / 0.14)); }

/* The Phantom.
   Earlier passes tinted it with its own tier colour and added a dashed rim.
   That was not enough: glanced at quickly it still reads "a 128, pink, normal"
   and gets dropped by mistake. Colour and number are the two things the eye
   grabs first, and both were saying "ordinary block".

   So the SILHOUETTE changes instead. It is hollow rather than solid — a thick
   ring around an empty middle, with no raised keycap base — which is a
   different shape at any size, in peripheral vision, at a glance. The tier
   colour survives in the ring so you can still read the value, and constant
   downward motion catches the eye even when you are not looking straight at it. */
.block[data-kind="phantom"] .block__face {
  background: oklch(from var(--block) l c h / 0.16);
  box-shadow:
    inset 0 0 0 calc(var(--cell) * 0.085) var(--block),
    inset 0 0 calc(var(--cell) * 0.18) oklch(from var(--block) l c h / 0.5),
    0 0 calc(var(--cell) * 0.45) oklch(from var(--block) l c h / 0.55);
  outline: none;
  opacity: 1;
}
.block[data-kind="phantom"] .block__num {
  color: var(--block);
  text-shadow: 0 0 calc(var(--cell) * 0.12) oklch(0 0 0 / 0.8);
}

/* Fallback for engines without relative colour syntax: a flat spectral fill
   still reads as hollow-and-different, just without the tier tint. */
@supports not (color: oklch(from red l c h)) {
  .block[data-kind="phantom"] .block__face {
    background: oklch(0.30 0.03 265 / 0.45);
    box-shadow:
      inset 0 0 0 calc(var(--cell) * 0.085) var(--block),
      0 0 calc(var(--cell) * 0.45) var(--t-phantom);
  }
}

/* The badge becomes a moving arrow rather than a static mark. */
.block[data-kind="phantom"] .block__badge {
  right: 50%;
  bottom: 2%;
  transform: translateX(50%);
  width: calc(var(--cell) * 0.42);
  height: calc(var(--cell) * 0.42);
  fill: var(--block);
  filter: none;
  animation: sinkHint 1.4s ease-in-out infinite;
}
@keyframes sinkHint {
  0%, 100% { transform: translateX(50%) translateY(0); opacity: 0.55; }
  50%      { transform: translateX(50%) translateY(12%); opacity: 1; }
}

/* On the block you are ABOUT to play, spell it out. No glance can misread a
   word. Only on the staging block — a chip is far too small for text.

   The word replaces the arrow there rather than sitting alongside it: label,
   numeral and arrow all at once is three things fighting over one small
   square, and the first attempt had the caption printing straight through
   the number. */
.stage__rail .stage__block[data-kind="phantom"] .block__badge { display: none; }

.stage__rail .stage__block[data-kind="phantom"] .block__num {
  transform: translateY(14%);
}

.stage__rail .stage__block[data-kind="phantom"] .block__face::after {
  content: 'UNDER';
  position: absolute;
  top: 11%;
  left: 0;
  right: 0;
  text-align: center;
  font-family: var(--font-mono);
  font-size: calc(var(--cell) * 0.155);
  font-weight: 700;
  letter-spacing: 0.18em;
  line-height: 1;
  color: var(--block);
  opacity: 0.95;
  pointer-events: none;
}

/* Sliding in underneath: it fades up from below rather than dropping. */
.block.is-phasing .block__face { animation: phase 300ms var(--ease-out-quint); }
@keyframes phase {
  0%   { opacity: 0; transform: translateY(35%) scale(0.9); }
  55%  { opacity: 0.9; }
  100% { opacity: 0.82; transform: none; }
}

/* Blocks being lifted by a Phantom move UP, so they get an ease-out rather
   than the accelerating fall curve — nothing is falling here. */
.block.is-lifting { transition: transform 240ms var(--ease-out-quint); }

/* The Vortex: near-black with a luminous rim, slowly turning. */
/* --- Halver ----------------------------------------------------------------
   Dark body with one hard diagonal blade across the face and a cold green rim.
   It never rests: on landing it cuts (halveCut) and is gone, and the block it
   cut flinches (halved) as its numeral drops to half. */
.block[data-kind="halver"] { --block: var(--t-halver); --block-dk: var(--t-halver-dk); }
.block[data-kind="halver"] .block__face {
  background: linear-gradient(135deg,
    var(--block) 0%, var(--block) 47%,
    var(--t-halver-hi) 47%, var(--t-halver-hi) 53%,
    var(--block) 53%, var(--block) 100%);
  box-shadow:
    inset 0 0 0 max(1.5px, calc(var(--cell) * 0.04)) var(--t-halver-hi),   /* floored: at queue-chip size a cell fraction rounds to nothing */
    inset 0 calc(var(--cell) * -0.05) 0 var(--block-dk),
    0 calc(var(--cell) * 0.05) 0 var(--block-dk),
    0 0 calc(var(--cell) * 0.4) var(--t-halver-glow);
}
.block[data-kind="halver"] .block__glyph { fill: oklch(0.985 0.012 150); }
.block.is-halving .block__face { animation: halveCut 240ms var(--ease-out-expo) forwards; }
@keyframes halveCut {
  0%   { transform: scale(1); opacity: 1; }
  40%  { transform: scale(1.18, 0.66); opacity: 1; }
  100% { transform: scale(0.15, 1.5); opacity: 0; }
}
.block.is-halved .block__face { animation: halved 300ms var(--ease-out-expo); }
@keyframes halved {
  0%   { transform: scale(1); filter: brightness(1); }
  30%  { transform: scale(0.80, 0.86); filter: brightness(1.6); }
  100% { transform: scale(1); filter: brightness(1); }
}

.block[data-kind="vortex"] { --block: var(--t-vortex); --block-dk: var(--t-vortex-dk); }
.block[data-kind="vortex"] .block__face {
  box-shadow:
    inset 0 0 0 calc(var(--cell) * 0.035) oklch(0.72 0.19 300),
    inset 0 calc(var(--cell) * -0.05) 0 var(--block-dk),
    0 calc(var(--cell) * 0.05) 0 var(--block-dk),
    0 0 calc(var(--cell) * 0.55) oklch(0.60 0.20 300 / 0.75);
}
.block[data-kind="vortex"] .block__glyph {
  fill: oklch(0.86 0.13 300);
  animation: spin 3.4s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Blocks being dragged in by a Vortex. */
.block.is-pulled {
  transition: transform 260ms cubic-bezier(0.55, 0, 0.85, 0.35), opacity 260ms linear 90ms;
  opacity: 0;
  z-index: 1;
}
.block.is-pulled .block__face { transform: scale(0.3) rotate(-160deg); transition: transform 260ms var(--ease-out-quint); }

/* A Vortex with nothing to grab: it dissipates instead of resting on the
   board. A special that could turn into permanent concrete would be a trap. */
.block.is-fizzling .block__face {
  animation: fizzle 300ms var(--ease-out-quint) forwards;
}
@keyframes fizzle {
  0%   { transform: scale(1) rotate(0); opacity: 1; }
  100% { transform: scale(0.25) rotate(220deg); opacity: 0; }
}

.block.is-vortexing .block__face { animation: vortexPop 340ms var(--ease-out-expo); }
@keyframes vortexPop {
  0%   { transform: scale(0.7) rotate(-40deg); }
  50%  { transform: scale(1.28) rotate(8deg); }
  100% { transform: scale(1) rotate(0); }
}

/* Detonation: the bomb flares white then collapses. */
.block.is-detonating .block__face {
  animation: detonate 260ms var(--ease-out-expo) forwards;
}
@keyframes detonate {
  0%   { transform: scale(1);   filter: brightness(1); }
  30%  { transform: scale(1.35); filter: brightness(3); }
  100% { transform: scale(0.2);  filter: brightness(1); opacity: 0; }
}

/* Blocks caught in the blast. */
.block.is-destroyed {
  transition: opacity 200ms linear, transform 200ms var(--ease-out-quint);
  opacity: 0;
}
.block.is-destroyed .block__face { transform: scale(0.45) rotate(8deg); transition: transform 200ms var(--ease-out-quint); }

/* From 512 up the block is emissive — rare blocks should feel rare. */
.block[data-glow="1"] .block__face {
  box-shadow:
    inset 0 1px 0 oklch(1 0 0 / 0.48),
    inset 0 calc(var(--cell) * 0.06) 0 oklch(1 0 0 / 0.28),
    inset 1px 0 0 oklch(1 0 0 / 0.10),
    inset -1px 0 0 oklch(0 0 0 / 0.16),
    inset 0 calc(var(--cell) * -0.06) 0 var(--block-dk),
    0 calc(var(--cell) * 0.05) 0 var(--block-dk),
    0 calc(var(--cell) * 0.09) calc(var(--cell) * 0.16) oklch(0 0 0 / 0.45),
    0 0 calc(var(--cell) * 0.42) var(--block);
}
.block[data-glow="2"] .block__face {
  box-shadow:
    inset 0 1px 0 oklch(1 0 0 / 0.52),
    inset 0 calc(var(--cell) * 0.06) 0 oklch(1 0 0 / 0.34),
    inset 1px 0 0 oklch(1 0 0 / 0.10),
    inset -1px 0 0 oklch(0 0 0 / 0.16),
    inset 0 calc(var(--cell) * -0.06) 0 var(--block-dk),
    0 calc(var(--cell) * 0.05) 0 var(--block-dk),
    0 calc(var(--cell) * 0.09) calc(var(--cell) * 0.16) oklch(0 0 0 / 0.45),
    0 0 calc(var(--cell) * 0.75) var(--block);
  animation: breathe 2.6s var(--ease-out-quint) infinite;
}
/* Dark numerals where the block is too light to carry white text. */
.block[data-tier="2048"] .block__num,
.block[data-tier="8192"] .block__num,
.block[data-tier="max"]  .block__num {
  color: var(--block-dk);
  text-shadow: 0 calc(var(--cell) * 0.02) 0 oklch(1 0 0 / 0.35);
}

@keyframes breathe {
  0%, 100% { filter: brightness(1); }
  50%      { filter: brightness(1.12); }
}

/* --- Block motion --------------------------------------------------------- */

/* Squash on landing. Applied to the face so it cannot fight the position
   transform on the parent. */
.block.is-landing .block__face { animation: squash 110ms var(--ease-out-quint); }
@keyframes squash {
  0%   { transform: scale(1, 1); }
  32%  { transform: scale(1.12, 0.84); }
  100% { transform: scale(1, 1); }
}

/* The block that survives a fuse. */
.block.is-fusing .block__face { animation: pop 185ms var(--ease-out-expo); }
@keyframes pop {
  0%   { transform: scale(0.86); }
  46%  { transform: scale(1.20); }
  100% { transform: scale(1); }
}

/* Blocks being absorbed slide into the anchor and vanish. */
.block.is-absorbed {
  transition: transform 130ms var(--ease-out-quint), opacity 130ms linear;
  opacity: 0;
  z-index: 1;
}
.block.is-absorbed .block__face { transform: scale(0.55); transition: transform 130ms var(--ease-out-quint); }

/* A white flash across the newly-created value. */
.block.is-fusing .block__face::after {
  content: '';
  position: absolute; inset: 0;
  border-radius: inherit;
  background: oklch(1 0 0);
  animation: flash 260ms var(--ease-out-expo) forwards;
  pointer-events: none;
}
@keyframes flash { from { opacity: 0.85; } to { opacity: 0; } }

/* --- The block awaiting a drop -------------------------------------------- */

/* The staging block reuses .block for its colour and typography, so this rule
   must out-specify .block's own positioning (which pins top/left to 0). */
.stage__rail .stage__block {
  position: absolute;
  top: auto;
  bottom: 0;
  left: var(--gap);   /* matches the board's inner padding, so columns align */
  width: var(--cell);
  height: var(--cell);
  transform: translate3d(calc(var(--col, 2) * var(--step)), 0, 0);
  transition: transform var(--dur-fast) var(--ease-out-quint);
  will-change: transform;
}
.stage__rail .stage__block.is-launching { transition: none; }

/* A guide dropping from the waiting block toward the board, so the column you
   are about to hit is unambiguous. It fades out rather than stopping dead. */
.stage__block::after {
  content: '';
  position: absolute;
  left: 50%;
  top: calc(100% + 2px);
  width: 3px;
  height: calc(var(--cell) * 0.55);
  transform: translateX(-50%);
  border-radius: 2px;
  background: linear-gradient(to bottom, var(--block) 0%, transparent 100%);
  opacity: 0.42;
}

/* --- Chain multiplier ----------------------------------------------------- */

.chain {
  position: absolute;
  left: 50%;
  top: 0;
  transform: translateX(-50%);
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: var(--text-2xl);
  color: var(--accent);
  opacity: 0;
  pointer-events: none;
  white-space: nowrap;
}
.chain.is-on { animation: chainPop 700ms var(--ease-out-expo); }
@keyframes chainPop {
  0%   { opacity: 0; transform: translateX(-50%) scale(0.7); }
  18%  { opacity: 1; transform: translateX(-50%) scale(1.1); }
  70%  { opacity: 1; transform: translateX(-50%) scale(1); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-14px) scale(1); }
}

/* --- FX canvas ------------------------------------------------------------ */

.fx {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: var(--z-fx);
  pointer-events: none;
}

/* --- Screen shake --------------------------------------------------------- */
/* render.js writes --shake-x / --shake-y each frame while a shake is live. */
.board-wrap { transform: translate3d(var(--shake-x, 0px), var(--shake-y, 0px), 0); }

/* --- Game over ------------------------------------------------------------ */

.board.is-over .board__blocks { filter: saturate(0.35) brightness(0.65); transition: filter 420ms var(--ease-out-quint); }

@media (min-height: 760px) and (max-width: 699px) {
  .app { padding-bottom: calc(env(safe-area-inset-bottom) + var(--s-8) + var(--cell)); }
}

/* --- Desktop ---------------------------------------------------------------
   The layout is built for a phone held in one hand. On a big screen, letting
   it stretch to the full viewport height just opens a void between the header
   and the board, so the frame is capped and centred instead — it reads as a
   cabinet sitting in a dark room rather than a column that failed to fill. */

@media (min-width: 700px) and (min-height: 720px) {
  /* Everything in the cabinet is sized in rem, so the root size is the one
     dial that scales the lot. It follows the viewport's height: a 1080 screen
     gets a board about a fifth larger than a phone's, and half of a 4K
     monitor gets one nearly twice the size, instead of a phone-sized column
     adrift in the middle of it. */
  html { font-size: clamp(16px, 1.7vh, 28px); }

  /* align-content, not place-items: centring the *items* would size the frame
     to its content and quietly discard max-width. This centres the row and
     leaves the frame free to stretch to its own limit. */
  body { display: grid; align-content: center; }

  .app {
    width: 100%;
    /* The board is square, so the frame's width decides its height. Capping
       the width by the height left over after the chrome (about 22rem) means
       the cabinet always fits the window it is in rather than scrolling. */
    max-width: min(33rem, calc(100dvh - 25.5rem));
    min-height: 0;
    padding-block: var(--s-12);
    border-radius: 2rem;
    box-shadow:
      0 0 0 1px oklch(1 0 0 / 0.05),
      0 2.5rem 7.5rem -2.5rem oklch(0 0 0 / 0.9);
  }
  .hud { margin-top: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .block.is-halving .block__face, .block.is-halved .block__face { animation: none; }
  .block.is-landing .block__face,
  .block.is-fusing .block__face,
  .block[data-glow="2"] .block__face { animation: none; }
  .block.is-fusing .block__face::after { animation: flash 120ms linear forwards; }
  .chain.is-on { animation: chainFade 700ms linear; }
  @keyframes chainFade { 0%,80% { opacity: 1; } 100% { opacity: 0; } }
  .board-wrap { transform: none; }
}
