:root {
  --bg: #fff6e5;
  --cell: #ffffff;
  --x: #ff6b6b;
  --o: #4ecdc4;
  --o-text: #1fa796;
  --ink: #2b2d42;
  --muted-ink: #5a5d73;
  --accent: #ffd93d;
  --amber: #f5a201;
  --shadow: 0 10px 24px rgba(43, 45, 66, 0.12);
  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100svh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 2.2vh, 20px);
  padding: 16px;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 10%, rgba(255, 217, 61, 0.28), transparent 42%),
    radial-gradient(circle at 94% 90%, rgba(78, 205, 196, 0.20), transparent 42%),
    radial-gradient(circle at 92% 8%, rgba(255, 107, 107, 0.14), transparent 38%),
    var(--bg);
  -webkit-tap-highlight-color: transparent;
}

/* ---------- Top bar ---------- */

.topbar {
  width: min(92vw, 470px);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  margin: 0;
  font-size: clamp(1.3rem, 3.4vw, 1.7rem);
  font-weight: 900;
  letter-spacing: -0.5px;
  user-select: none;
}

.t-x { color: var(--x); }
.t-y { color: var(--amber); }
.t-o { color: var(--o-text); }

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.round-pill {
  background: var(--cell);
  border-radius: 999px;
  padding: 0.4em 0.95em;
  font-size: 0.9rem;
  font-weight: 800;
  box-shadow: var(--shadow);
  white-space: nowrap;
  user-select: none;
}

.icon-btn {
  width: 42px;
  height: 42px;
  border: none;
  border-radius: 50%;
  background: var(--cell);
  color: var(--ink);
  box-shadow: var(--shadow);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.15s;
}

.icon-btn:hover { transform: translateY(-2px); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn .icon-off { display: none; }
.icon-btn.muted .icon-on { display: none; }
.icon-btn.muted .icon-off { display: block; }

/* ---------- Scoreboard ---------- */

.scoreboard {
  display: flex;
  align-items: center;
  gap: 12px;
}

.pill {
  display: flex;
  align-items: center;
  gap: 0.45em;
  background: var(--cell);
  border-radius: 999px;
  padding: 0.45em 1em;
  font-weight: 800;
  font-size: 0.98rem;
  box-shadow: var(--shadow);
  user-select: none;
}

.pill b {
  font-size: 1.25rem;
  min-width: 1ch;
  text-align: center;
}

.pill svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke-width: 13;
  stroke-linecap: round;
}

.pill-x svg { stroke: var(--x); }
.pill-o svg { stroke: var(--o); }

/* ---------- Status ---------- */

.status {
  margin: 0;
  min-height: 1.5em;
  font-size: clamp(1.35rem, 2.2vw + 0.8rem, 1.9rem);
  font-weight: 800;
  text-align: center;
  user-select: none;
}

.status.bump {
  animation: bump 0.3s var(--spring);
}

@keyframes bump {
  from { transform: scale(0.82); opacity: 0.4; }
}

.m-x { color: var(--x); }
.m-o { color: var(--o-text); }

/* ---------- Board ---------- */

.board {
  width: min(88vw, 60vh, 470px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 12px;
}

.cell {
  border: none;
  border-radius: 22px;
  background: var(--cell);
  box-shadow: var(--shadow);
  cursor: pointer;
  display: grid;
  place-items: center;
  padding: 0;
  transition: transform 0.15s, opacity 0.3s, box-shadow 0.3s;
}

.cell:hover:not(:disabled) { transform: translateY(-3px); }
.cell:active:not(:disabled) { transform: scale(0.96); }

.cell:focus-visible,
.btn:focus-visible,
.icon-btn:focus-visible {
  outline: 3px solid var(--amber);
  outline-offset: 2px;
}

.cell svg {
  width: 66%;
  height: 66%;
  fill: none;
  stroke-width: 13;
  stroke-linecap: round;
  animation: pop 0.35s var(--spring);
}

.cell.is-x svg { stroke: var(--x); }
.cell.is-o svg { stroke: var(--o); }

@keyframes pop {
  from { transform: scale(0.15) rotate(-16deg); opacity: 0; }
}

.cell.dim {
  opacity: 0.35;
  cursor: default;
}

.cell.won {
  animation: pulse 1s ease-in-out infinite alternate;
  cursor: default;
}

.cell.won.is-x { box-shadow: 0 0 0 6px rgba(255, 107, 107, 0.45), var(--shadow); }
.cell.won.is-o { box-shadow: 0 0 0 6px rgba(78, 205, 196, 0.55), var(--shadow); }

@keyframes pulse {
  to { transform: scale(1.05); }
}

/* ---------- Controls ---------- */

.controls {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 52px;
}

.btn {
  border: none;
  border-radius: 999px;
  padding: 0.75em 1.7em;
  font-family: inherit;
  font-size: 1.02rem;
  font-weight: 800;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.15s, box-shadow 0.15s, opacity 0.15s;
}

.btn:hover:not(:disabled) { transform: translateY(-2px); }
.btn:active:not(:disabled) { transform: scale(0.95); }
.btn:disabled { opacity: 0.4; cursor: default; }

.btn-ghost { background: var(--cell); color: var(--ink); }
.btn-primary { background: var(--accent); color: var(--ink); }

.popin { animation: popin 0.3s var(--spring); }

@keyframes popin {
  from { transform: scale(0.6); opacity: 0; }
}

/* ---------- Overlay ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(43, 45, 66, 0.55);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}

.overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.card {
  background: var(--cell);
  border-radius: 26px;
  padding: 2.2rem 2.6rem;
  max-width: min(90vw, 430px);
  text-align: center;
  box-shadow: 0 24px 60px rgba(43, 45, 66, 0.35);
  transform: scale(0.75);
  transition: transform 0.35s var(--spring);
}

.overlay.open .card { transform: scale(1); }

.card h2 {
  margin: 0 0 0.5em;
  font-size: clamp(1.7rem, 5vw, 2.3rem);
  font-weight: 900;
}

.card p {
  margin: 0 0 1.5em;
  font-weight: 700;
  color: var(--muted-ink);
}

/* ---------- Confetti ---------- */

#confetti {
  position: fixed;
  inset: 0;
  z-index: 100;
  pointer-events: none;
}

/* ---------- Small screens ---------- */

@media (max-height: 640px) {
  body { gap: 8px; }
  .logo { font-size: 1.15rem; }
  .status { min-height: 1.2em; }
}

/* ---------- Back to hub link ---------- */

.back-hub {
  position: fixed;
  top: 14px;
  inset-inline-start: 14px;
  z-index: 200;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0.35em 0.9em;
  border-radius: 999px;
  background: var(--cell);
  color: var(--ink);
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 800;
  text-decoration: none;
  box-shadow: var(--shadow);
  transition: transform 0.25s var(--spring), box-shadow 0.25s ease;
}

.back-hub:hover,
.back-hub:focus-visible {
  transform: translateY(-2px) scale(1.04);
  box-shadow: 0 14px 30px rgba(43, 45, 66, 0.20);
}

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