:root {
  --bg0: #f6f8fd;
  --bg1: #edf2fa;
  --panel: #ffffff;
  --panel-soft: #f8fbff;
  --line: #d6e0ee;
  --line-strong: #c3d1e4;
  --text: #0f2440;
  --muted: #5e7693;
  --accent: #2b63dd;
  --accent-strong: #1e4fb7;
  --tile-empty: #c8d5e6;
  --tile-2: #f6fbff;
  --tile-4: #b8d6ff;
  --tile-8: #ffe3b5;
  --tile-16: #ffc97c;
  --tile-32: #ffac54;
  --tile-64: #ff8740;
  --tile-128: #ffe162;
  --tile-256: #ffcf47;
  --tile-512: #ffb82f;
  --tile-1024: #ff9f1c;
  --tile-2048: #ff7f11;
}

* {
  box-sizing: border-box;
}

.hidden {
  display: none !important;
}

body {
  margin: 0;
  font-family: 'Space Grotesk', sans-serif;
  color: var(--text);
  background:
    radial-gradient(980px 620px at 100% -18%, rgba(43, 99, 221, 0.12), transparent 62%),
    radial-gradient(780px 520px at -12% -20%, rgba(74, 149, 239, 0.08), transparent 65%),
    linear-gradient(180deg, var(--bg0), var(--bg1));
}

.app {
  max-width: 1160px;
  margin: 0 auto;
  padding: 24px 16px 32px;
}

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

h1,
h2 {
  margin: 0;
  font-family: 'Sora', sans-serif;
}

h1 {
  font-size: clamp(1.9rem, 3vw, 2.45rem);
  letter-spacing: -0.02em;
}

h2 {
  font-size: 1rem;
}

.sub {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 0.94rem;
}

.back {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.9rem;
  padding-top: 6px;
}

.back:hover {
  color: #385a82;
}

.hud {
  display: grid;
  grid-template-columns: repeat(2, minmax(120px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}

.hud article,
.panel {
  border: 1px solid var(--line);
  background: linear-gradient(180deg, #ffffff, var(--panel-soft));
  border-radius: 16px;
  box-shadow: 0 12px 30px rgba(18, 44, 82, 0.08);
}

.hud article {
  padding: 11px;
  text-align: center;
}

.hud span {
  color: var(--muted);
  font-size: 0.74rem;
  letter-spacing: 0.09em;
  text-transform: uppercase;
}

.hud strong {
  display: block;
  margin-top: 4px;
  font-size: 1.26rem;
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 340px);
  gap: 12px;
  align-items: start;
}

.panel {
  padding: 16px;
}

.game-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.board {
  width: min(100%, 560px);
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: clamp(7px, 1.05vw, 10px);
  background: linear-gradient(180deg, #dbe6f2, #d3dfec);
  padding: clamp(7px, 1.05vw, 10px);
  border-radius: 16px;
  border: 1px solid #cdd9e8;
  touch-action: none;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.45);
}

.board-wrap {
  position: relative;
  width: min(100%, 560px);
  margin: 0 auto;
}

.cell {
  aspect-ratio: 1;
  border-radius: 11px;
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: clamp(1.02rem, 3.45vw, 1.92rem);
  background: var(--tile-empty);
  color: #1f3c61;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.35);
  user-select: none;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.35);
  transition: transform 120ms ease, filter 120ms ease;
}

.v2 { background: var(--tile-2); color: #12365c; }
.v4 { background: var(--tile-4); color: #0f2f52; }
.v8 { background: var(--tile-8); color: #5e2f00; text-shadow: none; }
.v16 { background: var(--tile-16); color: #4d2600; text-shadow: none; }
.v32 { background: var(--tile-32); color: #412000; text-shadow: none; }
.v64 { background: var(--tile-64); color: #ffffff; text-shadow: 0 1px 2px rgba(64, 24, 0, 0.45); }
.v128 { background: var(--tile-128); color: #4a3300; text-shadow: none; }
.v256 { background: var(--tile-256); color: #3c2600; text-shadow: none; }
.v512 { background: var(--tile-512); color: #2f1b00; text-shadow: none; }
.v1024 { background: var(--tile-1024); color: #ffffff; text-shadow: 0 1px 2px rgba(55, 20, 0, 0.5); }
.v2048 { background: var(--tile-2048); color: #ffffff; text-shadow: 0 1px 2px rgba(50, 18, 0, 0.55); }

.game-over-overlay {
  position: absolute;
  inset: 0;
  border-radius: 16px;
  background: linear-gradient(180deg, rgba(16, 36, 64, 0.28), rgba(16, 36, 64, 0.5));
  display: grid;
  place-items: center;
  padding: 16px;
  animation: overlayFadeIn 180ms ease;
}

.game-over-card {
  width: min(92%, 340px);
  border-radius: 14px;
  border: 1px solid rgba(216, 228, 243, 0.85);
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 18px 32px rgba(11, 30, 55, 0.3);
  padding: 14px 14px 12px;
  text-align: center;
  animation: gameOverPop 220ms ease;
}

.game-over-card h3 {
  margin: 0;
  font-family: 'Sora', sans-serif;
  font-size: 1.25rem;
}

.game-over-card p {
  margin: 8px 0 12px;
  color: #3c5679;
  font-size: 0.94rem;
}

.overlay-actions {
  display: grid;
}

.board-wrap.ended .cell {
  filter: saturate(0.88);
}

@keyframes overlayFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes gameOverPop {
  from { opacity: 0; transform: translateY(10px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

button,
input {
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  padding: 10px;
  font-size: 0.94rem;
}

input {
  width: 100%;
  background: #fff;
  color: var(--text);
}

button {
  background: linear-gradient(180deg, var(--accent), var(--accent-strong));
  border-color: var(--accent);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

button:hover {
  filter: brightness(1.04);
}

button.ghost {
  width: auto;
  background: #f8fbff;
  color: #334f74;
  border-color: var(--line-strong);
}

.status {
  margin: 10px 0 0;
  color: var(--muted);
  min-height: 1.2rem;
  font-size: 0.9rem;
}

.leader-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.leaderboard-list {
  margin: 0;
  padding-left: 22px;
  max-height: 355px;
  overflow: auto;
}

.leaderboard-list li {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 4px 0;
  font-size: 0.93rem;
}

.leaderboard-list .empty {
  list-style: none;
  margin-left: -22px;
  color: var(--muted);
}

.save-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  margin-top: 10px;
}

.save-row button {
  min-width: 112px;
}

@media (max-width: 1020px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .leaderboard-list {
    max-height: none;
  }
}

@media (max-width: 640px) {
  .app {
    padding: 16px 10px 22px;
  }

  .top {
    align-items: baseline;
  }

  .game-head {
    flex-direction: column;
    align-items: stretch;
  }

  .save-row {
    grid-template-columns: 1fr;
  }
}
