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

:root {
  --board-size: min(92vw, 62vh, 460px);
  --gap: calc(var(--board-size) * 0.026);
  --cell: calc((var(--board-size) - 5 * var(--gap)) / 4);
  --radius: calc(var(--board-size) * 0.03);
  --bg1: #0f0c1d;
  --bg2: #1b1035;
  --text: #f2eefc;
  --muted: #9b93b8;
  --board-bg: #241b3f;
  --cell-bg: rgba(255, 255, 255, 0.07);
  --move-ms: 120ms;
}

html, body { height: 100%; }

body {
  font-family: 'Outfit', system-ui, sans-serif;
  color: var(--text);
  background: radial-gradient(1200px 800px at 80% -10%, #3b1e6d 0%, transparent 55%),
              radial-gradient(900px 700px at -10% 100%, #16324f 0%, transparent 55%),
              linear-gradient(160deg, var(--bg1), var(--bg2));
  background-attachment: fixed;
  display: flex;
  justify-content: center;
  overscroll-behavior: none;
}

.page {
  width: var(--board-size);
  padding: clamp(14px, 3vh, 36px) 0 24px;
  display: flex;
  flex-direction: column;
}

.title-row {
  display: flex;
  align-items: center;
  gap: 14px;
}

.back {
  color: var(--muted);
  text-decoration: none;
  font-size: 1.5rem;
  line-height: 1;
  padding: 6px 10px 8px;
  border-radius: 10px;
  transition: background 0.15s, color 0.15s;
}
.back:hover { background: rgba(255, 255, 255, 0.08); color: var(--text); }

h1 {
  font-size: clamp(1.9rem, 8vw, 2.6rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  background: linear-gradient(90deg, #ffb347, #ff5e8a);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  margin-right: auto;
}

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

.score-box {
  position: relative;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  padding: 6px 14px;
  text-align: center;
  min-width: 72px;
}
.score-box .label {
  display: block;
  font-size: 0.62rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}
.score-box .value {
  font-size: 1.15rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.score-bump {
  position: absolute;
  right: 8px;
  top: 4px;
  font-weight: 800;
  color: #ffb347;
  opacity: 0;
  pointer-events: none;
}
.score-bump.animate { animation: floatUp 0.6s ease-out; }
@keyframes floatUp {
  0%   { opacity: 1; transform: translateY(0); }
  100% { opacity: 0; transform: translateY(-26px); }
}

.controls-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 14px 0 16px;
}

.hint { color: var(--muted); font-size: 0.85rem; }

.buttons { display: flex; gap: 8px; flex-shrink: 0; }

button {
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 12px;
  padding: 9px 16px;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  touch-action: manipulation;
}
button:hover { background: rgba(255, 255, 255, 0.18); }
button:active { transform: scale(0.96); }
button:disabled { opacity: 0.4; cursor: default; }

#new-game, #overlay-restart {
  background: linear-gradient(135deg, #ffb347, #ff5e8a);
  border: none;
  color: #2b1230;
}
#new-game:hover, #overlay-restart:hover { filter: brightness(1.1); }

.board-wrap { position: relative; }

.board {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  background: var(--board-bg);
  border-radius: var(--radius);
  padding: var(--gap);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.4);
}

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

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

.tile {
  position: absolute;
  width: var(--cell);
  height: var(--cell);
  border-radius: calc(var(--radius) * 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: calc(var(--cell) * 0.42);
  color: #4b3a20;
  transform: translate(calc(var(--x) * (var(--cell) + var(--gap))),
                       calc(var(--y) * (var(--cell) + var(--gap))));
  transition: transform var(--move-ms) ease-in-out;
  will-change: transform;
  z-index: 1;
}

.tile.spawn { animation: spawn 0.18s ease var(--move-ms) backwards; }
@keyframes spawn {
  from { scale: 0; }
  to   { scale: 1; }
}

.tile.merged {
  z-index: 2;
  animation: pop 0.22s ease;
}
@keyframes pop {
  0%   { scale: 1; }
  50%  { scale: 1.22; }
  100% { scale: 1; }
}

.tile[data-v="2"]    { background: #eee4da; }
.tile[data-v="4"]    { background: #ede0c8; }
.tile[data-v="8"]    { background: #f2b179; color: #fff; }
.tile[data-v="16"]   { background: #f59563; color: #fff; }
.tile[data-v="32"]   { background: #f67c5f; color: #fff; }
.tile[data-v="64"]   { background: #f65e3b; color: #fff; }
.tile[data-v="128"]  { background: #edcf72; color: #fff; font-size: calc(var(--cell) * 0.36); }
.tile[data-v="256"]  { background: #edcc61; color: #fff; font-size: calc(var(--cell) * 0.36);
                       box-shadow: 0 0 18px rgba(237, 204, 97, 0.45); }
.tile[data-v="512"]  { background: #edc850; color: #fff; font-size: calc(var(--cell) * 0.36);
                       box-shadow: 0 0 22px rgba(237, 200, 80, 0.55); }
.tile[data-v="1024"] { background: #edc53f; color: #fff; font-size: calc(var(--cell) * 0.3);
                       box-shadow: 0 0 26px rgba(237, 197, 63, 0.6); }
.tile[data-v="2048"] { background: #edc22e; color: #fff; font-size: calc(var(--cell) * 0.3);
                       box-shadow: 0 0 34px rgba(237, 194, 46, 0.8); }
.tile[data-v="4096"], .tile[data-v="8192"], .tile[data-v="16384"] {
  background: linear-gradient(135deg, #a06bff, #ff5e8a);
  color: #fff;
  font-size: calc(var(--cell) * 0.28);
  box-shadow: 0 0 34px rgba(160, 107, 255, 0.7);
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  border-radius: var(--radius);
  background: rgba(15, 12, 29, 0.82);
  backdrop-filter: blur(6px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s;
}
.overlay.show { opacity: 1; visibility: visible; }

.overlay h2 {
  font-size: 2rem;
  font-weight: 800;
  text-align: center;
  padding: 0 20px;
}

.overlay-buttons { display: flex; gap: 10px; }

button.ghost {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.3);
}
.overlay.no-continue #overlay-continue { display: none; }

@media (max-width: 420px) {
  .controls-row { flex-direction: column-reverse; align-items: stretch; }
  .buttons { justify-content: space-between; }
  .buttons button { flex: 1; }
  .hint { text-align: center; }
}
