:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --plot: #ffffff;
  --ink: #1b1c1f;
  --muted: #6b6e76;
  --grid: #e7e6e2;
  --line: #b9b8b3;
  --band: #d9eee0;
  --tune: #1d8a4b;
  --key-white: #fcfcfa;
  --key-black: #2b2c30;
  --key-edge: #c9c8c3;
  --key-label: #6b6e76;
  --target: #e0a100;
  --flat: #d0342c;
  --sharp: #2f5fd0;
  --accent: #2f5fd0;
  --border: #dcdbd6;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171a;
    --surface: #1f2024;
    --plot: #1b1c20;
    --ink: #ecebe7;
    --muted: #9a9ca3;
    --grid: #2a2b30;
    --line: #5c5e65;
    --band: #213a2b;
    --tune: #45c77d;
    --key-white: #cfcec9;
    --key-black: #111215;
    --key-edge: #8d8e93;
    --key-label: #3c3d42;
    --target: #e8b838;
    --flat: #f0625a;
    --sharp: #6b93f5;
    --accent: #6b93f5;
    --border: #34353b;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16171a;
  --surface: #1f2024;
  --plot: #1b1c20;
  --ink: #ecebe7;
  --muted: #9a9ca3;
  --grid: #2a2b30;
  --line: #5c5e65;
  --band: #213a2b;
  --tune: #45c77d;
  --key-white: #cfcec9;
  --key-black: #111215;
  --key-edge: #8d8e93;
  --key-label: #3c3d42;
  --target: #e8b838;
  --flat: #f0625a;
  --sharp: #6b93f5;
  --accent: #6b93f5;
  --border: #34353b;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

button, select, input { font: inherit; color: inherit; }
button, select, .btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 12px;
  min-height: 36px;
  cursor: pointer;
}
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.bar .spacer { flex: 1; }
.ref { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.ref input {
  width: 4.5em;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  min-height: 36px;
}

.settings {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px 20px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.settings[hidden] { display: none; }
label.check { display: flex; flex-direction: row; align-items: center; gap: 8px; color: var(--ink); font-size: 14px; cursor: pointer; }
label.check input { width: 18px; height: 18px; margin: 0; accent-color: var(--accent); }
.settings label.check { flex-direction: row; color: var(--ink); font-size: 14px; }
.check-group { display: flex; flex-direction: column; gap: 4px; }
.hint { color: var(--muted); font-size: 12px; }
.card .hint { margin: 4px 0 0 26px; font-size: 12px; }
.rec-badge { color: var(--flat); font-size: 13px; font-weight: 600; white-space: nowrap; cursor: help; }
.rec-badge[hidden] { display: none; }

.btn { display: inline-flex; align-items: center; color: inherit; text-decoration: none; }
.player {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.player[hidden] { display: none; }
.player-label { color: var(--muted); font-size: 13px; }
.player input[type="range"] { flex: 1; min-width: 0; accent-color: var(--accent); }
.player .time { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap; }
#playBtn { display: inline-flex; align-items: center; }
#playBtn .icon { fill: currentColor; stroke: none; }
#playBtn.playing .icon-play, #playBtn:not(.playing) .icon-pause { display: none; }
.settings label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 13px; }
.settings output { color: var(--ink); font-variant-numeric: tabular-nums; }
.settings input[type="range"] { width: 100%; accent-color: var(--accent); }
.meter { position: relative; height: 6px; border-radius: 3px; background: var(--grid); overflow: hidden; }
#levelBar { position: absolute; inset: 0 auto 0 0; width: 0; background: var(--muted); }
#gateMark { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--accent); }

.readout {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 16px;
  padding: 6px 16px 0;
  min-height: 52px;
  font-variant-numeric: tabular-nums;
}
.note { font-size: 36px; font-weight: 650; min-width: 2.6em; }
#noteOct { font-size: 18px; color: var(--muted); margin-left: 2px; }
.cents { font-size: 28px; font-weight: 600; min-width: 4em; }
.freq { color: var(--muted); min-width: 5.5em; text-align: right; }

/* Tuner needle: left of centre is flat, right is sharp */
.tuner { flex: 1; min-width: 220px; display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.tuner .scale {
  position: relative;
  flex: 1;
  height: 30px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--plot) repeating-linear-gradient(to right, var(--grid) 0 1px, transparent 1px 10%);
  overflow: hidden;
}
.tuner .zone { position: absolute; top: 0; bottom: 0; background: var(--band); }
.tuner .center { position: absolute; top: 0; bottom: 0; left: 50%; width: 2px; margin-left: -1px; background: var(--line); }
.tuner .needle { position: absolute; top: 3px; bottom: 3px; left: 50%; width: 8px; margin-left: -4px; border-radius: 4px; background: var(--ink); }
.tuner.idle .needle { opacity: 0; }

/* Mode switch */
.modes { display: flex; gap: 4px; padding: 8px 16px 0; }
.modes button { min-height: 32px; padding: 4px 12px; font-size: 14px; border-radius: 999px; }
.modes button[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--bg); font-weight: 600; }

/* Intonation system and key */
.modes { flex-wrap: wrap; align-items: center; }
.modes .spacer { flex: 1; }
.intonation { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 999px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; min-height: 32px; padding: 4px 11px; font-size: 14px; background: var(--surface); }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-checked="true"] { background: var(--ink); color: var(--bg); font-weight: 600; }
.seg .short { display: none; }
.key-step { display: inline-flex; align-items: stretch; border: 1px solid var(--border); border-radius: 999px; overflow: hidden; background: var(--surface); }
.key-step[hidden] { display: none; }
.key-step button { border: 0; border-radius: 0; min-height: 32px; min-width: 34px; padding: 0 8px; font-size: 18px; line-height: 1; background: transparent; }
.key-name { position: relative; display: flex; align-items: center; justify-content: center; min-width: 6.6em; padding: 0 4px; font-size: 14px; font-weight: 600; border-inline: 1px solid var(--border); cursor: pointer; }
.key-name select { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; font-size: 16px; }
.key-name:focus-within { outline: 2px solid var(--accent); outline-offset: -2px; }
.key-score { font-size: 14px; color: var(--muted); }
.key-score[hidden] { display: none; }
@media (max-width: 520px) {
  .seg .long { display: none; }
  .seg .short { display: inline; }
}

