:root {
  --ink: #1d2433;
  --paper: #eef1f6;
  --grid: #c9d1de;
  --x: #2f5fd0;
  --o: #d4880f;
  --muted: #5b6477;
  --good: #1f8a4c;
  --bad: #c0392b;
  --hl: #fdf1dc;
  /* Board size: fits the screen height, never bigger than 420px */
  --board: clamp(240px, calc(100vh - 190px), 420px);
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  overflow: hidden;              /* one static page, no scrolling */
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 28px 28px;
  color: var(--ink);
  font-family: "Bricolage Grotesque", "Segoe UI", system-ui, sans-serif;
  line-height: 1.4;
}
.page {
  height: 100vh;
  max-width: 1320px;
  margin: 0 auto;
  padding: 20px 24px;
  display: grid;
  grid-template-columns: 260px var(--board) minmax(300px, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  gap: 18px 28px;
}
header { grid-column: 1 / -1; display: flex; align-items: baseline; gap: 16px; flex-wrap: wrap; }
h1 { font-size: 2rem; font-weight: 800; margin: 0; line-height: 1.1; }
.sub { color: var(--muted); margin: 0; }
h2 { font-size: 1.1rem; margin: 0 0 4px; }
form { margin: 0; }
section { min-height: 0; }

/* Levels */
.levels { display: flex; gap: 4px; background: #fff; border: 2px solid var(--ink); border-radius: 10px; padding: 4px; }
.levels form { flex: 1; }
.level {
  width: 100%; font: inherit; font-size: 0.92rem; font-weight: 600; padding: 8px 2px; white-space: nowrap;
  border: none; border-radius: 6px; background: transparent; color: var(--ink); cursor: pointer;
}
.level.active { background: var(--ink); color: #fff; }
.mode-note { color: var(--muted); font-size: 0.86rem; margin: 8px 0 12px; }

/* Score */
.scoreboard { background: #fff; border: 2px solid var(--ink); border-radius: 10px; padding: 12px 14px; margin-bottom: 14px; }
.points { display: flex; justify-content: space-between; align-items: baseline; min-height: 2.4rem; }
.points .label { font-weight: 600; }
.points .value { font-size: 2rem; font-weight: 800; line-height: 1; }
.points .value.pos { color: var(--good); }
.points .value.neg { color: var(--bad); }
.tally { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-top: 4px; text-align: center; }
.tally strong { display: block; font-size: 1.25rem; }
.tally span { font-size: 0.78rem; color: var(--muted); }
.xc { color: var(--x); }
.oc { color: var(--o); }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.btn {
  width: 100%; font: inherit; font-size: 0.92rem; font-weight: 600; padding: 9px 4px; border-radius: 6px; white-space: nowrap;
  border: 2px solid var(--ink); background: var(--ink); color: #fff; cursor: pointer;
}
.btn.ghost { background: transparent; color: var(--ink); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* Board: fixed size and fixed equal rows, so it never changes shape */
.board {
  width: var(--board);
  height: var(--board);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 6px;
  background: var(--ink);
  padding: 6px;
  border-radius: 10px;
}
.cell {
  width: 100%; height: 100%; min-width: 0; min-height: 0;
  background: #fff; border: none; border-radius: 4px; padding: 0;
  font: inherit; font-size: calc(var(--board) / 7); font-weight: 800; line-height: 1;
  display: flex; align-items: center; justify-content: center;
}
button.cell { cursor: pointer; }
button.cell:hover { background: #f3f6fb; }
button:focus-visible { outline: 3px solid var(--x); outline-offset: 2px; }
.cell.x { color: var(--x); }
.cell.o { color: var(--o); }
.cell.last { box-shadow: inset 0 0 0 4px var(--o); }
.cell.win { background: var(--hl); }
.cell.hint {
  background: #e6f4ec; box-shadow: inset 0 0 0 4px var(--good);
  color: var(--good); font-size: 1rem; font-weight: 600;
}
.status { font-size: 1.05rem; font-weight: 600; margin: 12px 0 0; text-align: center; }

/* Right side: analysis + history, each scrolls inside itself if needed */
.side { display: flex; flex-direction: column; gap: 14px; min-height: 0; }
.card {
  background: #fff; border: 2px solid var(--ink); border-radius: 10px;
  padding: 14px 16px; overflow-y: auto; min-height: 0;
}
.analysis { flex: 0 1 auto; }
.history { flex: 0 0 auto; max-height: 30%; }
.note { color: var(--muted); font-size: 0.88rem; margin: 4px 0; }
.note.small { font-size: 0.78rem; }
.options { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; margin: 8px 0; }
.option { border: 2px solid var(--grid); border-radius: 8px; padding: 5px; }
.option.chosen { border-color: var(--o); background: var(--hl); }
.mini {
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
  gap: 2px; background: var(--ink); padding: 2px; border-radius: 4px; aspect-ratio: 1;
}
.mini span {
  background: #fff; display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 0.9rem; line-height: 1; min-height: 0;
}
.mini .x { color: var(--x); }
.mini .o { color: var(--o); }
.mini .new { background: var(--hl); }
.meta { display: flex; flex-wrap: wrap; gap: 0 8px; margin-top: 3px; font-size: 0.74rem; }
.tag { background: var(--o); color: #fff; border-radius: 4px; padding: 0 5px; font-weight: 600; }

.moves { margin: 6px 0 0; padding-left: 24px; columns: 2; column-gap: 24px; font-size: 0.88rem; }
.moves li { break-inside: avoid; }
.moves .xm { color: var(--x); }
.moves .om { color: var(--o); }

/* Small screens (phones): stack and allow scrolling */
@media (max-width: 900px) {
  body { overflow: auto; }
  .page { height: auto; grid-template-columns: 1fr; --board: min(90vw, 380px); }
  .play { justify-self: center; }
  .history { max-height: none; }
}