* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --accent: #e0a94e;
  --bg: #0b0b0c;
  --panel: #17181a;
  --panel-border: #2a2b2e;
  --text: #f2f2f2;
  --text-dim: rgba(242, 242, 242, 0.6);
}

html, body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Montserrat', sans-serif;
}

/* Generic force-hide utility, used to hide whole top-level sections while
   an envelope is being edited "full screen" (see setFocusMode() in
   app.js). !important because some targets (e.g. .editor-layout,
   .osc-stack) set their own `display`, and this utility's effect
   shouldn't depend on which of two same-specificity rules happens to
   come later in the file. */
.is-hidden {
  display: none !important;
}

body {
  padding: 24px 16px 64px;
  max-width: 1180px;
  margin: 0 auto;
}

header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 20px;
}

header h1 {
  font-size: clamp(22px, 4vw, 30px);
  font-weight: 700;
}

header a {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 14px;
}
header a:hover {
  color: var(--accent);
}

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 18px 20px;
  margin-bottom: 20px;
}
.panel.compact-panel {
  padding: 14px 16px;
  margin-bottom: 14px;
}

.panel h2 {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 4px;
}

.panel-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 4px;
}
.panel-header-row h2 {
  margin-bottom: 0;
}
.panel-toggle {
  padding: 4px 12px;
  font-size: 11px;
}
.panel-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.panel-note {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 12px;
  line-height: 1.4;
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: flex-end;
  margin-bottom: 10px;
}
.compact-panel .row {
  gap: 10px;
  margin-bottom: 8px;
}

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--text-dim);
}

select, input[type="text"] {
  background: #0f0f10;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 6px 8px;
  font-family: inherit;
  font-size: 13px;
  min-width: 150px;
}

