/* ===== Variables ===== */
:root {
  --bg-deep: #0a0b0f;
  --bg-card: rgba(18, 20, 28, 0.85);
  --bg-cell: rgba(28, 31, 42, 0.6);
  --border-subtle: rgba(255, 255, 255, 0.06);
  --border-cell: rgba(255, 255, 255, 0.08);
  --text-primary: #f0f2f5;
  --text-muted: #8b92a8;
  --accent-x: #3b82f6;
  --accent-x-glow: rgba(59, 130, 246, 0.35);
  --accent-o: #f59e0b;
  --accent-o-glow: rgba(245, 158, 11, 0.35);
  --win-line: linear-gradient(90deg, var(--accent-x), var(--accent-o));
  --radius-sm: 10px;
  --radius-md: 16px;
  --radius-lg: 24px;
  --shadow-glow-x: 0 0 40px var(--accent-x-glow);
  --shadow-glow-o: 0 0 40px var(--accent-o-glow);
  --font: 'Outfit', system-ui, sans-serif;
  --ease-out-expo: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --focus-ring: 0 0 0 2px var(--bg-deep), 0 0 0 4px var(--accent-x);
}

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

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font);
  background: var(--bg-deep);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-x: hidden;
  position: relative;
}

/* Subtle gradient orbs in background */
body::before,
body::after {
  content: '';
  position: fixed;
  width: 60vmax;
  height: 60vmax;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.12;
  pointer-events: none;
  z-index: 0;
}
body::before {
  background: var(--accent-x);
  top: -20%;
  left: -20%;
}
body::after {
  background: var(--accent-o);
  bottom: -20%;
  right: -20%;
}

/* Noise overlay for texture */
.noise {
  position: fixed;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.04'/%3E%3C/svg%3E");
  pointer-events: none;
  z-index: 1;
}

/* ===== Game container ===== */
.game-container {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 420px;
  padding: 2rem 1.5rem;
}

/* ===== Header ===== */
.game-header {
  text-align: center;
  margin-bottom: 2rem;
}

.title {
  font-size: 1.75rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin-bottom: 1.25rem;
  background: linear-gradient(135deg, var(--text-primary) 0%, var(--text-muted) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.scores {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  margin-bottom: 1rem;
}

.score-card {
  flex: 1;
  max-width: 100px;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 0.6rem 0.75rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  backdrop-filter: blur(12px);
  transition: transform 0.2s var(--ease-out-expo), box-shadow 0.2s ease;
}
.score-card:hover {
  transform: translateY(-2px);
}
.score-card.score-x { --score-accent: var(--accent-x); }
.score-card.score-o { --score-accent: var(--accent-o); }
.score-card.score-draw { --score-accent: var(--text-muted); }

.score-label {
  font-size: 0.7rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}
.score-card.score-x .score-label { color: var(--accent-x); }
.score-card.score-o .score-label { color: var(--accent-o); }

.score-value {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-primary);
}

.turn-indicator {
  font-size: 0.95rem;
  color: var(--text-muted);
  font-weight: 500;
}
.turn-indicator #currentPlayer {
  color: var(--accent-x);
  font-weight: 600;
  transition: color 0.25s ease;
}
.turn-indicator.player-o #currentPlayer {
  color: var(--accent-o);
}

.mode-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}
.mode-label {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.mode-toggle {
  display: flex;
  background: var(--bg-cell);
  border: 1px solid var(--border-cell);
  border-radius: var(--radius-sm);
  padding: 3px;
}
.mode-btn {
  font-family: var(--font);
  font-size: 0.8rem;
  font-weight: 500;
  padding: 0.4rem 0.75rem;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease;
}
.mode-btn:hover {
  color: var(--text-primary);
}
.mode-btn:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}
.mode-btn.active {
  background: var(--bg-card);
  color: var(--text-primary);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}
.ai-options {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  justify-content: center;
  margin-bottom: 0.75rem;
  transition: opacity 0.2s ease;
}
.ai-options[aria-hidden="true"] {
  display: none;
}
.ai-option {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.ai-option-label {
  font-size: 0.8rem;
  color: var(--text-muted);
}
.select {
  font-family: var(--font);
  font-size: 0.85rem;
  padding: 0.4rem 0.6rem;
  background: var(--bg-cell);
  border: 1px solid var(--border-cell);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  cursor: pointer;
}
.select:focus {
  outline: none;
  border-color: var(--accent-x);
}
.select:focus-visible {
  outline: none;
  border-color: var(--accent-x);
  box-shadow: 0 0 0 2px var(--accent-x-glow);
}
.keyboard-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 0.75rem;
  text-align: center;
  opacity: 0.85;
}

/* ===== Board ===== */
.board-wrapper {
  position: relative;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  margin-bottom: 1.5rem;
  backdrop-filter: blur(12px);
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.25);
}

.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
  position: relative;
  z-index: 1;
  transition: opacity 0.2s ease, pointer-events 0s;
}
.board.ai-thinking {
  pointer-events: none;
  opacity: 0.75;
}
@media (prefers-reduced-motion: no-preference) {
  .board.ai-thinking {
    animation: ai-thinking-pulse 1.5s ease-in-out infinite;
  }
}
@keyframes ai-thinking-pulse {
  0%, 100% { opacity: 0.75; }
  50% { opacity: 0.5; }
}

