:root {
  --primary: #6366f1;
  --primary-glow: rgba(99, 102, 241, 0.3);
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-light: #334155;
  --text: #e2e8f0;
  --text-dim: #94a3b8;
  --x-color: #38bdf8;
  --o-color: #f472b6;
  --x-glow: rgba(56, 189, 248, 0.3);
  --o-glow: rgba(244, 114, 182, 0.3);
  --win-color: #4ade80;
  --draw-color: #fbbf24;
  --cell-size: 96px;
  --board-gap: 6px;
}

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

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

#game-container {
  text-align: center;
  padding: 1.5rem;
  max-width: 480px;
  width: 100%;
  position: relative;
}

header {
  margin-bottom: 1rem;
}

h1 {
  font-size: 2rem;
  font-weight: 800;
  background: linear-gradient(135deg, var(--x-color), var(--primary), var(--o-color));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.02em;
}

.subtitle {
  color: var(--text-dim);
  font-size: 0.9rem;
  margin-top: 0.15rem;
}

/* Score Board */
#score-board {
  display: flex;
  justify-content: center;
  gap: 1.5rem;
  margin-bottom: 1rem;
}

.score-item {
  background: var(--surface);
  border-radius: 12px;
  padding: 0.5rem 1.2rem;
  min-width: 80px;
}

.score-label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.15rem;
}

.score-item:first-child .score-value { color: var(--x-color); }
.score-item:last-child .score-value { color: var(--o-color); }
.score-item.draws .score-value { color: var(--draw-color); }

.score-value {
  font-size: 1.5rem;
  font-weight: 700;
}

.score-pop {
  animation: scorePop 0.4s ease;
}

@keyframes scorePop {
  0% { transform: scale(1); }
  50% { transform: scale(1.4); }
  100% { transform: scale(1); }
}

/* Status Bar */
#status-bar {
  margin-bottom: 1rem;
  height: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

#status-text {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-dim);
  transition: color 0.3s;
}

#status-text.player-turn { color: var(--x-color); }
#status-text.ai-turn { color: var(--o-color); }
#status-text.win { color: var(--win-color); }
#status-text.draw { color: var(--draw-color); }

/* Board */
#board {
  display: grid;
  grid-template-columns: repeat(3, var(--cell-size));
  grid-template-rows: repeat(3, var(--cell-size));
  gap: var(--board-gap);
  justify-content: center;
  margin: 0 auto 1.2rem;
  position: relative;
  width: fit-content;
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: var(--surface);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s, transform 0.15s, box-shadow 0.2s;
  position: relative;
  font-size: 2.5rem;
  font-weight: 800;
}

.cell:hover:not(.taken):not(.disabled) {
  background: var(--surface-light);
  transform: scale(1.04);
  box-shadow: 0 0 20px var(--primary-glow);
}

.cell:active:not(.taken):not(.disabled) {
  transform: scale(0.97);
}

.cell.taken {
  cursor: default;
}

.cell.disabled {
  cursor: default;
  pointer-events: none;
}

.cell.x {
  color: var(--x-color);
  text-shadow: 0 0 20px var(--x-glow);
}

.cell.o {
  color: var(--o-color);
  text-shadow: 0 0 20px var(--o-glow);
}

.cell.place-anim {
  animation: placeMarker 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes placeMarker {
  0% { transform: scale(0); opacity: 0; }
  60% { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

.cell.win-cell {
  animation: winPulse 0.6s ease infinite alternate;
}

@keyframes winPulse {
  0% { transform: scale(1); }
  100% { transform: scale(1.08); box-shadow: 0 0 25px var(--win-color); }
}

.cell.draw-cell {
  animation: drawShake 0.5s ease;
}

@keyframes drawShake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  40% { transform: translateX(4px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}

/* Win Line SVG */
#win-line {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
}

#win-line-el {
  stroke: var(--win-color);
  stroke-width: 4;
  stroke-linecap: round;
  stroke-dasharray: 500;
  stroke-dashoffset: 500;
  filter: drop-shadow(0 0 8px var(--win-color));
}

#win-line-el.animate {
  animation: drawLine 0.5s ease forwards;
}

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

/* Controls */
#controls {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  align-items: center;
}

