/* Backgammon — walnut, ebony and felt. Every colour that changes with the
   look is a custom property under [data-theme]; the board, the checkers,
   the dice and the cube are painted on the canvas by render.js. Motion is
   transform and opacity only. */

: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);
  --spring: cubic-bezier(0.25, 0.8, 0.3, 1);
  --plate: 58px;
  --aspect-p: 0.8;
  --aspect-l: 1.23;
  --aspect-t: 0.65;
  --aspect: var(--aspect-p);
  --ca-accent: #9a3d26; /* the accounts SDK's sheets and chip: white on it reads 5.9:1 */
}

/* ------------------------------------------------------------- themes */
/* Walnut: an inlaid case on a sunny table */
:root,
[data-theme="walnut"] {
  color-scheme: light;
  --bg-a: #f2e6d2;
  --bg-b: #d8c2a0;
  --glow-1: rgba(255, 248, 232, 0.9);
  --glow-2: rgba(150, 60, 30, 0.14);
  --ink: #2c2017;
  --ink-soft: #6a5643;
  --chip-bg: rgba(255, 252, 245, 0.5);
  --chip-line: rgba(255, 255, 255, 0.72);
  --chip-shadow: 0 1px 2px rgba(60, 38, 18, 0.12);
  --active-bg: rgba(255, 252, 244, 0.94);
  --btn-bg: rgba(255, 255, 255, 0.56);
  --btn-hover: rgba(255, 255, 255, 0.86);
  --btn-line: rgba(70, 45, 25, 0.16);
  --primary-a: #b04a2e;
  --primary-b: #84301c;
  --primary-line: #62220f;
  --primary-ink: #fff6ec;
  --danger: #b23a26;
  --paper: #faf5ec;
  --paper-2: #efe5d4;
  --line: rgba(70, 45, 25, 0.14);
  --accent: #9a3d26;
  --accent-soft: rgba(154, 61, 38, 0.18);
  --brass: #b8893a;
  --backdrop: rgba(40, 26, 14, 0.42);
  --toast: rgba(38, 27, 18, 0.94);
  --d-frame: #6a4127;
  --d-field: #b88a55;
  --d-bone: #efe2c2;
  --d-red: #8a2419;
  --d-ivory: #f8efdc;
  --d-ebony: #2b1d16;
}

/* Felt: a travel case opened on a garden table */
[data-theme="felt"] {
  color-scheme: light;
  --bg-a: #eaeee6;
  --bg-b: #c8d2c4;
  --glow-1: rgba(255, 255, 250, 0.95);
  --glow-2: rgba(44, 94, 66, 0.16);
  --ink: #1f2a24;
  --ink-soft: #55625a;
  --chip-bg: rgba(255, 255, 255, 0.52);
  --chip-line: rgba(255, 255, 255, 0.8);
  --chip-shadow: 0 1px 2px rgba(30, 45, 40, 0.1);
  --active-bg: rgba(255, 255, 255, 0.94);
  --btn-bg: rgba(255, 255, 255, 0.6);
  --btn-hover: rgba(255, 255, 255, 0.9);
  --btn-line: rgba(30, 45, 40, 0.15);
  --primary-a: #3d7a58;
  --primary-b: #285a3f;
  --primary-line: #1d4430;
  --primary-ink: #f4f8f2;
  --danger: #b23a26;
  --paper: #f9faf6;
  --paper-2: #eaeee6;
  --line: rgba(30, 45, 40, 0.14);
  --accent: #2f6a4a;
  --accent-soft: rgba(47, 106, 74, 0.2);
  --brass: #a7782d;
  --backdrop: rgba(25, 32, 30, 0.4);
  --toast: rgba(30, 36, 34, 0.94);
  --d-frame: #b88a58;
  --d-field: #2f5e43;
  --d-bone: #efe5cc;
  --d-red: #6a2a22;
  --d-ivory: #f8efdb;
  --d-ebony: #2a1e18;
}

