/* Theme tokens: dark by default, light via system preference or the theme button.
   Colours the canvas reads (--stage, --text, --muted, --faint, --track, --grid, --l1..4)
   must stay 6-digit hex. */
:root {
  --bg: #0b0c10;
  --panel: #0f1116;
  --surface: #161920;
  --surface-2: #1d2029;
  --border: #262a35;
  --stage: #0b0c10;
  --stage-glow: #151821;
  --text: #eceef3;
  --muted: #8b92a3;
  --faint: #545b6b;
  --track: #1c1f28;
  --grid: #232733;
  --accent: #ff8a1f;
  --on-accent: #1a0d00;
  --l1: #ff8a1f;
  --l2: #2ec5e8;
  --l3: #b48cff;
  --l4: #9be15d;
  --shadow: 0 10px 30px rgba(0, 0, 0, .45);
  --topbar-h: 64px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f4f5f8;
    --panel: #eef0f4;
    --surface: #ffffff;
    --surface-2: #f1f2f6;
    --border: #dfe2e9;
    --stage: #fafbfc;
    --stage-glow: #ffffff;
    --text: #15171d;
    --muted: #5d6474;
    --faint: #a3a9b6;
    --track: #eceef3;
    --grid: #e3e6ec;
    --accent: #f07a0c;
    --on-accent: #ffffff;
    --l1: #ec7300;
    --l2: #0b9cc4;
    --l3: #7d52f0;
    --l4: #4c9d17;
    --shadow: 0 10px 30px rgba(20, 24, 35, .12);
    color-scheme: light;
  }
}

:root[data-theme="light"] {
  --bg: #f4f5f8;
  --panel: #eef0f4;
  --surface: #ffffff;
  --surface-2: #f1f2f6;
  --border: #dfe2e9;
  --stage: #fafbfc;
  --stage-glow: #ffffff;
  --text: #15171d;
  --muted: #5d6474;
  --faint: #a3a9b6;
  --track: #eceef3;
  --grid: #e3e6ec;
  --accent: #f07a0c;
  --on-accent: #ffffff;
  --l1: #ec7300;
  --l2: #0b9cc4;
  --l3: #7d52f0;
  --l4: #4c9d17;
  --shadow: 0 10px 30px rgba(20, 24, 35, .12);
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}

[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

kbd {
  display: inline-block;
  min-width: 1.6em;
  padding: 0 .35em;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface-2);
  font: 600 11px/1.6 var(--font);
  text-align: center;
}

/* ---------- Top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 16px;
  height: var(--topbar-h);
  padding: 0 20px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1 1 0;
  font-weight: 700;
  letter-spacing: .01em;
  white-space: nowrap;
}
.brand svg { width: 26px; height: 26px; color: var(--accent); }

.transport { display: flex; align-items: center; gap: 12px; }

.play {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
  box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 50%, transparent);
  transition: transform .12s, box-shadow .2s;
}
.play:hover { transform: scale(1.05); }
.play:active { transform: scale(.96); }
.play svg { width: 24px; height: 24px; }
.play .i-stop, .play[aria-pressed="true"] .i-play { display: none; }
.play[aria-pressed="true"] .i-stop { display: block; }
.play[aria-pressed="true"] { box-shadow: 0 0 0 6px color-mix(in srgb, var(--accent) 22%, transparent); }

.tempo {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.tempo > button {
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  font-size: 18px;
  color: var(--muted);
}
.tempo > button:hover { background: var(--surface-2); color: var(--text); }

.bpm { display: flex; align-items: baseline; gap: 4px; padding: 0 4px; cursor: text; }
.bpm input {
  width: 3.4ch;
  padding: 0;
  border: 0;
  background: none;
  font-size: 26px;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
  text-align: right;
  appearance: textfield;
  -moz-appearance: textfield;
}
.bpm input::-webkit-inner-spin-button, .bpm input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.bpm input:focus { outline: none; }
.bpm span { font-size: 11px; font-weight: 600; letter-spacing: .08em; color: var(--muted); }

.chip {
  height: 40px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-weight: 600;
}
.chip:hover { background: var(--surface-2); }
.chip.flash { border-color: var(--accent); color: var(--accent); }

.actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; flex: 1 1 0; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--muted);
}
.icon-btn:hover { color: var(--text); background: var(--surface-2); }
.icon-btn svg { width: 18px; height: 18px; }

/* ---------- Segmented control, stepper, switch, range, select ---------- */