/* Dropdown (labeled <select>) */
.dropdown {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.dropdown-label {
  font-size: 10px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.dropdown select {
  min-width: 130px;
}

/* Graphical dropdowns: a labeled button showing the current choice as an
   icon (waveforms, DCO1×DCO2 modulation, vibrato wave), which opens a
   popup grid of every choice (icon + name) to pick from. */
.icon-dropdown {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.icon-dropdown .dropdown-label {
  text-align: center;
}
.icon-glyph {
  width: 42px;
  height: 32px;
  flex: none;
  background: #0f0f10;
  border: 1px solid var(--panel-border);
  border-radius: 5px;
  padding: 2px 3px;
}
.icon-glyph-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 3;
  stroke-linecap: butt;
  stroke-linejoin: miter;
}

.icon-picker {
  position: relative;
}
.icon-picker-btn {
  padding: 0;
  border: none;
  background: none;
  line-height: 0;
  cursor: pointer;
  display: inline-flex;
}
.icon-picker-btn:hover .icon-glyph {
  border-color: var(--accent);
}
.icon-picker-menu {
  display: none;
  position: absolute;
  z-index: 30;
  top: calc(100% + 4px);
  right: 0;
  grid-template-columns: repeat(2, 1fr);
  gap: 4px;
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  box-shadow: 0 8px 22px rgba(0,0,0,0.45);
}
.icon-picker-menu.open {
  display: grid;
}
.icon-picker-option {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 5px;
  background: #0f0f10;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  cursor: pointer;
}
.icon-picker-option:hover {
  border-color: var(--accent);
}
.icon-picker-option.active {
  border-color: var(--accent);
  background: #241c0c;
}
.icon-picker-option .icon-glyph {
  width: 34px;
  height: 26px;
  background: transparent;
  border: none;
  padding: 0;
}
.icon-picker-option-label {
  font-size: 8px;
  color: var(--text-dim);
  text-align: center;
  line-height: 1.15;
  white-space: nowrap;
}

button {
  background: #232427;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 9px 16px;
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
}
button:hover {
  border-color: var(--accent);
}
button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
button.primary-btn {
  background: var(--accent);
  color: #1a1200;
  border-color: var(--accent);
  font-weight: 600;
}
button.primary-btn:hover {
  filter: brightness(1.08);
}

/* Knob */
.knob {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  width: 52px;
  user-select: none;
}
.knob-dial {
  width: 40px;
  height: 40px;
  cursor: ns-resize;
  touch-action: none;
}
.knob-dial:focus {
  outline: none;
}
.knob-dial:focus .knob-face {
  stroke: var(--accent);
}
.knob-face {
  fill: #0f0f10;
  stroke: var(--panel-border);
  stroke-width: 2;
}
.knob-pointer {
  stroke: var(--accent);
  stroke-width: 3;
  stroke-linecap: round;
}
.knob-label {
  font-size: 10px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.knob-value {
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

/* Choice group (button switches) */
.choice-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.choice-label {
  font-size: 10px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.choice-row {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}
.choice-btn {
  padding: 6px 10px;
  font-size: 12px;
  min-width: 34px;
}
.choice-btn.active {
  background: var(--accent);
  color: #1a1200;
  border-color: var(--accent);
  font-weight: 600;
}

/* Stepper (numeric rate/level control: drag the value, or nudge with the
   small arrows) */
.stepper {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  width: 32px;
  user-select: none;
}
.stepper-label {
  font-size: 9px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.stepper-arrow {
  width: 100%;
  height: 14px;
  padding: 0;
  font-size: 8px;
  line-height: 1;
  background: #1c1d1f;
  border: 1px solid var(--panel-border);
  border-radius: 3px;
}
.stepper-arrow:hover {
  border-color: var(--accent);
}
.stepper-value {
  width: 100%;
  padding: 2px 0;
  text-align: center;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  cursor: ns-resize;
  border-radius: 3px;
  /* Without this, a touch drag here scrolls the page instead of reaching
     the pointermove drag handler below (see knob-dial for the same fix). */
  touch-action: none;
}
.stepper-value:focus {
  outline: 1px solid var(--accent);
}

/* Envelope numeric strip: one column per stage, shown alongside the graph.
   A grid (not flex) so the 8 columns always span the full width of the
   editing area, evenly, instead of hugging their own content width and
   leaving space unused; auto-fit + minmax keeps each column at least
   44px, wrapping to fewer per row only if the panel gets too narrow for
   all 8 (see the max-width:560px rule below). Widened from 44px now that
   level/rate sit side by side again (see .envelope-strip-steppers) - two
   32px steppers plus their gap need more like 70px+ to stay unwrapped. */
.envelope-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(76px, 1fr));
  gap: 6px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--panel-border);
}
.envelope-strip-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 6px 5px;
  background: #0f0f10;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  min-width: 0;
}
.envelope-strip-col.inactive {
  opacity: 0.4;
}
.envelope-strip-num {
  font-size: 10px;
  font-weight: 600;
  color: var(--text-dim);
}
/* Level and rate side by side - now that the envelope editor takes over
   the full page width while expanded, there's room for both without
   wrapping the 8-stage strip. */
.envelope-strip-steppers {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 4px;
}
.envelope-strip-sustain {
  padding: 3px 6px;
  font-size: 9px;
  width: 100%;
}
.envelope-strip-sustain.active {
  background: #6fc2ff;
  color: #062033;
  border-color: #6fc2ff;
  font-weight: 600;
}

/* Envelope editor
   Each .envelope-block is a *subgrid* of .envelope-row's row tracks, so
   the title row, the stage-count/Edit row, and the graph row each take
   the height of their tallest sibling across DCA/DCW/DCO - keeping all
   three graphs vertically aligned even when one title wraps to two
   lines and the others don't. */
.envelope-row {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--panel-border);
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: auto auto auto;
  gap: 10px;
  align-items: start;
}
.envelope-row.is-hidden {
  display: none;
}
.envelope-block {
  display: grid;
  grid-template-rows: subgrid;
  grid-row: span 3;
  row-gap: 6px;
  background: #101112;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 8px;
}
.envelope-block.is-hidden {
  display: none;
}
.envelope-title {
  font-size: 11px;
  font-weight: 600;
}
.envelope-stats-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.envelope-summary {
  margin-right: auto;
  font-size: 10px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.envelope-toggle {
  padding: 4px 10px;
  font-size: 11px;
}
/* The 🎲 and 🔄 next to ✏️, for reshuffling/resetting this envelope without
   opening it - both hidden again while the block is expanded (see toggle()
   in envelopeBlock). */
.envelope-randomize {
  padding: 4px 7px;
  font-size: 11px;
}
.envelope-thumb {
  width: 100%;
  height: 130px;
  display: block;
  background: #0f0f10;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  cursor: pointer;
}
.envelope-thumb:hover {
  border-color: var(--accent);
}
.envelope-detail {
  margin-top: 8px;
}
.envelope-canvas {
  width: 100%;
  height: 260px;
  display: block;
  background: #0f0f10;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  touch-action: none;
  cursor: crosshair;
}
.envelope-note, .envelope-hint {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 4px;
  line-height: 1.4;
}

/* Envelope tools: curve generator, rate/level stretch, randomize, copy/
   paste - shown below the numeric strip while an envelope is expanded. */
.envelope-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--panel-border);
}
.tool-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1 1 220px;
  background: #0f0f10;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 8px 10px;
}
/* Stretch/scale needs more elbow room than the other two groups (it's grown
   from two horizontal sliders to four vertical slider/nudge columns);
   randomize/copy needs the least, being just three buttons - so it gives
   up width for stretch/scale to take. */