/* Ebony & brass: a games room after dark */
[data-theme="ebony"] {
  color-scheme: dark;
  --bg-a: #1c1714;
  --bg-b: #0b0908;
  --glow-1: rgba(226, 181, 92, 0.12);
  --glow-2: rgba(60, 120, 92, 0.12);
  --ink: #f2ebe0;
  --ink-soft: rgba(238, 226, 208, 0.68);
  --chip-bg: rgba(255, 255, 255, 0.05);
  --chip-line: rgba(255, 255, 255, 0.09);
  --chip-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  --active-bg: rgba(255, 230, 180, 0.1);
  --btn-bg: rgba(255, 255, 255, 0.07);
  --btn-hover: rgba(255, 255, 255, 0.13);
  --btn-line: rgba(255, 226, 170, 0.18);
  --primary-a: #e2b55c;
  --primary-b: #b98a35;
  --primary-line: #8d6420;
  --primary-ink: #1d1406;
  --danger: #e0634b;
  --paper: #1d1916;
  --paper-2: #2a2420;
  --line: rgba(240, 225, 200, 0.13);
  --accent: #e2b55c;
  --accent-soft: rgba(226, 181, 92, 0.22);
  --brass: #d1a24c;
  --backdrop: rgba(2, 2, 2, 0.62);
  --toast: rgba(44, 38, 33, 0.97);
  --d-frame: #2a201b;
  --d-field: #1f3a30;
  --d-bone: #d9c79c;
  --d-red: #7a1f25;
  --d-ivory: #f1e6cb;
  --d-ebony: #120b08;
}

/* before the script runs, a dark device starts on Ebony (no flash) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    --bg-a: #1c1714;
    --bg-b: #0b0908;
    --ink: #f2ebe0;
    --ink-soft: rgba(238, 226, 208, 0.68);
    --glow-1: rgba(226, 181, 92, 0.12);
    --glow-2: rgba(60, 120, 92, 0.12);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; overflow: hidden; overscroll-behavior: none; }
.no-anim *, .no-anim *::before, .no-anim *::after { transition: none !important; animation: none !important; }

body {
  font-family: var(--sans);
  color: var(--ink);
  background: var(--bg-b) linear-gradient(165deg, var(--bg-a), var(--bg-b)) fixed;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* ---------------------------------------------------------- background */
.bg { position: fixed; inset: 0; overflow: hidden; z-index: 0; pointer-events: none; }
.bg .glow { position: absolute; border-radius: 50%; filter: blur(70px); will-change: transform; }
.bg .g1 { width: 70vmax; height: 70vmax; left: -25vmax; top: -30vmax; background: var(--glow-1); animation: drift 58s ease-in-out infinite alternate; }
.bg .g2 { width: 60vmax; height: 60vmax; right: -25vmax; bottom: -25vmax; background: var(--glow-2); animation: drift 72s ease-in-out infinite alternate-reverse; }
@keyframes drift { to { transform: translate(7vmax, 5vmax) scale(1.1); } }

/* -------------------------------------------------------------- layout */
.app {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  max-width: 880px;
  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; gap: 8px; padding: 10px 10px 4px 16px; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { width: 40px; height: 40px; flex: 0 0 auto; filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.25)); }