.seg {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.seg button {
  padding: 5px 12px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
  font-size: 13px;
}
.seg button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .25);
}

.stepper {
  display: inline-flex;
  align-items: center;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.stepper button {
  width: 30px;
  height: 30px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 16px;
  border-radius: 9px;
}
.stepper button:hover:not(:disabled) { color: var(--text); background: var(--surface); }
.stepper button:disabled { opacity: .3; cursor: default; }
.stepper output, .stepper input {
  min-width: 2.6ch;
  text-align: center;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.stepper input {
  width: 3.2ch;
  padding: 0;
  border: 0;
  background: none;
  appearance: textfield;
  -moz-appearance: textfield;
}
.stepper input::-webkit-inner-spin-button, .stepper input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }

.switch { position: relative; display: inline-block; width: 38px; height: 22px; flex: none; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch span {
  position: absolute;
  inset: 0;
  border-radius: 22px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  transition: background .18s, border-color .18s;
  pointer-events: none;
}
.switch span::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform .18s, background .18s;
}
.switch input:checked + span { background: var(--accent); border-color: var(--accent); }
.switch input:checked + span::before { transform: translateX(16px); background: #fff; }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

input[type="range"] {
  flex: 1;
  min-width: 0;
  height: 22px;
  margin: 0;
  background: transparent;
  accent-color: var(--accent);
  -webkit-appearance: none;
  appearance: none;
  --fill: var(--accent);
  --pct: 50%; /* set from JS so WebKit can draw the filled part */
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 4px;
  background: linear-gradient(to right, var(--fill) 0 var(--pct), var(--border) var(--pct) 100%);
}
input[type="range"]::-moz-range-track { height: 4px; border-radius: 4px; background: var(--border); }
input[type="range"]::-moz-range-progress { height: 4px; border-radius: 4px; background: var(--fill); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  margin-top: -6px;
  border-radius: 50%;
  background: var(--fill);
  border: 3px solid var(--surface);
  box-shadow: 0 0 0 1px var(--fill);
}
input[type="range"]::-moz-range-thumb {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--fill);
  border: 3px solid var(--surface);
  box-shadow: 0 0 0 1px var(--fill);
}

select {
  flex: 1;
  min-width: 0;
  height: 32px;
  padding: 0 28px 0 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface-2) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%238b92a3' stroke-width='1.5'/%3E%3C/svg%3E") no-repeat right 10px center;
  appearance: none;
  -webkit-appearance: none;
}
select.compact { flex: none; height: 28px; font-size: 12px; }

.btn {
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface-2);
  font-weight: 600;
  font-size: 13px;
}
.btn:hover:not(:disabled) { border-color: var(--faint); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.ghost { background: transparent; color: var(--muted); }

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

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 372px;
  height: calc(100vh - var(--topbar-h));
  height: calc(100dvh - var(--topbar-h));
}

.stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  background: radial-gradient(ellipse at center, var(--stage-glow) 0%, var(--stage) 70%);
}

#canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: manipulation; }

