:root {
  color-scheme: light;
  --bg: #f4f2ed;
  --line: #d5d6cf;
  --muted: #686960;
  --text: #313d37;
  --x: #40645c;
  --o: #536b84;
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
* { box-sizing: border-box; }
html { overflow-y: scroll; }
body { margin: 0; padding: 20px; background: var(--bg); color: var(--text); font-size: 16px; }
button, input { font: inherit; }
button { -webkit-appearance: none; appearance: none; color: inherit; cursor: pointer; touch-action: manipulation; }
button:disabled { cursor: default; }
button:focus-visible, .moves-scroll:focus-visible { outline: 2px solid var(--x); outline-offset: 3px; }
[hidden] { display: none !important; }
.game { width: 100%; max-width: 580px; margin: 0 auto; }
.topbar, .controls, .actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.topbar { min-height: 44px; margin-bottom: 10px; }
h1 { margin: 0; font-family: Georgia, "Times New Roman", serif; font-size: 23px; line-height: 1.3; font-weight: 400; letter-spacing: -.025em; }
.matchbar { display: flex; align-items: center; justify-content: space-between; height: 30px; margin-bottom: 12px; }
.turn { display: flex; align-items: center; gap: 8px; height: 30px; font-size: 14px; color: var(--muted); }
#turn-symbol { width: 19px; text-align: center; font-size: 24px; line-height: 1; }
.x { color: var(--x); }
.o { color: var(--o); }
.segmented { display: flex; border: 1px solid var(--line); border-radius: 7px; padding: 3px; margin: 0; gap: 3px; min-width: 0; }
.segmented label { position: relative; cursor: pointer; touch-action: manipulation; }
.segmented input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.segmented label > span { display: flex; justify-content: center; align-items: center; min-height: 34px; padding: 0 12px; border-radius: 4px; font-size: 14px; color: var(--muted); }
.segmented input:checked + span { background: #e4e8df; color: var(--text); }
.segmented input:focus-visible + span { outline: 2px solid var(--x); outline-offset: 2px; }
.sides label > span { min-width: 35px; padding: 0 8px; }
.sides input:checked + .x { color: var(--x); }
.sides input:checked + .o { color: var(--o); }
.board-wrap { padding: 9px; border: 1px solid #cfd4c9; border-radius: 10px; background: #fffdf8; box-shadow: 0 5px 24px #3c493908; }
/* A definite square keeps Safari sizing independent of grid contents and previews. */
.board-frame { position: relative; width: 100%; }
.board-frame::before { content: ""; display: block; padding-top: 100%; }
.game-board {
  position: absolute; inset: 0; display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr)); gap: 9px;
}
.local-board {
  position: relative; min-width: 0; min-height: 0; display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  gap: 3px; padding: 4px; border: 1px solid #d8dad1; border-radius: 4px; background: #dddfd6;
}
.local-board.active { border-color: #8ca599; background: #c5d3c4; }
.local-board.inactive { border-color: #e0e1d9; }
.local-board.inactive .cell { background: #f2f2ec; }
.local-board.bot-active { border-color: #a2afbb; background: #d4dde4; }
.local-board.destination { box-shadow: 0 0 0 2px var(--x); opacity: 1; }
.cell {
  -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent;
  position: relative; display: grid; place-items: center;
  width: 100%; height: 100%; min-width: 0; min-height: 0;
  margin: 0; padding: 0; border: 0; border-radius: 2px; background: #f0f1eb;
  font-size: clamp(20px, 4vw, 38px); line-height: 1;
}
.cell:disabled { opacity: 1; -webkit-text-fill-color: currentColor; }
.active .cell { background: #f5f8f0; }
.cell:empty::after {
  content: attr(data-mark); position: absolute; inset: 0; display: grid; place-items: center;
  pointer-events: none; opacity: 0; visibility: hidden; color: var(--human-color, var(--x));
}
.cell:enabled:focus-visible:empty::after { opacity: .42; visibility: visible; }
.cell:focus-visible { z-index: 3; outline-offset: 0; }
.cell.last::before { content: ""; position: absolute; bottom: 3px; right: 3px; width: 4px; height: 4px; border-radius: 50%; background: currentColor; }
.claim { position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none; border-radius: 3px; background: #e6eee4f2; font-size: clamp(44px, 12vw, 90px); font-weight: 300; line-height: 1; }
.claim.o { background: #e6ebf1f2; }
.claim.draw { color: var(--muted); background: #ecebe5f2; font-size: 36px; }
.controls { margin-top: 14px; min-height: 44px; }
.text-button, .new-game { border: 0; border-radius: 5px; padding: 10px 13px; min-height: 40px; font-size: 14px; }
.text-button { background: transparent; color: var(--muted); }
.text-button:disabled { opacity: .45; }
.new-game { background: var(--x); color: #fffdf8; font-weight: 600; }
.error { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 12px; margin-top: 14px; border: 1px solid #c69c8c; border-radius: 6px; color: #834f41; font-size: 14px; }
.error p { margin: 0; }
.error button { color: inherit; }
body[data-side="O"] { --human-color: var(--o); }
@media (min-width: 700px) {
  .game { max-width: min(580px, calc(100vh - 190px)); min-width: 380px; }
}
@media (max-width: 420px) {
  body { padding: 12px; }
  .board-wrap { padding: 6px; }
  .game-board { gap: 6px; }
  .local-board { gap: 2px; padding: 3px; }
  .cell { font-size: 24px; }
  .segmented label > span { padding: 0 10px; }
  .sides label > span { padding: 0 8px; }
}
@media (hover: hover) and (pointer: fine) {
  .cell:enabled:hover { background: #dde9da; }
  .cell:enabled:hover:empty::after { opacity: .42; visibility: visible; }
  .text-button:enabled:hover { color: var(--text); }
  .new-game:hover { background: #34564e; }
}

#moves-toggle { min-height: 30px; padding: 5px 10px; }
#moves-toggle[aria-expanded="true"] { background: #e4e8df; color: var(--x); }
.moves-panel { margin-top: 14px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: #fffdf8; }
.moves-empty { margin: 0; padding: 20px 16px; color: var(--muted); font-size: 14px; }
.moves-scroll { max-height: 260px; overflow: auto; overscroll-behavior: contain; }
.moves-scroll:focus-visible { outline-offset: -3px; }
.moves-panel table { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: 14px; line-height: 1.45; text-align: left; }
.moves-panel th, .moves-panel td { padding: 10px 12px; border-bottom: 1px solid #e8e9e1; overflow-wrap: anywhere; }
.moves-panel thead th { position: sticky; top: 0; background: #eceee7; color: var(--muted); font-weight: 500; z-index: 1; }
.moves-panel th:first-child { width: 12%; color: var(--muted); font-variant-numeric: tabular-nums; }
.moves-panel thead th:nth-child(2) { width: 18%; }
.moves-panel tbody th { font-weight: 400; }
.moves-panel td:nth-child(2) { font-weight: 600; }
.moves-panel td:nth-child(n+3) { text-transform: capitalize; }
.moves-panel tbody tr:last-child { background: #f3f6ef; }
.moves-panel tbody tr:last-child > * { border-bottom: 0; }
@media (max-width: 420px) {
  .moves-panel th, .moves-panel td { padding: 9px 7px; }
}
