/* 2048 — warm, minimal, tactile. Ceramic tiles on matte paper. The colours
   of the chosen theme arrive as custom properties from themes.js. */

:root {
  --bg-a: #efe6d8;
  --bg-b: #dfd4c2;
  --blob-1: rgba(255, 250, 240, 0.9);
  --blob-2: rgba(240, 190, 120, 0.35);
  --blob-3: rgba(200, 180, 150, 0.35);
  --paper: #f8f3eb;
  --paper-2: #ede5d8;
  --ink: #3a3128;
  --ink-soft: #7a6c5d;
  --line: rgba(60, 45, 30, 0.12);
  --frame: #c9b79e;
  --frame-edge: #b39f84;
  --cell: #ddd0bb;
  --accent: #c47a3a;
  --accent-ink: #fff8f0;
  --t1: #f7f0e4; --t1-ink: #6f5e4b;
  --t11: #d8ab22; --t11-ink: #fffaf3;
  --radius: 18px;
  --ease: cubic-bezier(0.4, 0.1, 0.2, 1);
  --slide: 130ms;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --shadow-ink: 60, 45, 30;
}
:root[data-theme="slate"] { --shadow-ink: 0, 0, 0; color-scheme: dark; }
:root[data-theme="sorbet"] { --shadow-ink: 90, 60, 90; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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

body {
  font-family: var(--sans);
  color: var(--ink);
  background: linear-gradient(165deg, var(--bg-a), var(--bg-b));
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  transition: background 0.8s, color 0.4s;
}

/* ---------------------------------------------------------- background */
.bg {
  position: fixed;
  inset: 0;
  overflow: hidden;
  z-index: 0;
  pointer-events: none;
}
.bg .blob {
  position: absolute;
  border-radius: 50%;
  filter: blur(60px);
  opacity: 0.7;
  will-change: transform;
  transition: background 0.8s;
}
.bg .b1 { width: 60vmax; height: 60vmax; left: -20vmax; top: -25vmax; background: var(--blob-1); animation: drift 48s ease-in-out infinite alternate; }
.bg .b2 { width: 50vmax; height: 50vmax; right: -20vmax; bottom: -20vmax; background: var(--blob-2); animation: drift 60s ease-in-out infinite alternate-reverse; }
.bg .b3 { width: 40vmax; height: 40vmax; left: 30vw; bottom: -25vmax; background: var(--blob-3); animation: drift 72s ease-in-out infinite alternate; }
.bg .ring {
  position: absolute;
  border-radius: 28%;
  border: 1.5px solid rgba(var(--shadow-ink), 0.07);
  will-change: transform;
}
.bg .r1 { width: 38vmax; height: 38vmax; right: -12vmax; top: -8vmax; animation: turn 140s linear infinite; }
.bg .r2 { width: 26vmax; height: 26vmax; left: -8vmax; bottom: 6vmax; animation: turn 110s linear infinite reverse; }
:root[data-theme="slate"] .bg .ring { border-color: rgba(255, 255, 255, 0.05); }
@keyframes drift {
  from { transform: translate(0, 0) scale(1); }
  to { transform: translate(8vmax, 6vmax) scale(1.12); }
}
@keyframes turn { to { transform: rotate(360deg); } }

/* -------------------------------------------------------------- layout */
.app {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  max-width: 760px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px 4px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { width: 40px; height: 40px; border-radius: 10px; filter: drop-shadow(0 2px 3px rgba(var(--shadow-ink), 0.25)); }
.brand span { display: flex; flex-direction: column; line-height: 1.1; }
.brand b { font-family: var(--serif); font-weight: 600; font-size: 24px; letter-spacing: 0.04em; }
.brand small { font-size: 11.5px; color: var(--ink-soft); letter-spacing: 0.08em; text-transform: uppercase; margin-top: 2px; }
.menu { display: flex; gap: 4px; }

.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.2s, transform 0.2s;
}
.icon-btn:hover { background: rgba(var(--shadow-ink), 0.08); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn svg, .btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 4px 16px 6px;
}
.stat {
  position: relative;
  background: rgba(255, 255, 255, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.5);
  box-shadow: 0 1px 2px rgba(var(--shadow-ink), 0.06);
  border-radius: 12px;
  padding: 6px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.15;
  transition: background 0.4s, border-color 0.4s;
}
:root[data-theme="slate"] .stat { background: rgba(255, 255, 255, 0.06); border-color: rgba(255, 255, 255, 0.1); }
.stat span { font-size: 10.5px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-soft); }
.stat b { font-family: var(--serif); font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat b.bump { animation: bump 0.3s var(--ease); }
@keyframes bump { 40% { transform: scale(1.18); } }
.float-score {
  position: absolute;
  left: 50%;
  top: 0;
  transform: translate(-50%, 0);
  font: 600 15px var(--serif);
  color: var(--accent);
  opacity: 0;
  pointer-events: none;
  font-style: normal;
}
.float-score.go { animation: floatup 0.8s ease-out; }
@keyframes floatup { 0% { opacity: 0; transform: translate(-50%, 4px); } 20% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%, -28px); } }

