/* Mahjong — ivory tiles on a table. The table, the tile backs and the
   dialogs change with the theme; the tile faces stay hand-painted ivory. */

:root {
  --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;
  --ease: cubic-bezier(0.4, 0.1, 0.2, 1);
  --noise: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='220' height='220'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .5 0 0 0 0 .5 0 0 0 0 .5 0 0 0 .7 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* ------------------------------------------------------------- themes */
:root,
[data-theme="jade"] {
  color-scheme: light;
  --felt: #2c5e4a;
  --felt-hi: #3c7c62;
  --felt-lo: #14342a;
  --grain: 0.5;
  --pattern: none;
  --on-table: #f6eedb;
  --on-table-soft: rgba(246, 238, 219, 0.66);
  --chrome: rgba(8, 28, 21, 0.26);
  --chrome-hover: rgba(8, 28, 21, 0.4);
  --chrome-line: rgba(255, 255, 255, 0.1);
  --accent: #d9b25c;
  --accent-2: #b38634;
  --accent-ink: #2b2112;
  --ivory-1: #fffcf2;
  --ivory-2: #efe5cc;
  --side: #ddd0b0;
  --back: #17785a;
  --back-d: #0c4e3a;
  --dim: rgba(34, 40, 30, 0.22);
  --sel: #f2c75c;
  --sel-glow: rgba(255, 214, 110, 0.75);
  --sel-tint: rgba(255, 218, 120, 0.22);
  --hint: #7fe0c0;
  --hint-glow: rgba(127, 224, 192, 0.8);
  --paper: #f8f2e4;
  --paper-2: #ede5d3;
  --ink: #29251e;
  --ink-soft: #6f6655;
  --line: rgba(60, 45, 30, 0.13);
  --brand: #1d6b51;
  --backdrop: rgba(8, 22, 16, 0.5);
}

[data-theme="lacquer"] {
  color-scheme: light;
  --felt: #7a1d19;
  --felt-hi: #9c2b24;
  --felt-lo: #3e0b0a;
  --grain: 0.28;
  --pattern: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='140'%3E%3Cg fill='none' stroke='%23f0c66a' stroke-width='1.3' stroke-linecap='round'%3E%3Cpath d='M22 58c-9 0-11-12-2-14 1-11 16-13 21-4 5-8 20-6 20 5 9 0 12 13 2 13z'/%3E%3Cpath d='M33 50c0-4 5-5 7-2M52 49c1-3 5-3 6 0M28 58c3 3 8 3 10 0'/%3E%3Cpath d='M102 128c-9 0-11-12-2-14 1-11 16-13 21-4 5-8 20-6 20 5 9 0 12 13 2 13z'/%3E%3Cpath d='M113 120c0-4 5-5 7-2M132 119c1-3 5-3 6 0M108 128c3 3 8 3 10 0'/%3E%3C/g%3E%3C/svg%3E");
  --pattern-opacity: 0.12;
  --on-table: #fbefd8;
  --on-table-soft: rgba(251, 239, 216, 0.68);
  --chrome: rgba(40, 6, 4, 0.28);
  --chrome-hover: rgba(40, 6, 4, 0.44);
  --chrome-line: rgba(255, 220, 160, 0.14);
  --accent: #e7bd5e;
  --accent-2: #b98a2c;
  --accent-ink: #341a08;
  --ivory-1: #fffbf0;
  --ivory-2: #f0e3c6;
  --side: #e2d0a8;
  --back: #c18f2f;
  --back-d: #7d5717;
  --dim: rgba(60, 22, 12, 0.2);
  --sel: #ffd36b;
  --sel-glow: rgba(255, 210, 110, 0.8);
  --sel-tint: rgba(255, 214, 120, 0.22);
  --hint: #fff1b8;
  --hint-glow: rgba(255, 236, 170, 0.85);
  --paper: #fcf3e3;
  --paper-2: #f1e3cb;
  --ink: #2f1d16;
  --ink-soft: #7a6050;
  --line: rgba(90, 40, 20, 0.14);
  --brand: #9b2a22;
  --backdrop: rgba(30, 6, 4, 0.5);
}

[data-theme="ink"] {
  color-scheme: dark;
  --felt: #182033;
  --felt-hi: #243050;
  --felt-lo: #090c15;
  --grain: 0.35;
  --pattern: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1200 300' preserveAspectRatio='none'%3E%3Cpath d='M0 210 C120 150 190 120 270 160 S420 110 520 150 S700 90 820 140 S1000 120 1200 170 V300 H0Z' fill='%23a9b8de' fill-opacity='.07'/%3E%3Cpath d='M0 250 C150 205 260 190 360 225 S560 180 680 215 S880 200 980 230 S1120 210 1200 220 V300 H0Z' fill='%23a9b8de' fill-opacity='.08'/%3E%3C/svg%3E");
  --pattern-opacity: 1;
  --on-table: #e9e3d4;
  --on-table-soft: rgba(233, 227, 212, 0.62);
  --chrome: rgba(255, 255, 255, 0.06);
  --chrome-hover: rgba(255, 255, 255, 0.11);
  --chrome-line: rgba(255, 255, 255, 0.08);
  --accent: #e2c27c;
  --accent-2: #b8954b;
  --accent-ink: #1d1708;
  --ivory-1: #f3ecda;
  --ivory-2: #ddd2b6;
  --side: #c7bb9b;
  --back: #2d5291;
  --back-d: #172f5a;
  --dim: rgba(10, 12, 30, 0.3);
  --sel: #f1d27e;
  --sel-glow: rgba(241, 210, 126, 0.7);
  --sel-tint: rgba(255, 220, 140, 0.2);
  --hint: #9fd4ff;
  --hint-glow: rgba(140, 200, 255, 0.85);
  --paper: #1b2233;
  --paper-2: #252e44;
  --ink: #ece6d6;
  --ink-soft: #9ea7ba;
  --line: rgba(255, 255, 255, 0.1);
  --brand: #e2c27c;
  --backdrop: rgba(2, 4, 10, 0.6);
}
/* before the script runs, dark mode starts on the night table (no flash) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    --felt: #182033;
    --felt-hi: #243050;
    --felt-lo: #090c15;
    --grain: 0.35;
    --pattern: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1200 300' preserveAspectRatio='none'%3E%3Cpath d='M0 210 C120 150 190 120 270 160 S420 110 520 150 S700 90 820 140 S1000 120 1200 170 V300 H0Z' fill='%23a9b8de' fill-opacity='.07'/%3E%3Cpath d='M0 250 C150 205 260 190 360 225 S560 180 680 215 S880 200 980 230 S1120 210 1200 220 V300 H0Z' fill='%23a9b8de' fill-opacity='.08'/%3E%3C/svg%3E");
    --pattern-opacity: 1;
    --on-table: #e9e3d4;
    --on-table-soft: rgba(233, 227, 212, 0.62);
    --chrome: rgba(255, 255, 255, 0.06);
    --chrome-hover: rgba(255, 255, 255, 0.11);
    --chrome-line: rgba(255, 255, 255, 0.08);
    --accent: #e2c27c;
    --accent-2: #b8954b;
    --accent-ink: #1d1708;
    --ivory-1: #f3ecda;
    --ivory-2: #ddd2b6;
    --side: #c7bb9b;
    --back: #2d5291;
    --back-d: #172f5a;
    --dim: rgba(10, 12, 30, 0.3);
    --sel: #f1d27e;
    --sel-glow: rgba(241, 210, 126, 0.7);
    --sel-tint: rgba(255, 220, 140, 0.2);
    --hint: #9fd4ff;
    --hint-glow: rgba(140, 200, 255, 0.85);
    --paper: #1b2233;
    --paper-2: #252e44;
    --ink: #ece6d6;
    --ink-soft: #9ea7ba;
    --line: rgba(255, 255, 255, 0.1);
    --brand: #e2c27c;
    --backdrop: rgba(2, 4, 10, 0.6);
  }
}


* { 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(--on-table);
  background: var(--felt-lo);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

/* -------------------------------------------------------------- table */
.table {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  background:
    radial-gradient(125% 95% at 50% 40%, transparent 42%, rgba(0, 0, 0, 0.46) 100%),
    radial-gradient(90% 70% at 50% 34%, var(--felt-hi), var(--felt) 55%, var(--felt-lo));
  transition: background 0.6s;
}
.table::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--noise);
  opacity: var(--grain);
  mix-blend-mode: overlay;
}
.table::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--pattern);
  opacity: var(--pattern-opacity, 0);
  pointer-events: none;
}
[data-theme="ink"] .table::after { background: var(--pattern) bottom / 100% 34% no-repeat; }
[data-theme="ink"] .table .sheen {
  position: absolute;
  right: 8%;
  top: 6%;
  width: 26vmin;
  height: 26vmin;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(240, 228, 196, 0.16), rgba(240, 228, 196, 0.05) 45%, transparent 70%);
}
[data-theme="lacquer"] .table .sheen {
  position: absolute;
  inset: -20%;
  background: linear-gradient(115deg, transparent 30%, rgba(255, 220, 190, 0.07) 45%, transparent 60%);
}
.table .win-glow {
  position: absolute;
  left: 50%;
  top: 46%;
  width: 150vmax;
  height: 150vmax;
  margin: -75vmax 0 0 -75vmax;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 228, 165, 0.34), rgba(255, 228, 165, 0.1) 30%, transparent 58%);
  opacity: 0;
  transform: scale(0.15);
}
.table.won .win-glow { animation: winglow 2.6s cubic-bezier(0.2, 0.7, 0.3, 1) forwards; }
@keyframes winglow {
  0% { opacity: 0; transform: scale(0.15); }
  35% { opacity: 1; }
  100% { opacity: 0.55; transform: scale(1); }
}

