:root {
    --bg: #14161a;
    --bg-2: #1b1e24;
    --panel: #20242b;
    --panel-2: #282d35;
    --line: #333945;
    --text: #e9ecf1;
    --muted: #98a1b0;
    --accent: #7bc043;
    --accent-2: #5da02d;
    --accent-ink: #0f1a06;
    --danger: #e5534b;
    --warn: #e0a93b;
    --sq-light: #ebecd0;
    --sq-dark: #739552;
    --sq-last: rgba(255, 214, 10, .42);
    --sq-sel: rgba(255, 214, 10, .55);
    --radius: 14px;
    --shadow: 0 10px 30px rgba(0, 0, 0, .35);
    --font: "Inter", "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); }
body { background: radial-gradient(1200px 600px at 80% -10%, #1f2a1a 0%, transparent 60%), var(--bg); min-height: 100dvh; }
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
input { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 12px 18px; border-bottom: 1px solid var(--line); background: rgba(20, 22, 26, .8);
    backdrop-filter: blur(10px); position: sticky; top: 0; z-index: 30;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 20px; letter-spacing: .2px; }
.brand .logo {
    width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; font-size: 22px;
    background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: var(--accent-ink);
}
.topbar-right { display: flex; align-items: center; gap: 10px; }
.conn { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.conn i { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); display: inline-block; }
.conn.on i { background: var(--accent); box-shadow: 0 0 8px var(--accent); }
.conn.off i { background: var(--danger); }
.icon-btn {
    width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center;
    background: var(--panel); border: 1px solid var(--line); font-size: 17px; transition: .15s;
}
.icon-btn:hover { background: var(--panel-2); }

/* ---------- buttons ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 12px 18px; border-radius: 12px;
    background: var(--panel-2); border: 1px solid var(--line); font-weight: 600; transition: .15s; min-height: 44px;
}
.btn:hover { background: #313743; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: linear-gradient(180deg, var(--accent), var(--accent-2)); color: var(--accent-ink); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { background: rgba(229, 83, 75, .12); border-color: rgba(229, 83, 75, .45); color: #ff8a82; }
.btn.danger:hover { background: rgba(229, 83, 75, .22); }
.btn.sm { padding: 8px 12px; min-height: 36px; font-size: 14px; border-radius: 10px; }
.btn[disabled] { opacity: .45; pointer-events: none; }

/* ---------- lobby ---------- */
.container { max-width: 1120px; margin: 0 auto; padding: 22px 16px 60px; }
.hero { display: grid; grid-template-columns: 1.1fr .9fr; gap: 22px; align-items: start; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.card h2 { margin: 0 0 4px; font-size: 18px; }
.card .sub { color: var(--muted); font-size: 14px; margin: 0 0 14px; }
.section-title { font-size: 12px; text-transform: uppercase; letter-spacing: .12em; color: var(--muted); margin: 16px 0 8px; }

.controls { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.ctl {
    background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; padding: 14px 6px; text-align: center;
    display: flex; flex-direction: column; gap: 2px; transition: .15s;
}
.ctl b { font-size: 20px; }
.ctl span { font-size: 12px; color: var(--muted); }
.ctl:hover { border-color: var(--accent); transform: translateY(-2px); }
.ctl.sel { border-color: var(--accent); background: rgba(123, 192, 67, .12); }

.name-row { display: flex; gap: 8px; align-items: center; }
.name-row input {
    flex: 1; min-width: 0; padding: 10px 12px; border-radius: 10px; background: var(--bg-2); border: 1px solid var(--line); outline: none;
}
.name-row input:focus { border-color: var(--accent); }

.color-pick { display: flex; gap: 8px; margin: 10px 0 14px; }
.color-pick button { flex: 1; padding: 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel-2); font-weight: 600; }
.color-pick button.sel { border-color: var(--accent); background: rgba(123, 192, 67, .12); }

.list { display: flex; flex-direction: column; gap: 8px; }
.row {
    display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border-radius: 10px;
    background: var(--panel-2); border: 1px solid var(--line); font-size: 14px;
}
.row .muted, .muted { color: var(--muted); }
.row .tag { font-weight: 700; }
.empty { color: var(--muted); font-size: 14px; padding: 8px 2px; }
.res { font-weight: 700; padding: 2px 8px; border-radius: 999px; font-size: 12px; }
.res.win { background: rgba(123, 192, 67, .18); color: #a6e06f; }
.res.loss { background: rgba(229, 83, 75, .18); color: #ff8a82; }
.res.draw { background: rgba(224, 169, 59, .18); color: #f1c46b; }
.banner {
    display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; padding: 14px 16px;
    border-radius: var(--radius); background: rgba(123, 192, 67, .12); border: 1px solid rgba(123, 192, 67, .5);
}

.overlay {
    position: fixed; inset: 0; background: rgba(8, 10, 12, .72); backdrop-filter: blur(4px); z-index: 60;
    display: grid; place-items: center; padding: 16px;
}
.modal {
    width: min(420px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 24px; text-align: center;
    box-shadow: var(--shadow);
}
.modal h3 { margin: 0 0 6px; font-size: 22px; }
.modal p { margin: 0 0 16px; color: var(--muted); }
.modal .btns { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.modal .btns .btn { flex: 1 1 140px; }
.spinner {
    width: 42px; height: 42px; border-radius: 50%; border: 4px solid var(--line); border-top-color: var(--accent); margin: 0 auto 14px;
    animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.linkbox {
    display: flex; gap: 8px; margin: 10px 0 14px;
}
.linkbox input { flex: 1; min-width: 0; padding: 10px; border-radius: 10px; background: var(--bg-2); border: 1px solid var(--line); font-size: 13px; }

/* ---------- game layout ---------- */
.game-wrap {
    --bs: min(calc(100vw - 16px), calc(100dvh - 330px));
    display: grid; gap: 14px; padding: 12px 8px 24px; justify-content: center; align-items: start;
}
.board-col { width: var(--bs); display: flex; flex-direction: column; gap: 8px; margin: 0 auto; }
.side-col { width: 100%; max-width: var(--bs); margin: 0 auto; display: flex; flex-direction: column; gap: 10px; min-height: 0; }

.pbar {
    display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 6px 4px;
}
.pinfo { display: flex; align-items: center; gap: 10px; min-width: 0; }
.avatar {
    width: 38px; height: 38px; border-radius: 9px; display: grid; place-items: center; font-weight: 800; font-size: 17px; flex: none;
    background: var(--panel-2); border: 1px solid var(--line);
}
.avatar.w { background: #f0f0f0; color: #222; }
.avatar.b { background: #2b2b2b; color: #eee; }
.pname { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; }
.pmeta { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); min-height: 18px; }
.caps { letter-spacing: -2px; font-size: 15px; }
.clock {
    font-variant-numeric: tabular-nums; font-weight: 800; font-size: 26px; padding: 4px 14px; border-radius: 10px; min-width: 96px; text-align: center;
    background: var(--panel); border: 1px solid var(--line); color: var(--muted); transition: background .2s, color .2s;
}
.clock.active { background: #f1f1e6; color: #1b1e24; border-color: #f1f1e6; }
.clock.low { background: var(--danger); color: #fff; border-color: var(--danger); }

.board-shell { display: flex; gap: 6px; }
.evalbar { width: 14px; border-radius: 6px; overflow: hidden; background: #222; position: relative; flex: none; border: 1px solid var(--line); }
.evalbar .w { position: absolute; left: 0; right: 0; bottom: 0; background: #eee; height: 50%; transition: height .35s; }
.evalbar span { position: absolute; left: 50%; transform: translateX(-50%); font-size: 9px; font-weight: 800; mix-blend-mode: difference; color: #fff; }
.evalbar .top { top: 2px; } .evalbar .bot { bottom: 2px; }

.board {
    position: relative; width: 100%; aspect-ratio: 1; border-radius: 8px; overflow: hidden; user-select: none; touch-action: none;
    box-shadow: var(--shadow); background: var(--sq-light); flex: 1;
}
.sq { position: absolute; width: 12.5%; height: 12.5%; }
.sq.l { background: var(--sq-light); }
.sq.d { background: var(--sq-dark); }
.sq .coord { position: absolute; font-size: clamp(8px, 1.6vw, 12px); font-weight: 700; line-height: 1; pointer-events: none; }
.sq.l .coord { color: var(--sq-dark); }
.sq.d .coord { color: var(--sq-light); }
.sq .coord.f { right: 3px; bottom: 2px; }
.sq .coord.r { left: 3px; top: 3px; }
.sq.last::before, .sq.sel::before, .sq.chk::before, .sq.pre::before { content: ""; position: absolute; inset: 0; }
.sq.last::before { background: var(--sq-last); }
.sq.pre::before { background: rgba(235, 97, 80, .62); }
.sq.sel::before { background: var(--sq-sel); }
.sq.chk::before { background: radial-gradient(circle, rgba(229, 20, 20, .95) 0%, rgba(229, 20, 20, .55) 45%, transparent 75%); }
.sq.dot::after { content: ""; position: absolute; width: 30%; height: 30%; left: 35%; top: 35%; border-radius: 50%; background: rgba(20, 30, 10, .32); }
.sq.cap::after { content: ""; position: absolute; inset: 4%; border-radius: 50%; border: 5px solid rgba(20, 30, 10, .32); }
.sq.hover { box-shadow: inset 0 0 0 4px rgba(255, 255, 255, .55); }
.piece {
    position: absolute; width: 12.5%; height: 12.5%; background-size: 92%; background-repeat: no-repeat; background-position: center;
    transition: transform .16s ease-out; will-change: transform; z-index: 3; pointer-events: none;
}
.piece.glyph { background: none; display: grid; place-items: center; font-size: calc(var(--bs) / 9); line-height: 1; }
.piece.glyph.w { color: #fff; text-shadow: 0 0 2px #000, 0 0 2px #000, 0 0 3px #000; }
.piece.glyph.b { color: #111; text-shadow: 0 0 2px #fff; }
.piece.drag { transition: none; z-index: 20; pointer-events: none; }
.board.flipped { }

.promo {
    position: absolute; z-index: 25; width: 12.5%; background: #fff; border-radius: 6px; box-shadow: 0 8px 24px rgba(0, 0, 0, .5);
    display: flex; flex-direction: column;
}
.promo button { width: 100%; aspect-ratio: 1; background-size: 90%; background-repeat: no-repeat; background-position: center; border-radius: 6px; }
.promo button:hover { background-color: #ffe9a8; }
.promo.glyphs button { font-size: calc(var(--bs) / 11); color: #111; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.actions .btn { flex: 1 1 auto; }
.draw-bar {
    display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border-radius: 12px;
    background: rgba(224, 169, 59, .12); border: 1px solid rgba(224, 169, 59, .5); font-size: 14px;
}
.status-line { text-align: center; color: var(--muted); font-size: 14px; min-height: 20px; }
.status-line.turn { color: var(--text); font-weight: 600; }

.tabs { display: flex; gap: 6px; }
.tab { flex: 1; padding: 9px; border-radius: 10px; background: var(--panel); border: 1px solid var(--line); font-weight: 600; font-size: 14px; position: relative; }
.tab.on { background: var(--panel-2); border-color: var(--accent); }
.badge { position: absolute; top: -6px; right: 8px; background: var(--danger); color: #fff; border-radius: 999px; font-size: 11px; padding: 1px 6px; }
.pane { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.moves { padding: 8px; overflow: auto; max-height: 190px; min-height: 90px; display: grid; grid-template-columns: 34px 1fr 1fr; gap: 2px 4px; align-content: start; font-size: 15px; }
.moves .n { color: var(--muted); padding: 5px 2px; text-align: right; font-size: 13px; }
.moves .mv { padding: 5px 8px; border-radius: 6px; font-weight: 600; text-align: left; }
.moves .mv:hover { background: var(--panel-2); }
.moves .mv.cur { background: var(--accent); color: var(--accent-ink); }
.nav { display: flex; border-top: 1px solid var(--line); }
.nav button { flex: 1; padding: 10px; font-size: 16px; }
.nav button:hover { background: var(--panel-2); }
.chat { display: flex; flex-direction: column; height: 230px; }
.chat-log { flex: 1; overflow: auto; padding: 10px; display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.chat-log .m b { margin-right: 6px; }
.chat-log .m.w b { color: #fff; } .chat-log .m.b b { color: #9aa4b5; }
.chat-form { display: flex; border-top: 1px solid var(--line); }
.chat-form input { flex: 1; padding: 11px 12px; background: transparent; border: 0; outline: none; min-width: 0; }
.chat-form button { padding: 0 16px; color: var(--accent); font-weight: 700; }
.tools { display: flex; align-items: center; justify-content: space-between; gap: 8px; color: var(--muted); font-size: 13px; padding: 0 2px; }
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.switch input { accent-color: var(--accent); width: 18px; height: 18px; }

.toast {
    position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: #0e1013; border: 1px solid var(--line);
    padding: 10px 16px; border-radius: 12px; z-index: 80; box-shadow: var(--shadow); max-width: 92vw; font-size: 14px;
}

/* tabs content on desktop shows both */
@media (min-width: 960px) {
    .game-wrap {
        --bs: min(calc(100dvh - 160px), calc(100vw - 480px));
        grid-template-columns: auto 380px; padding: 18px 18px 30px; gap: 22px;
    }
    .board-col { margin: 0; }
    .side-col { max-width: none; margin: 0; }
    .tabs { display: none; }
    .pane[data-pane] { display: flex !important; }
    .moves { max-height: calc(var(--bs) * .42); }
    .chat { height: calc(var(--bs) * .3); min-height: 190px; }
    .evalbar { width: 16px; }
}
@media (max-width: 959px) {
    .hero { grid-template-columns: 1fr; }
    .controls { grid-template-columns: repeat(3, 1fr); }
    .clock { font-size: 22px; min-width: 84px; }
    .evalbar { width: 10px; }
}
@media (max-width: 420px) {
    .controls { grid-template-columns: repeat(2, 1fr); }
    .brand span.t { font-size: 18px; }
}
