:root {
  color-scheme: light;
  --bg: #f4f1ea;
  --ink: #25302f;
  --muted: #68716f;
  --panel: #fffdf8;
  --line: #d8d0c1;
  --board: #938a7d;
  --cell: #c7bdad;
  --accent: #1e7067;
  --accent-dark: #15524c;
  --danger: #9b3d30;
  --tile-size: clamp(64px, 18vw, 106px);
  --gap: clamp(9px, 2.2vw, 14px);
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  background:
    radial-gradient(circle at top left, rgba(30, 112, 103, 0.14), transparent 34rem),
    linear-gradient(135deg, #f8f5ee 0%, var(--bg) 48%, #e8e2d5 100%);
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button {
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fffdf8;
  cursor: pointer;
  font: inherit;
  font-weight: 800;
  padding: 0 18px;
  transition: background 140ms ease, transform 140ms ease;
}

button:hover {
  background: var(--accent-dark);
  transform: translateY(-1px);
}

button:focus-visible {
  outline: 3px solid rgba(30, 112, 103, 0.35);
  outline-offset: 3px;
}

.shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.game-panel {
  width: min(100%, calc((var(--tile-size) * 4) + (var(--gap) * 5) + 48px));
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 2px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  color: var(--accent-dark);
  font-size: clamp(3.5rem, 16vw, 6rem);
  line-height: 0.9;
}

.score-row {
  display: flex;
  gap: 8px;
}

.score-box {
  min-width: 82px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.84);
  padding: 9px 12px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(64, 53, 38, 0.08);
}

.score-box span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
}

.score-box strong {
  display: block;
  margin-top: 2px;
  font-size: 1.25rem;
}

.actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.status {
  margin: 0;
  color: var(--muted);
  font-weight: 700;
}

.board-wrap {
  position: relative;
}

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(4, var(--tile-size));
  grid-template-rows: repeat(4, var(--tile-size));
  gap: var(--gap);
  width: calc((var(--tile-size) * 4) + (var(--gap) * 5));
  height: calc((var(--tile-size) * 4) + (var(--gap) * 5));
  border-radius: 8px;
  background: var(--board);
  padding: var(--gap);
  touch-action: none;
  box-shadow: 0 22px 55px rgba(64, 53, 38, 0.22);
}

.cell,
.tile {
  width: var(--tile-size);
  height: var(--tile-size);
  border-radius: 7px;
}

.cell {
  background: var(--cell);
  opacity: 0.72;
}

.tile {
  position: absolute;
  top: var(--gap);
  left: var(--gap);
  display: grid;
  place-items: center;
  color: #27302f;
  font-size: clamp(1.7rem, 8vw, 2.9rem);
  font-weight: 900;
  line-height: 1;
  transform: translate(
    calc(var(--col) * (var(--tile-size) + var(--gap))),
    calc(var(--row) * (var(--tile-size) + var(--gap)))
  );
  scale: 1;
  transition: transform 140ms cubic-bezier(0.2, 0.8, 0.2, 1);
  will-change: transform;
}

.tile.is-merged {
  animation: merge 180ms ease-out;
}

.tile-1 { background: #eee5d8; }
.tile-2 { background: #e6d7bf; }
.tile-4 { background: #f1be72; color: #fffdf8; }
.tile-8 { background: #e99757; color: #fffdf8; }
.tile-16 { background: #dd704b; color: #fffdf8; }
.tile-32 { background: #c85643; color: #fffdf8; }
.tile-64 { background: #b7443e; color: #fffdf8; }
.tile-128 { background: #62a884; color: #fffdf8; font-size: clamp(1.45rem, 6.8vw, 2.55rem); }
.tile-256 { background: #399386; color: #fffdf8; font-size: clamp(1.45rem, 6.8vw, 2.55rem); }
.tile-512 { background: #1f756e; color: #fffdf8; font-size: clamp(1.45rem, 6.8vw, 2.55rem); }
.tile-1024 { background: #124f55; color: #fffdf8; font-size: clamp(1.25rem, 6vw, 2.25rem); }
.tile-super { background: #263a3f; color: #fffdf8; font-size: clamp(1.15rem, 5.5vw, 2rem); }

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.84);
  backdrop-filter: blur(2px);
}

.overlay[hidden] {
  display: none;
}

.overlay-content {
  width: min(86%, 320px);
  border: 1px solid rgba(216, 208, 193, 0.84);
  border-radius: 8px;
  background: var(--panel);
  padding: 22px;
  text-align: center;
  box-shadow: 0 18px 44px rgba(64, 53, 38, 0.18);
}

.overlay-content h2 {
  margin: 0 0 6px;
  font-size: 2rem;
}

.overlay-content p {
  margin: 0 0 18px;
  color: var(--muted);
  font-weight: 700;
}

.overlay-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
}

#try-again {
  background: var(--danger);
}

@keyframes merge {
  0% { scale: 1; }
  50% { scale: 1.12; }
  100% { scale: 1; }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0s !important;
    animation-duration: 0s !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
  }
}

@media (max-width: 520px) {
  .shell {
    align-items: start;
    padding: 18px 12px;
  }

  .topbar {
    align-items: start;
  }

  .score-row {
    flex-direction: column;
  }

  .score-box {
    min-width: 72px;
    padding: 7px 10px;
  }

  .actions {
    align-items: stretch;
  }

  .status {
    align-self: center;
    font-size: 0.92rem;
  }
}