/* -------------------------------------------------------------- layout */
.app {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "top" "stats" "stage" "actions";
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.top, .stats, .actions { width: 100%; max-width: 760px; justify-self: center; }

.top {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px 4px 16px;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { flex: 0 0 auto; width: 40px; height: 40px; filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.35)); }
.brand span { display: flex; flex-direction: column; line-height: 1.1; min-width: 0; overflow: hidden; }
.brand b { font-family: var(--serif); font-weight: 600; font-size: 23px; letter-spacing: 0.02em; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand small {
  font-size: 11px;
  color: var(--on-table-soft);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  margin-top: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.menu { display: flex; gap: 2px; flex: 0 0 auto; }

.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: inherit;
  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: var(--chrome-hover); }
.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; }
.icon-btn:focus-visible, .btn:focus-visible, .opt:focus-visible, .seg-btn:focus-visible, .layout-card:focus-visible, .chip:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.stats {
  grid-area: stats;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 4px 16px 4px;
}
.stat {
  background: var(--chrome);
  border: 1px solid var(--chrome-line);
  border-radius: 12px;
  padding: 5px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.15;
  min-width: 0;
}
.stat span { font-size: 10px; letter-spacing: 0.09em; text-transform: uppercase; color: var(--on-table-soft); white-space: nowrap; }
.stat b { font-family: var(--serif); font-size: 19px; font-weight: 600; font-variant-numeric: tabular-nums; }

