/* ===== Variables ===== */
:root {
  --color-bg: #f4f4f8;
  --color-surface: #ffffff;
  --color-text: #1a1a2e;
  --color-text-muted: #6b7280;
  --color-x: #e74c3c;
  --color-o: #3498db;
  --color-border: #d1d5db;
  --color-hover: #e5e7eb;
  --color-accent: #6366f1;
  --color-accent-hover: #4f46e5;
  --radius-sm: 6px;
  --radius-md: 12px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
  --board-size: min(80vw, 360px);
  --cell-size: calc(var(--board-size) / 3);
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #0f0f1a;
    --color-surface: #1a1a2e;
    --color-text: #e5e7eb;
    --color-text-muted: #9ca3af;
    --color-border: #374151;
    --color-hover: #2d2d44;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
  }
}

/* ===== Reset & Base ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font);
  background: var(--color-bg);
  color: var(--color-text);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  overflow-x: hidden;
}

/* ===== Titre ===== */
h1 {
  font-size: 2rem;
  font-weight: 700;
  margin-bottom: 1rem;
  letter-spacing: 0.02em;
}

/* ===== Paramètres ===== */
#settings {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  justify-content: center;
  margin-bottom: 1rem;
}

#settings div {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

#settings label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

#settings select {
  appearance: none;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 2rem 0.5rem 0.75rem;
  font-size: 0.95rem;
  font-family: var(--font);
  color: var(--color-text);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%236b7280' d='M6 8.5L1 3.5h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.6rem center;
  transition: border-color 0.2s, box-shadow 0.2s;
}

#settings select:hover {
  border-color: var(--color-accent);
}

#settings select:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.25);
}

/* ===== Statut ===== */
#status {
  margin-bottom: 1rem;
}

#status-text {
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--color-text);
  text-align: center;
}

/* ===== Grille de jeu ===== */
#board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: var(--board-size);
  height: var(--board-size);
  gap: 0;
  margin-bottom: 1rem;
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  border: 2px solid var(--color-border);
}

#board button {
  width: 100%;
  height: 100%;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  font-size: calc(var(--board-size) / 9);
  font-weight: 700;
  font-family: var(--font);
  color: var(--color-text);
  cursor: pointer;
  transition: background 0.2s, transform 0.1s, box-shadow 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

#board button:first-child,
#board button:nth-child(2),
#board button:nth-child(3) {
  border-top: none;
}

#board button:nth-child(3n + 1) {
  border-left: none;
}

#board button:nth-child(3n + 2),
#board button:nth-child(3n + 3) {
  border-right: none;
}

#board button:nth-child(7),
#board button:nth-child(8),
#board button:nth-child(9) {
  border-bottom: none;
}

#board button:hover:not(:disabled) {
  background: var(--color-hover);
  box-shadow: inset 0 0 0 2px var(--color-accent);
}

#board button:active:not(:disabled) {
  transform: scale(0.95);
}

#board button:disabled {
  cursor: default;
}

#board button[data-value="X"] {
  color: var(--color-x);
  animation: popIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

#board button[data-value="O"] {
  color: var(--color-o);
  animation: popIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes popIn {
  0% {
    transform: scale(0);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  #board button[data-value="X"],
  #board button[data-value="O"] {
    animation: none;
  }
}

/* ===== Scores ===== */
#scores {
  background: var(--color-surface);
  border-radius: var(--radius-md);
  padding: 1rem 1.5rem;
  box-shadow: var(--shadow-md);
  margin-bottom: 1rem;
  text-align: center;
}

#scores h2 {
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
  margin-bottom: 0.75rem;
}

#scores table {
  width: 100%;
  border-collapse: collapse;
}

#scores th,
#scores td {
  padding: 0.5rem 1.5rem;
  text-align: center;
}

#scores th {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 2px solid var(--color-border);
}

#scores td {
  font-size: 1.5rem;
  font-weight: 700;
}

#score-x {
  color: var(--color-x);
}

#score-o {
  color: var(--color-o);
}

/* ===== Boutons d'action ===== */
#buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  justify-content: center;
}

#buttons button {
  appearance: none;
  background: var(--color-accent);
  color: #ffffff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.65rem 1.5rem;
  font-size: 0.95rem;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background 0.2s, box-shadow 0.2s, transform 0.1s;
}

#buttons button:hover {
  background: var(--color-accent-hover);
  box-shadow: var(--shadow-md);
}

#buttons button:active {
  transform: scale(0.97);
}

/* ===== Accessibilité : reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
  #board button,
  #buttons button {
    transition: none;
  }

  #board button:active:not(:disabled) {
    transform: none;
  }

  #buttons button:active {
    transform: none;
  }
}

/* ===== Cellule gagnante ===== */
#board button.win {
  background: var(--color-accent);
  color: #ffffff;
  animation: pulse 0.4s ease-in-out;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}

@media (prefers-reduced-motion: reduce) {
  #board button.win {
    animation: none;
    background: var(--color-accent);
    color: #ffffff;
  }
}

/* ===== Responsive ===== */
@media (min-width: 640px) {
  h1 {
    font-size: 2.5rem;
  }

  #status-text {
    font-size: 1.3rem;
  }
}
