:root {
  color-scheme: dark;
  --bg: #0b0d12;
  --surface: #121620;
  --surface-2: #191f2b;
  --surface-3: #232b39;
  --text: #f4f7fb;
  --muted: #9ca8ba;
  --accent: #8b5cf6;
  --accent-2: #22d3ee;
  --danger: #fb7185;
  --board: #202633;
  --cell: #2c3443;
  --gap: clamp(7px, 1.4vmin, 12px);
  --radius: 22px;
  --shadow: 0 24px 70px rgb(0 0 0 / 0.35);
  --board-size: min(76vw, 58dvh, 540px);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-width: 320px;
  min-height: 100%;
  background: var(--bg);
}

body {
  min-height: 100dvh;
  color: var(--text);
  overflow-x: hidden;
  background:
    radial-gradient(circle at 15% 10%, rgb(139 92 246 / 0.14), transparent 28rem),
    radial-gradient(circle at 90% 85%, rgb(34 211 238 / 0.08), transparent 24rem),
    var(--bg);
}

button,
dialog {
  font: inherit;
}

button {
  color: inherit;
}

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

.app-shell {
  width: min(1120px, 100%);
  min-height: 100dvh;
  margin: 0 auto;
  padding: clamp(12px, 2.2vmin, 24px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: clamp(10px, 1.8vmin, 18px);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 42px;
  aspect-ratio: 1;
  border-radius: 13px;
  display: grid;
  place-items: center;
  font-weight: 900;
  background: linear-gradient(145deg, var(--accent), #6d28d9);
  box-shadow: 0 10px 28px rgb(139 92 246 / 0.28);
}

.brand h1 {
  margin: 0;
  font-size: clamp(1.5rem, 3vmin, 2.35rem);
  letter-spacing: -0.04em;
}

.brand p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: clamp(0.75rem, 1.5vmin, 0.95rem);
}

.icon-button,
.secondary-button,
.primary-button {
  border: 0;
  cursor: pointer;
  transition: transform 160ms ease, background-color 160ms ease, opacity 160ms ease;
}

.icon-button:hover,
.secondary-button:hover,
.primary-button:hover {
  transform: translateY(-1px);
}

.icon-button:active,
.secondary-button:active,
.primary-button:active {
  transform: translateY(0);
}

.icon-button {
  width: 42px;
  aspect-ratio: 1;
  border-radius: 13px;
  display: grid;
  place-items: center;
  background: var(--surface-2);
  border: 1px solid rgb(255 255 255 / 0.06);
}

.main-layout {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(210px, 0.72fr) minmax(300px, 1.55fr) minmax(210px, 0.72fr);
  gap: clamp(12px, 2vmin, 22px);
  align-items: center;
}

.side-panel {
  display: grid;
  gap: 12px;
  align-content: center;
}

.score-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.score-card,
.info-card {
  background: linear-gradient(160deg, rgb(255 255 255 / 0.045), transparent), var(--surface);
  border: 1px solid rgb(255 255 255 / 0.06);
  box-shadow: var(--shadow);
}

.score-card {
  min-width: 0;
  padding: 14px 12px;
  border-radius: 16px;
  text-align: center;
}

.score-card span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
}

.score-card strong {
  display: block;
  margin-top: 3px;
  font-size: clamp(1.25rem, 2.6vmin, 2rem);
  line-height: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}

.info-card {
  padding: 16px;
  border-radius: 18px;
}

.info-card h2 {
  margin: 0 0 6px;
  font-size: 0.95rem;
}

.info-card p {
  margin: 0;
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.55;
}

.actions {
  display: grid;
  gap: 9px;
}

.primary-button,
.secondary-button {
  min-height: 44px;
  border-radius: 13px;
  padding: 0 15px;
  font-weight: 800;
}

