:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --text: #1c1e24;
  --muted: #6b7080;
  --border: #dddbd5;
  --accent: #1c1e24;
  --accent-text: #ffffff;
  --red: #d93a3f;
  --blue: #2b6fe0;
  --free-dot: #b9b6ad;
  --board: #fbfaf7;
  --hint: #1f9d63;
  --radius: 10px;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161b;
    --surface: #1d2027;
    --text: #e8e9ed;
    --muted: #9297a5;
    --border: #2e323c;
    --accent: #e8e9ed;
    --accent-text: #14161b;
    --red: #f0595e;
    --blue: #5b93f5;
    --free-dot: #4a4f5c;
    --board: #191c22;
    --hint: #3ecf8e;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
}

.brand {
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0.02em;
  color: var(--text);
  text-decoration: none;
}

nav { display: flex; gap: 4px; }

nav a {
  padding: 6px 12px;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
  font-weight: 500;
}

nav a[aria-current] { color: var(--text); background: var(--surface); box-shadow: inset 0 0 0 1px var(--border); }

/* ------------------------------------------------------------- layout */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 20px;
  max-width: 1180px;
  margin: 0 auto;
  padding: 20px;
  align-items: start;
}

.layout:has(.panel.wide) { grid-template-columns: minmax(0, 1fr) 420px; max-width: 1260px; }

.board-host {
  background: var(--board);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px;
  max-width: min(100%, calc(100vh - 100px));
  width: 100%;
  justify-self: center;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
}

@media (max-width: 860px) {
  .layout, .layout:has(.panel.wide) { grid-template-columns: minmax(0, 1fr); padding: 12px; gap: 12px; }
  .board-host { max-width: 100%; }
}

/* -------------------------------------------------------------- board */

.board { display: block; width: 100%; height: auto; touch-action: manipulation; user-select: none; }
.board.interactive { cursor: pointer; }
.board line { stroke-linecap: round; }

.goal.red { fill: var(--red); opacity: 0.55; }
.goal.blue { fill: var(--blue); opacity: 0.55; }
.coord { font-size: 0.34px; fill: var(--muted); text-anchor: middle; dominant-baseline: central; }

.dot.free { fill: var(--free-dot); }
.dot.red { fill: var(--red); }
.dot.blue { fill: var(--blue); }

.stroke { stroke-width: 0.17; }
.stroke.red { stroke: var(--red); }
.stroke.blue { stroke: var(--blue); }
.stroke.last { stroke-width: 0.26; }

.link { stroke-width: 0.06; stroke-dasharray: 0.02 0.14; opacity: 0.8; }
.link.red { stroke: var(--red); }
.link.blue { stroke: var(--blue); }

.winpath { fill: none; stroke-width: 0.5; stroke-linejoin: round; stroke-linecap: round; opacity: 0.25; }
.winpath.red { stroke: var(--red); }
.winpath.blue { stroke: var(--blue); }

.preview { stroke-width: 0.17; opacity: 0.45; pointer-events: none; }
.preview.red { stroke: var(--red); }
.preview.blue { stroke: var(--blue); }

.hint { stroke: var(--hint); stroke-width: 0.3; opacity: 0.3; }
.hint.rank1 { opacity: 0.6; }
.hint-badge { fill: var(--hint); }
.hint-label { font-size: 0.26px; font-weight: 700; fill: #fff; text-anchor: middle; dominant-baseline: central; }
.layer-hints, .layer-preview, .layer-labels { pointer-events: none; }

/* ----------------------------------------------------------- controls */

h2 { margin: 0; font-size: 17px; }
p { margin: 0; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.warning { color: var(--red); }

button, select, input[type='text'], textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 12px;
}

button { cursor: pointer; font-weight: 500; }
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.big { padding: 11px 16px; font-size: 16px; }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }

input[type='text'], select, textarea { width: 100%; min-width: 0; }
textarea { resize: vertical; font-family: ui-monospace, 'SF Mono', Menlo, monospace; font-size: 13px; }
input.code { text-transform: uppercase; letter-spacing: 0.15em; font-family: ui-monospace, Menlo, monospace; }

