:root {
  --gap: 12px;
  --radius: 6px;
  --bg: #faf8ef;
  --board-bg: #bbada0;
  --cell-bg: #cdc1b4;
  --text-dark: #776e65;
  --text-light: #f9f6f2;
}

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

body {
  font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text-dark);
  display: flex;
  justify-content: center;
  padding: 20px 12px 40px;
  min-height: 100vh;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

.container {
  width: 100%;
  max-width: 480px;
}

header {
  margin-bottom: 18px;
}

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

h1 {
  font-size: 64px;
  font-weight: 800;
  line-height: 1;
}

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

.score-box {
  background: var(--board-bg);
  border-radius: var(--radius);
  padding: 8px 16px;
  text-align: center;
  color: var(--text-light);
  min-width: 78px;
  display: flex;
  flex-direction: column;
}

.score-label {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 1px;
  color: #eee4da;
}

.score-value {
  font-size: 24px;
  font-weight: 800;
}

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

.subtitle {
  font-size: 15px;
  line-height: 1.4;
}

.new-game-btn {
  background: #8f7a66;
  color: var(--text-light);
  border: none;
  border-radius: var(--radius);
  padding: 10px 18px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s ease;
}

.new-game-btn:hover {
  background: #9f8b77;
}

.game-area {
  position: relative;
}

.grid {
  position: relative;
  background: var(--board-bg);
  border-radius: var(--radius);
  padding: var(--gap);
  width: 100%;
  aspect-ratio: 1 / 1;
  touch-action: none;
}

.cell-bg {
  position: absolute;
  background: var(--cell-bg);
  border-radius: var(--radius);
}

.tile {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  font-weight: 800;
  background: #eee4da;
  color: var(--text-dark);
  transition: top 0.12s ease, left 0.12s ease;
  z-index: 2;
}

.tile-new {
  animation: appear 0.18s ease;
}

.tile-merged {
  animation: pop 0.18s ease;
  z-index: 3;
}

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

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

/* Tile colours */
.tile-2    { background: #eee4da; }
.tile-4    { background: #ede0c8; }
.tile-8    { background: #f2b179; color: var(--text-light); }
.tile-16   { background: #f59563; color: var(--text-light); }
.tile-32   { background: #f67c5f; color: var(--text-light); }
.tile-64   { background: #f65e3b; color: var(--text-light); }
.tile-128  { background: #edcf72; color: var(--text-light); }
.tile-256  { background: #edcc61; color: var(--text-light); }
.tile-512  { background: #edc850; color: var(--text-light); }
.tile-1024 { background: #edc53f; color: var(--text-light); }
.tile-2048 { background: #edc22e; color: var(--text-light); }
/* Beyond 2048 — the game keeps going forever */
.tile-super { background: #3c3a32; color: var(--text-light); }

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(238, 228, 218, 0.73);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 10;
  animation: appear 0.3s ease;
}

.overlay h2 {
  font-size: 34px;
  color: var(--text-dark);
}

.overlay .final-score {
  font-size: 18px;
  color: var(--text-dark);
}

.submit-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 80%;
  max-width: 280px;
}

.submit-form input {
  padding: 11px 14px;
  font-size: 16px;
  border: 2px solid #bbada0;
  border-radius: var(--radius);
  background: #fdfcf9;
  color: var(--text-dark);
  text-align: center;
  outline: none;
}

.submit-form input:focus {
  border-color: #8f7a66;
}

.overlay .retry-btn {
  background: #8f7a66;
  color: var(--text-light);
  border: none;
  border-radius: var(--radius);
  padding: 12px 24px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}

.overlay .retry-btn.ghost {
  background: transparent;
  color: #776e65;
  text-decoration: underline;
  padding: 4px 8px;
  font-size: 14px;
}

.submit-status {
  font-size: 14px;
  color: #776e65;
  min-height: 1em;
}

/* Global leaderboard */
.leaderboard {
  margin-top: 26px;
}

.leaderboard h2 {
  font-size: 22px;
  margin-bottom: 12px;
  color: var(--text-dark);
}

.leaderboard-list {
  list-style: none;
  background: var(--board-bg);
  border-radius: var(--radius);
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.lb-row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: #eee4da;
  border-radius: 4px;
  padding: 10px 14px;
  font-weight: 700;
  color: var(--text-dark);
}

.lb-rank {
  width: 32px;
  text-align: center;
  font-size: 17px;
  flex-shrink: 0;
}

.lb-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lb-score {
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.lb-empty {
  background: #eee4da;
  border-radius: 4px;
  padding: 14px;
  text-align: center;
  color: #8a8178;
  font-size: 14px;
  line-height: 1.4;
}

.hidden {
  display: none;
}

.hint {
  margin-top: 20px;
  font-size: 14px;
  line-height: 1.5;
  color: #8a8178;
}

@media (max-width: 420px) {
  h1 { font-size: 48px; }
  .subtitle-row { flex-direction: column; align-items: stretch; }
  .new-game-btn { width: 100%; }
}