.brand span { display: flex; flex-direction: column; line-height: 1.1; min-width: 0; }
.brand b { font-family: var(--serif); font-weight: 600; font-size: 24px; letter-spacing: 0.02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand small { font-size: 11px; color: var(--ink-soft); letter-spacing: 0.07em; text-transform: uppercase; margin-top: 2px; white-space: nowrap; overflow: hidden; }
.menu { display: flex; align-items: center; gap: 2px; flex: 0 0 auto; }
.account-chip { display: flex; align-items: center; min-width: 0; margin-left: 2px; }
.account-chip:empty { display: none; }

.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.2s, transform 0.2s, opacity 0.2s;
  flex: 0 0 auto;
  touch-action: manipulation;
}
.icon-btn:hover { background: var(--btn-bg); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn:disabled { opacity: 0.3; cursor: default; transform: none; background: transparent; }
.icon-btn svg, .btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* --------------------------------------------------------------- stage */
.stage { flex: 1; min-height: 0; container-type: size; display: grid; place-items: center; padding: 2px 10px 4px; position: relative; }
/* plates and board travel together as one table, centred in the stage */
.table {
  width: min(100cqw, calc((100cqh - 2 * var(--plate) - 16px) * var(--aspect)));
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* -------------------------------------------------------------- plates */
.plate {
  position: relative;
  height: var(--plate);
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
  padding: 5px 5px 5px 6px;
  border-radius: 999px;
  background: var(--chip-bg);
  border: 1px solid var(--chip-line);
  box-shadow: var(--chip-shadow);
  transition: background 0.35s, box-shadow 0.35s, rotate 0.5s var(--ease);
}
.plate::after {
  content: "";
  position: absolute;
  left: 18%;
  right: 18%;
  bottom: -1px;
  height: 2.5px;
  border-radius: 2px;
  background: var(--accent);
  transform: scaleX(0);
  opacity: 0;
  transition: transform 0.4s var(--ease), opacity 0.3s;
}
.plate.active { background: var(--active-bg); box-shadow: 0 0 0 1px var(--accent-soft), 0 6px 22px -10px var(--accent); }
.plate.active::after { transform: scaleX(1); opacity: 1; }
.plate.won { box-shadow: 0 0 0 1.5px var(--accent), 0 6px 20px -8px var(--accent); }
.plate.swap-in { animation: plate-in 0.42s var(--ease); }
@keyframes plate-in { from { opacity: 0; transform: translateY(6px); } }
/* face to face: the far player reads their own plate */
.app.face .plate.seat-top { rotate: 180deg; }

.token { position: relative; flex: 0 0 auto; width: 46px; height: 46px; display: grid; place-items: center; }
.man-token { display: block; width: var(--size, 34px); height: var(--size, 34px); }
.token .man-token { position: absolute; inset: 3px; }
.token .count {
  position: relative;
  margin-top: -2px;
  font-family: var(--serif);
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
.token[data-side="0"] .count { color: #3a2a18; text-shadow: 0 1px 0 rgba(255, 255, 255, 0.55); }
.token[data-side="1"] .count { color: #fbf3e4; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.65); }
.token.bump { animation: bump 0.3s var(--ease); }
@keyframes bump { 50% { transform: scale(1.14); } }

.who { display: flex; flex-direction: column; min-width: 0; flex: 1; line-height: 1.15; }
.name-row { display: flex; align-items: baseline; gap: 7px; min-width: 0; }
.name-row .name { font-size: 15.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.score {
  flex: 0 0 auto;
  font: 700 13px var(--serif);
  font-style: normal;
  font-variant-numeric: tabular-nums;
  min-width: 22px;
  padding: 1px 7px;
  border-radius: 999px;
  text-align: center;
  color: var(--ink);
  background: var(--paper-2);
  box-shadow: inset 0 0 0 1px var(--line);
}
.who small { font-size: 10.5px; letter-spacing: 0.07em; text-transform: uppercase; color: var(--ink-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 3px; min-height: 1.15em; }
.plate.active .who small { color: var(--accent); font-weight: 600; }
.plate.thinking .who small { animation: breathe 1.4s ease-in-out infinite; }
@keyframes breathe { 50% { opacity: 0.45; } }

.acts { display: flex; align-items: center; gap: 4px; flex: 0 0 auto; }
.plate .icon-btn { width: 42px; height: 42px; color: var(--ink-soft); }
.plate .icon-btn svg { width: 20px; height: 20px; }
.plate .icon-btn:hover { color: var(--ink); }
.plate .btn { min-height: 44px; padding: 8px 15px 8px 12px; border-radius: 999px; flex: 0 0 auto; font-size: 15px; }
.plate .btn svg { width: 20px; height: 20px; }
.plate .btn.done:not(.ready) { background: var(--btn-bg); color: var(--ink-soft); border-color: var(--btn-line); box-shadow: none; text-shadow: none; }
.plate .btn.done.ready { animation: ready 0.5s var(--spring); }
@keyframes ready { 40% { transform: scale(1.06); } }
.plate .btn.roll.shaking svg { animation: shake 0.32s ease-in-out; }
@keyframes shake { 20% { transform: rotate(-16deg) translateY(-1px); } 45% { transform: rotate(14deg); } 70% { transform: rotate(-9deg); } }
.cube-btn { position: relative; }
.cube-btn .cube-n {
  position: absolute;
  right: 2px;
  bottom: 3px;
  min-width: 17px;
  height: 17px;
  padding: 0 3px;
  border-radius: 6px;
  font: 700 10.5px/17px var(--serif);
  color: #3b2510;
  background: linear-gradient(135deg, #f6dc96, var(--brass));
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}
.plate .cube-btn { color: var(--ink); }

/* ---------------------------------------------------------------- board */
.board { position: relative; width: 100%; aspect-ratio: var(--aspect); }
.board canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  outline: none;
  cursor: pointer;
}
.board canvas:focus-visible { outline: none; }
.banner {
  position: absolute;
  left: 50%;
  top: 50%;
  translate: -50% -50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 12px 22px;
  border-radius: 18px;
  border: 1px solid var(--chip-line);
  background: var(--paper);
  color: var(--ink);
  font: 600 19px var(--serif);
  box-shadow: 0 16px 40px -14px rgba(0, 0, 0, 0.55);
  cursor: pointer;
  animation: popc 0.32s var(--ease);
  max-width: 86%;
  text-align: center;
}
.banner small { font: 500 11px var(--sans); letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-soft); }

/* -------------------------------------------------------------- actions */
.actions { display: flex; gap: 10px; padding: 6px 16px 12px; }
.btn {
  appearance: none;
  flex: 1;
  min-width: 0;
  border: 1px solid var(--btn-line);
  background: var(--btn-bg);
  color: var(--ink);
  font: 600 15px var(--sans);
  padding: 10px 14px;
  min-height: 48px;
  border-radius: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.15s, background 0.2s, box-shadow 0.2s, filter 0.2s, opacity 0.2s, color 0.2s;
}
.btn svg { width: 19px; height: 19px; flex: 0 0 auto; }
.btn span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.btn:hover { background: var(--btn-hover); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: default; transform: none; }
.btn.icon-only { flex: 0 0 auto; width: 52px; padding: 0; }
.btn.primary {
  background: linear-gradient(180deg, var(--primary-a), var(--primary-b));
  color: var(--primary-ink);
  border-color: var(--primary-line);
  box-shadow: 0 6px 16px -6px rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.24);
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.18);
}
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.start { width: 100%; margin-top: 22px; min-height: 52px; font: 600 18px var(--serif); border-radius: 16px; }
.link { appearance: none; border: 0; background: none; color: var(--ink-soft); font: 500 14px var(--sans); padding: 12px 8px 0; cursor: pointer; text-decoration: underline; text-underline-offset: 3px; min-height: 40px; }
.link:hover { color: var(--ink); }
.link.clear { display: block; margin: 14px auto 0; }

/* -------------------------------------------------------- sheets */
.backdrop { position: fixed; inset: 0; z-index: 20; background: var(--backdrop); -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px); animation: fade 0.25s; }
.backdrop.soft { background: color-mix(in srgb, var(--backdrop) 55%, transparent); -webkit-backdrop-filter: none; backdrop-filter: none; }
@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: 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);
}
@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 { font: 600 24px var(--serif); 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; touch-action: pan-y; }
.sheet-body h3 { font: 600 17px var(--serif); margin: 20px 0 8px; color: var(--ink); }
.sheet-body h3:first-child { margin-top: 6px; }
.sheet-body h4 { font: 600 13px var(--sans); letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-soft); margin: 16px 0 8px; }
.prose p { margin: 0 0 12px; }
.prose p.lead { font-size: 16px; color: var(--ink); margin-top: 12px; }
.prose ul { margin: 0; padding-left: 20px; }
.prose li { margin: 5px 0; }
.prose, .prose * { -webkit-user-select: text; user-select: text; }
.muted { color: var(--ink-soft); }
.note { font-size: 13px; color: var(--ink-soft); margin: 8px 0 0; line-height: 1.45; }
.about { font-size: 13px; margin-top: 10px; text-align: center; color: var(--ink-soft); }
.about b { font-weight: 600; color: var(--ink); }
@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); }
}
@keyframes pop { from { opacity: 0; transform: translateY(-50%) scale(0.96); } }

