:root {
  --font-display: "Fraunces", "Iowan Old Style", Georgia, serif;
  --font-ui: "Sora", "Avenir Next", system-ui, sans-serif;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --ease-pop: cubic-bezier(0.34, 1.4, 0.64, 1);
  --move: 130ms;
  --gap: clamp(8px, 2.2vw, 12px);
  --radius: 14px;
  --radius-lg: 22px;
}

/* ---------- Themes ---------- */
[data-theme="obsidian"] {
  --bg: #0e0c0a; --bg-2: #161310; --ink: #f4ece2; --muted: #a39484;
  --line: rgba(244, 236, 226, 0.08);
  --surface: #1c1814; --surface-2: #241e18; --board: #1a1612; --slot: #2a241e;
  --accent: #e2a35a; --accent-ink: #1a1208; --glow: rgba(226, 163, 90, 0.45);
  --halo-a: rgba(226, 163, 90, 0.16); --halo-b: rgba(155, 30, 50, 0.18);
  --shadow: 0 30px 80px rgba(0, 0, 0, 0.55);
  --v2: #efe7d8; --v2ink: #4a3c2a; --v4: #e7d3b0; --v4ink: #4a3c2a;
  --v8: #e09a5a; --v8ink: #1a120c; --v16: #d46a3a; --v16ink: #fff8f0;
  --v32: #c43c2c; --v32ink: #fff8f0; --v64: #9b1e32; --v64ink: #fff8f0;
  --v128: #c9a227; --v128ink: #1a1408; --v256: #e0b33a; --v256ink: #1a1408;
  --v512: #f0c14e; --v512ink: #1a1408; --v1024: #f6d76e; --v1024ink: #3a2a08;
  --v2048: #fff1c2; --v2048ink: #5a3a08; --vhi: #2a1210; --vhiink: #ffd9a8;
  color-scheme: dark;
}
[data-theme="porcelain"] {
  --bg: #efe6d6; --bg-2: #e6d9c4; --ink: #2a2118; --muted: #7a6a58;
  --line: rgba(42, 33, 24, 0.1);
  --surface: #f7f1e6; --surface-2: #fffaf2; --board: #d9cbb4; --slot: #cfc0a8;
  --accent: #b4452c; --accent-ink: #fff8f0; --glow: rgba(180, 69, 44, 0.28);
  --halo-a: rgba(255, 250, 240, 0.7); --halo-b: rgba(180, 69, 44, 0.1);
  --shadow: 0 24px 50px rgba(80, 50, 20, 0.16);
  --v2: #fffaf2; --v2ink: #5a4a38; --v4: #f3e6d0; --v4ink: #5a4a38;
  --v8: #e8b48a; --v8ink: #3a2418; --v16: #d97757; --v16ink: #fff8f0;
  --v32: #c45c3e; --v32ink: #fff8f0; --v64: #9b2e1f; --v64ink: #fff8f0;
  --v128: #243f5e; --v128ink: #f3eadc; --v256: #1c334d; --v256ink: #f3eadc;
  --v512: #15263a; --v512ink: #f7e7c2; --v1024: #101c2c; --v1024ink: #f7e7c2;
  --v2048: #0c0c0c; --v2048ink: #e2a35a; --vhi: #3a1020; --vhiink: #ffd0c0;
  color-scheme: light;
}
[data-theme="aurora"] {
  --bg: #070a12; --bg-2: #0b1020; --ink: #e8eef8; --muted: #8b9bb8;
  --line: rgba(160, 200, 255, 0.1);
  --surface: #10182a; --surface-2: #162038; --board: #10182c; --slot: #1a2744;
  --accent: #5eead4; --accent-ink: #04221c; --glow: rgba(94, 234, 212, 0.35);
  --halo-a: rgba(94, 234, 212, 0.14); --halo-b: rgba(167, 139, 250, 0.18);
  --shadow: 0 30px 80px rgba(0, 10, 40, 0.65);
  --v2: #dbeafe; --v2ink: #1e3a5f; --v4: #a5f3fc; --v4ink: #164e63;
  --v8: #5eead4; --v8ink: #042f2e; --v16: #34d399; --v16ink: #022c22;
  --v32: #818cf8; --v32ink: #f5f3ff; --v64: #a78bfa; --v64ink: #f5f3ff;
  --v128: #e879f9; --v128ink: #1a0a22; --v256: #f472b6; --v256ink: #1a0a16;
  --v512: #fb7185; --v512ink: #1a0a10; --v1024: #fbbf24; --v1024ink: #1a1408;
  --v2048: #fef3c7; --v2048ink: #3a2a08; --vhi: #082f49; --vhiink: #a5f3fc;
  color-scheme: dark;
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  min-height: 100dvh;
  font-family: var(--font-ui);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 12% -10%, var(--halo-a), transparent 55%),
    radial-gradient(900px 500px at 110% 10%, var(--halo-b), transparent 50%),
    linear-gradient(180deg, var(--bg-2), var(--bg));
  background-color: var(--bg);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.4s, color 0.4s;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.board:focus { outline: none; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }

