/* =========================================================
   Tic Tac Toe — Premium Navy Edition
   Design tokens live at the top for easy customization.
   ========================================================= */

:root {
  /* ---- Brand palette ---- */
  --navy: #0C346C;
  --navy-mid: #123B78;
  --navy-deep: #081D3D;
  --navy-deepest: #051023;
  --white: #FFFFFF;
  --gold: #FFD566;
  --gold-soft: #FFE9AE;
  --ice: #9CC0EA;

  /* ---- Glass surfaces ---- */
  --glass-bg: rgba(255, 255, 255, 0.07);
  --glass-bg-strong: rgba(255, 255, 255, 0.12);
  --glass-border: rgba(255, 255, 255, 0.20);
  --glass-blur: 18px;

  /* ---- Text ---- */
  --text-main: #F4F8FF;
  --text-muted: #9FB6DD;
  --text-on-white: var(--navy-deep);

  /* ---- Radii / shadow ---- */
  --radius-lg: 20px;
  --radius-md: 16px;
  --shadow-soft: 0 20px 50px rgba(3, 12, 28, 0.45);
  --shadow-tile: 0 6px 16px rgba(3, 12, 28, 0.35);

  /* ---- Type ---- */
  --font-display: "Poppins", "Segoe UI", system-ui, sans-serif;
  --font-body: "Inter", "Segoe UI", system-ui, sans-serif;

  --transition-fast: 140ms cubic-bezier(.22,1,.36,1);
  --transition-med: 320ms cubic-bezier(.22,1,.36,1);
}

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

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: var(--font-body);
  color: var(--text-main);
  min-height: 100vh;
  background: radial-gradient(circle at 20% 15%, var(--navy-mid), var(--navy-deep) 55%, var(--navy-deepest) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-x: hidden;
}

button { font-family: inherit; border: none; background: none; color: inherit; cursor: pointer; }

/* =========================================================
   Ambient background orbs (signature motion layer)
   ========================================================= */
.scene {
  position: relative;
  width: 100%;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.bg-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(60px);
  opacity: 0.35;
  pointer-events: none;
  animation: floatOrb 14s ease-in-out infinite;
}
.orb-1 { width: 320px; height: 320px; background: var(--ice); top: -80px; left: -60px; animation-duration: 16s; }
.orb-2 { width: 260px; height: 260px; background: var(--gold); bottom: -60px; right: -40px; animation-duration: 20s; animation-delay: -4s; }
.orb-3 { width: 220px; height: 220px; background: #4884D1; top: 40%; right: 10%; animation-duration: 18s; animation-delay: -8s; }

@keyframes floatOrb {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(30px, -20px) scale(1.08); }
}

@media (prefers-reduced-motion: reduce) {
  .bg-orb { animation: none; }
}

/* =========================================================
   App shell
   ========================================================= */
.app {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 550px;
  padding: clamp(14px, 4vw, 26px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(12px, 2.4vw, 18px);
}

/* ---- Header ---- */
.app-header {
  width: 100%;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 10px;
  position: relative;
  padding-top: 4px;
}

.logo {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(1.8rem, 7vw, 2.6rem);
  margin: 0;
  letter-spacing: -0.5px;
  color: var(--white);
}
.logo-accent { color: var(--gold); }

.tagline {
  margin: 0;
  font-size: clamp(0.65rem, 2vw, 0.78rem);
  text-transform: uppercase;
  letter-spacing: 2px;
  color: var(--text-muted);
  align-self: flex-end;
  padding-bottom: 4px;
}

.icon-btn {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(var(--glass-blur));
  transition: background var(--transition-fast), transform var(--transition-fast);
}
.icon-btn:hover { background: var(--glass-bg-strong); }
.icon-btn:active { transform: translateY(-50%) scale(0.92); }
.icon-btn[aria-pressed="false"] { color: var(--text-muted); }

/* ---- Glass panel ---- */
.panel {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: clamp(14px, 3vw, 18px);
}

.glass {
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(var(--glass-blur));
  -webkit-backdrop-filter: blur(var(--glass-blur));
  border-radius: var(--radius-lg);
  padding: clamp(16px, 4vw, 26px);
  box-shadow: var(--shadow-soft);
}

.hidden { display: none !important; }

/* ---- Setup fields ---- */
.field-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field-label {
  font-size: clamp(0.7rem, 2vw, 0.78rem);
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--text-muted);
  font-weight: 600;
}

