/* ---------- Theme ---------- */
:root {
  --bg: #111318;
  --surface: #1a1d24;
  --surface-2: #232731;
  --border: #2e333f;
  --text: #ecebe7;
  --text-muted: #9aa0ad;
  --accent: #f5b942;        /* beat 1 */
  --accent-ink: #1d1503;
  --primary: #4fc39a;       /* running / primary actions */
  --primary-ink: #04150e;
  --secondary: #8fb4ff;     /* secondary accent (6/8 beat 4) */
  --danger: #ff6b6b;
  --wave: #7fb2ff;
  --grid: rgba(236, 235, 231, 0.22);
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 20px rgba(0,0,0,.18);
  --radius: 14px;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f2ee;
    --surface: #ffffff;
    --surface-2: #efede8;
    --border: #dedad2;
    --text: #1c1d21;
    --text-muted: #646a75;
    --accent: #d18a00;
    --accent-ink: #ffffff;
    --primary: #1f9a70;
    --primary-ink: #ffffff;
    --secondary: #3f6fd8;
    --danger: #d63c3c;
    --wave: #3f6fd8;
    --grid: rgba(28, 29, 33, 0.2);
    --shadow: 0 1px 2px rgba(0,0,0,.06), 0 6px 20px rgba(0,0,0,.06);
    color-scheme: light;
  }
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
h2 { font-size: 1.05rem; margin: 0 0 .6rem; display: flex; align-items: center; gap: .5rem; }
p { margin: .4rem 0; }
small, .small { font-size: .82rem; }
.muted { color: var(--text-muted); }
.center { text-align: center; justify-content: center; }
.grow { flex: 1 1 auto; min-width: 0; }
[hidden] { display: none !important; }
kbd {
  font: inherit; font-size: .78rem; padding: 0 .35em; border: 1px solid var(--border);
  border-radius: 4px; background: var(--surface-2);
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
button:disabled { opacity: .4; cursor: default; }
:focus-visible { outline: 2px solid var(--secondary); outline-offset: 2px; }

main { max-width: 640px; margin: 0 auto; padding: 0 16px 32px; }

/* ---------- Header & tabs ---------- */
.topbar {
  max-width: 640px; margin: 0 auto; padding: 12px 16px 4px;
  display: flex; align-items: center; justify-content: space-between;
  padding-top: max(12px, env(safe-area-inset-top));
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.2rem; letter-spacing: .01em; }
.tabs {
  position: sticky; top: 0; z-index: 5;
  max-width: 640px; margin: 0 auto 12px; padding: 8px 16px;
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px;
  background: var(--bg);
}
.tabs button {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 9px 4px; font-weight: 600; color: var(--text-muted);
}
.tabs button[aria-selected="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }

/* ---------- Generic UI ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 14px; box-shadow: var(--shadow);
}
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 10px;
  padding: 10px 14px; min-height: 44px; font-weight: 600; white-space: nowrap;
}
.btn.small { padding: 6px 10px; min-height: 36px; font-size: .88rem; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.danger { color: var(--danger); }
.icon-btn {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 10px;
  min-width: 44px; min-height: 44px; font-size: 1rem;
}
.icon-btn.small { min-width: 34px; min-height: 34px; font-size: .9rem; }
.icon-btn.subtle { background: transparent; }
.round-btn {
  width: 56px; height: 56px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface-2); font-size: 1.8rem; line-height: 1; flex: none;
  user-select: none; -webkit-user-select: none; touch-action: manipulation;
}
.round-btn.small { width: 40px; height: 40px; font-size: 1.3rem; }

input[type="text"], input:not([type]), input[type="number"], select {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 9px 12px; min-height: 44px; width: 100%; min-width: 0;
}
.row > input, .row > select { flex: 1 1 auto; width: auto; }
input[type="range"] { width: 100%; accent-color: var(--primary); height: 32px; }

.field { margin: 0 0 14px; }
.field:last-child { margin-bottom: 0; }
.field .label, .field > .label { display: block; font-size: .85rem; color: var(--text-muted); margin-bottom: 6px; font-weight: 600; }
.field.inline { display: flex; align-items: center; justify-content: space-between; gap: 12px; cursor: pointer; }

.seg {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 4px;
}
.seg button {
  border: 0; background: transparent; border-radius: 9px; padding: 9px 4px; min-height: 40px;
  font-weight: 600; color: var(--text-muted);
}
.seg button small { display: block; font-weight: 400; font-size: .72rem; }
.seg button[aria-checked="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.25); }

.switch {
  appearance: none; -webkit-appearance: none; flex: none;
  width: 48px; height: 28px; border-radius: 999px; background: var(--border);
  position: relative; cursor: pointer; transition: background .15s; margin: 0;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px;
  border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.3);
}
.switch:checked { background: var(--primary); }
.switch:checked::after { transform: translateX(20px); }

.tip {
  display: flex; gap: 10px; align-items: center; justify-content: space-between;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px;
  padding: 10px 12px; margin: 0 0 12px;
}
.tip.warn { border-color: var(--danger); }
.count {
  font-size: .75rem; font-weight: 700; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 999px; padding: 0 8px; color: var(--text-muted);
}

/* ---------- Beat display ---------- */
.beat-panel {
  border-radius: var(--radius); padding: 22px 12px 14px; margin-bottom: 14px;
  background: var(--surface); border: 1px solid var(--border); text-align: center;
  transition: background .25s ease-out; box-shadow: var(--shadow);
}
.beat-dots { display: flex; justify-content: center; gap: clamp(8px, 3vw, 16px); flex-wrap: wrap; }
.beat-dot {
  width: clamp(30px, 11vw, 50px); aspect-ratio: 1; border-radius: 50%;
  background: var(--surface-2); border: 2px solid var(--border);
  transition: background .12s ease-out, transform .12s ease-out, border-color .12s;
}
.beat-dot.accent { border-color: var(--accent); }
.beat-dot.secondary { border-color: var(--secondary); }
.beat-dot.on { background: var(--text); border-color: var(--text); transform: scale(1.12); }
.beat-dot.accent.on { background: var(--accent); border-color: var(--accent); }
.beat-dot.secondary.on { background: var(--secondary); border-color: var(--secondary); }
.beat-panel.silent .beat-dot.on { background: transparent; border-style: dashed; border-color: var(--text-muted); }
.beat-panel.silent .beat-dot.accent.on { border-color: var(--accent); }
.beat-panel.flash { background: color-mix(in srgb, var(--accent) 22%, var(--surface)); transition: none; }
.bar-status { margin-top: 12px; font-weight: 700; font-size: 1.05rem; letter-spacing: .02em; color: var(--text-muted); min-height: 1.5em; }
.beat-panel.silent .bar-status { color: var(--accent); }
.beat-dots.mini { margin: 8px 0 12px; }
.beat-dots.mini .beat-dot { width: 22px; }

/* ---------- Tempo ---------- */
.tempo-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.tempo-display { text-align: center; flex: 1; min-width: 0; }
#bpm-input {
  font-size: clamp(3rem, 16vw, 4.5rem); font-weight: 800; text-align: center; line-height: 1.05;
  background: transparent; border: 0; padding: 0; min-height: 0; width: 100%;
  font-variant-numeric: tabular-nums; -moz-appearance: textfield; appearance: textfield;
}
#bpm-input::-webkit-outer-spin-button, #bpm-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.tempo-meta { color: var(--text-muted); font-size: .9rem; font-weight: 600; }
.tempo input[type="range"] { margin: 10px 0 8px; }
#tap-btn { min-width: 160px; }
#tap-btn.tapped { background: var(--secondary); color: #fff; }

.start-btn {
  display: block; width: 100%; border: 0; border-radius: var(--radius); min-height: 68px;
  font-size: 1.4rem; font-weight: 800; letter-spacing: .03em; margin-bottom: 14px;
  background: var(--primary); color: var(--primary-ink); box-shadow: var(--shadow);
}
.start-btn[aria-pressed="true"] { background: var(--text); color: var(--bg); }

.steppers { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 10px; transition: opacity .15s; }
.steppers.off { opacity: .45; }
.stepper { background: var(--surface-2); border-radius: 12px; padding: 10px; text-align: center; }
.stepper .label { font-size: .82rem; color: var(--text-muted); font-weight: 600; }
.stepper-ctl { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 6px; }
.stepper output { font-size: 1.7rem; font-weight: 800; min-width: 2ch; font-variant-numeric: tabular-nums; }
.shortcuts { margin-top: 4px; }

/* ---------- Setlist bar ---------- */
.setlist-bar {
  display: flex; align-items: center; gap: 8px; margin-bottom: 12px; padding: 8px;
  background: var(--surface); border: 1px solid var(--accent); border-radius: var(--radius);
}
.setlist-bar-mid { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.setlist-name { font-size: .75rem; font-weight: 700; color: var(--accent); text-transform: uppercase; letter-spacing: .06em; }
.setlist-bar select { min-height: 38px; padding: 6px 10px; font-weight: 700; }

/* ---------- Record ---------- */
.rec-controls { display: flex; align-items: center; justify-content: center; gap: 20px; margin: 6px 0 12px; }
.rec-btn {
  display: inline-flex; align-items: center; gap: 12px; padding: 14px 28px; min-height: 64px;
  border-radius: 999px; border: 2px solid var(--danger); background: transparent;
  font-size: 1.25rem; font-weight: 800; color: var(--danger);
}
.rec-dot { width: 20px; height: 20px; border-radius: 50%; background: var(--danger); }
.rec-btn[aria-pressed="true"] { background: var(--danger); color: #fff; }
.rec-btn[aria-pressed="true"] .rec-dot { border-radius: 4px; background: #fff; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .4; } }
.rec-time { font-size: 1.8rem; font-weight: 800; font-variant-numeric: tabular-nums; min-width: 4.5ch; }
.meter-row { display: flex; align-items: center; gap: 10px; }
.meter { flex: 1; height: 10px; border-radius: 999px; background: var(--surface-2); overflow: hidden; border: 1px solid var(--border); }
#level-bar { height: 100%; width: 0; background: linear-gradient(90deg, var(--primary) 0 70%, var(--accent) 85%, var(--danger)); transition: width .05s linear; }
.options { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 10px; }
.options summary { cursor: pointer; font-weight: 600; padding: 6px 0 10px; }

/* ---------- Lists ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.list-item { border-top: 1px solid var(--border); padding: 12px 0; }
.list-item:first-child { border-top: 0; padding-top: 4px; }
.list-item.current .list-main strong::after { content: " • current"; color: var(--accent); font-size: .78rem; font-weight: 600; }
.list-main { display: flex; flex-direction: column; min-width: 0; margin-bottom: 8px; overflow-wrap: anywhere; }
.list-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.setlist { border: 1px solid var(--border); border-radius: 12px; padding: 12px; margin-top: 12px; }
.setlist.current { border-color: var(--accent); }
.setlist-head { display: flex; flex-direction: column; gap: 8px; margin-bottom: 6px; }
.setlist-songs { list-style: none; margin: 0 0 10px; padding: 0; counter-reset: s; }
.setlist-songs li { display: flex; align-items: center; gap: 6px; padding: 4px 0; counter-increment: s; }
.setlist-songs li::before { content: counter(s) "."; color: var(--text-muted); min-width: 1.6em; font-weight: 600; }
.setlist-songs li .grow { overflow-wrap: anywhere; }

/* ---------- Player ---------- */
.player { margin-top: 10px; }
.wave-main {
  display: block; width: 100%; height: 130px; border-radius: 10px; background: var(--surface-2);
  border: 1px solid var(--border); touch-action: none; cursor: pointer;
}
.wave-overview {
  display: block; width: 100%; height: 34px; margin-top: 6px; border-radius: 8px;
  background: var(--surface-2); border: 1px solid var(--border); touch-action: none; cursor: pointer;
}
.player-controls { display: flex; align-items: center; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.player-time { font-variant-numeric: tabular-nums; font-weight: 600; }
.legend .sw { display: inline-block; width: 12px; height: 3px; vertical-align: middle; margin-right: 4px; }
.legend .sw.bar { background: var(--accent); }
.legend .sw.beat { background: var(--grid); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translate(-50%, 30px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  font-weight: 600; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
  max-width: calc(100vw - 32px); z-index: 20; box-shadow: var(--shadow);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