.app {
  width: min(100%, 520px);
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) 16px max(28px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ---------- Studio logo ---------- */
.studio {
  display: flex; align-items: center; justify-content: center; gap: 7px;
  margin-bottom: -2px;
  font-size: 10.5px; letter-spacing: 0.2em; text-transform: uppercase; color: var(--muted);
}
.studio svg { width: 16px; height: 16px; flex: none; color: var(--accent); }
.studio b { font-weight: 700; color: var(--ink); }

/* ---------- Header ---------- */
.top { display: flex; align-items: center; gap: 12px; }
.mark {
  width: 40px; height: 40px; flex: none;
  display: grid; grid-template-columns: 1fr 1fr; gap: 3px; padding: 9px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface);
}
.mark span { border-radius: 3px; background: var(--slot); }
.mark span:nth-child(1) { background: var(--v2); }
.mark span:nth-child(4) { background: var(--accent); }
.brand { flex: 1; min-width: 0; }
.brand h1 {
  margin: 0; font-family: var(--font-display); font-weight: 700;
  font-size: clamp(34px, 9vw, 44px); line-height: 0.95; letter-spacing: -0.02em;
}
.tagline { margin-top: 4px; font-size: 10px; letter-spacing: 0.22em; text-transform: uppercase; color: var(--muted); white-space: nowrap; }
.tools { display: flex; align-items: center; gap: 6px; }
.swatches {
  display: flex; gap: 4px; padding: 4px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--line);
}
.swatch {
  width: 20px; height: 20px; padding: 0; border-radius: 50%;
  border: 2px solid transparent; transition: transform 0.2s var(--ease-pop), border-color 0.2s;
}
.swatch[data-theme-id="obsidian"] { background: linear-gradient(135deg, #e2a35a, #3a2414); }
.swatch[data-theme-id="porcelain"] { background: linear-gradient(135deg, #fffaf2, #d97757); }
.swatch[data-theme-id="aurora"] { background: linear-gradient(135deg, #5eead4, #6d5bd0); }
.swatch[aria-pressed="true"] { border-color: var(--accent); transform: scale(1.12); }
.swatch:hover { transform: scale(1.12); }
.icon-btn {
  width: 36px; height: 36px; display: grid; place-items: center; padding: 0;
  border-radius: 11px; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  transition: background 0.2s, transform 0.15s;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn svg { width: 18px; height: 18px; }
#btn-sound .ico-off { display: none; }
#btn-sound.muted .ico-on { display: none; }
#btn-sound.muted .ico-off { display: block; }

/* ---------- Scores ---------- */
.scores { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.score-card {
  position: relative; display: flex; flex-direction: column; align-items: center;
  padding: 10px 6px 9px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--line);
}
.score-card .label { font-size: 9.5px; letter-spacing: 0.2em; text-transform: uppercase; color: var(--muted); }
.score-card .value {
  margin-top: 2px; font-family: var(--font-display); font-weight: 700;
  font-size: 22px; line-height: 1.1; font-variant-numeric: tabular-nums;
}
.bump {
  position: absolute; left: 50%; top: 22px; transform: translateX(-50%);
  font-weight: 700; font-size: 14px; color: var(--accent); pointer-events: none; opacity: 0;
}
.bump.go { animation: bump 0.7s var(--ease-out); }
@keyframes bump {
  0% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, -26px); }
}

/* ---------- Modes ---------- */
.modes {
  position: relative; display: grid; grid-template-columns: 1fr 1fr;
  padding: 4px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line);
}
.mode-pill {
  position: absolute; top: 4px; bottom: 4px; left: 4px; width: calc(50% - 4px);
  border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--accent) 18%, transparent);
  transition: transform 0.3s var(--ease-out);
}
.modes[data-active="daily"] .mode-pill { transform: translateX(100%); }
.mode {
  position: relative; z-index: 1; padding: 7px 0; border: 0; background: none;
  font-size: 11px; font-weight: 600; letter-spacing: 0.16em; text-transform: uppercase; color: var(--muted);
  transition: color 0.2s;
}
.mode[aria-selected="true"] { color: var(--ink); }
.status { margin-top: -6px; text-align: center; font-size: 12px; color: var(--muted); min-height: 1.2em; }

/* ---------- Board ---------- */
.board-wrap { position: relative; }
.board {
  position: relative; aspect-ratio: 1; width: 100%;
  padding: var(--gap); border-radius: var(--radius-lg);
  background: var(--board); border: 1px solid var(--line); box-shadow: var(--shadow);
  container-type: inline-size;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.slots, .tiles { position: absolute; inset: var(--gap); }
.slots { display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 1fr); gap: var(--gap); }
.slots i { border-radius: var(--radius); background: var(--slot); }

.tile {
  position: absolute; top: 0; left: 0;
  width: calc((100% - 3 * var(--gap)) / 4);
  height: calc((100% - 3 * var(--gap)) / 4);
  transform: translate(calc(var(--c) * (100% + var(--gap))), calc(var(--r) * (100% + var(--gap))));
  transition: transform var(--move) var(--ease-out);
  will-change: transform;
}
.tile.no-anim { transition: none; }
.tile-face {
  width: 100%; height: 100%; display: grid; place-items: center;
  border-radius: var(--radius);
  font-family: var(--font-ui); font-weight: 700; font-variant-numeric: tabular-nums;
  font-size: 30px; font-size: 11cqw; letter-spacing: -0.02em;
  background: var(--tbg); color: var(--tink);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.12), inset 0 1px 0 rgba(255, 255, 255, 0.25);
}
.tile.is-new .tile-face { animation: appear 0.2s var(--ease-pop) var(--move) backwards; }
.tile.is-merged .tile-face { animation: pop 0.22s var(--ease-pop) var(--move) backwards; }
@keyframes appear { from { opacity: 0; transform: scale(0.2); } to { opacity: 1; transform: scale(1); } }
@keyframes pop { 0% { transform: scale(0.85); } 55% { transform: scale(1.16); } 100% { transform: scale(1); } }

.tile.d3 .tile-face { font-size: 9cqw; }
.tile.d4 .tile-face { font-size: 7cqw; }
.tile.d5 .tile-face { font-size: 5.6cqw; }
.tile.d6 .tile-face { font-size: 4.6cqw; }

.tile.v2    { --tbg: var(--v2);    --tink: var(--v2ink); }
.tile.v4    { --tbg: var(--v4);    --tink: var(--v4ink); }
.tile.v8    { --tbg: var(--v8);    --tink: var(--v8ink); }
.tile.v16   { --tbg: var(--v16);   --tink: var(--v16ink); }
.tile.v32   { --tbg: var(--v32);   --tink: var(--v32ink); }
.tile.v64   { --tbg: var(--v64);   --tink: var(--v64ink); }
.tile.v128  { --tbg: var(--v128);  --tink: var(--v128ink); }
.tile.v256  { --tbg: var(--v256);  --tink: var(--v256ink); }
.tile.v512  { --tbg: var(--v512);  --tink: var(--v512ink); }
.tile.v1024 { --tbg: var(--v1024); --tink: var(--v1024ink); }
.tile.v2048 { --tbg: var(--v2048); --tink: var(--v2048ink); }
.tile.vhi   { --tbg: var(--vhi);   --tink: var(--vhiink); }
.tile.v128 .tile-face, .tile.v256 .tile-face, .tile.v512 .tile-face { box-shadow: 0 0 18px -4px var(--tbg), inset 0 -3px 0 rgba(0, 0, 0, 0.12); }
.tile.v1024 .tile-face, .tile.v2048 .tile-face, .tile.vhi .tile-face { box-shadow: 0 0 28px -2px var(--glow), inset 0 -3px 0 rgba(0, 0, 0, 0.12); }

/* ---------- Buttons ---------- */
.actions { display: grid; grid-template-columns: 1fr 1.6fr; gap: 10px; }
.btn {
  min-height: 46px; padding: 0 18px; border-radius: 13px;
  font-size: 12px; font-weight: 700; letter-spacing: 0.14em; text-transform: uppercase;
  border: 1px solid var(--line);
  transition: transform 0.15s, background 0.2s, box-shadow 0.2s, opacity 0.2s;
}
.btn:active { transform: scale(0.97); }
.btn.primary {
  background: var(--accent); color: var(--accent-ink); border-color: transparent;
  box-shadow: 0 10px 28px -10px var(--glow);
}
.btn.primary:hover { box-shadow: 0 12px 34px -8px var(--glow); }
.btn.ghost { background: var(--surface); }
.btn.ghost:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.4; cursor: default; transform: none; }
.hint { margin: 0; text-align: center; font-size: 11.5px; color: var(--muted); }
.sponsor {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  margin-top: 6px; padding-top: 14px; border-top: 1px solid var(--line);
  font-size: 11.5px; color: var(--muted);
}
.ad-tag {
  padding: 2px 7px; border-radius: 999px; border: 1px solid var(--line);
  font-size: 9.5px; font-weight: 600; letter-spacing: 0.12em; text-transform: uppercase;
}
.sponsor a { color: var(--ink); font-weight: 600; text-decoration: none; border-bottom: 1px solid var(--accent); }
.sponsor a:hover { color: var(--accent); }