.pill-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.pill {
  flex: 1 1 auto;
  padding: 10px 14px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--glass-border);
  color: var(--text-main);
  font-size: clamp(0.75rem, 2.2vw, 0.85rem);
  font-weight: 600;
  text-align: center;
  transition: background var(--transition-fast), border-color var(--transition-fast), transform var(--transition-fast), color var(--transition-fast);
  white-space: nowrap;
}
.pill:hover { background: rgba(255, 255, 255, 0.14); }
.pill:active { transform: scale(0.96); }
.pill.active {
  background: linear-gradient(135deg, var(--gold), #F0B93E);
  color: var(--navy-deep);
  border-color: transparent;
  box-shadow: 0 6px 16px rgba(255, 213, 102, 0.35);
}

/* ---- Buttons ---- */
.btn {
  border-radius: 14px;
  font-weight: 700;
  font-size: clamp(0.85rem, 2.4vw, 0.95rem);
  transition: background var(--transition-fast), transform var(--transition-fast), box-shadow var(--transition-fast);
}
.btn:active { transform: scale(0.96); }

.btn-primary {
  background: linear-gradient(135deg, var(--gold), #F0B93E);
  color: var(--navy-deep);
  padding: 14px 20px;
  box-shadow: 0 10px 24px rgba(255, 213, 102, 0.3);
}
.btn-primary:hover { filter: brightness(1.05); }

.btn-start { width: 100%; }

.btn-secondary {
  background: rgba(255, 255, 255, 0.12);
  color: var(--text-main);
  padding: 14px 20px;
  border: 1px solid var(--glass-border);
}
.btn-secondary:hover { background: rgba(255, 255, 255, 0.2); }

.btn-icon {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 6px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--glass-border);
  font-size: clamp(0.6rem, 1.8vw, 0.7rem);
  font-weight: 600;
  color: var(--text-main);
}
.btn-icon:hover { background: rgba(255, 255, 255, 0.14); }
.btn-icon:disabled { opacity: 0.35; pointer-events: none; }

.link-btn {
  font-size: clamp(0.7rem, 2vw, 0.8rem);
  color: var(--gold);
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* ---- Match bar ---- */
.match-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: clamp(0.75rem, 2.2vw, 0.85rem);
  color: var(--text-muted);
}

/* ---- Scoreboard ---- */
.scoreboard {
  display: flex;
  gap: 8px;
}

.score-pill {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 4px;
  border-radius: var(--radius-md);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--glass-border);
}
.score-x { border-top: 3px solid var(--ice); }
.score-o { border-top: 3px solid var(--gold); }
.score-draw { border-top: 3px solid var(--text-muted); }

.score-name {
  font-size: clamp(0.6rem, 1.8vw, 0.7rem);
  color: var(--text-muted);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.score-num {
  font-family: var(--font-display);
  font-size: clamp(1.2rem, 4.5vw, 1.6rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  transition: transform 200ms ease;
}
.score-num.bump { animation: scoreBump 380ms ease; }
@keyframes scoreBump {
  0% { transform: scale(1); }
  40% { transform: scale(1.35); color: var(--gold); }
  100% { transform: scale(1); }
}

/* ---- Turn indicator ---- */
.turn-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(0.9rem, 3vw, 1.05rem);
  color: var(--text-main);
}

.turn-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--ice);
  box-shadow: 0 0 0 0 rgba(156, 192, 234, 0.6);
  animation: pulseDot 1.4s ease-in-out infinite;
}
.turn-dot.o { background: var(--gold); box-shadow: 0 0 0 0 rgba(255, 213, 102, 0.6); }

@keyframes pulseDot {
  0% { box-shadow: 0 0 0 0 currentColor; opacity: 1; }
  70% { box-shadow: 0 0 0 8px transparent; opacity: 0.7; }
  100% { box-shadow: 0 0 0 0 transparent; opacity: 1; }
}

/* ---- Board ---- */
.board-wrapper {
  display: flex;
  justify-content: center;
}

