:root {
  --bg-0: #07111e;
  --bg-1: #0c1930;
  --bg-2: #112547;
  --text-main: #f3f7ff;
  --text-soft: rgba(224, 233, 255, 0.78);
  --panel: rgba(8, 18, 36, 0.78);
  --panel-strong: rgba(7, 16, 31, 0.92);
  --stroke: rgba(140, 184, 255, 0.18);
  --accent: #4fe3ff;
  --accent-strong: #22bfff;
  --accent-2: #9b7dff;
  --success: #91ffbb;
  --danger: #ff7e8f;
  --shadow: 0 24px 60px rgba(0, 0, 0, 0.38);
  --field-line: rgba(226, 237, 255, 0.16);
  --hud-glow: 0 0 22px rgba(79, 227, 255, 0.26);
}

body[data-theme="retro"] {
  --bg-0: #120816;
  --bg-1: #1a0d24;
  --bg-2: #231137;
  --text-main: #fff7ec;
  --text-soft: rgba(255, 243, 224, 0.76);
  --panel: rgba(21, 9, 28, 0.82);
  --panel-strong: rgba(12, 5, 19, 0.94);
  --stroke: rgba(255, 159, 118, 0.18);
  --accent: #ff9d5c;
  --accent-strong: #ffd357;
  --accent-2: #ff4d8f;
  --success: #8dffcf;
  --danger: #ff8597;
  --field-line: rgba(255, 218, 189, 0.18);
  --hud-glow: 0 0 22px rgba(255, 157, 92, 0.3);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: "Space Grotesk", system-ui, sans-serif;
  background:
    radial-gradient(circle at 20% 10%, rgba(79, 227, 255, 0.15), transparent 30%),
    radial-gradient(circle at 82% 22%, rgba(155, 125, 255, 0.15), transparent 30%),
    linear-gradient(160deg, var(--bg-0), var(--bg-1) 45%, var(--bg-2));
  color: var(--text-main);
  overflow-x: hidden;
  overflow-y: auto;
}

body {
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior-x: none;
}

body.menu-hidden,
body.game-scroll-locked {
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior-x: none;
  overscroll-behavior-y: auto;
}

button,
input,
select {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  margin: 0;
  -webkit-appearance: none;
}

.app-shell {
  position: relative;
  min-height: 100vh;
  min-height: 100svh;
  padding: 22px;
  overflow: visible;
  -webkit-overflow-scrolling: touch;
}

body.menu-hidden .app-shell,
body.game-scroll-locked .app-shell {
  min-height: calc(100dvh + 120px);
  min-height: calc(100svh + 120px);
  padding-top: max(14px, env(safe-area-inset-top));
  padding-bottom: calc(120px + env(safe-area-inset-bottom));
}

.ambient {
  position: absolute;
  border-radius: 50%;
  filter: blur(18px);
  opacity: 0.42;
  pointer-events: none;
}

.ambient-a {
  top: -80px;
  left: -120px;
  width: 300px;
  height: 300px;
  background: radial-gradient(circle, rgba(79, 227, 255, 0.52), transparent 65%);
  animation: drift 12s ease-in-out infinite alternate;
}

.ambient-b {
  right: -80px;
  bottom: -120px;
  width: 360px;
  height: 360px;
  background: radial-gradient(circle, rgba(155, 125, 255, 0.38), transparent 65%);
  animation: drift 14s ease-in-out infinite alternate-reverse;
}

.layout {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: minmax(280px, 420px) minmax(0, 1fr);
  gap: 22px;
  align-items: start;
  min-height: auto;
}

.card {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.06), rgba(255, 255, 255, 0.02));
  background-color: var(--panel);
  border: 1px solid var(--stroke);
  border-radius: 28px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.menu-panel {
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding: 28px;
}

.menu-copy {
  display: grid;
  gap: 10px;
}

.eyebrow,
.overlay-label,
.setting-label,
.match-status {
  color: var(--text-soft);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-size: 0.79rem;
}

h1,
h2,
.player-score,
#countdownOverlay {
  font-family: "Oxanium", "Space Grotesk", sans-serif;
}

