:root {
  --bg: #f6f3ec;
  --surface: #ffffff;
  --surface-2: #efeae0;
  --ink: #1d2024;
  --muted: #6a6d72;
  --line: #ded8cc;
  --accent: #26407f;
  --accent-ink: #ffffff;
  --accent-soft: #e3e8f6;
  --gold: #e3a21a;
  --gold-soft: rgba(227, 162, 26, 0.18);
  --loop: rgba(38, 64, 127, 0.10);
  --danger: #b3261e;
  --ok: #2e7d4f;
  --shadow: 0 1px 2px rgba(40, 30, 10, .06), 0 6px 20px rgba(40, 30, 10, .07);
  --radius: 16px;
  --tap: 52px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16181b; --surface: #1f2226; --surface-2: #2a2e33; --ink: #ebe8e2; --muted: #a2a5aa;
    --line: #33373d; --accent: #9db2ff; --accent-ink: #10131a; --accent-soft: #273152;
    --gold: #f2b83b; --gold-soft: rgba(242, 184, 59, 0.20); --loop: rgba(157, 178, 255, 0.12);
    --danger: #ff8a80; --ok: #7fd4a1;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16181b; --surface: #1f2226; --surface-2: #2a2e33; --ink: #ebe8e2; --muted: #a2a5aa;
  --line: #33373d; --accent: #9db2ff; --accent-ink: #10131a; --accent-soft: #273152;
  --gold: #f2b83b; --gold-soft: rgba(242, 184, 59, 0.20); --loop: rgba(157, 178, 255, 0.12);
  --danger: #ff8a80; --ok: #7fd4a1; color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 17px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased; overscroll-behavior-y: none;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 24px; height: 24px; flex: none; }
h1, h2, h3, h4 { margin: 0; line-height: 1.2; }
h2 { font-size: 20px; }
h4 { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 22px 0 8px; font-weight: 650; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
[hidden] { display: none !important; }

/* ---------- home ---------- */
#view-home { max-width: 760px; margin: 0 auto; padding: calc(20px + var(--safe-t)) 16px calc(40px + var(--safe-b)); }
.brand { display: flex; align-items: center; gap: 14px; margin: 6px 0 22px; }
.logo { width: 52px; height: 52px; border-radius: 15px; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; box-shadow: var(--shadow); }
.logo svg { width: 30px; height: 30px; }
.brand h1 { font-size: 28px; letter-spacing: -.02em; font-weight: 750; }
.tagline { margin: 2px 0 0; color: var(--muted); }

.scan-card, .pending, .library { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 20px; box-shadow: var(--shadow); margin-bottom: 18px; }
.library { background: transparent; border: 0; box-shadow: none; padding: 8px 2px; }
.scan-card p { margin: 6px 0 16px; }
.scan-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 520px) { .scan-actions { grid-template-columns: 1fr; } }

.big-btn { display: flex; align-items: center; justify-content: center; gap: 10px; min-height: 64px; padding: 12px 18px;
  border-radius: 14px; border: 1.5px solid var(--line); background: var(--surface); font-weight: 650; font-size: 18px;
  cursor: pointer; user-select: none; transition: transform .08s ease, background .15s; }
.big-btn:active { transform: scale(.98); }
.big-btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.big-btn.wide { width: 100%; margin-top: 18px; }
.big-btn[disabled] { opacity: .5; pointer-events: none; }

.pending-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.thumbs { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 6px; }
.thumb { position: relative; width: 96px; height: 128px; flex: none; border-radius: 10px; overflow: hidden;
  background: var(--surface-2); border: 1px solid var(--line); display: grid; place-items: center; font-size: 13px; color: var(--muted); text-align: center; padding: 4px; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb .x { position: absolute; top: 4px; right: 4px; width: 30px; height: 30px; border-radius: 50%; border: 0;
  background: rgba(0,0,0,.6); color: #fff; display: grid; place-items: center; }
.thumb .x svg { width: 16px; height: 16px; }
.thumb .n { position: absolute; left: 6px; bottom: 6px; background: rgba(0,0,0,.6); color: #fff; border-radius: 8px; padding: 0 7px; font-size: 13px; }
.row-actions { display: flex; flex-wrap: wrap; gap: 10px; margin: 12px 0 4px; }

.field { display: block; margin-top: 16px; }
.field > span { display: block; font-size: 14px; font-weight: 650; color: var(--muted); margin-bottom: 6px; }
.field input, .field select { width: 100%; min-height: var(--tap); border-radius: 12px; border: 1.5px solid var(--line);
  background: var(--bg); padding: 0 14px; font-size: 17px; }
.field input:focus, .field select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { flex: 1; min-height: 44px; border: 0; border-radius: 9px; background: transparent; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }

.chip-btn { display: inline-flex; align-items: center; gap: 6px; min-height: 44px; padding: 0 16px; border-radius: 22px;
  border: 1.5px solid var(--line); background: var(--surface); font-weight: 600; font-size: 15px; text-decoration: none; color: var(--ink); cursor: pointer; }
.chip-btn svg { width: 18px; height: 18px; }
.chip-btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.text-btn { border: 0; background: none; color: var(--accent); font-weight: 650; min-height: 44px; padding: 0 6px; }
.text-btn.danger, .danger { color: var(--danger); }

.lib-list { display: grid; gap: 10px; margin-top: 12px; }
.lib-item { display: flex; align-items: center; gap: 14px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 14px 14px 14px 16px; text-align: left; width: 100%; min-height: 76px; box-shadow: var(--shadow); }
.lib-item .ico { width: 46px; height: 46px; border-radius: 12px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; flex: none; }
.lib-item .meta { flex: 1; min-width: 0; }
.lib-item b { display: block; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-item small { color: var(--muted); font-size: 14px; }
.lib-item .bar { height: 6px; background: var(--surface-2); border-radius: 3px; margin-top: 8px; overflow: hidden; }
.lib-item .bar i { display: block; height: 100%; width: 10%; background: var(--accent); border-radius: 3px; transition: width .6s ease; }
.lib-item.err small { color: var(--danger); }
.empty { padding: 22px; text-align: center; color: var(--muted); border: 1.5px dashed var(--line); border-radius: 14px; }

/* ---------- player ---------- */
#view-player { display: flex; flex-direction: column; height: 100dvh; height: 100vh; }
@supports (height: 100dvh) { #view-player { height: 100dvh; } }
.topbar { display: flex; align-items: center; gap: 6px; padding: calc(6px + var(--safe-t)) 8px 6px;
  background: var(--surface); border-bottom: 1px solid var(--line); z-index: 5; }
.icon-btn { display: inline-grid; place-items: center; min-width: 48px; min-height: 48px; border-radius: 12px; border: 0; background: transparent; color: var(--ink); padding: 0; }
.icon-btn:active { background: var(--surface-2); }
.icon-btn.lg { min-width: 54px; min-height: 54px; }
.icon-btn .lbl { font-size: 11px; font-weight: 650; color: var(--muted); margin-top: -4px; }
.icon-btn.lg:has(.lbl) { grid-template-rows: auto auto; }
.icon-btn.toggle.on { background: var(--accent-soft); color: var(--accent); }
.title-btn { flex: 1; min-width: 0; border: 0; background: none; text-align: left; padding: 4px 6px; display: flex; flex-direction: column; }
.title-btn span { font-weight: 700; font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.title-btn small { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.fixbar, .loopbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 8px 12px; background: var(--gold-soft);
  border-bottom: 1px solid var(--line); font-size: 15px; }
.fixbar span, .loopbar span { flex: 1; min-width: 160px; }
.loopbar { background: var(--accent-soft); }

.score-wrap { flex: 1; overflow: auto; position: relative; -webkit-overflow-scrolling: touch; background: var(--surface); }
.score { min-height: 100%; padding-bottom: 40px; position: relative; }
.score svg { width: auto; height: auto; }
.overlay { position: absolute; left: 0; top: 0; pointer-events: none; }
.meas-box { position: absolute; background: var(--gold-soft); border-radius: 6px; transition: left .12s linear, top .12s linear, width .12s, height .12s; }
.loop-box { position: absolute; background: var(--loop); border: 2px dashed var(--accent); border-radius: 8px; }
.playhead { position: absolute; width: 3px; margin-left: -1.5px; background: var(--gold); border-radius: 2px; box-shadow: 0 0 6px var(--gold); }
.sel-note { position: absolute; width: 34px; height: 34px; margin: -17px 0 0 -17px; border-radius: 50%;
  border: 3px solid var(--danger); background: rgba(179, 38, 30, .12); }
.score-loading { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); background: var(--surface); font-size: 17px; }
.fixing .score-wrap { cursor: crosshair; }

.transport { display: flex; align-items: center; justify-content: center; gap: 4px; padding: 8px 8px calc(8px + var(--safe-b));
  background: var(--surface); border-top: 1px solid var(--line); flex-wrap: nowrap; position: relative; }
.t-pos { position: absolute; left: 12px; top: -30px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 2px 10px; font-size: 13px; font-weight: 650; color: var(--muted); box-shadow: var(--shadow); }
.play-btn { width: 70px; height: 70px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; box-shadow: var(--shadow); margin: 0 6px; flex: none; transition: transform .08s; }
.play-btn:active { transform: scale(.95); }
.play-btn svg { width: 34px; height: 34px; }
.play-btn.counting { background: var(--gold); color: #1d1600; }
.tempo { display: flex; align-items: center; background: var(--surface-2); border-radius: 16px; margin: 0 4px; }
.tempo .icon-btn { min-width: 44px; }
.tempo-val { border: 0; background: none; min-width: 56px; min-height: 48px; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.05; }
.tempo-val b { font-size: 20px; font-variant-numeric: tabular-nums; }
.tempo-val small { font-size: 11px; color: var(--muted); font-weight: 600; }
@media (max-width: 480px) {
  .transport { gap: 0; padding-left: 4px; padding-right: 4px; }
  .play-btn { width: 62px; height: 62px; margin: 0 2px; }
  .icon-btn.lg { min-width: 46px; }
  .tempo .icon-btn { min-width: 36px; }
  .tempo-val { min-width: 44px; }
  #btn-start { display: none; }
}
@media (max-width: 360px) { .icon-btn .lbl { display: none; } }

/* ---------- sheets ---------- */
.scrim { position: fixed; inset: 0; background: rgba(10, 12, 16, .38); z-index: 40; animation: fade .18s ease; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; max-height: 86vh; z-index: 50; background: var(--surface);
  border-radius: 22px 22px 0 0; box-shadow: 0 -10px 40px rgba(0,0,0,.2); display: flex; flex-direction: column;
  animation: up .22s cubic-bezier(.2,.8,.2,1); max-width: 720px; margin: 0 auto; }
@media (min-width: 900px) {
  .sheet { left: auto; right: 16px; bottom: 16px; top: 16px; max-height: none; width: 420px; border-radius: 22px; animation: slide .22s cubic-bezier(.2,.8,.2,1); }
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 10px 6px 20px; }
.sheet-head h3 { font-size: 20px; }
.sheet-body { overflow: auto; padding: 4px 20px calc(24px + var(--safe-b)); }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@keyframes slide { from { transform: translateX(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

.presets { display: flex; gap: 8px; overflow-x: auto; padding: 4px 0 10px; }
.preset { flex: none; min-height: 48px; padding: 6px 14px; border-radius: 14px; border: 1.5px solid var(--line); background: var(--bg); font-weight: 650; text-align: left; line-height: 1.15; }
.preset small { display: block; font-weight: 500; color: var(--muted); font-size: 12px; }
.preset.on { border-color: var(--accent); background: var(--accent-soft); }

.mix-row { display: grid; grid-template-columns: 1fr auto; gap: 6px 10px; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--line); }
.mix-row .name b { display: block; font-size: 16px; }
.mix-row .name small { color: var(--muted); font-size: 13px; }
.mix-row .btns { display: flex; gap: 6px; }
.ms { width: 48px; height: 44px; border-radius: 10px; border: 1.5px solid var(--line); background: var(--bg); font-weight: 800; font-size: 15px; }
.ms.m.on { background: var(--danger); border-color: var(--danger); color: #fff; }
.ms.s.on { background: var(--gold); border-color: var(--gold); color: #1d1600; }
.mix-row input[type=range] { grid-column: 1 / -1; width: 100%; }
.mix-row.off .name { opacity: .5; }

input[type=range] { -webkit-appearance: none; appearance: none; height: 36px; background: transparent; }
input[type=range]::-webkit-slider-runnable-track { height: 6px; border-radius: 3px; background: var(--surface-2); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 28px; height: 28px; margin-top: -11px; border-radius: 50%; background: var(--accent); border: 3px solid var(--surface); box-shadow: 0 1px 4px rgba(0,0,0,.3); }
input[type=range]::-moz-range-track { height: 6px; border-radius: 3px; background: var(--surface-2); }
input[type=range]::-moz-range-thumb { width: 24px; height: 24px; border-radius: 50%; background: var(--accent); border: 3px solid var(--surface); }

.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 12px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.switch-row span b { display: block; font-weight: 650; }
.switch-row span small { color: var(--muted); font-size: 13px; }
.switch-row input { display: none; }
.switch-row i { width: 52px; height: 32px; border-radius: 16px; background: var(--surface-2); position: relative; flex: none; transition: background .15s; border: 1px solid var(--line); }
.switch-row i::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .15s; }
.switch-row input:checked + i { background: var(--accent); border-color: var(--accent); }
.switch-row input:checked + i::after { transform: translateX(20px); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { min-height: 44px; padding: 0 14px; border-radius: 22px; border: 1.5px solid var(--line); background: var(--bg); font-weight: 600; font-size: 15px; }
.chip.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.chord-strip { display: flex; flex-wrap: wrap; gap: 6px; }
.chord { min-width: 64px; min-height: 52px; border-radius: 10px; border: 1.5px solid var(--line); background: var(--bg); display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 2px 8px; }
.chord b { font-size: 16px; }
.chord small { font-size: 11px; color: var(--muted); }
.chord.forced { border-color: var(--gold); }
.chord.now { background: var(--gold-soft); }
.chord-pick { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px; margin: 6px 0; background: var(--surface-2); border-radius: 12px; width: 100%; }
.chord-pick button { min-width: 56px; min-height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-weight: 650; }
.chord-pick input { flex: 1; min-width: 100px; min-height: 44px; border-radius: 10px; border: 1px solid var(--line); padding: 0 10px; background: var(--surface); }

.menu-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }
.menu-btn { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; min-height: 84px; border-radius: 14px; border: 1.5px solid var(--line); background: var(--bg); font-weight: 650; }
.part-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; align-items: end; padding: 6px 0 12px; border-bottom: 1px solid var(--line); }
.part-row .field { margin-top: 6px; }
.part-row .pname { grid-column: 1 / -1; font-weight: 700; margin-top: 8px; }

/* ---------- note popover ---------- */
.popover { position: fixed; z-index: 60; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: 0 10px 40px rgba(0,0,0,.25); padding: 10px; width: 280px; }
.pop-title { font-weight: 700; padding: 2px 4px 8px; }
.pop-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.pop-grid.one { grid-template-columns: 1fr; margin-top: 6px; }
.pop-grid button { min-height: 56px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); font-size: 22px; font-weight: 700; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1; }
.pop-grid button small { font-size: 10px; font-weight: 600; color: var(--muted); margin-top: 3px; }
.pop-grid button.danger { font-size: 15px; }

.modal { position: fixed; inset: 0; z-index: 70; background: var(--bg); display: flex; flex-direction: column; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: calc(8px + var(--safe-t)) 8px 8px 16px; border-bottom: 1px solid var(--line); background: var(--surface); }
.modal-body { overflow: auto; padding: 12px; display: grid; gap: 12px; justify-items: center; }
.modal-body img { max-width: 100%; border-radius: 8px; box-shadow: var(--shadow); background: #fff; }

.toast { position: fixed; left: 50%; bottom: calc(110px + var(--safe-b)); transform: translateX(-50%); z-index: 80;
  background: var(--ink); color: var(--bg); padding: 12px 18px; border-radius: 14px; font-weight: 600; box-shadow: var(--shadow);
  max-width: calc(100vw - 32px); animation: fade .2s ease; font-size: 15px; }