.stage {
  grid-area: stage;
  position: relative;
  min-height: 0;
  min-width: 0;
  display: grid;
  place-items: center;
  padding: 8px 10px 6px;
  touch-action: none;
}

.actions {
  grid-area: actions;
  display: flex;
  gap: 8px;
  padding: 6px 16px 14px;
}
.btn {
  appearance: none;
  flex: 1;
  min-width: 0;
  border: 1px solid var(--chrome-line);
  background: var(--chrome);
  color: var(--on-table);
  font: 500 15px var(--sans);
  padding: 11px 10px;
  border-radius: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition: transform 0.15s, background 0.2s, opacity 0.2s, box-shadow 0.2s;
}
.btn svg { width: 19px; height: 19px; flex: 0 0 auto; }
.btn:hover { background: var(--chrome-hover); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.38; cursor: default; transform: none; }
.btn.primary {
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  color: var(--accent-ink);
  border-color: rgba(0, 0, 0, 0.18);
  box-shadow: 0 6px 14px -6px rgba(0, 0, 0, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.35);
  font-weight: 600;
}
.btn.primary:hover { filter: brightness(1.05); }
.btn.primary.thinking { animation: breathe 0.8s ease-in-out infinite; }
@keyframes breathe { 50% { opacity: 0.65; } }

