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

:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --cell-bg: #0f3460;
  --cell-hover: #1a4a7a;
  --x-color: #e94560;
  --o-color: #00d2ff;
  --text: #eee;
  --text-muted: #8899aa;
  --win-glow: #f5c518;
  --shadow-subtle: rgba(255, 255, 255, 0.08);
  --shadow-active: rgba(0, 0, 0, 0.3);
  --cell-size: min(24vw, 120px);
  --gap: 8px;
  --radius: 12px;
}

/* Light theme via system preference */
@media (prefers-color-scheme: light) {
  :root:not([data-theme='dark']) {
    --bg: #f0f2f5;
    --surface: #dfe3ea;
    --cell-bg: #c8d0dc;
    --cell-hover: #b4bfce;
    --x-color: #c8243a;
    --o-color: #0090b3;
    --text: #1a1a2e;
    --text-muted: #4a5568;
    --win-glow: #d4a800;
    --shadow-subtle: rgba(0, 0, 0, 0.06);
    --shadow-active: rgba(0, 0, 0, 0.15);
  }
}

/* Explicit light theme override */
[data-theme='light'] {
  --bg: #f0f2f5;
  --surface: #dfe3ea;
  --cell-bg: #c8d0dc;
  --cell-hover: #b4bfce;
  --x-color: #c8243a;
  --o-color: #0090b3;
  --text: #1a1a2e;
  --text-muted: #4a5568;
  --win-glow: #d4a800;
  --shadow-subtle: rgba(0, 0, 0, 0.06);
  --shadow-active: rgba(0, 0, 0, 0.15);
}

body {
  font-family:
    'Segoe UI',
    system-ui,
    -apple-system,
    sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* --- Layout --- */
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 24px;
  position: relative;
}

.title {
  font-size: 2.8rem;
  font-weight: 800;
  letter-spacing: 0.15em;
  background: linear-gradient(135deg, var(--x-color), var(--o-color));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: fadeInDown 0.6s ease-out;
}

/* --- Status --- */
.status {
  font-size: 1.15rem;
  font-weight: 600;
  min-height: 1.6em;
  transition: opacity 0.3s;
}

.status.status-win {
  color: var(--win-glow);
}
.status.status-draw {
  color: var(--text-muted);
}

/* --- Board --- */
.board {
  display: grid;
  grid-template-columns: repeat(3, var(--cell-size));
  gap: var(--gap);
  position: relative;
}

.board-row {
  display: contents;
}

/* --- Cell --- */
.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: var(--cell-bg);
  border: none;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-family: inherit;
  font-size: calc(var(--cell-size) * 0.55);
  font-weight: 700;
  color: var(--text);
  user-select: none;
  position: relative;
  transition:
    background 0.2s,
    transform 0.15s,
    box-shadow 0.2s;
}

.cell:focus-visible {
  outline: 3px solid var(--o-color);
  outline-offset: 2px;
}

.cell:hover:not(.taken):not(.game-over) {
  background: var(--cell-hover);
  transform: scale(1.05);
  box-shadow: 0 0 16px var(--shadow-subtle);
}

.cell:hover:not(.taken):not(.game-over)::after {
  content: var(--hover-mark, 'X');
  color: var(--hover-color, rgba(233, 69, 96, 0.2));
  font-size: calc(var(--cell-size) * 0.55);
  font-weight: 700;
  position: absolute;
  animation: fadeIn 0.15s ease-out;
}

.cell.placed {
  animation: cellBounce 0.3s ease-out;
}

.cell.taken {
  cursor: default;
}

.cell.game-over {
  cursor: default;
}

/* --- X & O marks --- */
.cell .mark {
  display: inline-block;
  animation: markPlace 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.cell .mark.x {
  color: var(--x-color);
  filter: drop-shadow(0 0 0 transparent);
  animation:
    markPlace 0.35s cubic-bezier(0.34, 1.56, 0.64, 1),
    xGlow 0.35s ease-out;
}

.cell .mark.o {
  color: var(--o-color);
  filter: drop-shadow(0 0 0 transparent);
  animation:
    markPlace 0.35s cubic-bezier(0.34, 1.56, 0.64, 1),
    oGlow 0.35s ease-out;
}

/* --- Winning cells --- */
.cell.winner {
  animation: winPulse 0.6s ease-in-out 3;
  box-shadow:
    0 0 20px var(--win-glow),
    0 0 40px rgba(245, 197, 24, 0.3);
  z-index: 2;
}

.cell.winner:nth-child(1) {
  animation-delay: 0s;
}
.cell.winner:nth-child(2) {
  animation-delay: 0.08s;
}
.cell.winner:nth-child(3) {
  animation-delay: 0.16s;
}

/* --- Winning line overlay --- */
.winning-line-svg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 10;
}