#difficulty-selector {
  display: flex;
  gap: 0.5rem;
}

.diff-btn {
  padding: 0.5rem 1.2rem;
  border: 2px solid var(--surface-light);
  border-radius: 8px;
  background: transparent;
  color: var(--text-dim);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.diff-btn:hover {
  border-color: var(--primary);
  color: var(--text);
}

.diff-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
  box-shadow: 0 0 15px var(--primary-glow);
}

#action-buttons {
  display: flex;
  gap: 0.5rem;
}

#action-buttons button {
  padding: 0.5rem 1.2rem;
  border: none;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

#new-game-btn {
  background: var(--primary);
  color: white;
}

#new-game-btn:hover {
  opacity: 0.85;
  transform: translateY(-1px);
}

#reset-scores-btn {
  background: var(--surface);
  color: var(--text-dim);
}

#reset-scores-btn:hover {
  background: var(--surface-light);
  color: var(--text);
}

#sound-btn {
  background: var(--surface);
  color: var(--text-dim);
  width: 40px;
  padding: 0.5rem;
  font-size: 1rem;
  line-height: 1;
}

#sound-btn:hover {
  background: var(--surface-light);
}

#sound-btn.muted {
  opacity: 0.5;
}

/* Overlay */
#overlay {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  border-radius: 16px;
  animation: fadeIn 0.3s ease;
}

#overlay.hidden {
  display: none;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

#overlay-content {
  animation: slideUp 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes slideUp {
  from { transform: translateY(30px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

#overlay-title {
  font-size: 2rem;
  font-weight: 800;
  margin-bottom: 0.5rem;
}

#overlay-title.win { color: var(--win-color); }
#overlay-title.lose { color: var(--o-color); }
#overlay-title.draw { color: var(--draw-color); }

#overlay-message {
  color: var(--text-dim);
  margin-bottom: 1.5rem;
  font-size: 1rem;
}

#overlay-btn {
  padding: 0.7rem 2rem;
  border: none;
  border-radius: 10px;
  background: var(--primary);
  color: white;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

#overlay-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px var(--primary-glow);
}

/* High Scores */
#high-scores {
  margin-top: 1rem;
  padding: 0.75rem;
  background: var(--surface);
  border-radius: 12px;
}

#high-scores h3 {
  font-size: 0.8rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.4rem;
}

#streak-display {
  display: flex;
  justify-content: center;
  gap: 2rem;
  font-size: 0.9rem;
}

#streak-display strong {
  color: var(--win-color);
}

/* Board reset animation */
.board-reset .cell {
  animation: cellFadeOut 0.25s ease forwards;
}

@keyframes cellFadeOut {
  to { transform: scale(0.8); opacity: 0.3; }
}

/* Responsive */
@media (max-width: 420px) {
  :root {
    --cell-size: 80px;
    --board-gap: 5px;
  }

  #game-container { padding: 1rem; }
  h1 { font-size: 1.6rem; }
  #score-board { gap: 0.75rem; }
  .score-item { padding: 0.4rem 0.8rem; min-width: 65px; }
  .score-value { font-size: 1.2rem; }
  .cell { font-size: 2rem; }
}

@media (max-width: 340px) {
  :root {
    --cell-size: 70px;
    --board-gap: 4px;
  }
}

@media (max-height: 650px) {
  #game-container { padding: 0.75rem; }
  header { margin-bottom: 0.5rem; }
  #score-board { margin-bottom: 0.5rem; }
  #status-bar { margin-bottom: 0.5rem; }
  #high-scores { margin-top: 0.5rem; }
}
