/* ---------- Design tokens ---------- */

:root {
  color-scheme: light dark;

  --bg: #f4f5fb;
  --bg-accent: #e6e9f7;
  --surface: #ffffff;
  --surface-muted: #eef0f8;
  --text: #1b1d2a;
  --text-muted: #4d5270;
  --border: #d3d7e8;
  --border-strong: #8d93b0;

  --x: #3346cc;
  --x-soft: #e3e7ff;
  --o: #b8174f;
  --o-soft: #fde3ec;
  --draw: #4d5270;
  --draw-soft: #e9ebf3;

  --primary: #3346cc;
  --primary-hover: #2636a8;
  --on-primary: #ffffff;
  --focus: #1f2fa3;
  --pending-bg: #fff4d6;
  --pending-text: #6b4a00;
  --pending-border: #e0b44a;

  --shadow: 0 1px 2px rgb(20 24 50 / 6%), 0 8px 24px rgb(20 24 50 / 8%);
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;

  --font: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11131c;
    --bg-accent: #181b2a;
    --surface: #1b1e2b;
    --surface-muted: #242839;
    --text: #eef0fa;
    --text-muted: #b0b5d0;
    --border: #353a52;
    --border-strong: #6c7395;

    --x: #93a3ff;
    --x-soft: #262d58;
    --o: #ff85b0;
    --o-soft: #48202f;
    --draw: #b0b5d0;
    --draw-soft: #2a2e40;

    --primary: #93a3ff;
    --primary-hover: #b3beff;
    --on-primary: #10132a;
    --focus: #c7d0ff;
    --pending-bg: #3a2f12;
    --pending-text: #ffe1a1;
    --pending-border: #8a6a1f;

    --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 8px 24px rgb(0 0 0 / 30%);
  }
}

/* ---------- Base ---------- */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font);
  font-size: 1rem;
  line-height: 1.5;
  color: var(--text);
  background:
    radial-gradient(circle at 15% 0%, var(--bg-accent), transparent 45%),
    radial-gradient(circle at 100% 100%, var(--bg-accent), transparent 40%), var(--bg);
}

h1,
h2,
p {
  margin: 0;
}

button,
input {
  font: inherit;
}

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: 16px;
  top: -100px;
  z-index: 10;
  padding: 0.5rem 1rem;
  border-radius: var(--radius-sm);
  background: var(--primary);
  color: var(--on-primary);
  font-weight: 600;
  text-decoration: none;
}

.skip-link:focus {
  top: 16px;
}

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

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas:
    'header'
    'scores'
    'play'
    'settings';
  gap: 1.25rem;
  width: 100%;
  max-width: 1040px;
  margin: 0 auto;
  padding: 1.5rem 16px 1rem;
}

.app-header {
  grid-area: header;
  text-align: center;
}

.scoreboard {
  grid-area: scores;
}