.primary-button {
  background: linear-gradient(135deg, var(--accent), #7c3aed);
  box-shadow: 0 12px 30px rgb(124 58 237 / 0.22);
}

.secondary-button {
  background: var(--surface-2);
  border: 1px solid rgb(255 255 255 / 0.07);
}

.secondary-button:disabled {
  opacity: 0.42;
  cursor: not-allowed;
  transform: none;
}

.game-area {
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
}

.board-wrap {
  position: relative;
  width: var(--board-size);
  max-width: 100%;
  aspect-ratio: 1;
}

.game-board {
  --cell-size: calc((100% - (var(--gap) * 5)) / 4);
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: var(--radius);
  padding: var(--gap);
  background: var(--board);
  box-shadow: 0 28px 85px rgb(0 0 0 / 0.38), inset 0 1px rgb(255 255 255 / 0.055);
  touch-action: none;
  user-select: none;
  overflow: hidden;
}

.board-cell,
.tile {
  position: absolute;
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: clamp(10px, 2vmin, 16px);
}

.board-cell {
  left: var(--x);
  top: var(--y);
  background: var(--cell);
}

.tile {
  left: var(--x);
  top: var(--y);
  z-index: 3;
  display: grid;
  place-items: center;
  font-size: clamp(1.55rem, calc(var(--board-size) / 10), 3.6rem);
  font-weight: 900;
  letter-spacing: -0.055em;
  color: #14161b;
  box-shadow: inset 0 1px rgb(255 255 255 / 0.28), 0 8px 20px rgb(0 0 0 / 0.14);
  transition: left 120ms ease, top 120ms ease, transform 120ms ease;
}

.tile-font-sm { font-size: clamp(1.2rem, calc(var(--board-size) / 12.7), 2.8rem); }
.tile-font-xs { font-size: clamp(0.95rem, calc(var(--board-size) / 15), 2.25rem); }
.tile-new { animation: tile-new 150ms ease-out; }
.tile-merged { animation: tile-merged 190ms ease-out; }

.tile-2 { background: #d9e2ec; }
.tile-4 { background: #bfdbfe; }
.tile-8 { background: #67e8f9; }
.tile-16 { background: #5eead4; }
.tile-32 { background: #86efac; }
.tile-64 { background: #fde047; }
.tile-128 { background: #fdba74; }
.tile-256 { background: #fb923c; color: #fff; }
.tile-512 { background: #f87171; color: #fff; }
.tile-1024 { background: #e879f9; color: #fff; }
.tile-2048 { background: linear-gradient(145deg, #a78bfa, #7c3aed); color: #fff; box-shadow: 0 0 34px rgb(139 92 246 / 0.48); }
.tile-4096 { background: linear-gradient(145deg, #818cf8, #4338ca); color: #fff; }
.tile-8192 { background: linear-gradient(145deg, #22d3ee, #0e7490); color: #fff; }

.game-overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  border-radius: var(--radius);
  display: grid;
  place-items: center;
  padding: 22px;
  background: rgb(10 13 18 / 0.76);
  backdrop-filter: blur(7px);
}

.game-overlay[hidden] {
  display: none;
}

.overlay-card {
  width: min(360px, 92%);
  padding: 22px;
  border-radius: 20px;
  text-align: center;
  background: var(--surface);
  border: 1px solid rgb(255 255 255 / 0.09);
  box-shadow: var(--shadow);
}

.overlay-card h2 {
  margin: 0 0 8px;
  font-size: clamp(1.3rem, 3.3vmin, 2rem);
}

.overlay-card p {
  margin: 0 0 18px;
  color: var(--muted);
  line-height: 1.5;
}

.overlay-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.footer-hint {
  min-height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.78rem;
  text-align: center;
}

kbd {
  min-width: 1.7em;
  display: inline-grid;
  place-items: center;
  margin: 0 2px;
  padding: 2px 5px;
  border-radius: 6px;
  color: var(--text);
  background: var(--surface-3);
  border: 1px solid rgb(255 255 255 / 0.08);
  box-shadow: inset 0 -1px rgb(0 0 0 / 0.25);
}

.help-dialog {
  width: min(480px, calc(100% - 28px));
  padding: 0;
  border: 1px solid rgb(255 255 255 / 0.09);
  border-radius: 22px;
  color: var(--text);
  background: var(--surface);
  box-shadow: 0 28px 90px rgb(0 0 0 / 0.55);
}

.help-dialog::backdrop {
  background: rgb(0 0 0 / 0.68);
  backdrop-filter: blur(4px);
}

.help-content {
  padding: 22px;
}

.help-content h2 {
  margin: 0 0 10px;
}

.help-content p,
.help-content li {
  color: var(--muted);
  line-height: 1.55;
}

.help-content ul {
  padding-left: 20px;
}

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

@keyframes tile-new {
  from { transform: scale(0.72); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@keyframes tile-merged {
  0% { transform: scale(1); }
  55% { transform: scale(1.13); }
  100% { transform: scale(1); }
}

@media (max-width: 820px) {
  :root {
    --board-size: min(92vw, 62dvh, 520px);
  }

  body {
    overflow-y: auto;
  }

  .app-shell {
    min-height: 100dvh;
    grid-template-rows: auto auto auto auto;
  }

  .main-layout {
    display: contents;
  }

  .side-panel:first-of-type {
    order: 1;
  }

  .game-area {
    order: 2;
  }

  .side-panel:last-of-type {
    order: 3;
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .score-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .info-card {
    display: none;
  }

  .actions {
    grid-template-columns: repeat(3, 1fr);
  }

  .primary-button,
  .secondary-button {
    min-height: 42px;
    padding-inline: 10px;
    font-size: 0.82rem;
  }

  .footer-hint {
    order: 4;
  }
}

@media (max-width: 560px) {
  :root {
    --board-size: min(94vw, 59dvh, 470px);
    --radius: 18px;
  }

  .app-shell {
    padding: 10px;
    gap: 9px;
  }

  .brand p {
    display: none;
  }

  .brand-mark,
  .icon-button {
    width: 38px;
  }

  .score-card {
    padding: 10px;
  }

  .side-panel:last-of-type {
    grid-template-columns: 1fr;
  }

  .actions {
    grid-template-columns: repeat(3, 1fr);
  }

  .footer-hint {
    font-size: 0.72rem;
  }

  .desktop-only {
    display: none;
  }
}

@media (max-height: 700px) and (min-width: 821px) {
  :root {
    --board-size: min(68vw, 60dvh, 470px);
  }

  .app-shell {
    padding-block: 10px;
    gap: 8px;
  }

  .brand-mark,
  .icon-button {
    width: 36px;
  }

  .info-card {
    padding: 12px;
  }
}

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