:root {
  --bg:      #111111;
  --surface: #1a1a1a;
  --border:  #333333;
  --text:    #e0e0e0;
  --muted:   #888888;
  --blue:    #888888;
  --red:     #f85149;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  color: var(--text);
  padding: 24px;
}

#layout {
  position: relative;
}

#sidebar {
  position: absolute;
  left: calc(100% + 32px);
  top: 0;
  width: 230px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#board-wrap {
  background: #1e1b18;
  border-radius: 14px;
  padding: 16px;
  overflow: hidden;
  box-shadow: 0 8px 40px rgba(0,0,0,0.7),
              0 2px 8px rgba(0,0,0,0.4);
}

canvas { display: block; }

/* ── card ── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
}
.card-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.4px;
  color: var(--muted);
  margin-bottom: 10px;
}

/* ── player rows ── */
.player-entry {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  transition: background 0.2s, box-shadow 0.2s;
}
.player-entry + .player-entry { margin-top: 4px; }

.player-entry.active-p1 {
  background: rgba(63,185,80,0.12);
  box-shadow: inset 0 0 0 1px rgba(63,185,80,0.35);
}
.player-entry.active-p2 {
  background: rgba(248,81,73,0.12);
  box-shadow: inset 0 0 0 1px rgba(248,81,73,0.35);
}

.pawn-dot {
  width: 11px; height: 11px;
  border-radius: 50%;
  flex-shrink: 0;
}
.pawn-dot.blue { background: #3fb950; box-shadow: 0 0 6px #3fb950; }
.pawn-dot.red  { background: var(--red);  box-shadow: 0 0 6px var(--red);  }

.player-name { font-size: 13px; font-weight: 600; flex: 1; }
.wall-count  { font-size: 12px; color: var(--muted); }

/* No status text — turn/agent are in the Players card, search progress is
   the bar at the bottom of it, the result is drawn over the board. */

/* ── button ── */
button {
  width: 100%;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background 0.15s, border-color 0.15s;
}
button:hover { background: var(--surface); border-color: var(--muted); }
button.toggle-active { background: rgba(255,255,255,0.07); border-color: var(--muted); }

/* ── agent select ── */
.agent-select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  /* right padding clears the arrow — checkpoint labels are long enough that
     without it the text runs underneath it */
  padding: 8px 28px 8px 10px;
  border-radius: 8px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  margin-bottom: 8px;
  appearance: none;
  text-overflow: ellipsis;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%23888' d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  transition: border-color 0.15s;
}
.agent-select:hover { border-color: var(--muted); }
.agent-select:focus { outline: none; border-color: var(--muted); }
.agent-select option:disabled { color: var(--muted); }

/* .agent-select:focus above kills the default outline, so put one back for
   keyboard users. */
button:focus-visible,
select:focus-visible,
input:focus-visible {
  outline: 2px solid #8ab4ff;
  outline-offset: 2px;
}

