:root {
  --bg: #eceff4;
  --surface: #ffffff;
  --surface-2: #f5f7fa;
  --ink: #1b2430;
  --ink-soft: #5c6672;
  --line: #dde3ea;
  --accent: #1f6feb;
  --accent-ink: #ffffff;
  --success: #1f9d63;
  --danger: #e0484d;
  --warn: #c9820a;
  --canvas: #f7f8fa;
  --canvas-dot: #dbe1e9;
  --edge: #aab3bf;
  --edge-ink: #6b7580;
  --shadow: 0 1px 2px rgba(20, 28, 40, .08), 0 8px 24px rgba(20, 28, 40, .08);
  --radius: 12px;
  --radius-sm: 8px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1218;
    --surface: #161b22;
    --surface-2: #1b222c;
    --ink: #e6edf3;
    --ink-soft: #9aa7b3;
    --line: #2a323d;
    --accent: #4c8dff;
    --success: #35c07f;
    --danger: #f0595e;
    --warn: #e0a83d;
    --canvas: #0b0f15;
    --canvas-dot: #1a2430;
    --edge: #3d4854;
    --edge-ink: #8b97a3;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 10px 30px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0 0 .3em; }
p { line-height: 1.5; }

.wrap { max-width: 1180px; margin: 0 auto; padding: 24px 18px 60px; }
.wrap--narrow { max-width: 620px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 14px 20px; background: var(--surface);
  border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), #7c4cff); color: #fff; font-size: 18px;
}
.brand h1 { font-size: 17px; }
.brand p { margin: 0; font-size: 12px; color: var(--ink-soft); }
.topbar-nav { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 22px; box-shadow: var(--shadow);
}
.card + .card { margin-top: 18px; }
.card h2 { font-size: 18px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; color: var(--ink-soft); }
.field input[type=text], .field input[type=number], .field input[type=password],
.field input[type=file], .field select, .field textarea {
  width: 100%; padding: 9px 11px; border-radius: var(--radius-sm); border: 1px solid var(--line);
  background: var(--surface-2); color: var(--ink); font: inherit; font-size: 14px;
}
.field textarea { min-height: 140px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.field-hint { font-size: 12px; color: var(--ink-soft); margin-top: 4px; }
.field-row { display: flex; gap: 12px; flex-wrap: wrap; }
.field-row > * { flex: 1; min-width: 160px; }

.switch-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 0; border-bottom: 1px solid var(--line);
}
.switch-row:last-child { border-bottom: none; }
.switch-row .lbl { font-size: 14px; font-weight: 600; }
.switch-row .sub { font-size: 12px; color: var(--ink-soft); margin-top: 2px; }
.switch {
  position: relative; width: 42px; height: 24px; flex: none;
}
.switch input { opacity: 0; width: 100%; height: 100%; margin: 0; position: absolute; cursor: pointer; z-index: 1; }
.switch .track {
  position: absolute; inset: 0; background: var(--line); border-radius: 99px; transition: .15s;
}
.switch .knob {
  position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.3); transition: .15s;
}
.switch input:checked ~ .track { background: var(--accent); }
.switch input:checked ~ .knob { transform: translateX(18px); }

.btn {
  appearance: none; border: 1px solid var(--line); background: var(--surface-2); color: var(--ink);
  padding: 9px 16px; border-radius: 999px; font-size: 13.5px; font-weight: 600; cursor: pointer;
  transition: .12s; display: inline-flex; align-items: center; gap: 6px; text-decoration: none;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); color: #fff; }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-block { width: 100%; justify-content: center; }
.btn-sm { padding: 5px 11px; font-size: 12.5px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

.pill {
  display: inline-flex; align-items: center; gap: 5px; padding: 3px 10px; border-radius: 999px;
  font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
  background: var(--surface-2); color: var(--ink-soft); border: 1px solid var(--line);
}
.pill--lobby { color: var(--warn); border-color: var(--warn); }
.pill--active { color: var(--success); border-color: var(--success); }
.pill--finished { color: var(--ink-soft); }

.error-box {
  background: rgba(224,72,77,.1); border: 1px solid var(--danger); color: var(--danger);
  padding: 10px 13px; border-radius: var(--radius-sm); font-size: 13.5px; margin-bottom: 14px;
}
.info-box {
  background: rgba(31,111,235,.08); border: 1px solid var(--accent); color: var(--accent);
  padding: 10px 13px; border-radius: var(--radius-sm); font-size: 13.5px; margin-bottom: 14px;
}

table.simple { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.simple th, table.simple td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); }
table.simple th { font-size: 11.5px; text-transform: uppercase; letter-spacing: .03em; color: var(--ink-soft); }
table.simple tr:last-child td { border-bottom: none; }

