:root {
  --bg: #07060c;
  --panel: rgba(14, 10, 22, 0.94);
  --line: rgba(192, 132, 252, 0.55);
  --violet: #c084fc;
  --pink: #ff4fa3;
  --text: #f7f2ff;
  --muted: #b7a8cc;
  --gold: #ffd36a;
  --ok: #86efac;
  --danger: #fb7185;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
  background:
    radial-gradient(900px 480px at 50% 0%, rgba(124, 58, 237, 0.28), transparent 60%),
    radial-gradient(700px 420px at 80% 100%, rgba(255, 79, 163, 0.12), transparent 55%),
    var(--bg);
}

button, input { font: inherit; color: inherit; }

#app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 8px 14px calc(8px + env(safe-area-inset-top));
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.mark {
  width: 42px;
  height: 42px;
  flex: none;
  filter: drop-shadow(0 0 8px rgba(255, 79, 163, 0.65));
}

.brand h1 {
  margin: 0;
  font-size: clamp(16px, 2.4vw, 26px);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  line-height: 1;
}

.brand p {
  margin: 3px 0 0;
  color: var(--violet);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  font-size: 11px;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.top-actions .btn.tiny {
  min-height: 44px;
  padding: 8px 12px;
}

.player-chip {
  max-width: 9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid rgba(192, 132, 252, 0.35);
  background: rgba(10, 8, 16, 0.65);
  color: var(--gold);
  font-size: 13px;
  letter-spacing: 0.04em;
}

.cabinet-row {
  flex: 1;
  min-height: 0;
  display: flex;
  justify-content: center;
  padding: 0 0 env(safe-area-inset-bottom);
}

#stage {
  position: relative;
  width: min(480px, 100%);
  height: 100%;
  background: #09070f;
  border: 1px solid rgba(192, 132, 252, 0.4);
  border-radius: 22px 22px 0 0;
  box-shadow: 0 0 50px rgba(168, 85, 247, 0.2), inset 0 0 30px rgba(0, 0, 0, 0.35);
  overflow: hidden;
  touch-action: none;
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

#hud {
  display: none;
  position: absolute;
  top: 8px;
  left: 8px;
  right: 8px;
  z-index: 3;
  pointer-events: none;
}

#hud.show { display: block; }

.hud-top, .hud-sub {
  display: flex;
  gap: 6px;
  align-items: center;
}

.hud-sub { margin-top: 6px; min-height: 22px; }

.chip {
  background: rgba(8, 6, 14, 0.78);
  border: 1px solid rgba(192, 132, 252, 0.35);
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 12px;
  letter-spacing: 0.04em;
}

.chip.score, .chip.best {
  font-weight: 700;
  color: var(--gold);
  font-variant-numeric: tabular-nums;
}