/* -------------------------------------------------------------- tiles */
.board {
  position: relative;
  touch-action: none;
  --tw: 40px;
  --th: 53px;
}
.tile {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--tw);
  height: var(--th);
  font-size: var(--tw);
  cursor: default;
}
.tile.free { cursor: pointer; }
.tile.gone { display: none; }
.tile.flying { pointer-events: none; }
.tile .face {
  position: absolute;
  inset: 0 0.035em 0.035em 0;
  border-radius: 0.1em;
  background: var(--art) center / 100% 100% no-repeat, linear-gradient(150deg, var(--ivory-1), var(--ivory-2));
  box-shadow:
    inset -0.016em 0.016em 0 rgba(255, 255, 255, 0.9),
    inset 0.03em -0.03em 0.05em rgba(110, 80, 30, 0.2),
    -0.018em 0.018em 0 var(--side),
    -0.034em 0.034em 0 var(--side),
    -0.05em 0.05em 0 var(--back),
    -0.068em 0.068em 0 var(--back),
    -0.086em 0.086em 0 var(--back),
    -0.104em 0.104em 0 var(--back),
    -0.12em 0.12em 0 var(--back-d),
    -0.06em 0.08em 0.05em rgba(0, 0, 0, 0.22),
    -0.15em 0.18em 0.22em rgba(0, 0, 0, 0.3);
  transition: transform 0.18s var(--ease);
}
.tile .face::before,
.tile .face::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.28s;
}
.tile .face::before { background: var(--dim); }
.board.dim .tile:not(.free) .face::before { opacity: 1; }
.tile.sel .face { transform: translate(0.03em, -0.05em); }
.tile.sel .face::before { background: var(--sel-tint); opacity: 1; }
.tile .face::after {
  inset: -0.02em;
  border-radius: 0.12em;
  box-shadow: 0 0 0 0.045em var(--sel), 0 0 0.3em 0.05em var(--sel-glow);
}
.tile.sel .face::after { opacity: 1; }
.tile.hint .face { transform: translate(0.025em, -0.045em); }
.tile.hint .face::after {
  box-shadow: 0 0 0 0.055em var(--hint), 0 0 0.45em 0.1em var(--hint-glow);
  animation: hintpulse 1.2s ease-in-out infinite;
}
@keyframes hintpulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.55; } }
.tile.cursor .face::after {
  opacity: 1;
  box-shadow: 0 0 0 0.04em var(--accent), 0 0 0 0.075em rgba(0, 0, 0, 0.35);
}
.tile.sel.cursor .face::after { box-shadow: 0 0 0 0.045em var(--sel), 0 0 0.3em 0.05em var(--sel-glow), 0 0 0 0.09em var(--accent); }
@media (hover: hover) and (pointer: fine) {
  .tile.free:hover .face { transform: translate(0.012em, -0.02em); }
  .tile.sel:hover .face { transform: translate(0.03em, -0.05em); }
}

.burst {
  position: absolute;
  left: calc(var(--tw) * -0.9);
  top: calc(var(--tw) * -0.9);
  width: calc(var(--tw) * 1.8);
  height: calc(var(--tw) * 1.8);
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 246, 214, 0.9), rgba(255, 222, 150, 0.35) 38%, transparent 68%);
  pointer-events: none;
  z-index: 4000;
  opacity: 0;
}