.stage {
  flex: 1;
  min-height: 0;
  container-type: size;
  display: grid;
  place-items: center;
  padding: 8px 16px;
  touch-action: none;
}

.actions {
  display: flex;
  gap: 10px;
  padding: 6px 16px 14px;
}
.btn {
  appearance: none;
  flex: 1;
  border: 1px solid rgba(var(--shadow-ink), 0.14);
  background: rgba(255, 255, 255, 0.55);
  color: var(--ink);
  font: 500 15px var(--sans);
  padding: 11px 12px;
  border-radius: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(var(--shadow-ink), 0.08);
  transition: transform 0.15s, background 0.2s, opacity 0.2s, box-shadow 0.2s;
}
:root[data-theme="slate"] .btn { background: rgba(255, 255, 255, 0.08); border-color: rgba(255, 255, 255, 0.12); }
.btn svg { width: 19px; height: 19px; }
.btn:hover { background: rgba(255, 255, 255, 0.8); }
:root[data-theme="slate"] .btn:hover { background: rgba(255, 255, 255, 0.14); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: default; transform: none; }
.btn.primary, :root[data-theme="slate"] .btn.primary {
  background: linear-gradient(180deg, color-mix(in oklab, var(--accent) 82%, white), var(--accent));
  color: var(--accent-ink);
  border-color: color-mix(in oklab, var(--accent) 70%, black);
  box-shadow: 0 4px 12px -4px color-mix(in oklab, var(--accent) 70%, black), inset 0 1px 0 rgba(255, 255, 255, 0.3);
}
.btn.primary:hover { filter: brightness(1.06); }
.btn.subtle { flex: 0 0 auto; background: transparent; box-shadow: none; color: var(--ink-soft); font-size: 13.5px; margin: 18px auto 0; display: flex; }

/* --------------------------------------------------------------- board */
.board {
  --pad: 3cqw;
  --gap: 2.6cqw;
  width: min(100cqw, 100cqh);
  aspect-ratio: 1;
  position: relative;
  container-type: inline-size;
  border-radius: 4.2cqw;
  background: linear-gradient(170deg, color-mix(in oklab, var(--frame) 88%, white), var(--frame) 55%, color-mix(in oklab, var(--frame) 92%, black));
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.35),
    inset 0 -2px 0 rgba(var(--shadow-ink), 0.12),
    0 0 0 1px var(--frame-edge),
    0 24px 44px -20px rgba(var(--shadow-ink), 0.55),
    0 6px 14px -6px rgba(var(--shadow-ink), 0.3);
  transition: background 0.6s, box-shadow 0.6s;
}
.board.nudge { animation: nudge 0.3s var(--ease); }
@keyframes nudge {
  30% { translate: calc(var(--nx) * 9px) calc(var(--ny) * 9px); }
  65% { translate: calc(var(--nx) * -3px) calc(var(--ny) * -3px); }
  100% { translate: 0 0; }
}
.board.flash::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: rgba(255, 255, 255, 0.35);
  animation: fadeout 0.4s ease-out forwards;
  pointer-events: none;
}
@keyframes fadeout { to { opacity: 0; } }