/* the starting position in the rules */
.figure { margin: 14px 0 6px; }
.figure figcaption { font-size: 13px; color: var(--ink-soft); line-height: 1.45; margin-top: 8px; }
.diagram { display: block; width: 100%; height: auto; filter: drop-shadow(0 6px 12px rgba(0, 0, 0, 0.18)); }
.diagram .d-frame { fill: var(--d-frame); }
.diagram .d-field { fill: var(--d-field); }
.diagram .d-bar { fill: var(--d-frame); stroke: rgba(0, 0, 0, 0.25); stroke-width: 1; }
.diagram .d-bone { fill: var(--d-bone); }
.diagram .d-red { fill: var(--d-red); }
.diagram .d-ivory { fill: var(--d-ivory); stroke: rgba(80, 60, 30, 0.55); stroke-width: 1; }
.diagram .d-ebony { fill: var(--d-ebony); stroke: rgba(255, 240, 220, 0.3); stroke-width: 1; }
.diagram .d-num { font: 600 7.5px var(--sans); fill: rgba(255, 244, 222, 0.8); text-anchor: middle; dominant-baseline: middle; }
.diagram .d-path { fill: none; stroke: #ffd27a; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; stroke-dasharray: 5 4; opacity: 0.95; }
.diagram .d-path.head { stroke-dasharray: none; }

/* -------------------------------------------------------- choices */
.choices { display: grid; gap: 8px; }
.choices.two { grid-template-columns: 1fr 1fr; }
.choice {
  appearance: none;
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  padding: 10px 12px;
  min-height: 60px;
  border-radius: 16px;
  border: 1.5px solid var(--line);
  background: var(--paper-2);
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s, box-shadow 0.2s, transform 0.15s;
}
.choice > svg { flex: 0 0 auto; width: 32px; height: 32px; fill: none; stroke: var(--accent); stroke-width: 1.6; stroke-linejoin: round; stroke-linecap: round; }
.choice span { display: flex; flex-direction: column; min-width: 0; }
.choice b { font: 600 15px var(--sans); }
.choice small { font-size: 12px; color: var(--ink-soft); line-height: 1.3; margin-top: 1px; }
.choice:active { transform: scale(0.98); }
.choice[aria-checked="true"] { border-color: var(--accent); background: var(--paper); box-shadow: 0 0 0 3px var(--accent-soft); }

.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;
  min-height: 44px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  cursor: pointer;
  touch-action: manipulation;
  transition: border-color 0.2s, background 0.2s, transform 0.15s, box-shadow 0.2s;
}
.opt:active { transform: scale(0.97); }
.opt[aria-checked="true"] { border-color: var(--accent); background: var(--paper); box-shadow: 0 0 0 3px var(--accent-soft); }
.opts.plain .opt { padding-left: 16px; padding-right: 16px; min-width: 84px; justify-content: center; }
.opts.lengths .opt { min-width: 0; padding-left: 13px; padding-right: 13px; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin: 4px 0 8px; }
.tabs .opt { padding: 6px 14px; min-height: 40px; font-size: 13.5px; }
.swatch {
  display: inline-block;
  width: 28px;
  height: 28px;
  border-radius: 7px;
  flex: 0 0 auto;
  position: relative;
  background: linear-gradient(90deg, var(--sb) 0 50%, var(--sc) 50% 100%);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.25), inset 0 0 0 4px var(--sa);
  overflow: hidden;
}
.swatch::after { content: ""; position: absolute; left: 50%; top: 50%; width: 11px; height: 11px; margin: -5.5px 0 0 -5.5px; border-radius: 50%; background: var(--sd); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.45); }
.swatch.auto { background: linear-gradient(135deg, #e9d8b8 50%, #1c1714 50%); box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.2); }
.swatch.auto::after { display: none; }

.toggle { display: flex; align-items: center; gap: 12px; margin-top: 14px; cursor: pointer; font-size: 15px; line-height: 1.35; min-height: 32px; }
.toggle input { position: absolute; opacity: 0; width: 0; height: 0; }
.toggle .track { flex: 0 0 auto; width: 46px; height: 28px; border-radius: 999px; background: var(--line); position: relative; transition: background 0.25s; box-shadow: inset 0 0 0 1px var(--line); }
.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; }

.players-edit { display: grid; gap: 8px; }
.player-row { display: flex; align-items: center; gap: 10px; }
.player-row .man-token { flex: 0 0 auto; }
.text-in {
  flex: 1;
  min-width: 0;
  appearance: none;
  font: 500 16px var(--sans); /* 16 px: iOS never zooms into the field */
  color: var(--ink);
  background: var(--paper-2);
  border: 1.5px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  min-height: 46px;
  -webkit-user-select: text;
  user-select: text;
}
.text-in:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* ------------------------------------------------------------- results */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(118px, 1fr)); gap: 8px; margin: 4px 0 6px; }
.tile { background: var(--paper-2); border-radius: 16px; padding: 10px 12px; display: flex; flex-direction: column-reverse; gap: 2px; }
.tile small { font-size: 10.5px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-soft); }
.tile b { font: 700 24px var(--serif); font-variant-numeric: tabular-nums; }
.board-mount { min-width: 0; min-height: 96px; background: var(--paper-2); border-radius: 16px; padding: 4px 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: 600; 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.num, td.num { text-align: right; }
td.name { font-weight: 600; max-width: 12em; overflow: hidden; text-overflow: ellipsis; }
td.name .man-token { display: inline-block; vertical-align: -4px; margin-right: 8px; }
.recent { list-style: none; margin: 0; padding: 0; }
.recent li { display: grid; grid-template-columns: auto 1fr auto; gap: 2px 10px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13.5px; }
.recent .when { color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.recent .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent .res { color: var(--ink-soft); white-space: nowrap; }
.recent .res.w { color: var(--accent); font-weight: 600; }
.recent .why { grid-column: 2 / 4; font-size: 12px; color: var(--ink-soft); }

/* ------------------------------------------------------------- dialogs */
/* centred; in face-to-face play each faces the player it speaks to */
.dialogs { position: fixed; inset: 0; z-index: 22; pointer-events: none; }
.dialog {
  position: absolute;
  left: 50%;
  top: 50%;
  translate: -50% -50%;
  width: min(92vw, 400px);
  max-height: 92dvh;
  overflow: auto;
  background: var(--paper);
  color: var(--ink);
  border-radius: 24px;
  padding: 24px 22px 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
  text-align: center;
  pointer-events: auto;
  animation: popc 0.32s var(--ease);
}
@keyframes popc { from { opacity: 0; scale: 0.95; } }
.dialog p { position: relative; text-wrap: pretty; margin: 4px 0 18px; color: var(--ink-soft); font-size: 15.5px; line-height: 1.5; }
.dialog p.lead { color: var(--ink); font: 600 19px/1.35 var(--serif); margin-bottom: 6px; }
.dialog .row { position: relative; display: flex; gap: 10px; }
.dialog .row .btn { min-width: 0; }
.dialog .stack { display: grid; gap: 8px; }
.dialogs.face .dialog.to-top { top: 25%; rotate: 180deg; max-height: 48dvh; }
.dialogs.face .dialog.to-bottom { top: 75%; max-height: 48dvh; }
.dialogs.face .dialog.result { width: min(92vw, 380px); padding: 12px 16px 8px; }
.dialogs.face .result .crest { height: 44px; margin: -2px auto 2px; }
.dialogs.face .result .crest .man-token { width: 44px; height: 44px; }
.dialogs.face .result h2 { font-size: 23px; }
.dialogs.face .result p { margin: 2px 0 8px; font-size: 14.5px; }
.dialogs.face .result-stats { margin-bottom: 10px; }
.dialogs.face .dialog .btn { min-height: 44px; padding: 9px 10px; }
.dialogs.face .result .link { padding-top: 6px; min-height: 34px; }

.result { overflow: hidden; }
.result .glow { position: absolute; inset: -45% -20% auto; height: 75%; background: radial-gradient(ellipse at 50% 42%, color-mix(in oklab, var(--accent) 42%, transparent), transparent 66%); pointer-events: none; }
.result .crest { position: relative; display: flex; justify-content: center; height: 64px; margin: -4px auto 6px; }
.result h2 { position: relative; margin: 0; font: 600 28px var(--serif); animation: shimmer 2.6s ease-in-out infinite; }
@keyframes shimmer { 50% { text-shadow: 0 0 18px color-mix(in oklab, var(--accent) 70%, transparent); } }
.result p { margin: 6px 0 14px; }
.result p.note-line { color: var(--accent); font-weight: 600; font-size: 13.5px; margin: -6px 0 14px; }
.result-stats { position: relative; display: flex; justify-content: center; flex-wrap: wrap; gap: 6px; margin: 0 0 14px; }
.result-stats > span { display: inline-flex; align-items: baseline; gap: 6px; background: var(--paper-2); border-radius: 999px; padding: 5px 12px; font-size: 10.5px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-soft); }
.result-stats b { font-family: var(--serif); font-size: 15px; font-weight: 600; color: var(--ink); letter-spacing: 0; text-transform: none; }

/* phones: the result card sits low, so the board stays in view */
@media (max-width: 600px) and (min-height: 600px) {
  .dialogs:not(.face) .dialog.result { top: auto; bottom: calc(12px + env(safe-area-inset-bottom)); translate: -50% 0; padding: 18px 18px 12px; }
  .result h2 { font-size: 25px; }
}

/* ---------------------------------------------------------------- toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(86px + env(safe-area-inset-bottom));
  translate: -50% 8px; /* the individual property: applied before a rotation */
  z-index: 30;
  background: var(--toast);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  width: max-content;
  max-width: min(88vw, 440px);
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, translate 0.25s;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}
.toast.show { opacity: 1; translate: -50% 0; }
.toast.to-top { bottom: auto; top: calc(84px + env(safe-area-inset-top)); rotate: 180deg; }