.tool-group-wide {
  flex: 1.6 1 260px;
}
.tool-group-narrow {
  flex: 0.6 1 140px;
}
.tool-group-label {
  font-size: 10px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.tool-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px;
}
.tool-btn {
  padding: 6px 10px;
  font-size: 11px;
}
.tool-pair {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}
/* Pairing a vertical RateSlider with its nudge stepper - both are narrow
   columns of about the same height, so center them on each other instead
   of the horizontal pairing's flex-end (baseline-ish) alignment. */
.tool-pair-vertical {
  align-items: center;
}
/* Houses the four stretch/scale columns (level slider, level nudge, rate
   slider, rate nudge) - more breathing room between the level and rate
   pairs than the default .tool-row gap. */
.tool-row-vertical {
  gap: 18px;
}
.tool-hint {
  font-size: 10px;
  color: var(--text-dim);
  line-height: 1.35;
}
/* Sole child of the .library-list grid when a filter matches nothing -
   without this it would sit in just one narrow grid column instead of
   reading as a message for the whole panel. */
.library-list .tool-hint {
  grid-column: 1 / -1;
}
.tool-checkbox-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 10px;
  color: var(--text-dim);
  cursor: pointer;
}
.tool-checkbox {
  width: 13px;
  height: 13px;
  accent-color: var(--accent);
  cursor: pointer;
}

/* RateSlider: a "press and hold, releases to center" slider used by the
   envelope Stretch/Scale tools - see RateSlider in knob.js. */
.rate-slider {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 2px;
  width: 150px;
}
.rate-slider-label {
  font-size: 10px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  text-align: center;
}
.rate-slider-input {
  width: 100%;
  accent-color: var(--accent);
  cursor: ew-resize;
}
.rate-slider-hint {
  display: flex;
  justify-content: space-between;
  font-size: 9px;
  color: var(--text-dim);
}

/* Vertical variant - stretch/scale now runs its sliders top-to-bottom
   (grow at top, shrink at bottom) so level and rate can sit side by side
   with their nudge steppers instead of stacked full-width rows. Chromium
   (this app already requires Chrome/Edge/Brave/Opera for MIDI SysEx) turns
   a range input vertical via -webkit-appearance:slider-vertical, which
   renders it rotated: width is the track's thickness, height is its length. */
.rate-slider-vertical {
  width: auto;
  align-items: center;
}
.rate-slider-input.rate-slider-input-vertical {
  -webkit-appearance: slider-vertical;
  width: 8px;
  height: 92px;
  cursor: ns-resize;
}
.rate-slider-hint-vert {
  font-size: 9px;
  color: var(--text-dim);
}