.cells {
  position: absolute;
  inset: var(--pad);
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  gap: var(--gap);
}
.cell {
  border-radius: 12%;
  background: var(--cell);
  box-shadow: inset 0 1.5px 3px rgba(var(--shadow-ink), 0.14), inset 0 -1px 0 rgba(255, 255, 255, 0.25);
  transition: background 0.6s;
}

.tiles {
  position: absolute;
  inset: var(--pad);
  touch-action: none;
}
.tile, .float {
  position: absolute;
  left: 0;
  top: 0;
  width: calc((100% - (var(--n) - 1) * var(--gap)) / var(--n));
  height: calc((100% - (var(--n) - 1) * var(--gap)) / var(--n));
  transform: translate(calc(var(--c) * (100% + var(--gap))), calc(var(--r) * (100% + var(--gap))));
}
.tile {
  --tile: var(--t1);
  --tile-ink: var(--t1-ink);
  display: grid;
  place-items: center;
  border-radius: 12%;
  font-family: var(--serif);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  color: var(--tile-ink);
  background:
    radial-gradient(120% 90% at 30% 10%, rgba(255, 255, 255, 0.28), rgba(255, 255, 255, 0) 60%),
    linear-gradient(165deg, color-mix(in oklab, var(--tile) 90%, white), var(--tile) 50%, color-mix(in oklab, var(--tile) 88%, black));
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.4),
    inset 0 -0.05em 0 rgba(0, 0, 0, 0.1),
    0 0.05em 0.1em rgba(var(--shadow-ink), 0.22),
    0 0.22em 0.4em -0.12em rgba(var(--shadow-ink), 0.35);
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.18);
  transition: transform var(--slide) var(--ease), background 0.6s, color 0.6s;
  will-change: transform;
  z-index: 2;
  font-size: calc(var(--fs) * 4 / var(--n) * 1cqw);
  --fs: 11.5;
}
.tile[data-len="2"] { --fs: 10.5; }
.tile[data-len="3"] { --fs: 8.4; }
.tile[data-len="4"] { --fs: 6.6; }
.tile[data-len="5"] { --fs: 5.4; }
.tile[data-len="6"] { --fs: 4.6; }
:root[data-theme="slate"] .tile {
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.22),
    0 0.05em 0.1em rgba(0, 0, 0, 0.35),
    0 0 0.55em color-mix(in oklab, var(--tile) 45%, transparent);
}
:root[data-theme="slate"] .tile[data-len="1"], :root[data-theme="slate"] .tile[data-v="2"], :root[data-theme="slate"] .tile[data-v="4"] { box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12), 0 0.05em 0.1em rgba(0, 0, 0, 0.35); }
.tile[data-v="2048"] { box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.5), 0 0.05em 0.1em rgba(var(--shadow-ink), 0.22), 0 0 0.6em color-mix(in oklab, var(--tile) 60%, transparent); }
.tile.new { animation: appear 0.2s var(--ease) backwards; animation-delay: 0.02s; }
@keyframes appear { from { scale: 0.3; opacity: 0; } }
.tile.merged { z-index: 3; animation: pop 0.24s var(--ease); }
@keyframes pop { 0% { scale: 0.9; } 45% { scale: 1.16; } 100% { scale: 1; } }
.tile.going { z-index: 1; }

.float {
  display: grid;
  place-items: center;
  pointer-events: none;
  z-index: 5;
  font: 600 calc(5.2 * 4 / var(--n) * 1cqw) var(--serif);
  color: var(--ink);
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
  transition: none;
}
.float i { font-style: normal; animation: rise-pts 0.75s ease-out forwards; }
@keyframes rise-pts { 0% { opacity: 0; translate: 0 10%; } 25% { opacity: 1; } 100% { opacity: 0; translate: 0 -70%; } }
:root[data-theme="slate"] .float { text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6); color: #fff; }

