:root {
  color-scheme: light dark;
  --bg: #f3f0ea;
  --panel: #ffffff;
  --panel-2: #f7f4ee;
  --text: #25221e;
  --muted: #7b756b;
  --line: #ddd7cc;
  --accent: #35679b;
  --accent-text: #ffffff;
  --good: #2c9a4c;
  --bad: #b13a30;
  --sq-light: #f0d9b5;
  --sq-dark: #b58863;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.22);
  --thumb: rgba(70, 60, 48, 0.28);
  --thumb-hover: rgba(70, 60, 48, 0.55);
  --list-w: 330px;
  --bs: clamp(280px, min(100vh - 150px, 100vw - 330px - 270px - 90px), 780px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1b19;
    --panel: #262421;
    --panel-2: #2d2a27;
    --text: #e8e4dc;
    --muted: #9b958a;
    --line: #3b3834;
    --accent: #6aa2da;
    --accent-text: #0f1923;
    --good: #4fc673;
    --bad: #ec6e63;
    --sq-light: #dcc9a2;
    --sq-dark: #9a7351;
    --shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
    --thumb: rgba(232, 228, 220, 0.24);
    --thumb-hover: rgba(232, 228, 220, 0.5);
  }
}

* { box-sizing: border-box; }
/* Floating scroll bars: a thin rounded thumb, no visible track or gutter, stronger on hover. */
* { scrollbar-width: thin; scrollbar-color: var(--thumb) transparent; }
*:hover { scrollbar-color: var(--thumb-hover) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; background: transparent; }
::-webkit-scrollbar-track, ::-webkit-scrollbar-track-piece, ::-webkit-scrollbar-corner { background: transparent; border: 0; box-shadow: none; }
::-webkit-scrollbar-button { display: none; width: 0; height: 0; }
::-webkit-scrollbar-thumb { background: var(--thumb); border-radius: 99px; border: 3px solid transparent; background-clip: padding-box; }
:hover::-webkit-scrollbar-thumb { background: var(--thumb-hover); background-clip: padding-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
.defs { position: absolute; }
a { color: var(--accent); }
button { font: inherit; color: inherit; }

.btn {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 14px;
  cursor: pointer;
  min-height: 36px;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn.nav { min-width: 46px; font-weight: 700; letter-spacing: 0.02em; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app {
  display: grid;
  grid-template-columns: var(--list-w) minmax(0, 1fr);
  grid-template-rows: auto 1fr auto;
  min-height: 100%;
}
.topbar { display: none; }

/* ---- list */
.list-panel {
  grid-row: 1 / 3;
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  max-height: 100vh;
  position: sticky;
  top: 0;
  align-self: start;
  height: 100vh;
}
.list-head {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.list-count { flex: 1; }
.list-count span { color: var(--muted); margin-left: 6px; }
.close-list { display: none; }
.save-note { padding: 8px 14px; color: var(--bad); font-size: 13px; }
#list h3 {
  margin: 18px 14px 6px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
#list ul { list-style: none; margin: 0; padding: 0 6px 4px; }
.item { display: flex; align-items: stretch; border-radius: 8px; }
.item.current { background: var(--panel-2); box-shadow: inset 3px 0 0 var(--accent); }
.item-main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  background: none;
  border: 0;
  padding: 8px 8px;
  cursor: pointer;
  border-radius: 8px;
}
.item-main:hover { background: var(--panel-2); }
.item .name { flex: 1; min-width: 0; line-height: 1.3; }
.item.done .name { color: var(--muted); }
.goal {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 99px;
  padding: 1px 8px;
}
.tick { flex: none; width: 22px; height: 22px; }
.tick svg { display: block; width: 22px; height: 22px; }
.tick .ring { fill: none; stroke: var(--line); stroke-width: 2; }
.tick .disc { fill: var(--good); }
.tick .mark { fill: none; stroke: #fff; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.item .clear { align-self: center; margin-right: 4px; }

/* ---- stage */
.stage {
  display: grid;
  grid-template-columns: var(--bs) minmax(220px, 270px);
  grid-template-rows: auto auto auto;
  column-gap: 28px;
  justify-content: center;
  align-content: start;
  padding: 22px 28px 8px;
}
.col-side { grid-column: 2; grid-row: 1; min-width: 0; align-self: start; }
.board-host { grid-column: 1; grid-row: 1; }
.controls { grid-column: 1; grid-row: 2; }
.moves { grid-column: 1; grid-row: 3; }
.info h1 { margin: 0 0 2px; font-size: 20px; line-height: 1.25; font-weight: 650; }
.info p { margin: 0 0 12px; color: var(--muted); }
.board-host { width: var(--bs); height: var(--bs); }
.board { display: block; width: 100%; height: 100%; touch-action: none; user-select: none; -webkit-user-select: none; border-radius: 4px; box-shadow: 0 2px 14px rgba(0, 0, 0, 0.18); }
.sq-light { fill: var(--sq-light); }
.sq-dark { fill: var(--sq-dark); }
.coord { font: 600 21px system-ui, sans-serif; opacity: 0.9; }
.coord.on-light { fill: var(--sq-dark); }
.coord.on-dark { fill: var(--sq-light); }
.hi-last { fill: rgba(155, 199, 0, 0.42); }
.hi-sel { fill: rgba(20, 110, 40, 0.5); }
.dot-move { fill: rgba(20, 85, 30, 0.5); }
.dot-capture { fill: none; stroke: rgba(20, 85, 30, 0.5); stroke-width: 10; }
.promo-dim { fill: rgba(0, 0, 0, 0.55); }
.promo-cell { fill: #f4eee2; stroke: #b9ae98; stroke-width: 2; cursor: pointer; }
.promo-cell:hover { fill: #ffe9b0; }

.controls { display: flex; align-items: center; margin-top: 12px; width: var(--bs); }
.controls .nav + .nav { margin-left: 8px; }
/* Restart sits centred in the space to the right of the four move buttons. */
.controls .restart { margin: 0 auto; min-width: 96px; }

.status { min-height: 24px; margin: 0 0 12px; font-weight: 600; }
.status.bad { color: var(--bad); }
.status.good { color: var(--good); }
.result {
  background: var(--panel);
  border: 1px solid var(--line);
  border-left: 4px solid var(--bad);
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 14px;
}
.result.good { border-left-color: var(--good); }
.result p { margin: 0 0 10px; }
.result p:last-child { margin: 0; }

.moves {
  list-style: none;
  margin: 0;
  padding: 0;
  width: min(var(--bs), 340px);
  max-height: 240px;
  margin-top: 14px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: 38px 1fr 1fr;
  align-content: start;
  gap: 2px 4px;
  font-variant-numeric: tabular-nums;
}
.moves .no { color: var(--muted); padding: 3px 4px; text-align: right; }
.moves button {
  background: none;
  border: 0;
  text-align: left;
  padding: 3px 8px;
  border-radius: 6px;
  cursor: pointer;
}
.moves button:hover { background: var(--panel-2); }
.moves button.now { background: var(--accent); color: var(--accent-text); }
.moves .blank { visibility: hidden; }

.credits {
  grid-column: 2;
  justify-self: end;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  text-align: right;
  gap: 2px;
  padding: 18px 16px 12px 20px;
  color: var(--muted);
  opacity: 0.85;
  font-size: 11px;
  line-height: 1.35;
}

/* ---- confetti, dialogs, toast */
.confetti { position: fixed; inset: 0; width: 100vw; height: 100vh; pointer-events: none; z-index: 60; }
.modal-back {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal {
  background: var(--panel);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 22px 24px;
  width: min(460px, 100%);
}
.modal h2 { margin: 0 0 6px; font-size: 20px; }
.modal p { margin: 0 0 16px; color: var(--muted); }
.modal .row { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
.modal .win-top { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 0; margin-bottom: 10px; }
.modal .win-actions { flex: 1 1 auto; display: flex; flex-wrap: nowrap; justify-content: center; gap: 8px; padding: 0 12px; }
.modal .win-actions .btn { white-space: nowrap; }
.modal .win-badge {
  flex: none; width: 44px; height: 44px; border-radius: 50%; background: var(--good);
  display: flex; align-items: center; justify-content: center;
}
.modal .win-badge svg { width: 26px; height: 26px; fill: none; stroke: #fff; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }

#toast-root { position: fixed; left: 16px; bottom: 16px; z-index: 70; }
.toast {
  background: #2b2926;
  color: #f3efe8;
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 12px 14px 14px;
  min-width: 260px;
  max-width: min(380px, calc(100vw - 32px));
  position: relative;
  overflow: hidden;
}
.toast .row { display: flex; align-items: center; gap: 14px; justify-content: space-between; }
.toast button { background: transparent; border: 1px solid #77716a; color: #fff; border-radius: 6px; padding: 4px 12px; cursor: pointer; font-weight: 600; }
.toast .bar { position: absolute; left: 0; bottom: 0; height: 4px; width: 100%; background: #6aa2da; transform-origin: left; animation: drain 10s linear forwards; }
@keyframes drain { to { transform: scaleX(0); } }

.scrim { display: none; }

/* ---- phone */
@media (max-width: 860px) {
  :root { --bs: min(100vw - 16px, 620px, 100vh - 250px); }
  .app { display: block; }
  .topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px;
    position: sticky;
    top: 0;
    z-index: 10;
    background: var(--bg);
  }
  .topbar-name { display: none; }
  .chip { color: var(--muted); margin-left: 4px; }
  .list-panel {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(92vw, 380px);
    height: 100%;
    max-height: none;
    z-index: 40;
    transform: translateX(-102%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }
  .list-panel.open { transform: none; }
  .close-list { display: inline-block; }
  .scrim.open { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); z-index: 30; }
  /* Stacked: board, title and status, buttons, moves, credits. */
  .stage { display: flex; flex-direction: column; align-items: center; padding: 0 8px; gap: 0; }
  .board-host { order: 1; }
  .col-side { order: 2; width: var(--bs); margin-top: 10px; }
  .controls { order: 3; margin-top: 4px; }
  .moves { order: 4; width: var(--bs); max-height: 220px; }
  .info h1 { font-size: 17px; }
  .info p { margin-bottom: 8px; }
  .status { margin: 0 0 8px; }
  .credits { grid-column: auto; padding: 18px 12px 24px; }
  #toast-root { left: 8px; right: 8px; bottom: 8px; }
  .toast { max-width: none; }
}
@media (max-width: 860px) and (min-height: 760px) {
  :root { --bs: min(100vw - 16px, 620px); }
}
@media (min-width: 861px) and (max-width: 1180px) {
  :root { --list-w: 290px; --bs: clamp(280px, min(100vh - 170px, 100vw - 290px - 260px - 90px), 760px); }
  .stage { grid-template-columns: var(--bs) minmax(200px, 260px); gap: 22px; padding-left: 20px; padding-right: 20px; }
}