.join-code-display {
  font-size: 40px; font-weight: 800; letter-spacing: .12em; text-align: center;
  background: var(--surface-2); border: 2px dashed var(--accent); border-radius: var(--radius);
  padding: 16px; color: var(--accent); font-family: ui-monospace, monospace;
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 800px) { .grid-2 { grid-template-columns: 1fr; } }

.board-pick {
  display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
}
.board-pick label {
  display: block; border: 2px solid var(--line); border-radius: var(--radius-sm); padding: 12px;
  cursor: pointer; font-weight: 600; font-size: 13.5px;
}
.board-pick input { display: none; }
.board-pick input:checked + span { color: var(--accent); }
.board-pick label:has(input:checked) { border-color: var(--accent); background: rgba(31,111,235,.06); }
.board-pick .meta { font-weight: 400; color: var(--ink-soft); font-size: 12px; margin-top: 4px; }

/* ---------- Play screen ---------- */
.play-shell { display: grid; grid-template-columns: 1fr 300px; height: 100vh; }
@media (max-width: 880px) { .play-shell { grid-template-columns: 1fr; height: auto; min-height: 100vh; } }

.play-main { position: relative; display: flex; flex-direction: column; min-width: 0; }
.play-topbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: var(--surface);
  border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.play-topbar .spacer { flex: 1; }
.chip {
  border: 1px solid var(--line); background: var(--surface-2); color: var(--ink); border-radius: 999px;
  padding: 6px 13px; font-size: 12.5px; font-weight: 600; cursor: pointer; display: inline-flex; gap: 6px; align-items: center;
}
.chip.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip--timer { font-family: ui-monospace, monospace; font-weight: 700; }
.chip--timer.low { color: var(--danger); border-color: var(--danger); }

