:root {
  --bg: #0f1220;
  --panel: #1a1f35;
  --cell-bg: #252b45;
  --text: #e8ebff;
  --muted: #8a91b4;
  --board: min(calc(100vw - 32px), 480px);
  --gap: calc(var(--board) * 0.028);
  --cell: calc((var(--board) - var(--gap) * 5) / 4);
  --radius: calc(var(--cell) * 0.12);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { min-height: 100%; }

body {
  background: radial-gradient(1200px 600px at 50% -10%, #232a52 0%, var(--bg) 60%) fixed, var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  display: flex;
  justify-content: center;
  padding: 24px 16px 40px;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

.app { width: var(--board); display: flex; flex-direction: column; gap: 18px; }

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

.logo {
  font-size: clamp(48px, 14vw, 72px);
  font-weight: 800;
  letter-spacing: -2px;
  line-height: 1;
  background: linear-gradient(135deg, #ffd166, #ff8a3d 50%, #ef476f);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

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

.score {
  background: var(--panel);
  border-radius: 10px;
  padding: 8px 14px;
  min-width: 76px;
  text-align: center;
  display: flex;
  flex-direction: column;
}

.score-label { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); font-weight: 700; }
.score-value { font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }

.bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.tagline { color: var(--muted); font-size: 15px; line-height: 1.4; }
.tagline strong { color: var(--text); }

.btn {
  appearance: none;
  border: 0;
  cursor: pointer;
  background: linear-gradient(135deg, #ff8a3d, #ef476f);
  color: #fff;
  font: inherit;
  font-weight: 700;
  font-size: 15px;
  padding: 11px 18px;
  border-radius: 10px;
  white-space: nowrap;
  text-decoration: none;
  transition: transform .1s ease, filter .15s ease;
}
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: scale(.97); }
.btn:focus-visible { outline: 3px solid #ffd166; outline-offset: 2px; }
.btn-ghost { background: transparent; box-shadow: inset 0 0 0 2px var(--text); }
.btn[hidden] { display: none; }

.board {
  position: relative;
  width: var(--board);
  height: var(--board);
  background: var(--panel);
  border-radius: calc(var(--radius) * 1.4);
  padding: var(--gap);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .35);
}

.grid {
  display: grid;
  grid-template-columns: repeat(4, var(--cell));
  grid-template-rows: repeat(4, var(--cell));
  gap: var(--gap);
}
.grid > div { background: var(--cell-bg); border-radius: var(--radius); }

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

.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--cell);
  height: var(--cell);
  transform: translate(calc(var(--c) * (var(--cell) + var(--gap))), calc(var(--r) * (var(--cell) + var(--gap))));
  transition: transform 110ms ease-in-out;
  will-change: transform;
}

.tile-inner {
  width: 100%;
  height: 100%;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: calc(var(--cell) * .42);
  font-variant-numeric: tabular-nums;
  background: var(--t-bg);
  color: var(--t-fg);
  box-shadow: 0 0 var(--t-glow, 0) var(--t-glow-color, transparent);
}

.tile[data-len="3"] .tile-inner { font-size: calc(var(--cell) * .34); }
.tile[data-len="4"] .tile-inner { font-size: calc(var(--cell) * .27); }
.tile[data-len="5"] .tile-inner,
.tile[data-len="6"] .tile-inner { font-size: calc(var(--cell) * .22); }

.tile-new .tile-inner { animation: appear 160ms ease 90ms backwards; }
.tile-merged .tile-inner { animation: pop 180ms ease; }

.tile[data-value="2"]    { --t-bg: #e8ecff; --t-fg: #2a3060; }
.tile[data-value="4"]    { --t-bg: #cdd5ff; --t-fg: #2a3060; }
.tile[data-value="8"]    { --t-bg: #7ad3c4; --t-fg: #0f1a2a; }
.tile[data-value="16"]   { --t-bg: #4fb7d9; --t-fg: #fff; }
.tile[data-value="32"]   { --t-bg: #5b8def; --t-fg: #fff; }
.tile[data-value="64"]   { --t-bg: #7a6cf0; --t-fg: #fff; }
.tile[data-value="128"]  { --t-bg: #a66cf0; --t-fg: #fff; --t-glow: 18px; --t-glow-color: rgba(166, 108, 240, .45); }
.tile[data-value="256"]  { --t-bg: #d86ad0; --t-fg: #fff; --t-glow: 20px; --t-glow-color: rgba(216, 106, 208, .45); }
.tile[data-value="512"]  { --t-bg: #ef476f; --t-fg: #fff; --t-glow: 22px; --t-glow-color: rgba(239, 71, 111, .5); }
.tile[data-value="1024"] { --t-bg: #ff8a3d; --t-fg: #fff; --t-glow: 26px; --t-glow-color: rgba(255, 138, 61, .55); }
.tile[data-value="2048"] { --t-bg: #ffd166; --t-fg: #3a2400; --t-glow: 32px; --t-glow-color: rgba(255, 209, 102, .65); }
.tile[data-value="super"] { --t-bg: linear-gradient(135deg, #ffd166, #ef476f); --t-fg: #fff; --t-glow: 34px; --t-glow-color: rgba(239, 71, 111, .6); }

@keyframes appear { from { transform: scale(0); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes pop { 0% { transform: scale(1); } 50% { transform: scale(1.18); } 100% { transform: scale(1); } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  border-radius: inherit;
  background: rgba(15, 18, 32, .8);
  backdrop-filter: blur(3px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  animation: fade .3s ease;
}
.overlay[hidden] { display: none; }
.overlay-title { font-size: clamp(28px, 8vw, 44px); font-weight: 800; }
.overlay-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }

.howto { background: var(--panel); border-radius: 14px; padding: 16px 18px; color: var(--muted); font-size: 14px; line-height: 1.6; }
.howto h2 { color: var(--text); font-size: 16px; margin-bottom: 6px; }
.howto strong { color: var(--text); }

.foot { text-align: center; color: var(--muted); font-size: 12px; }

.notfound { align-items: center; justify-content: center; text-align: center; min-height: 60vh; }

@media (max-width: 420px) {
  .bar { flex-direction: column; align-items: stretch; text-align: center; }
  .score { min-width: 64px; padding: 6px 10px; }
}

@media (prefers-reduced-motion: reduce) {
  .tile { transition: none; }
  .tile-inner, .overlay { animation: none !important; }
}
