/* Layout: the board is the table, a slate tablet that fills the screen's left;
   a narrow ledger beside it keeps the turn, the two dice and the six houses.
   The tablet keeps its own colors in both themes, as a real board would. */
:root {
  --bg: #e9ecef;
  --surface: #f6f7f8;
  --ink: #171b21;
  --muted: #59626e;
  --line: #d0d5db;
  --accent: #1f7fa3;
  --accent-ink: #ffffff;
  --warn: #b4442e;
  --shadow: 0 1px 2px rgb(20 26 34 / 0.08), 0 8px 24px rgb(20 26 34 / 0.08);

  /* The board, the same in both themes. */
  --slab: #252a31;
  --slab-edge: #3a414b;
  --space: #181b20;
  --space-rim: #3b424c;
  --ghost: #e3e8ef;
  --glow: #8fe3ff;
  --danger: #ff8a6b;

  --house-0: #e5484d;
  --house-1: #f08a2e;
  --house-2: #f2c94c;
  --house-3: #46b866;
  --house-4: #4a8fe0;
  --house-5: #9d6fdb;

  --font-display: "Marcellus", "Trajan Pro", "Optima", Georgia, serif;
  --font-body: "Alegreya Sans", "Gill Sans", "Segoe UI", system-ui, sans-serif;
  --step--1: 0.875rem;
  --step-0: 1rem;
  --step-1: 1.2rem;
  --step-2: 1.5rem;
  --step-3: 2.1rem;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1215;
    --surface: #171b20;
    --ink: #e6e9ed;
    --muted: #98a1ac;
    --line: #2a3038;
    --accent: #5cc3e6;
    --accent-ink: #0b1418;
    --warn: #ff8a6b;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 10px 30px rgb(0 0 0 / 0.35);
    --slab-edge: #4a525e;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1215;
  --surface: #171b20;
  --ink: #e6e9ed;
  --muted: #98a1ac;
  --line: #2a3038;
  --accent: #5cc3e6;
  --accent-ink: #0b1418;
  --warn: #ff8a6b;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 10px 30px rgb(0 0 0 / 0.35);
  --slab-edge: #4a525e;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 var(--step-0) / 1.45 var(--font-body);
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
h1, h2, h3 { font-family: var(--font-display); font-weight: 400; text-wrap: balance; margin: 0; line-height: 1.15; }
p { margin: 0; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.wrap {
  width: 100%;
  max-width: 1280px;
  margin: 0 auto;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}

/* ---------------------------------------------------------------- masthead */

.masthead { border-bottom: 1px solid var(--line); }
.masthead .wrap { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding-block: 10px; }
.wordmark { display: flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; }
.wordmark svg { height: 30px; width: auto; display: block; }
.wordmark path { fill: currentColor; }
.nav { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------------------------------------------------------------- controls */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px; padding: 8px 14px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--ink);
  font-weight: 500; cursor: pointer; text-decoration: none;
  transition: background-color 120ms, border-color 120ms, transform 80ms;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.quiet { border-color: transparent; background: transparent; }
.btn.quiet:hover:not(:disabled) { background: var(--surface); border-color: var(--line); }
.btn.primary { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.btn.primary:hover:not(:disabled) { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.wide { width: 100%; }
.btn .key { font-size: var(--step--1); opacity: 0.7; }

.seg { justify-self: start; display: inline-flex; border: 1px solid var(--line); border-radius: 8px; padding: 2px; background: var(--surface); gap: 2px; flex-wrap: wrap; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.seg span {
  display: block; padding: 6px 12px; border-radius: 6px; font-size: var(--step--1); font-weight: 500;
  color: var(--muted); cursor: pointer; white-space: nowrap;
}
.seg input:checked + span { background: var(--ink); color: var(--bg); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }

.eyebrow { font-size: 0.78rem; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); font-weight: 500; }
.muted { color: var(--muted); }

/* ---------------------------------------------------------------- setup */

.setup { display: grid; grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr); gap: 40px; padding-block: 32px 48px; align-items: start; }
.setup-board { position: sticky; top: 16px; }
.setup-board svg { width: 100%; height: auto; display: block; max-height: 78vh; }
.setup-side { display: grid; gap: 40px; min-width: 0; }
.setup-form, .lobby .setup-side { display: grid; gap: 28px; min-width: 0; }
.setup-form h1, .lobby h1 { font-size: var(--step-3); }
.room-code { letter-spacing: 0.14em; }
.lede { color: var(--muted); max-width: 52ch; margin-top: 8px; }
.resume { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }

fieldset { border: 0; margin: 0; padding: 0; display: grid; gap: 10px; min-width: 0; }
legend { padding: 0; margin-bottom: 10px; }
.castles { display: grid; gap: 6px; }
.castle-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 6px 0; border-bottom: 1px solid var(--line); }
.castle-row:last-child { border-bottom: 0; }
.castle-name { display: flex; align-items: center; gap: 10px; min-width: 9rem; }
.castle-name b { font-weight: 500; }
.castle-name small { color: var(--muted); font-size: var(--step--1); }
.chip { width: 28px; height: 28px; flex: none; border-radius: 50%; display: grid; place-items: center; box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.25); }
.chip svg { width: 70%; height: 70%; color: rgb(0 0 0 / 0.55); }
.option { display: grid; gap: 6px; }
.option p { font-size: var(--step--1); color: var(--muted); max-width: 60ch; }
.setup-error { color: var(--warn); font-size: var(--step--1); min-height: 1.4em; }
.online { display: grid; gap: 14px; padding-top: 28px; border-top: 1px solid var(--line); }
.online h2 { font-size: var(--step-2); }
.online p { max-width: 60ch; }
.field { display: grid; gap: 6px; margin: 0; }
.field label { font-size: var(--step--1); font-weight: 500; }
.share-row { display: flex; gap: 8px; flex-wrap: wrap; }
.share-row .text { flex: 1 1 12rem; min-width: 0; }
.text {
  min-height: 40px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--ink);
}
.text:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.text.code { flex: 0 1 9rem; text-transform: uppercase; letter-spacing: 0.18em; font-variant-numeric: tabular-nums; }
.occupant { flex: 1 1 8rem; min-width: 0; }
.occupant small { color: var(--muted); font-size: var(--step--1); }
.seat-controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.castle-row.off .castle-name { opacity: 0.6; }
.start-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* ---------------------------------------------------------------- game */

