:root {
  --bg: #0d0f14;
  --panel: #161a22;
  --panel-2: #1d222c;
  --border: #2a303c;
  --text: #eef1f6;
  --dim: #9aa3b2;
  --accent: #ff4d5e;
  --gold: #f5c451;
  --good: #3ddc97;
  --light-sq: #e9dcc1;
  --dark-sq: #a9805b;
  --last: rgba(245, 196, 81, .45);
  --check: rgba(255, 77, 94, .75);
  --radius: 14px;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
:root[data-theme="light"] {
  --bg: #f4f2ee; --panel: #ffffff; --panel-2: #f1ede6; --border: #ddd6ca; --text: #1b1d22; --dim: #5d6370;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) { --bg: #f4f2ee; --panel: #ffffff; --panel-2: #f1ede6; --border: #ddd6ca; --text: #1b1d22; --dim: #5d6370; }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font-family: var(--sans); min-height: 100vh; display: flex; flex-direction: column; }

.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 20px; border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; }
.live-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--dim); }
.live-dot.on { background: var(--accent); box-shadow: 0 0 0 0 rgba(255, 77, 94, .7); animation: pulse 1.6s infinite; }
.live-word { font-size: 12px; letter-spacing: .14em; color: var(--dim); }
.live-dot.on + .live-word { color: var(--accent); }
.title { font-size: 18px; letter-spacing: .02em; }
.top-meta { display: flex; gap: 14px; color: var(--dim); font-size: 14px; }
@keyframes pulse { 70% { box-shadow: 0 0 0 10px rgba(255, 77, 94, 0); } 100% { box-shadow: 0 0 0 0 rgba(255, 77, 94, 0); } }

.stage { flex: 1; display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 24px; padding: 20px; max-width: 1240px; width: 100%; margin: 0 auto; }
.board-column { display: flex; flex-direction: column; gap: 12px; align-items: center; min-width: 0; }

