:root {
  color-scheme: dark;
  --ink: #f7f4ff;
  --muted: #aaa5be;
  --panel: rgba(19, 17, 38, 0.88);
  --line: rgba(255, 255, 255, 0.15);
  --pink: #ff3cab;
  --cyan: #24e4ff;
  --yellow: #ffe45e;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  overflow-x: hidden;
  background:
    radial-gradient(circle at 15% 15%, rgba(255, 60, 171, 0.16), transparent 35%),
    radial-gradient(circle at 85% 75%, rgba(36, 228, 255, 0.14), transparent 38%),
    #080713;
}

body {
  min-height: 100svh;
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Rounded", "Arial Rounded MT Bold", system-ui, sans-serif;
  display: grid;
  place-items: center;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  touch-action: manipulation;
}

button { font: inherit; }

.game-shell {
  width: min(1080px, 100%);
  display: grid;
  gap: 12px;
}

.topbar, .scoreboard, .controls {
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.28);
}

.topbar {
  min-height: 54px;
  border-radius: 18px;
  padding: 8px 10px 8px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: clamp(1rem, 3vw, 1.35rem);
  font-weight: 950;
  letter-spacing: 0.06em;
}

.icon-button {
  width: 42px;
  height: 42px;
  border-radius: 13px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.07);
  color: white;
  cursor: pointer;
}

.scoreboard {
  border-radius: 18px;
  padding: 10px 18px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 14px;
}

.player-score {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.88rem;
  font-weight: 900;
  letter-spacing: 0.08em;
}

.player-score strong {
  font-size: clamp(2rem, 7vw, 3.25rem);
  line-height: 0.85;
  font-variant-numeric: tabular-nums;
}

.player-one strong { color: var(--pink); text-shadow: 0 0 22px rgba(255, 60, 171, 0.62); }
.player-two { justify-content: flex-end; text-align: right; }
.player-two strong { color: var(--cyan); text-shadow: 0 0 22px rgba(36, 228, 255, 0.55); }

.round-status {
  color: var(--yellow);
  font-size: 0.82rem;
  font-weight: 950;
  letter-spacing: 0.09em;
  text-align: center;
}

.play-stage { display: block; }
.rail-deck { display: none; }

.arena-wrap {
  position: relative;
  overflow: hidden;
  border-radius: 24px;
  border: 1px solid rgba(255,255,255,0.2);
  background: #0d0b1b;
  box-shadow: 0 26px 70px rgba(0, 0, 0, 0.5), 0 0 70px rgba(127, 72, 255, 0.1);
}

.touch-rail { display: none; }

canvas {
  width: 100%;
  height: auto;
  max-height: 62svh;
  display: block;
  aspect-ratio: 48 / 31;
  touch-action: none;
  outline: none;
}

canvas:focus-visible { box-shadow: inset 0 0 0 4px var(--yellow); }

.start-card {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(8, 7, 19, 0.73);
  backdrop-filter: blur(7px);
  text-align: center;
  transition: opacity 160ms ease, visibility 160ms ease;
}

.start-card.hidden { opacity: 0; visibility: hidden; pointer-events: none; }
.ball-preview { font-size: clamp(3rem, 10vw, 5.5rem); filter: drop-shadow(0 0 24px rgba(255,228,94,.7)); }
.start-card h1 { margin: 6px 0 4px; font-size: clamp(2rem, 7vw, 4.5rem); line-height: 1; }
.start-card p { max-width: 500px; margin: 8px 0 20px; color: #d6d2e3; font-size: clamp(1rem, 2.7vw, 1.2rem); line-height: 1.45; }

.primary-button {
  min-height: 52px;
  padding: 0 26px;
  border: 0;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--yellow), #ff9f45);
  color: #171020;
  font-weight: 950;
  letter-spacing: 0.06em;
  cursor: pointer;
  box-shadow: 0 10px 28px rgba(255, 194, 69, 0.32);
}

.primary-button:active, .mode-button:active, .icon-button:active { transform: translateY(1px) scale(0.98); }

.controls {
  border-radius: 18px;
  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.mode-switch { display: flex; gap: 6px; }
.mode-button {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0 16px;
  background: rgba(255,255,255,.05);
  color: #c8c3d8;
  font-weight: 900;
  cursor: pointer;
}
.mode-button.active { color: #10101c; background: white; }

.control-help {
  display: flex;
  justify-content: flex-end;
  gap: 22px;
  color: var(--muted);
  font-size: 0.86rem;
}
.control-help b { color: white; }

@media (max-width: 660px) {
  body { align-items: start; padding-top: max(8px, env(safe-area-inset-top)); }
  .game-shell { gap: 8px; }
  .topbar { min-height: 48px; border-radius: 14px; }
  .scoreboard { border-radius: 14px; padding: 8px 12px; }
  .player-score { flex-direction: column; gap: 2px; font-size: 0.68rem; }
  .player-two { flex-direction: column; }
  .round-status { font-size: 0.66rem; }
  .play-stage {
    display: block;
  }
  .rail-deck {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-top: 7px;
  }
  .touch-rail {
    position: relative;
    display: block;
    height: 96px;
    overflow: hidden;
    border: 1px solid rgba(255,255,255,.18);
    border-radius: 13px;
    background: rgba(255,255,255,.06);
    touch-action: none;
    user-select: none;
  }
  .left-rail { box-shadow: inset 0 0 18px rgba(255,60,171,.14); }
  .right-rail { box-shadow: inset 0 0 18px rgba(36,228,255,.14); }
  .touch-rail.inactive { opacity: .28; }
  .rail-label {
    position: absolute;
    top: 8px;
    left: 10px;
    right: auto;
    color: rgba(255,255,255,.72);
    font-size: .63rem;
    font-weight: 950;
    text-align: center;
  }
  .rail-thumb {
    position: absolute;
    top: calc(50% - 18px);
    left: calc(50% - 32px);
    width: 64px;
    height: 36px;
    display: grid;
    place-items: center;
    border-radius: 10px;
    background: rgba(255,255,255,.16);
    color: white;
    font-size: 1.25rem;
    font-weight: 950;
    box-shadow: 0 5px 15px rgba(0,0,0,.28);
  }
  .left-rail .rail-thumb { background: rgba(255,60,171,.42); }
  .right-rail .rail-thumb { background: rgba(36,228,255,.35); }
  .arena-wrap { border-radius: 18px; }
  canvas { object-fit: contain; }
  .start-card { padding: 10px 16px 12px; }
  .ball-preview { font-size: clamp(2.5rem, 11vw, 3.4rem); }
  .start-card h1 { margin: 1px 0; font-size: clamp(2rem, 10vw, 3rem); }
  .start-card p { margin: 3px 0 10px; font-size: clamp(.9rem, 4vw, 1.05rem); line-height: 1.3; }
  .primary-button { min-height: 44px; max-width: 88%; padding: 0 20px; font-size: .88rem; }
  .controls { border-radius: 14px; flex-direction: column; align-items: stretch; }
  .mode-switch { display: grid; grid-template-columns: 1fr 1fr; }
  .mode-button { padding: 0 8px; font-size: .78rem; }
  .control-help { justify-content: space-between; gap: 10px; font-size: 0.72rem; }
  .control-help span { max-width: 48%; }
}

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