:root {
  color-scheme: dark;

  --bg-dark: #0a0c16;
  --bg-card: rgba(22, 27, 46, 0.7);
  --bg-card-hover: rgba(30, 37, 62, 0.85);
  --border-subtle: rgba(255, 255, 255, 0.08);
  --border-active: rgba(255, 255, 255, 0.2);

  --color-x: #00f0ff;
  --glow-x: rgba(0, 240, 255, 0.5);
  --color-o: #ff2a85;
  --glow-o: rgba(255, 42, 133, 0.5);
  --color-draw: #ffb800;
  --glow-draw: rgba(255, 184, 0, 0.4);

  --text-main: #f0f4fc;
  --text-muted: #8b9bb4;
  --text-dim: #7a8aa6;

  --accent-purple: #7928ca;
  --accent-gradient: linear-gradient(135deg, #00f0ff, #7928ca, #ff2a85);

  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-pill: 9999px;

  --font-main: 'Outfit', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', monospace;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

button,
.cell,
.icon-btn,
.segment-btn,
.diff-btn,
.action-btn,
.mark-btn,
.order-btn {
  touch-action: manipulation;
}

/* Accessible focus rings */
button:focus-visible,
.cell:focus-visible,
[role="radio"]:focus-visible,
[role="tab"]:focus-visible {
  outline: 2px solid var(--color-x);
  outline-offset: 3px;
}

body {
  background-color: var(--bg-dark);
  color: var(--text-main);
  font-family: var(--font-main);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow-x: hidden;
  user-select: none;
  padding: 16px;
}

/* Background Ambient Glows */
.ambient-glow {
  position: fixed;
  border-radius: 50%;
  filter: blur(140px);
  z-index: 0;
  pointer-events: none;
  opacity: 0.28;
}

.glow-1 {
  width: 450px;
  height: 450px;
  background: var(--color-x);
  top: -80px;
  left: -80px;
}

.glow-2 {
  width: 480px;
  height: 480px;
  background: var(--color-o);
  bottom: -100px;
  right: -100px;
}

/* Fullscreen Confetti Canvas */
#confetti-canvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1000;
}

/* Main Container */
.app-container {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 460px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  align-items: center;
  transition: filter 0.2s ease, opacity 0.2s ease;
}

.app-container[inert] {
  filter: blur(2px);
  opacity: 0.6;
  pointer-events: none;
}

/* Header */
.game-header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 4px;
}

.title-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
}

.glitch-title {
  font-size: 1.75rem;
  font-weight: 900;
  letter-spacing: 2px;
  text-wrap: balance;
  background: linear-gradient(90deg, #ffffff, #a3b8cc);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.glitch-title span {
  background: linear-gradient(90deg, var(--color-x), var(--color-o));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.badge {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 1px;
  padding: 3px 7px;
  background: rgba(0, 240, 255, 0.12);
  border: 1px solid rgba(0, 240, 255, 0.3);
  color: var(--color-x);
  border-radius: var(--radius-pill);
}

.top-controls {
  display: flex;
  gap: 8px;
}

.icon-btn {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  color: var(--text-muted);
  width: 44px;
  height: 44px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease, transform 0.2s ease;
  backdrop-filter: blur(10px);
}

.icon-btn:hover {
  background: var(--bg-card-hover);
  color: var(--text-main);
  border-color: var(--border-active);
  transform: translateY(-2px);
}

.icon-btn:active {
  transform: translateY(0);
}

.icon {
  width: 20px;
  height: 20px;
}

.hidden {
  display: none !important;
}

/* Mode Selector & Difficulty */
.mode-selector-wrap {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg-card);
  padding: 6px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-subtle);
  backdrop-filter: blur(12px);
}

.segmented-control {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  background: rgba(10, 12, 22, 0.6);
  padding: 4px;
  border-radius: var(--radius-md);
}

.segment-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-family: var(--font-main);
  font-size: 0.85rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  transition: background-color 0.25s cubic-bezier(0.4, 0, 0.2, 1), color 0.25s ease, box-shadow 0.25s ease;
}

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