/* players */
.player { width: 100%; max-width: calc(var(--board) + 44px); display: flex; align-items: center; gap: 12px; padding: 10px 14px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); position: relative; transition: border-color .2s, box-shadow .2s; }
.player.to-move { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold) inset; }
.player.winner { border-color: var(--good); box-shadow: 0 0 24px rgba(61, 220, 151, .25); }
.avatar { width: 40px; height: 40px; flex: none; border-radius: 50%; display: grid; place-items: center; font-size: 22px; }
.avatar.white { background: #f6f3ec; color: #1b1d22; }
.avatar.black { background: #1b1d22; color: #f6f3ec; border: 1px solid var(--border); }
.who { min-width: 0; flex: 1; }
.name { font-weight: 700; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model { font-size: 12px; color: var(--dim); }
.strikes { color: var(--accent); font-weight: 800; font-size: 13px; letter-spacing: .1em; }
.captured { font-size: 18px; letter-spacing: -.1em; color: var(--dim); min-height: 22px; }
.thinking { font-size: 12px; color: var(--gold); font-weight: 700; }
.thinking span { animation: blink 1.2s infinite; }
.thinking span:nth-child(2) { animation-delay: .2s; }
.thinking span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 100% { opacity: .2; } 50% { opacity: 1; } }
.bubble { position: relative; flex: 0 1 46%; min-width: 0; padding: 8px 12px; border-radius: 12px; background: var(--panel-2); border: 1px solid var(--border); font-size: 14px; line-height: 1.35; }
.bubble::before { content: ""; position: absolute; left: -7px; top: 50%; width: 12px; height: 12px; background: var(--panel-2); border-left: 1px solid var(--border); border-bottom: 1px solid var(--border); transform: translateY(-50%) rotate(45deg); }
.bubble.pop { animation: pop .35s ease-out; }
@keyframes pop { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* board */
.board-row { display: flex; gap: 10px; align-items: stretch; }
.board-wrap { position: relative; width: var(--board); height: var(--board); }
.board { position: relative; width: 100%; height: 100%; border-radius: 6px; overflow: hidden; box-shadow: 0 20px 50px rgba(0, 0, 0, .35); }
.sq { position: absolute; width: 12.5%; height: 12.5%; }
.sq.light { background: var(--light-sq); }
.sq.dark { background: var(--dark-sq); }
.sq.last::after { content: ""; position: absolute; inset: 0; background: var(--last); }
.sq.check::after { content: ""; position: absolute; inset: 0; background: radial-gradient(circle, var(--check) 0%, rgba(255, 77, 94, .2) 70%, transparent 75%); }
.coord { position: absolute; font-size: 11px; font-weight: 700; opacity: .75; pointer-events: none; }
.coord.file { right: 3px; bottom: 1px; }
.coord.rank { left: 3px; top: 1px; }
.sq.light .coord { color: var(--dark-sq); }
.sq.dark .coord { color: var(--light-sq); }
.piece { position: absolute; width: 12.5%; height: 12.5%; display: grid; place-items: center; font-size: calc(var(--board) / 8 * .8); line-height: 1; transition: transform .35s cubic-bezier(.2, .8, .2, 1); z-index: 2; user-select: none; }
.piece.w { color: #fbfaf6; -webkit-text-stroke: 1.2px #2a2a2a; text-shadow: 0 2px 2px rgba(0, 0, 0, .35); }
.piece.b { color: #1f1f1f; -webkit-text-stroke: .6px #000; text-shadow: 0 1px 0 rgba(255, 255, 255, .25), 0 2px 3px rgba(0, 0, 0, .35); }
.piece.moving { z-index: 4; }
.piece.gone { animation: gone .3s forwards; }
@keyframes gone { to { opacity: 0; transform: var(--at) scale(.6); } }

/* eval bar */
.eval { position: relative; width: 26px; border-radius: 6px; overflow: hidden; background: #2b2b2b; border: 1px solid var(--border); }
.eval-white { position: absolute; left: 0; right: 0; bottom: 0; height: 50%; background: #f3f0e8; transition: height .6s ease; }
.eval-label { position: absolute; left: 0; right: 0; text-align: center; font-size: 10px; font-weight: 800; }
.eval.white-ahead .eval-label { bottom: 4px; color: #1b1d22; }
.eval:not(.white-ahead) .eval-label { top: 4px; color: #f3f0e8; }

/* overlay */
.overlay, .idle { position: absolute; inset: 0; display: grid; place-items: center; z-index: 10; background: rgba(8, 10, 14, .62); border-radius: 6px; backdrop-filter: blur(2px); text-align: center; padding: 20px; }
.overlay[hidden], .idle[hidden] { display: none; }
.overlay-card { animation: pop .5s ease-out; }
.overlay-kicker { margin: 0; color: var(--gold); letter-spacing: .2em; font-size: 13px; font-weight: 800; text-transform: uppercase; }
.overlay-title { margin: 8px 0; font-size: clamp(26px, 5vw, 44px); font-weight: 900; color: #fff; }
.overlay-reason { margin: 0; color: #dfe4ec; font-size: 16px; }
.overlay-score { margin: 10px 0 0; font-size: 22px; font-weight: 800; color: #fff; }
.idle-title { margin: 0 0 6px; font-size: 24px; font-weight: 800; color: #fff; }
.idle-text { margin: 0; color: #dfe4ec; }

/* side */
.side { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.panel h2 { margin: 0 0 10px; font-size: 13px; letter-spacing: .12em; text-transform: uppercase; color: var(--dim); }
.bill-total { margin: 0; display: flex; align-items: baseline; gap: 8px; }
#bill-total { font-size: 30px; font-weight: 900; font-variant-numeric: tabular-nums; color: var(--good); }
.bill-sub { color: var(--dim); font-size: 13px; }
.bill-split { margin: 6px 0 0; color: var(--dim); font-size: 13px; font-variant-numeric: tabular-nums; }
.moves { margin: 0; padding: 0; list-style: none; max-height: 300px; overflow-y: auto; display: grid; grid-template-columns: 36px 1fr 1fr; gap: 2px 8px; font-variant-numeric: tabular-nums; font-size: 15px; }
.moves .num { color: var(--dim); }
.moves .mv.latest { background: var(--panel-2); border-radius: 4px; font-weight: 700; }
.q-blunder { color: var(--accent); font-weight: 800; }
.q-mistake { color: #ff9f43; font-weight: 800; }
.events { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 8px; max-height: 260px; overflow-y: auto; font-size: 14px; }
.events li { display: flex; gap: 8px; align-items: baseline; }
.tag { flex: none; font-size: 10px; font-weight: 800; letter-spacing: .08em; padding: 2px 6px; border-radius: 4px; text-transform: uppercase; background: var(--panel-2); color: var(--dim); }
.tag.illegal, .tag.blunder { background: rgba(255, 77, 94, .15); color: var(--accent); }
.tag.trouble { background: rgba(255, 159, 67, .15); color: #ff9f43; }
.tag.end { background: rgba(61, 220, 151, .15); color: var(--good); }

button { font: inherit; font-weight: 700; padding: 10px 16px; border-radius: 999px; border: 0; background: var(--accent); color: #fff; cursor: pointer; min-height: 44px; }
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }

.sound { min-height: 36px; padding: 6px 14px; font-size: 14px; background: var(--panel-2); color: var(--text); border: 1px solid var(--border); }
.sound[aria-pressed="true"] { background: var(--good); color: #08130e; border-color: transparent; }
.top-meta { position: relative; align-items: center; }
#volume-toggle { min-width: 44px; padding: 6px 10px; }
.volume { position: absolute; right: 0; top: calc(100% + 8px); z-index: 20; display: flex; flex-direction: column; gap: 10px; padding: 14px 16px; min-width: 220px; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 12px 30px rgba(0, 0, 0, .3); }
.volume[hidden] { display: none; }
.volume label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--dim); }
.volume input { width: 100%; min-height: 32px; }
.sound-hint { font-size: 13px; font-weight: 700; color: var(--gold); animation: blink 1.6s infinite; }
.sound-hint[hidden] { display: none; }
.caption { width: 100%; max-width: calc(var(--board) + 44px); margin: 0; padding: 12px 16px; border-radius: var(--radius); background: var(--panel); border: 1px solid var(--border); border-left: 3px solid var(--gold); font-size: 17px; line-height: 1.4; }
.caption.pop { animation: pop .35s ease-out; }
.caption-who { font-weight: 800; color: var(--gold); }
.foot { text-align: center; color: var(--dim); font-size: 12px; padding: 16px; }

@media (max-width: 900px) {
  .stage { grid-template-columns: minmax(0, 1fr); padding: 12px 16px; }
  .top { padding: 12px 16px; flex-wrap: wrap; }
  .top-meta { font-size: 13px; }
  .player { gap: 8px; padding: 8px 10px; }
  .avatar { width: 32px; height: 32px; font-size: 18px; }
  .bubble { max-width: 70%; }
}