/* ---------- Overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: center; padding: 16px;
  background: rgba(0, 0, 0, 0.45);
  background: color-mix(in srgb, var(--bg) 62%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  animation: fade 0.25s var(--ease-out);
}
.card {
  width: min(100%, 360px); padding: 22px 20px 20px;
  border-radius: var(--radius-lg); background: var(--surface);
  border: 1px solid var(--line); box-shadow: var(--shadow);
  animation: rise 0.32s var(--ease-out);
}
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(14px) scale(0.98); } }
.eyebrow { font-size: 9.5px; font-weight: 600; letter-spacing: 0.22em; text-transform: uppercase; color: var(--accent); }
.display { margin: 6px 0 8px; font-family: var(--font-display); font-weight: 700; font-size: 44px; line-height: 1; letter-spacing: -0.02em; }
.lede { margin: 0 0 14px; font-size: 13px; line-height: 1.5; color: var(--muted); }
.rules { list-style: none; margin: 0 0 18px; padding: 0; display: grid; gap: 10px; }
.rules li { display: grid; grid-template-columns: 22px 1fr; font-size: 12.5px; line-height: 1.45; font-weight: 500; }
.rules span { font-family: var(--font-display); font-weight: 700; color: var(--accent); }
.resume p { margin: 0 0 10px; font-size: 12.5px; color: var(--muted); }
.resume { margin-bottom: 4px; }
.stack { display: grid; gap: 8px; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.final { margin: -4px 0 14px; font-size: 13px; font-weight: 600; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 4px 0 16px; }
.stats div { padding: 10px 12px; border-radius: var(--radius); background: var(--surface-2); border: 1px solid var(--line); }
.stats .wide { grid-column: 1 / -1; }
.stats dt { font-size: 9.5px; letter-spacing: 0.18em; text-transform: uppercase; color: var(--muted); }
.stats dt span { letter-spacing: 0.06em; }
.stats dd { margin: 4px 0 0; font-family: var(--font-display); font-weight: 700; font-size: 24px; font-variant-numeric: tabular-nums; }

.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); z-index: 30;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  padding: 10px 16px; border-radius: 999px; font-size: 12.5px; font-weight: 600;
  background: var(--ink); color: var(--bg);
  transition: opacity 0.25s, transform 0.25s var(--ease-out);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 400px) {
  .tagline { letter-spacing: 0.14em; font-size: 9px; }
  .swatch { width: 16px; height: 16px; }
  .icon-btn { width: 32px; height: 32px; }
  .mark { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  :root { --move: 0ms; }
  .tile.is-new .tile-face, .tile.is-merged .tile-face, .card, .overlay { animation: none; }
}