.segment-btn.active {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.diff-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

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

.diff-btn.active {
  background: rgba(0, 240, 255, 0.15);
  border-color: rgba(0, 240, 255, 0.4);
  color: var(--color-x);
}

.difficulty-control {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px 2px;
}

.label-tiny {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 1px;
  color: var(--text-dim);
}

.pill-group {
  display: flex;
  gap: 4px;
}

/* Scoreboard */
.scoreboard {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
}

.score-card {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 8px 10px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 10px;
  backdrop-filter: blur(12px);
  position: relative;
  overflow: hidden;
  transition: transform 0.2s ease, border-color 0.2s ease;
}

.score-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: transparent;
  transition: background-color 0.3s ease;
}

.score-card.player-x::before {
  background: var(--color-x);
}

.score-card.score-draw::before {
  background: var(--color-draw);
}

.score-card.player-o::before {
  background: var(--color-o);
}

.score-card.player-x.active-turn {
  border-color: rgba(0, 240, 255, 0.5);
  box-shadow: 0 0 20px rgba(0, 240, 255, 0.2);
}

.score-card.player-o.active-turn {
  border-color: rgba(255, 42, 133, 0.5);
  box-shadow: 0 0 20px rgba(255, 42, 133, 0.2);
}

.score-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 1rem;
}

.x-avatar {
  background: rgba(0, 240, 255, 0.15);
  color: var(--color-x);
}

.draw-avatar {
  background: rgba(255, 184, 0, 0.15);
  color: var(--color-draw);
}

.o-avatar {
  background: rgba(255, 42, 133, 0.15);
  color: var(--color-o);
}

.score-info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 0;
}

.score-label {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  text-transform: uppercase;
}

