:root {
  --bg-0: #05070f;
  --bg-1: #0b1020;
  --panel: rgba(15, 23, 42, 0.58);
  --panel-strong: rgba(17, 24, 39, 0.76);
  --line: rgba(148, 163, 184, 0.18);
  --text: #f8fbff;
  --muted: #9aa8c3;
  --cyan: #00e5ff;
  --cyan-soft: rgba(0, 229, 255, 0.25);
  --pink: #ff2ed8;
  --pink-soft: rgba(255, 46, 216, 0.25);
  --gold: #ffe66d;
  --green: #34f5a7;
  --shadow-cyan: 0 0 16px rgba(0, 229, 255, 0.65), 0 0 42px rgba(0, 229, 255, 0.25);
  --shadow-pink: 0 0 16px rgba(255, 46, 216, 0.62), 0 0 42px rgba(255, 46, 216, 0.25);
  --radius: 8px;
  --board-size: min(78vw, 430px);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg-0);
}

body {
  min-height: 100vh;
  margin: 0;
  display: grid;
  place-items: center;
  overflow-x: hidden;
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    linear-gradient(116deg, rgba(0, 229, 255, 0.08), transparent 36%),
    linear-gradient(244deg, rgba(255, 46, 216, 0.08), transparent 42%),
    linear-gradient(135deg, #05070f 0%, #090f1d 42%, #100817 100%);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.03) 1px, transparent 1px);
  background-size: 44px 44px;
  mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.72), transparent 82%);
}

button {
  font: inherit;
}

.app-shell {
  width: min(1120px, calc(100vw - 32px));
  min-height: min(760px, calc(100vh - 32px));
  display: grid;
  align-content: center;
  gap: 18px;
  padding: clamp(18px, 4vw, 34px);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: var(--radius);
  background: linear-gradient(145deg, rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0.03));
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.42), inset 0 1px 0 rgba(255, 255, 255, 0.12);
  -webkit-backdrop-filter: blur(22px);
  backdrop-filter: blur(22px);
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 20px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--green);
  font-size: 0.75rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
  text-shadow: 0 0 18px rgba(52, 245, 167, 0.52);
}

h1 {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 4.9rem;
  line-height: 0.9;
  letter-spacing: 0;
  color: #ffffff;
  text-shadow: 0 0 18px rgba(0, 229, 255, 0.32), 0 0 34px rgba(255, 46, 216, 0.24);
}

.mode-stack {
  display: grid;
  gap: 10px;
  justify-items: end;
}

.segmented-control {
  display: inline-grid;
  grid-auto-flow: column;
  gap: 4px;
  padding: 4px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: var(--radius);
  background: rgba(2, 6, 23, 0.56);
  box-shadow: inset 0 0 24px rgba(0, 0, 0, 0.22);
}

.segment {
  min-height: 38px;
  min-width: 84px;
  padding: 0 14px;
  border: 0;
  border-radius: 6px;
  color: var(--muted);
  background: transparent;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 800;
  transition: color 180ms ease, background 180ms ease, box-shadow 180ms ease, transform 180ms ease;
}

.segment:hover {
  color: var(--text);
  transform: translateY(-1px);
}

.segment:disabled {
  cursor: default;
}

.segment.is-active {
  color: var(--text);
  background: linear-gradient(135deg, rgba(0, 229, 255, 0.2), rgba(255, 46, 216, 0.16));
  box-shadow: 0 0 22px rgba(0, 229, 255, 0.16), inset 0 1px 0 rgba(255, 255, 255, 0.16);
}

.difficulty-control {
  opacity: 0;
  pointer-events: none;
  transform: translateY(-4px);
  transition: opacity 180ms ease, transform 180ms ease;
}

