:root {
  --bg: #15161c;
  --bg2: #1c1e26;
  --bg3: #252834;
  --bg4: #2f3342;
  --line: #343849;
  --line-strong: #4a5066;
  --text: #e9ecf5;
  --muted: #9aa1b8;
  --accent: #ffd43b;
  --accent2: #4dabf7;
  --danger: #ff6b6b;
  --fn-home: #51cf66;
  --fn-away: #4dabf7;
  --fn-tension: #ff922b;
  --head-w: 250px;
  --radius: 8px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
button, input, select { font: inherit; color: inherit; }
h3 { margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.hint { color: var(--muted); font-size: 12px; margin: 6px 0 0; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 8px 14px; background: var(--bg2); border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}
.brand {
  font-weight: 800; font-size: 18px; letter-spacing: .02em;
  background: linear-gradient(90deg, var(--accent), #ff922b, #f783ac);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.group { display: flex; align-items: center; gap: 8px; }
.field { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.field.check { cursor: pointer; }
.field input[type=number] { width: 58px; }
input[type=number], select, input[type=text] {
  background: var(--bg3); border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px; color: var(--text);
}
input[type=range] { accent-color: var(--accent); width: 90px; }
.btn {
  background: var(--bg3); border: 1px solid var(--line); border-radius: 6px; padding: 5px 10px;
  cursor: pointer; color: var(--text); white-space: nowrap;
}
.btn:hover { background: var(--bg4); border-color: var(--line-strong); }
.btn.small { padding: 4px 7px; font-size: 12px; }
.btn.big { font-size: 18px; padding: 10px 26px; background: var(--accent); color: #222; border: none; font-weight: 700; }
.btn.play { width: 38px; font-size: 16px; background: var(--fn-home); color: #111; border: none; font-weight: 700; }
.btn.play.playing { background: var(--danger); }
.btn.on { background: var(--accent); color: #222; border-color: var(--accent); }
.btn.mute.on { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.solo.on { background: var(--accent2); border-color: var(--accent2); color: #111; }

.tabs { display: flex; gap: 2px; margin-left: auto; background: var(--bg); padding: 3px; border-radius: 8px; }
.tab { background: none; border: none; padding: 6px 14px; border-radius: 6px; cursor: pointer; color: var(--muted); }
.tab.active { background: var(--bg4); color: var(--text); }

.dropdown { position: relative; }
.dropdown-menu {
  display: none; position: absolute; right: 0; top: calc(100% + 4px); z-index: 50;
  background: var(--bg3); border: 1px solid var(--line-strong); border-radius: 8px; padding: 6px; min-width: 220px;
  box-shadow: 0 10px 30px #0008;
}
.dropdown-menu.open { display: block; }
.dropdown-menu button { display: block; width: 100%; text-align: left; background: none; border: none; padding: 7px 10px; border-radius: 5px; cursor: pointer; }
.dropdown-menu button:hover { background: var(--bg4); }
.dropdown-menu hr { border: none; border-top: 1px solid var(--line); margin: 4px 0; }

/* ---------- Layout ---------- */
.app { flex: 1 1 auto; display: flex; min-height: 0; }
.sidebar {
  width: 330px; flex: 0 0 330px; overflow-y: auto; padding: 10px; border-right: 1px solid var(--line);
  background: var(--bg2); display: flex; flex-direction: column; gap: 10px;
}
.card { background: var(--bg3); border-radius: var(--radius); padding: 10px 12px; }
.main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }
.tabpanel { display: none; flex: 1 1 auto; min-height: 0; }
.tabpanel.active { display: flex; flex-direction: column; }
#tab-learn iframe { flex: 1; border: none; width: 100%; background: var(--bg); }

/* ---------- Circle of fifths ---------- */
#circle svg { width: 100%; height: auto; display: block; user-select: none; }
#circle .seg { cursor: pointer; transition: opacity .15s; }
#circle .seg:hover { opacity: .85; }
#circle text { pointer-events: none; font-family: inherit; }

/* ---------- Scale card ---------- */
.scale-title { font-size: 16px; font-weight: 700; margin-bottom: 2px; }
.scale-mood { color: var(--muted); font-size: 12px; margin-bottom: 8px; }
.degrees { display: flex; align-items: flex-start; gap: 1px; flex-wrap: wrap; }
.deg-chip {
  display: flex; flex-direction: column; align-items: center; min-width: 27px; padding: 4px 2px; border-radius: 6px;
  background: var(--bg4); cursor: pointer; border: 1px solid transparent;
}
.deg-chip:hover { border-color: var(--accent); }
.deg-chip .num { font-weight: 800; font-size: 16px; }
.deg-chip .iv { font-size: 10px; color: var(--muted); }
.deg-chip .nm { font-size: 11px; color: var(--accent); }
.deg-chip.tonic { background: #3b3520; }
.step-gap { font-size: 9px; color: var(--muted); align-self: center; min-width: 11px; text-align: center; }
.keysig { font-size: 12px; color: var(--muted); margin-top: 6px; }

/* ---------- Chord palette ---------- */
.chord-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; }
.chord-btn {
  border: 2px solid var(--fn, var(--line)); background: var(--bg4); border-radius: 7px; padding: 5px 2px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; line-height: 1.15;
}
.chord-btn:hover { background: var(--bg); }
.chord-btn .rn { font-weight: 800; font-size: 15px; }
.chord-btn .cn { font-size: 11px; color: var(--muted); }
.type-row { display: flex; gap: 4px; flex-wrap: wrap; margin: 8px 0; }
.type-row .btn { font-size: 11px; padding: 3px 7px; }
.legend { display: flex; gap: 10px; font-size: 11px; color: var(--muted); margin-top: 6px; flex-wrap: wrap; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }
.sugg { display: flex; flex-direction: column; gap: 5px; }
.sugg-item { display: flex; gap: 8px; align-items: center; background: var(--bg4); border-radius: 6px; padding: 5px 8px; cursor: pointer; border-left: 4px solid var(--fn); }
.sugg-item:hover { background: var(--bg); }
.sugg-item .rn { font-weight: 800; min-width: 44px; }
.sugg-item .why { font-size: 11px; color: var(--muted); }
.prog-list { display: flex; flex-direction: column; gap: 4px; max-height: 320px; overflow-y: auto; }
.prog-item { background: var(--bg4); border-radius: 6px; padding: 6px 8px; cursor: pointer; }
.prog-item:hover { background: var(--bg); }
.prog-item .pn { font-weight: 700; display: flex; justify-content: space-between; gap: 6px; }
.prog-item .pr { color: var(--accent); font-size: 12px; font-weight: 600; }
.prog-item .ph { font-size: 11px; color: var(--muted); }
.prog-item.fits .pn::after { content: "fits this scale"; font-size: 10px; color: var(--fn-home); font-weight: 500; }

/* note-mode switch */
.seg { display: flex; align-items: center; gap: 0; }
.seg-label { font-size: 12px; color: var(--muted); margin-right: 6px; }
.seg button { background: var(--bg3); border: 1px solid var(--line); padding: 4px 10px; cursor: pointer; font-size: 12px; }
.seg button:first-of-type { border-radius: 6px 0 0 6px; }
.seg button:last-of-type { border-radius: 0 6px 6px 0; border-left: none; }
.seg button.on { background: var(--accent); color: #222; border-color: var(--accent); font-weight: 700; }
.scale-status { display: flex; align-items: center; gap: 8px; font-size: 12px; font-weight: 700; color: var(--danger); }
.scale-status[hidden] { display: none; }

/* ---------- Timeline ---------- */
.compose-toolbar { display: flex; align-items: center; gap: 14px; padding: 8px 12px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.compose-toolbar .hint { margin: 0; }
.timeline { flex: 1 1 auto; overflow: auto; position: relative; }
.timeline-inner { position: relative; min-width: max-content; padding-bottom: 60px; }
.playhead { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--accent); pointer-events: none; display: none; z-index: 3; box-shadow: 0 0 8px var(--accent); }

.lane { display: flex; border-bottom: 1px solid var(--line); }
.lane-head {
  position: sticky; left: 0; z-index: 4; width: var(--head-w); flex: 0 0 var(--head-w);
  background: var(--bg2); border-right: 1px solid var(--line); display: flex;
}
.lane-body { position: relative; }
.lane.selected .lane-head { box-shadow: inset 3px 0 0 var(--accent); }
.head-controls { flex: 1; padding: 6px 8px; display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.head-row { display: flex; gap: 4px; align-items: center; }
.head-row select { max-width: 100%; min-width: 0; flex: 1; font-size: 12px; padding: 2px 4px; }
.head-row .btn { padding: 2px 6px; font-size: 11px; }
.head-row input[type=range] { width: 70px; }
.head-row label { font-size: 11px; color: var(--muted); display: flex; align-items: center; gap: 3px; }
.track-name { background: transparent; border: none; font-weight: 700; width: 100%; padding: 0; min-width: 0; }
.track-name:focus { outline: 1px solid var(--line-strong); }
.color-dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 10px; }
canvas.keys { display: block; cursor: pointer; border-left: 1px solid var(--line); }
canvas.roll { display: block; cursor: crosshair; touch-action: none; }

.ruler .lane-head { align-items: center; padding: 0 10px; font-size: 12px; color: var(--muted); }
.ruler .lane-body { height: 24px; }
.ruler canvas { display: block; }

.section-title .lane-head { padding: 10px 10px 6px; font-weight: 800; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.section-title .lane-body { flex: 1; }

/* chord lane */
.chord-lane .lane-body { display: flex; height: 64px; }
.chord-slot {
  flex: 0 0 auto; border-right: 1px solid var(--line-strong); padding: 4px 6px; cursor: pointer; position: relative;
  display: flex; flex-direction: column; justify-content: center; overflow: hidden;
  background: color-mix(in srgb, var(--fn, var(--bg3)) 18%, var(--bg2));
  border-top: 3px solid var(--fn, transparent);
}
.chord-slot.empty { background: var(--bg2); border-top-color: transparent; color: var(--muted); font-size: 12px; }
.chord-slot.selected { outline: 2px solid var(--accent); outline-offset: -2px; }
.chord-slot.dragover { outline: 2px dashed var(--accent); outline-offset: -2px; }
.chord-slot .rn { font-weight: 800; font-size: 18px; line-height: 1; }
.chord-slot .cn { font-size: 11px; color: var(--muted); white-space: nowrap; }
.chord-slot .fn { font-size: 10px; text-transform: uppercase; letter-spacing: .05em; color: var(--fn); }

/* drums */
.drum-body { display: flex; align-items: center; height: 30px; }
.step {
  flex: 0 0 auto; height: 24px; margin: 0 1px; border-radius: 4px; cursor: pointer; border: none; padding: 0;
  background: #3a3f52;
}
.step.alt { background: #2c3040; }
.step.v1 { background: var(--row-color, var(--accent)); opacity: .75; }
.step.v2 { background: var(--row-color, var(--accent)); box-shadow: 0 0 0 2px #fff inset; }
.step.now { outline: 2px solid #fff; }
.drum-head { align-items: center; padding: 0 8px; gap: 5px; }
.drum-head .dname { flex: 1; font-size: 12px; font-weight: 600; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drum-head .dname:hover { color: var(--accent); }
.drum-head input[type=range] { width: 50px; }
.drum-head .btn { padding: 1px 5px; font-size: 11px; }
.drum-lane.drop .lane-head { outline: 2px dashed var(--accent); }

/* ---------- Sound design ---------- */
#tab-sound { overflow: auto; }
.sd { display: flex; gap: 14px; padding: 14px; align-items: flex-start; flex-wrap: wrap; }
.sd-list { width: 220px; display: flex; flex-direction: column; gap: 4px; }
.sd-list .patch-item { padding: 7px 10px; border-radius: 6px; cursor: pointer; background: var(--bg3); display: flex; justify-content: space-between; }
.sd-list .patch-item.active { background: var(--accent); color: #222; font-weight: 700; }
.sd-list .patch-item small { opacity: .7; }
.sd-main { flex: 1; min-width: 420px; display: flex; flex-direction: column; gap: 12px; }
.sd-panel { background: var(--bg3); border-radius: var(--radius); padding: 12px 14px; }
.sd-row { display: flex; gap: 18px; flex-wrap: wrap; align-items: flex-end; }
.knob { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); min-width: 120px; }
.knob input[type=range] { width: 140px; }
.knob output { color: var(--text); font-variant-numeric: tabular-nums; }
.wave-btns { display: flex; gap: 6px; }
.wave-btn { background: var(--bg4); border: 2px solid transparent; border-radius: 8px; padding: 6px 8px; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; }
.wave-btn.on { border-color: var(--accent); }
.wave-btn svg { width: 54px; height: 26px; }
.test-keys { display: flex; gap: 4px; flex-wrap: wrap; }
.test-keys button { width: 42px; height: 54px; border-radius: 6px; border: 1px solid var(--line-strong); background: var(--bg4); cursor: pointer; font-weight: 800; font-size: 16px; }
.test-keys button.tonic { background: #3b3520; }
.test-keys button:active { background: var(--accent); color: #222; }
canvas.scope, canvas.envcv { width: 100%; height: 110px; background: var(--bg); border-radius: 6px; display: block; }
.drum-params { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
.drum-param { background: var(--bg4); border-radius: 6px; padding: 8px 10px; }
.drum-param h4 { margin: 0 0 6px; display: flex; justify-content: space-between; align-items: center; font-size: 13px; }

/* ---------- Misc ---------- */
.toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--bg4); border: 1px solid var(--line-strong); padding: 9px 16px; border-radius: 8px; transition: .25s; pointer-events: none; z-index: 100;
  max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%); }
.modal { position: fixed; inset: 0; background: #000a; display: flex; align-items: center; justify-content: center; z-index: 90; }
.modal[hidden] { display: none; }
.modal-box { background: var(--bg2); border: 1px solid var(--line-strong); border-radius: 12px; padding: 18px 20px; min-width: 320px; max-width: 520px; }
.modal-box h2 { margin: 0 0 10px; font-size: 18px; }
.modal-box .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.start-overlay { position: fixed; inset: 0; background: radial-gradient(circle at 50% 40%, #2a2410, var(--bg) 70%); display: flex; align-items: center; justify-content: center; z-index: 200; }
.start-overlay.hidden { display: none; }
.start-box { text-align: center; max-width: 460px; padding: 20px; }
.start-box h1 {
  font-size: 54px; margin: 0 0 10px;
  background: linear-gradient(90deg, var(--accent), #ff922b, #f783ac); -webkit-background-clip: text; background-clip: text; color: transparent;
}
.start-box p { color: var(--muted); font-size: 15px; }

@media (max-width: 900px) {
  :root { --head-w: 170px; }
  body { overflow: auto; }
  .app { flex-direction: column; }
  .sidebar { width: 100%; flex: 0 0 auto; max-height: 45vh; border-right: none; border-bottom: 1px solid var(--line); }
  .tabs { margin-left: 0; }
  .sd-main { min-width: 0; }
}