@media (max-width: 560px) {
  .envelope-row {
    grid-template-columns: 1fr;
  }
}

/* Shared full-width host for whichever envelope is currently being
   edited - see the comment above buildOscillatorSection() in app.js.
   No border/background of its own (the moved-in .envelope-block already
   has that); just page-flow spacing to match the other panels. */
.expanded-envelope-host {
  margin-bottom: 14px;
}

/* Patch library (see buildLibrarySection() in app.js) */
.library-text-input {
  min-width: 0;
}
.library-save-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  padding: 10px;
  background: #0f0f10;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
}
.library-save-form input {
  flex: 1 1 160px;
}
.library-body-actions {
  justify-content: flex-end;
  margin-bottom: 4px;
}
.library-filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
}
.library-search {
  flex: 0 1 220px;
  min-width: 140px;
}
.library-tag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  flex: 1 1 auto;
}
.library-tag-chip {
  padding: 3px 10px;
  font-size: 10px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.library-tag-chip.active {
  background: var(--accent);
  color: #1a1200;
  border-color: var(--accent);
  font-weight: 600;
}
/* A tag pill shown on a library row, not a filter toggle - not a button, so
   no hover/active affordance, just the same pill shape for visual grouping. */
.library-tag-chip-static {
  display: inline-block;
  padding: 3px 10px;
  font-size: 10px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  background: #0f0f10;
  border: 1px solid var(--panel-border);
  color: var(--text-dim);
}
/* A fixed-height, scrolling grid rather than a single column that just
   keeps growing - with 100+ factory patches now alongside the hand-built
   set, letting the list run to whatever length it wants would make this
   panel dwarf the rest of the editor. Multiple compact columns (like a
   hardware librarian's patch list) fit far more on screen at once than
   one wide row per patch ever could; auto-fill lets the column count grow
   on wider windows rather than locking in a fixed number. */
.library-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  align-content: start;
  gap: 6px;
  margin-top: 10px;
  max-height: 360px;
  overflow-y: auto;
  padding-right: 4px; /* keeps the scrollbar from sitting flush against card content */
}
.library-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 7px 9px;
  background: #0f0f10;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  min-width: 0;
}
.library-row-main {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}
.library-row-name {
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.library-row-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  min-width: 0;
}
/* Load (⬇️) sits right before the name, and the tag-edit icon (🏷️) right
   before the tags themselves - each icon reads as a label for what follows
   it, rather than living off in a separate actions column. */
.library-row-name-row,
.library-row-tags-row {
  display: flex;
  align-items: center;
  gap: 5px;
  min-width: 0;
}
.library-row-name-row .tool-btn,
.library-row-tags-row .tool-btn {
  padding: 3px 6px;
  font-size: 11px;
  flex-shrink: 0;
}
.library-row-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
  justify-content: flex-end;
}
.library-row-actions .tool-btn {
  padding: 4px 8px;
}

/* MIDI status log */
#status-log {
  margin-top: 12px;
  max-height: 160px;
  overflow-y: auto;
  font-size: 11px;
  font-family: 'SF Mono', Consolas, monospace;
  color: var(--text-dim);
  background: #0f0f10;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 8px 10px;
}
#status-log div {
  padding: 2px 0;
  border-bottom: 1px solid rgba(255,255,255,0.04);
  word-break: break-all;
}

/* Voice basics (skinny, left) next to both oscillator cards stacked on
   top of each other (fat, right). Below the breakpoint this is just two
   stacked block-level divs - Voice basics above the oscillator stack -
   which is the right fallback for a narrow/mobile layout anyway. */
@media (min-width: 760px) {
  .editor-layout {
    display: grid;
    grid-template-columns: minmax(220px, 300px) 1fr;
    gap: 20px;
    /* Voice basics is much shorter than the oscillator stack beside it -
       without this it would stretch to match the taller column. */
    align-items: start;
  }
}

.osc-stack {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
