:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --border: #dfe2e7;
  --text: #1d2228;
  --muted: #646d78;
  --accent: #1494cd;
  --accent-text: #ffffff;
  --focus: #1494cd66;
  --stage-light: #ffffff;
  --stage-dark: #0d0f11;
  --shadow: 0 10px 30px #0000000d;
  color-scheme: light;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121417; --surface: #1a1d21; --surface-2: #20242a; --border: #2e333a;
    --text: #eef0f3; --muted: #9aa3ad; --accent: #3aaee2; --focus: #3aaee266;
    --shadow: 0 10px 30px #00000040; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121417; --surface: #1a1d21; --surface-2: #20242a; --border: #2e333a;
  --text: #eef0f3; --muted: #9aa3ad; --accent: #3aaee2; --focus: #3aaee266;
  --shadow: 0 10px 30px #00000040; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45; min-height: 100vh; }
h1 { font-size: 1.15rem; margin: 0; }
h2 { font-size: .95rem; margin: 0 0 12px; }
code { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: .92em; }
.muted { color: var(--muted); margin: 0; }
.small { font-size: 12.5px; }
.mono { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-variant-numeric: tabular-nums; }

button, select, input, textarea { font: inherit; color: inherit; }
button, select, .file-btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px;
  padding: 6px 11px; cursor: pointer;
}
button:hover, .file-btn:hover { border-color: var(--muted); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 1px; }
button.ghost, .file-btn.ghost { background: transparent; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.primary small { color: #ffffffcc; }
button:disabled { opacity: .5; cursor: progress; }
.file-btn { position: relative; overflow: hidden; display: inline-block; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

/* ---------- top bar */
.topbar {
  display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: center; justify-content: space-between;
  padding: 14px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; gap: 12px; align-items: center; }
.brand-mark { width: 34px; height: 34px; flex: none; }
.preset-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

/* ---------- layout */
.layout { display: grid; grid-template-columns: 360px minmax(0, 1fr); gap: 20px; padding: 20px; max-width: 1400px; margin: 0 auto; }
.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
  max-height: calc(100vh - 120px); overflow: auto; position: sticky; top: 20px;
}
.workspace { display: grid; gap: 20px; align-content: start; min-width: 0; }

/* ---------- control groups */
.group { border-bottom: 1px solid var(--border); }
.group:last-child { border-bottom: 0; }
.group > summary {
  list-style: none; cursor: pointer; padding: 12px 16px; font-weight: 600; display: flex; justify-content: space-between; align-items: center;
}
.group > summary::-webkit-details-marker { display: none; }
.group > summary::after { content: "▾"; color: var(--muted); transition: transform .15s; }
.group:not([open]) > summary::after { transform: rotate(-90deg); }
.group-body { padding: 0 16px 14px; display: grid; gap: 10px; }
.group-note { font-size: 12px; color: var(--muted); margin: -2px 0 2px; }

.ctl { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; align-items: center; }
.ctl > label { font-size: 13px; }
.ctl.is-disabled { opacity: .45; }
.ctl input[type="range"] { grid-column: 1 / -1; width: 100%; accent-color: var(--accent); margin: 0; }
.ctl input[type="number"] {
  width: 74px; padding: 3px 6px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface-2); text-align: right;
}
.ctl input[type="text"] { width: 150px; padding: 4px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface-2); }
.ctl select { padding: 4px 8px; }
.colour { display: flex; gap: 6px; align-items: center; }
.colour input[type="color"] { width: 32px; height: 26px; padding: 0; border: 1px solid var(--border); border-radius: 6px; background: none; cursor: pointer; }
.colour input[type="text"] { width: 84px; font-family: ui-monospace, Consolas, monospace; text-transform: uppercase; }

.switch { position: relative; width: 36px; height: 20px; flex: none; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--border); transition: background .15s; }
.switch span::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px #0003; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(16px); }
.switch input:focus-visible + span { outline: 3px solid var(--focus); }

/* ---------- stage */
.stage-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); padding: 16px; display: grid; gap: 12px; }
.stage {
  border-radius: 10px; display: grid; place-items: center; min-height: 420px; padding: 24px;
  background: var(--stage-light); transition: background .2s; border: 1px solid var(--border);
}
.stage[data-bg="dark"] { background: var(--stage-dark); }
.stage[data-bg="checker"] {
  background-color: #fff;
  background-image: linear-gradient(45deg, #e9ebee 25%, transparent 25%), linear-gradient(-45deg, #e9ebee 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #e9ebee 75%), linear-gradient(-45deg, transparent 75%, #e9ebee 75%);
  background-size: 20px 20px; background-position: 0 0, 0 10px, 10px -10px, -10px 0;
}
.stage-inner { width: min(100%, 360px); aspect-ratio: 1; }
.stage-inner > svg, .stage-inner > img { display: block; width: 100%; height: 100%; }
.stage-tools { display: flex; flex-wrap: wrap; gap: 10px; justify-content: space-between; align-items: center; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; padding: 5px 10px; background: transparent; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }
.inline { display: inline-flex; gap: 8px; align-items: center; }

.transport { display: grid; grid-template-columns: auto 1fr auto auto; gap: 10px; align-items: center; }
.transport input[type="range"] { width: 100%; accent-color: var(--accent); }
.transport.is-locked input[type="range"], .transport.is-locked .icon-btn { opacity: .45; pointer-events: none; }
.icon-btn { width: 36px; height: 32px; padding: 0; font-size: 12px; }
.phase-bar { display: flex; height: 18px; border-radius: 6px; overflow: hidden; font-size: 10.5px; color: #fff; }
.phase-bar span { display: grid; place-items: center; white-space: nowrap; overflow: hidden; min-width: 0; }
.phase-bar .p-pause { background: #9aa3ad; }
.phase-bar .p-fill { background: #1494cd; }
.phase-bar .p-hold { background: #aec531; }
.phase-bar .p-drain { background: #f27121; }

/* ---------- exports */
.exports { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); padding: 16px; display: grid; gap: 12px; }
.export-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.export-grid button { display: grid; gap: 2px; text-align: left; padding: 10px 12px; font-weight: 600; }
.export-grid small { font-weight: 400; font-size: 12px; color: var(--muted); }
.progress { display: flex; align-items: center; gap: 10px; }
.progress[hidden] { display: none; }
.progress-bar { flex: 1; height: 8px; border-radius: 99px; background: var(--surface-2); border: 1px solid var(--border); overflow: hidden; }
.progress-bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .1s; }
.embed-box { display: grid; gap: 8px; border-top: 1px solid var(--border); padding-top: 12px; }
.embed-box h3 { font-size: .9rem; margin: 0; }
.embed-box input {
  width: 100%; padding: 7px 9px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2);
  font: 12px ui-monospace, Consolas, monospace;
}
.embed-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.embed-actions a { color: var(--accent); }
.code summary { cursor: pointer; color: var(--muted); }
.code textarea {
  width: 100%; margin-top: 8px; resize: vertical; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); padding: 10px; font: 12px/1.45 ui-monospace, Consolas, monospace;
}

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 9px 14px; border-radius: 8px; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- responsive */
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 16px; }
  .panel { position: static; max-height: none; order: 2; }
  .stage { min-height: 300px; }
}
@media (max-width: 520px) {
  .topbar { padding: 12px 16px; }
  .transport { grid-template-columns: auto 1fr auto; }
  .transport select { grid-column: 1 / -1; justify-self: end; }
}