.play {
  grid-area: play;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  padding: 1.25rem 1rem 1.5rem;
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.settings {
  grid-area: settings;
}

@media (min-width: 880px) {
  .app {
    grid-template-columns: minmax(0, 1fr) 340px;
    grid-template-areas:
      'header header'
      'play scores'
      'play settings';
    grid-template-rows: auto auto 1fr;
    align-items: start;
    gap: 1.5rem;
    padding-top: 2.5rem;
  }

  .app-header {
    text-align: left;
  }

  .play {
    padding: 2rem 1.5rem 2rem;
  }
}

/* ---------- Header ---------- */

.app-title {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  font-size: clamp(1.75rem, 4vw + 0.5rem, 2.5rem);
  font-weight: 800;
  letter-spacing: -0.02em;
}

.title-x,
.title-o {
  font-size: 0.8em;
  font-weight: 900;
}

.title-x {
  color: var(--x);
}

.title-o {
  color: var(--o);
  margin-left: -0.45rem;
}

.app-subtitle {
  margin-top: 0.25rem;
  color: var(--text-muted);
}

/* ---------- Scoreboard ---------- */

.scores {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.75rem;
  margin: 0;
}

.score {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 0.35rem;
  padding: 0.85rem 0.75rem;
  border: 2px solid transparent;
  border-radius: var(--radius-md);
  background: var(--surface);
  box-shadow: var(--shadow);
  transition:
    border-color 0.2s ease,
    background-color 0.2s ease,
    transform 0.2s ease;
}

.score dt {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.score-label {
  font-size: 0.875rem;
  font-weight: 700;
  line-height: 1.25;
}

.score-meta {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  line-height: 1.3;
}

.score-value {
  margin: 0;
  font-size: clamp(1.75rem, 3vw + 1rem, 2.25rem);
  font-weight: 800;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.score--x .score-value {
  color: var(--x);
}

.score--o .score-value {
  color: var(--o);
}

.score--draw .score-value {
  color: var(--draw);
}

.score.is-active.score--x {
  border-color: var(--x);
  background: var(--x-soft);
}

.score.is-active.score--o {
  border-color: var(--o);
  background: var(--o-soft);
}

.score.is-winner.score--x {
  border-color: var(--x);
  background: var(--x-soft);
}

.score.is-winner.score--o {
  border-color: var(--o);
  background: var(--o-soft);
}

.score-value.is-bumped {
  animation: bump 0.45s ease;
}

@media (min-width: 880px) {
  .scores {
    grid-template-columns: minmax(0, 1fr);
  }

  .score {
    flex-direction: row;
    align-items: center;
    padding: 1rem 1.25rem;
  }

  .score-label {
    font-size: 1rem;
  }
}

/* ---------- Mark badges ---------- */

.mark-badge {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 1.5em;
  height: 1.5em;
  border-radius: 6px;
  font-size: 0.85em;
  font-weight: 900;
  line-height: 1;
}

.mark-badge--x {
  color: var(--x);
  background: var(--x-soft);
}

.mark-badge--o {
  color: var(--o);
  background: var(--o-soft);
}

.mark-badge[hidden] {
  display: none;
}

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

.status {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  min-height: 3rem;
  padding: 0.5rem 1.25rem;
  border-radius: 999px;
  background: var(--surface-muted);
  font-size: clamp(1.15rem, 1.5vw + 0.9rem, 1.4rem);
  font-weight: 750;
  text-align: center;
}

.status .mark-badge {
  font-size: 1rem;
  width: 1.9rem;
  height: 1.9rem;
}

.status[data-tone='win'] {
  background: var(--pending-bg);
  color: var(--pending-text);
  box-shadow: inset 0 0 0 2px var(--pending-border);
}

.status[data-tone='draw'] {
  background: var(--draw-soft);
}

.status-hint {
  min-height: 1.5em;
  margin-top: -0.4rem;
  color: var(--text-muted);
  font-size: 0.95rem;
  text-align: center;
}

/* ---------- Board ---------- */

.board {
  --gap: clamp(6px, 1.6vw, 12px);
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  gap: var(--gap);
  width: min(100%, 420px);
  aspect-ratio: 1;
  padding: var(--gap);
  border-radius: var(--radius-lg);
  background: var(--surface-muted);
}

.board:focus {
  outline: none;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: 2px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition:
    background-color 0.15s ease,
    border-color 0.15s ease,
    transform 0.1s ease,
    opacity 0.25s ease;
}

.cell:focus-visible {
  z-index: 1;
  outline-offset: 2px;
}

.cell[aria-disabled='true'] {
  cursor: default;
}

.board[data-interactive='true'] .cell[aria-disabled='false']:hover {
  border-color: var(--border-strong);
  background: var(--surface-muted);
}

.board[data-interactive='true'] .cell[aria-disabled='false']:active {
  transform: scale(0.97);
}

/* Ghost preview of the mark the current player would place. */
.board[data-interactive='true'] .cell[aria-disabled='false']:is(:hover, :focus-visible)::after {
  content: '';
  position: absolute;
  inset: 22%;
  opacity: 0.22;
  border-radius: 50%;
  pointer-events: none;
}

.board[data-interactive='true'][data-turn='O'] .cell[aria-disabled='false']:is(:hover, :focus-visible)::after {
  border: 0.55rem solid var(--o);
}

.board[data-interactive='true'][data-turn='X'] .cell[aria-disabled='false']:is(:hover, :focus-visible)::after {
  border-radius: 0;
  background:
    linear-gradient(45deg, transparent 44%, var(--x) 44%, var(--x) 56%, transparent 56%),
    linear-gradient(-45deg, transparent 44%, var(--x) 44%, var(--x) 56%, transparent 56%);
}

.board.is-thinking .cell {
  cursor: progress;
}

.cell-mark {
  display: grid;
  place-items: center;
  width: 78%;
  height: 78%;
  pointer-events: none;
}

.cell-mark svg {
  width: 100%;
  height: 100%;
  overflow: visible;
}

.cell-mark .stroke {
  fill: none;
  stroke-width: 11;
  stroke-linecap: round;
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
  animation: draw 0.28s ease-out forwards;
}

.cell-mark .stroke--second {
  animation-delay: 0.14s;
}

.cell[data-mark='X'] .stroke {
  stroke: var(--x);
}

.cell[data-mark='O'] .stroke {
  stroke: var(--o);
}

.cell.is-last {
  border-color: var(--border-strong);
}

/* Finished rounds: fade the rest of the board and highlight the result. */
.board[data-status='won'] .cell:not(.is-winning),
.board[data-status='draw'] .cell {
  opacity: 0.55;
}

.board[data-status='draw'] {
  animation: settle 0.5s ease;
}

.cell.is-winning {
  opacity: 1;
  animation: win-pulse 0.9s ease 0.2s 2;
}

.cell.is-winning[data-mark='X'] {
  border-color: var(--x);
  background: var(--x-soft);
}

.cell.is-winning[data-mark='O'] {
  border-color: var(--o);
  background: var(--o-soft);
}

.cell.is-rejected {
  animation: shake 0.3s ease;
}

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

.controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.75rem;
  width: min(100%, 420px);
}

.button {
  flex: 1 1 10rem;
  min-height: 3rem;
  padding: 0.65rem 1.25rem;
  border: 2px solid transparent;
  border-radius: var(--radius-sm);
  font-weight: 700;
  cursor: pointer;
  transition:
    background-color 0.15s ease,
    border-color 0.15s ease,
    transform 0.1s ease;
}

.button:active {
  transform: translateY(1px);
}

.button--primary {
  background: var(--primary);
  color: var(--on-primary);
}

.button--primary:hover {
  background: var(--primary-hover);
}

.button--secondary {
  border-color: var(--border-strong);
  background: var(--surface);
  color: var(--text);
}

.button--secondary:hover {
  background: var(--surface-muted);
}

/* ---------- Settings ---------- */

.settings {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1.25rem;
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.settings-heading {
  font-size: 1.15rem;
  font-weight: 750;
}

.option-group {
  margin: 0;
  padding: 0;
  border: 0;
  min-width: 0;
}

.option-group legend {
  margin-bottom: 0.4rem;
  padding: 0;
  font-weight: 650;
}

.option-group:disabled legend {
  color: var(--text-muted);
}

.option-help {
  margin-top: 0.4rem;
  color: var(--text-muted);
  font-size: 0.875rem;
}

.segmented {
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: 12px;
  background: var(--surface-muted);
}

.segment {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
}

.segment input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.segment span {
  display: grid;
  place-items: center;
  min-height: 2.75rem;
  padding: 0.35rem 0.5rem;
  border: 2px solid transparent;
  border-radius: 9px;
  font-weight: 650;
  color: var(--text-muted);
  text-align: center;
  transition:
    background-color 0.15s ease,
    color 0.15s ease;
  pointer-events: none;
}

.segment input:hover + span {
  color: var(--text);
}

.segment input:checked + span {
  border-color: var(--primary);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgb(20 24 50 / 15%);
}

.segment input:focus-visible + span {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.segment input:disabled {
  cursor: not-allowed;
}

.option-group:disabled .segmented {
  opacity: 0.6;
}

.option-group:disabled .segment input:checked + span {
  border-color: var(--border-strong);
  box-shadow: none;
}

.settings-note {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem 0.9rem;
  border-radius: var(--radius-sm);
  background: var(--surface-muted);
  font-size: 0.9rem;
}

.settings-active {
  font-weight: 600;
}

.settings-pending {
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--pending-border);
  border-radius: 8px;
  background: var(--pending-bg);
  color: var(--pending-text);
}

.settings-pending[hidden] {
  display: none;
}

/* ---------- Footer ---------- */

.app-footer {
  padding: 0.5rem 16px 2rem;
  color: var(--text-muted);
  font-size: 0.85rem;
  text-align: center;
}

.noscript {
  max-width: 40rem;
  margin: 1rem auto;
  padding: 1rem;
  border-radius: var(--radius-sm);
  background: var(--pending-bg);
  color: var(--pending-text);
  text-align: center;
}

/* ---------- Animations ---------- */

@keyframes draw {
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes win-pulse {
  50% {
    transform: scale(1.05);
  }
}

@keyframes bump {
  40% {
    transform: scale(1.25);
  }
}

@keyframes shake {
  25% {
    transform: translateX(-4px);
  }
  75% {
    transform: translateX(4px);
  }
}

@keyframes settle {
  40% {
    transform: scale(0.98);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-delay: 0s !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Windows high contrast / forced colors: keep marks and states visible. */
@media (forced-colors: active) {
  .cell-mark .stroke {
    stroke: CanvasText !important;
  }

  .cell.is-winning {
    outline: 4px solid Highlight;
  }

  .segment input:checked + span {
    border-color: Highlight;
  }
}