.cell {
  position: relative;
  aspect-ratio: 1;
  min-height: 90px;
  background: var(--bg-cell);
  border: 1px solid var(--border-cell);
  border-radius: var(--radius-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.5rem;
  font-weight: 700;
  color: transparent;
  transition: background 0.2s ease, border-color 0.2s ease, transform 0.2s var(--ease-spring), box-shadow 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}
.cell:hover:not(.taken) {
  background: rgba(40, 44, 58, 0.8);
  border-color: rgba(255, 255, 255, 0.12);
  transform: scale(1.02);
}
.cell:active:not(.taken) {
  transform: scale(0.98);
}
.cell:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}
.board.next-x .cell:not(.taken):hover::after,
.board.next-o .cell:not(.taken):hover::after {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.5rem;
  font-weight: 700;
  pointer-events: none;
}
.board.next-x .cell:not(.taken):hover::after {
  content: 'X';
  color: var(--accent-x);
  opacity: 0.35;
}
.board.next-o .cell:not(.taken):hover::after {
  content: 'O';
  color: var(--accent-o);
  opacity: 0.35;
}
.cell.taken {
  cursor: default;
}
.cell.x {
  color: var(--accent-x);
  text-shadow: 0 0 20px var(--accent-x-glow);
}
.cell.o {
  color: var(--accent-o);
  text-shadow: 0 0 20px var(--accent-o-glow);
}
.cell.winner {
  animation: pulse-win 0.6s var(--ease-spring) forwards;
}
@keyframes pulse-win {
  0% { transform: scale(1); }
  50% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

/* Place animation */
.cell.x .symbol,
.cell.o .symbol {
  animation: symbol-in 0.35s var(--ease-spring) forwards;
}
@keyframes symbol-in {
  from {
    opacity: 0;
    transform: scale(0.3);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* Win line */
.win-line {
  position: absolute;
  height: 4px;
  background: var(--win-line);
  border-radius: 2px;
  box-shadow: 0 0 20px rgba(59, 130, 246, 0.5);
  transform-origin: left center;
  pointer-events: none;
  z-index: 2;
  opacity: 0;
  transition: opacity 0.3s ease;
}
.win-line.visible {
  opacity: 1;
  animation: win-line-in 0.45s var(--ease-out-expo) forwards;
}
.win-line.visible.row-line {
  transform-origin: left center;
  animation: draw-line-h 0.5s var(--ease-out-expo) forwards;
}
.win-line.visible.col-line {
  transform-origin: center top;
  animation: draw-line-v 0.5s var(--ease-out-expo) forwards;
}
.win-line.visible.diag-line {
  animation: win-line-fade 0.5s var(--ease-out-expo) forwards;
}
@keyframes win-line-in {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}
@keyframes win-line-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes draw-line-h {
  from { transform: scaleX(0); }
  to { transform: scaleX(1); }
}
@keyframes draw-line-v {
  from { transform: scaleY(0); }
  to { transform: scaleY(1); }
}

/* ===== Buttons ===== */
.actions {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  flex-wrap: wrap;
}

.btn {
  font-family: var(--font);
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.7rem 1.25rem;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: transform 0.2s var(--ease-out-expo), box-shadow 0.2s ease, background 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}
.btn:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}
.btn:hover {
  transform: translateY(-2px);
}
.btn:active {
  transform: translateY(0);
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent-x), #2563eb);
  color: white;
  box-shadow: 0 4px 14px rgba(59, 130, 246, 0.4);
}
.btn-primary:hover {
  box-shadow: 0 6px 20px rgba(59, 130, 246, 0.5);
}

.btn-secondary {
  background: var(--bg-card);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
  backdrop-filter: blur(8px);
}
.btn-secondary:hover {
  background: rgba(40, 44, 58, 0.9);
  border-color: rgba(255, 255, 255, 0.1);
}

.btn-large {
  padding: 0.9rem 2rem;
  font-size: 1rem;
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1.5rem;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}
.modal-overlay.visible {
  opacity: 1;
  visibility: visible;
}

.modal {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 2rem;
  max-width: 340px;
  width: 100%;
  text-align: center;
  transform: scale(0.9) translateY(10px);
  transition: transform 0.35s var(--ease-spring);
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.4);
}
.modal-overlay.visible .modal {
  transform: scale(1) translateY(0);
}

.modal-title {
  font-size: 1.5rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
}

.modal-desc {
  color: var(--text-muted);
  margin-bottom: 1.5rem;
  font-size: 1rem;
}

/* ===== Reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
  .cell.winner { animation: none; }
  .cell.x .symbol,
  .cell.o .symbol { animation: none; }
  .board.ai-thinking { animation: none !important; }
  .win-line.visible { animation: none; }
  .win-line.visible.row-line,
  .win-line.visible.col-line,
  .win-line.visible.diag-line { animation: none; }
}

/* ===== Responsive ===== */
@media (max-width: 480px) {
  .game-container {
    padding: 1.5rem 1rem;
  }
  .cell {
    min-height: 72px;
    font-size: 2rem;
  }
  .scores {
    gap: 0.5rem;
  }
  .score-card {
    padding: 0.5rem 0.5rem;
    max-width: 80px;
  }
  .score-value {
    font-size: 1.1rem;
  }
}