.winning-line-svg line {
  stroke: var(--win-glow);
  stroke-width: 4;
  stroke-linecap: round;
  stroke-dasharray: 500;
  stroke-dashoffset: 500;
  filter: drop-shadow(0 0 6px var(--win-glow));
}

.winning-line-svg line.animate {
  animation:
    drawLine 0.5s ease-out forwards,
    lineGlow 1.2s ease-in-out 0.5s infinite alternate;
}

/* --- Score Board --- */
.score-board {
  display: flex;
  gap: 24px;
}

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

.score-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.score-value {
  font-size: 1.6rem;
  font-weight: 700;
  transition: transform 0.3s;
}

.score-value.bump {
  animation: scoreBump 0.4s ease-out;
}

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

/* --- Reset Scores Button --- */
.btn-reset-scores {
  padding: 4px 12px;
  font-size: 0.75rem;
  font-weight: 500;
  border: 1px solid var(--text-muted);
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition:
    color 0.2s,
    border-color 0.2s;
}

.btn-reset-scores:hover {
  color: var(--x-color);
  border-color: var(--x-color);
}

.btn-reset-scores:focus-visible {
  outline: 3px solid var(--o-color);
  outline-offset: 2px;
}

/* --- Difficulty Selector --- */
.difficulty-selector {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  border: none;
  padding: 0;
}

.difficulty-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.difficulty-buttons {
  display: flex;
  gap: 4px;
  background: var(--surface);
  border-radius: 8px;
  padding: 3px;
}

.difficulty-btn {
  padding: 6px 16px;
  font-size: 0.85rem;
  font-weight: 600;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition:
    background 0.2s,
    color 0.2s;
}

.difficulty-btn:hover {
  color: var(--text);
}

.difficulty-btn.active {
  background: var(--cell-bg);
  color: var(--text);
  box-shadow: 0 1px 4px var(--shadow-active);
}

/* --- Restart Button --- */
.btn-restart {
  padding: 10px 28px;
  font-size: 1rem;
  font-weight: 600;
  border: 2px solid var(--text-muted);
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  transition:
    background 0.2s,
    border-color 0.2s,
    transform 0.15s;
}

.btn-restart:hover {
  background: var(--shadow-subtle);
  border-color: var(--text);
  transform: scale(1.04);
}

/* --- Sound Toggle --- */
.sound-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}

.sound-toggle input {
  accent-color: var(--o-color);
}

/* --- Theme Toggle --- */
.theme-toggle {
  position: fixed;
  top: 12px;
  right: 12px;
  width: 36px;
  height: 36px;
  border: 2px solid var(--text-muted);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  z-index: 100;
  transition:
    background 0.2s,
    border-color 0.2s,
    transform 0.15s;
}

.theme-toggle:hover {
  border-color: var(--text);
  transform: scale(1.1);
}

.theme-toggle:focus-visible {
  outline: 3px solid var(--o-color);
  outline-offset: 2px;
}

/* --- Skip to content link --- */
.skip-link {
  position: absolute;
  top: -100%;
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 16px;
  font-size: 0.9rem;
  font-weight: 600;
  background: var(--surface);
  color: var(--text);
  border: 2px solid var(--o-color);
  border-radius: 8px;
  text-decoration: none;
  z-index: 200;
  transition: top 0.2s;
}

.skip-link:focus {
  top: 12px;
  outline: 3px solid var(--o-color);
  outline-offset: 2px;
}

/* --- Screen reader only --- */
.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;
}