/* ------------------------------------------------------- small screens */
@media (max-height: 720px) {
  :root { --plate: 52px; }
  .top { padding-top: 6px; }
  .brand img { width: 34px; height: 34px; }
  .brand b { font-size: 21px; }
  .token { width: 40px; height: 40px; }
  .token .man-token { inset: 2px; }
  .token .count { font-size: 14px; }
  .actions { padding-bottom: 8px; padding-top: 4px; }
  .btn { min-height: 44px; padding: 8px 10px; }
  .plate .btn { min-height: 40px; }
  .plate .icon-btn { height: 40px; }
  .table { gap: 6px; }
}
@media (max-width: 420px) {
  .top { padding-left: 12px; padding-right: 8px; }
  .brand b { font-size: 21px; letter-spacing: 0; }
  .brand { gap: 8px; }
  .brand img { width: 36px; height: 36px; }
  .menu .icon-btn { width: 40px; }
  .actions { gap: 8px; padding-left: 12px; padding-right: 12px; }
  .actions .btn { padding: 10px 10px; gap: 6px; }
  #btn-new { flex: 1.3; }
}
@media (max-width: 520px) {
  .dialog .btn, .sheet .btn { font-size: 14.5px; padding: 11px 10px; }
  .tiles { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 374px) {
  .top { padding-left: 10px; padding-right: 6px; }
  .menu .icon-btn { width: 36px; }
  .brand { gap: 6px; }
  .brand b { font-size: 17px; }
  .brand img { width: 28px; height: 28px; }
  .plate { gap: 6px; }
  .name-row .name { font-size: 14px; }
  .plate .icon-btn { width: 36px; }
  .plate .btn { padding: 8px 11px 8px 9px; font-size: 14px; gap: 5px; }
  .actions { padding-left: 10px; padding-right: 10px; gap: 8px; }
  .stage { padding-left: 6px; padding-right: 6px; }
}

/* phones upright: the board stands up and stretches its points to fill the height
   between the plates (up to --aspect-t), so no empty bands are left */
@media (max-width: 599px) and (max-aspect-ratio: 5/4) {
  .board {
    aspect-ratio: auto;
    height: min(calc(100cqh - 2 * var(--plate) - 16px), calc(min(100cqw, (100cqh - 2 * var(--plate) - 16px) * var(--aspect-p)) / var(--aspect-t)));
  }
}

/* tablets and wide windows upright: the classic wide board, plates above and below */
@media (min-width: 600px) and (max-aspect-ratio: 5/4) {
  :root { --aspect: var(--aspect-l); }
}

/* landscape: the board fills the height on the left; header, plates and
   actions stack beside it (the far plate on top, the near one below) */
@media (min-aspect-ratio: 5/4) {
  :root { --aspect: var(--aspect-l); }
  .app {
    display: grid;
    max-width: none;
    grid-template-columns: auto minmax(310px, 360px);
    grid-template-rows: auto auto 1fr auto auto;
    justify-content: center;
    align-content: stretch;
    column-gap: 22px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(10px, env(safe-area-inset-right));
  }
  .stage, .table { display: contents; }
  .board {
    grid-column: 1;
    grid-row: 1 / -1;
    align-self: center;
    width: min(calc((100dvh - 20px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * var(--aspect-l)), calc(100vw - 390px - env(safe-area-inset-left) - env(safe-area-inset-right)), 1500px);
  }
  .top { grid-column: 2; grid-row: 1; padding-left: 0; }
  .plate.seat-top { grid-column: 2; grid-row: 2; margin: 8px 6px 0 0; }
  .plate.seat-bottom { grid-column: 2; grid-row: 4; margin: 0 6px 8px 0; }
  .actions { grid-column: 2; grid-row: 5; padding-left: 0; padding-right: 6px; gap: 8px; }
  .actions .btn { padding: 8px 8px; font-size: 14px; gap: 6px; }
  .actions .btn svg { width: 17px; height: 17px; }
  .actions .btn.icon-only { width: 46px; }
  .toast { left: auto; right: max(20px, env(safe-area-inset-right)); translate: 0 8px; bottom: calc(80px + env(safe-area-inset-bottom)); max-width: 300px; }
  .toast.show { translate: 0 0; }
  .toast.to-top { bottom: auto; top: calc(70px + env(safe-area-inset-top)); }
}
@media (min-aspect-ratio: 5/4) and (max-width: 1280px) {
  .app { grid-template-columns: auto minmax(318px, 340px); column-gap: 14px; }
  .brand { gap: 6px; }
  .brand b { font-size: 18.5px; letter-spacing: 0; }
  .brand img { width: 30px; height: 30px; }
  .menu .icon-btn { width: 36px; }
  .actions #btn-new svg { display: none; }
  .board { width: min(calc((100dvh - 16px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * var(--aspect-l)), calc(100vw - 364px - env(safe-area-inset-left) - env(safe-area-inset-right))); }
  .who small { letter-spacing: 0.03em; }
  .plate { gap: 6px; }
  .plate .icon-btn { width: 36px; }
  .plate .btn { padding: 8px 11px 8px 9px; font-size: 14px; gap: 5px; }
}
@media (min-aspect-ratio: 5/4) and (max-height: 440px) {
  .brand small { display: none; }
  .top { padding-top: 4px; padding-bottom: 0; }
  :root { --plate: 50px; }
  .actions { padding-bottom: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  .bg .glow { animation: none; }
  .plate { transition-duration: 0.01s; }
  .plate.thinking .who small { animation: none; }
  .sheet, .dialog, .backdrop, .result h2, .banner, .plate.swap-in, .token.bump, .plate .btn.done.ready, .plate .btn.roll.shaking svg { animation: none; }
}