/* Dark scrollbars for the panels that scroll. */
#analysis-panel, #nn-panel, .move-bar-list {
  scrollbar-width: thin;
  scrollbar-color: #3d3d3d transparent;
}
#analysis-panel::-webkit-scrollbar,
#nn-panel::-webkit-scrollbar,
.move-bar-list::-webkit-scrollbar { width: 9px; height: 9px; }
#analysis-panel::-webkit-scrollbar-track,
#nn-panel::-webkit-scrollbar-track,
.move-bar-list::-webkit-scrollbar-track { background: transparent; }
#analysis-panel::-webkit-scrollbar-thumb,
#nn-panel::-webkit-scrollbar-thumb,
.move-bar-list::-webkit-scrollbar-thumb {
  background: #3d3d3d;
  border-radius: 5px;
}
#analysis-panel::-webkit-scrollbar-thumb:hover,
#nn-panel::-webkit-scrollbar-thumb:hover,
.move-bar-list::-webkit-scrollbar-thumb:hover { background: #4f4f4f; }

/* ── mode selector ── */
.mode-btn-group { display: flex; gap: 6px; }
/* Four modes don't fit one row in the sidebar — lay them out 2x2.
   The Board card still uses .mode-btn-group (only two buttons). */
.mode-btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.mode-btn, .board-btn { flex: 1; padding: 7px 4px; font-size: 11px; font-weight: 600; letter-spacing: 0.2px; }
.mode-btn.active, .board-btn.active { background: rgba(255,255,255,0.07); border-color: var(--muted); }

/* ── thinking animation ──
   On the searching side's own dot in the Players card, so "who is thinking"
   and "who is to move" are the same piece of UI instead of two. */
@keyframes thinking-pulse { 0%,100%{opacity:1} 50%{opacity:0.25} }
.player-entry.thinking .pawn-dot { animation: thinking-pulse 1.1s ease-in-out infinite; }

/* ── search progress (finished MCTS simulations) ──
   Always in the layout, empty when idle: toggling it would shift every card
   below it each time a search starts. */
.sim-progress {
  height: 4px;
  margin-top: 8px;
  border-radius: 2px;
  background: rgba(255,255,255,0.07);
  overflow: hidden;
}
.sim-progress > div {
  height: 100%;
  width: 0;
  border-radius: 2px;
  background: var(--muted);
  transition: width 0.12s linear;
}
/* the analysis panel accents in red (sliders, checkboxes) — match it */
#analysis-progress > div { background: var(--red); }

/* ── board column (board + nn panel stacked) ── */
#board-col {
  display: flex;
  flex-direction: column;
  gap: 16px;
  align-items: center;
}

/* Board and its action row grouped so the row is exactly the board's width
   and not the column's — the NN panel below can be wider than the board. */
#board-stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
#board-actions { display: flex; gap: 6px; flex-wrap: wrap; }
#board-actions button {
  width: auto;
  /* basis small enough that all four stay on one row at 7x7 (546px) and
     9x9 (690px), but they wrap rather than crush if the board gets smaller */
  flex: 1 1 100px;
  padding: 9px 6px;
  font-size: 12px;
  white-space: nowrap;
}

/* ── timeline (game-history scrubber) ──
   Lives inside #board-stack so it is exactly the board's width — anything
   in flow here that is WIDER than the board shoves the sidebar sideways
   (see the note on #nn-panel below). */
#timeline-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
}
#timeline-row button {
  width: auto;
  flex: 0 0 auto;
  padding: 5px 9px;
  font-size: 12px;
  white-space: nowrap;
}
#timeline-row button:disabled { opacity: 0.35; cursor: default; }
#timeline-row button:disabled:hover { background: transparent; border-color: var(--border); }
#timeline-slider {
  flex: 1 1 auto;
  min-width: 0;              /* allow it to shrink inside the flex row */
  accent-color: var(--muted);
  cursor: pointer;
}
#timeline-slider:disabled { cursor: default; opacity: 0.4; }
#timeline-label {
  flex: 0 0 auto;
  min-width: 96px;
  text-align: right;
  font-family: 'Consolas', 'SF Mono', monospace;
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
/* Parked on an earlier position. */
#timeline-label.reviewing { color: #d9a05b; }

/* ── NN channel viewer ── */
/* Absolute so its width never feeds back into #layout: the sidebar is
   positioned at left: calc(100% + 32px) of #layout, so an in-flow panel
   wider than the board (the numbered channels are 28px/cell) used to shove
   the sidebar sideways every time this opened. Out of flow, it just unrolls
   underneath everything and nothing else moves. `top` is refined in JS to
   clear whichever side panel is currently tallest. */
#nn-panel {
  display: none;
  position: absolute;
  top: calc(100% + 16px);
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100vw - 48px);
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px 24px;
}
#nn-panel.open { display: block; }
#nn-panel-inner {}
#nn-panel h2 {
  font-size: 15px; font-weight: 700;
  margin-bottom: 16px; color: var(--text);
}
.nn-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
}
.nn-channel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
}
.nn-channel-name {
  font-size: 10px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 1px;
  color: var(--muted); margin-bottom: 8px;
}
.nn-channel canvas { display: block; }

/* ── Analysis panel ── */
#analysis-panel {
  display: none;
  position: absolute;
  right: calc(100% + 24px);
  top: 0;
  width: 340px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px 24px;
}
#analysis-panel.open { display: block; }
/* Not enough room in the left rail: stack it under the board like the NN
   panel. body centres its content, so an overflowing child is clipped with
   no scrollbar rather than reachable. */
