/* Merge — matches the games hub palette. Dark by default, light via prefers-color-scheme. */

:root {
  color-scheme: dark;
  --font-display: 'Bricolage Grotesque', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --font-body: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;

  --bg: #0d0d10;
  --surface: #17171c;
  --surface-2: #222229;
  --text: #f5f5f7;
  --muted: #a1a1aa;
  --line: #2a2a31;
  --accent: #b18cff;
  --accent-2: #3dd6d0;
  --gold: #ffd23f;
  --danger: #ff5d5d;
  --focus: #ffd23f;

  --board-bg: #17171c;
  --slot: #222229;

  /* Tile ramp: deep violet and teal, then lit-up pastels, then gold. Each has an ink color. */
  --t2: #37325a;    --t2-ink: #e4dfff;
  --t4: #4b3f8a;    --t4-ink: #f1edff;
  --t8: #17707a;    --t8-ink: #ffffff;
  --t16: #2a6fc0;   --t16-ink: #ffffff;
  --t32: #6a4fd8;   --t32-ink: #ffffff;
  --t64: #a53bb0;   --t64-ink: #ffffff;
  --t128: #3dd6d0;  --t128-ink: #04201f;
  --t256: #5aa9ff;  --t256-ink: #061428;
  --t512: #b18cff;  --t512-ink: #170a33;
  --t1024: #ff8fd0; --t1024-ink: #2a0619;
  --t2048: #ffd23f; --t2048-ink: #1a1400;
  --tbig: #f5f5f7;  --tbig-ink: #0d0d10;
  --glow: rgba(255, 210, 63, 0.45);

  --slide-ms: 110ms;
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg: #f6f6f3;
    --surface: #ffffff;
    --surface-2: #ececea;
    --text: #141416;
    --muted: #55555e;
    --line: #dcdcd6;
    --accent: #6b46c1;
    --accent-2: #0f8b86;
    --focus: #141416;

    --board-bg: #dedcd5;
    --slot: #ecebe6;

    --t2: #dcd5ff;    --t2-ink: #2b2450;
    --t4: #c3b5fb;    --t4-ink: #241c4a;
    --t8: #9fe3df;    --t8-ink: #06302e;
    --t16: #9ccbff;   --t16-ink: #0a2a52;
    --t32: #b39af7;   --t32-ink: #1e0d47;
    --t64: #f0a6dc;   --t64-ink: #3d0a30;
    --t128: #0b7a75;  --t128-ink: #ffffff;
    --t256: #1f6fd1;  --t256-ink: #ffffff;
    --t512: #6b46c1;  --t512-ink: #ffffff;
    --t1024: #b8307f; --t1024-ink: #ffffff;
    --t2048: #ffc61a; --t2048-ink: #1a1400;
    --tbig: #141416;  --tbig-ink: #f6f6f3;
    --glow: rgba(224, 160, 0, 0.45);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  min-height: 100dvh;
  background:
    radial-gradient(60rem 30rem at 15% -10%, rgba(177, 140, 255, 0.14), transparent 60%),
    radial-gradient(50rem 28rem at 110% 0%, rgba(61, 214, 208, 0.10), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.app {
  max-width: 1100px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(32px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.topbar {
  width: 100%;
  max-width: 560px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
}
.back {
  justify-self: start;
  display: inline-flex; align-items: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-weight: 600; font-size: 0.95rem;
  padding: 6px 8px 6px 2px; border-radius: 10px;
}
.back:hover { color: var(--text); }
.back svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.logo {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(1.5rem, 6vw, 2rem);
  letter-spacing: -0.03em;
  margin: 0;
}

/* ---- HUD -------------------------------------------------------------- */

.hud {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  width: min(100%, 480px);
}
.stat {
  position: relative;
  display: inline-flex; flex-direction: column; align-items: flex-start;
  min-width: 104px;
  padding: 6px 14px 7px; border-radius: 12px;
  background: var(--surface); border: 1px solid var(--line);
  font: 800 1.25rem / 1.15 var(--font-display);
  font-variant-numeric: tabular-nums;
}
.stat:last-child { align-items: flex-end; }
.stat-label {
  font: 700 0.68rem / 1.2 var(--font-body);
  letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--muted);
}
.gain {
  position: absolute; right: 12px; top: 4px;
  font: 800 0.95rem var(--font-display);
  color: var(--accent-2);
  pointer-events: none;
  animation: gain 700ms ease-out forwards;
}
@keyframes gain {
  from { opacity: 1; transform: translateY(0); }
  to { opacity: 0; transform: translateY(-26px); }
}

.reset {
  width: 52px; height: 52px; border-radius: 16px; cursor: pointer;
  background: var(--surface); border: 1px solid var(--line);
  display: grid; place-items: center;
  transition: transform 120ms ease;
}
.reset svg, .undo svg {
  width: 22px; height: 22px; fill: none; stroke: currentColor;
  stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round;
}
.reset:hover { border-color: var(--muted); }
.reset:active { transform: scale(0.94); }

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

.board-wrap {
  width: min(100%, 480px);
  container-type: inline-size;
  padding: 4px 0;
}
.board {
  /* Fallback sizing from the viewport; replaced by container units where supported. */
  --w: min(100vw - 32px, 480px);
  --gap: calc(var(--w) * 0.028);
  --cell: calc((var(--w) - 5 * var(--gap)) / 4);
  position: relative;
  width: var(--w);
  height: var(--w);
  border-radius: calc(var(--gap) * 2.4);
  background: var(--board-bg);
  box-shadow: 0 0 0 1px var(--line);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
@supports (width: 1cqi) {
  .board { --w: 100cqi; }
}
.board:focus-visible { outline-offset: 4px; }

.slots, .tiles { position: absolute; inset: 0; }
.slots {
  display: grid;
  grid-template-columns: repeat(4, var(--cell));
  grid-template-rows: repeat(4, var(--cell));
  gap: var(--gap);
  padding: var(--gap);
}
.slots span { border-radius: calc(var(--cell) * 0.14); background: var(--slot); }

.tile {
  --x: 0; --y: 0;
  position: absolute; left: var(--gap); top: var(--gap);
  width: var(--cell); height: var(--cell);
  transform: translate(calc(var(--x) * (var(--cell) + var(--gap))), calc(var(--y) * (var(--cell) + var(--gap))));
  transition: transform var(--slide-ms) ease-out;
  z-index: 1;
}
.tile.merged { z-index: 2; }
.tile-face {
  width: 100%; height: 100%;
  display: grid; place-items: center;
  border-radius: calc(var(--cell) * 0.14);
  background: var(--bg-t, var(--tbig));
  color: var(--ink, var(--tbig-ink));
  box-shadow: inset 0 calc(var(--cell) * -0.05) 0 rgba(0, 0, 0, 0.18);
  font: 800 calc(var(--cell) * 0.44) / 1 var(--font-display);
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
}
.tile[data-digits="3"] .tile-face { font-size: calc(var(--cell) * 0.36); }
.tile[data-digits="4"] .tile-face { font-size: calc(var(--cell) * 0.29); }
.tile[data-digits="5"] .tile-face { font-size: calc(var(--cell) * 0.23); }
.tile[data-digits="6"] .tile-face { font-size: calc(var(--cell) * 0.19); }

.v2 { --bg-t: var(--t2); --ink: var(--t2-ink); }
.v4 { --bg-t: var(--t4); --ink: var(--t4-ink); }
.v8 { --bg-t: var(--t8); --ink: var(--t8-ink); }
.v16 { --bg-t: var(--t16); --ink: var(--t16-ink); }
.v32 { --bg-t: var(--t32); --ink: var(--t32-ink); }
.v64 { --bg-t: var(--t64); --ink: var(--t64-ink); }
.v128 { --bg-t: var(--t128); --ink: var(--t128-ink); }
.v256 { --bg-t: var(--t256); --ink: var(--t256-ink); }
.v512 { --bg-t: var(--t512); --ink: var(--t512-ink); }
.v1024 { --bg-t: var(--t1024); --ink: var(--t1024-ink); }
.v2048 { --bg-t: var(--t2048); --ink: var(--t2048-ink); }
.v2048 .tile-face { box-shadow: 0 0 calc(var(--cell) * 0.28) var(--glow), inset 0 calc(var(--cell) * -0.05) 0 rgba(0, 0, 0, 0.14); }

.tile.new .tile-face { animation: appear 160ms ease-out 70ms backwards; }
.tile.merged .tile-face { animation: pop 170ms ease-out; }
@keyframes appear {
  from { opacity: 0; transform: scale(0.3); }
  to { opacity: 1; transform: scale(1); }
}
@keyframes pop {
  0% { transform: scale(1); }
  45% { transform: scale(1.16); }
  100% { transform: scale(1); }
}

.board.over .tiles { opacity: 0.55; transition: opacity 300ms ease; }

/* ---- Below the board -------------------------------------------------- */

.controls {
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  max-width: 420px; text-align: center;
}
.undo {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 9px 18px 9px 14px; border-radius: 999px; cursor: pointer;
  background: var(--surface); border: 1px solid var(--line);
  font-weight: 700;
}
.undo svg { width: 18px; height: 18px; }
.undo:hover:not(:disabled) { border-color: var(--muted); }
.undo:disabled { cursor: default; color: var(--muted); opacity: 0.6; }
.hint { margin: 0; color: var(--muted); font-size: 0.9rem; }

.status-row {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 6px 12px;
  min-height: 2.2em; text-align: center;
}
.status {
  margin: 0;
  font: 800 1.25rem var(--font-display); letter-spacing: -0.01em;
}
.status.win { color: var(--accent); }
.status.lose { color: var(--danger); }
.keep {
  padding: 7px 16px; border-radius: 999px; cursor: pointer;
  border: 0; background: var(--accent); color: #170a33;
  font-weight: 800;
}
@media (prefers-color-scheme: light) { .keep { color: #ffffff; } }
.keep[hidden] { display: none; }
.best { margin: -10px 0 0; color: var(--muted); font-size: 0.9rem; min-height: 1.4em; }

@media (prefers-reduced-motion: reduce) {
  .reset, .tile, .board.over .tiles { transition: none; }
  .tile.new .tile-face, .tile.merged .tile-face { animation: none; }
}