/* -------------------------------------------------------- sheets, dialogs */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: var(--backdrop);
  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: 580px;
  max-height: 88dvh;
  display: flex;
  flex-direction: column;
  background: var(--paper);
  color: var(--ink);
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.3);
  padding-bottom: env(safe-area-inset-bottom);
  animation: rise 0.32s var(--ease);
}
.sheet::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 7px;
  width: 38px;
  height: 4px;
  margin-left: -19px;
  border-radius: 2px;
  background: var(--line);
}
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
.sheet header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 12px 4px 22px;
  flex: 0 0 auto;
}
.sheet .icon-btn:hover { background: var(--paper-2); }
.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;
  -webkit-user-select: text;
  user-select: text;
}
.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); }
.note { font-size: 13.5px; line-height: 1.45; margin: 10px 0 0; }
.about { font-size: 13px; margin-top: 24px; text-align: center; }

.strip { display: flex; gap: 5px; margin: 6px 0 8px; flex-wrap: wrap; }
.mini {
  width: 30px;
  height: 40px;
  border-radius: 4px;
  background: var(--art) center / 100% 100% no-repeat, linear-gradient(150deg, #fffcf2, #efe5cc);
  box-shadow: -1px 1px 0 #ddd0b0, -2px 2px 0 var(--back), -3px 3px 0 var(--back-d), -3px 4px 5px rgba(0, 0, 0, 0.25);
}

@media (min-width: 700px) and (min-height: 560px) {
  .sheet {
    bottom: auto;
    top: 50%;
    transform: translateY(-50%);
    border-radius: 24px;
    max-height: 84dvh;
    animation: pop 0.28s var(--ease);
  }
  .sheet::before { display: none; }
}
@keyframes pop { 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);
  color: var(--ink);
  border-radius: 24px;
  padding: 26px 22px 20px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.45);
  text-align: center;
  animation: popc 0.3s var(--ease);
}
@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; }
.dialog .btn { color: var(--ink); background: var(--paper-2); border-color: var(--line); font-size: 14.5px; }
.dialog .btn:hover { background: color-mix(in oklab, var(--paper-2) 80%, var(--ink)); }
.dialog .btn.primary { color: var(--accent-ink); background: linear-gradient(180deg, var(--accent), var(--accent-2)); border-color: rgba(0, 0, 0, 0.15); }