/* --- Keyboard shortcut hints --- */
.shortcut-hint {
  font-size: 0.7rem;
  font-weight: 500;
  font-family: inherit;
  color: var(--text-muted);
  background: var(--surface);
  padding: 1px 5px;
  border-radius: 3px;
  margin-left: 4px;
  vertical-align: middle;
  opacity: 0.7;
}

/* --- Hidden utility --- */
.hidden {
  display: none !important;
}

/* --- Start Screen --- */
.start-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
  padding: 40px 32px;
  animation: fadeIn 0.4s ease-out;
}

.start-title {
  font-size: 3.5rem;
  font-weight: 800;
  letter-spacing: 0.15em;
  background: linear-gradient(135deg, var(--x-color), var(--o-color));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.start-subtitle {
  font-size: 1rem;
  color: var(--text-muted);
  margin-top: -16px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.start-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.start-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.start-difficulty {
  display: flex;
  gap: 4px;
  background: var(--surface);
  border-radius: 8px;
  padding: 3px;
}

.start-diff-btn {
  padding: 8px 20px;
  font-size: 0.9rem;
  font-weight: 600;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition:
    background 0.2s,
    color 0.2s;
}

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

.start-diff-btn.active {
  background: var(--cell-bg);
  color: var(--text);
  box-shadow: 0 1px 4px var(--shadow-active);
}

.start-diff-btn:focus-visible {
  outline: 3px solid var(--o-color);
  outline-offset: 2px;
}

.start-side {
  display: flex;
  gap: 12px;
}

.start-side-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 16px 28px;
  border: 2px solid var(--surface);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
  transition:
    border-color 0.2s,
    background 0.2s,
    transform 0.15s;
}

.start-side-btn:hover {
  border-color: var(--text-muted);
  transform: scale(1.04);
}

.start-side-btn.active {
  border-color: var(--o-color);
  background: var(--cell-bg);
}

.start-side-btn:focus-visible {
  outline: 3px solid var(--o-color);
  outline-offset: 2px;
}

.side-mark {
  font-size: 2rem;
  font-weight: 800;
}

.side-x {
  color: var(--x-color);
}

.side-o {
  color: var(--o-color);
}

.side-desc {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.start-play-btn {
  padding: 12px 48px;
  font-size: 1.1rem;
  font-weight: 700;
  border: none;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--x-color), var(--o-color));
  color: #fff;
  cursor: pointer;
  transition:
    transform 0.15s,
    box-shadow 0.2s;
  margin-top: 8px;
}

.start-play-btn:hover {
  transform: scale(1.06);
  box-shadow: 0 4px 20px rgba(233, 69, 96, 0.4);
}

.start-play-btn:focus-visible {
  outline: 3px solid var(--text);
  outline-offset: 2px;
}

/* --- Change Settings Button --- */
.btn-change-settings {
  padding: 6px 16px;
  font-size: 0.8rem;
  font-weight: 500;
  border: 1px solid var(--text-muted);
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition:
    color 0.2s,
    border-color 0.2s;
}

.btn-change-settings:hover {
  color: var(--text);
  border-color: var(--text);
}

.btn-change-settings:focus-visible {
  outline: 3px solid var(--o-color);
  outline-offset: 2px;
}

/* --- Game Actions Row --- */
.game-actions {
  display: flex;
  gap: 12px;
  align-items: center;
}

/* --- Undo Button --- */
.btn-undo {
  padding: 10px 28px;
  font-size: 1rem;
  font-weight: 600;
  border: 2px solid var(--text-muted);
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  transition:
    background 0.2s,
    border-color 0.2s,
    transform 0.15s,
    opacity 0.2s;
}

.btn-undo:hover:not(:disabled) {
  background: var(--shadow-subtle);
  border-color: var(--text);
  transform: scale(1.04);
}

.btn-undo:disabled {
  opacity: 0.35;
  cursor: default;
}

.btn-undo:focus-visible {
  outline: 3px solid var(--o-color);
  outline-offset: 2px;
}

/* --- Game History --- */
.game-history {
  width: 100%;
  max-width: calc(var(--cell-size) * 3 + var(--gap) * 2);
}

.history-toggle {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  text-align: center;
  padding: 6px;
  list-style: none;
  user-select: none;
  transition: color 0.2s;
}

.history-toggle::-webkit-details-marker {
  display: none;
}

