:root {
  --bg: #f3f0fb;
  --surface: #ffffff;
  --board: #d6d0ee;
  --cell: #e9e5f7;
  --text: #211f3a;
  --muted: #6d6890;
  --accent: #5a63e8;
  --accent-2: #d23fb4;
  --shadow: 0 6px 20px rgba(40, 30, 90, 0.12);
  --anim: 110ms;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12111f;
    --surface: #1d1b31;
    --board: #262342;
    --cell: #322e55;
    --text: #f1effa;
    --muted: #9b96c2;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #12111f;
  --surface: #1d1b31;
  --board: #262342;
  --cell: #322e55;
  --text: #f1effa;
  --muted: #9b96c2;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

button { font: inherit; color: inherit; cursor: pointer; }

.app {
  --w: min(calc(100vw - 32px), 520px, calc(100dvh - 230px));
  width: max(var(--w), 280px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top)) 0 calc(16px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 14px;
}

/* ---------- Header ---------- */
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.logo {
  margin: 0;
  font-size: clamp(2.6rem, 12vw, 4rem);
  font-weight: 800;
  letter-spacing: -0.04em;
  line-height: 1;
  background: linear-gradient(120deg, #2fb8a8, var(--accent) 50%, var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.scores { display: flex; gap: 8px; }

.score-box {
  position: relative;
  min-width: 76px;
  padding: 6px 12px;
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: center;
  display: flex;
  flex-direction: column;
}
.score-box .label { font-size: 0.7rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.score-box .value { font-size: 1.35rem; font-weight: 800; font-variant-numeric: tabular-nums; }

.score-add {
  position: absolute;
  left: 0; right: 0; bottom: 8px;
  font-weight: 800;
  color: var(--accent);
  opacity: 0;
  pointer-events: none;
}
.score-add.show { animation: float-up 700ms ease-out; }
@keyframes float-up {
  0% { opacity: 1; transform: translateY(0); }
  100% { opacity: 0; transform: translateY(-36px); }
}

/* ---------- Toolbar ---------- */
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }

.sizes {
  display: flex;
  padding: 3px;
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.sizes button {
  border: 0;
  background: transparent;
  padding: 7px 9px;
  border-radius: 9px;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--muted);
}
.sizes button[aria-pressed="true"] { background: var(--accent); color: #fff; }

.actions { display: flex; gap: 8px; }

.icon-btn {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border: 0;
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:disabled { opacity: 0.4; cursor: default; }
.icon-btn:not(:disabled):active, .sizes button:active, .btn:active { transform: scale(0.94); }

/* ---------- Board ---------- */
.board {
  --n: 4;
  --gap: 12px;
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  padding: var(--gap);
  border-radius: 18px;
  background: var(--board);
  box-shadow: var(--shadow);
  touch-action: none;
}

.grid, .tiles { position: absolute; inset: var(--gap); }

.grid {
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  gap: var(--gap);
}
.grid div { border-radius: calc(var(--gap) * 0.9); background: var(--cell); }

.tiles { container-type: size; }

.tile {
  position: absolute;
  top: 0; left: 0;
  width: calc((100% - (var(--n) - 1) * var(--gap)) / var(--n));
  height: calc((100% - (var(--n) - 1) * var(--gap)) / var(--n));
  transform: translate(calc(var(--c) * (100% + var(--gap))), calc(var(--r) * (100% + var(--gap))));
  transition: transform var(--anim) ease-in-out;
  z-index: 1;
}
.tile.merged { z-index: 2; }

.tile-inner {
  --fs: 0.46;
  width: 100%; height: 100%;
  display: grid; place-items: center;
  border-radius: calc(var(--gap) * 0.9);
  background: var(--tile-bg);
  color: var(--tile-fg);
  font-weight: 800;
  font-size: calc(100cqw / var(--n) * var(--fs));
  font-variant-numeric: tabular-nums;
  box-shadow: var(--tile-glow, none);
}
.tile[data-d="3"] .tile-inner { --fs: 0.38; }
.tile[data-d="4"] .tile-inner { --fs: 0.29; }
.tile[data-d="5"] .tile-inner { --fs: 0.23; }

.tile.new .tile-inner { animation: appear 180ms ease-out calc(var(--anim)) backwards; }
.tile.merged .tile-inner { animation: pop 200ms ease-out calc(var(--anim)) backwards; }

@keyframes appear { from { opacity: 0; transform: scale(0.1); } to { opacity: 1; transform: scale(1); } }
@keyframes pop { 0% { transform: scale(0.2); } 60% { transform: scale(1.15); } 100% { transform: scale(1); } }

/* Tile colours: teal -> blue -> violet -> magenta -> coral -> gold */
.tile[data-v="2"]    { --tile-bg: #e4f6f2; --tile-fg: #1d3d38; }
.tile[data-v="4"]    { --tile-bg: #bfeae1; --tile-fg: #1d3d38; }
.tile[data-v="8"]    { --tile-bg: #6fd6c4; --tile-fg: #0f2f2a; }
.tile[data-v="16"]   { --tile-bg: #2fbfae; --tile-fg: #fff; }
.tile[data-v="32"]   { --tile-bg: #27a2c8; --tile-fg: #fff; }
.tile[data-v="64"]   { --tile-bg: #2f7ee0; --tile-fg: #fff; }
.tile[data-v="128"]  { --tile-bg: #5763ea; --tile-fg: #fff; --tile-glow: 0 0 14px rgba(87, 99, 234, 0.55); }
.tile[data-v="256"]  { --tile-bg: #7f50e6; --tile-fg: #fff; --tile-glow: 0 0 16px rgba(127, 80, 230, 0.55); }
.tile[data-v="512"]  { --tile-bg: #a644d9; --tile-fg: #fff; --tile-glow: 0 0 18px rgba(166, 68, 217, 0.6); }
.tile[data-v="1024"] { --tile-bg: #d23fb4; --tile-fg: #fff; --tile-glow: 0 0 20px rgba(210, 63, 180, 0.6); }
.tile[data-v="2048"] { --tile-bg: #ee4a7c; --tile-fg: #fff; --tile-glow: 0 0 22px rgba(238, 74, 124, 0.65); }
.tile[data-v="4096"] { --tile-bg: linear-gradient(135deg, #ff7a45, #ffb627); --tile-fg: #fff; --tile-glow: 0 0 26px rgba(255, 140, 60, 0.75); }
.tile[data-v="super"] { --tile-bg: linear-gradient(135deg, #1b1a2e, #3d2b6b); --tile-fg: #ffd36b; --tile-glow: 0 0 26px rgba(255, 211, 107, 0.5); }

/* ---------- Overlay ---------- */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: 18px;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(3px);
  animation: fade 300ms ease-out;
  text-align: center;
  padding: 16px;
}
.overlay[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.overlay-title { margin: 0; font-size: clamp(1.8rem, 8vw, 2.6rem); font-weight: 800; }
.overlay-sub { margin: 0 0 12px; color: var(--muted); font-weight: 600; }
.overlay-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }

.btn {
  border: 0;
  padding: 11px 20px;
  border-radius: 12px;
  font-weight: 700;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.btn.primary { background: var(--accent); color: #fff; }
.btn[hidden] { display: none; }

.hint { margin: 0; text-align: center; color: var(--muted); font-size: 0.9rem; }

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

@media (prefers-reduced-motion: reduce) {
  :root { --anim: 0ms; }
  .tile .tile-inner, .score-add.show, .overlay { animation: none !important; }
}