.difficulty-control.is-visible {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.scoreboard {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.score-card {
  min-height: 86px;
  display: grid;
  align-content: center;
  gap: 6px;
  padding: 16px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  overflow: hidden;
  position: relative;
}

.score-card::after {
  content: "";
  position: absolute;
  inset: auto 14px 0;
  height: 2px;
  opacity: 0.9;
  background: var(--card-accent, var(--gold));
  box-shadow: 0 0 16px var(--card-accent, var(--gold));
}

.score-x {
  --card-accent: var(--cyan);
}

.score-o {
  --card-accent: var(--pink);
}

.score-draw {
  --card-accent: var(--gold);
}

.score-label {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
  text-transform: uppercase;
}

.score-card strong {
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 2.5rem;
  line-height: 1;
}

.game-panel {
  display: grid;
  justify-items: center;
  gap: 18px;
  padding: clamp(16px, 3vw, 24px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: var(--radius);
  background: var(--panel-strong);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
}

.status-row {
  width: 100%;
  display: flex;
  justify-content: center;
}

.turn-indicator {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 10px 16px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 999px;
  color: var(--text);
  background: rgba(2, 6, 23, 0.52);
  box-shadow: inset 0 0 24px rgba(0, 0, 0, 0.2);
  font-weight: 800;
}

.pulse-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--cyan);
  box-shadow: var(--shadow-cyan);
  animation: pulse 1.4s ease-in-out infinite;
}

.pulse-dot.o-turn {
  background: var(--pink);
  box-shadow: var(--shadow-pink);
}

.pulse-dot.draw-turn {
  background: var(--gold);
  box-shadow: 0 0 18px rgba(255, 230, 109, 0.6), 0 0 38px rgba(255, 230, 109, 0.18);
}

.board-wrap {
  display: grid;
  place-items: center;
  width: 100%;
}

.board {
  width: var(--board-size);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: clamp(8px, 2vw, 12px);
  padding: clamp(8px, 2vw, 12px);
  position: relative;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: var(--radius);
  background:
    linear-gradient(145deg, rgba(0, 229, 255, 0.08), rgba(255, 46, 216, 0.07)),
    rgba(2, 6, 23, 0.58);
  box-shadow: 0 18px 46px rgba(0, 0, 0, 0.36), inset 0 1px 0 rgba(255, 255, 255, 0.14);
}

.cell {
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: var(--radius);
  background: rgba(15, 23, 42, 0.72);
  box-shadow: inset 0 0 32px rgba(0, 0, 0, 0.24);
  cursor: pointer;
  transition: transform 180ms ease, border-color 180ms ease, background 180ms ease, box-shadow 180ms ease;
}

.cell:hover:not(:disabled) {
  transform: translateY(-2px) scale(1.015);
  border-color: rgba(0, 229, 255, 0.42);
  background: rgba(15, 23, 42, 0.92);
  box-shadow: 0 0 28px rgba(0, 229, 255, 0.12), inset 0 0 28px rgba(0, 229, 255, 0.08);
}

.cell:focus-visible,
.segment:focus-visible,
.action-button:focus-visible {
  outline: 2px solid var(--green);
  outline-offset: 3px;
}

.cell:disabled {
  cursor: default;
}

.cell.is-winning {
  border-color: rgba(255, 230, 109, 0.82);
  box-shadow: 0 0 28px rgba(255, 230, 109, 0.2), inset 0 0 24px rgba(255, 230, 109, 0.08);
}

.mark {
  display: block;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 7.1rem;
  font-weight: 700;
  line-height: 0.75;
  animation: markIn 260ms cubic-bezier(0.2, 1.3, 0.35, 1) both;
  user-select: none;
}

.mark.x {
  color: var(--cyan);
  text-shadow: var(--shadow-cyan);
}

.mark.o {
  color: var(--pink);
  text-shadow: var(--shadow-pink);
}

.winning-line {
  --line-x: 0px;
  --line-y: 0px;
  --line-length: 0px;
  --line-angle: 0deg;
  position: absolute;
  left: 0;
  top: 0;
  width: var(--line-length);
  height: clamp(6px, 1.8vw, 10px);
  border-radius: 999px;
  background: linear-gradient(90deg, var(--cyan), var(--gold), var(--pink));
  box-shadow: 0 0 16px rgba(255, 230, 109, 0.86), 0 0 42px rgba(255, 46, 216, 0.36);
  transform: translate(var(--line-x), var(--line-y)) rotate(var(--line-angle)) scaleX(0);
  transform-origin: left center;
  opacity: 0;
  pointer-events: none;
  z-index: 2;
}

.winning-line.is-visible {
  animation: lineDraw 520ms cubic-bezier(0.18, 0.9, 0.24, 1) forwards;
}

.actions {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
}

.action-button {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  padding: 0 16px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: var(--radius);
  color: var(--text);
  background: rgba(2, 6, 23, 0.54);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
  cursor: pointer;
  font-size: 0.92rem;
  font-weight: 800;
  transition: transform 180ms ease, border-color 180ms ease, box-shadow 180ms ease, background 180ms ease;
}

.action-button:hover {
  transform: translateY(-2px);
  border-color: rgba(255, 255, 255, 0.28);
  background: rgba(15, 23, 42, 0.78);
}

.primary-action {
  border-color: rgba(0, 229, 255, 0.38);
  box-shadow: 0 0 24px rgba(0, 229, 255, 0.12), inset 0 1px 0 rgba(255, 255, 255, 0.12);
}

@keyframes markIn {
  from {
    opacity: 0;
    transform: scale(0.4) rotate(-8deg);
    filter: blur(6px);
  }
  to {
    opacity: 1;
    transform: scale(1) rotate(0deg);
    filter: blur(0);
  }
}

@keyframes lineDraw {
  0% {
    opacity: 0;
    transform: translate(var(--line-x), var(--line-y)) rotate(var(--line-angle)) scaleX(0);
  }
  18% {
    opacity: 1;
  }
  100% {
    opacity: 1;
    transform: translate(var(--line-x), var(--line-y)) rotate(var(--line-angle)) scaleX(1);
  }
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(1);
    opacity: 0.72;
  }
  50% {
    transform: scale(1.28);
    opacity: 1;
  }
}

@media (max-width: 760px) {
  body {
    align-items: start;
    padding: 14px 0;
  }

  .app-shell {
    width: min(100vw - 24px, 560px);
    min-height: auto;
    padding: 16px;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  h1 {
    font-size: 2.7rem;
  }

  .mode-stack {
    justify-items: stretch;
  }

  .segmented-control {
    width: 100%;
  }

  .segment {
    min-width: 0;
    width: 100%;
    padding: 0 8px;
    font-size: 0.78rem;
  }

  .scoreboard {
    grid-template-columns: 1fr;
  }

  .score-card {
    min-height: 70px;
  }

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

  .score-card strong {
    font-size: 2rem;
  }

  .game-panel {
    padding: 14px;
  }

  .board {
    width: min(88vw, 430px);
  }

  .mark {
    font-size: 4.6rem;
  }

  .action-button {
    flex: 1 1 150px;
  }
}

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