:root {
  --bg: #ece5d6; --panel-border: #cabd9f; --ink: #262019; --ink-muted: #6b6152;
  --square-light: #ddc9a3; --square-dark: #8a6a45; --frame: #4a3826;
  --jade: #3f6b56; --rose: #a1433a; --brass: #b8863b; --ivory: #faf5ea;
  --btn-bg: #4a3826; --btn-text: #f5eee0;
  --water: #6f95ad;
  --last-move: rgba(206, 210, 107, 0.55);
  --selected: rgba(20, 85, 30, 0.5);
  --hint: rgba(20, 30, 20, 0.3);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1712; --panel-border: #423a2c; --ink: #ede4d3; --ink-muted: #a99e8a;
    --square-light: #b99f74; --square-dark: #5c4429; --frame: #2c2115; --ivory: #f2ead9;
    --btn-bg: #b8863b; --btn-text: #1b1712; --water: #3f5f73;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1712; --panel-border: #423a2c; --ink: #ede4d3; --ink-muted: #a99e8a;
  --square-light: #b99f74; --square-dark: #5c4429; --frame: #2c2115; --ivory: #f2ead9;
  --btn-bg: #b8863b; --btn-text: #1b1712; --water: #3f5f73;
}
* { box-sizing: border-box; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
body { margin: 0; background: var(--bg); color: var(--ink); font-family: 'Inter', system-ui, sans-serif; line-height: 1.5; }
.page { max-width: 1100px; margin: 0 auto; padding: 2.5rem 1.25rem 3rem; }
h1, h2, h3, h4 { font-family: 'Fraunces', Georgia, serif; margin: 0; }
.hero h1 { font-size: clamp(1.8rem, 4vw, 2.5rem); font-weight: 600; letter-spacing: -0.01em; }
.hero .tagline { margin: 0.5rem 0 2rem; color: var(--ink-muted); max-width: 60ch; font-size: 1.02rem; }
.hidden { display: none !important; }

/* ---------- Buttons & inputs ---------- */
.btn {
  background: var(--btn-bg); color: var(--btn-text); border: none; padding: 0.55rem 1.1rem;
  border-radius: 4px; font-family: 'Inter', sans-serif; font-size: 0.92rem; font-weight: 600; cursor: pointer;
}
.btn:hover { filter: brightness(1.1); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; filter: none; }
.btn:focus-visible { outline: 3px solid var(--jade); outline-offset: 2px; }
.btn-secondary { background: transparent; color: var(--ink); border: 1px solid var(--panel-border); }
.btn-row { display: flex; flex-wrap: wrap; gap: 0.5rem; }
input[type="text"] {
  flex: 1; min-width: 0; padding: 0.55rem 0.75rem; border: 1px solid var(--panel-border); border-radius: 4px;
  background: var(--bg); color: var(--ink); font-family: 'Inter', sans-serif; font-size: 0.95rem;
}

/* ---------- Lobby ---------- */
.lobby { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1.25rem; max-width: 960px; margin: 1rem auto; }
.lobby-card { border: 1px solid var(--panel-border); border-radius: 6px; padding: 1.5rem; display: flex; flex-direction: column; }
.lobby-card h3 { margin-bottom: 0.5rem; }
.lobby-card p { color: var(--ink-muted); font-size: 0.9rem; margin: 0 0 1rem; }
.lobby-card .btn:last-child { margin-top: auto; align-self: flex-start; }
.row { display: flex; gap: 0.6rem; margin-top: auto; }
.field { margin-bottom: 0.85rem; border: 0; padding: 0; }
.field-label { display: block; font-size: 0.8rem; font-weight: 600; color: var(--ink-muted); margin-bottom: 0.35rem; }
.choice-group { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.choice-group.variant-groups { flex-direction: column; flex-wrap: nowrap; gap: 0.7rem; }
.choice-section-title { font-size: 0.72rem; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-muted); margin-bottom: 0.3rem; }
.choice-row { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.choice input { position: absolute; opacity: 0; pointer-events: none; }
.choice span {
  display: inline-block; padding: 0.35rem 0.75rem; border: 1px solid var(--panel-border); border-radius: 999px;
  font-size: 0.85rem; cursor: pointer; user-select: none;
}
.choice input:checked + span { background: var(--btn-bg); color: var(--btn-text); border-color: var(--btn-bg); }
.choice input:focus-visible + span { outline: 3px solid var(--jade); outline-offset: 2px; }

/* ---------- Game layout ---------- */
.game-top { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 1rem; }
.room-share { display: flex; gap: 0.5rem; flex: 1; min-width: 260px; max-width: 520px; }
.room-share input { font-size: 0.82rem; }
.mode-label { font-size: 0.9rem; color: var(--ink-muted); }
.layout { display: flex; gap: 2.5rem; align-items: flex-start; }
@media (max-width: 860px) { .layout { flex-direction: column; } }
.board-area {
  --n: 8;  /* files (squares per rank); --rows is set when the board has a different number of ranks */
  --sq: clamp(24px, calc((100vw - 2.5rem - 8px) / var(--n)), calc(480px / var(--n)));
  display: flex; flex-direction: column; gap: 0.6rem;
}
.annot-help { font-size: 0.78rem; color: var(--ink-muted); max-width: calc(var(--sq) * var(--n) + 8px); margin: 0; }
@media (hover: none) { .annot-help { display: none; } }

/* ---------- Board ---------- */
.board {
  position: relative; display: grid;
  grid-template-columns: repeat(var(--n), var(--sq)); grid-template-rows: repeat(var(--rows, var(--n)), var(--sq));
  border: 4px solid var(--frame); border-radius: 3px; box-shadow: 0 6px 18px rgba(0,0,0,0.18);
  user-select: none; -webkit-user-select: none; touch-action: manipulation;
}
.square {
  position: relative; width: var(--sq); height: var(--sq); border: 0; padding: 0; margin: 0;
  display: flex; align-items: center; justify-content: center; cursor: pointer; font: inherit;
}
.sq-light { background-color: var(--square-light); }
.sq-dark  { background-color: var(--square-dark); }
/* Federation 1: the river is water; the bridges are ordinary squares marked with a brass frame */
.sq-water {
  background-color: var(--water); cursor: default;
  background-image: repeating-linear-gradient(0deg, rgba(255,255,255,0.14) 0 2px, transparent 2px 9px);
}
.sq-water .coord { color: rgba(255,255,255,0.75); }
.sq-bridge { box-shadow: inset 0 0 0 3px rgba(184,134,59,0.7); }
.square:focus-visible { outline: 3px solid var(--jade); outline-offset: -3px; z-index: 2; }
.square.last-move { background-image: linear-gradient(var(--last-move), var(--last-move)); }
.square.selected  { background-image: linear-gradient(var(--selected), var(--selected)); }
.square.in-check  { background-image: radial-gradient(ellipse at center, #ff2a2a 0%, #e70000 25%, rgba(169,0,0,0) 89%); }
/* threat display: hover or select a piece (red = can capture, green = protects, dashed amber = jumped screen); check marks the checker */
.square.th-path   { background-image: linear-gradient(rgba(214,40,40,0.30), rgba(214,40,40,0.30)); }
.square.th-screen { outline: 3px dashed #e0a21a; outline-offset: -4px; z-index: 1; }
.square.th-target { outline: 3px solid rgba(214,40,40,0.95); outline-offset: -3px; z-index: 1; }
.square.th-defends { outline: 3px solid rgba(30,150,70,0.95); outline-offset: -3px; z-index: 1; }
.square.th-source { outline: 3px solid rgba(60,90,200,0.85); outline-offset: -3px; z-index: 1; }
.square.th-checker { outline: 4px solid #e70000; outline-offset: -4px; z-index: 1; }
/* right-click highlights (drawn under the piece) */
.square[data-mark="g"] { background-image: linear-gradient(rgba(21,120,27,0.62), rgba(21,120,27,0.62)); }
.square[data-mark="r"] { background-image: linear-gradient(rgba(170,32,32,0.62), rgba(170,32,32,0.62)); }
.square[data-mark="b"] { background-image: linear-gradient(rgba(0,58,160,0.58), rgba(0,58,160,0.58)); }
.square[data-mark="y"] { background-image: linear-gradient(rgba(230,143,0,0.62), rgba(230,143,0,0.62)); }
/* legal-move hints: dot on empty squares, ring on captures */
.square.hint::before {
  content: ''; position: absolute; width: 30%; height: 30%; border-radius: 50%;
  background: var(--hint); pointer-events: none;
}
.square.hint.capture::before {
  width: 100%; height: 100%; border-radius: 0;
  background: radial-gradient(transparent 0%, transparent 72%, var(--hint) 73%);
}
.square.enhanced-piece::after {
  content: ''; position: absolute; top: 3px; right: 3px; width: 6px; height: 6px;
  border-radius: 50%; background: var(--brass); z-index: 1;
}
.coord { position: absolute; font-size: calc(var(--sq) * 0.2); font-weight: 600; line-height: 1; pointer-events: none; z-index: 1; }
.coord-rank { top: 3px; left: 3px; }
.coord-file { bottom: 2px; right: 3px; }
.sq-light .coord { color: var(--square-dark); }
.sq-dark  .coord { color: var(--square-light); }
.annotations { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 3; }

.piece { position: relative; z-index: 1; line-height: 1; pointer-events: none; font-size: calc(var(--sq) * 0.7); }
.piece-white { color: var(--ivory); text-shadow: 0 1px 2px rgba(0,0,0,0.55); }
.piece-black { color: #1a140d; text-shadow: 0 1px 1px rgba(255,255,255,0.15); }
.piece-custom {
  display: inline-flex; align-items: center; justify-content: center; border-radius: 50%;
  width: calc(var(--sq) * 0.58); height: calc(var(--sq) * 0.58); font-size: calc(var(--sq) * 0.3);
  font-family: 'Inter', sans-serif; font-weight: 700; border: 2px solid var(--brass); text-shadow: none;
}
.piece-custom.piece-white { background: var(--ivory); color: #1a140d; }
.piece-custom.piece-black { background: #1a140d; color: var(--ivory); }

/* ---------- Side panel ---------- */
.panel { flex: 1; min-width: 260px; max-width: 360px; display: flex; flex-direction: column; gap: 1.5rem; }
@media (max-width: 860px) { .panel { max-width: none; width: 100%; } }
.panel section { border-top: 1px solid var(--panel-border); padding-top: 1rem; }
.panel section:first-child { border-top: none; padding-top: 0; }
.panel h3 { font-size: 1.05rem; font-weight: 600; margin-bottom: 0.75rem; }
#turnIndicator { font-size: 1.3rem; font-weight: 600; margin-bottom: 0.2rem; }
#seatIndicator { font-size: 0.88rem; color: var(--ink-muted); margin-bottom: 0.9rem; min-height: 1.3em; }
.thinking::after { content: ''; display: inline-block; width: 0.6em; height: 0.6em; margin-left: 0.5em; border-radius: 50%; background: var(--brass); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.25; } }

.legend-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.7rem; }
.legend-item { display: flex; gap: 0.65rem; align-items: flex-start; }
.legend-icon { flex-shrink: 0; width: 1.9rem; height: 1.9rem; border-radius: 50%; background: var(--frame); display: flex; align-items: center; justify-content: center; font-size: 1.15rem; }
.legend-icon.piece-custom { width: 1.5rem; height: 1.5rem; font-size: 0.75rem; margin: 0.2rem; }
.legend-item strong { font-size: 0.92rem; }
.legend-item p { margin: 0.15rem 0 0; font-size: 0.84rem; color: var(--ink-muted); }

.captured-row { display: flex; align-items: center; gap: 0.6rem; margin-bottom: 0.5rem; }
.captured-label { font-size: 0.82rem; color: var(--ink-muted); width: 5rem; flex-shrink: 0; }
.captured-icons { display: flex; flex-wrap: wrap; gap: 0.25rem; min-height: 1.6rem; }
.captured-icon { font-size: 1.15rem; }
.captured-icon.piece-custom { width: 1.3rem; height: 1.3rem; font-size: 0.65rem; }

.move-log { list-style: none; margin: 0; padding: 0; max-height: 220px; overflow-y: auto; font-size: 0.86rem; }
.move-log li { padding: 0.2rem 0; border-bottom: 1px dotted var(--panel-border); }

.notes { margin-top: 2.5rem; border-top: 1px solid var(--panel-border); padding-top: 1rem; }
.notes p { margin: 0; font-size: 0.86rem; color: var(--ink-muted); max-width: 70ch; }

/* ---------- Modals & toast ---------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(20,15,8,0.55); display: flex; align-items: center; justify-content: center; z-index: 10; padding: 1rem; }
.modal { position: relative; background: var(--bg); border: 1px solid var(--panel-border); border-radius: 6px; padding: 1.5rem; max-width: 340px; box-shadow: 0 10px 30px rgba(0,0,0,0.35); }
.modal h3 { margin-bottom: 1rem; }
.modal.modal-wide { max-width: 920px; width: 100%; max-height: 85vh; overflow-y: auto; }
.modal-close { position: absolute; top: 0.6rem; right: 0.7rem; background: none; border: none; font-size: 1.4rem; line-height: 1; cursor: pointer; color: var(--ink-muted); padding: 0.2rem; }
.modal-close:hover { color: var(--ink); }
.modal-intro { margin: 0 0 0.5rem; font-size: 0.84rem; color: var(--ink-muted); max-width: 70ch; }
.promotion-options { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.6rem; }
.promo-btn { display: flex; flex-direction: column; align-items: center; gap: 0.3rem; background: var(--frame); border: 1px solid var(--panel-border); border-radius: 4px; padding: 0.6rem 0.3rem; cursor: pointer; color: var(--ivory); --sq: 44px; }
.promo-btn:hover { filter: brightness(1.15); }
.promo-btn:focus-visible { outline: 3px solid var(--jade); outline-offset: 1px; }
.promo-label { font-size: 0.72rem; text-align: center; }
.toast { position: fixed; bottom: 1.5rem; left: 50%; transform: translateX(-50%); background: var(--rose); color: var(--ivory); padding: 0.6rem 1.1rem; border-radius: 4px; font-size: 0.88rem; z-index: 20; box-shadow: 0 6px 16px rgba(0,0,0,0.3); max-width: calc(100vw - 2rem); }

/* ---------- Movement cheatsheet ---------- */
.cheatsheet-legend-key { display: flex; flex-wrap: wrap; gap: 1rem; font-size: 0.8rem; color: var(--ink-muted); margin: 0 0 1rem; }
.key-swatch { display: inline-block; width: 0.7rem; height: 0.7rem; border-radius: 50%; margin-right: 0.35rem; vertical-align: middle; }
.key-dual, .mini-square.dest-dual::after { background: conic-gradient(var(--jade) 0turn 0.5turn, var(--rose) 0.5turn 1turn); }
.cheatsheet-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 1.5rem 1rem; }
.cheatsheet-item { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 0.5rem; }
.cheatsheet-item h4 { font-size: 0.92rem; font-weight: 600; }
.cheatsheet-item p { font-size: 0.78rem; color: var(--ink-muted); margin: 0; }
.mini-board { --msq: 25px; display: grid; grid-template-columns: repeat(8, var(--msq)); grid-template-rows: repeat(8, var(--msq)); border: 3px solid var(--frame); border-radius: 2px; overflow: hidden; }
.mini-square { width: var(--msq); height: var(--msq); display: flex; align-items: center; justify-content: center; }
.mini-square.origin { box-shadow: inset 0 0 0 2px var(--brass); }
.mini-square .piece { font-size: 1.05rem; }
.mini-square .piece-custom { width: 1rem; height: 1rem; font-size: 0.5rem; border-width: 1px; }
.mini-square.dest-move::after, .mini-square.dest-capture::after, .mini-square.dest-dual::after { content: ''; width: 9px; height: 9px; border-radius: 50%; }
.mini-square.dest-move::after { background: var(--jade); }
.mini-square.dest-capture::after { background: var(--rose); }

/* ---------- Armies setup (lobby) ---------- */
.setup-card { grid-column: 1 / -1; }
.setup-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 0.5rem; margin-bottom: 0.5rem; }
.setup-head h3 { margin: 0; }
.lobby-card .setup-head .btn { margin-top: 0; align-self: center; }
.setup-row { display: flex; flex-direction: column; }
.field-hint { font-weight: 400; }
.army-notes { margin: 0.25rem 0 0; display: grid; grid-template-columns: max-content 1fr; gap: 0.35rem 0.9rem; font-size: 0.84rem; }
.army-notes dt { font-weight: 600; }
.army-notes dd { margin: 0; color: var(--ink-muted); }
@media (max-width: 560px) { .army-notes { grid-template-columns: 1fr; } .army-notes dd { margin-bottom: 0.4rem; } }

/* ---------- Ambassador hands ---------- */
.hand {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem;
  width: calc(var(--sq) * var(--n) + 8px); min-height: calc(var(--sq) * 0.8); padding: 0.25rem 0.6rem;
  border: 1px dashed var(--panel-border); border-radius: 6px; font-size: 0.82rem; color: var(--ink-muted);
}
.hand-label { margin-right: 0.35rem; }
.hand-empty { font-style: italic; }
.hand-count { font-size: 0.78rem; font-weight: 600; color: var(--ink); margin-left: 0.15rem; }
.hand-next { margin-left: auto; font-size: 0.78rem; }
.hand.pickup-target .hand-next { display: none; }
.hand-piece {
  display: inline-flex; align-items: center; justify-content: center; background: none; cursor: pointer;
  border: 2px solid transparent; border-radius: 999px; padding: 2px 6px 2px 2px; font: inherit;
}
.hand-piece:disabled { cursor: default; }
.hand-piece:not(:disabled):hover { border-color: var(--panel-border); }
.hand-piece.selected { border-color: var(--jade); box-shadow: 0 0 0 3px rgba(63,107,86,0.25); }
.hand-piece:focus-visible { outline: 3px solid var(--jade); outline-offset: 1px; }
.hand-return { margin-left: auto; padding: 0.3rem 0.7rem; font-size: 0.82rem; }
.hand.pickup-target { border-style: solid; border-color: var(--jade); background: rgba(63,107,86,0.08); }

/* ---------- Legend groups ---------- */
.legend-group + .legend-group { margin-top: 1.1rem; }
.legend-group h4 { font-size: 0.9rem; font-weight: 600; margin-bottom: 0.6rem; color: var(--ink-muted); }

/* ---------- Cheatsheet: tabs + spreadsheet colours ---------- */
.cheat-tabs { display: flex; gap: 0.4rem; margin-bottom: 0.75rem; }
.cheat-tab { background: transparent; color: var(--ink); border: 1px solid var(--panel-border); border-radius: 999px; padding: 0.35rem 0.9rem; font: inherit; font-size: 0.85rem; cursor: pointer; }
.cheat-tab[aria-selected="true"] { background: var(--btn-bg); color: var(--btn-text); border-color: var(--btn-bg); }
.cat-first   { background: #ffc000 !important; }
.cat-move    { background: #ffff00 !important; }
.cat-capture { background: #c00000 !important; }
.cat-dual    { background: #92d050 !important; }
.cat-leap    { background: #00b050 !important; }
.cat-capleap { background: #00b0f0 !important; }
.cat-moveleap { background: #002060 !important; }
.cat-cannon  { background: linear-gradient(135deg, #ffff00 0 50%, #c00000 50% 100%) !important; }
.key-swatch { border-radius: 2px; box-shadow: inset 0 0 0 1px rgba(0,0,0,0.25); }
.mini-square[class*="cat-"] { box-shadow: inset 0 0 0 1px rgba(0,0,0,0.12); }

.legend-note { margin: 0.8rem 0 0; font-size: 0.84rem; color: var(--ink-muted); border-left: 3px solid var(--brass); padding-left: 0.6rem; }

/* Two-letter discs (Praetorian "PG") */
.piece-custom.glyph-2 { font-size: calc(var(--sq) * 0.23); letter-spacing: -0.03em; }
.legend-icon.glyph-2 { font-size: 0.58rem; }
.captured-icon.glyph-2 { font-size: 0.5rem; }
.mini-square .glyph-2 { font-size: 0.38rem; }
.promo-btn .glyph-2 { font-size: 0.7rem; }
.calasit-note { margin: -0.4rem 0 0.8rem !important; font-size: 0.82rem; color: var(--ink) !important; }
a.btn { text-decoration: none; display: inline-block; align-self: flex-start; }

/* the game rules, folded away under the lobby */
.rules-details { grid-column: 1 / -1; display: block; }
.rules-details > summary { cursor: pointer; list-style: none; display: flex; align-items: center; gap: 0.5rem; }
.rules-details > summary::-webkit-details-marker { display: none; }
.rules-details > summary h3 { margin: 0; }
.rules-details > summary::before { content: '▸'; color: var(--ink-muted); transition: transform 0.15s; }
.rules-details[open] > summary::before { transform: rotate(90deg); }
.rules-details > summary:focus-visible { outline: 3px solid var(--jade); outline-offset: 4px; border-radius: 4px; }
.rules-details .army-notes { margin-top: 0.9rem; }