.history-toggle::before {
  content: '\25B6  ';
  font-size: 0.65rem;
  vertical-align: middle;
}

.game-history[open] .history-toggle::before {
  content: '\25BC  ';
}

.history-toggle:hover {
  color: var(--text);
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 180px;
  overflow-y: auto;
  padding: 8px 0;
}

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 10px;
  border-radius: 6px;
  background: var(--surface);
  font-size: 0.8rem;
  color: var(--text-muted);
}

.history-result {
  font-weight: 700;
}

.history-result.win {
  color: var(--win-glow);
}

.history-result.loss {
  color: var(--x-color);
}

.history-result.draw {
  color: var(--text-muted);
}

.history-empty {
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-muted);
  padding: 12px;
}

/* --- Animations --- */
@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

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

@keyframes markPlace {
  0% {
    transform: scale(0) rotate(-15deg);
    opacity: 0;
  }
  50% {
    transform: scale(1.15) rotate(3deg);
    opacity: 1;
  }
  75% {
    transform: scale(0.95) rotate(-1deg);
  }
  100% {
    transform: scale(1) rotate(0deg);
  }
}

@keyframes xGlow {
  0% {
    filter: drop-shadow(0 0 8px var(--x-color));
  }
  100% {
    filter: drop-shadow(0 0 0 transparent);
  }
}

@keyframes oGlow {
  0% {
    filter: drop-shadow(0 0 8px var(--o-color));
  }
  100% {
    filter: drop-shadow(0 0 0 transparent);
  }
}

@keyframes winPulse {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.08);
    box-shadow: 0 0 30px var(--win-glow);
  }
}

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

@keyframes lineGlow {
  0% {
    filter: drop-shadow(0 0 6px var(--win-glow));
    stroke-width: 4;
  }
  100% {
    filter: drop-shadow(0 0 14px var(--win-glow));
    stroke-width: 5;
  }
}

@keyframes cellBounce {
  0% {
    transform: scale(0.92);
  }
  50% {
    transform: scale(1.04);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes scoreBump {
  0% {
    transform: scale(1);
  }
  40% {
    transform: scale(1.35);
  }
  100% {
    transform: scale(1);
  }
}

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

.board.reset-anim {
  animation: boardFadeIn 0.35s ease-out;
}

/* --- Game-over fade on non-winning cells --- */
.cell.dimmed {
  opacity: 0.35;
  transition: opacity 0.5s;
}

/* --- Focus styles for interactive elements --- */
.btn-restart:focus-visible,
.difficulty-btn:focus-visible {
  outline: 3px solid var(--o-color);
  outline-offset: 2px;
}

/* --- Responsive --- */
@media (max-width: 480px) {
  :root {
    --cell-size: min(26vw, 100px);
    --gap: 6px;
  }

  .start-screen {
    padding: 32px 20px;
    gap: 22px;
  }

  .start-title {
    font-size: 2.8rem;
  }

  .start-side-btn {
    padding: 12px 20px;
  }

  .start-diff-btn {
    padding: 6px 14px;
    font-size: 0.85rem;
  }

  .container {
    gap: 16px;
    padding: 16px;
  }

  .title {
    font-size: 2.2rem;
  }

  .status {
    font-size: 1rem;
  }

  .score-board {
    gap: 16px;
  }

  .score-label {
    font-size: 0.7rem;
  }

  .score-value {
    font-size: 1.3rem;
  }

  .difficulty-btn {
    padding: 5px 12px;
    font-size: 0.8rem;
  }

  .btn-restart {
    padding: 8px 22px;
    font-size: 0.9rem;
  }
}

@media (max-width: 340px) {
  :root {
    --cell-size: min(28vw, 85px);
  }

  .title {
    font-size: 1.8rem;
  }
}

/* --- Landscape phones --- */
@media (max-height: 500px) and (orientation: landscape) {
  .container {
    gap: 10px;
    padding: 10px;
  }

  .title {
    font-size: 1.6rem;
  }

  :root {
    --cell-size: min(18vh, 80px);
  }
}

/* --- Reduced motion --- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .winning-line-svg line.animate {
    stroke-dashoffset: 0;
    animation: none !important;
  }
}
