/* Threes — paper cards on a soft tray. Every colour arrives as a custom
   property from themes.js; the defaults below are the Paper theme. Board
   classes are prefixed t- so they never collide with the panels. */

:root {
  --bg-a: #f2eadb;
  --bg-b: #d9d3c3;
  --blob-1: #fffdf6;
  --blob-2: #bcd6f2;
  --blob-3: #f6c9d6;
  --ink: #2c2620;
  --ink-soft: #6f6457;
  --paper: #fbf7ef;
  --paper-2: #f0e9dc;
  --line: rgba(60, 45, 30, 0.12);
  --glass: rgba(255, 255, 255, 0.42);
  --glass-edge: rgba(255, 255, 255, 0.6);
  --tray: #e2d6bf;
  --tray-edge: #cbbd9f;
  --slot: rgba(85, 65, 35, 0.1);
  --card: #fdf9ee;
  --card-ink: #3a2f27;
  --one: #4f9ddd;
  --two: #ec6a8b;
  --face-ink: #fff;
  --accent: #5878b4;
  --accent-ink: #fff;
  --shadow-rgb: 60, 40, 20;
  --radius: 18px;
  --ease: cubic-bezier(0.3, 0.1, 0.2, 1);
  --slide: 0.18s;
  --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;
}

* { 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.6;
  will-change: transform;
  transition: background 0.8s;
}
.bg .b1 { width: 60vmax; height: 60vmax; left: -22vmax; top: -26vmax; background: var(--blob-1); animation: drift 50s ease-in-out infinite alternate; }
.bg .b2 { width: 48vmax; height: 48vmax; right: -18vmax; bottom: -18vmax; background: var(--blob-2); animation: drift 64s ease-in-out infinite alternate-reverse; }
.bg .b3 { width: 40vmax; height: 40vmax; left: 28vw; bottom: -24vmax; background: var(--blob-3); animation: drift 76s ease-in-out infinite alternate; }
@keyframes drift {
  from { transform: translate(0, 0) scale(1); }
  to { transform: translate(8vmax, 6vmax) scale(1.12); }
}

/* -------------------------------------------------------------- layout */
.app {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  max-width: 720px;
  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; filter: drop-shadow(0 2px 3px rgba(var(--shadow-rgb), 0.3)); }
.brand span { display: flex; flex-direction: column; line-height: 1.1; }
.brand b { font-family: var(--serif); font-weight: 600; font-size: 22px; letter-spacing: 0.02em; }
.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-rgb), 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 {
  background: var(--glass);
  border: 1px solid var(--glass-edge);
  box-shadow: 0 1px 2px rgba(var(--shadow-rgb), 0.06);
  border-radius: 12px;
  padding: 6px 8px;
  min-height: 54px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1.15;
  transition: background 0.4s, border-color 0.4s;
}
.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.next { padding-top: 4px; padding-bottom: 4px; gap: 2px; }
.stat b.bump { animation: bump 0.3s var(--ease); }
@keyframes bump { 40% { transform: scale(1.18); } }

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

.actions {
  display: flex;
  gap: 10px;
  padding: 6px 16px 14px;
}
.btn {
  appearance: none;
  flex: 1;
  border: 1px solid var(--line);
  background: var(--glass);
  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-rgb), 0.08);
  transition: transform 0.15s, background 0.2s, opacity 0.2s, box-shadow 0.2s;
}
.btn svg { width: 19px; height: 19px; }
.btn:hover { background: var(--glass-edge); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: default; transform: none; }
.btn.primary {
  background: linear-gradient(180deg, color-mix(in oklab, var(--accent) 80%, 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); background: linear-gradient(180deg, color-mix(in oklab, var(--accent) 80%, white), var(--accent)); }
.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; }