.game { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 28px; padding-block: 16px 32px; align-items: start; }
.table { position: relative; min-width: 0; }
.table svg.board { width: 100%; height: auto; max-height: calc(100vh - 110px); display: block; margin: 0 auto; touch-action: manipulation; }
.ledger { display: grid; gap: 20px; min-width: 0; }
.ledger section { display: grid; gap: 10px; }
.ledger section + section { border-top: 1px solid var(--line); padding-top: 18px; }

.turn-head { display: flex; align-items: center; gap: 12px; }
.turn-head .chip { width: 40px; height: 40px; }
.turn-head h2 { font-size: var(--step-2); }
.turn-head p { font-size: var(--step--1); color: var(--muted); }
.hint { font-size: var(--step--1); color: var(--muted); min-height: 2.9em; }

.dice { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.die { display: flex; align-items: center; gap: 10px; }
.die-face {
  width: 52px; height: 52px; flex: none; border-radius: 12px;
  display: grid; place-items: center;
  box-shadow: inset 0 -3px 0 rgb(0 0 0 / 0.18), inset 0 0 0 1px rgb(0 0 0 / 0.2), var(--shadow);
}
.die-face svg { width: 62%; height: 62%; color: rgb(0 0 0 / 0.55); }
.die-face.number { background: var(--surface); color: var(--ink); font: 400 1.7rem/1 var(--font-display); box-shadow: inset 0 -3px 0 var(--line), inset 0 0 0 1px var(--line), var(--shadow); }
.die-label { font-size: var(--step--1); line-height: 1.3; }
.die-label b { display: block; font-weight: 500; font-size: var(--step-0); }
.pips { display: flex; gap: 4px; margin-top: 4px; }
.pips i { width: 9px; height: 9px; border-radius: 50%; background: var(--ink); }
.pips i.used { background: transparent; box-shadow: inset 0 0 0 1.5px var(--line); }
.die-face.rolling { animation: tumble 600ms cubic-bezier(.3,.7,.3,1); }
@keyframes tumble {
  0% { transform: rotate(-200deg) scale(0.6); }
  70% { transform: rotate(12deg) scale(1.06); }
  100% { transform: none; }
}

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.actions .btn { font-size: var(--step--1); }
.actions .end { font-size: var(--step-0); }

.houses { display: grid; gap: 4px; }
.house { display: grid; grid-template-columns: 44px minmax(0, 1fr) auto; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 8px; }
.house.current { background: var(--surface); box-shadow: inset 0 0 0 1px var(--line); }
.house .who b { font-weight: 500; }
.house .who small { display: block; color: var(--muted); font-size: 0.8rem; }
.house .count { font-variant-numeric: tabular-nums; color: var(--muted); font-size: var(--step--1); }
.house.winner .count { color: var(--ink); font-weight: 700; }
.collection { width: 44px; height: 44px; display: block; }

.log { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; font-size: var(--step--1); color: var(--muted); max-height: 11.5em; overflow-y: auto; }
.log li { display: flex; gap: 8px; align-items: baseline; }
.log li .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; transform: translateY(-1px); }
.log li.big { color: var(--ink); font-weight: 500; }