h1 {
  margin: 0;
  font-size: clamp(2.5rem, 5vw, 4.2rem);
  line-height: 0.95;
}

h2 {
  margin: 0;
  font-size: clamp(1.8rem, 4vw, 2.6rem);
}

.lead,
.menu-tips p,
.overlay-card p,
.orientation-card p {
  margin: 0;
  color: var(--text-soft);
  line-height: 1.55;
}

.settings-grid {
  display: grid;
  gap: 16px;
}

.setting-block {
  display: grid;
  gap: 10px;
}

.setting-inline {
  grid-template-columns: 1fr auto;
  align-items: center;
}

.segmented {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.segmented-btn,
.mini-btn,
.secondary-btn,
.step-btn,
.primary-btn {
  border-radius: 16px;
  transition:
    transform 160ms ease,
    background-color 160ms ease,
    border-color 160ms ease,
    box-shadow 180ms ease,
    opacity 160ms ease;
}

.segmented-btn,
.mini-btn,
.secondary-btn,
.step-btn {
  background: rgba(255, 255, 255, 0.04);
  color: var(--text-main);
  border: 1px solid rgba(255, 255, 255, 0.12);
}

.segmented-btn {
  min-width: 108px;
  padding: 13px 16px;
}

.segmented-btn.is-active {
  background: linear-gradient(135deg, rgba(79, 227, 255, 0.18), rgba(155, 125, 255, 0.14));
  border-color: rgba(79, 227, 255, 0.46);
  box-shadow: var(--hud-glow);
}

.points-control {
  display: grid;
  grid-template-columns: 52px minmax(80px, 110px) 52px;
  gap: 10px;
  align-items: center;
}

#pointsInput {
  width: 100%;
  min-height: 52px;
  padding: 0 14px;
  text-align: center;
  color: var(--text-main);
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 16px;
  outline: none;
}

.step-btn {
  min-height: 52px;
  font-size: 1.45rem;
}

.toggle {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
}

.toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.toggle-track {
  position: relative;
  width: 56px;
  height: 32px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.12);
}

.toggle-track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #ffffff;
  transition: transform 180ms ease;
}

.toggle input:checked + .toggle-track {
  background: linear-gradient(135deg, rgba(79, 227, 255, 0.55), rgba(155, 125, 255, 0.45));
  box-shadow: var(--hud-glow);
}

.toggle input:checked + .toggle-track::after {
  transform: translateX(24px);
}

.menu-actions,
.overlay-actions,
.hud-actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.primary-btn,
.secondary-btn {
  min-height: 52px;
  padding: 0 20px;
}

.primary-btn {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #06101d;
  font-weight: 700;
  box-shadow: var(--hud-glow);
}

.secondary-btn,
.mini-btn {
  color: var(--text-main);
}

.mini-btn {
  min-height: 40px;
  padding: 0 16px;
}

.menu-tips {
  display: grid;
  gap: 6px;
  padding-top: 4px;
}

.game-panel {
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 18px;
}

.hud {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 14px;
  padding: 18px 20px;
}

.player-tag {
  display: flex;
  align-items: center;
  gap: 14px;
}

.player-tag-right {
  justify-content: flex-end;
}

.player-name {
  color: var(--text-soft);
  font-size: 0.98rem;
}

.player-score {
  font-size: clamp(2rem, 4vw, 3.6rem);
  line-height: 1;
  text-shadow: var(--hud-glow);
}

.hud-center {
  display: grid;
  justify-items: center;
  gap: 10px;
}

.playfield-wrap {
  min-height: 0;
  padding: 16px;
}

.playfield {
  position: relative;
  width: 100%;
  max-width: 100%;
  aspect-ratio: 16 / 9;
  height: auto;
  min-height: 0;
  margin-inline: auto;
  border-radius: 22px;
  overflow: hidden;
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.08), transparent 45%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.04), transparent),
    rgba(0, 0, 0, 0.26);
  border: 1px solid rgba(255, 255, 255, 0.08);
  touch-action: pan-y;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: pan-y;
}

