:root {
  color-scheme: dark;
  --bg: #07101f;
  --ink: #f7f8fc;
  --muted: #b7c2d6;
  --card: #141c31;
  --line: rgba(255, 255, 255, 0.1);
  --move-ms: 160ms;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  max-width: 100%;
  overflow-x: clip;
}

body {
  font-family: "Segoe UI", "Avenir Next", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  background:
    radial-gradient(820px 420px at 50% -12%, rgba(92, 140, 255, 0.18), transparent 58%),
    radial-gradient(640px 360px at 100% 110%, rgba(255, 196, 84, 0.08), transparent 50%),
    var(--bg);
  color: var(--ink);
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left);
}

.shell {
  width: min(480px, 100%);
  margin: 0 auto;
  padding: 14px 16px 20px;
}

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

.brand {
  margin: 0;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #ff8ab4;
}

.mark {
  display: inline-block;
  width: 12px;
  height: 12px;
  margin-right: 8px;
  border-radius: 4px;
  vertical-align: -1px;
  background: linear-gradient(135deg, #6dffd8, #ffe566 55%, #ff8aa6);
}

h1 {
  margin: 2px 0 0;
  font-size: clamp(40px, 11vw, 60px);
  line-height: 0.9;
  letter-spacing: -0.05em;
}

.tagline, .hint, .fine-print, .dialog p, .share-menu p {
  color: var(--muted);
  line-height: 1.4;
}

.tagline { margin: 6px 0 0; max-width: 28ch; }

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

.score-card {
  position: relative;
  min-width: 88px;
  margin: 0;
  padding: 8px 12px 10px;
  border-radius: 16px;
  background: var(--card);
  box-shadow: inset 0 0 0 1px var(--line);
  text-align: center;
}

.score-card span {
  display: block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #c5d0e4;
}

.score-card strong {
  display: block;
  margin-top: 2px;
  font-size: 28px;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.04em;
  line-height: 1.1;
}

.score-card strong.bump { animation: bump 180ms ease; }

.gain {
  position: absolute;
  left: 50%;
  top: 4px;
  font-style: normal;
  font-weight: 800;
  font-size: 15px;
  color: #ffe566;
  opacity: 0;
  pointer-events: none;
}

.gain.float { animation: float-up 420ms ease-out forwards; }

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin: 12px 0 10px;
}

.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.hint { margin: 0; font-size: 14px; flex: 1 1 160px; }

button {
  font: inherit;
  font-weight: 700;
  min-height: 44px;
  padding: 10px 16px;
  border: 0;
  border-radius: 999px;
  background: #f7f8fc;
  color: #121826;
  cursor: pointer;
  touch-action: manipulation;
}

button.secondary {
  background: transparent;
  color: var(--ink);
  box-shadow: inset 0 0 0 1.5px rgba(255, 255, 255, 0.32);
}

#sound-toggle[aria-pressed="true"] {
  background: #6dffd8;
  color: #04281f;
}

#sound-toggle[aria-pressed="false"] {
  background: transparent;
  color: var(--ink);
  box-shadow: inset 0 0 0 1.5px rgba(255, 255, 255, 0.32);
}

button:hover { transform: translateY(-1px); }
button:active { transform: translateY(0) scale(0.98); }

button:focus-visible,
.board:focus-visible {
  outline: 3px solid #6dffd8;
  outline-offset: 3px;
}

.share-menu {
  margin: 0 0 12px;
  padding: 12px;
  border-radius: 18px;
  background: #121a31;
  box-shadow: inset 0 0 0 1px var(--line);
}

.share-menu p { margin: 0 0 10px; font-size: 14px; }

.share-actions,
.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.share-actions button,
.dialog-actions button { flex: 1 1 120px; }

.stage { width: 100%; }

.board {
  --pad: 12px;
  --gap: 12px;
  --cell: 72px;
  position: relative;
  width: min(100%, 420px);
  margin: 0 auto;
  aspect-ratio: 1;
  padding: var(--pad);
  border-radius: 28px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0)),
    #10182c;
  box-shadow:
    0 22px 50px rgba(0, 0, 0, 0.35),
    inset 0 0 0 1px rgba(255, 255, 255, 0.07);
  touch-action: none;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.board.shake { animation: shake 160ms linear; }

.cells, .tiles { position: absolute; inset: var(--pad); }

.cell, .tile {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--cell);
  height: var(--cell);
  transform: translate3d(
    calc(var(--c) * (var(--cell) + var(--gap))),
    calc(var(--r) * (var(--cell) + var(--gap))),
    0
  );
}

.cell {
  border-radius: calc(var(--cell) * 0.2);
  background: rgba(255, 255, 255, 0.05);
}

.tile {
  z-index: 1;
  transition: transform var(--move-ms) cubic-bezier(0.2, 0.75, 0.2, 1);
}

.tile.no-motion { transition: none; }
.tile.merging { z-index: 2; }

.face {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  border-radius: calc(var(--cell) * 0.2);
  background: var(--tile);
  color: var(--ink-tile, var(--ink));
  font-size: calc(var(--cell) * 0.42);
  font-weight: 800;
  letter-spacing: -0.045em;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.75),
    0 8px 14px rgba(0, 0, 0, 0.2);
}

