/* ========= Variables ========= */
:root {
  --bg: #0b0f16;
  --bg2: #070a10;
  --card: #101622;
  --text: #ffffff;
  --muted: #9aa4b2;
  --x: #2dd4ff;
  --o: #ff4d4d;
  --ring: rgba(255,255,255,0.18);
}

body[data-theme="light"] {
  --bg: #e8ecf4;
  --bg2: #dfe4ef;
  --card: rgba(255,255,255,0.75);
  --text: #2d3748;
  --muted: #5a6578;
  --ring: rgba(0,0,0,0.1);
}

* { box-sizing: border-box; font-family: system-ui, sans-serif; }

/* ========= Background (gradient mix, both themes) ========= */
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  color: var(--text);
  background: var(--bg);
  background-image:
    radial-gradient(ellipse 120% 80% at 20% 10%, rgba(45, 212, 255, 0.12) 0%, transparent 50%),
    radial-gradient(ellipse 80% 100% at 80% 90%, rgba(255, 77, 77, 0.1) 0%, transparent 50%),
    radial-gradient(ellipse 60% 60% at 50% 50%, rgba(255,255,255,0.04) 0%, transparent 60%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg2) 100%);
}

body[data-theme="light"] {
  background-image:
    radial-gradient(ellipse 120% 80% at 20% 10%, rgba(45, 180, 220, 0.08) 0%, transparent 50%),
    radial-gradient(ellipse 80% 100% at 80% 90%, rgba(220, 90, 90, 0.06) 0%, transparent 50%),
    radial-gradient(ellipse 60% 60% at 50% 50%, rgba(0,0,0,0.015) 0%, transparent 60%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg2) 100%);
}

/* ========= Accessibility ========= */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

/* ========= Theme toggle ========= */
.theme-toggle {
  position: fixed;
  top: 12px;
  right: 12px;
  width: 44px;
  height: 44px;
  border-radius: 14px;
  border: 1px solid var(--ring);
  background: rgba(255,255,255,0.06);
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  z-index: 10;
}

/* ========= Screens ========= */
.screen {
  display: none;
  width: 100%;
  max-width: 420px;
  padding: 24px;
  text-align: center;
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 0.3s ease, transform 0.3s ease;
}
.screen.active {
  display: block;
}
.screen.active:not(.fade-out) {
  opacity: 1;
  transform: translateY(0);
}
.screen.fade-in {
  opacity: 1;
  transform: translateY(0);
}
.screen.fade-out {
  opacity: 0;
  transform: translateY(-10px);
}

@media (prefers-reduced-motion: reduce) {
  .screen {
    transition: none;
  }
}

.title { margin: 0 0 10px; font-size: 28px; }
.muted { color: var(--muted); }
.small { display: block; margin-top: 12px; }

/* ========= Welcome (interactive, not plain) ========= */
#welcomeScreen {
  position: relative;
  overflow: hidden;
}

.welcome-content {
  position: relative;
  z-index: 1;
}

.welcome-title {
  font-size: 32px;
  margin-bottom: 6px;
  animation: welcomeFadeIn 0.6s ease-out;
}

.welcome-subtitle {
  font-size: 16px;
  margin-bottom: 24px;
  animation: welcomeFadeIn 0.6s ease-out 0.1s both;
}

.welcome-logo {
  display: grid;
  place-items: center;
  margin: 28px 0 24px;
  animation: welcomeFadeIn 0.6s ease-out 0.2s both;
}

.logo-box {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 8px;
  width: 140px;
  height: 140px;
  padding: 12px;
  border-radius: 24px;
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--ring);
  animation: logoPulse 3s ease-in-out infinite;
}

.logo-cell {
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: 36px;
  border-radius: 14px;
  transition: transform 0.2s ease;
}
.logo-cell.x { color: var(--x); }
.logo-cell.o { color: var(--o); }
.logo-cell.tl { grid-column: 1; grid-row: 1; }
.logo-cell.tr { grid-column: 2; grid-row: 1; }
.logo-cell.bl { grid-column: 1; grid-row: 2; }
.logo-cell.br { grid-column: 2; grid-row: 2; }

.btn-cta {
  animation: welcomeFadeIn 0.6s ease-out 0.3s both;
  min-height: 48px;
  font-size: 17px;
}

.welcome-bg-shapes {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background:
    radial-gradient(circle at 10% 20%, rgba(45,212,255,0.08) 0%, transparent 40%),
    radial-gradient(circle at 90% 80%, rgba(255,77,77,0.06) 0%, transparent 40%);
}

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

