:root {
  --bg: #312e2b;
  --panel: #262522;
  --panel-2: #21201d;
  --row-alt: #2a2926;
  --line: #3c3a37;
  --text: #fff;
  --text-2: #c3c2c1;
  --muted: #8b8987;
  --green: #81b64c;
  --green-hi: #a3d160;
  --green-sh: #45753c;
  --sq-light: #ebecd0;
  --sq-dark: #739552;
  --hl-light: #f5f682;
  --hl-dark: #b9ca43;
  --mark-light: #ef8a77;
  --mark-dark: #d9694f;
  --bar: 46px;
  --panel-w: 400px;
  --gap: 20px;
  /* Board is as big as the viewport allows, chess.com style: height-bound on desktop. */
  --bsize: min(calc(100dvh - 2 * var(--bar) - 84px), calc(100vw - var(--panel-w) - 3 * var(--gap)), 860px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; opacity: .4; }
img { display: block; }

body[data-view="setup"] .game-only,
body[data-view="game"] .setup-only { display: none !important; }

/* ---------- top bar ---------- */
.topbar { height: 40px; display: flex; align-items: center; padding: 0 var(--gap); }
.brand { display: flex; align-items: center; gap: 6px; color: var(--text-2); text-decoration: none; font-weight: 700; font-size: 15px; }
.brand img { width: 26px; height: 26px; }
.brand span { color: var(--green); }
.brand:hover { color: #fff; }

/* ---------- layout ---------- */
.layout { display: flex; justify-content: center; align-items: flex-start; gap: var(--gap); padding: 4px var(--gap) 12px; }
.board-col { width: var(--bsize); flex: none; }
.board-wrap { position: relative; width: var(--bsize); height: var(--bsize); }

/* ---------- player bars ---------- */
.player { height: var(--bar); display: flex; align-items: center; gap: 10px; padding: 5px 0; }
.player .pinfo { min-width: 0; display: flex; flex-direction: column; justify-content: center; gap: 1px; }
.pname { font-weight: 700; font-size: 14px; }
.ptag { color: var(--muted); font-size: 13px; }
.player.to-move .pname::after { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--green); margin-left: 7px; vertical-align: 1px; }
.captured { display: flex; align-items: center; height: 16px; }
.captured img { width: 16px; height: 16px; margin-right: -5px; }
.captured img[src*="/b"] { filter: drop-shadow(0 0 1px rgba(255, 255, 255, .55)); }
.captured .adv { margin-left: 10px; color: var(--muted); font-size: 12px; font-weight: 600; }

.avatar { flex: none; width: 36px; height: 36px; border-radius: 4px; display: grid; place-items: center; overflow: hidden; }
.avatar svg { width: 62%; height: 62%; }
.avatar img { width: 100%; height: 100%; }
.avatar.lg { width: 48px; height: 48px; border-radius: 6px; }
.avatar.xl { width: 88px; height: 88px; border-radius: 8px; }

/* ---------- board ---------- */
.board { position: relative; width: 100%; height: 100%; user-select: none; -webkit-user-select: none; touch-action: none; border-radius: 3px; overflow: hidden; cursor: pointer; }
.squares { position: absolute; inset: 0; display: grid; grid-template: repeat(8, 1fr) / repeat(8, 1fr); }
.sq { position: relative; }
.sq.light { background-color: var(--sq-light); }
.sq.dark { background-color: var(--sq-dark); }
.sq.light.last, .sq.light.selected { background-color: var(--hl-light); }
.sq.dark.last, .sq.dark.selected { background-color: var(--hl-dark); }
.sq.light.marked { background-color: var(--mark-light); }
.sq.dark.marked { background-color: var(--mark-dark); }
.sq.check { background-image: radial-gradient(ellipse at center, #ff0000 0%, #e70000 25%, rgba(169, 0, 0, 0) 89%, rgba(158, 0, 0, 0) 100%); }
.sq.dest::after { content: ""; position: absolute; inset: 0; margin: auto; width: 33%; height: 33%; border-radius: 50%; background: rgba(0, 0, 0, .14); z-index: 3; pointer-events: none; }
.sq.dest-capture::after { content: ""; position: absolute; inset: 0; border-radius: 50%; background: radial-gradient(transparent 0, transparent 79%, rgba(0, 0, 0, .14) 80%); z-index: 3; pointer-events: none; }
.sq.hover { box-shadow: inset 0 0 0 calc(var(--bsize) / 8 * .055) rgba(255, 255, 255, .65); }
.coord { position: absolute; font-weight: 700; font-size: calc(var(--bsize) / 8 * .19); line-height: 1; pointer-events: none; }
.coord.rank { top: 4%; left: 5%; }
.coord.file { bottom: 4%; right: 6%; }
.sq.light .coord { color: var(--sq-dark); }
.sq.dark .coord { color: var(--sq-light); }

.pieces { position: absolute; inset: 0; pointer-events: none; z-index: 2; }
.piece { position: absolute; top: 0; left: 0; width: 12.5%; height: 12.5%; background-size: 100% 100%; will-change: transform; }
.piece.anim { transition: transform .14s ease-out; }
.piece.dragging { z-index: 10; transition: none; scale: 1.06; }
.board.viewing-past .pieces { opacity: .97; }

.arrows { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 4; }

.promo { position: absolute; inset: 0; z-index: 20; background: rgba(0, 0, 0, .3); }
.promo-col { position: absolute; width: 12.5%; display: flex; background: #fff; border-radius: 4px; box-shadow: 0 4px 14px rgba(0, 0, 0, .5); overflow: hidden; }
.promo-col button { aspect-ratio: 1; border: 0; background: #fff center / 88% no-repeat; }
.promo-col button:hover { background-color: #e8e8e8; }
.promo-col .promo-x { aspect-ratio: auto; height: calc(var(--bsize) / 16); background: #f1f1f1; color: #8b8987; font-size: 13px; }

/* ---------- result modal ---------- */
.result { position: absolute; inset: 0; z-index: 30; display: grid; place-items: center; background: rgba(0, 0, 0, .35); }
.result[hidden] { display: none; }
.r-card { position: relative; width: min(300px, 82%); background: var(--panel); border-radius: 10px; overflow: hidden; box-shadow: 0 10px 40px rgba(0, 0, 0, .55); text-align: center; }
.r-head { padding: 20px 16px 16px; background: #3c3a37; }
.result.win .r-head { background: linear-gradient(#5b8a33, #3f6a22); }
.r-icon { width: 56px; height: 56px; margin: 0 auto 8px; border-radius: 8px; display: grid; place-items: center; font-size: 30px; }
.r-icon svg { width: 62%; height: 62%; }
.r-title { font-size: 26px; font-weight: 800; }
.r-reason { color: rgba(255, 255, 255, .7); font-size: 14px; }
.r-body { padding: 16px; display: grid; gap: 10px; }
.r-close { position: absolute; top: 6px; right: 8px; border: 0; background: none; color: rgba(255, 255, 255, .6); font-size: 16px; padding: 6px; }
.r-close:hover { color: #fff; }

/* ---------- side panel ---------- */
.panel {
  width: var(--panel-w);
  flex: none;
  height: calc(var(--bsize) + 2 * var(--bar));
  min-height: 520px;
  background: var(--panel);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.panel-title { display: flex; align-items: center; justify-content: center; gap: 8px; height: 52px; flex: none; font-size: 20px; font-weight: 800; background: var(--panel-2); }
.panel-title svg { width: 26px; height: 26px; color: var(--green); }
.panel-body { flex: 1; overflow-y: auto; padding: 16px; }
.panel-foot { flex: none; padding: 14px 16px 18px; background: var(--panel-2); display: grid; gap: 14px; }
.panel-foot.slim { padding: 8px 16px; text-align: center; background: none; }

.hero { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; }
.bubble { position: relative; flex: 1; background: #fff; color: #312e2b; border-radius: 8px; padding: 10px 12px; font-size: 14px; }
.bubble::before { content: ""; position: absolute; left: -7px; top: 18px; border: 7px solid transparent; border-left: 0; border-right-color: #fff; }
.bubble b { display: block; font-size: 15px; margin-bottom: 2px; }

.bot-group-title { color: var(--text-2); font-weight: 700; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; margin: 14px 0 8px; }
.bot-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.bot-card { display: flex; align-items: center; gap: 10px; text-align: left; padding: 8px; border: 0; border-radius: 6px; background: rgba(255, 255, 255, .06); box-shadow: inset 0 0 0 2px transparent; }
.bot-card:hover { background: rgba(255, 255, 255, .1); }
.bot-card.active { box-shadow: inset 0 0 0 2px var(--green); background: rgba(129, 182, 76, .12); }
.bot-meta { display: flex; flex-direction: column; min-width: 0; }
.bot-name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bot-sub { color: var(--muted); font-size: 12px; }

.color-pick { display: flex; justify-content: center; gap: 10px; }
.color-pick button { width: 52px; height: 52px; border-radius: 6px; border: 0; background: rgba(255, 255, 255, .08); display: grid; place-items: center; box-shadow: inset 0 0 0 2px transparent; }
.color-pick button img { width: 40px; height: 40px; }
.color-pick button span { font-size: 24px; font-weight: 800; color: var(--text-2); }
.color-pick button.active { box-shadow: inset 0 0 0 3px var(--green); }

.btn { border: 0; border-radius: 6px; padding: 10px 12px; font-weight: 700; color: var(--text-2); background: rgba(255, 255, 255, .08); box-shadow: 0 3px 0 rgba(0, 0, 0, .25); }
.btn:hover:not(:disabled) { background: rgba(255, 255, 255, .14); color: #fff; }
.btn:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0, 0, 0, .25); }
.btn.primary { background: var(--green); color: #fff; box-shadow: 0 4px 0 var(--green-sh); text-shadow: 0 1px 1px rgba(0, 0, 0, .25); }
.btn.primary:hover:not(:disabled) { background: var(--green-hi); }
.btn.big { font-size: 24px; font-weight: 800; padding: 14px; border-radius: 10px; box-shadow: 0 5px 0 var(--green-sh); }
.link { border: 0; background: none; color: var(--muted); font-weight: 600; padding: 4px; }
.link:hover { color: #fff; }

/* game view */
.bot-say { display: flex; align-items: flex-start; gap: 12px; padding: 14px 14px 12px; flex: none; }
.bot-say .bubble { min-height: 48px; }
.bot-say .bubble::before { top: 16px; }
#bubble-text { word-wrap: break-word; }
#bubble-text .err { color: #b33430; }
#bubble-text .link { color: #2f7ec0; padding: 0; }
#bubble-thoughts { margin-top: 6px; font-size: 12.5px; color: #5c5a57; }
#bubble-thoughts summary { cursor: pointer; font-weight: 600; color: #7c7a77; }
#bubble-thoughts div { margin-top: 4px; max-height: 140px; overflow-y: auto; white-space: pre-wrap; }
.dots { display: inline-flex; gap: 3px; vertical-align: middle; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: #8b8987; animation: blink 1.2s infinite; }
.dots i:nth-child(2) { animation-delay: .2s; }
.dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.moves { flex: 1; overflow-y: auto; border-top: 1px solid var(--line); font-size: 14px; }
.mrow { display: grid; grid-template-columns: 44px 1fr 1fr; align-items: center; height: 30px; padding-right: 8px; }
.mrow.alt { background: var(--row-alt); }
.mnum { color: var(--muted); padding-left: 14px; }
.san { justify-self: start; border: 0; background: none; padding: 2px 6px; border-radius: 3px; font-weight: 700; color: var(--text-2); border-bottom: 2px solid transparent; }
.san:hover { background: rgba(255, 255, 255, .08); }
.san.current { background: rgba(255, 255, 255, .18); color: #fff; border-bottom-color: rgba(255, 255, 255, .35); }
.san { display: inline-flex; align-items: center; }
.fig { width: 1.25em; height: 1.25em; margin: -2px 0 0 -2px; }
.bi { width: 14px; height: 14px; fill: currentColor; vertical-align: -2px; }
.mresult { text-align: center; padding: 10px; font-weight: 800; color: var(--text-2); }
.mempty { color: var(--muted); padding: 16px; text-align: center; }

.nav { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 8px 12px; flex: none; border-top: 1px solid var(--line); }
.icon { height: 38px; border: 0; border-radius: 6px; background: rgba(255, 255, 255, .08); display: grid; place-items: center; box-shadow: 0 3px 0 rgba(0, 0, 0, .25); }
.icon:hover:not(:disabled) { background: rgba(255, 255, 255, .14); }
.icon svg { width: 22px; height: 22px; fill: var(--text-2); }
.actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 4px 12px 8px; flex: none; }
.actions .btn { padding: 9px 4px; font-size: 13px; white-space: nowrap; }
#over-actions { grid-template-columns: 1fr 1fr; }
#over-actions[hidden], #game-actions[hidden] { display: none; }

.credits { text-align: center; color: #6c6a67; font-size: 12px; padding: 4px 16px 18px; }
.credits a { color: inherit; }

/* ---------- coach ---------- */
.cap { width: 18px; height: 18px; fill: var(--green); flex: none; }
.switch { position: relative; flex: none; width: 36px; height: 20px; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 10px; background: #4b4946; transition: background .15s; }
.switch span::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked + span { background: var(--green); }
.switch input:checked + span::after { transform: translateX(16px); }
.switch input:focus-visible + span { outline: 2px solid #fff; outline-offset: 2px; }

.coach-pick { display: flex; gap: 12px; align-items: flex-start; margin-top: 16px; padding: 12px; border-radius: 6px; background: rgba(255, 255, 255, .06); color: var(--text-2); font-size: 13px; cursor: pointer; }
.coach-pick b { display: flex; align-items: center; gap: 6px; color: #fff; font-size: 14px; margin-bottom: 2px; }
.coach-pick .switch { margin-top: 2px; }

.coach { flex: none; border-top: 1px solid var(--line); background: var(--panel-2); }
.coach-head { display: flex; align-items: center; gap: 8px; padding: 8px 14px; }
.coach-head > b { font-size: 14px; }
.coach-eval { flex: 1; display: flex; align-items: center; gap: 8px; min-width: 0; }
.coach-eval[hidden] { display: none; }
.coach-head .switch { margin-left: auto; }
.evalbar { flex: 1; height: 8px; border-radius: 4px; background: #403e3b; overflow: hidden; max-width: 160px; }
.evalbar i { display: block; height: 100%; width: 50%; background: #f0f0ee; transition: width .4s ease; }
.evalnum { font-size: 12px; font-weight: 700; color: var(--text-2); font-variant-numeric: tabular-nums; min-width: 34px; }
.coach-body { padding: 0 14px 10px; display: grid; gap: 6px; font-size: 13.5px; color: var(--text-2); max-height: 230px; overflow-y: auto; }
.coach-body:empty { display: none; }
.c-item { display: flex; gap: 8px; align-items: flex-start; padding: 6px 8px; border-radius: 5px; background: rgba(255, 255, 255, .04); border-left: 3px solid transparent; }
.c-item > .c-ico { flex: none; width: 18px; height: 18px; }
.c-ico svg { width: 18px; height: 18px; display: block; }
.c-opening { display: inline-flex; align-items: center; gap: 5px; align-self: center; }
.c-opening svg { width: 13px; height: 13px; }
.c-item b { color: #fff; }
.c-item.threat { border-left-color: #dc3c32; background: rgba(220, 60, 50, .1); }
.c-item.opp { border-left-color: #f7c631; background: rgba(247, 198, 49, .08); }
.c-item.tactic { border-left-color: var(--green); background: rgba(129, 182, 76, .1); }
.c-item.tip { border-left-color: #5d7a99; }
.c-meta { color: var(--muted); font-size: 12px; }
.c-try { margin-top: 5px; color: var(--text-2); }
.c-later { margin-top: 6px; }
.c-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.c-actions .btn { padding: 6px 10px; font-size: 12.5px; box-shadow: 0 2px 0 rgba(0, 0, 0, .25); }
.c-busy { color: var(--muted); display: flex; align-items: center; gap: 8px; padding: 4px 0; }
.c-busy .dots i { background: var(--muted); }
.grade { flex: none; display: inline-grid; place-items: center; min-width: 18px; height: 18px; padding: 0 3px; border-radius: 9px; color: #fff; font-size: 11px; font-weight: 800; line-height: 1; text-shadow: 0 1px 1px rgba(0, 0, 0, .3); }
.c-chips { display: flex; flex-wrap: wrap; gap: 6px 12px; margin: 6px 0 4px; }
.c-chip { display: inline-flex; align-items: center; gap: 5px; }
.coach .link.inline { color: var(--green-hi); padding: 0; font-size: 12.5px; }
.coach .link.inline:hover { color: #fff; text-decoration: underline; }
.san .grade { margin-left: 4px; min-width: 15px; height: 15px; font-size: 9.5px; }

/* ---------- small screens: board full width, panel underneath ---------- */
@media (max-width: 900px) {
  :root { --gap: 0px; --bsize: min(100vw, 640px); --bar: 44px; }
  .topbar { padding: 0 12px; }
  .layout { flex-direction: column; align-items: center; padding: 0 0 12px; gap: 10px; }
  .player { padding: 5px 10px; }
  .panel { width: min(100vw - 16px, 640px); height: auto; min-height: 0; }
  .moves { max-height: 180px; min-height: 90px; }
  .coach-body { max-height: none; }
  .panel { overflow: visible; }
  .panel-title { border-radius: 8px 8px 0 0; }
  .panel-body { overflow: visible; }
  /* Keep Play reachable while scrolling the bot list. */
  .panel-foot:not(.slim) { position: sticky; bottom: 0; border-radius: 0 0 8px 8px; box-shadow: 0 -8px 16px rgba(0, 0, 0, .35); z-index: 5; }
  .board { border-radius: 0; }
}
@media (max-width: 420px) {
  .bot-card { padding: 6px; gap: 8px; }
  .bot-card .avatar.lg { width: 38px; height: 38px; }
  .hero .avatar.xl { width: 64px; height: 64px; }
  .color-pick button { width: 46px; height: 46px; }
  .btn.big { font-size: 20px; padding: 11px; }
  .panel-foot:not(.slim) { padding: 10px 12px 12px; gap: 10px; grid-template-columns: auto 1fr; align-items: center; }
}