.canvas-wrap { position: relative; flex: 1; overflow: hidden; background: var(--canvas); }
.canvas-wrap svg { width: 100%; height: 100%; display: block; touch-action: none; }
#three-container { position: absolute; inset: 0; }
#three-container canvas { display: block; }
.css2d-node {
  pointer-events: auto; display: flex; flex-direction: column; align-items: center; cursor: pointer;
  transform: translate(-50%, -50%); user-select: none;
}
.css2d-node .dot {
  width: 22px; height: 22px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4);
  background-size: cover; background-position: center;
}
.css2d-node .lbl {
  margin-top: 3px; font-size: 11px; font-weight: 700; color: #fff; background: rgba(0,0,0,.55);
  padding: 1px 6px; border-radius: 6px; white-space: nowrap; max-width: 140px; overflow: hidden; text-overflow: ellipsis;
}
.css2d-node.is-a { outline: 3px solid #fff; }
.css2d-node.is-found .dot { box-shadow: 0 0 0 3px var(--success); }

.node circle { stroke: var(--node-ring, #fff); stroke-width: 2.5px; cursor: pointer; }
.node text { font-size: 11px; font-weight: 700; fill: var(--ink); pointer-events: none; }
.node.is-picked circle { stroke: var(--accent); stroke-width: 4px; }
.node.is-found circle { filter: drop-shadow(0 0 3px var(--success)); }
.link { stroke: var(--edge); stroke-width: 1.6px; }
.link.is-correct { stroke: var(--success); stroke-width: 2.4px; }
.link.is-claimed { stroke: var(--warn); stroke-width: 2px; stroke-dasharray: 2 3; }
.link.is-free { stroke: var(--accent); stroke-dasharray: 4 3; }
.link.is-hint { stroke: var(--warn); stroke-width: 2.4px; stroke-dasharray: 6 3; }
.node.is-hint circle { stroke: var(--warn); stroke-width: 4px; }
.css2d-node.is-hint .dot { outline: 3px solid var(--warn); }
.node.is-hidden { display: none; }
.node.is-nolabel text { display: none; }
.css2d-node.is-hidden { display: none; }
.css2d-node.is-nolabel .lbl { display: none; }
.link-label { font-size: 9.5px; fill: var(--edge-ink); pointer-events: none; }

.statusline {
  position: absolute; left: 14px; bottom: 12px; background: var(--surface); border: 1px solid var(--line);
  padding: 6px 12px; border-radius: 999px; font-size: 12.5px; color: var(--ink-soft); box-shadow: var(--shadow);
}
.legend { position: absolute; right: 14px; top: 14px; display: flex; flex-direction: column; gap: 5px; }
.legend .row { display: flex; align-items: center; gap: 6px; font-size: 11.5px; background: var(--surface);
  border: 1px solid var(--line); padding: 3px 9px; border-radius: 999px; }
.legend .dot { width: 9px; height: 9px; border-radius: 50%; }

.sidebar { background: var(--surface); border-left: 1px solid var(--line); overflow-y: auto; padding: 16px; }
.sidebar-toggle { display: none; }
@media (max-width: 880px) {
  .sidebar { border-left: none; border-top: 1px solid var(--line); padding: 0; }
  .sidebar-toggle {
    display: flex; align-items: center; justify-content: space-between; width: 100%;
    padding: 13px 16px; background: var(--surface); border: none; cursor: pointer;
    font-size: 13.5px; font-weight: 700; color: var(--ink); font-family: var(--font);
  }
  .sidebar-toggle-arrow { transition: transform .15s; color: var(--ink-soft); }
  .sidebar-toggle[aria-expanded="true"] .sidebar-toggle-arrow { transform: rotate(180deg); }
  .sidebar-body {
    padding: 0 16px 16px; max-height: 70vh; overflow-y: auto;
    transition: max-height .2s ease, padding .2s ease;
  }
  .sidebar-body.is-collapsed { max-height: 0; overflow: hidden; padding-top: 0; padding-bottom: 0; }
}
.sidebar h3 { font-size: 12.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-soft); margin: 18px 0 8px; }
.sidebar h3:first-child { margin-top: 0; }

.team-row {
  display: flex; align-items: center; gap: 8px; padding: 7px 9px; border-radius: var(--radius-sm);
  font-size: 13.5px; margin-bottom: 4px; background: var(--surface-2);
}
.team-row .swatch { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.team-row .name { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.team-row .score { font-weight: 800; font-variant-numeric: tabular-nums; }
.team-row.is-you { outline: 2px solid var(--accent); }
.team-row .rank { color: var(--ink-soft); font-size: 11px; width: 16px; }

.found-list { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.found-list li { padding: 5px 0; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; gap: 6px; }
.found-list li:last-child { border-bottom: none; }
.found-empty { color: var(--ink-soft); font-style: italic; }

.overlay {
  position: fixed; inset: 0; background: rgba(10, 14, 20, .55); display: grid; place-items: center;
  z-index: 50; padding: 20px;
}
.overlay-card {
  background: var(--surface); border-radius: var(--radius); padding: 28px; max-width: 480px; width: 100%;
  box-shadow: var(--shadow); text-align: center;
}
.overlay-card h2 { font-size: 22px; }
.team-pick-list { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin: 14px 0; }
.team-pick { border: 2px solid var(--line); border-radius: 999px; padding: 7px 16px; cursor: pointer; font-weight: 600; font-size: 13px; }
.team-pick.is-selected { border-color: var(--accent); color: var(--accent); }

.results-table { width: 100%; margin: 16px 0; }
.results-table tr.is-winner td { font-weight: 800; color: var(--success); }

.toast-stack { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 80; }
.toast {
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); border-radius: var(--radius-sm);
  padding: 10px 14px; font-size: 13.5px; min-width: 200px;
}
.toast.good { border-color: var(--success); color: var(--success); }
.toast.bad { border-color: var(--danger); color: var(--danger); }

.node-editor-row {
  display: grid; grid-template-columns: 1.2fr 1.4fr 1fr 1.6fr auto; gap: 8px; align-items: center; margin-bottom: 6px;
}
.edge-editor-row {
  display: grid; grid-template-columns: 1fr 1fr 1fr 1fr auto; gap: 8px; align-items: center; margin-bottom: 6px;
}
@media (max-width: 720px) {
  .node-editor-row, .edge-editor-row { grid-template-columns: 1fr; }
}
.muted { color: var(--ink-soft); }
.tabs { display: flex; gap: 6px; margin-bottom: 16px; flex-wrap: wrap; }
.tabs .btn.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.samples { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0 16px; }