body.game-scroll-locked .playfield,
body.game-scroll-locked #gameCanvas {
  touch-action: none;
}

.overlay,
.orientation-hint {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  transition: opacity 180ms ease, visibility 180ms ease;
}

.overlay {
  background: rgba(3, 7, 16, 0.28);
}

.countdown-overlay {
  font-size: clamp(3rem, 14vw, 8rem);
  color: var(--text-main);
  text-shadow: 0 0 28px rgba(255, 255, 255, 0.24), var(--hud-glow);
  backdrop-filter: blur(2px);
}

.overlay-card,
.orientation-card {
  width: min(100%, 420px);
  padding: 26px;
  border-radius: 26px;
  background: var(--panel-strong);
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: var(--shadow);
  text-align: center;
  display: grid;
  gap: 12px;
}

.orientation-hint {
  pointer-events: none;
  align-items: start;
  padding-top: 20px;
}

.orientation-card {
  width: min(100%, 360px);
  background: rgba(8, 14, 27, 0.8);
}

.is-hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.menu-hidden .layout {
  grid-template-columns: minmax(0, 1fr);
  min-height: calc(100dvh - 44px);
  min-height: calc(100vh - 44px);
  min-height: calc(100svh - 44px);
  align-items: stretch;
}

.menu-hidden .menu-panel {
  display: none;
}

body:not(.menu-hidden) .game-panel {
  display: none;
}

.segmented-btn:hover,
.mini-btn:hover,
.secondary-btn:hover,
.step-btn:hover,
.primary-btn:hover {
  transform: translateY(-1px);
}

.segmented-btn:active,
.mini-btn:active,
.secondary-btn:active,
.step-btn:active,
.primary-btn:active {
  transform: translateY(1px) scale(0.99);
}

.segmented-btn:focus-visible,
.mini-btn:focus-visible,
.secondary-btn:focus-visible,
.step-btn:focus-visible,
.primary-btn:focus-visible,
#pointsInput:focus-visible {
  outline: 2px solid rgba(255, 255, 255, 0.65);
  outline-offset: 2px;
}

@keyframes drift {
  from {
    transform: translate3d(0, 0, 0) scale(1);
  }
  to {
    transform: translate3d(22px, 26px, 0) scale(1.08);
  }
}

@media (max-width: 1180px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .menu-panel {
    max-width: none;
  }
}

@media (max-width: 820px) {
  .app-shell {
    padding: 14px;
  }

  .layout {
    gap: 14px;
    min-height: calc(100dvh - 28px);
    min-height: calc(100svh - 28px);
  }

  .menu-panel,
  .hud,
  .playfield-wrap {
    border-radius: 22px;
  }

  .menu-panel {
    padding: 20px;
  }

  .hud {
    grid-template-columns: 1fr;
    justify-items: center;
    text-align: center;
  }

  .player-tag,
  .player-tag-right {
    justify-content: center;
  }

}

@media (max-width: 820px) and (orientation: portrait) {
  .playfield {
    min-height: 260px;
  }
}

@media (max-height: 520px) and (orientation: landscape) {
  .app-shell {
    padding-top: max(8px, env(safe-area-inset-top));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  .menu-hidden .layout {
    gap: 10px;
    min-height: auto;
  }

  .game-panel {
    gap: 10px;
  }

  .hud {
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    gap: 10px;
    padding: 10px 12px;
  }

  .player-name,
  .match-status {
    font-size: 0.8rem;
  }

  .player-score {
    font-size: clamp(1.6rem, 3.2vw, 2.2rem);
  }

  .hud-actions {
    gap: 8px;
  }

  .mini-btn {
    min-height: 34px;
    padding: 0 12px;
  }

  .playfield-wrap {
    padding: 10px;
  }

  .playfield {
    min-height: 0;
  }
}

@media (max-width: 560px) {
  .menu-actions,
  .overlay-actions,
  .hud-actions {
    display: grid;
    grid-template-columns: 1fr;
  }

  .segmented {
    display: grid;
    grid-template-columns: 1fr;
  }

  .points-control {
    grid-template-columns: 1fr 1.3fr 1fr;
  }
}

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