/* ---------------------------------------------------------------- cards */
.t-card, .t-mini {
  --face: var(--card);
  --fi: var(--card-ink);
}
.t-one { --face: var(--one); --fi: var(--face-ink); }
.t-two { --face: var(--two); --fi: var(--face-ink); }
.t-hi { --face: color-mix(in oklab, var(--card) 84%, #f0c468); }

.t-mini {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 32px;
  border-radius: 6px;
  font: 600 16px/1 var(--serif);
  color: var(--fi);
  background: linear-gradient(172deg, color-mix(in oklab, var(--face) 88%, white), var(--face) 55%, color-mix(in oklab, var(--face) 93%, black));
  box-shadow: 0 2px 0 color-mix(in oklab, var(--face) 72%, black), 0 3px 6px rgba(var(--shadow-rgb), 0.25);
  transition: background 0.25s, color 0.25s;
}
.t-mini.t-d3 { font-size: 12px; }
.t-mini.t-d4 { font-size: 10px; }
.t-mini.t-swap { animation: swap 0.3s var(--ease); }
@keyframes swap { from { transform: translateY(6px) scale(0.8); opacity: 0; } }

/* ---------------------------------------------------------------- board */
.t-board {
  width: min(100cqw, 100cqh);
  aspect-ratio: 1;
  position: relative;
  border-radius: 7%;
  background: linear-gradient(160deg, color-mix(in oklab, var(--tray) 94%, white), var(--tray) 50%, color-mix(in oklab, var(--tray) 92%, black));
  box-shadow:
    inset 0 0 0 1px color-mix(in oklab, var(--tray-edge) 70%, transparent),
    inset 0 2px 0 rgba(255, 255, 255, 0.35),
    inset 0 -3px 6px rgba(var(--shadow-rgb), 0.12),
    0 24px 44px -20px rgba(var(--shadow-rgb), 0.45),
    0 4px 10px -4px rgba(var(--shadow-rgb), 0.25);
  overflow: hidden;
  container-type: size;
  touch-action: none;
  cursor: grab;
  transition: background 0.6s, box-shadow 0.6s;
}
.t-board:active { cursor: grabbing; }
.t-board.t-nudge-left { animation: nudge-x 0.3s var(--ease); --nx: -6px; }
.t-board.t-nudge-right { animation: nudge-x 0.3s var(--ease); --nx: 6px; }
.t-board.t-nudge-up { animation: nudge-y 0.3s var(--ease); --ny: -6px; }
.t-board.t-nudge-down { animation: nudge-y 0.3s var(--ease); --ny: 6px; }
@keyframes nudge-x { 35% { transform: translateX(var(--nx)); } }
@keyframes nudge-y { 35% { transform: translateY(var(--ny)); } }

.t-slots, .t-cards {
  position: absolute;
  inset: 4%;
}
.t-slots { display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 1fr); }
.t-slot { position: relative; }
.t-slot::before {
  content: "";
  position: absolute;
  inset: 6%;
  border-radius: 14%;
  background: var(--slot);
  box-shadow: inset 0 1px 2px rgba(var(--shadow-rgb), 0.12), 0 1px 0 rgba(255, 255, 255, 0.18);
  transition: background 0.6s;
}
.t-cards { transition: transform 0.2s var(--ease); }
.t-cards.t-live { transition: none; }

.t-card {
  position: absolute;
  left: 0;
  top: 0;
  width: 25%;
  height: 25%;
  --ox: 0px;
  --oy: 0px;
  --lift: 0;
  transform: translate(calc(var(--c) * 100% + var(--ox)), calc(var(--r) * 100% + var(--oy)));
  transition: transform var(--slide) var(--ease);
  z-index: 2;
  will-change: transform;
}
.t-cards.t-live .t-card, .t-cards.t-instant .t-card, .t-cards.t-instant .t-face, .t-card.t-enter, .t-card.t-enter .t-face { transition: none; }
.t-card.t-moving { z-index: 3; }
.t-card.t-merging { z-index: 4; }
.t-card.t-enter { z-index: 1; }
.t-card.t-enter .t-face { transform: scale(0.7); opacity: 0.6; }

.t-face {
  position: absolute;
  inset: 6%;
  border-radius: 14%;
  background: linear-gradient(172deg, color-mix(in oklab, var(--face) 88%, white) 0%, var(--face) 55%, color-mix(in oklab, var(--face) 93%, black) 100%);
  color: var(--fi);
  font-size: 10cqw;
  display: grid;
  place-items: center;
  box-shadow:
    0 0.08em 0 color-mix(in oklab, var(--face) 72%, black),
    0 calc(2px + var(--lift) * 12px) calc(4px + var(--lift) * 18px) rgba(var(--shadow-rgb), calc(0.2 + var(--lift) * 0.2)),
    inset 0 1px 0 rgba(255, 255, 255, 0.55);
  transform: scale(calc(1 + var(--lift) * 0.05));
  transition: transform 0.18s var(--ease), box-shadow 0.18s, opacity 0.18s, background 0.3s, color 0.3s;
}
.t-face::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: radial-gradient(120% 80% at 50% -10%, rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0) 60%);
  pointer-events: none;
}
.t-face b {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 1em;
  line-height: 1;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.25);
}
.t-one .t-face b, .t-two .t-face b { text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2); }
.t-d3 .t-face { font-size: 8cqw; }
.t-d4 .t-face { font-size: 6.4cqw; }
.t-face small {
  position: absolute;
  right: 9%;
  bottom: 6%;
  font-size: 0.3em;
  font-family: var(--sans);
  font-weight: 500;
  opacity: 0;
  color: inherit;
  transition: opacity 0.3s;
}
.t-board.t-points .t-face small { opacity: 0.55; }
.t-face.t-pop { animation: pop 0.32s var(--ease); }
@keyframes pop { 45% { transform: scale(1.14); } }
.t-cards.t-refresh .t-card .t-face { animation: appear 0.3s var(--ease) backwards; }
@keyframes appear { from { transform: scale(0.85); opacity: 0; } }