/* Where the note would sit in the other systems */
.tuner .ticks { position: absolute; inset: 0; pointer-events: none; }
.tuner .tick { position: absolute; top: 0; bottom: 0; width: 2px; margin-left: -1px; background: var(--muted); opacity: 0.7; }
.tuner .tick span { position: absolute; bottom: 1px; left: 4px; font-size: 9px; line-height: 1; color: var(--muted); white-space: nowrap; }
.tuner.idle .ticks { display: none; }

/* Practice mode */
.practice { display: flex; flex-direction: column; gap: 6px; padding: 8px 16px 0; }
.practice[hidden] { display: none; }
.practice-bar { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.practice-bar select { max-width: 100%; min-width: 0; flex: 0 1 auto; }
.practice-bar .spacer { flex: 1; }
.practice-bar .short { display: none; }
@media (max-width: 600px) {
  .practice-bar select { flex: 1 1 100%; }
  .practice-bar .long { display: none; }
  .practice-bar .short { display: inline; }
}
.progress { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.score-view {
  overflow-x: auto;
  overflow-y: hidden;
  background: var(--plot);
  border: 1px solid var(--border);
  border-radius: 8px;
  min-height: 120px;
  scrollbar-width: thin;
}
.score-view svg { display: block; }
.practice-msg { margin: 0; font-size: 14px; min-height: 0; }
.practice-msg:empty { display: none; }

.add-dialog {
  width: min(640px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--ink);
}
.add-dialog::backdrop { background: color-mix(in srgb, var(--bg) 70%, transparent); }
.add-dialog h2 { margin: 0 0 8px; font-size: 20px; }
.add-dialog p, .add-dialog ol { margin: 0 0 10px; }
.add-dialog ol { padding-left: 20px; }
.add-dialog a { color: var(--accent); }
.prompt-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px; }
.prompt {
  max-height: 150px;
  overflow: auto;
  margin: 0 0 12px;
  padding: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12px;
  white-space: pre-wrap;
  user-select: all;
}
.add-dialog textarea {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font: 13px/1.4 ui-monospace, "Cascadia Mono", Consolas, monospace;
}
.file-row { margin: 8px 0; }
.abc-check { font-size: 14px; }
.abc-check.bad { color: var(--flat); }
.abc-check:empty { display: none; }
.scratch { position: absolute; left: -10000px; top: 0; width: 800px; visibility: hidden; }

.charts {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 6px 16px 12px;
}
.charts canvas {
  display: block;
  width: 100%;
  min-height: 0;
  background: var(--plot);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.charts { position: relative; }
#pitchChart { flex: 3; }
.mute {
  position: absolute;
  top: 14px;
  right: 24px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}
.mute[hidden] { display: none; }
.mute[aria-pressed="true"] .icon-on, .mute[aria-pressed="false"] .icon-off { display: none; }

.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(3px);
}
.overlay[hidden] { display: none; }
.card {
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
}
.card h1 { margin: 0 0 8px; font-size: 22px; }
.card p { margin: 0 0 12px; }
.card .small { color: var(--muted); font-size: 13px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; }
.flat { color: var(--flat); font-weight: 600; }
.sharp { color: var(--sharp); font-weight: 600; }
.tune { color: var(--tune); font-weight: 600; }
.notice { margin: 12px 0 0; padding: 8px 10px; border-radius: 8px; background: var(--bg); font-size: 14px; }
.notice:empty { display: none; }
.error { color: var(--flat); margin: 12px 0 0; }
.error:empty { display: none; }

@media (max-width: 600px) {
  .tuner { order: 5; flex-basis: 100%; }
  .freq { margin-left: auto; }
}
@media (max-width: 520px) {
  .bar { padding: 8px 16px; gap: 6px; }
  .note { font-size: 30px; }
  .cents { font-size: 22px; }
  .charts { padding: 6px 16px 10px; }
}
#settingsBtn { display: inline-flex; align-items: center; gap: 6px; }
.icon { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
@media (max-width: 440px) {
  .ref .unit, #settingsBtn .label, .player-label, .rec-badge .label { display: none; }
  .ref input { width: 4em; }
  button, select { padding: 6px 10px; }
}