#analysis-panel.stacked {
  right: auto;
  left: 50%;
  top: calc(100% + 16px);
  transform: translateX(-50%);
  max-height: none;
}
#analysis-panel h2 { font-size: 15px; font-weight: 700; margin-bottom: 16px; }
.analysis-section { margin-bottom: 18px; }
.analysis-section-title {
  font-size: 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 1.4px; color: var(--muted); margin-bottom: 8px;
}
.win-bar-track {
  height: 16px; border-radius: 8px; background: var(--bg);
  overflow: hidden; display: flex; margin-bottom: 6px;
}
.win-bar-p1 { height: 100%; background: #3fb950; transition: width 0.3s; min-width: 2px; }
.win-bar-p2 { height: 100%; background: #f85149; flex: 1; }
.win-bar-labels { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); }
.move-bar-list { display: flex; flex-direction: column; gap: 4px; max-height: 340px; overflow-y: auto; }
.move-bar-row { display: grid; grid-template-columns: 72px 1fr 78px; gap: 6px; align-items: center; cursor: pointer; padding: 2px 4px; border-radius: 4px; transition: background 0.15s; }
.move-bar-row:hover { background: rgba(255,255,255,0.06); }
.move-bar-label {
  font-family: 'Consolas', 'SF Mono', monospace; font-size: 11px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.move-bar-label.pawn { color: #3fb950; }
.move-bar-label.wall { color: #d97f2e; }
.move-bar-track { height: 14px; border-radius: 4px; background: var(--bg); position: relative; }
.move-bar-nn {
  position: absolute; top: 0; left: 0; height: 8px; border-radius: 2px;
  background: rgba(63,185,80,0.75); transition: width 0.3s;
}
.move-bar-mcts {
  position: absolute; bottom: 0; left: 0; height: 6px; border-radius: 2px;
  background: rgba(248,81,73,0.85); transition: width 0.3s;
}
.move-bar-pct { text-align: right; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.mcts-sims-row { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.mcts-sims-row span { font-size: 11px; color: var(--muted); flex: 1; }
#analysis-sims-display { font-size: 12px; font-weight: 600; }
/* while running, the progress bar below shows activity, so just dim the
   button rather than also pulsing it */
#btn-run-mcts.running { opacity: 0.55; pointer-events: none; }

#site-footer { padding: 2px 4px 0; font-size: 11px; line-height: 1.55; color: var(--muted); }
#site-footer h1 { font-size: 13px; font-weight: 600; color: var(--text); }
#site-footer p { margin-top: 5px; }
#site-footer a { color: var(--text); text-decoration: none; border-bottom: 1px solid var(--border); }
#site-footer a:hover { color: #fff; border-bottom-color: var(--muted); }

/* ── Phone shell ──
   The desktop layout cannot be squeezed: the 9x9 board alone is
   9*72-10+20 = 658 logical px, and #sidebar hangs off the board's right edge at
   left: calc(100% + 32px). So below the breakpoint the page becomes a fixed
   three-row column (top bar / board / tab bar), the board scales to fit
   (boardFit in app.js) and everything from the right rail moves into sheets.
   These four selectors are the shell; they stay out of the way until then. */
#m-topbar, #m-players, #m-tabbar, .m-sheet { display: none; }

@media (max-width: 900px) {
  body {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    padding: 0;
    height: 100vh;
    height: 100dvh;   /* the game view must not resize when Safari's bar hides */
    min-height: 0;
    overflow: hidden;
    overscroll-behavior: none;   /* no pull-to-refresh while dragging a wall */
    -webkit-tap-highlight-color: transparent;
  }

  #m-topbar {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    gap: 10px;
    padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
    border-bottom: 1px solid var(--border);
    font-size: 12px;
    color: var(--muted);
  }
  #m-topbar-variant { font-weight: 700; color: var(--text); }
  #m-topbar-mode { flex: 1; }
  #m-topbar-status { font-size: 11px; }

  /* #layout is only a positioning anchor for the side panels on desktop; here
     it is just the middle row that the board gets to fill. */
  #layout { flex: 1 1 auto; min-height: 0; display: flex; }
  #sidebar { display: none; }
  /* min-width: 0 because this one IS a row-flex item: without it its automatic
     minimum is the 658px canvas and the column can't narrow past it. */
  #board-col { flex: 1 1 auto; min-width: 0; min-height: 0; width: 100%; gap: 8px; }
  /* A square board on a narrow screen is width-bound, so there is spare height
     no matter what. Rather than ring the board with it, it all goes into one gap
     (see #timeline-row below): board and players on top, controls on the bottom
     edge where a thumb reaches them. */
  #board-stack {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    gap: 8px;
    padding: 8px;
    justify-content: flex-start;
  }

  /* Hugs the board instead of filling the column, so the panel doesn't show a
     field of empty felt above and below it. boardFit() measures #board-stack. */
  #board-wrap {
    flex: 0 0 auto;
    align-self: center;
    padding: 8px;
    border-radius: 10px;
  }
  /* The play view does not scroll, so the board can own every gesture — no
     competing scroll to cancel when a long press starts. */
  #board { touch-action: none; }
  /* iOS answers a long press with the selection loupe and the callout menu,
     which is exactly the gesture that places a wall. Killing selection over the
     whole play area is what stops it; the sheets keep theirs, so the footer
     link is still selectable. */
  #layout, #board, #m-players, #timeline-row, #board-actions {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
  }

  /* The spare height a width-bound board leaves over goes into touch targets
     rather than padding — every row here is something you tap. */
  #m-players { display: flex; }
  #m-players .card { flex: 1; padding: 10px 12px; }
  #m-players .card-label { display: none; }   /* two pawn rows need no heading */
  #m-players .player-entry { padding: 11px 10px; }

  #timeline-row { padding: 8px 10px; gap: 8px; margin-top: auto; }   /* takes the slack */
  #timeline-row button { padding: 11px 14px; }
  #timeline-slider { height: 28px; }
  #timeline-label { min-width: 0; font-size: 10px; }
  #board-actions button { padding: 14px 8px; font-size: 13px; }
  /* These two are tabs down in the bar instead. */
  #btn-nn, #btn-analysis { display: none; }

  #m-tabbar {
    display: flex;
    flex: 0 0 auto;
    position: relative;   /* so z-index applies and it stays above the sheets */
    z-index: 60;
    border-top: 1px solid var(--border);
    background: var(--surface);
    padding-bottom: env(safe-area-inset-bottom);
  }
  #m-tabbar button {
    flex: 1 1 0;
    border: 0;
    border-radius: 0;
    padding: 12px 4px;
    font-size: 11px;
    font-weight: 600;
    color: var(--muted);
    touch-action: manipulation;   /* no double-tap-to-zoom delay */
  }
  #m-tabbar button:hover { background: transparent; }
  #m-tabbar button.active { color: var(--text); box-shadow: inset 0 2px 0 var(--text); }

  .m-sheet {
    position: fixed;
    inset: 0;
    z-index: 50;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    background: var(--bg);
    padding: calc(env(safe-area-inset-top) + 14px) 14px
             calc(env(safe-area-inset-bottom) + 76px);   /* clear the tab bar */
  }
  .m-sheet.open { display: flex; flex-direction: column; gap: 12px; }

  /* Both side panels stop being panels and become plain sheet content. Their
     own `open` class still gates visibility, which is also what tells
     applyState() whether to keep refreshing them. */
  #analysis-panel, #nn-panel {
    position: static;
    transform: none;
    width: 100%;
    max-width: none;
    max-height: none;
    overflow: visible;
    border: 0;
    border-radius: 0;
    padding: 0;
  }
  #analysis-panel.stacked { left: auto; right: auto; top: auto; }
  .nn-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  /* renderChannel() writes a desktop pixel size inline (252px at 9x9), which two
     columns of a 390px screen can't take. Only !important beats an inline style;
     the backing store is already dpr-scaled, so downscaling it stays sharp. */
  .nn-channel canvas { width: 100% !important; height: auto !important; }
  .move-bar-row { grid-template-columns: 56px 1fr 46px; }
  .move-bar-list { max-height: none; }
}
