/* ===========================================================================
   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; }

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. */
  background-image:
    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;
  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, 13vh, 7rem);
  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. */
.tools { margin-top: auto; }

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

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

.board-wrap { position: relative; flex: 0 0 auto; }

.board {
  position: relative;
  aspect-ratio: var(--cols) / var(--rows);
  width: 100%;
  padding: var(--gap);
  border-radius: var(--radius-tray);
  background: var(--tray);
  box-shadow:
    var(--inset-tray),
    0 0 0 1px var(--tray-edge),
    0 1px 0 0 oklch(1 0 0 / 0.06);
  touch-action: none;
  outline: none;
}

.board:focus-visible { box-shadow: var(--inset-tray), 0 0 0 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;
}
.board__stripes i { background: var(--surface-cell); border-radius: var(--radius-sm); }
.board__stripes i:nth-child(even) { background: var(--surface-cell-alt); }

/* Pressure gradient. --danger-level (0–1) is set by render.js from how close
   the stack is to the top; it is information, not decoration. */
.board__danger {
  position: absolute;
  inset: var(--gap);
  border-radius: calc(var(--radius-tray) - var(--gap));
  background: linear-gradient(to bottom,
    oklch(0.63 0.20 25 / calc(0.34 * var(--danger-level, 0))) 0%,
    oklch(0.63 0.20 25 / calc(0.10 * var(--danger-level, 0))) 38%,
    transparent 68%);
  transition: opacity var(--dur-slow) var(--ease-out-quint);
  pointer-events: 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);
}

.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 calc(var(--cell) * 0.055) 0 oklch(1 0 0 / 0.20),  /* top light  */
    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%;
}

.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; }
.block[data-len="5"] { --num-scale: 0.20; }

/* 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 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(1 0 0 / 0.22); }

/* 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 calc(var(--cell) * 0.06) 0 oklch(1 0 0 / 0.28),
    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 calc(var(--cell) * 0.06) 0 oklch(1 0 0 / 0.34),
    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); }

/* --- 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) {
  /* 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%;
    /* A wider frame here does double duty: the board gets real presence on a
       big screen, and because the board is square, the extra width also eats
       the spare vertical space that would otherwise sit empty above it. */
    max-width: 33rem;
    min-height: min(100dvh, 880px);
    border-radius: 32px;
    box-shadow:
      0 0 0 1px oklch(1 0 0 / 0.05),
      0 40px 120px -40px oklch(0 0 0 / 0.9);
  }
}

@media (prefers-reduced-motion: reduce) {
  .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; }
}