/* -------------------------------------------------------- sheets, dialogs */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(var(--shadow-rgb), 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-rgb), 0.3);
  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; }

.rules-cards { display: flex; flex-wrap: wrap; gap: 10px 8px; margin: 14px 0 6px; }
.rules-cards div { display: flex; flex-direction: column; align-items: center; gap: 4px; width: 44px; font-size: 12px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.rules-cards .t-mini { width: 30px; height: 37px; font-size: 16px; }
.rules-cards .t-mini.t-d3 { font-size: 12px; }
.rules-pair { display: flex; align-items: center; gap: 6px; margin: 10px 0 2px; font-size: 15px; color: var(--ink-soft); }
.rules-pair .t-mini { width: 24px; height: 30px; font-size: 15px; }
.rules-pair + p { margin-top: 16px; }

@media (min-width: 700px) and (min-height: 560px) {
  .sheet {
    bottom: auto;
    top: 50%;
    transform: translateY(-50%);
    border-radius: 24px;
    max-height: 80dvh;
    animation: popin 0.28s var(--ease);
  }
}
@keyframes popin { 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-rgb), 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(--two) 35%, transparent), transparent 65%);
  pointer-events: none;
  opacity: 0.8;
}
.result h2 { position: relative; font-size: 30px; }
.result[data-best="true"] h2 { animation: shimmer 2.4s ease-in-out infinite; }
@keyframes shimmer { 50% { text-shadow: 0 0 18px color-mix(in oklab, var(--one) 80%, white); } }
.result-stats {
  display: grid;
  grid-template-columns: repeat(4, 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; }
.total { display: flex; flex-direction: column; align-items: center; margin: 4px 0 18px; }
.total span { font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft); }
.total 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.score, th.high, th.moves, th.time, th.undos, td.score, td.high, td.moves, td.time, td.undos { text-align: right; }
td.score { font-family: var(--serif); font-weight: 600; font-size: 15px; }
@media (max-width: 420px) { th.undos, td.undos { 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 { padding-left: 14px; min-width: 96px; justify-content: center; }
.swatch { display: inline-block; width: 24px; height: 24px; border-radius: 50%; flex: 0 0 auto; background: linear-gradient(135deg, var(--sw) 45%, var(--sw2) 55%); box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15); }
.toggle { display: flex; align-items: center; gap: 12px; margin-top: 18px; cursor: pointer; font-size: 15px; line-height: 1.35; }
.toggle input { position: absolute; opacity: 0; width: 0; height: 0; }
.toggle .track { flex: 0 0 auto; width: 46px; height: 28px; border-radius: 999px; background: color-mix(in oklab, var(--ink-soft) 45%, var(--paper-2)); position: relative; transition: background 0.25s; }
.toggle .track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3); transition: transform 0.25s var(--ease); }
.toggle input:checked + .track { background: var(--accent); }
.toggle input:checked + .track::after { transform: translateX(18px); }
.toggle input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------------------------------------------------------- toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(10px);
  z-index: 15;
  background: rgba(30, 26, 22, 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: 19px; }
  .stat { min-height: 48px; padding: 4px 6px; }
  .stat b { font-size: 17px; }
  .t-mini { width: 23px; height: 28px; font-size: 14px; }
  .actions { padding-bottom: 10px; }
  .btn { padding: 9px 10px; }
}
@media (max-width: 520px) {
  .btn { flex-direction: column; gap: 3px; padding: 8px 6px 7px; font-size: 12.5px; }
  .btn span { white-space: nowrap; }
  .btn svg { width: 21px; height: 21px; }
}
@media (min-width: 900px) and (min-height: 700px) {
  .brand b { font-size: 26px; }
  .stage { padding: 14px 16px; }
}

@media (prefers-reduced-motion: reduce) {
  .bg .blob { animation: none; }
  .t-card, .t-face, .t-cards, .t-board, .sheet, .dialog, .backdrop, .t-mini { animation: none; transition-duration: 0.01s; }
}