.score-number {
  font-family: var(--font-mono);
  font-size: 1.3rem;
  font-weight: 700;
  color: #fff;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}

/* Turn Banner */
.turn-banner {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 0;
}

.turn-indicator {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 6px 16px;
  border-radius: var(--radius-pill);
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  transition: background-color 0.3s ease, border-color 0.3s ease, color 0.3s ease, box-shadow 0.3s ease;
}

.current-turn-icon {
  font-weight: 900;
  font-size: 1.2rem;
  transition: color 0.3s ease;
}

.turn-text {
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.turn-indicator.turn-x {
  border-color: rgba(0, 240, 255, 0.3);
}
.turn-indicator.turn-x .current-turn-icon {
  color: var(--color-x);
  text-shadow: 0 0 12px var(--glow-x);
}

.turn-indicator.turn-o {
  border-color: rgba(255, 42, 133, 0.3);
}
.turn-indicator.turn-o .current-turn-icon {
  color: var(--color-o);
  text-shadow: 0 0 12px var(--glow-o);
}

.turn-indicator.winner-x {
  background: rgba(0, 240, 255, 0.15);
  border-color: var(--color-x);
  color: var(--color-x);
  box-shadow: 0 0 25px var(--glow-x);
}

.turn-indicator.winner-o {
  background: rgba(255, 42, 133, 0.15);
  border-color: var(--color-o);
  color: var(--color-o);
  box-shadow: 0 0 25px var(--glow-o);
}

.turn-indicator.draw-result {
  background: rgba(255, 184, 0, 0.15);
  border-color: var(--color-draw);
  color: var(--color-draw);
  box-shadow: 0 0 25px var(--glow-draw);
}

/* Board Wrapper & Grid */
.board-wrapper {
  width: 100%;
  aspect-ratio: 1 / 1;
  max-width: 420px;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

.board-container {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 12px;
  position: relative;
  background: rgba(14, 18, 32, 0.5);
  padding: 12px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-subtle);
  backdrop-filter: blur(16px);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.4), inset 0 0 20px rgba(255, 255, 255, 0.02);
}

/* Board Cell */
.cell {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  transition: background-color 0.2s ease, border-color 0.2s ease, transform 0.15s ease, box-shadow 0.2s ease;
  padding: 16px;
}

.cell:focus:not(:focus-visible) {
  outline: none;
}

@media (hover: hover) {
  .cell:hover:not(.taken) {
    background: var(--bg-card-hover);
    border-color: var(--border-active);
  }
}

/* Ghost preview of the mark you're about to place */
.cell::after {
  content: '';
  position: absolute;
  inset: 22%;
  background: center / contain no-repeat;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
}

.board-container[data-ghost="X"] .cell:not(.taken):focus-visible::after {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cg stroke='%2300f0ff' stroke-width='12' stroke-linecap='round'%3E%3Cline x1='20' y1='20' x2='80' y2='80'/%3E%3Cline x1='80' y1='20' x2='20' y2='80'/%3E%3C/g%3E%3C/svg%3E");
  opacity: 0.35;
}

.board-container[data-ghost="O"] .cell:not(.taken):focus-visible::after {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Ccircle cx='50' cy='50' r='32' fill='none' stroke='%23ff2a85' stroke-width='12' stroke-linecap='round'/%3E%3C/svg%3E");
  opacity: 0.35;
}

@media (hover: hover) {
  .board-container[data-ghost="X"] .cell:not(.taken):hover::after {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cg stroke='%2300f0ff' stroke-width='12' stroke-linecap='round'%3E%3Cline x1='20' y1='20' x2='80' y2='80'/%3E%3Cline x1='80' y1='20' x2='20' y2='80'/%3E%3C/g%3E%3C/svg%3E");
    opacity: 0.35;
  }

  .board-container[data-ghost="O"] .cell:not(.taken):hover::after {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Ccircle cx='50' cy='50' r='32' fill='none' stroke='%23ff2a85' stroke-width='12' stroke-linecap='round'/%3E%3C/svg%3E");
    opacity: 0.35;
  }
}

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

.cell.taken {
  cursor: default;
}

/* SVG Symbols Inside Cell */
.cell svg {
  width: 80%;
  height: 80%;
  display: block;
}

/* Mark X SVG */
.cell-x svg line {
  stroke: var(--color-x);
  stroke-width: 12;
  stroke-linecap: round;
  stroke-dasharray: 120;
  stroke-dashoffset: 120;
  animation: drawLine 0.28s ease-out forwards;
  filter: drop-shadow(0 0 8px var(--glow-x));
}

.cell-x svg line:nth-child(2) {
  animation-delay: 0.12s;
}

/* Mark O SVG */
.cell-o svg circle {
  stroke: var(--color-o);
  stroke-width: 12;
  fill: none;
  stroke-linecap: round;
  stroke-dasharray: 280;
  stroke-dashoffset: 280;
  animation: drawCircle 0.35s ease-out forwards;
  filter: drop-shadow(0 0 8px var(--glow-o));
}

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

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

/* Winning Cell Highlight */
.cell.winning-cell {
  background: rgba(255, 255, 255, 0.08);
  animation: pulseWinCell 1.2s infinite alternate ease-in-out;
}

@keyframes pulseWinCell {
  from {
    transform: scale(0.98);
  }
  to {
    transform: scale(1.02);
  }
}

/* Win Line Overlay */
.win-line-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 20;
}

#win-line {
  stroke-width: 8;
  opacity: 0;
  transition: opacity 0.2s ease;
}

#win-line.active {
  opacity: 1;
  stroke-dasharray: 600;
  stroke-dashoffset: 600;
  animation: strikeThrough 0.4s ease-out forwards;
}

#win-line.win-line-x {
  stroke: var(--color-x);
  filter: drop-shadow(0 0 12px var(--glow-x));
}

#win-line.win-line-o {
  stroke: var(--color-o);
  filter: drop-shadow(0 0 12px var(--glow-o));
}

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

/* Footer & Buttons */
.game-footer {
  width: 100%;
  display: flex;
  gap: 12px;
}

.action-btn {
  flex: 1;
  padding: 12px 18px;
  border-radius: var(--radius-md);
  font-family: var(--font-main);
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease, transform 0.2s ease;
}

