:root {
  --bg: #0a0b0d;
  --panel: rgba(18, 20, 24, 0.72);
  --panel-solid: #121418;
  --line: #1c1f24;
  --text: #e6e8ec;
  --muted: #8b929c;
  --accent: #37e0d0;
  --accent2: #e05a9a;
  --radius: 8px;
  --sans: 'Inter', system-ui, -apple-system, sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font-family: var(--sans); }

#gl { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; z-index: 0; }

/* ---------- Top bar ---------- */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; height: 46px; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px; background: linear-gradient(to bottom, rgba(10,11,13,0.85), rgba(10,11,13,0));
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }
.brand { font-weight: 600; letter-spacing: 0.5px; color: var(--accent); font-size: 15px; }
.brand .sub { color: var(--muted); font-weight: 400; font-size: 12px; margin-left: 6px; }
.top-actions { display: flex; gap: 8px; }

/* ---------- Buttons ---------- */
.btn {
  background: var(--panel); color: var(--text); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 6px 12px; font-size: 12px; font-family: var(--sans);
  cursor: pointer; backdrop-filter: blur(10px); transition: all 0.15s;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.icon { width: 34px; text-align: center; padding: 6px 0; }
.btn.ghost { background: transparent; }
.btn.rec { border-color: var(--accent2); color: var(--accent2); }
.btn.active { border-color: var(--accent); color: var(--accent); background: rgba(55,224,208,0.12); }
.mini {
  background: transparent; border: 1px solid var(--line); color: var(--muted);
  border-radius: 5px; width: 22px; height: 22px; cursor: pointer; font-size: 12px; line-height: 1;
}
.mini:hover { color: var(--accent); border-color: var(--accent); }

/* ---------- Panels ---------- */
.panel {
  position: fixed; z-index: 9; top: 54px; bottom: 54px; width: 264px;
  background: var(--panel); backdrop-filter: blur(16px);
  border: 1px solid var(--line); border-radius: 12px;
  display: flex; flex-direction: column; overflow: hidden;
  transition: transform 0.25s ease;
}
.panel.left { left: 12px; }
.panel.right { right: 12px; }
.panel.left.collapsed { transform: translateX(calc(-100% - 16px)); }
.panel.right.collapsed { transform: translateX(calc(100% + 16px)); }
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border-bottom: 1px solid var(--line);
  font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted);
}
.scroll { overflow-y: auto; flex: 1; padding: 8px; }
.scroll::-webkit-scrollbar { width: 8px; }
.scroll::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }

/* ---------- Library ---------- */
.lib-group { margin-bottom: 10px; }
.lib-fam { font-size: 10px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin: 6px 4px; }
.lib-item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  background: transparent; border: 1px solid transparent; border-radius: 8px;
  padding: 5px; margin-bottom: 3px; cursor: pointer; color: var(--text);
  font-size: 12px; text-align: left; transition: all 0.12s;
}
.lib-item:hover { background: rgba(255,255,255,0.03); border-color: var(--line); }
.lib-item.active { border-color: var(--accent); background: rgba(55,224,208,0.08); }
.lib-item .thumb { width: 48px; height: 30px; border-radius: 4px; background: #000; flex: none; }

/* ---------- Layer stack ---------- */
.stack { border-top: 1px solid var(--line); padding: 8px; max-height: 42%; overflow-y: auto; }
.panel-title { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin: 2px 4px 8px; }
.panel-desc { font-size: 11px; color: var(--muted); margin: 0 4px 12px; line-height: 1.4; }
.layer {
  display: grid; grid-template-columns: 1fr auto; gap: 4px 6px; align-items: center;
  padding: 6px; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 5px;
}
.layer.active { border-color: var(--accent); }
.layer.muted { opacity: 0.5; }
.layer-name { grid-column: 1 / -1; background: transparent; border: none; color: var(--text); text-align: left; font-size: 12px; cursor: pointer; font-family: var(--mono); }
.layer-blend { font-size: 10px; }
.layer-op { width: 100%; }

/* ---------- Interaction links ---------- */
.layer-link-badge {
  display: flex; align-items: center; justify-content: space-between; gap: 6px;
  font-family: var(--mono); font-size: 10px; color: var(--accent);
  padding: 2px 6px 5px 14px; margin: -3px 0 6px;
}
.link-popover {
  grid-column: 1 / -1; display: flex; flex-direction: column; gap: 5px;
  margin-top: 6px; padding: 8px; border: 1px solid var(--accent); border-radius: 8px;
  background: rgba(10, 12, 16, 0.96);
}
.link-field { display: flex; align-items: center; gap: 6px; font-size: 10px; color: var(--muted); }
.link-field > span { min-width: 30px; }
.link-field select { flex: 1; font-size: 10px; }
.link-gain-lbl { font-family: var(--mono); font-size: 10px; color: var(--accent); }
.link-bar { display: flex; gap: 6px; justify-content: flex-end; }
.link-add { font-size: 11px; padding: 3px 12px; border: 1px solid var(--accent); border-radius: 6px; background: transparent; color: var(--accent); cursor: pointer; }
.link-add:hover { background: var(--accent); color: #05060a; }

/* ---------- Controls ---------- */
.ctrl { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.ctrl > label { font-size: 11px; color: var(--muted); }
.range-wrap { display: flex; align-items: center; gap: 8px; }
.range-wrap input[type=range] { flex: 1; }
.val { font-family: var(--mono); font-size: 11px; color: var(--accent); min-width: 44px; text-align: right; }
.seed-num { width: 80px; background: var(--panel-solid); border: 1px solid var(--line); color: var(--text); border-radius: 5px; padding: 4px; font-family: var(--mono); }
.hint { color: var(--muted); font-size: 12px; padding: 12px; }

select, input[type=text], input[type=number] {
  background: var(--panel-solid); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 5px 7px; font-size: 12px; font-family: var(--sans); outline: none;
}
select:focus, input:focus { border-color: var(--accent); }
input.expr { font-family: var(--mono); font-size: 11px; }
input.expr.invalid { border-color: var(--accent2); }

input[type=range] { -webkit-appearance: none; appearance: none; height: 4px; background: var(--line); border-radius: 2px; }
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 13px; height: 13px; border-radius: 50%;
  background: var(--accent); cursor: pointer; border: 2px solid var(--bg);
}
input[type=range]::-moz-range-thumb { width: 13px; height: 13px; border-radius: 50%; background: var(--accent); cursor: pointer; border: none; }
input[type=color] { width: 100%; height: 26px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel-solid); cursor: pointer; padding: 2px; }

/* Toggle switch */
.switch { position: relative; display: inline-block; width: 40px; height: 22px; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider { position: absolute; inset: 0; background: var(--line); border-radius: 22px; transition: 0.2s; cursor: pointer; }
.switch .slider:before { content: ''; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; background: var(--muted); border-radius: 50%; transition: 0.2s; }
.switch input:checked + .slider { background: rgba(55,224,208,0.3); }
.switch input:checked + .slider:before { transform: translateX(18px); background: var(--accent); }

/* ---------- Preset gallery ---------- */
.presets { border-top: 1px solid var(--line); padding: 8px; max-height: 40%; overflow-y: auto; }
.preset-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; margin-bottom: 8px; }
.preset-chip {
  display: flex; align-items: center; gap: 6px; background: transparent; color: var(--text);
  border: 1px solid var(--line); border-radius: 7px; padding: 5px 7px; font-size: 11px;
  cursor: pointer; text-align: left; transition: all 0.12s; overflow: hidden; white-space: nowrap;
}
.preset-chip:hover { border-color: var(--accent); color: var(--accent); background: rgba(55,224,208,0.06); }
.preset-chip.user { border-style: dashed; }
.preset-chip .preset-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; box-shadow: 0 0 4px rgba(0,0,0,0.4); }
.preset-save { display: flex; gap: 6px; }
.preset-name-input { flex: 1; }
.preset-save .btn { white-space: nowrap; }

/* ---------- Transport / timeline ---------- */
.transport {
  position: fixed; bottom: 54px; left: 50%; transform: translateX(-50%); z-index: 11;
  display: flex; align-items: center; gap: 10px; padding: 7px 14px;
  background: var(--panel); backdrop-filter: blur(16px);
  border: 1px solid var(--line); border-radius: 10px; max-width: min(720px, 70vw); width: max-content;
}
.transport .scrub { width: 320px; max-width: 42vw; }
.transport-val { font-family: var(--mono); font-size: 11px; color: var(--muted); min-width: 42px; }
.transport-val.fps { color: var(--accent); }
.transport-loop { font-size: 11px; color: var(--muted); display: flex; align-items: center; gap: 4px; }
.looplen { width: 62px; }

/* ---------- MIDI ---------- */
.midi-status { font-family: var(--mono); font-size: 11px; color: var(--muted); padding: 0 8px; }
.midi-learn { min-width: 30px; width: auto; padding: 0 5px; font-family: var(--mono); font-size: 9px; }
.midi-learn.bound { border-color: var(--accent2); color: var(--accent2); }
.midi-learn.learning { border-color: var(--accent); color: var(--accent); }

/* ---------- Export scale ---------- */
.scale-sel { padding: 5px 6px; }

/* ---------- Bottom bar ---------- */
.bottombar {
  position: fixed; bottom: 0; left: 0; right: 0; height: 46px; z-index: 10;
  display: flex; align-items: center; padding: 0 14px; gap: 10px;
  background: linear-gradient(to top, rgba(10,11,13,0.85), rgba(10,11,13,0)); pointer-events: none;
}
.bottombar > * { pointer-events: auto; }
.export-group { display: flex; gap: 6px; }
.spacer { flex: 1; }

/* ---------- Hide UI ---------- */
body.ui-hidden .topbar, body.ui-hidden .panel, body.ui-hidden .bottombar, body.ui-hidden .transport { opacity: 0; pointer-events: none; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; bottom: 60px; left: 50%; transform: translateX(-50%) translateY(10px);
  background: var(--panel-solid); border: 1px solid var(--accent); color: var(--text);
  padding: 8px 16px; border-radius: 8px; font-size: 12px; z-index: 20;
  opacity: 0; transition: all 0.25s; pointer-events: none;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 720px) {
  .panel { width: 210px; }
  .brand .sub { display: none; }
}