.board {
  position: relative;
  width: min(340px, 78vw);
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: clamp(8px, 2.4vw, 12px);
  background: rgba(5, 16, 35, 0.4);
  border-radius: var(--radius-lg);
  padding: clamp(8px, 2.4vw, 12px);
  box-shadow: inset 0 2px 12px rgba(0,0,0,0.35), var(--shadow-soft);
}

.cell {
  position: relative;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(2rem, 10vw, 3.2rem);
  color: var(--white);
  transition: background var(--transition-fast), transform var(--transition-fast);
}
.cell:hover:not(.filled) { background: rgba(255, 255, 255, 0.14); }
.cell:active:not(.filled) { transform: scale(0.95); }
.cell:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--gold);
}

.cell .mark {
  display: inline-block;
  animation: markPop 260ms cubic-bezier(.22,1.4,.36,1);
}
.cell .mark.x { color: var(--ice); }
.cell .mark.o { color: var(--gold); }

@keyframes markPop {
  0% { transform: scale(0) rotate(-15deg); opacity: 0; }
  70% { transform: scale(1.15) rotate(4deg); opacity: 1; }
  100% { transform: scale(1) rotate(0); }
}

.cell.winning {
  background: rgba(255, 213, 102, 0.18);
  animation: winGlow 900ms ease-in-out infinite alternate;
}
@keyframes winGlow {
  from { box-shadow: 0 0 8px 0 rgba(255, 213, 102, 0.3); }
  to   { box-shadow: 0 0 20px 4px rgba(255, 213, 102, 0.55); }
}

.board.shake { animation: shakeBoard 420ms ease; }
@keyframes shakeBoard {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
}

.win-line {
  position: absolute;
  height: 6px;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--gold), var(--white), var(--gold));
  box-shadow: 0 0 18px 4px rgba(255, 213, 102, 0.65);
  opacity: 0;
  transform-origin: left center;
  transition: transform 480ms cubic-bezier(.22,1,.36,1), opacity 160ms ease;
  pointer-events: none;
  z-index: 3;
}
.win-line.show { opacity: 1; }

/* ---- Controls row ---- */
.controls-row {
  display: flex;
  gap: 8px;
}

/* ---- History ---- */
.history-box {
  border-radius: var(--radius-md);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--glass-border);
  padding: 10px 14px;
}
.history-box summary {
  cursor: pointer;
  font-weight: 600;
  font-size: clamp(0.78rem, 2.2vw, 0.88rem);
  color: var(--text-main);
}
.history-list {
  margin: 10px 0 0;
  padding-left: 20px;
  max-height: 140px;
  overflow-y: auto;
  font-size: clamp(0.75rem, 2vw, 0.82rem);
  color: var(--text-muted);
}
.history-list li { margin-bottom: 4px; }
.history-list li.win-x { color: var(--ice); }
.history-list li.win-o { color: var(--gold); }

/* =========================================================
   Overlay
   ========================================================= */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(5, 16, 35, 0.72);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 1;
  transition: opacity var(--transition-med);
}
.overlay.hidden { opacity: 0; pointer-events: none; }

.confetti-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.overlay-card {
  position: relative;
  z-index: 1;
  background: var(--glass-bg-strong);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(var(--glass-blur));
  border-radius: var(--radius-lg);
  padding: clamp(24px, 6vw, 36px);
  text-align: center;
  max-width: 90%;
  width: 340px;
  box-shadow: var(--shadow-soft);
  animation: markPop 300ms cubic-bezier(.22,1.4,.36,1);
}

.overlay-card h2 {
  font-family: var(--font-display);
  margin: 0 0 8px;
  font-size: clamp(1.4rem, 6vw, 1.9rem);
  color: var(--white);
}
.overlay-card p {
  margin: 0 0 20px;
  color: var(--text-muted);
  font-size: clamp(0.85rem, 3vw, 0.95rem);
}
.overlay-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

/* =========================================================
   Responsive breakpoints
   ========================================================= */
@media (max-width: 768px) {
  .app { max-width: 460px; }
}

@media (max-width: 480px) {
  .board { width: 90vw; }
  .score-name { font-size: 0.6rem; }
  .btn-icon span { display: none; }
}

@media (max-width: 340px) {
  .pill { font-size: 0.68rem; padding: 8px 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}
