:root {
  color-scheme: light dark;
  --bg: #faf8f5;
  --surface: #ffffff;
  --board: #d8cfc4;
  --cell: #eae3da;
  --text: #1b1a18;
  --muted: #6c6862;
  --accent: #7b5cff;
  --accent-text: #ffffff;
  --line: rgba(0,0,0,.1);
  --gap: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16151a;
    --surface: #201f26;
    --board: #2b2933;
    --cell: #343240;
    --text: #ece9f2;
    --muted: #9d98ab;
    --accent: #9b83ff;
    --line: rgba(255,255,255,.1);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  user-select: none;
}

.screen {
  display: none;
  flex: 1;
  flex-direction: column;
  padding: 18px;
  min-height: 0;
  padding-top: calc(18px + env(safe-area-inset-top, 0px));
  padding-bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  max-width: 560px;
  width: 100%;
  margin: 0 auto;
}
.screen.active { display: flex; }

.brand { font-size: 30px; font-weight: 800; letter-spacing: -.02em; margin: 8vh 0 2px; text-align: center; }
.tagline { text-align: center; color: var(--muted); font-size: 14px; margin-bottom: 32px; }

.menu { display: flex; flex-direction: column; gap: 12px; }

button {
  font: inherit;
  cursor: pointer;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  padding: 14px 18px;
  transition: transform .08s ease, opacity .15s ease;
}
button:active { transform: scale(.985); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }
button.ghost { background: transparent; }
button.small { padding: 8px 12px; font-size: 14px; border-radius: 9px; }

