:root {
  color-scheme: light;
  --bg: #fff;
  --page: #fbf1d6;
  --surface: #fffaf0;
  --text: #3b2a1a;
  --muted: #7a6450;
  --line: #e7d6b6;
  --accent: #e8a92a;
  --accent-text: #8a5410;
  --tint: #fdf0cf;
  --cell: #fff;
  --cell-line: #9c8a74;
  --determined-black: #555;
  --determined-white: #eae7e2;
  --stone: #111;
  --on-stone: #fff;
  --shadow: 0 16px 70px #3b2a1a26;
}

:root[data-theme=dark] {
  color-scheme: dark;
  --bg: #241a10;
  --page: #1a120a;
  --surface: #2c2014;
  --text: #f5ead8;
  --muted: #c4ad8e;
  --line: #4a3824;
  --accent: #f0b53a;
  --accent-text: #f0b53a;
  --tint: #3d2d14;
  --cell: #f6f3ee;
  --cell-line: #8a7860;
  --determined-black: #555;
  --determined-white: #b9b4ad;
  --stone: #0a0a0a;
  --on-stone: #fff;
  --shadow: 0 16px 70px #0009;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--page); color: var(--text); font: 16px/1.6 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
button, select, input { font: inherit; }
button, select { min-height: 44px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text); }
button { padding: 9px 18px; cursor: pointer; font-weight: 600; touch-action: manipulation; }
button:hover:not(:disabled) { background: var(--tint); border-color: var(--accent); }
button:disabled, select:disabled { opacity: .5; cursor: default; }
button:focus-visible, select:focus-visible, input:focus-visible, summary:focus-visible, a:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
a { color: var(--accent-text); text-underline-offset: 4px; }
.page { width: 100%; max-width: 820px; margin: auto; padding: 0 16px 32px; }
.header { padding: 16px 0 24px; }
.preferences { display: flex; justify-content: space-between; gap: 12px; }
.small-toggle { display: inline-flex; align-items: center; gap: 2px; font-size: .8rem; color: var(--muted); }
.small-toggle button { padding: 6px 9px; min-width: 44px; border-color: transparent; background: transparent; font-weight: 400; }
.small-toggle button[aria-pressed=true] { color: var(--accent-text); font-weight: 750; text-decoration: underline; text-underline-offset: 5px; }
h1 { font-size: clamp(1.45rem, 4.6vw, 2.15rem); letter-spacing: -.035em; line-height: 1.3; margin: 16px 0 12px; overflow-wrap: anywhere; }
.panel { max-width: 620px; margin: 0 auto 24px; border: 1px solid var(--line); border-radius: 18px; padding: 24px; background: var(--surface); }
h2 { font-size: 1.15rem; margin: 0 0 20px; }
.field { display: grid; grid-template-columns: 110px minmax(0, 1fr); align-items: center; gap: 10px; margin: 14px 0; font-weight: 600; }
select { width: 100%; min-width: 0; padding: 9px 12px; }
.field-note { margin: -4px 0 20px; color: var(--muted); font-size: .8rem; }
.rules { padding: 0; margin: 0 0 22px; border: 0; min-width: 0; }
.rules legend { font-weight: 600; margin-bottom: 6px; }
.rule-choice { display: flex; align-items: flex-start; gap: 12px; padding: 12px 0; cursor: pointer; }
.rule-choice + .rule-choice { border-top: 1px solid var(--line); }
.rule-choice input { width: 20px; height: 20px; margin: 3px 0 0; flex-shrink: 0; accent-color: var(--accent-text); }
.rule-choice strong { display: block; font-size: .94rem; }
.rule-choice small { display: block; color: var(--muted); font-size: .8rem; margin-top: 2px; }
.primary { background: var(--accent); border-color: var(--accent); color: #2a1800; }
.primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.08); }
.start { width: 100%; }
.game { max-width: 620px; margin: auto; }
.scoreboard { text-align: center; padding-top: 8px; }
.game-label { color: var(--muted); font-size: .78rem; margin: 0 0 8px; min-height: 1.6em; }
.counter { margin: 0 0 12px; display: flex; align-items: baseline; justify-content: center; flex-wrap: wrap; gap: 0 7px; line-height: 1.3; font-size: .95rem; }
.counter strong { font-size: clamp(2.15rem, 8vw, 3.1rem); font-weight: 750; letter-spacing: -.05em; font-variant-numeric: tabular-nums; }
.progress { height: 4px; overflow: hidden; border-radius: 4px; background: var(--line); width: min(100%, 360px); margin: auto; }
.progress span { display: block; width: 0; height: 100%; background: var(--accent); }
.preview { min-height: 1.6em; color: var(--accent-text); font-size: .82rem; margin: 10px 0 4px; font-variant-numeric: tabular-nums; }
.status { font-weight: 700; margin: 0; min-height: 1.6em; }
.status.thinking::before { content: ""; display: inline-block; width: 8px; height: 8px; margin-right: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1s ease-in-out infinite alternate; }
.passes { color: var(--muted); font-size: .85rem; margin: 4px 0 0; }
.error { border: 1px solid var(--accent); padding: 12px; border-radius: 10px; overflow-wrap: anywhere; }
#board { width: 100%; max-width: 620px; display: block; margin: 10px auto 12px; overflow: visible; touch-action: manipulation; }
#board .cell { stroke: var(--cell-line); stroke-width: .32; stroke-linejoin: round; }
#board .free { fill: var(--cell); }
#board .fb { fill: var(--determined-black); }
#board .fw { fill: var(--determined-white); }
#board .stone { fill: var(--stone); }
#board .last { stroke: var(--accent); stroke-width: .95; }
#board .playable { cursor: pointer; }
#board .playable:hover .cell, #board .playable:focus .cell { stroke: var(--accent); stroke-width: .95; }
#board .playable:hover .free, #board .playable:focus .free { fill: var(--tint); }
#board .playable.pass:hover .cell, #board .playable.pass:focus .cell { stroke-dasharray: 1.3 .8; }
#board g:focus { outline: none; }
#board .focus-ring { fill: none; stroke: var(--accent); stroke-width: .55; opacity: 0; pointer-events: none; transform: scale(1.11); transform-box: fill-box; transform-origin: center; }
#board .playable:focus-visible .focus-ring { opacity: 1; }
#board text { font-size: 2.8px; text-anchor: middle; dominant-baseline: central; pointer-events: none; fill: var(--on-stone); font-weight: 700; }
#board .placed { transform-box: fill-box; transform-origin: center; animation: place .22s ease-out; }
.legend { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px 16px; font-size: .72rem; color: var(--muted); margin: 0 0 24px; }
.legend > span { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 13px; height: 15px; display: inline-block; clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%); background: var(--cell-line); position: relative; }
.swatch::after { content: ""; position: absolute; inset: 1px; clip-path: inherit; }
.swatch.undetermined::after { background: var(--cell); }
.swatch.black::after { background: var(--determined-black); }
.swatch.white::after { background: var(--determined-white); }
.game-actions { display: flex; justify-content: center; gap: 12px; margin: 0 0 32px; }
.game-actions button { min-width: 110px; }
.how-to { max-width: 620px; margin: 28px auto 0; border-top: 1px solid var(--line); padding-top: 12px; font-size: .9rem; }
summary { padding: 10px 0; min-height: 44px; cursor: pointer; font-weight: 600; }
.how-to ul { padding-left: 22px; color: var(--muted); }
.how-to li { margin: 10px 0; }
dialog { width: min(420px, calc(100% - 32px)); max-height: calc(100% - 32px); border: 1px solid var(--line); border-radius: 22px; padding: 28px; background: var(--bg); color: var(--text); box-shadow: var(--shadow); text-align: center; }
dialog::backdrop { background: #0007; }
dialog h2 { font-size: 1.65rem; margin-bottom: 8px; }
.result-actions { display: grid; gap: 10px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }
.noscript { padding: 16px; text-align: center; }
@keyframes place { from { transform: scale(.65); opacity: .4; } to { transform: scale(1); opacity: 1; } }
@keyframes pulse { from { opacity: .35; } to { opacity: 1; } }
@media (max-width: 480px) {
  .panel { padding: 18px 16px; }
  .field { grid-template-columns: 1fr; gap: 6px; }
  .header { padding-bottom: 18px; }
  .legend { gap: 8px 12px; }
  dialog { padding: 24px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* setup popup: toggle buttons and rule cards */
dialog.setup-dialog { width: min(560px, calc(100% - 24px)); text-align: left; padding: 24px; }
.setup-dialog h2 { font-size: 1.3rem; margin: 0 0 14px; }
.choice { border: 0; padding: 0; margin: 0 0 16px; min-width: 0; }
.choice legend { font-weight: 650; font-size: .88rem; color: var(--muted); margin-bottom: 6px; padding: 0; }
.choice.off { opacity: .45; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg label { flex: 1 1 0; min-width: max-content; position: relative; }
.seg input, .rule-card input { position: absolute; opacity: 0; width: 1px; height: 1px; pointer-events: none; }
.seg span { display: flex; align-items: center; justify-content: center; min-height: 44px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-weight: 600; font-size: .9rem; cursor: pointer; text-align: center; }
.seg label:hover span { border-color: var(--accent); }
.seg input:checked + span { background: var(--accent-text); border-color: var(--accent-text); color: #fff; }
:root[data-theme=dark] .seg input:checked + span { background: var(--accent); border-color: var(--accent); color: #2a1800; }
.seg input:focus-visible + span, .rule-card input:focus-visible + .rule-body { outline: 3px solid var(--accent); outline-offset: 2px; }
.seg input:disabled + span { cursor: default; }
.field-note { margin: 6px 0 0; }
.rule-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.rule-card { position: relative; display: block; cursor: pointer; }
.rule-body { display: block; height: 100%; padding: 12px 14px; border: 2px solid var(--line); border-radius: 14px; background: var(--bg); }
.rule-card:hover .rule-body { border-color: var(--accent); }
.rule-card input:checked + .rule-body { border-color: var(--accent-text); background: var(--tint); box-shadow: inset 0 0 0 1px var(--accent-text); }
:root[data-theme=dark] .rule-card input:checked + .rule-body { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.rule-body strong { display: block; font-size: .95rem; }
.rule-body small { display: block; color: var(--muted); font-size: .78rem; line-height: 1.45; margin-top: 4px; }
.rule-icon { display: block; width: 78px; height: 36px; margin-bottom: 6px; }
.ri-cell { fill: #fff; stroke: var(--cell-line); stroke-width: 1; }
.ri-stone { fill: var(--stone); stroke: var(--stone); stroke-width: 1; }
.ri-black { fill: var(--determined-black); stroke: var(--accent); stroke-width: 1.4; stroke-dasharray: 2.5 1.8; }
.setup-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }
.setup-actions .start { flex: 1 1 auto; max-width: 260px; }
@media (max-width: 480px) {
  .rule-cards { grid-template-columns: 1fr; }
  .seg label { min-width: 0; }
  .setup-actions .start { max-width: none; }
}
.check { display: flex; align-items: center; gap: 10px; min-height: 40px; font-weight: 600; font-size: .92rem; cursor: pointer; }
.check input { width: 20px; height: 20px; margin: 0; accent-color: var(--accent-text); }
.check:has(input:disabled) { opacity: .55; cursor: default; }
.check + .field-note { margin: -4px 0 6px 30px; }
.foul-cross { fill: none; stroke: #d62828; stroke-width: 1.4; stroke-linecap: round; pointer-events: none; }
.load { display: flex; align-items: center; gap: 12px; margin-top: 16px; font-size: .85rem; color: var(--muted); }
.load-bar { flex: 1; height: 8px; border-radius: 4px; background: var(--determined-white); overflow: hidden; }
.load-bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .15s; }
.load.indeterminate .load-bar span { width: 30%; animation: load-slide 1.2s ease-in-out infinite; }
@keyframes load-slide { from { transform: translateX(-100%); } to { transform: translateX(340%); } }
@media (prefers-reduced-motion: reduce) { .load-bar span { transition: none; } .load.indeterminate .load-bar span { animation: none; width: 100%; opacity: .4; } }
.heat-toggle { justify-content: center; margin: 4px 0 0; font-size: .88rem; }
/* end of game: a banner above the board, which stays visible */
.result-banner { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px 16px; margin: 4px 0 12px; padding: 12px 16px; border: 2px solid var(--accent); border-radius: 14px; background: var(--bg); }
.result-banner h2 { margin: 0; font-size: 1.25rem; }
.result-banner #result-moves { margin: 2px 0 0; color: var(--muted); font-size: .85rem; }
.result-banner .result-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.result-banner .result-actions button { min-height: 40px; padding: 6px 14px; font-size: .9rem; }
@media (max-width: 480px) { .result-banner .result-actions { width: 100%; } .result-banner .result-actions button { flex: 1 1 auto; } }
/* non-modal setup popup: the backdrop starts below the site bar, which stays clickable */
dialog.setup-dialog[open] { position: fixed; inset: 36px 0 0; margin: auto; z-index: 45; max-height: calc(100dvh - 52px); overflow: auto; }
body:has(dialog.setup-dialog[open])::before { content: ""; position: fixed; inset: 36px 0 0; z-index: 44; background: #0007; }
body:has(dialog.setup-dialog[open]) { overflow: hidden; }

/* app layout: the page fits the viewport and never scrolls; the board takes the free height */
body.app { height: 100vh; height: 100dvh; overflow: hidden; display: flex; flex-direction: column; }
body.app .page { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; max-width: 820px; padding: 0 16px 10px; }
body.app main, body.app .game { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; width: 100%; }
body.app .header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 0 2px; }
body.app h1 { font-size: 1.05rem; letter-spacing: -.01em; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
body.app .preferences { flex-shrink: 0; align-items: center; gap: 4px; }
body.app .small-toggle button { min-height: 36px; min-width: 38px; padding: 4px 6px; }
.how-open { min-height: 32px; width: 32px; padding: 0; border-radius: 50%; font-size: .9rem; margin-left: 4px; }
body.app .scoreboard { padding-top: 0; }
body.app .game-label { margin: 0; min-height: 1.4em; }
body.app .counter { margin: 0 0 6px; }
body.app .counter strong { font-size: clamp(1.8rem, 6vh, 3rem); }
body.app .preview { margin: 4px 0 0; min-height: 1.4em; }
body.app .status { min-height: 1.5em; }
body.app #board { flex: 1 1 0; min-height: 120px; width: 100%; height: auto; max-width: none; margin: 4px 0; }
body.app .result-banner { margin: 4px 0; padding: 8px 12px; }
.board-foot { flex-shrink: 0; display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 4px 16px; }
body.app .legend { margin: 0; width: 100%; }
body.app .heat-toggle { margin: 0; min-height: 40px; }
body.app .game-actions { margin: 0; gap: 8px; }
body.app .game-actions button { min-width: 96px; min-height: 40px; padding: 6px 14px; }
.how-dialog { text-align: left; width: min(480px, calc(100% - 32px)); }
.how-dialog h2 { font-size: 1.2rem; }
.how-dialog ul { padding-left: 20px; color: var(--muted); font-size: .92rem; }
.how-dialog li { margin: 8px 0; }
.how-dialog form { text-align: right; margin-top: 12px; }
@media (max-height: 700px) { body.app .preview, body.app .game-label { display: none; } }
/* phones: the site bar sits at the bottom, so the setup popup and its backdrop stop above it */
@media (max-width: 640px) {
  body.app .page { padding: 0 10px 4px; }
  body.app .counter { font-size: .85rem; }
  body.app .legend { font-size: .68rem; gap: 4px 10px; }
  dialog.setup-dialog[open], body:has(dialog.setup-dialog[open])::before { inset: 0 0 calc(58px + env(safe-area-inset-bottom)); }
  dialog.setup-dialog[open] { max-height: calc(100dvh - 70px - env(safe-area-inset-bottom)); padding: 18px 16px; }
  .setup-dialog h2 { font-size: 1.1rem; margin-bottom: 8px; }
  .choice { margin-bottom: 10px; }
  .choice legend { margin-bottom: 4px; font-size: .8rem; }
  .seg { gap: 4px; flex-wrap: nowrap; }
  .seg span { min-height: 40px; padding: 4px 2px; font-size: .8rem; line-height: 1.25; white-space: nowrap; }
  .seg label { flex: 1 1 auto; }
  .rule-cards { grid-template-columns: 1fr 1fr; gap: 6px; }
  .rule-body { padding: 8px 10px; }
  .rule-icon { width: 52px; height: 24px; margin-bottom: 2px; }
  .rule-body small { font-size: .72rem; line-height: 1.35; }
  .field-note { font-size: .74rem; }
  .check { min-height: 34px; font-size: .86rem; }
  .setup-actions { position: sticky; bottom: -18px; margin: 12px -16px -18px; padding: 10px 16px; background: var(--bg); border-top: 1px solid var(--line); }
  .setup-actions .start { max-width: none; }
  .result-banner h2 { font-size: 1.05rem; }
  .result-banner .result-actions button { min-height: 36px; padding: 4px 8px; font-size: .82rem; }
}