@keyframes logoPulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 rgba(0,0,0,0); }
  50% { transform: scale(1.03); box-shadow: 0 8px 32px rgba(0,0,0,0.15); }
}

/* ========= Loading ========= */
.loader {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 4px solid var(--ring);
  border-top-color: var(--text);
  margin: 22px auto 14px;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.progress-bar-container {
  width: 200px;
  height: 4px;
  background: rgba(255,255,255,0.1);
  border-radius: 2px;
  margin: 16px auto;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--x), var(--o));
  border-radius: 2px;
  transition: width 0.2s ease;
}

/* ========= Buttons ========= */
.btn {
  width: 100%;
  padding: 14px;
  margin-top: 12px;
  border: none;
  border-radius: 14px;
  font-weight: 800;
  cursor: pointer;
  background: var(--text);
  color: var(--bg);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  position: relative;
  overflow: hidden;
}
.btn:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0,0,0,0.15); }
.btn:focus-visible { outline: 2px solid var(--x); outline-offset: 2px; }

.ripple {
  position: absolute;
  border-radius: 50%;
  background: rgba(255,255,255,0.4);
  transform: scale(0);
  animation: rippleExpand 0.6s ease-out;
  pointer-events: none;
}
@keyframes rippleExpand {
  to {
    transform: scale(4);
    opacity: 0;
  }
}

.btn-secondary {
  background: rgba(255,255,255,0.12);
  color: var(--text);
  border: 1px solid var(--ring);
}

.hidden { display: none !important; }

/* ========= Mode Screen (interactive) ========= */
#modeScreen {
  position: relative;
  overflow: hidden;
}

.mode-content {
  position: relative;
  z-index: 1;
}

.mode-icon {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
  animation: modeFadeIn 0.5s ease-out;
}

.mode-vs {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--x), var(--o));
  font-size: 24px;
  font-weight: 900;
  color: white;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
  animation: vsPulse 2s ease-in-out infinite;
}

@keyframes vsPulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(45, 212, 255, 0.4); }
  50% { transform: scale(1.05); box-shadow: 0 0 20px 5px rgba(255, 77, 77, 0.3); }
}

.mode-title {
  font-size: 28px;
  margin-bottom: 8px;
  animation: modeFadeIn 0.5s ease-out 0.1s both;
}

.mode-subtitle {
  font-size: 16px;
  margin-bottom: 24px;
  animation: modeFadeIn 0.5s ease-out 0.2s both;
}

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

.mode-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  animation: modeFadeIn 0.5s ease-out 0.3s both;
}

.mode-btn {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 20px;
  text-align: left;
  margin-top: 0 !important;
}

.mode-btn .btn-icon {
  font-size: 28px;
  flex-shrink: 0;
}

.mode-btn .btn-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.mode-btn .btn-text strong {
  font-size: 16px;
}

.mode-btn .btn-text small {
  font-size: 12px;
  opacity: 0.7;
  font-weight: 400;
}

#aiDifficultyPanel {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  animation: modeFadeIn 0.3s ease-out;
}

.difficulty-prompt {
  font-size: 14px;
  margin-bottom: 8px;
}

.difficulty-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 14px 16px;
  gap: 4px;
}

.difficulty-label {
  font-size: 16px;
  font-weight: 700;
}

.difficulty-desc {
  font-size: 11px;
  opacity: 0.7;
  font-weight: 400;
}

.privacy-note {
  animation: modeFadeIn 0.5s ease-out 0.4s both;
}

.mode-bg-shapes {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background:
    radial-gradient(circle at 85% 15%, rgba(45,212,255,0.08) 0%, transparent 35%),
    radial-gradient(circle at 15% 85%, rgba(255,77,77,0.06) 0%, transparent 35%);
}

/* ========= Game Screen (interactive) ========= */
#gameScreen {
  position: relative;
  overflow: hidden;
}

.game-content {
  position: relative;
  z-index: 1;
}

.game-header {
  margin-bottom: 16px;
  animation: gameFadeIn 0.4s ease-out;
}

.turn-display {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 12px 20px;
  background: rgba(255,255,255,0.06);
  border-radius: 16px;
  border: 1px solid var(--ring);
}

.turn-icon {
  font-size: 32px;
  animation: turnIconPulse 1.5s ease-in-out infinite;
}

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

.turn-info {
  display: flex;
  flex-direction: column;
  text-align: left;
}

.turn {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  transition: opacity 0.15s ease;
}

.difficulty-tag {
  font-size: 12px;
  margin: 2px 0 0;
}