.topbar { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.topbar h2 { font-size: 17px; margin: 0; flex: 1; }

.stats { display: flex; gap: 10px; margin-bottom: 14px; }
.stat { flex: 1; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 9px 12px; }
.stat .k { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.stat .v { font-size: 21px; font-weight: 700; font-variant-numeric: tabular-nums; }

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  background: var(--board);
  border-radius: 14px;
  --gap: 10px;
  touch-action: none;
  margin: 0 auto;
  max-width: min(92vw, 58vh);
}

.grid {
  position: absolute;
  inset: 0;
  padding: var(--gap);
  display: grid;
  grid-template-columns: repeat(var(--n, 4), 1fr);
  grid-template-rows: repeat(var(--n, 4), 1fr);
  gap: var(--gap);
}
.cell { background: var(--cell); border-radius: max(4px, calc(var(--cell-size, 70px) * .13)); }

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

.tile {
  position: absolute;
  width: var(--cell-size, 70px);
  height: var(--cell-size, 70px);
  border-radius: max(4px, calc(var(--cell-size, 70px) * .13));
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-size: clamp(15px, calc(var(--cell-size, 70px) * .38), 34px);
  background: #efe6da;
  color: #4a4238;
  transition: transform .11s ease-out;
  will-change: transform;
}

.tile[data-value="2"]    { background:#efe6da; color:#4a4238; }
.tile[data-value="4"]    { background:#e9dcc4; color:#4a4238; }
.tile[data-value="8"]    { background:#f2b179; color:#fff; }
.tile[data-value="16"]   { background:#f59563; color:#fff; }
.tile[data-value="32"]   { background:#f57c5f; color:#fff; }
.tile[data-value="64"]   { background:#f45b37; color:#fff; }
.tile[data-value="128"]  { background:#6fc4a8; color:#fff; font-size: clamp(13px, calc(var(--cell-size, 70px)*.32), 28px); }
.tile[data-value="256"]  { background:#4fb08f; color:#fff; font-size: clamp(13px, calc(var(--cell-size, 70px)*.32), 28px); }
.tile[data-value="512"]  { background:#3f9c8a; color:#fff; font-size: clamp(13px, calc(var(--cell-size, 70px)*.32), 28px); }
.tile[data-value="1024"] { background:#6b8dff; color:#fff; font-size: clamp(11px, calc(var(--cell-size, 70px)*.26), 23px); }
.tile[data-value="2048"] { background:#7b5cff; color:#fff; font-size: clamp(11px, calc(var(--cell-size, 70px)*.26), 23px); }
.tile[data-value="4096"] { background:#a24fd0; color:#fff; font-size: clamp(11px, calc(var(--cell-size, 70px)*.26), 23px); }
.tile[data-value="8192"] { background:#d0417a; color:#fff; font-size: clamp(11px, calc(var(--cell-size, 70px)*.26), 23px); }

.tile.is-new { animation: pop .16s ease-out; }
.tile.is-merged { animation: bump .16s ease-out; }

@keyframes pop { from { opacity: 0; transform: scale(.5) } }
@keyframes bump { 50% { scale: 1.12 } }

@media (prefers-reduced-motion: reduce) {
  .tile { transition: none; animation: none !important; }
}

.overlay {
  display: none;
  margin-top: 14px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  text-align: center;
}
.overlay.show { display: block; }
.overlay h3 { margin: 0 0 4px; font-size: 20px; }
.overlay p { margin: 0 0 12px; color: var(--muted); font-size: 14px; }

.hint { text-align: center; color: var(--muted); font-size: 13px; margin-top: 14px; }

.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; gap: 12px; align-items: baseline; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.list .rank { width: 26px; color: var(--muted); font-variant-numeric: tabular-nums; }
.list .name { flex: 1; }
.list .sc { font-weight: 700; font-variant-numeric: tabular-nums; }
.list li.me { background: color-mix(in srgb, var(--accent) 12%, transparent); border-radius: 8px; }

.note { color: var(--muted); font-size: 14px; text-align: center; padding: 24px 12px; }
.name-row { display: flex; gap: 8px; align-items: center; margin-bottom: 16px; }
.name-row .who { flex: 1; font-weight: 600; }

.name-pick {
  text-align: center;
  font-size: 27px;
  font-weight: 800;
  letter-spacing: -.01em;
  font-variant-numeric: tabular-nums;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 22px 14px;
  margin-bottom: 10px;
  overflow-wrap: anywhere;
}
.name-warn { margin: 0 0 26px; }

.versus.screen { max-width: 900px; overflow: hidden; }

.versus .arena {
  display: flex;
  gap: 12px;
  flex: 1;
  min-height: 0;
  align-items: flex-start;
  justify-content: center;
}

.versus .side {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.versus .side-head {
  display: flex;
  justify-content: center;
  align-items: baseline;
  gap: 12px;
  margin: 0 auto 6px;
  width: 100%;
  white-space: nowrap;
  font-size: 13px;
}
.versus .side-head .who { font-weight: 600; }
.versus .side-head .nums { color: var(--muted); font-variant-numeric: tabular-nums; }
.versus .side-head b { color: var(--text); }

.versus .board {
  --gap: 7px;
  max-width: none;
  width: var(--vs-size, 100%);
  height: var(--vs-size, auto);
  margin-inline: auto;
}

.rivalry {
  display: flex;
  justify-content: center;
  gap: 14px;
  font-size: 14px;
  color: var(--muted);
  margin-bottom: 12px;
}
.rivalry b { color: var(--text); font-variant-numeric: tabular-nums; }

@media (orientation: landscape) {
  .versus.screen {
    max-width: none;
    padding-top: calc(6px + env(safe-area-inset-top, 0px));
    padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px));
    padding-left: calc(12px + env(safe-area-inset-left, 0px));
    padding-right: calc(12px + env(safe-area-inset-right, 0px));
  }
  .versus .topbar { margin-bottom: 4px; }
  .versus .topbar h2 { font-size: 15px; }
  .versus .rivalry { margin-bottom: 4px; font-size: 13px; }
  .versus .side-head { margin-bottom: 4px; font-size: 12px; }
  .versus .arena { gap: 18px; }
  .versus .board { --gap: 6px; }
  .versus .hint { display: none; }
  .versus .overlay {
    margin-top: 8px;
    padding: 8px 14px;
    align-items: center;
    gap: 16px;
    text-align: left;
  }
  .versus .overlay.show { display: flex; }
  .versus .overlay h3 { margin: 0; font-size: 16px; white-space: nowrap; }
  .versus .overlay p { margin: 0; font-size: 12.5px; flex: 1; }
  .versus .overlay button { padding: 9px 18px; font-size: 14px; }
}

@media (orientation: portrait) and (max-width: 820px) {
  .versus .arena { gap: 8px; }
  .versus .side-head { font-size: 11.5px; }
  .versus .board { --gap: 5px; }
}