.win { padding-top: 22px; }
.seal {
  width: 64px;
  height: 64px;
  margin: 0 auto 10px;
  border-radius: 12px;
  background: var(--seal) center / 100% 100% no-repeat;
  filter: drop-shadow(0 3px 6px rgba(120, 20, 10, 0.35));
  animation: stamp 0.6s 0.15s cubic-bezier(0.2, 0.9, 0.3, 1.2) both;
}
@keyframes stamp {
  from { opacity: 0; transform: scale(1.6) rotate(-14deg); }
  to { opacity: 1; transform: scale(1) rotate(-4deg); }
}
.win h2 { font-size: 30px; }
.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 2px;
  line-height: 1.2;
  min-width: 0;
}
.result-stats span, .summary span { font-size: 9px; letter-spacing: 0.03em; text-transform: uppercase; color: var(--ink-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.result-stats b, .summary b { font-family: var(--serif); font-size: 19px; font-weight: 600; }
.final { display: flex; flex-direction: column; align-items: center; margin: 4px 0 18px; }
.final span { font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft); }
.final b { font-family: var(--serif); font-size: 46px; font-weight: 600; line-height: 1.05; }
.final small { font-size: 12.5px; color: var(--brand); font-weight: 600; margin-top: 2px; }

/* the empty table after a win */
.cleared {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  color: var(--on-table);
  animation: fade 0.8s 0.3s both;
}
.cleared h2 { font-family: var(--serif); font-weight: 600; font-size: 30px; margin: 4px 0 6px; text-shadow: 0 2px 8px rgba(0, 0, 0, 0.35); }
.cleared p { margin: 0 0 20px; color: var(--on-table-soft); font-size: 15px; max-width: 320px; line-height: 1.45; }
.cleared .btn { flex: 0 0 auto; flex-direction: row; padding: 11px 22px; font-size: 15px; }

/* ---------------------------------------------------------- new game */
.seg {
  display: flex;
  background: var(--paper-2);
  border-radius: 12px;
  padding: 3px;
  gap: 3px;
}
.seg-btn {
  appearance: none;
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--ink-soft);
  font: 500 14.5px var(--sans);
  padding: 9px 6px;
  border-radius: 9px;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, box-shadow 0.2s;
}
.seg-btn[aria-checked="true"] { background: var(--paper); color: var(--ink); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.16); font-weight: 600; }
.layouts { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.layout-card {
  appearance: none;
  border: 1.5px solid var(--line);
  background: var(--paper-2);
  color: var(--ink);
  border-radius: 16px;
  padding: 10px 10px 9px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  cursor: pointer;
  text-align: left;
  font: inherit;
  transition: transform 0.15s, border-color 0.2s, box-shadow 0.2s;
}
.layout-card:hover { border-color: color-mix(in oklab, var(--brand) 50%, transparent); }
.layout-card:active { transform: scale(0.98); }
.layout-card.current { border-color: var(--brand); box-shadow: 0 0 0 3px color-mix(in oklab, var(--brand) 18%, transparent); }
.layout-card .pic {
  align-self: stretch;
  height: 86px;
  margin-bottom: 7px;
  border-radius: 10px;
  background: radial-gradient(90% 90% at 50% 40%, var(--felt-hi), var(--felt) 70%, var(--felt-lo));
  position: relative;
}
.layout-card svg { position: absolute; inset: 9px; width: calc(100% - 18px); height: calc(100% - 18px); overflow: visible; }
.layout-card rect { stroke: rgba(0, 0, 0, 0.35); stroke-width: 0.1; }
.layout-card rect.l0 { fill: #cfc3a6; }
.layout-card rect.l1 { fill: #ded3b9; }
.layout-card rect.l2 { fill: #ebe2cc; }
.layout-card rect.l3 { fill: #f6efdd; }
.layout-card rect.l4 { fill: #fffaf0; }
.layout-card b { font-family: var(--serif); font-size: 17px; font-weight: 600; }
.layout-card small { font-size: 12.5px; color: var(--ink-soft); }
.layout-card small.best { color: var(--brand); font-weight: 600; }

/* ------------------------------------------------------------- results */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 4px; }
.chip {
  appearance: none;
  border: 1.5px solid var(--line);
  background: transparent;
  color: var(--ink);
  font: 500 13.5px var(--sans);
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--brand); border-color: var(--brand); color: var(--paper); }
.summary { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 12px; }
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: 10.5px; letter-spacing: 0.06em; text-transform: uppercase; padding: 4px; border-bottom: 1px solid var(--line); }
td { padding: 7px 4px; border-bottom: 1px solid var(--line); white-space: nowrap; vertical-align: middle; }
th.time, th.score, td.time, td.score { text-align: right; }
td.score { font-family: var(--serif); font-weight: 600; font-size: 15px; }
tr[data-won="true"] td.result { color: var(--brand); font-weight: 600; }
tr[data-won="false"] td.result { color: var(--ink-soft); }
.btn.subtle { flex: 0 0 auto; background: transparent; border: 0; color: var(--ink-soft); font-size: 13.5px; margin: 18px auto 0; display: flex; }
@media (max-width: 420px) {
  th.level, td.level { display: none; }
  td { padding: 7px 3px; }
  .summary { grid-template-columns: repeat(2, 1fr); }
}

/* ------------------------------------------------------------- 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(--brand); box-shadow: 0 0 0 3px color-mix(in oklab, var(--brand) 20%, transparent); }
.opts.lang .opt { padding-left: 14px; min-width: 92px; justify-content: center; }
.swatch {
  position: relative;
  display: inline-block;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  flex: 0 0 auto;
  background: radial-gradient(circle at 35% 30%, color-mix(in oklab, var(--a) 70%, white), var(--a) 70%);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.25);
}
.swatch::after {
  content: "";
  position: absolute;
  left: 8px;
  top: 5px;
  width: 10px;
  height: 13px;
  border-radius: 2.5px;
  background: var(--b);
  box-shadow: -1.5px 1.5px 0 var(--c);
}
.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)); 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(--brand); }
.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(86px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(10px);
  z-index: 15;
  background: rgba(20, 18, 14, 0.9);
  color: #f8f2e4;
  padding: 10px 16px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.35;
  width: max-content;
  max-width: min(88vw, 440px);
  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.3);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ------------------------------------------------------- small screens */
@media (max-width: 520px) {
  .btn { flex-direction: column; gap: 3px; padding: 7px 4px 6px; font-size: 12.5px; }
  .btn span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
  .btn svg { width: 21px; height: 21px; }
  .dialog .btn { flex-direction: row; font-size: 14px; padding: 11px 8px; }
  .stats { gap: 6px; padding: 4px 12px; }
  .actions { gap: 6px; padding: 6px 12px 12px; }
  .stage { padding: 6px 4px 4px; }
}
@media (max-width: 380px) {
  .top { padding: 8px 6px 2px 12px; }
  .brand { gap: 8px; }
  .brand img { width: 36px; height: 36px; }
  .brand b { font-size: 21px; }
  .icon-btn { width: 40px; height: 40px; }
  .stat b { font-size: 17px; }
}
@media (max-height: 640px) {
  .top { padding-top: 6px; }
  .brand b { font-size: 20px; }
  .stat { padding: 3px 6px; }
  .stat b { font-size: 16px; }
  .actions { padding-bottom: 8px; }
}
/* phones on their side: the controls move into a column on the left */
@media (orientation: landscape) and (max-height: 540px) {
  .app {
    grid-template-columns: minmax(208px, 236px) minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas: "top stage" "stats stage" "actions stage";
  }
  .top { padding: 8px 2px 4px 12px; gap: 4px; }
  .brand { gap: 7px; }
  .brand img { width: 28px; height: 28px; }
  .brand b { font-size: 17px; }
  .brand small { display: none; }
  .icon-btn { width: 34px; height: 34px; }
  .icon-btn svg { width: 20px; height: 20px; }
  .menu { gap: 0; }
  .stats { grid-template-columns: repeat(2, 1fr); padding: 4px 8px 4px 12px; gap: 5px; }
  .stat b { font-size: 16px; }
  .actions { display: grid; grid-template-columns: repeat(2, 1fr); align-self: end; padding: 4px 8px 10px 12px; gap: 6px; }
  .btn { flex-direction: row; font-size: 12.5px; padding: 8px 4px; gap: 5px; }
  .btn svg { width: 17px; height: 17px; }
  .stage { padding: 6px 8px 6px 4px; }
  .toast { bottom: calc(14px + env(safe-area-inset-bottom)); }
}
@media (min-width: 900px) and (min-height: 700px) {
  .brand b { font-size: 26px; }
  .brand img { width: 44px; height: 44px; }
  .stage { padding: 14px 24px 12px; }
}
/* wide screens: the stats join the header row, the board gets the height */
@media (min-width: 1000px) and (min-height: 600px) {
  .app { grid-template-rows: auto 0 minmax(0, 1fr) auto; }
  .top, .actions { max-width: 1040px; }
  .top { padding-top: 12px; }
  .stats {
    grid-area: auto;
    position: absolute;
    left: 50%;
    top: calc(12px + env(safe-area-inset-top));
    transform: translateX(-50%);
    width: min(440px, 38vw);
    max-width: none;
    padding: 0;
  }
  .stat { padding: 4px 6px; }
  .actions { max-width: 640px; }
}

@media (prefers-reduced-motion: reduce) {
  .tile .face, .tile .face::before, .tile .face::after, .sheet, .dialog, .backdrop, .seal, .cleared { animation: none; transition-duration: 0.01s; }
  .tile.hint .face::after { animation: none; }
  .table.won .win-glow { animation: none; opacity: 0.5; transform: none; }
}