.turn-fade-out {
  opacity: 0;
}
.turn-fade-in {
  animation: turnFadeIn 0.2s ease;
}
@keyframes turnFadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}
.turn.ai-thinking {
  opacity: 0.7;
  animation: thinkingPulse 1s ease-in-out infinite;
}

@keyframes gameFadeIn {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Score Panel */
.score-panel {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 16px;
  animation: gameFadeIn 0.4s ease-out 0.1s both;
}

.score-card {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: rgba(255,255,255,0.06);
  border-radius: 12px;
  border: 1px solid var(--ring);
  transition: transform 0.2s ease, background 0.2s ease, border-color 0.2s ease;
}

.score-card.active {
  background: rgba(255,255,255,0.12);
  border-color: rgba(255,255,255,0.3);
  transform: translateY(-2px);
}

.score-card .score-emoji {
  font-size: 20px;
}

.score-details {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.score-name {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.score-value {
  font-size: 20px;
  font-weight: 800;
  line-height: 1.1;
}

.score-card[data-player="X"] .score-value { color: var(--x); }
.score-card[data-player="O"] .score-value { color: var(--o); }
.score-card.score-draw .score-value { color: var(--muted); }

.score-card.score-updated {
  animation: scoreCardBounce 0.4s ease;
}

@keyframes scoreCardBounce {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08) translateY(-3px); }
}

.game-bg-shapes {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background:
    radial-gradient(circle at 10% 90%, rgba(45,212,255,0.06) 0%, transparent 40%),
    radial-gradient(circle at 90% 10%, rgba(255,77,77,0.05) 0%, transparent 40%);
}
@keyframes thinkingPulse {
  0%, 100% { opacity: 0.7; }
  50% { opacity: 1; }
}

.session-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin: 0 0 4px;
}

.mini-score {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 14px;
  background: rgba(255,255,255,0.08);
  border: 1px solid var(--ring);
  color: var(--muted);
}

.score-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 10px;
  transition: background 0.2s ease, color 0.2s ease;
}
.score-item.active {
  background: rgba(255,255,255,0.14);
  color: var(--text);
  font-weight: 700;
}
.score-item.score-updated {
  animation: scoreBounce 0.4s ease;
}
@keyframes scoreBounce {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.15); }
}

#difficultyLabel,
#loadingDifficulty {
  font-size: 14px;
  opacity: 0.85;
  margin: 0;
}

/* ========= Board ========= */
.board-container {
  position: relative;
  margin-top: 14px;
  width: 100%;
  aspect-ratio: 1;
  padding: 16px;
  background: rgba(255,255,255,0.08);
  border: 1px solid var(--ring);
  border-radius: 20px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.06);
  animation: boardFadeIn 0.5s ease-out 0.2s both;
}

body[data-theme="light"] .board-container {
  background: rgba(255,255,255,0.5);
  border: 1px solid rgba(0,0,0,0.12);
  box-shadow: 0 4px 24px rgba(0,0,0,0.08);
}

@keyframes boardFadeIn {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  width: 100%;
  height: 100%;
  position: relative;
}

.win-line {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.win-line.visible {
  opacity: 1;
}

.win-line line {
  stroke-dasharray: 1000;
  stroke-dashoffset: 1000;
  animation: drawLine 0.6s ease-out forwards;
  filter: drop-shadow(0 0 4px currentColor);
}

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

@media (prefers-reduced-motion: reduce) {
  .win-line line {
    animation: none;
    stroke-dasharray: none;
    stroke-dashoffset: 0;
  }
}

.cell {
  background: var(--card);
  border: 1px solid var(--ring);
  border-radius: 18px;
  display: grid;
  place-items: center;
  font-size: clamp(46px, 10vw, 72px);
  font-weight: 900;
  cursor: pointer;
  user-select: none;
  transition: transform 0.12s ease, background 0.12s ease;
  position: relative;
}
.cell:hover { transform: translateY(-2px); background: rgba(255,255,255,0.10); }
.cell:focus-visible { outline: 2px solid var(--x); outline-offset: 2px; }
.cell.disabled { pointer-events: none; opacity: 0.95; }
.cell.x { color: var(--x); }
.cell.o { color: var(--o); }

/* Hover preview on empty cells */
.cell.hover-preview::before {
  content: attr(data-preview);
  position: absolute;
  font-size: clamp(46px, 10vw, 72px);
  font-weight: 900;
  opacity: 0.3;
  pointer-events: none;
  z-index: 1;
}
.cell.hover-preview[data-preview="X"]::before {
  color: var(--x);
}
.cell.hover-preview[data-preview="O"]::before {
  color: var(--o);
}

/* Board reset animation */
.cell-reset {
  animation: cellReset 0.3s ease-out forwards;
}
@keyframes cellReset {
  to {
    transform: scale(0);
    opacity: 0;
  }
}

/* Draw state visual feedback */
.cell.draw-cell {
  border-color: var(--muted);
  background: rgba(255,255,255,0.05);
  opacity: 0.85;
}

/* Move confirmation (brief scale-in) */
.cell.placed {
  animation: placed 0.3s ease-out;
}
@keyframes placed {
  0% { transform: scale(0.85); opacity: 0.7; }
  70% { transform: scale(1.08); }
  100% { transform: scale(1); opacity: 1; }
}

/* Winning line highlight */
.cell.win {
  animation: winPulse 600ms ease-in-out infinite alternate;
  border-color: rgba(255,255,255,0.5);
}
@keyframes winPulse {
  from {
    transform: scale(1);
    box-shadow: 0 0 0 rgba(0,0,0,0);
  }
  to {
    transform: scale(1.06);
    box-shadow: 0 0 26px rgba(255,255,255,0.25);
  }
}

/* ========= Result Screen (interactive) ========= */
#resultScreen {
  position: relative;
  overflow: hidden;
}

