:root {
  --bg: #f3efe6;
  --surface: #fffdf8;
  --ink: #1f1c17;
  --muted: #7a7266;
  --line: #e4ddcf;
  --accent: #2f6f5e;
  --accent-ink: #fff;
  --warn: #b7791f;
  --danger: #b4432f;
  --wood1: #e7c07a;
  --wood2: #d6a95c;
  --good: #2f8f5b;
  --shadow: 0 1px 2px rgba(40, 30, 10, .06), 0 8px 24px rgba(40, 30, 10, .07);
  /* chart identities (validated categorical order): opponent, assistant, katago, llm */
  --series-1: #2a78d6; --series-2: #eb6834; --series-3: #1baf7a; --series-4: #eda100;
  --neutral-series: #52514e;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171613; --surface: #211f1b; --ink: #ece6da; --muted: #a39a8b; --line: #37332c;
    --accent: #5fb39a; --accent-ink: #0f1a16; --warn: #e0a84a; --danger: #e07a64;
    --wood1: #d8b06b; --wood2: #c49551; --good: #5cc98d;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
    --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
    --neutral-series: #c3c2b7;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.55 "IBM Plex Sans Thai", -apple-system, "Helvetica Neue", sans-serif;
  min-height: 100vh;
}
button, input, select { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px;
  padding: 8px 14px; cursor: pointer; transition: transform .08s, background .15s, border-color .15s;
}
button:hover { border-color: var(--muted); }
button:active { transform: translateY(1px); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.accent { border-color: var(--accent); color: var(--accent); font-weight: 600; }
button.danger { color: var(--danger); }
kbd { font-size: 11px; border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; margin-left: 4px; color: var(--muted); }

.topbar { display: flex; align-items: center; justify-content: space-between; padding: 12px 24px; }
.brand { font-weight: 700; font-size: 20px; letter-spacing: .2px; }
.brand small { font-weight: 500; color: var(--muted); font-size: 13px; margin-left: 6px; }
.logo { letter-spacing: -6px; margin-right: 10px; }
.engine { display: flex; gap: 8px; align-items: center; font-size: 13px; color: var(--muted); }
.engine .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--warn); animation: pulse 1.2s infinite; }
.engine.ok .dot { background: var(--good); animation: none; }
.engine.err .dot { background: var(--danger); animation: none; }
@keyframes pulse { 50% { opacity: .3; } }

.layout { display: grid; grid-template-columns: minmax(0, 1fr) 400px; gap: 24px; padding: 0 24px 24px; max-width: 1400px; margin: 0 auto; }
@media (max-width: 980px) { .layout { grid-template-columns: 1fr; padding: 0 16px 16px; } .topbar { padding: 12px 16px; } }