.hud {
  position: absolute;
  top: 14px;
  left: 18px;
  right: 18px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  pointer-events: none;
  z-index: 1;
}
.ratio { display: flex; align-items: baseline; gap: 6px; font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.ratio .sep { color: var(--faint); font-weight: 500; }
.ratio .muted { opacity: .35; }
.hud-meta { text-align: right; color: var(--muted); font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }
.hud-meta b { color: var(--text); font-size: 15px; }

.alt {
  position: absolute;
  top: 52px;
  left: 18px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  max-width: 60%;
  pointer-events: none;
  z-index: 1;
}
.alt-group { display: flex; gap: 3px; padding: 4px 6px; border-radius: 8px; transition: background .2s; }
.alt-group.active { background: color-mix(in srgb, var(--c) 16%, transparent); }
.alt-group span { width: 8px; height: 8px; border-radius: 50%; border: 1.5px solid var(--c); opacity: .6; }
.alt-group span.on { background: var(--c); opacity: 1; }

.pads {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  z-index: 1;
}
.pad {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 92px;
  height: 70px;
  border: 1.5px solid var(--c);
  border-radius: 14px;
  background: color-mix(in srgb, var(--c) 10%, var(--surface));
  color: var(--text);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  transition: transform .06s, background .15s;
}
.pad.hit { transform: scale(.95); background: color-mix(in srgb, var(--c) 35%, var(--surface)); }
.pad-key { position: absolute; top: 5px; left: 8px; font-size: 10px; font-weight: 700; color: var(--muted); }
.pad-name { font-size: 18px; font-weight: 800; color: var(--c); }
.pad-err { font-size: 11px; font-weight: 700; font-variant-numeric: tabular-nums; min-height: 1.2em; }

.panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  padding: 14px;
  overflow-y: auto;
  background: var(--panel);
  border-left: 1px solid var(--border);
}

.card {
  padding: 12px 14px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}
.card h2 {
  margin: 0 0 6px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--muted);
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 6px; }
.card-head h2 { margin: 0; }

.field { display: flex; align-items: center; gap: 10px; min-height: 40px; }
.field > label:not(.switch), .field > .label { flex: 0 0 88px; color: var(--muted); }
.field .val { min-width: 4ch; text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.unit { color: var(--muted); font-size: 13px; }
.offset { flex: 1; font-weight: 600; font-variant-numeric: tabular-nums; }

[data-requires].is-off { display: none; }

.hint { margin: 6px 0 4px; color: var(--muted); font-size: 12.5px; }
.hint b { color: var(--text); font-weight: 600; }
.shortcuts { margin: 0 4px 8px; color: var(--muted); font-size: 12px; line-height: 2; }

/* ---------- Layer cards ---------- */

.layers { display: flex; flex-direction: column; gap: 8px; margin-bottom: 4px; }

.layer {
  padding: 8px 10px 8px 12px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--c);
  border-radius: 11px;
  background: var(--surface-2);
}
.layer.is-muted { opacity: .55; }
.layer-row { display: flex; align-items: center; gap: 8px; min-height: 36px; }
.layer-name { flex: 1; font-weight: 700; color: var(--c); }
.layer .stepper { background: var(--surface); }
.layer select { background-color: var(--surface); }
.layer input[type="range"] { --fill: var(--c); }
.layer .icon-btn { width: 30px; height: 30px; border-radius: 8px; }
.layer .icon-btn svg { width: 16px; height: 16px; }
.layer .icon-btn[aria-pressed="true"] { color: var(--c); border-color: var(--c); }

/* ---------- Train stats ---------- */

.stats { display: grid; grid-template-columns: auto repeat(4, 1fr); gap: 2px 10px; margin: 4px 0; font-size: 12.5px; font-variant-numeric: tabular-nums; }
.stats .h { color: var(--muted); font-size: 11px; font-weight: 600; text-align: right; }
.stats .h:first-child { text-align: left; }
.stats .c { text-align: right; font-weight: 600; }
.stats .n { font-weight: 800; color: var(--c); }

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

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 50;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-weight: 600;
  opacity: 0;
  transform: translate(-50%, 10px);
  transition: opacity .2s, transform .2s;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Narrow screens: stage on top, settings below ---------- */

@media (max-width: 900px) {
  .topbar { flex-wrap: wrap; height: auto; padding: 10px 16px; row-gap: 10px; }
  .brand { order: 1; }
  .actions { order: 2; }
  .transport { order: 3; flex: 1 0 100%; justify-content: center; }
  .app { grid-template-columns: 1fr; height: auto; }
  .stage { height: min(100vw, 68vh); min-height: 340px; }
  .panel { overflow: visible; border-left: 0; border-top: 1px solid var(--border); padding: 12px 16px; }
}

@media (max-width: 420px) {
  .brand span { display: none; }
  .pad { width: 76px; height: 62px; }
  .ratio { font-size: 18px; }
}
