/* Chess: a quiet study. Light: ivory paper, a walnut board, a deep green accent. Dark: charcoal, a slate board.
   house/theme.js sets data-theme="dark" on <html>. Board colours can be picked in Board options (html.bd-walnut,
   .bd-slate, .bd-sage; "Auto" follows the theme). Pieces are our own flat set (game.js SHAPES). */
@font-face { font-family: "Alegreya Sans"; font-style: normal; font-weight: 400; font-display: swap; src: url(/fonts/alegreya-sans-latin-400-normal.woff2) format("woff2"); }
@font-face { font-family: "Alegreya Sans"; font-style: normal; font-weight: 500; font-display: swap; src: url(/fonts/alegreya-sans-latin-500-normal.woff2) format("woff2"); }
@font-face { font-family: "Alegreya Sans"; font-style: normal; font-weight: 700; font-display: swap; src: url(/fonts/alegreya-sans-latin-700-normal.woff2) format("woff2"); }
@font-face { font-family: "Alegreya"; font-style: normal; font-weight: 700; font-display: swap; src: url(/fonts/alegreya-latin-700-normal.woff2) format("woff2"); }

:root {
  --bg: #F3EEE4; --bg2: #EAE3D5; --card: #FFFDF8; --ink: #24211C; --muted: #6B6457; --line: #DCD2C0;
  --accent: #2F6B4F; --on-accent: #FFFFFF; --bad: #B23A3A; --good: #2F6B4F; --backdrop: rgba(30, 26, 20, .45);
  --sq-l: #EAD6B4; --sq-d: #B08660; --coord-l: #8E6A48; --coord-d: #F3E6CD;
  --hl-last: rgba(236, 196, 64, .42); --hl-sel: rgba(47, 107, 79, .45); --hl-pre: rgba(70, 110, 190, .42);
  --dot: rgba(36, 30, 20, .26); --ring: rgba(36, 30, 20, .3); --chk: rgba(214, 48, 40, .85);
  --pw-fill: #FBF8F1; --pw-line: #24201B; --pb-fill: #2B2723; --pb-line: #100E0C; --pb-detail: #E9E2D4;
  --font: "Alegreya Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; --head: "Alegreya", Georgia, serif;
  --lb-panel: var(--card); --lb-panel2: var(--bg2); --lb-text: var(--ink); --lb-muted: var(--muted); --lb-line: var(--line);
  --lb-accent: var(--accent); --lb-ink: var(--on-accent); --lb-bad: var(--bad); --lb-font: var(--font); --lb-head: var(--head);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #14161B; --bg2: #1C1F26; --card: #1F232B; --ink: #ECE8E1; --muted: #A39D92; --line: #30353F;
  --accent: #7CC59E; --on-accent: #0D1A13; --bad: #FF8A80; --good: #7CC59E; --backdrop: rgba(0, 0, 0, .6);
  --sq-l: #A3AEBD; --sq-d: #5A687E; --coord-l: #4C596D; --coord-d: #D6DDE7;
  --hl-last: rgba(240, 200, 80, .4); --hl-sel: rgba(124, 197, 158, .5); --dot: rgba(10, 14, 20, .34); --ring: rgba(10, 14, 20, .38);
  color-scheme: dark;
}
:root.bd-walnut { --sq-l: #EAD6B4; --sq-d: #B08660; --coord-l: #8E6A48; --coord-d: #F3E6CD; --dot: rgba(36, 30, 20, .26); --ring: rgba(36, 30, 20, .3); }
:root.bd-slate { --sq-l: #A3AEBD; --sq-d: #5A687E; --coord-l: #4C596D; --coord-d: #D6DDE7; --dot: rgba(10, 14, 20, .34); --ring: rgba(10, 14, 20, .38); }
:root.bd-sage { --sq-l: #E4E8D3; --sq-d: #84A06C; --coord-l: #63804D; --coord-d: #F1F4E6; --dot: rgba(24, 36, 18, .28); --ring: rgba(24, 36, 18, .32); }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { touch-action: manipulation; }
body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--font); font-size: 17px; line-height: 1.45; min-height: 100vh; min-height: 100dvh;
  -webkit-text-size-adjust: 100%; padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
a { color: inherit; }
button { font: inherit; color: inherit; -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ------------------------------------------------------------ page frame */
.wrap { max-width: 1060px; margin: 0 auto; padding: 0 14px; }
.top { display: flex; justify-content: space-between; align-items: center; padding: 10px 0 0; }
.back { text-decoration: none; color: var(--muted); font-size: .95rem; white-space: nowrap; }
.top-actions { display: flex; gap: 4px; }
.icon-btn { width: 40px; height: 40px; border-radius: 12px; border: 0; background: transparent; cursor: pointer; display: grid; place-items: center; font-weight: 700; font-size: 1.1rem; color: var(--ink); }
@media (hover: hover) { .icon-btn:hover { background: var(--bg2); } }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg .fill { fill: currentColor; stroke: none; }
.title-row { display: flex; align-items: baseline; justify-content: space-between; padding: 4px 0 8px; gap: 10px; max-width: 600px; margin: 0 auto; }
body.ingame .title-row { max-width: none; }
h1 { font: 700 2rem/1 var(--head); margin: 0; letter-spacing: .01em; display: flex; align-items: center; gap: 10px; }
.h-mark { width: 22px; height: 22px; border-radius: 4px; flex: none; border: 1.5px solid var(--ink);
  background: conic-gradient(var(--ink) 0 25%, transparent 0 50%, var(--ink) 0 75%, transparent 0) 0 0 / 50% 50%; }
.sub { margin: 0; color: var(--muted); font-size: .95rem; text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.lb-modes { max-width: 600px; }
body.ingame .lb-modes { max-width: none; }

/* ------------------------------------------------------------ cards, buttons, segmented pickers */
.card { background: var(--card); border: 1px solid var(--line); border-radius: 20px; padding: 18px 16px; max-width: 600px; margin: 0 auto; }
.setup-h { margin: 0 0 12px; font: 700 1.25rem/1.2 var(--head); }
.field { margin: 0 0 14px; }
.lbl { margin: 0 0 6px; font-weight: 700; font-size: .95rem; }
.fine { margin: 6px 0 0; color: var(--muted); font-size: .92rem; }
.seg { display: flex; flex-wrap: wrap; gap: 3px; padding: 3px; background: var(--bg2); border: 1px solid var(--line); border-radius: 14px; }
.seg button { flex: 1 1 0; min-width: 0; border: 0; background: transparent; border-radius: 11px; min-height: 40px; padding: 4px 8px; font-weight: 700; font-size: .95rem; color: var(--muted); cursor: pointer; white-space: nowrap; }
.seg button.on { background: var(--card); color: var(--accent); box-shadow: 0 1px 4px rgba(20, 16, 10, .16); }
.seg-tc { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
.seg-opp button { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; }
@media (hover: hover) { .seg button:not(.on):hover { color: var(--ink); } }
.go { display: block; width: 100%; border: 0; background: var(--accent); color: var(--on-accent); font-weight: 700; font-size: 1.05rem; border-radius: 999px; min-height: 48px; padding: 0 26px; cursor: pointer; margin: 6px 0 0; }
.btn { border: 1px solid var(--line); background: var(--card); color: var(--ink); border-radius: 999px; padding: 0 16px; min-height: 40px; font-weight: 700; cursor: pointer; }
.btn.sm { min-height: 38px; padding: 0 14px; font-size: .95rem; }
.btn.ghost { background: transparent; }
.btn.pri { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.dng { color: var(--bad); }
.btn.armed { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn:disabled { opacity: .5; cursor: default; }
@media (hover: hover) { .btn:not(:disabled):hover { border-color: var(--accent); } }
.lnk { border: 0; background: none; padding: 6px 2px; color: var(--muted); text-decoration: underline; text-underline-offset: 3px; cursor: pointer; font-size: .92rem; }
.chk { display: flex; align-items: center; gap: 10px; min-height: 40px; font-weight: 500; cursor: pointer; }
.chk input { width: 20px; height: 20px; accent-color: var(--accent); margin: 0; }

/* the online lobby */
.online { max-width: 600px; margin: 0 auto; }
.online .lb-box { margin-top: 0; }
.rs { display: grid; gap: 0; margin-top: 4px; }
.rs .field { margin-bottom: 10px; }
.rs-line { margin: 0 0 6px; }

/* ------------------------------------------------------------ the game */
.game { display: grid; gap: 12px 24px; justify-content: center; grid-template-columns: minmax(0, 640px); margin-top: 2px; }
.play { width: 100%; min-width: 0; }
@media (min-width: 900px) {
  .game { grid-template-columns: minmax(0, min(640px, calc(100dvh - 200px))) 300px; align-items: start; }
  .play { min-width: 320px; }
}
.pbar { display: flex; align-items: center; gap: 8px; height: 40px; min-width: 0; padding: 0 2px; }
.pdot { width: 14px; height: 14px; border-radius: 50%; flex: none; border: 1.5px solid var(--pw-line); }
.pdot.w { background: var(--pw-fill); }
.pdot.b { background: var(--pb-fill); border-color: var(--pb-line); }
:root[data-theme="dark"] .pdot.b { border-color: #6b6a66; }
.pname { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; flex: 0 1 auto; }
.pbar.turn .pname { color: var(--accent); }
.pcap { display: flex; align-items: center; flex: 1 1 0; min-width: 0; overflow: hidden; gap: 0; }
.pcap .cp { display: inline-block; width: 17px; height: 17px; margin-right: -5px; flex: none; }
.pcap .cp svg { width: 100%; height: 100%; display: block; }
.pcap .adv { margin-left: 9px; font-size: .85rem; color: var(--muted); }
.clock { margin-left: auto; flex: none; font: 700 1.15rem/1 var(--font); font-variant-numeric: tabular-nums; padding: 7px 10px; border-radius: 9px; background: var(--bg2);
  color: var(--muted); min-width: 74px; text-align: right; border: 1px solid var(--line); }
.clock.run { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.clock.low { color: var(--bad); }
.clock.low.run { background: var(--bad); border-color: var(--bad); color: #fff; }

.board-wrap { width: 100%; }
.board { position: relative; width: 100%; aspect-ratio: 1 / 1; border-radius: 6px; overflow: hidden; touch-action: none; -webkit-user-select: none; user-select: none;
  box-shadow: 0 1px 0 rgba(0, 0, 0, .06), 0 6px 22px rgba(40, 30, 15, .16); -webkit-touch-callout: none; }
.sqs { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr); }
.sq { position: relative; outline: none; }
.sq.lt { background: var(--sq-l); }
.sq.dk { background: var(--sq-d); }
.sq.last::before, .sq.sel::before, .sq.pre::before { content: ""; position: absolute; inset: 0; background: var(--hl-last); }
.sq.sel::before { background: var(--hl-sel); }
.sq.pre::before { background: var(--hl-pre); }
.sq.chk { background-image: radial-gradient(circle, var(--chk) 0, rgba(214, 48, 40, .55) 32%, rgba(214, 48, 40, 0) 70%); }
.sq.dot::after { content: ""; position: absolute; left: 50%; top: 50%; width: 30%; height: 30%; margin: -15% 0 0 -15%; border-radius: 50%; background: var(--dot); z-index: 1; }
.sq.cap::after { content: ""; position: absolute; inset: 4%; border-radius: 50%; border: 5px solid var(--ring); z-index: 1; }
.sq.hov::after { width: auto; height: auto; inset: 0; margin: 0; border-radius: 0; border: 0; background: var(--dot); }
.sq:focus-visible { box-shadow: inset 0 0 0 3px var(--accent); }
.cr, .cf { position: absolute; font: 700 11px/1 var(--font); pointer-events: none; z-index: 2; }
.cr { left: 3px; top: 3px; }
.cf { right: 3px; bottom: 2px; }
.lt .cr, .lt .cf { color: var(--coord-l); }
.dk .cr, .dk .cf { color: var(--coord-d); }
.pcs { position: absolute; inset: 0; pointer-events: none; z-index: 3; }
.pc { position: absolute; left: 0; top: 0; width: 12.5%; height: 12.5%; }
.pc svg { width: 100%; height: 100%; display: block; }
.pc.slide { transition: transform .17s cubic-bezier(.3, .7, .4, 1); }
.pc.drag { z-index: 5; }
.pc.drag svg { transform: scale(1.18); filter: drop-shadow(0 6px 6px rgba(0, 0, 0, .3)); }
.board.review .pcs { opacity: .88; }
.board.review { box-shadow: 0 0 0 3px var(--hl-pre), 0 6px 22px rgba(40, 30, 15, .16); }
/* the piece set: one outline width, white and black */
.pw .b { fill: var(--pw-fill); stroke: var(--pw-line); stroke-width: 3; stroke-linejoin: round; }
.pw .d { fill: none; stroke: var(--pw-line); stroke-width: 3; stroke-linecap: round; }
.pw .e { fill: var(--pw-line); }
.pb .b { fill: var(--pb-fill); stroke: var(--pb-line); stroke-width: 3; stroke-linejoin: round; }
.pb .d { fill: none; stroke: var(--pb-detail); stroke-width: 2.6; stroke-linecap: round; }
.pb .e { fill: var(--pb-detail); }
/* promotion choice */
.promo { position: absolute; inset: 0; z-index: 6; background: rgba(15, 12, 8, .42); }
.pr { position: absolute; left: 0; top: 0; width: 12.5%; height: 12.5%; border: 0; padding: 0; margin: 0; background: var(--card); cursor: pointer; border-radius: 50%; display: grid; place-items: center; box-shadow: 0 2px 8px rgba(0, 0, 0, .35); }
.pr svg { width: 88%; height: 88%; display: block; }
.pr.px { font-size: 1.6rem; font-weight: 700; color: var(--muted); border-radius: 50%; transform-origin: center; }
@media (hover: hover) { .pr:hover { background: var(--bg2); } }

/* the side panel */
.side { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.status { margin: 0; min-height: 1.45em; font-weight: 500; }
.offer { background: var(--card); border: 1px solid var(--accent); border-radius: 14px; padding: 10px 12px; }
.offer p { margin: 0 0 8px; font-weight: 700; }
.offer-b { display: flex; gap: 8px; }
.result { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 12px 14px; }
.result .big { margin: 0; font: 700 1.5rem/1.2 var(--head); }
.result.win .big { color: var(--good); }
.result.loss .big { color: var(--bad); }
.res-line { margin: 2px 0 0; color: var(--muted); }
.nav { display: flex; gap: 4px; }
.nb { flex: 1 1 0; min-width: 0; height: 38px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); cursor: pointer; display: grid; place-items: center; }
.nb svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
#btn-flip { flex: 1.3 1 0; }
.moves { list-style: none; margin: 0; padding: 4px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; max-height: 300px; min-height: 44px; overflow-y: auto; font-variant-numeric: tabular-nums; }
.mv { display: grid; grid-template-columns: 2.6em 1fr 1fr; align-items: center; }
.mv .no { color: var(--muted); font-size: .9rem; padding-left: 4px; }
.san { border: 0; background: transparent; text-align: left; padding: 4px 8px; border-radius: 7px; cursor: pointer; font-weight: 500; min-height: 30px; }
.san.on { background: var(--bg2); color: var(--accent); font-weight: 700; }
.mv.res { display: block; text-align: center; font-weight: 700; padding: 4px; }
.acts { display: flex; flex-wrap: wrap; gap: 6px; }
.acts .btn { flex: 1 1 auto; }
.watch { margin: 0; color: var(--muted); font-size: .92rem; }
.watch:empty { display: none; }
.pgn-row { display: flex; gap: 14px; }

/* phones: the board as wide as the screen, the moves as one scrolling line, the actions right under the board */
@media (max-width: 899px) {
  body.ingame .title-row { display: none; }
  body.ingame .wrap { padding: 0 8px; }
  body.ingame .top { padding: 4px 6px 0; }
  body.ingame .lb-modes { margin: 4px 0 4px; }
  .side { gap: 6px; }
  .status { order: 1; font-size: .95rem; }
  .offer { order: 2; } .result { order: 3; } .acts { order: 4; } .nav { order: 5; } .moves { order: 6; } .watch { order: 7; } .pgn-row { order: 8; }
  .moves { display: flex; flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden; max-height: none; min-height: 0; padding: 2px 4px; scrollbar-width: none; }
  .moves::-webkit-scrollbar { display: none; }
  .moves:empty { display: none; }
  .mv { display: flex; flex: none; }
  .mv .no { padding: 0 0 0 6px; }
  .san { padding: 4px 6px; white-space: nowrap; }
  .pbar { height: 38px; }
  .clock { font-size: 1.05rem; padding: 6px 9px; min-width: 66px; }
}
@media (max-width: 380px) { .seg button { font-size: .88rem; padding: 4px 4px; } .acts .btn { padding: 0 10px; } }

/* ------------------------------------------------------------ dialogs, toast, footer */
dialog.dlg { border: 1px solid var(--line); border-radius: 20px; background: var(--card); color: var(--ink); max-width: min(460px, calc(100vw - 24px)); width: 100%; padding: 24px 22px; }
dialog.dlg::backdrop { background: var(--backdrop); }
dialog.dlg h2 { margin: 0 0 10px; font: 700 1.35rem/1.2 var(--head); }
dialog.dlg h3 { margin: 14px 0 4px; font-size: 1.05rem; }
dialog.dlg p { margin: 6px 0; }
dialog.dlg .x { border: 0; background: var(--bg2); color: var(--ink); font-size: 1.5rem; cursor: pointer; }
dialog.dlg .go { margin-top: 14px; }
.toast { position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px); background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 999px;
  opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s; z-index: 50; font-weight: 700; max-width: calc(100vw - 32px); text-align: center; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.ad-inline { margin: 18px 0; }
.foot { display: flex; flex-wrap: wrap; gap: 8px 18px; justify-content: center; padding: 18px 0 30px; color: var(--muted); font-size: .85rem; border-top: 1px solid var(--line); margin-top: 10px; }
.foot a { color: var(--muted); }
.tg-about { max-width: 640px; margin-left: auto; margin-right: auto; }
@media (prefers-reduced-motion: reduce) { .pc.slide, .toast { transition: none; } }
/* captured black pieces on the dark page need a lighter edge to show */
:root[data-theme="dark"] .pcap .pb .b { stroke: #8D877C; }