.tile[data-digits="3"] .face { font-size: calc(var(--cell) * 0.34); }
.tile[data-digits="4"] .face { font-size: calc(var(--cell) * 0.26); }
.tile[data-digits="5"] .face,
.tile[data-digits="6"] .face { font-size: calc(var(--cell) * 0.2); }

.tile-2 { --tile: #6dffd8; --ink-tile: #04281f; }
.tile-4 { --tile: #53e4ff; --ink-tile: #032631; }
.tile-8 { --tile: #8eb6ff; --ink-tile: #0c1738; }
.tile-16 { --tile: #c4a6ff; --ink-tile: #241048; }
.tile-32 { --tile: #ff9eef; --ink-tile: #3d1040; }
.tile-64 { --tile: #ff8aa6; --ink-tile: #40101c; }
.tile-128 { --tile: #ffb07a; --ink-tile: #3d1a06; }
.tile-256 { --tile: #ffd36a; --ink-tile: #3d2804; }
.tile-512 { --tile: #ffe566; --ink-tile: #3d3204; }
.tile-1024 { --tile: #c6f56a; --ink-tile: #243806; }
.tile-2048 { --tile: #fff3b0; --ink-tile: #3d2c04; }
.tile-4096 { --tile: #ff8ac4; --ink-tile: #3d0a28; }
.tile-8192 { --tile: #e4d6ff; --ink-tile: #241450; }
.tile-super { --tile: #fff7d6; --ink-tile: #2a2208; }

.tile-256 .face,
.tile-512 .face { box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8), 0 0 14px rgba(255, 214, 80, 0.35); }
.tile-1024 .face { box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8), 0 0 16px rgba(198, 245, 106, 0.4); }
.tile-2048 .face { box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.9), 0 0 22px rgba(255, 220, 90, 0.7); }
.tile-4096 .face,
.tile-8192 .face,
.tile-super .face { box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.85), 0 0 22px rgba(255, 138, 196, 0.55); }

.face.spawn { animation: spawn 150ms ease-out; }
.face.merge { animation: merge 160ms ease-out; }

.status {
  min-height: 1.4em;
  margin: 10px 0 4px;
  color: #d5deef;
  font-size: 14px;
}

.manual-copy {
  margin: 8px 0 12px;
  padding: 12px;
  border-radius: 16px;
  background: #121a31;
  box-shadow: inset 0 0 0 1px var(--line);
}

.manual-copy label {
  display: block;
  font-size: 14px;
  color: #ffe566;
}

.manual-copy textarea {
  display: block;
  width: 100%;
  box-sizing: border-box;
  min-height: 96px;
  margin: 8px 0 0;
  padding: 10px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 12px;
  background: #0c1426;
  color: #f7f8fc;
  font: 13px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  resize: vertical;
  touch-action: manipulation;
  user-select: text;
  -webkit-user-select: text;
}

.board .overlay button {
  position: relative;
  z-index: 1;
  touch-action: manipulation;
  user-select: none;
}

.fine-print { margin: 0; font-size: 13px; }

.overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: center;
  padding: 10px;
  background: rgba(7, 16, 31, 0.62);
  border-radius: inherit;
  touch-action: manipulation;
  overflow: auto;
}

.overlay[hidden] { display: none; }

.dialog {
  width: min(100%, 320px);
  max-height: 100%;
  padding: 14px;
  border-radius: 20px;
  background: #182238;
  box-shadow:
    0 16px 40px rgba(0, 0, 0, 0.35),
    inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}

.dialog h2 {
  margin: 2px 0 6px;
  font-size: clamp(22px, 6vw, 30px);
  letter-spacing: -0.04em;
  line-height: 1.05;
}

.dialog p { margin: 0; font-size: 14px; }

.dialog-actions { margin-top: 12px; }

.dialog button:focus-visible {
  outline-offset: 1px;
}

.noscript {
  width: min(480px, calc(100% - 32px));
  margin: 12px auto;
  color: #ffe566;
}

@keyframes bump {
  50% { transform: scale(1.08); }
}

@keyframes float-up {
  0% { opacity: 0; transform: translateX(-50%) translateY(8px); }
  25% { opacity: 1; }
  100% { opacity: 0; transform: translateX(-50%) translateY(-14px); }
}

@keyframes spawn {
  from { transform: scale(0.35); opacity: 0.4; }
  to { transform: scale(1); opacity: 1; }
}

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

@keyframes shake {
  20% { transform: translateX(-3px); }
  40% { transform: translateX(3px); }
  60% { transform: translateX(-2px); }
  80% { transform: translateX(2px); }
}

@media (max-width: 380px) {
  .shell { padding: 12px 12px 16px; }
  .board { --pad: 8px; --gap: 8px; }
  .dialog { padding: 12px; }
  .dialog h2 { font-size: 22px; }
}

@media (prefers-reduced-motion: reduce) {
  .tile { transition: none; }
  .score-card strong.bump,
  .gain.float,
  .face.spawn,
  .face.merge,
  .board.shake,
  button:hover,
  button:active {
    animation: none;
    transform: none;
  }
}
