:root {
  --bg: #f4f2ee;
  --card: #ffffff;
  --text: #1d1f24;
  --muted: #6b6f78;
  --line: #dedad3;
  --accent: #2f6fdb;
  --accent-text: #ffffff;
  --good: #1f9d55;
  --warn: #c99a06;
  --bad: #d64545;
  --ring-bg: #e6e2db;
  --chip-on: #2f6fdb;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181d;
    --card: #20232a;
    --text: #eceef2;
    --muted: #9aa0ab;
    --line: #333740;
    --accent: #5b92f0;
    --accent-text: #0f1115;
    --good: #3cc47c;
    --warn: #e7bb2d;
    --bad: #ef6b6b;
    --ring-bg: #2c3038;
    --chip-on: #5b92f0;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { color: var(--bad); }

.bar {
  display: flex; align-items: baseline; gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
}
.bar h1 { font-size: 1.1rem; margin: 0; }
.bar .reload {
  margin-left: auto; align-self: center;
  width: 32px; height: 32px; padding: 0; border-radius: 50%;
  font-size: 1.1rem; line-height: 1;
}
/* Installed app: no pull-to-refresh (the reload button replaces it). */
@media (display-mode: standalone) {
  html, body { overscroll-behavior-y: none; }
}

.view { width: 100%; max-width: 720px; margin: 0 auto; padding: 16px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 16px;
  margin-bottom: 16px;
}
.row {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 8px 0;
  flex-wrap: wrap;
}
.row + .row { border-top: 1px solid var(--line); }

select, input, button {
  font: inherit; color: inherit;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 10px;
}
input[type=number] { width: 5.5em; text-align: right; }
button { cursor: pointer; }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.stepper { display: flex; align-items: center; gap: 6px; }
.stepper button { width: 2.4em; }

.seg { display: flex; gap: 4px; }
.seg label {
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer;
}
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label:has(input:checked) { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.seg label:has(input:focus-visible) { outline: 2px solid var(--accent); }


.big { display: block; width: 100%; padding: 14px; font-size: 1.15rem; font-weight: 600; letter-spacing: 0.05em; }
.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

/* Tuning view */
.tune { display: flex; gap: 16px; flex: 1; max-width: 900px; }
.sidebar {
  width: 112px; flex: none;
  overflow-y: auto;
  max-height: calc(100dvh - 90px);
  border-right: 1px solid var(--line);
  padding-right: 8px;
}
.sidebar h3 { font-size: 0.72rem; font-weight: 600; color: var(--muted); margin: 10px 0 2px; text-transform: uppercase; letter-spacing: 0.04em; }
.sidebar h3:first-child { margin-top: 0; }
.sidebar ul { list-style: none; margin: 0; padding: 0; }
.sidebar li {
  display: flex; align-items: center; gap: 8px;
  padding: 3px 4px; border-radius: 6px;
  font-variant-numeric: tabular-nums;
}
.sidebar li.active { background: var(--ring-bg); }
.dot {
  width: 14px; height: 14px; border-radius: 50%; flex: none;
  border: 2px solid var(--muted);
}
.dot.full { background: var(--good); border-color: var(--good); }
.sidebar .count { margin-left: auto; font-size: 0.75rem; color: var(--muted); }

.stage { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 12px; min-width: 0; }
.dial { position: relative; width: min(72vw, 320px, 100%); aspect-ratio: 1; }
.dial svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-bg { fill: none; stroke: var(--ring-bg); stroke-width: 8; }
.ring { fill: none; stroke: var(--accent); stroke-width: 8; stroke-linecap: round; transition: stroke-dasharray 80ms linear; }
.ring.done { stroke: var(--good); }
.check { fill: none; stroke: var(--good); stroke-width: 7; stroke-linecap: round; stroke-linejoin: round; transform: rotate(90deg); transform-origin: 60px 60px; }
.note {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: clamp(2.6rem, 14vw, 4.5rem); font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.note.dim { color: var(--muted); }
.fingering { position: absolute; left: 0; right: 0; bottom: 24%; text-align: center; font-size: 1.1rem; }
.hint-label { font-size: 0.75rem; color: var(--muted); font-weight: 400; }
.dial.done .note { opacity: 0.15; }
.hint { min-height: 1.4em; margin: 0; text-align: center; }
.meter { width: min(72vw, 320px, 100%); height: 4px; background: var(--ring-bg); border-radius: 2px; overflow: hidden; }
.meter div { height: 100%; width: 0; background: var(--muted); }
.stage .big { max-width: 320px; margin-top: auto; }
.trace { width: 100%; height: 170px; display: block; border: 1px solid var(--line); border-radius: 8px; background: var(--card); }
/* Harmonics mode: fingering pills whose outline fills with progress */
.harm-pills { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; max-width: 340px; margin-top: auto; }
.harm-pills:not([hidden]) + .big { margin-top: 0; }
.hpill {
  position: relative; min-width: 3.1em; height: 34px; padding: 0 10px; border: 0; border-radius: 17px;
  background: transparent; color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums;
  touch-action: manipulation;
}
.hpill svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.hpill rect { fill: none; stroke-width: 3; }
.hpill .track { stroke: var(--line); }
.hpill .prog { stroke: var(--good); stroke-width: 4; stroke-linecap: round; }
.hpill .prog[stroke-dasharray^="0 "] { stroke: none; }
.hpill span { position: relative; }
.hpill[aria-pressed=true] { background: var(--accent); color: var(--accent-text); }
.hpill[aria-pressed=true] .track { stroke: var(--accent); }
.hpill:disabled { opacity: 0.35; }
input[type=checkbox] { width: 1.3em; height: 1.3em; accent-color: var(--accent); }

/* Results */
.results { list-style: none; margin: 0; padding: 0; }
.results li {
  display: grid; grid-template-columns: 14px 4.4em 1fr 8.5em 1.8em; align-items: center; gap: 10px;
  padding: 6px 0; border-top: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
}
.results li:first-child { border-top: 0; }
.results li.group-head { display: flex !important; align-items: center; justify-content: space-between; }
.results .del, .results li.group-head .del {
  padding: 2px 6px; border: 0; background: transparent; color: var(--muted); font-size: 0.9rem; line-height: 1;
}
.hpill { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.results .cents { font-weight: 600; text-align: right; }
.results .bar-cell { position: relative; height: 10px; }
.results .bar-cell::before { content: ""; position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px; background: var(--line); }
.results .bar-cell span { position: absolute; top: 2px; height: 6px; border-radius: 3px; }
.s-good { color: var(--good); } .bg-good { background: var(--good); }
.s-warn { color: var(--warn); } .bg-warn { background: var(--warn); }
.s-bad { color: var(--bad); } .bg-bad { background: var(--bad); }
.s-missing { color: var(--muted); } .bg-missing { background: transparent; border: 2px solid var(--muted); }
.swatch { width: 12px; height: 12px; border-radius: 50%; }
.results .sub { font-size: 0.75rem; color: var(--muted); font-weight: 400; margin-left: 4px; }

#summary h2 { margin: 0 0 4px; font-size: 1.6rem; }
#summary p { margin: 0; }
.group h3 { margin: 0 0 4px; font-size: 1rem; display: flex; justify-content: space-between; gap: 8px; }
.group p { margin: 4px 0 0; }
.actions { display: grid; gap: 8px; grid-template-columns: 1fr 1fr; }
.actions .big { grid-column: 1 / -1; }

@media (max-width: 480px) {
  .view { padding: 12px 16px; }
  .tune { gap: 10px; }
  .sidebar { width: 88px; }
}

dialog {
  background: var(--card); color: var(--text);
  border: 1px solid var(--line); border-radius: 14px;
  padding: 16px; width: min(92vw, 360px);
}
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
dialog h2 { margin: 0 0 4px; font-size: 1.15rem; }
dialog p { margin: 0 0 12px; }
.side-buttons { display: grid; gap: 8px; margin-bottom: 8px; }
#side-cancel { width: 100%; }

/* Range on a treble staff */
.range-staff { display: flex; justify-content: center; padding: 6px 0 2px; color: var(--text); }
.range-staff svg { max-width: 100%; height: auto; }
.staff .line, .staff .ledger { stroke: currentColor; stroke-width: 1; }
.staff .ledger { stroke-width: 1.2; }
.staff .hole { fill: var(--staff-bg, var(--card)); }
.staff .head { fill: var(--muted); opacity: 0.55; }
.staff .head.done { fill: var(--text); opacity: 1; }
.staff .head.active { fill: var(--accent); opacity: 1; }
.staff g:not(.head) > g { color: var(--muted); }
.staff g.done > g { color: var(--text); }
.staff g.active > g { color: var(--accent); }
.sidebar.chord { width: 142px; overflow-x: hidden; --staff-bg: var(--bg); color: var(--text); display: flex; flex-direction: column; align-items: center; }
.side-pills { margin-top: auto; display: flex; flex-direction: column; gap: 8px; }
.side-pills[hidden] { display: none; }
.side-pills .hpill { width: 112px; }
.sidebar.chord svg { flex: none; }
.staff .join { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.staff .ottava { fill: var(--muted); font: italic 8px system-ui, sans-serif; text-anchor: middle; }

/* Fingerings (brass) */
#fing-card summary { cursor: pointer; font-weight: 500; }
.fing-list { display: grid; gap: 6px; }
.fing-row { display: grid; grid-template-columns: 3.2em 1fr; align-items: center; gap: 8px; }
.fing-note { font-weight: 600; }
.fing-alts { display: flex; flex-wrap: wrap; gap: 4px; }
.fing-alts button {
  min-width: 2.6em; padding: 3px 8px; border-radius: 999px; font-size: 0.85rem;
  border: 1px dashed var(--line); background: transparent; color: var(--muted);
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; touch-action: manipulation;
}
.fing-alts button.main { border: 1px solid var(--accent); background: var(--accent); color: var(--bg, #fff); font-weight: 700; opacity: 1; }
.fing-alts button[aria-pressed=true] { border: 1px solid var(--accent); color: var(--accent); background: transparent; font-weight: 600; }
.fingering.prompt { color: var(--accent); font-weight: 700; }

/* Debug settings, below START */
#debug-card { margin-top: 24px; }
#detect-card { border-top: 1px solid var(--line); padding-top: 8px; display: grid; gap: 6px; }
#detect-card p { margin: 0; }
#notes-row[hidden] + #key-row[hidden] + .row { border-top: 0; }
