:root {
  color-scheme: dark;
  --bg: #17140f;
  --panel: #211d17;
  --panel-2: #2a251d;
  --line: #3a3328;
  --text: #f1e9dc;
  --muted: #a89c88;
  --lead: #ff8a3d;
  --lead-soft: rgba(255, 138, 61, 0.18);
  --bass: #4cc9b0;
  --bass-soft: rgba(76, 201, 176, 0.18);
  --danger: #ff6b6b;
  --key-white: #efe8dc;
  --key-white-scale: #ffe1c7;
  --key-black: #1d1a16;
  --key-black-scale: #4a3a2b;
  --key-line: #8e8474;
  --radius: 10px;
  --mono: ui-monospace, 'SF Mono', 'JetBrains Mono', Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --bar-h: 64px;
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg: #f4efe6;
    --panel: #fffcf6;
    --panel-2: #f1ebe0;
    --line: #ddd3c3;
    --text: #231e17;
    --muted: #6f6556;
    --lead: #e2620f;
    --lead-soft: rgba(226, 98, 15, 0.14);
    --bass: #128a76;
    --bass-soft: rgba(18, 138, 118, 0.14);
    --key-white: #fffdf9;
    --key-white-scale: #ffe6d1;
    --key-black: #2a251f;
    --key-black-scale: #6b4c33;
    --key-line: #b8ad9c;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--sans);
  padding-bottom: calc(var(--bar-h) + 24px + env(safe-area-inset-bottom));
}

button, input, select { font: inherit; color: inherit; }

h1, h2 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h1 { font-size: 18px; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }

[hidden] { display: none !important; }

/* ---- Header & transport ---- */

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 16px;
  max-width: 1120px;
  margin: 0 auto;
}

.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 22px; height: 22px; fill: var(--lead); }

.transport {
  position: fixed;
  z-index: 10;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
}

.transport .primary { flex: 1; }
.transport .play { flex: 1; }

.tempo {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}
.tempo input { width: 64px; text-align: center; }