.chip.best { font-weight: 600; color: #f5d0fe; }

.chip.lives { color: var(--pink); letter-spacing: 0.08em; }
.chip.hot { color: #f5d0fe; border-color: rgba(244, 114, 182, 0.7); }
.chip.hot.is-on { animation: multpulse 0.85s ease-in-out infinite; }
.chip.combo { color: #ffd36a; border-color: rgba(255, 211, 106, 0.75); font-weight: 700; }
.chip.effect { color: var(--gold); }

#hud-level { margin-right: auto; max-width: 46%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

#aim-hint {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: calc(74px + env(safe-area-inset-bottom));
  z-index: 4;
  margin: 0;
  text-align: center;
  pointer-events: none;
  color: #f9a8d4;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-size: 11px;
}

#btn-drop {
  position: absolute;
  z-index: 5;
  left: 50%;
  bottom: calc(14px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  min-width: 148px;
  min-height: 48px;
  animation: pulse 1.4s ease-in-out infinite;
}

#nudge-bar {
  position: absolute;
  z-index: 5;
  left: 12px;
  right: 12px;
  bottom: calc(14px + env(safe-area-inset-bottom));
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.nudge-count {
  font-size: 12px;
  letter-spacing: 0.14em;
  color: var(--muted);
}

.btn.nudge {
  width: 72px;
  height: 48px;
  padding: 0;
}

.screen {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 20;
  overflow: auto;
  padding: 16px 12px 20px;
  background: rgba(7, 6, 12, 0.62);
}

.screen.active {
  display: flex;
  align-items: safe center;
  justify-content: center;
}

.panel {
  width: min(420px, 100%);
  margin: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 18px 16px 16px;
  box-shadow: 0 16px 50px rgba(0, 0, 0, 0.35), 0 0 28px rgba(168, 85, 247, 0.16);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--violet);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  font-size: 11px;
}

h2 {
  margin: 0 0 8px;
  font-size: 28px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.lede, .fine {
  margin: 0 0 12px;
  color: var(--muted);
  line-height: 1.4;
  font-size: 14px;
}

.fine { font-size: 12px; margin-bottom: 0; }

.steps {
  margin: 0 0 14px;
  padding-left: 18px;
  color: #efe7ff;
}

.steps li { margin: 4px 0; }

.stack, .row-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.row-actions { margin-top: 12px; }

.btn {
  appearance: none;
  border: 1px solid rgba(216, 180, 254, 0.7);
  background: linear-gradient(180deg, #3a2458, #221433);
  color: white;
  border-radius: 999px;
  min-height: 44px;
  padding: 10px 16px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 0 16px rgba(168, 85, 247, 0.22);
  touch-action: manipulation;
}

.btn:hover { filter: brightness(1.08); }

.btn:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

.btn:disabled {
  cursor: not-allowed;
  filter: grayscale(0.4);
  opacity: 0.55;
}

.btn.primary {
  background: linear-gradient(180deg, #ff4fa3, #a21caf);
  border-color: #ffd0ea;
}

.btn.ghost {
  background: transparent;
  box-shadow: none;
}

.btn.tiny {
  min-height: 36px;
  padding: 6px 10px;
  font-size: 12px;
  letter-spacing: 0.1em;
}

.btn.is-on {
  background: linear-gradient(180deg, #ff4fa3, #7c3aed);
}

input[type="text"] {
  width: 100%;
  margin: 8px 0 4px;
  background: #0c0912;
  border: 1px solid rgba(192, 132, 252, 0.65);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 18px;
  text-align: center;
  letter-spacing: 0.08em;
}

input[type="text"]:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

.form-error {
  min-height: 1.2em;
  margin: 0 0 8px;
  color: var(--danger);
  font-size: 13px;
}

.level-list, .board-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: min(48vh, 380px);
  overflow: auto;
  margin: 8px 0 4px;
  padding-right: 2px;
}

.level-btn {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: center;
  text-align: left;
  text-transform: none;
  letter-spacing: 0;
  border-radius: 14px;
  width: 100%;
}

.lv-index {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: rgba(255, 79, 163, 0.18);
  color: var(--pink);
  font-weight: 700;
}

.lv-copy { display: flex; flex-direction: column; min-width: 0; }
.lv-copy strong { font-size: 15px; }
.lv-copy small {
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 0;
  text-transform: none;
  font-weight: 400;
}

.lv-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  font-style: normal;
  gap: 2px;
}

.lv-meta em { font-style: normal; color: var(--ok); }
.lv-meta b { color: var(--gold); font-size: 12px; }
.medal-bronze { color: #e0a06a; }
.medal-silver { color: #e5e7eb; }
.medal-gold { color: var(--gold); }

.badge-row { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 8px; }
.badge-row:not(:has(.new-best:not(.hidden))) { display: none; }
.badge-row .new-best { margin: 0; }

.unlocks { margin: 0 0 12px; color: var(--gold); }

.ball-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: min(52vh, 420px);
  overflow: auto;
  margin: 8px 0 12px;
}

.ball-btn {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 10px;
  align-items: center;
  text-align: left;
  text-transform: none;
  letter-spacing: 0;
  border-radius: 14px;
  width: 100%;
}

.swatch {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.75);
  box-shadow: 0 0 12px rgba(192, 132, 252, 0.35);
}

.toast {
  position: absolute;
  z-index: 18;
  left: 12px;
  right: 12px;
  top: 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 12px;
  border-radius: 14px;
  background: rgba(14, 10, 22, 0.94);
  border: 1px solid rgba(255, 211, 106, 0.7);
  box-shadow: 0 0 18px rgba(255, 79, 163, 0.25);
  pointer-events: none;
}

.toast strong {
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--gold);
  font-size: 13px;
}

.toast span { color: var(--muted); font-size: 12px; }

.level-btn.is-locked .lv-meta em { color: var(--muted); }

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 0 0 10px;
}

.stats div {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(192, 132, 252, 0.2);
  border-radius: 12px;
  padding: 8px 10px;
}

.stats dt {
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.stats dd {
  margin: 2px 0 0;
  font-size: 20px;
  font-variant-numeric: tabular-nums;
}

.bonus { margin: 0 0 12px; color: #f5d0fe; }

.new-best {
  display: inline-block;
  margin: 0 0 8px;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(255, 211, 106, 0.15);
  color: var(--gold);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-size: 12px;
}

.tabs, .level-tabs { display: flex; flex-wrap: wrap; gap: 6px; }
.level-tabs { margin: 8px 0 4px; }
.level-tabs .btn.tiny { min-width: 36px; padding-left: 8px; padding-right: 8px; }

#screen-complete h2 { color: #bbf7d0; }
#screen-over h2 { color: #fecdd3; }

.board-row {
  display: grid;
  grid-template-columns: 22px 1fr auto auto auto;
  gap: 6px;
  align-items: center;
  padding: 7px 4px;
  border-bottom: 1px solid rgba(192, 132, 252, 0.16);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.board-row span:nth-child(4) { color: var(--gold); }
.board-row.is-dnf { opacity: 0.72; }
.board-row.is-dnf span:last-child { color: #fb7185; letter-spacing: 0.06em; }
.board-row span:nth-child(2) {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty-board {
  margin: 8px 0;
  color: var(--muted);
}

.boot-error {
  position: absolute;
  z-index: 30;
  left: 12px;
  right: 12px;
  bottom: 12px;
  margin: 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: #3f1218;
  color: #ffe4e6;
  border: 1px solid var(--danger);
}

.hidden { display: none !important; }

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 12px rgba(255, 79, 163, 0.35); }
  50% { box-shadow: 0 0 22px rgba(255, 79, 163, 0.75); }
}

@keyframes multpulse {
  0%, 100% { box-shadow: 0 0 0 rgba(244, 114, 182, 0); }
  50% { box-shadow: 0 0 10px rgba(244, 114, 182, 0.7); }
}

@media (max-width: 640px) {
  .topbar { padding-left: 10px; padding-right: 10px; }
  #stage {
    width: 100%;
    border-left: none;
    border-right: none;
    border-radius: 0;
  }
  h2 { font-size: 24px; }
  .brand h1 { letter-spacing: 0.08em; }
}

@media (max-height: 700px) {
  .panel { padding: 12px; }
  h2 { font-size: 22px; margin-bottom: 4px; }
  .lede, .steps { display: none; }
  .level-list, .board-list, .ball-list { max-height: 34vh; }
}