.result-content {
  position: relative;
  z-index: 1;
}

.result-trophy {
  font-size: 64px;
  margin-bottom: 12px;
  animation: trophyBounce 0.8s ease-out, trophyGlow 2s ease-in-out infinite 0.8s;
}

@keyframes trophyBounce {
  0% { transform: scale(0) rotate(-20deg); opacity: 0; }
  50% { transform: scale(1.2) rotate(10deg); }
  70% { transform: scale(0.9) rotate(-5deg); }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

@keyframes trophyGlow {
  0%, 100% { filter: drop-shadow(0 0 8px rgba(255,215,0,0.4)); }
  50% { filter: drop-shadow(0 0 20px rgba(255,215,0,0.7)); }
}

.result-trophy.draw {
  animation: trophyBounce 0.8s ease-out;
}

.result-title {
  font-size: 32px;
  font-weight: 800;
  margin: 0 0 8px;
  animation: resultFadeIn 0.5s ease-out 0.3s both;
}

.result-title.x-wins { color: var(--x); }
.result-title.o-wins { color: var(--o); }
.result-title.draw { color: var(--muted); }

.result-subtitle {
  font-size: 16px;
  margin: 0 0 24px;
  animation: resultFadeIn 0.5s ease-out 0.4s both;
}

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

.result-stats {
  animation: resultFadeIn 0.5s ease-out 0.5s both;
}

.stats-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin: 0 0 12px;
}

.stats-grid {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-bottom: 24px;
}

.stat-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  background: rgba(255,255,255,0.06);
  border-radius: 14px;
  border: 1px solid var(--ring);
  transition: transform 0.2s ease, background 0.2s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  background: rgba(255,255,255,0.1);
}

.stat-icon {
  font-size: 24px;
}

.stat-info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.stat-value {
  font-size: 24px;
  font-weight: 800;
  line-height: 1.1;
}

.stat-name {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-x .stat-value { color: var(--x); }
.stat-o .stat-value { color: var(--o); }
.stat-draw .stat-value { color: var(--muted); }

.stat-card.winner {
  background: rgba(255,255,255,0.12);
  border-color: rgba(255,255,255,0.25);
  animation: winnerPulse 1.5s ease-in-out infinite;
}

@keyframes winnerPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255,255,255,0.1); }
  50% { box-shadow: 0 4px 20px rgba(255,255,255,0.15); }
}

.result-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  animation: resultFadeIn 0.5s ease-out 0.6s both;
}

.result-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.result-btn .btn-icon {
  font-size: 18px;
}

.result-btn.primary-action {
  background: linear-gradient(135deg, var(--x), var(--o));
  color: white;
  font-weight: 700;
}

.result-btn.primary-action:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 24px rgba(45, 212, 255, 0.3);
}

.result-bg-shapes {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background:
    radial-gradient(circle at 20% 20%, rgba(45,212,255,0.08) 0%, transparent 40%),
    radial-gradient(circle at 80% 80%, rgba(255,77,77,0.06) 0%, transparent 40%);
}

/* Legacy support */
.score-item-result {
  margin: 8px 0;
  transition: transform 0.2s ease;
}
.score-item-result.score-updated {
  animation: scoreBounce 0.4s ease;
}

@media (prefers-reduced-motion: reduce) {
  .result-celebration,
  .cell-reset,
  .score-item.score-updated,
  .turn-fade-in,
  .turn.ai-thinking {
    animation: none;
  }
}