:root {
  --bg: #f6f7fb; --card: #fff; --fg: #1b1f2a; --muted: #6b7280; --line: #d9dce6;
  --x: #e5484d; --o: #2f6feb; --accent: #2f6feb; --win: #fde68a;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12141a; --card: #1c2029; --fg: #eef0f6; --muted: #9aa2b4; --line: #323847; --win: #5b4a10;
    --x: #ff6b70; --o: #6ea0ff; --accent: #6ea0ff;
  }
}
:root[data-theme="dark"] {
  --bg: #12141a; --card: #1c2029; --fg: #eef0f6; --muted: #9aa2b4; --line: #323847; --win: #5b4a10;
  --x: #ff6b70; --o: #6ea0ff; --accent: #6ea0ff;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100dvh; background: var(--bg); color: var(--fg);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  display: grid; place-items: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}
main { width: 100%; max-width: 420px; display: grid; gap: 14px; }
header { display: flex; justify-content: space-between; align-items: center; }
h1 { margin: 0; font-size: 1.6rem; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
#theme { width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line); background: var(--card); font-size: 1.2rem; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 4px; gap: 4px; }
.seg[hidden] { display: none; }
.seg button { border: 0; background: none; padding: 10px; border-radius: 9px; min-height: 44px; }
.seg button[aria-pressed="true"] { background: var(--accent); color: #fff; }
.score { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; font-size: .85rem; color: var(--muted); }
.score div { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 8px 4px; }
.score b { display: block; font-size: 1.5rem; color: var(--fg); }
#status { margin: 0; text-align: center; font-size: 1.15rem; font-weight: 600; min-height: 1.6em; }
#board { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; aspect-ratio: 1; }
.cell {
  border: 1px solid var(--line); background: var(--card); border-radius: 14px;
  font-size: clamp(2.5rem, 16vw, 4.5rem); font-weight: 700; line-height: 1; padding: 0;
}
.cell[data-p="X"] { color: var(--x); }
.cell[data-p="O"] { color: var(--o); }
.cell:disabled { cursor: default; opacity: 1; }
.cell.win { background: var(--win); }
.cell:focus-visible, button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.primary { border: 0; background: var(--accent); color: #fff; border-radius: 12px; min-height: 48px; font-weight: 600; }
@media (prefers-reduced-motion: no-preference) { .cell { transition: background .15s; } }