/* -------------------------------------------------------- sheets, dialogs */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(var(--shadow-ink), 0.38);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  animation: fade 0.25s;
}
@keyframes fade { from { opacity: 0; } }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0 auto;
  z-index: 21;
  max-width: 560px;
  max-height: 86dvh;
  display: flex;
  flex-direction: column;
  background: var(--paper);
  color: var(--ink);
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -10px 40px rgba(var(--shadow-ink), 0.25);
  padding-bottom: env(safe-area-inset-bottom);
  animation: rise 0.32s var(--ease);
}
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
.sheet header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 12px 6px 22px;
  flex: 0 0 auto;
}
.sheet h2, .dialog h2 { font-family: var(--serif); font-weight: 600; font-size: 24px; margin: 0; letter-spacing: 0.01em; }
.sheet-body {
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 2px 22px 26px;
  font-size: 15px;
  line-height: 1.55;
}
.sheet-body h3 { font-family: var(--serif); font-weight: 600; font-size: 17px; margin: 20px 0 8px; color: var(--ink); }
.prose p { margin: 0 0 12px; }
.prose ul { margin: 0; padding-left: 20px; }
.prose li { margin: 4px 0; }
.muted { color: var(--ink-soft); }
.about { font-size: 13px; margin-top: 22px; text-align: center; }

@media (min-width: 700px) and (min-height: 560px) {
  .sheet {
    bottom: auto;
    top: 50%;
    transform: translateY(-50%);
    border-radius: 24px;
    max-height: 80dvh;
    animation: pop-sheet 0.28s var(--ease);
  }
}
@keyframes pop-sheet { from { opacity: 0; transform: translateY(-50%) scale(0.96); } }

.dialog {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 22;
  width: min(92vw, 400px);
  background: var(--paper);
  border-radius: 24px;
  padding: 26px 24px 22px;
  box-shadow: 0 20px 60px rgba(var(--shadow-ink), 0.35);
  text-align: center;
  animation: popc 0.3s var(--ease);
  overflow: hidden;
}
@keyframes popc { from { opacity: 0; transform: translate(-50%, -50%) scale(0.94); } }
.dialog p { margin: 8px 0 18px; color: var(--ink-soft); font-size: 15.5px; line-height: 1.5; }
.dialog .row { display: flex; gap: 10px; }
.result .glow {
  position: absolute;
  inset: -40% -20% auto;
  height: 70%;
  background: radial-gradient(ellipse at 50% 40%, color-mix(in oklab, var(--t11) 45%, transparent), transparent 65%);
  pointer-events: none;
  opacity: 0.8;
}
.result[data-kind="over"] .glow { opacity: 0.2; }
.hero-tile {
  --tile: var(--t11);
  --tile-ink: var(--t11-ink);
  position: relative;
  width: 84px;
  height: 84px;
  margin: 0 auto 14px;
  display: grid;
  place-items: center;
  border-radius: 12%;
  font: 600 26px var(--serif);
  letter-spacing: -0.02em;
  color: var(--tile-ink);
  background:
    radial-gradient(120% 90% at 30% 10%, rgba(255, 255, 255, 0.28), rgba(255, 255, 255, 0) 60%),
    linear-gradient(165deg, color-mix(in oklab, var(--tile) 90%, white), var(--tile) 50%, color-mix(in oklab, var(--tile) 88%, black));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 4px 10px -2px rgba(var(--shadow-ink), 0.35), 0 0 24px color-mix(in oklab, var(--tile) 45%, transparent);
}
.hero-tile[data-len="5"], .hero-tile[data-len="6"] { font-size: 20px; }
.result[data-kind="won"] .hero-tile { animation: hero 0.6s var(--ease); }
@keyframes hero { 0% { scale: 0.5; rotate: -8deg; opacity: 0; } 60% { scale: 1.1; rotate: 2deg; } 100% { scale: 1; rotate: 0deg; } }
.result h2 { position: relative; font-size: 30px; }
.result-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin-bottom: 12px;
}
.result-stats div, .summary div { display: flex; flex-direction: column; align-items: center; background: var(--paper-2); border-radius: 12px; padding: 8px 4px; line-height: 1.2; }
.result-stats span, .summary span { font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-soft); }
.result-stats b, .summary b { font-family: var(--serif); font-size: 19px; font-weight: 600; }
.final-score { display: flex; flex-direction: column; align-items: center; margin: 4px 0 18px; }
.final-score span { font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft); }
.final-score b { font-family: var(--serif); font-size: 44px; font-weight: 600; line-height: 1.05; }