.field { display: flex; flex-direction: column; gap: 6px; font-weight: 500; font-size: 13px; color: var(--muted); }

.segmented { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented button { flex: 1; border: 0; border-radius: 0; padding: 7px 8px; color: var(--muted); }
.segmented button + button { border-left: 1px solid var(--border); }
.segmented button[aria-checked='true'] { background: var(--accent); color: var(--accent-text); }

.check { display: flex; gap: 10px; align-items: flex-start; cursor: pointer; }
.check input { margin-top: 3px; accent-color: var(--accent); }
.check span span { display: block; margin-top: 2px; }

.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--border); }

details.rules { border-top: 1px solid var(--border); padding-top: 12px; color: var(--muted); font-size: 14px; }
details.rules summary { cursor: pointer; color: var(--text); font-weight: 500; }
details.rules p { margin-top: 8px; }

/* --------------------------------------------------------------- game */

.players { display: flex; flex-direction: column; gap: 6px; }

.player {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.player.active { border-color: var(--text); box-shadow: inset 0 0 0 1px var(--text); }
.player-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-goal { color: var(--muted); font-size: 13px; margin-left: auto; white-space: nowrap; }
.swatch { width: 14px; height: 14px; border-radius: 50%; flex: none; }
.swatch.red { background: var(--red); }
.swatch.blue { background: var(--blue); }
.tag { font-size: 12px; padding: 1px 7px; border-radius: 99px; border: 1px solid var(--border); color: var(--muted); }
.tag.win { border-color: var(--hint); color: var(--hint); }

.status { font-weight: 600; min-height: 1.45em; }

.move-list {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 8px;
  max-height: 180px;
  overflow-y: auto;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: ui-monospace, 'SF Mono', Menlo, monospace;
  font-size: 13px;
}

.move-number { color: var(--muted); }
.move.red { color: var(--red); }
.move.blue { color: var(--blue); }
.share input { font-size: 13px; }

/* ----------------------------------------------------------- analysis */

.game-title { margin-top: 10px; }

.evalbar { height: 10px; border-radius: 99px; background: var(--blue); overflow: hidden; margin: 10px 0 6px; }
.evalbar-red { height: 100%; background: var(--red); transition: width 0.25s; }
.eval-label { font-size: 14px; }

.pvs { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }

.pv {
  display: flex;
  gap: 10px;
  align-items: baseline;
  text-align: left;
  padding: 5px 8px;
  font-family: ui-monospace, 'SF Mono', Menlo, monospace;
  font-size: 13px;
  font-weight: 400;
}

.rank { color: var(--hint); font-weight: 700; }
.pv-score { font-weight: 700; min-width: 3.2em; }
.pv-score.red { color: var(--red); }
.pv-score.blue { color: var(--blue); }
.pv-moves { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }

.tree { display: flex; flex-direction: column; gap: 10px; }

.tree-moves {
  max-height: 260px;
  overflow-y: auto;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: ui-monospace, 'SF Mono', Menlo, monospace;
  font-size: 13px;
  line-height: 1.9;
}

.tmove { padding: 2px 4px; border-radius: 5px; cursor: pointer; white-space: nowrap; }
.tmove:hover { background: var(--border); }
.tmove.current { background: var(--accent); color: var(--accent-text); }
.tmove.noted { text-decoration: underline dotted; }
.variation { color: var(--muted); }

.library { display: flex; flex-direction: column; gap: 6px; max-height: 60vh; overflow-y: auto; }
.library-row { display: flex; gap: 6px; }
.library-open { flex: 1; text-align: left; font-weight: 400; }

dialog {
  width: min(560px, calc(100vw - 32px));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 20px;
}

dialog[open] { display: flex; flex-direction: column; gap: 14px; }
dialog::backdrop { background: rgb(0 0 0 / 0.45); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  background: var(--accent);
  color: var(--accent-text);
  padding: 9px 16px;
  border-radius: 99px;
  font-weight: 500;
  z-index: 10;
  max-width: calc(100vw - 32px);
}