.board-wrap { display: flex; flex-direction: column; gap: 12px; align-items: center; }
.players { width: 100%; max-width: calc(100vh - 190px); min-width: 280px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; }
.player { display: flex; gap: 10px; align-items: center; padding: 8px 12px; border-radius: 12px; border: 1px solid transparent; }
.player.right { justify-content: flex-end; text-align: right; }
.player small { display: block; color: var(--muted); font-size: 12px; }
.player.active { background: var(--surface); border-color: var(--line); box-shadow: var(--shadow); }
.stone { width: 22px; height: 22px; border-radius: 50%; flex: none; }
.stone.b { background: radial-gradient(circle at 35% 30%, #666, #111 60%); }
.stone.w { background: radial-gradient(circle at 35% 30%, #fff, #d9d4c7 75%); border: 1px solid #bbb; }
.turn { font-size: 13px; color: var(--muted); text-align: center; }

.board-box { position: relative; width: 100%; max-width: calc(100vh - 190px); min-width: 280px; aspect-ratio: 1; }
#board { width: 100%; height: 100%; border-radius: 6px; box-shadow: var(--shadow); cursor: pointer; display: block; }
.thinking { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); background: rgba(20,18,14,.82); color: #fff; padding: 6px 14px; border-radius: 99px; font-size: 13px; display: flex; gap: 8px; align-items: center; }
.hidden { display: none !important; }
.spinner { width: 12px; height: 12px; border: 2px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.controls { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
button.on kbd { color: inherit; border-color: currentColor; }
button.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

.panel { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); padding: 14px 16px; }
.eval-head { display: flex; justify-content: space-between; font-weight: 600; margin-bottom: 8px; }
#scoreText { font-variant-numeric: tabular-nums; }
.wrbar { position: relative; height: 26px; border-radius: 8px; overflow: hidden; background: #f4f1e8; border: 1px solid var(--line); }
.wrB { height: 100%; background: linear-gradient(#2a2724, #121110); transition: width .4s ease; }
.wrbar .lbl { position: absolute; top: 3px; font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }
.wrbar .l { left: 8px; color: #fff; mix-blend-mode: difference; }
.wrbar .r { right: 8px; color: #333; }
#graph { width: 100%; height: 70px; margin-top: 10px; display: block; }

.assistant { display: flex; flex-direction: column; gap: 10px; flex: 1; min-height: 420px; }
.assist-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.assist-head small { display: block; color: var(--muted); font-size: 12px; font-weight: 400; }
.switch { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); cursor: pointer; white-space: nowrap; }
.switch input { accent-color: var(--accent); }
.feed { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; max-height: calc(100vh - 430px); min-height: 220px; padding-right: 4px; }
@media (max-width: 980px) { .feed { max-height: 420px; } }
.msg { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; font-size: 14px; background: var(--bg); }
.msg .t { font-weight: 600; margin-bottom: 4px; display: flex; justify-content: space-between; gap: 8px; }
.msg .t small { color: var(--muted); font-weight: 400; }
.msg ul { margin: 4px 0 0; padding-left: 0; list-style: none; }
.msg li { margin: 2px 0; }
.msg.user { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); align-self: flex-end; max-width: 85%; }
.msg.best { border-left: 4px solid var(--good); }
.msg.good { border-left: 4px solid #6aa84f; }
.msg.ok { border-left: 4px solid var(--warn); }
.msg.mistake { border-left: 4px solid #d9822b; }
.msg.blunder { border-left: 4px solid var(--danger); }
.msg.hint { border-left: 4px solid var(--accent); }
.msg .mv { font-weight: 700; font-family: ui-monospace, Menlo, monospace; cursor: pointer; text-decoration: underline dotted; }
.msg.pending { color: var(--muted); font-style: italic; }
.quick { display: flex; flex-wrap: wrap; gap: 6px; }
.quick button { font-size: 12.5px; padding: 5px 10px; border-radius: 99px; }
.chat { display: flex; gap: 6px; }
.chat input { flex: 1; min-width: 0; border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 8px 12px; }
.chat input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

dialog { border: none; border-radius: 18px; padding: 0; background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.3); width: min(480px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(20, 16, 10, .45); backdrop-filter: blur(2px); }
.newgame, .result { padding: 22px 24px; display: flex; flex-direction: column; gap: 14px; }
.newgame h2, .result h2 { margin: 0; }
.newgame label, .newgame .field { display: flex; flex-direction: column; gap: 6px; font-weight: 500; font-size: 14px; }
.newgame select, .newgame input[type=number] { border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 8px 10px; }
.newgame label.check { flex-direction: row; align-items: center; font-weight: 400; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button { flex: 1; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.note { margin: 0; font-size: 12.5px; color: var(--muted); }
.actions { display: flex; justify-content: flex-end; gap: 8px; }
.result p { margin: 0; white-space: pre-line; }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: -4px -4px 2px; }
.tabs button { border: none; background: none; border-radius: 8px 8px 0 0; padding: 6px 10px; color: var(--muted); font-weight: 500; border-bottom: 2px solid transparent; }
.tabs button.sel { color: var(--ink); border-bottom-color: var(--accent); }
.tabpanel { display: flex; flex-direction: column; gap: 10px; flex: 1; min-height: 0; }
#tab-compare, #tab-machine { overflow-y: auto; max-height: calc(100vh - 330px); padding-right: 4px; }
@media (max-width: 980px) { #tab-compare, #tab-machine { max-height: none; } }
.tabpanel h4 { margin: 6px 0 -4px; font-size: 13px; font-weight: 600; }
.note b { font-weight: 600; }
.k-ai { color: var(--ink); border-bottom: 3px solid var(--series-1); }
.k-as { color: var(--ink); border-bottom: 3px solid var(--series-2); }
.k-llm { color: var(--ink); border-bottom: 3px solid var(--series-4); }
details.note ul { margin: 6px 0 0; padding-left: 18px; }
details.note summary { cursor: pointer; }

/* ---------- stat tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(108px, 1fr)); gap: 8px; }
.tile { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: var(--bg); }
.tile small { display: block; font-size: 11.5px; color: var(--muted); line-height: 1.3; }
.tile b { font-size: 19px; font-variant-numeric: tabular-nums; font-weight: 600; }
.tile span { font-size: 11.5px; color: var(--muted); }
.tile .sw { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; vertical-align: 1px; }
.top-right { display: flex; align-items: center; gap: 16px; }
.mini { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; cursor: pointer; }
.mini b { color: var(--ink); font-weight: 600; }
@media (max-width: 700px) { .mini { display: none; } }

/* ---------- charts ---------- */
.chart { position: relative; }
.chart svg { display: block; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .base { stroke: var(--muted); stroke-width: 1; }
.chart .ref { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .cross { stroke: var(--muted); stroke-width: 1; }
.chart .axis-t { fill: var(--muted); font-size: 10.5px; font-variant-numeric: tabular-nums; }
.chart .end-t { fill: var(--ink); font-size: 11px; font-weight: 500; font-variant-numeric: tabular-nums; }
.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.legend i, .tip i { display: inline-block; width: 10px; height: 3px; border-radius: 2px; margin-right: 5px; vertical-align: 3px; }
.tip { position: absolute; z-index: 5; pointer-events: none; background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); border-radius: 8px; padding: 6px 9px; font-size: 12px; min-width: 150px; }
.tip .tt { font-weight: 600; margin-bottom: 2px; }
.tip div { display: flex; align-items: center; gap: 2px; }
.tip div b { margin-left: auto; padding-left: 10px; font-variant-numeric: tabular-nums; }
.tip small { display: block; color: var(--muted); margin: -1px 0 3px 15px; }

/* ---------- "what each AI is thinking" ---------- */
.think { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (max-width: 420px) { .think { grid-template-columns: 1fr; } }
.think .col { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: var(--bg); min-width: 0; }
.think .col h5 { margin: 0 0 6px; font-size: 12.5px; font-weight: 600; }
.think .col h5 small { font-weight: 400; color: var(--muted); display: block; }
.cand { display: grid; grid-template-columns: 38px 1fr auto; gap: 6px; align-items: center; font-size: 12px; margin: 3px 0; font-variant-numeric: tabular-nums; }
.cand .mv { font-weight: 700; font-family: ui-monospace, Menlo, monospace; cursor: pointer; }
.cand .bar { height: 8px; border-radius: 0 4px 4px 0; min-width: 2px; }
.cand .v { color: var(--muted); white-space: nowrap; }
.cand.chosen .mv::after { content: " ✔"; color: var(--good); }

/* ---------- player setup (new game dialog) ---------- */
.players-cfg { display: flex; flex-direction: column; gap: 6px; font-weight: 400; }
.pcfg { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; background: var(--bg); display: flex; flex-direction: column; gap: 8px; }
.pc-head { display: flex; align-items: center; gap: 10px; }
.pc-head > div:first-of-type { flex: 1; }
.pc-head small { display: block; color: var(--muted); font-size: 12px; }
.pc-ai { display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr); gap: 8px; align-items: end; }
.pc-ai select { min-width: 0; width: 100%; }
.pc-ai.no-vis { grid-template-columns: 1fr; }
.seg.sm button { padding: 5px 10px; font-size: 13px; flex: none; }
.pc-swap { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.pc-swap button { font-size: 12.5px; padding: 4px 10px; border-radius: 99px; }
.pc-swap .note { margin-left: auto; }

/* ---------- mode picker ---------- */
.modes { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
@media (max-width: 520px) { .modes { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.modes button { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 12px 6px; border-radius: 14px; text-align: center; }
.modes button span { font-size: 24px; line-height: 1.1; }
.modes button b { font-weight: 600; font-size: 14px; }
.modes button small { font-size: 11.5px; color: var(--muted); line-height: 1.3; }
.modes button.on small { color: inherit; opacity: .85; }
.p2p-note { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; background: var(--bg); display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.p2p-note div { display: flex; align-items: center; gap: 8px; }
.p2p-note .stone { width: 18px; height: 18px; }
.p2p-note .note { margin: 2px 0 0; }

.mini-field { display: flex !important; flex-direction: column; gap: 3px !important; font-size: 12px !important; color: var(--muted); font-weight: 500 !important; min-width: 0; }
.mini-field select { color: var(--ink); font-size: 14px; }
.pc-ai .human-note { grid-column: 1 / -1; margin: 0; }
.pc-ai:not(.no-vis) .human-note { display: none; }
.pc-ai.no-vis .vis-field { display: none !important; }
.p2p-row { display: grid !important; grid-template-columns: auto minmax(0, 1fr) minmax(0, 150px); gap: 10px !important; align-items: center; }
.p2p-row > div { display: block !important; min-width: 0; line-height: 1.35; }
.p2p-row small { display: block; color: var(--muted); font-size: 12px; }
.p2p-row select { border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 6px 8px; width: 100%; }
.eval.locked .wrbar, .eval.locked #graph { opacity: .25; filter: grayscale(1); }

/* ---------- online rooms ---------- */
.online-box { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; background: var(--bg); display: flex; flex-direction: column; gap: 10px; }
.online-box input { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 8px 10px; width: 100%; }
.online-box .note { margin: 0; }
.online-box .note.warn { color: var(--danger); }
.roombar { width: 100%; max-width: calc(100vh - 190px); min-width: 280px; border: 1px solid var(--line); background: var(--surface);
  border-radius: 12px; padding: 8px 12px; display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; font-size: 13px; box-shadow: var(--shadow); }
.roombar .code { font-family: ui-monospace, Menlo, monospace; font-weight: 700; font-size: 16px; letter-spacing: 2px; }
.roombar .link { flex: 1 1 220px; min-width: 0; display: flex; gap: 6px; align-items: center; }
.roombar .link input { flex: 1; min-width: 0; border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 4px 8px; font-size: 12px; }
.roombar button { padding: 4px 10px; font-size: 12.5px; border-radius: 8px; }
.roombar .seats { display: flex; gap: 10px; color: var(--muted); }
.roombar .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); margin-right: 4px; }
.roombar .dot.on { background: var(--good); }
.roombar .warn { color: var(--danger); flex-basis: 100%; }
.msg.room { border-left: 4px solid var(--muted); }
.msg.room.mine { background: var(--surface); }
.msg.sys { font-size: 12.5px; color: var(--muted); padding: 6px 10px; }
.chat-to { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); }

.mini-btn { padding: 4px 10px; font-size: 12.5px; border-radius: 99px; }
.modes button:disabled small::after { content: " · ต้องมีรหัสเข้าถึง"; }
