:root {
  --bg: #0d1020;
  --panel: #161a30;
  --panel-2: #1e2340;
  --line: #2b3157;
  --text: #f4f6ff;
  --muted: #9aa3c7;
  --x: #ff6b81;
  --o: #36d7e0;
  --accent: #6c7bff;
  --accent-hover: #8a96ff;
  --radius: 16px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  min-height: 100vh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--text);
  background: radial-gradient(circle at 20% 0%, #1b2150 0%, var(--bg) 55%);
  line-height: 1.5;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}

.app { width: 100%; max-width: 440px; }
[hidden] { display: none !important; }
.x { color: var(--x); }
.o { color: var(--o); }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px 24px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.4);
}

h1 { text-align: center; font-size: 2.2rem; letter-spacing: -0.5px; }
.subtitle { text-align: center; color: var(--muted); margin: 4px 0 24px; }

fieldset { border: 0; margin-bottom: 18px; min-width: 0; }
legend { font-size: 0.85rem; font-weight: 600; color: var(--muted); margin-bottom: 8px; }

/* Segmented radio buttons */
.segmented { display: flex; gap: 6px; flex-wrap: wrap; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label {
  flex: 1 1 0;
  min-width: max-content;
  text-align: center;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-2);
  cursor: pointer;
  font-size: 0.95rem;
  transition: background 0.15s, border-color 0.15s;
}
.segmented label:hover { border-color: var(--accent); }
.segmented input:checked + label { background: var(--accent); border-color: var(--accent); color: #fff; }
.segmented input:checked + label .x,
.segmented input:checked + label .o { color: #fff; }
.segmented input:focus-visible + label { outline: 3px solid var(--accent-hover); outline-offset: 2px; }

.hint { margin-top: 8px; font-size: 0.85rem; color: var(--muted); }

button { font: inherit; cursor: pointer; }
button:focus-visible { outline: 3px solid var(--accent-hover); outline-offset: 2px; }

.primary, .secondary {
  border-radius: 10px;
  padding: 12px 18px;
  font-weight: 600;
  border: 1px solid transparent;
  transition: background 0.15s, transform 0.1s;
}
.primary { background: var(--accent); color: #fff; width: 100%; }
.primary:hover { background: var(--accent-hover); }
.primary:active, .secondary:active { transform: translateY(1px); }
.secondary { background: var(--panel-2); color: var(--text); border-color: var(--line); }
.secondary:hover { border-color: var(--accent); }

.link { background: none; border: 0; color: var(--muted); text-decoration: underline; font-size: 0.85rem; }
.link:hover { color: var(--text); }

.alltime { margin-top: 24px; padding-top: 18px; border-top: 1px solid var(--line); text-align: center; }
.alltime h2 { font-size: 0.85rem; color: var(--muted); margin-bottom: 6px; }
.alltime p { font-size: 0.95rem; }
.alltime .link { margin-top: 8px; }

/* Game screen */
.match-info { text-align: center; color: var(--muted); font-size: 0.9rem; margin-bottom: 14px; }

.scoreboard { display: grid; grid-template-columns: 1fr auto 1fr; gap: 10px; margin-bottom: 14px; }
.score {
  display: grid; justify-items: center; gap: 2px;
  background: var(--panel-2);
  border: 2px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
  transition: border-color 0.2s;
}
.score.active.x-side { border-color: var(--x); }
.score.active.o-side { border-color: var(--o); }
.score-name { font-size: 0.8rem; color: var(--muted); text-align: center; }
.score-value { font-size: 1.8rem; font-weight: 700; line-height: 1.1; }
.x-side .score-value { color: var(--x); }
.o-side .score-value { color: var(--o); }
.draws-side { min-width: 70px; }

.pips { display: flex; gap: 4px; min-height: 10px; }
.pip { width: 10px; height: 10px; border-radius: 50%; background: var(--line); }
.x-side .pip.on { background: var(--x); }
.o-side .pip.on { background: var(--o); }

.status { text-align: center; font-weight: 600; min-height: 1.6em; margin-bottom: 14px; }

.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  width: min(100%, 360px);
  margin: 0 auto 18px;
}
.cell {
  aspect-ratio: 1;
  border: 0;
  border-radius: 14px;
  background: var(--panel-2);
  color: var(--text);
  display: grid;
  place-items: center;
  padding: 14%;
  transition: background 0.15s, transform 0.1s;
}
.cell:not(:disabled):hover { background: #272d54; transform: translateY(-2px); }
.cell:disabled { cursor: default; }
.cell.win { background: #2a3160; box-shadow: inset 0 0 0 3px currentColor; }

.mark { width: 100%; height: 100%; }
.mark.x { color: var(--x); }
.mark.o { color: var(--o); }
.mark path, .mark circle {
  fill: none; stroke: currentColor; stroke-width: 10; stroke-linecap: round;
  stroke-dasharray: 100; stroke-dashoffset: 100;
  animation: draw 0.35s ease forwards;
}
.mark path + path { animation-delay: 0.12s; }
@keyframes draw { to { stroke-dashoffset: 0; } }

.controls { display: flex; gap: 8px; flex-wrap: wrap; }
.controls .primary { flex: 1 1 100%; }
.controls .secondary { flex: 1 1 0; }

.footer { text-align: center; color: var(--muted); font-size: 0.8rem; margin-top: 16px; }

/* Match result dialog */
dialog {
  margin: auto;
  width: min(92vw, 380px);
  padding: 28px 24px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  text-align: center;
}
dialog::backdrop { background: rgba(5, 7, 18, 0.7); }
dialog h2 { font-size: 1.5rem; margin-bottom: 6px; }
dialog p { color: var(--muted); margin-bottom: 20px; }

@media (max-width: 420px) {
  .panel { padding: 22px 16px; }
  .scoreboard { gap: 6px; }
  .score { padding: 8px 8px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation-duration: 0.01ms !important; animation-delay: 0s !important; }
}