@media (min-width: 760px) {
  body { padding-bottom: 32px; }
  .transport {
    position: static;
    padding: 0;
    background: none;
    border: 0;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
  .transport .primary, .transport .play { flex: none; min-width: 120px; }
}

/* ---- Controls ---- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  cursor: pointer;
  font-weight: 600;
  white-space: nowrap;
  touch-action: manipulation;
  transition: background 0.12s, border-color 0.12s, transform 0.06s;
}
.btn:hover { border-color: var(--muted); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.4; cursor: default; transform: none; }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: 13px; }
.btn.ghost { background: none; border-color: transparent; color: var(--muted); }
.btn.on { background: var(--lead-soft); border-color: var(--lead); }

.btn.primary { background: var(--lead); border-color: var(--lead); color: #1b1209; }
.btn.primary.stale { box-shadow: 0 0 0 3px var(--lead-soft); }
.btn.primary.stale::after { content: '•'; margin-left: -2px; }

.play svg { width: 16px; height: 16px; fill: currentColor; }
.play .i-stop { display: none; }
.play[aria-pressed="true"] { border-color: var(--bass); background: var(--bass-soft); }
.play[aria-pressed="true"] .i-play { display: none; }
.play[aria-pressed="true"] .i-stop { display: block; }

input[type="text"], input[type="number"], select {
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  width: 100%;
  min-width: 0;
}
select { appearance: none; -webkit-appearance: none; padding-right: 32px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%;
  background-size: 5px 5px;
  background-repeat: no-repeat;
}
input:focus-visible, select:focus-visible, .btn:focus-visible, .step:focus-visible {
  outline: 2px solid var(--lead);
  outline-offset: 2px;
}

input[type="range"] { width: 100%; accent-color: var(--lead); min-height: 44px; }

.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); cursor: pointer; }
.check input { accent-color: var(--lead); width: 16px; height: 16px; }

.hint { margin: 6px 0 0; font-size: 12.5px; color: var(--muted); }
.hint.error { color: var(--danger); }

/* ---- Layout ---- */

main {
  display: grid;
  gap: 14px;
  max-width: 1120px;
  margin: 0 auto;
  padding: 0 16px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  min-width: 0;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
.field { display: grid; gap: 4px; align-content: start; min-width: 0; }
.field > label { font-size: 12px; font-weight: 600; color: var(--muted); }
.field > label small { font-weight: 400; }
.field output { color: var(--text); font-family: var(--mono); margin-left: 4px; }
.sound, .length, .density, .spacing, .chords, .style-hint { grid-column: 1 / -1; }

.range-ends { display: flex; justify-content: space-between; margin-top: -6px; font-size: 11px; color: var(--muted); }

.label-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.label-row label { font-size: 12px; font-weight: 600; color: var(--muted); }
.field output small { color: var(--muted); font-family: var(--sans); font-weight: 400; }
.link-btn {
  padding: 0;
  border: 0;
  background: none;
  color: var(--lead);
  font-size: 12px;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.style-hint { margin-top: -4px; }

.chord-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.chord-row input { grid-column: 1 / -1; }
.chord-row select { width: 100%; }

@media (min-width: 760px) {
  .controls { grid-template-columns: repeat(6, minmax(0, 1fr)); }
  .sound, .length, .density, .spacing { grid-column: span 2; }
  .chords { grid-column: span 4; }
  .chord-row { grid-template-columns: 1fr 180px auto; }
  .chord-row input { grid-column: auto; }
}

/* ---- Keyboard ---- */

.legend { display: flex; gap: 14px; margin: 0; padding: 0; list-style: none; font-size: 12px; color: var(--muted); }
.legend li { display: flex; align-items: center; gap: 6px; }
.sw { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.sw-lead { background: var(--lead); }
.sw-bass { background: var(--bass); border-radius: 2px; }
.sw-scale { background: var(--key-white-scale); border: 1px solid var(--key-line); border-radius: 2px; }

/* On phones, keep the keyboard in reach while a step is being edited. */
@media (max-width: 759px) {
  .editing .kb-panel {
    position: sticky;
    top: 0;
    z-index: 5;
    padding: 8px;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.18);
  }
  .editing .kb-panel .panel-head { display: none; }
}

.keyboard {
  overflow-x: auto;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
  border-radius: 6px;
}
.keyboard svg { display: block; width: 100%; height: auto; touch-action: manipulation; user-select: none; -webkit-user-select: none; }

.key { cursor: pointer; }
.key .body { stroke: var(--key-line); stroke-width: 1; }
.key.white .body { fill: var(--key-white); }
.key.white.in-scale .body { fill: var(--key-white-scale); }
.key.black .body { fill: var(--key-black); }
.key.black.in-scale .body { fill: var(--key-black-scale); }
.key.white.now-lead .body, .key.black.now-lead .body { fill: var(--lead); }
.key.white.now-bass .body, .key.black.now-bass .body { fill: var(--bass); }
.key.selected .body { stroke: var(--text); stroke-width: 2.5; }
.key:active .body { filter: brightness(0.9); }
.key text { font: 600 8px var(--sans); fill: #7d7263; text-anchor: middle; pointer-events: none; }
.key.root text { fill: var(--lead); }
.mark-lead { fill: var(--lead); stroke: rgba(0, 0, 0, 0.25); pointer-events: none; }
.mark-bass { fill: var(--bass); stroke: rgba(0, 0, 0, 0.25); pointer-events: none; }
.key.now-lead .mark-lead, .key.now-bass .mark-bass { fill: #fff; }

/* ---- Steps ---- */

.toggles { display: flex; gap: 14px; flex-wrap: wrap; }

.lane + .lane { margin-top: 14px; }
.lane-label { font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }

.steps {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 4px;
}
@media (min-width: 760px) {
  .steps { grid-template-columns: repeat(16, minmax(0, 1fr)); }
}

.step {
  position: relative;
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 1px;
  min-height: 52px;
  padding: 14px 0 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-2);
  cursor: pointer;
  touch-action: manipulation;
  min-width: 0;
}
.step.beat { border-left: 2px solid var(--muted); }
.step .num { position: absolute; top: 3px; left: 5px; font-size: 9.5px; color: var(--muted); font-family: var(--mono); }
.step .note { font-family: var(--mono); font-weight: 650; font-size: 13px; }
.step .chord {
  position: absolute;
  top: 2px;
  right: 4px;
  font-size: 9.5px;
  font-weight: 700;
  color: var(--lead);
  max-width: 70%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.step.rest .note, .step.tie .note { color: var(--muted); font-weight: 400; }
.step.tie { background: transparent; border-style: dashed; }

[data-lane="lead"] .step.note { background: var(--lead-soft); }
[data-lane="bass"] .step.note { background: var(--bass-soft); }
[data-lane="lead"] .step.playing { background: var(--lead); color: #1b1209; border-color: var(--lead); }
[data-lane="bass"] .step.playing { background: var(--bass); color: #08201b; border-color: var(--bass); }
.step.playing .note, .step.playing .num, .step.playing .chord { color: inherit; }
.step.selected { outline: 2px solid var(--text); outline-offset: 1px; }

/* ---- Editor ---- */

.editor { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.editor .editor-tools { display: none; }
.editor.active .editor-tools { display: flex; }
.editor.active .editor-empty { display: none; }
.editor-empty { margin: 0; }

.editor-tools { flex-wrap: wrap; gap: 10px; align-items: center; }
.editor-label { font-family: var(--mono); font-size: 13px; width: 100%; }
.tool-group { display: flex; gap: 4px; }

@media (min-width: 760px) {
  .editor-label { width: auto; min-width: 200px; }
}

/* ---- Text & saves ---- */

.text-out {
  margin: 0;
  padding: 12px;
  background: var(--panel-2);
  border-radius: 6px;
  font: 12.5px/1.6 var(--mono);
  overflow-x: auto;
  white-space: pre;
}

.save-row { display: flex; gap: 8px; }
.saves { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 6px; }
.saves li { display: flex; align-items: center; gap: 6px; }
.saves li.hint { display: block; }
.save-load {
  flex: 1;
  display: grid;
  text-align: left;
  min-width: 0;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  cursor: pointer;
}
.save-load:hover { border-color: var(--muted); }
.save-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.save-meta { font-size: 12px; color: var(--muted); }

/* ---- Toast ---- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--bar-h) + 24px + env(safe-area-inset-bottom));
  transform: translate(-50%, 8px);
  padding: 10px 16px;
  border-radius: 8px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
  z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (min-width: 760px) {
  .toast { bottom: 24px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
