:root {
  color-scheme: dark;
  --ink: #f4f6ff;
  --muted: rgba(244, 246, 255, 0.6);
  --accent: #5ce0ff;
  --bg: #0a0c1c;
  --panel: rgba(12, 16, 32, 0.78);
  --stroke: rgba(92, 224, 255, 0.25);
  --cell-bg: rgba(18, 22, 46, 0.9);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Space Mono", "Segoe UI", monospace;
  background: radial-gradient(circle at 20% 20%, rgba(53, 72, 143, 0.45), transparent 45%),
    radial-gradient(circle at 80% 10%, rgba(50, 30, 120, 0.55), transparent 50%),
    radial-gradient(circle at 50% 90%, rgba(22, 12, 50, 0.8), transparent 45%),
    var(--bg);
  color: var(--ink);
}

.page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 26px clamp(20px, 5vw, 48px) 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--stroke);
  backdrop-filter: blur(14px);
}

.title-block {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.badge {
  display: inline-flex;
  padding: 4px 12px;
  border-radius: 999px;
  background: rgba(14, 18, 38, 0.9);
  border: 1px solid rgba(92, 224, 255, 0.4);
  font-size: 0.7rem;
  letter-spacing: 0.2rem;
  text-transform: uppercase;
  color: var(--muted);
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.2rem;
  color: var(--muted);
  text-decoration: none;
}

.back-link:hover {
  color: var(--ink);
}

h1 {
  margin: 0;
  font-family: "Chakra Petch", "Space Mono", sans-serif;
  font-size: clamp(1.8rem, 2.6vw + 1rem, 2.8rem);
  letter-spacing: 0.02em;
}

p {
  margin: 0;
  color: var(--muted);
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 14px;
}

.scoreboard {
  background: rgba(15, 20, 42, 0.9);
  border: 1px solid rgba(92, 224, 255, 0.3);
  border-radius: 12px;
  padding: 10px 14px;
  min-width: 90px;
  text-align: center;
}

.scoreboard .label {
  display: block;
  font-size: 0.7rem;
  letter-spacing: 0.12rem;
  text-transform: uppercase;
  color: var(--muted);
}

button {
  border: 1px solid rgba(92, 224, 255, 0.4);
  background: linear-gradient(135deg, rgba(16, 22, 44, 0.9), rgba(10, 12, 30, 0.9));
  color: var(--ink);
  padding: 10px 16px;
  border-radius: 999px;
  font-weight: 700;
  font-family: "Chakra Petch", "Space Mono", sans-serif;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

button:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(6, 8, 18, 0.4);
}

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

.stage {
  flex: 1;
  display: grid;
  place-items: center;
  padding: 40px 20px 60px;
  position: relative;
}

.board {
  width: min(92vw, 420px);
  aspect-ratio: 1;
  background: rgba(8, 10, 24, 0.7);
  border-radius: 22px;
  --tile-gap: 12px;
  --tile-size: calc((100% - (var(--tile-gap) * 5)) / 4);
  padding: var(--tile-gap);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--tile-gap);
  box-shadow: 0 22px 50px rgba(5, 8, 18, 0.55);
  border: 1px solid rgba(92, 224, 255, 0.18);
  position: relative;
}

.cell {
  background: var(--cell-bg);
  border-radius: 14px;
}

.tiles {
  position: absolute;
  inset: 0;
}

.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--tile-size);
  height: var(--tile-size);
  transition: transform 0.16s ease, opacity 0.16s ease;
  will-change: transform;
}

.tile-face {
  width: 100%;
  height: 100%;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: "Chakra Petch", "Space Mono", sans-serif;
  font-size: clamp(1rem, 4vw, 2rem);
  font-weight: 700;
  color: var(--ink);
  background: #203a73;
  box-shadow: inset 0 0 0 rgba(0, 0, 0, 0);
}

.tile.new .tile-face {
  animation: pop 0.2s ease-out;
}

.tile.merge .tile-face {
  animation: merge 0.2s ease-out;
}

.tile.fade {
  opacity: 0;
}

.tile[data-value="2"] .tile-face {
  background: #203a73;
}

.tile[data-value="4"] .tile-face {
  background: #2a4c8c;
}

.tile[data-value="8"] .tile-face {
  background: #2f6aad;
}

.tile[data-value="16"] .tile-face {
  background: #2f8ac1;
}

.tile[data-value="32"] .tile-face {
  background: #27a5c7;
}

.tile[data-value="64"] .tile-face {
  background: #37c2ba;
}

.tile[data-value="128"] .tile-face {
  background: #5adfa8;
  color: #091018;
}

.tile[data-value="256"] .tile-face {
  background: #84f6a2;
  color: #091018;
}

.tile[data-value="512"] .tile-face {
  background: #f4f79b;
  color: #202004;
}

.tile[data-value="1024"] .tile-face {
  background: #ffd166;
  color: #241700;
}

.tile[data-value="2048"] .tile-face {
  background: #ff9b54;
  color: #2b0f00;
}

.tile[data-value="4096"] .tile-face {
  background: #ff6b6b;
  color: #2b0f00;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(8, 10, 24, 0.65);
  backdrop-filter: blur(8px);
}

.overlay[hidden] {
  display: none;
}

.overlay-card {
  background: rgba(12, 16, 32, 0.9);
  border: 1px solid rgba(92, 224, 255, 0.4);
  border-radius: 18px;
  padding: 24px 28px;
  text-align: center;
  box-shadow: 0 20px 40px rgba(5, 8, 18, 0.4);
}

.overlay-card h2 {
  margin: 0 0 8px;
  font-family: "Chakra Petch", "Space Mono", sans-serif;
}

.overlay-card p {
  margin: 0 0 16px;
  color: var(--muted);
}

@keyframes pop {
  0% {
    transform: scale(0.92);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes merge {
  0% {
    transform: scale(1);
  }
  60% {
    transform: scale(1.08);
  }
  100% {
    transform: scale(1);
  }
}

@media (max-width: 700px) {
  .topbar {
    padding: 20px;
  }
}

@media (prefers-reduced-motion: reduce) {
  button {
    transition: none;
  }

  .tile {
    transition: none;
  }

  .tile.new .tile-face,
  .tile.merge .tile-face {
    animation: none;
  }
}
