:root {
  --bg-primary: #0D1117;
  --bg-secondary: #161B22;
  --bg-tertiary: #21262D;
  --text-primary: #F0F6FC;
  --text-secondary: #8B949E;
  --text-muted: #484F58;
  --accent: #3B82F6;
  --border: #30363D;
  --btn-bg: #21262D;
  --btn-active: #30363D;
  --overlay-bg: rgba(0, 0, 0, 0.85);
  --board-bg: #0A0E14;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg-primary: #FFFFFF;
    --bg-secondary: #F6F8FA;
    --bg-tertiary: #E9ECF0;
    --text-primary: #1B1E28;
    --text-secondary: #57606A;
    --text-muted: #8C959F;
    --accent: #3182F6;
    --border: #D0D7DE;
    --btn-bg: #E9ECF0;
    --btn-active: #D0D7DE;
    --overlay-bg: rgba(255, 255, 255, 0.9);
    --board-bg: #F0F2F5;
  }
}

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

html, body {
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Pretendard', 'Segoe UI', Roboto, sans-serif;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.app {
  width: 100%;
  max-width: 480px;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 24px 16px;
}

/* ──────────────────────────────────────
   OVERLAY SCREENS
────────────────────────────────────── */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
  background: var(--overlay-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.screen.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.screen-content {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
  padding: 0 24px;
}

/* ──────────────────────────────────────
   START SCREEN
────────────────────────────────────── */
.game-logo {
  font-size: 64px;
  line-height: 1;
  filter: drop-shadow(0 4px 16px rgba(59, 130, 246, 0.4));
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0px); }
  50% { transform: translateY(-8px); }
}

.game-title {
  font-size: 36px;
  font-weight: 800;
  letter-spacing: 4px;
  background: linear-gradient(135deg, #3B82F6 0%, #8B5CF6 50%, #EC4899 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-align: center;
}

.difficulty-section {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.section-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--text-muted);
  text-align: center;
}

.difficulty-buttons {
  display: flex;
  gap: 8px;
  width: 100%;
}

.diff-btn {
  flex: 1;
  padding: 10px 8px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  background: var(--btn-bg);
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.18s ease;
  text-align: center;
}

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

.diff-btn.active {
  border-color: var(--accent);
  background: rgba(59, 130, 246, 0.15);
  color: var(--accent);
}

/* ──────────────────────────────────────
   BUTTONS
────────────────────────────────────── */
.btn-primary {
  width: 100%;
  padding: 14px 24px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(135deg, #3B82F6, #8B5CF6);
  color: #fff;
  font-family: inherit;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s ease;
  letter-spacing: 0.5px;
  position: relative;
  overflow: hidden;
}

.btn-primary::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0);
  transition: background 0.2s ease;
}

.btn-primary:hover::after {
  background: rgba(255, 255, 255, 0.08);
}

.btn-primary:active {
  transform: scale(0.98);
}

.btn-secondary {
  padding: 10px 20px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  background: var(--btn-bg);
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.18s ease;
}

.btn-secondary:hover {
  border-color: var(--accent);
  color: var(--text-primary);
  background: var(--btn-active);
}

.btn-secondary:active {
  transform: scale(0.97);
}

/* ──────────────────────────────────────
   RESULT SCREEN
────────────────────────────────────── */
.result-emoji {
  font-size: 72px;
  line-height: 1;
  animation: popIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes popIn {
  from { transform: scale(0.3); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.result-title {
  font-size: 28px;
  font-weight: 800;
  letter-spacing: 2px;
  text-align: center;
}

.result-title.win {
  background: linear-gradient(135deg, #3B82F6, #8B5CF6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.result-title.lose {
  background: linear-gradient(135deg, #EC4899, #EF4444);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.result-title.draw {
  color: var(--text-secondary);
}

.result-subtitle {
  font-size: 14px;
  color: var(--text-secondary);
  text-align: center;
}

.result-buttons {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ──────────────────────────────────────
   GAME SCREEN (Main)
────────────────────────────────────── */
.game-header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header-title {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 2px;
  background: linear-gradient(135deg, #3B82F6, #8B5CF6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-diff {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 1px;
  color: var(--text-muted);
  text-transform: uppercase;
}

/* ──────────────────────────────────────
   SCORE BOARD
────────────────────────────────────── */
.scoreboard {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 8px;
  align-items: center;
  margin-bottom: 20px;
  padding: 16px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 16px;
}

.score-player {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.score-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--text-muted);
}

.score-name {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-secondary);
}

.score-value {
  font-size: 32px;
  font-weight: 800;
  line-height: 1;
}

.score-value.x-color {
  color: #3B82F6;
}

.score-value.o-color {
  color: #EC4899;
}

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

.score-divider {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.score-draw-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--text-muted);
}

.score-draw-value {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-muted);
}

/* ──────────────────────────────────────
   STATUS BAR
────────────────────────────────────── */
.status-bar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  margin-bottom: 16px;
}

.status-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  transition: all 0.3s ease;
  flex-shrink: 0;
}

.status-indicator.player-turn {
  background: #3B82F6;
  box-shadow: 0 0 8px rgba(59, 130, 246, 0.6);
  animation: pulse-blue 1.5s ease-in-out infinite;
}

.status-indicator.ai-turn {
  background: #EC4899;
  box-shadow: 0 0 8px rgba(236, 72, 153, 0.6);
  animation: pulse-pink 1.5s ease-in-out infinite;
}

@keyframes pulse-blue {
  0%, 100% { box-shadow: 0 0 4px rgba(59, 130, 246, 0.4); }
  50% { box-shadow: 0 0 12px rgba(59, 130, 246, 0.8); }
}

@keyframes pulse-pink {
  0%, 100% { box-shadow: 0 0 4px rgba(236, 72, 153, 0.4); }
  50% { box-shadow: 0 0 12px rgba(236, 72, 153, 0.8); }
}

.status-text {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  transition: color 0.3s ease;
}

.status-text.player-turn {
  color: #3B82F6;
}

.status-text.ai-turn {
  color: #EC4899;
}

/* ──────────────────────────────────────
   BOARD
────────────────────────────────────── */
.board-wrapper {
  width: 100%;
  aspect-ratio: 1;
  position: relative;
  margin-bottom: 20px;
}

.board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  background: var(--board-bg);
  border: 1.5px solid var(--border);
  border-radius: 20px;
  overflow: hidden;
  position: relative;
}