.primary-action {
  background: linear-gradient(135deg, #00f0ff, #0088ff);
  color: #050b14;
  box-shadow: 0 4px 18px rgba(0, 240, 255, 0.3);
}

.primary-action:hover {
  box-shadow: 0 6px 24px rgba(0, 240, 255, 0.5);
  transform: translateY(-2px);
}

.primary-action.pulse {
  animation: nextPulse 1.2s ease-in-out infinite alternate;
}

@keyframes nextPulse {
  from { box-shadow: 0 4px 18px rgba(0, 240, 255, 0.3); }
  to   { box-shadow: 0 4px 32px rgba(0, 240, 255, 0.8); }
}

.secondary-action {
  background: var(--bg-card);
  border-color: var(--border-subtle);
  color: var(--text-muted);
}

.secondary-action:hover {
  background: var(--bg-card-hover);
  color: var(--text-main);
  border-color: var(--border-active);
  transform: translateY(-2px);
}

.danger-action {
  background: linear-gradient(135deg, #ff2a85, #e0004d);
  color: #fff;
  box-shadow: 0 4px 18px rgba(255, 42, 133, 0.35);
}

.danger-action:hover {
  box-shadow: 0 6px 24px rgba(255, 42, 133, 0.55);
  transform: translateY(-2px);
}

.action-btn:active {
  transform: translateY(0);
}

.btn-icon {
  width: 18px;
  height: 18px;
}

.full-width {
  width: 100%;
}

/* Numpad Hint */
.numpad-hint {
  font-size: 0.75rem;
  color: var(--text-dim);
  text-align: center;
}

.numpad-hint strong {
  color: var(--text-muted);
  font-family: var(--font-mono);
}

/* Modals */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(5, 7, 14, 0.75);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  opacity: 1;
  transition: opacity 0.25s ease;
  padding: 16px;
  overscroll-behavior: contain;
}

.modal-backdrop.hidden {
  opacity: 0;
  pointer-events: none;
}

.modal-card {
  width: 100%;
  max-width: 380px;
  background: #121626;
  border: 1px solid var(--border-active);
  border-radius: var(--radius-lg);
  padding: 24px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
  display: flex;
  flex-direction: column;
  gap: 20px;
  transform: translateY(0);
  transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
  overscroll-behavior: contain;
}

.modal-backdrop.hidden .modal-card {
  transform: translateY(20px);
}

.confirm-card {
  max-width: 340px;
}

.confirm-message {
  font-size: 0.9rem;
  color: var(--text-muted);
  line-height: 1.5;
}

.confirm-footer {
  display: flex;
  gap: 10px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h2 {
  font-size: 1.25rem;
  font-weight: 800;
  text-wrap: balance;
}

.modal-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 1.8rem;
  cursor: pointer;
  line-height: 1;
  padding: 0 4px;
  border-radius: var(--radius-sm);
  transition: color 0.2s ease;
}

.modal-close:hover {
  color: #fff;
}

.modal-body {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.setting-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.setting-title {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-muted);
}

.mark-selector,
.turn-order-selector {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.turn-order-selector {
  grid-template-columns: 1fr 1fr 1fr;
}

.mark-btn,
.order-btn {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border-subtle);
  color: var(--text-muted);
  padding: 10px 8px;
  border-radius: var(--radius-sm);
  font-family: var(--font-main);
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease;
  text-align: center;
}

.mark-btn.active,
.order-btn.active {
  background: rgba(0, 240, 255, 0.15);
  border-color: var(--color-x);
  color: #fff;
}

/* Responsiveness */
@media (max-width: 440px) {
  .app-container {
    gap: 12px;
  }

  .segment-btn {
    font-size: 0.78rem;
    padding: 8px 6px;
  }

  .glitch-title {
    font-size: 1.5rem;
  }
  
  .scoreboard {
    gap: 6px;
  }

  .score-card {
    padding: 6px 6px;
    gap: 6px;
  }

  .score-number {
    font-size: 1.2rem;
  }

  .board-container {
    gap: 8px;
    padding: 8px;
  }
}

@media (max-width: 380px) {
  .score-avatar {
    display: none;
  }
}

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

  .cell-x svg line,
  .cell-o svg circle {
    stroke-dashoffset: 0 !important;
    animation: none !important;
  }

  .cell.winning-cell {
    animation: none !important;
    border-color: rgba(255, 255, 255, 0.6) !important;
  }

  #win-line.active {
    stroke-dashoffset: 0 !important;
    animation: none !important;
  }
}