/* ---------------------------------------------------------------- the board */

.board .slab { fill: var(--slab); stroke: var(--slab); stroke-width: 0.5; stroke-linejoin: round; }
.board .slab-edge { fill: none; stroke: var(--slab-edge); stroke-width: 0.04; stroke-linejoin: round; }
.board .space { fill: var(--space); stroke: var(--space-rim); stroke-width: 0.035; }
.board .court-ring { stroke-width: 0.05; }
.board .dim { opacity: 0.35; }
.board .stone, .board .hero { transition: transform 260ms cubic-bezier(.2,.7,.2,1); }
.board .hit { fill: transparent; cursor: default; }
.board .hit.act { cursor: pointer; }
.board .hit:focus { outline: none; }
.board .hit:focus-visible { stroke: var(--glow); stroke-width: 0.06; }
.board .sel { fill: none; stroke: var(--glow); stroke-width: 0.07; animation: breathe 1.6s ease-in-out infinite; }
.board .can { fill: none; stroke: var(--glow); stroke-width: 0.045; opacity: 0.55; }
.board .walk { fill: var(--glow); }
.board .walk-ring { fill: none; stroke: var(--glow); stroke-width: 0.03; opacity: 0.5; }
.board .disp { fill: none; stroke: var(--danger); stroke-width: 0.06; stroke-dasharray: 0.14 0.09; }
.board .arrow { fill: var(--glow); }
.board .arrow.push { fill: var(--danger); }
.board .push-to { fill: none; stroke: var(--glow); stroke-width: 0.035; stroke-dasharray: 0.08 0.08; opacity: 0.8; }
.board .recent { fill: none; stroke: #ffffff; stroke-width: 0.04; opacity: 0.32; }
@keyframes breathe { 50% { opacity: 0.45; } }

.popover {
  position: absolute; z-index: 5; display: flex; gap: 6px; padding: 6px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
  transform: translate(-50%, calc(-100% - 14px));
}
.popover .btn { min-height: 36px; padding: 6px 12px; font-size: var(--step--1); white-space: nowrap; }
.popover .btn.choice { flex-direction: column; gap: 4px; padding: 8px 10px 6px; }
.popover .btn.choice svg { display: block; }
.popover .btn.choice span { font-size: 0.8rem; color: var(--muted); }
.popover .btn.choice:hover span { color: inherit; }

/* ---------------------------------------------------------------- dialogs, toasts */

dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 0; max-width: min(640px, calc(100vw - 32px));
  background: var(--surface); color: var(--ink); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgb(8 10 12 / 0.55); }
.dialog-body { padding: 24px; display: grid; gap: 14px; max-height: min(80vh, 760px); overflow-y: auto; }
.dialog-body h2 { font-size: var(--step-2); }
.dialog-body h3 { font-size: var(--step-1); margin-top: 6px; }
.dialog-body p, .dialog-body li { max-width: 62ch; }
.dialog-body ul { margin: 0; padding-left: 1.2em; display: grid; gap: 4px; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 24px 20px; flex-wrap: wrap; }
.win-body { text-align: center; justify-items: center; }
.win-body .collection { width: 120px; height: 120px; }

.toast {
  position: fixed; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%);
  z-index: 20; max-width: calc(100vw - 32px);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow);
  font-weight: 500; font-size: var(--step--1);
  transition: opacity 200ms, transform 200ms;
}
.toast.gone { opacity: 0; transform: translate(-50%, 8px); pointer-events: none; }

/* ---------------------------------------------------------------- narrow */

@media (max-width: 900px) {
  .setup { grid-template-columns: minmax(0, 1fr); gap: 20px; padding-block: 16px 32px; }
  .setup-board { position: static; max-width: 520px; margin: 0 auto; width: 100%; }
  .game { grid-template-columns: minmax(0, 1fr); gap: 16px; padding-block: 8px 24px; }
  .table svg.board { max-height: none; }
}
@media (max-width: 480px) {
  .wordmark svg { height: 24px; }
  .castle-name { min-width: 0; }
  .table { margin-inline: -12px; }
}
@media (prefers-reduced-motion: reduce) {
  .board .stone, .board .hero, .btn, .toast { transition: none; }
  .board .sel, .die-face.rolling { animation: none; }
}