.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 52px;
  font-weight: 900;
  position: relative;
  transition: background 0.15s ease;
  user-select: none;
  -webkit-user-select: none;
}

.cell::before {
  content: '';
  position: absolute;
  inset: 0;
  background: transparent;
  transition: background 0.15s ease;
}

.cell:hover:not(.taken)::before {
  background: rgba(59, 130, 246, 0.06);
}

.cell:active:not(.taken)::before {
  background: rgba(59, 130, 246, 0.12);
}

/* Grid lines */
.cell:not(:nth-child(3n)) {
  border-right: 1.5px solid var(--border);
}

.cell:not(:nth-child(n+7)) {
  border-bottom: 1.5px solid var(--border);
}

.cell.taken {
  cursor: default;
}

/* X mark */
.cell-mark {
  position: relative;
  z-index: 1;
  line-height: 1;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s ease;
  transform: scale(0);
  opacity: 0;
}

.cell-mark.visible {
  transform: scale(1);
  opacity: 1;
}

.cell-mark.x-mark {
  color: #3B82F6;
  text-shadow: 0 0 20px rgba(59, 130, 246, 0.5);
}

.cell-mark.o-mark {
  color: #EC4899;
  text-shadow: 0 0 20px rgba(236, 72, 153, 0.5);
}

/* Winning cells highlight */
.cell.winning {
  background: rgba(59, 130, 246, 0.08);
}

.cell.winning .cell-mark.o-mark {
  text-shadow: 0 0 30px rgba(236, 72, 153, 0.8);
}

.cell.winning .cell-mark.x-mark {
  text-shadow: 0 0 30px rgba(59, 130, 246, 0.8);
}

/* Win line SVG overlay */
.win-line-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

.win-line-svg {
  width: 100%;
  height: 100%;
}

.win-line-path {
  stroke-width: 6;
  stroke-linecap: round;
  stroke-dasharray: 600;
  stroke-dashoffset: 600;
  fill: none;
  filter: drop-shadow(0 0 8px currentColor);
  animation: drawLine 0.5s ease forwards 0.1s;
}

.win-line-path.x-line {
  stroke: #3B82F6;
  color: #3B82F6;
}

.win-line-path.o-line {
  stroke: #EC4899;
  color: #EC4899;
}

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

/* ──────────────────────────────────────
   BOTTOM CONTROLS
────────────────────────────────────── */
.game-controls {
  width: 100%;
  display: flex;
  gap: 10px;
  justify-content: center;
}

.btn-icon {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 18px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  background: var(--btn-bg);
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.18s ease;
}

.btn-icon:hover {
  border-color: var(--accent);
  color: var(--text-primary);
  background: var(--btn-active);
}

.btn-icon:active {
  transform: scale(0.97);
}

.btn-icon svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

/* ──────────────────────────────────────
   AI THINKING INDICATOR
────────────────────────────────────── */
.thinking-dots {
  display: inline-flex;
  gap: 3px;
  align-items: center;
}

.thinking-dots span {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: #EC4899;
  animation: thinkDot 1.2s ease-in-out infinite;
}

.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes thinkDot {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* ──────────────────────────────────────
   RESPONSIVE
────────────────────────────────────── */
@media (max-width: 380px) {
  .cell {
    font-size: 40px;
  }

  .score-value {
    font-size: 26px;
  }

  .game-title {
    font-size: 28px;
  }
}

@media (max-height: 700px) {
  .app {
    justify-content: flex-start;
    padding-top: 16px;
    padding-bottom: 16px;
  }

  .game-header {
    margin-bottom: 12px;
  }

  .scoreboard {
    margin-bottom: 12px;
    padding: 12px;
  }

  .status-bar {
    margin-bottom: 10px;
    min-height: 32px;
  }

  .board-wrapper {
    margin-bottom: 14px;
  }
}
