:root {
  --bg: #f4f1ea;
  --surface: #ffffff;
  --ink: #1f2328;
  --muted: #6b7078;
  --line: #d6d1c4;
  --line-strong: #1f2328;
  --x: #2463d1;
  --x-soft: rgba(36, 99, 209, 0.14);
  --o: #d9480f;
  --o-soft: rgba(217, 72, 15, 0.14);
  --active: #f2c94c;
  --active-soft: rgba(242, 201, 76, 0.28);
  --preview: rgba(31, 35, 40, 0.08);
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181c;
    --surface: #1f2228;
    --ink: #e8e6e1;
    --muted: #9aa0a8;
    --line: #353a42;
    --line-strong: #c9c6bf;
    --x: #5b9bff;
    --x-soft: rgba(91, 155, 255, 0.16);
    --o: #ff7a45;
    --o-soft: rgba(255, 122, 69, 0.16);
    --active: #e0b83a;
    --active-soft: rgba(224, 184, 58, 0.18);
    --preview: rgba(255, 255, 255, 0.07);
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.45;
  min-height: 100vh;
}

.app {
  max-width: 980px;
  margin: 0 auto;
  padding: 20px 16px 40px;
}

.top h1 {
  margin: 0;
  font-size: 1.9rem;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 2px 0 16px;
  color: var(--muted);
}

.x-text { color: var(--x); font-weight: 700; }
.o-text { color: var(--o); font-weight: 700; }

/* ---------- Controls ---------- */

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 16px;
  margin-bottom: 14px;
}

.control-group { display: flex; flex-direction: column; gap: 4px; }

.control-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.segmented {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}

.segmented button {
  border: 0;
  background: transparent;
  color: var(--ink);
  padding: 7px 14px;
  font: inherit;
  cursor: pointer;
}

.segmented button + button { border-left: 1px solid var(--line); }
.segmented button:hover { background: var(--preview); }
.segmented button.selected { background: var(--ink); color: var(--bg); }

.primary {
  border: 0;
  border-radius: 8px;
  padding: 8px 18px;
  font: inherit;
  font-weight: 600;
  background: var(--x);
  color: #fff;
  cursor: pointer;
}
.primary:hover { filter: brightness(1.08); }

/* ---------- Status ---------- */

.status {
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--line);
  margin-bottom: 16px;
  font-weight: 500;
  min-height: 1.45em;
}
.status.turn-x { border-left: 5px solid var(--x); }
.status.turn-o { border-left: 5px solid var(--o); }
.status.over { font-weight: 700; }

/* ---------- Layout ---------- */

.play-area {
  display: flex;
  gap: 24px;
  align-items: flex-start;
}

@media (max-width: 760px) {
  .play-area { flex-direction: column; align-items: stretch; }
}

/* ---------- Big board ---------- */

.board {
  --size: min(92vw, 600px);
  width: var(--size);
  height: var(--size);
  flex-shrink: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 8px;
  padding: 8px;
  background: var(--line-strong);
  border-radius: 14px;
}

@media (max-width: 760px) {
  .board { --size: min(calc(100vw - 32px), 600px); margin: 0 auto; }
}

.mini {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 3px;
  padding: 5px;
  background: var(--line);
  border-radius: 8px;
  transition: box-shadow 0.15s, background 0.15s, opacity 0.15s;
}

.mini.playable {
  background: var(--active);
  box-shadow: 0 0 0 3px var(--active);
}

.mini.dimmed { opacity: 0.55; }

.mini.preview-target:not(.claimed) {
  box-shadow: 0 0 0 3px var(--ink);
}

.mini.preview-target.claimed .claim {
  outline: 3px dashed var(--active);
  outline-offset: -6px;
}

.cell {
  border: 0;
  border-radius: 4px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 800;
  font-size: calc(var(--size) / 22);
  line-height: 1;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: default;
  transition: background 0.1s;
}

.cell.x { color: var(--x); }
.cell.o { color: var(--o); }

.cell.legal { cursor: pointer; }
.cell.legal:hover { background: var(--active-soft); }
.cell.legal:hover::after {
  content: "X";
  color: var(--x);
  opacity: 0.35;
}

.cell.last {
  box-shadow: inset 0 0 0 2px currentColor;
}

.cell.in-line.x { background: var(--x-soft); }
.cell.in-line.o { background: var(--o-soft); }

/* Claimed overlay */

.claim {
  position: absolute;
  inset: 0;
  border-radius: 8px;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.mini.claimed .claim { display: flex; }
.mini.claimed.x .claim { background: color-mix(in srgb, var(--surface) 55%, transparent); }
.mini.claimed.o .claim { background: color-mix(in srgb, var(--surface) 55%, transparent); }

.claim-mark {
  font-size: calc(var(--size) / 5);
  font-weight: 900;
  line-height: 1;
  animation: pop 0.25s ease-out;
}
.mini.x .claim-mark { color: var(--x); }
.mini.o .claim-mark { color: var(--o); }

.claim-note {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--ink);
  background: var(--surface);
  padding: 1px 7px;
  border-radius: 999px;
  margin-top: 2px;
}

.mini.game-line { box-shadow: 0 0 0 4px var(--active); }

@keyframes pop {
  from { transform: scale(0.4); opacity: 0; }
  to   { transform: scale(1);   opacity: 1; }
}

/* ---------- Side panel ---------- */

.side {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.scores {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.score {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  column-gap: 10px;
  align-items: center;
}

.score-mark {
  grid-row: span 2;
  font-size: 1.8rem;
  font-weight: 900;
}
.x-score .score-mark { color: var(--x); }
.o-score .score-mark { color: var(--o); }

.score-label { font-size: 0.8rem; color: var(--muted); }
.score-value { font-size: 1.4rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.score-note {
  grid-column: span 2;
  margin: -4px 0 0;
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
}

.rules {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 14px;
}

.rules summary {
  cursor: pointer;
  font-weight: 700;
}

.rules ol {
  padding-left: 1.2em;
  margin: 10px 0;
}

.rules li { margin-bottom: 6px; font-size: 0.92rem; }

.hint {
  font-size: 0.82rem;
  color: var(--muted);
  margin: 0;
}
