:root {
  --bg: #0b0f1a;
  --panel: #141a2a;
  --panel-2: #1c2438;
  --line: #2a3450;
  --text: #eef2fb;
  --muted: #9aa6c2;
  --accent: #3aa0ff;
  --accent-2: #9fd6ff;
  --gold: #ffcf4a;
  --good: #3ddc84;
  --bad: #ff4d5e;
  --radius: 14px;
  --tap: 48px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 17px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}
button, input { font: inherit; color: inherit; }
#app { height: 100dvh; }

.screen { display: none; height: 100%; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.screen.active { display: flex; flex-direction: column; }

/* Buttons */
.btn {
  min-height: var(--tap); min-width: var(--tap);
  padding: 0 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel-2); cursor: pointer; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  touch-action: manipulation; white-space: nowrap;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #04101f; }
.btn.big { min-height: 60px; font-size: 1.15rem; padding: 0 28px; }
.btn.icon { padding: 0; width: var(--tap); font-size: 1.3rem; }
.btn.on { background: var(--gold); border-color: var(--gold); color: #1d1500; }
.btn:disabled { opacity: 0.4; }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.seg button { min-height: 44px; min-width: 64px; padding: 0 14px; border: 0; background: var(--panel-2); font-weight: 600; cursor: pointer; }
.seg button.active { background: var(--accent); color: #04101f; }

/* Home */
.home-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 20px 8px; flex-wrap: wrap; }
.home-header h1 { margin: 0; font-size: 1.8rem; }
.home-actions { display: flex; gap: 10px; }
.filters { display: flex; gap: 10px; padding: 8px 20px 14px; overflow-x: auto; scrollbar-width: none; }
.chip { min-height: 44px; padding: 0 18px; border-radius: 22px; border: 1px solid var(--line); background: var(--panel); font-weight: 600; cursor: pointer; white-space: nowrap; }
.chip.active { background: var(--text); color: var(--bg); }
.grid { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 4px 20px 28px; display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); align-content: start; }
.card {
  position: relative; text-align: left; min-height: 150px; padding: 18px; border-radius: var(--radius);
  background: linear-gradient(160deg, var(--panel-2), var(--panel)); border: 1px solid var(--line);
  cursor: pointer; display: flex; flex-direction: column; gap: 6px;
}
.card:active { transform: scale(0.98); }
.card h3 { margin: 0; font-size: 1.15rem; line-height: 1.25; padding-right: 30px; }
.card .composer { color: var(--muted); font-size: 0.95rem; }
.card .meta { margin-top: auto; display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: 0.9rem; }
.card .diff { color: var(--gold); letter-spacing: 2px; }
.card .best { color: var(--good); font-weight: 700; }
.card .del { position: absolute; top: 6px; right: 6px; width: 44px; height: 44px; border: 0; background: transparent; color: var(--muted); font-size: 1.2rem; cursor: pointer; }
.empty { color: var(--muted); grid-column: 1 / -1; text-align: center; padding: 40px 0; }

/* Mic setup */
#screen-mic { align-items: center; justify-content: center; overflow-y: auto; }
.mic-box { width: min(560px, 92vw); text-align: center; display: flex; flex-direction: column; gap: 14px; align-items: center; padding: 20px 0; }
.mic-box h2 { margin: 0; font-size: 1.7rem; }
.mic-box p { margin: 0; color: var(--muted); }
.mic-status { color: var(--text) !important; font-weight: 600; }
#mic-live { width: 100%; display: flex; flex-direction: column; gap: 14px; align-items: center; }
#mic-live[hidden] { display: none; }
.meter { position: relative; width: 100%; height: 22px; border-radius: 11px; background: var(--panel-2); overflow: hidden; border: 1px solid var(--line); }
#mic-meter-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--good), var(--gold)); transition: width 60ms linear; }
#mic-meter-th { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--bad); }
.heard-box { width: 180px; height: 120px; border-radius: 18px; border: 2px solid var(--line); background: var(--panel); display: flex; align-items: center; justify-content: center; font-size: 3.4rem; font-weight: 800; }
.heard-box.ok { border-color: var(--good); color: var(--good); }
.slider-row { display: flex; align-items: center; gap: 12px; width: 100%; color: var(--muted); }
.slider-row input { flex: 1; }
.mic-buttons { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }
.debug { font: 13px ui-monospace, Menlo, monospace; color: var(--muted); }
input[type=range] { min-height: 44px; accent-color: var(--accent); }

/* Play */
#screen-play { background: #05070c; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 6px 10px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.topbar .title { font-weight: 700; font-size: 1.05rem; max-width: 26ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.progress-wrap { flex: 1; min-width: 140px; display: flex; align-items: center; gap: 8px; }
.progress { flex: 1; height: 10px; border-radius: 5px; background: var(--panel-2); overflow: hidden; }
#progress-fill { height: 100%; width: 0; background: var(--accent); }
#progress-text { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 0.9rem; min-width: 56px; text-align: right; }
.speed { display: flex; align-items: center; gap: 6px; }
.speed input { width: 120px; }
#speed-label { min-width: 46px; font-variant-numeric: tabular-nums; font-weight: 700; }
.stage { position: relative; flex: 1; min-height: 0; }
#lane { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.bigstart {
  position: absolute; left: 50%; bottom: 34%; transform: translateX(-50%);
  min-height: 72px; padding: 0 40px; border-radius: 36px; border: 0; font-size: 1.5rem; font-weight: 800;
  background: var(--accent); color: #04101f; box-shadow: 0 8px 30px rgba(58,160,255,0.45); cursor: pointer;
}
.bigstart[hidden] { display: none; }
.stage-hint { position: absolute; top: 14px; left: 50%; transform: translateX(-50%); background: rgba(255,207,74,0.95); color: #1d1500; padding: 10px 18px; border-radius: 12px; font-weight: 700; pointer-events: none; text-align: center; }
.debug-panel { position: absolute; top: 10px; right: 10px; background: rgba(0,0,0,0.7); padding: 8px 12px; border-radius: 10px; font: 13px ui-monospace, Menlo, monospace; pointer-events: none; white-space: pre; }
.controls { display: flex; align-items: center; gap: 8px; padding: 8px 10px; background: var(--panel); border-top: 1px solid var(--line); flex-wrap: wrap; }
.controls .wide { min-width: 120px; }
.heard { margin-left: auto; display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 14px; border-radius: 22px; background: var(--panel-2); font-weight: 700; font-variant-numeric: tabular-nums; }
.heard .dot { width: 12px; height: 12px; border-radius: 50%; background: #444; transition: transform 60ms linear, background 60ms; }
.heard .dot.live { background: var(--good); }
.heard .dot.midi { background: var(--accent); }

/* Results */
#screen-results { align-items: center; justify-content: center; }
.results-box { width: min(620px, 92vw); text-align: center; display: flex; flex-direction: column; gap: 16px; }
.results-box h2 { margin: 0; font-size: 2rem; }
.stars { font-size: 3.6rem; color: var(--gold); letter-spacing: 8px; }
.stars .off { color: #3a4258; }
.res-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.res-grid div { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 8px; display: flex; flex-direction: column; }
.res-grid span { font-size: 2rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.res-grid small { color: var(--muted); }
.res-best { color: var(--good); margin: 0; min-height: 1.4em; font-weight: 600; }
.res-buttons { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }

/* Dialogs */
dialog { border: 1px solid var(--line); border-radius: 18px; background: var(--panel); color: var(--text); padding: 0; width: min(560px, 94vw); max-height: 90dvh; }
dialog::backdrop { background: rgba(0,0,0,0.6); }
.sheet { padding: 20px 22px; display: flex; flex-direction: column; gap: 12px; }
.sheet h2 { margin: 0 0 4px; }
.sheet h3 { margin: 8px 0 0; font-size: 1rem; color: var(--muted); }
.sheet p, .sheet li { color: #cfd6e8; }
.set-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; }
.set-row.col { flex-direction: column; align-items: stretch; gap: 2px; }
.set-row span small { display: block; color: var(--muted); font-size: 0.8rem; }
.set-row input[type=text], .bar-range input { min-height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel-2); padding: 0 10px; }
.bar-range input { width: 76px; }
.toggle { appearance: none; -webkit-appearance: none; width: 56px; height: 32px; border-radius: 16px; background: #3a4258; position: relative; cursor: pointer; flex: none; transition: background 0.15s; }
.toggle::after { content: ''; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; transition: transform 0.15s; }
.toggle:checked { background: var(--good); }
.toggle:checked::after { transform: translateX(24px); }
.sheet-buttons { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; margin-top: 6px; }
.track-list { display: flex; flex-direction: column; gap: 8px; }
.track { display: flex; align-items: center; gap: 10px; min-height: 52px; padding: 6px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel-2); cursor: pointer; }
.track input { width: 22px; height: 22px; }
.track small { color: var(--muted); display: block; }
.muted { color: var(--muted) !important; margin: 0; }
.error { color: var(--bad) !important; margin: 0; }

.toast { position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: #000d; border: 1px solid var(--line); padding: 12px 20px; border-radius: 12px; z-index: 50; max-width: 90vw; text-align: center; }

/* Portrait: stack, keep targets large */
@media (orientation: portrait) {
  .topbar .title { flex: 1 1 0; max-width: none; }
  .progress-wrap { order: 5; flex-basis: 100%; }
  .controls .btn { flex: 1 1 auto; }
  .heard { margin-left: 0; }
}
@media (max-width: 700px) {
  .speed input { width: 90px; }
  .btn { padding: 0 12px; }
}
@media (prefers-reduced-motion: reduce) {
  .btn:active, .card:active { transform: none; }
}