/* ------------------------------------------------------------- results */
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; font-variant-numeric: tabular-nums; }
th { text-align: left; vertical-align: bottom; font-weight: 500; color: var(--ink-soft); font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; padding: 4px 4px; border-bottom: 1px solid var(--line); }
td { padding: 7px 4px; border-bottom: 1px solid var(--line); white-space: nowrap; vertical-align: middle; }
th.c-tile, th.c-score, th.c-moves, th.c-time, th.c-undos, td.c-tile, td.c-score, td.c-moves, td.c-time, td.c-undos { text-align: right; }
td.c-score { font-family: var(--serif); font-weight: 600; font-size: 15px; }
td.c-tile { font-family: var(--serif); font-weight: 600; }
tr[data-won="true"] td.c-tile { color: color-mix(in oklab, var(--t11) 70%, var(--ink)); }
@media (max-width: 420px) { th.c-undos, td.c-undos, th.c-moves, td.c-moves { display: none; } td { padding: 7px 3px; } }

/* ------------------------------------------------------------- settings */
.opts { display: flex; flex-wrap: wrap; gap: 8px; }
.opt {
  appearance: none;
  border: 1.5px solid var(--line);
  background: var(--paper-2);
  color: var(--ink);
  font: 500 14px var(--sans);
  padding: 8px 14px 8px 10px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s, transform 0.15s;
}
.opt:active { transform: scale(0.97); }
.opt[aria-checked="true"] { border-color: var(--accent); background: var(--paper); box-shadow: 0 0 0 3px color-mix(in oklab, var(--accent) 25%, transparent); }
.opts.lang .opt, .opts.grid .opt { padding-left: 14px; min-width: 96px; justify-content: center; }
.swatch { display: inline-flex; gap: 2px; flex: 0 0 auto; }
.swatch i { width: 12px; height: 22px; border-radius: 4px; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08); }
.swatch i:first-child { border-radius: 8px 4px 4px 8px; }
.swatch i:last-child { border-radius: 4px 8px 8px 4px; }
.best-note { font-size: 13px; color: var(--ink-soft); margin: 8px 0 0; }

/* ---------------------------------------------------------------- toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(10px);
  z-index: 15;
  background: rgba(43, 36, 29, 0.92);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  max-width: min(88vw, 420px);
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ------------------------------------------------------- small screens */
@media (max-height: 640px) {
  .top { padding-top: 6px; }
  .brand small { display: none; }
  .brand b { font-size: 20px; }
  .stat b { font-size: 17px; }
  .actions { padding-bottom: 10px; }
  .btn { padding: 9px 10px; }
}
@media (max-width: 520px) {
  .btn { gap: 6px; padding: 9px 8px; font-size: 14px; }
  .btn span { white-space: nowrap; }
}
@media (min-width: 900px) and (min-height: 700px) {
  .brand b { font-size: 28px; }
  .stage { padding: 14px 16px; }
}

@media (prefers-reduced-motion: reduce) {
  :root { --slide: 10ms; }
  .bg .blob, .bg .ring { animation: none; }
  .tile, .sheet, .dialog, .backdrop, .board, .hero-tile, .float i, .stat b { animation: none; transition-duration: 0.01s; }
}
