:root {
  --bg: #0e0f13;
  --panel: #15171d;
  --raised: #1d2027;
  --line: #2a2e38;
  --line-strong: #3a3f4b;
  --text: #e8eaf0;
  --muted: #9aa1ad;
  --accent: #5b9dff;
  --accent-bg: #1a2a45;
  --danger: #ff6b6b;
  --radius: 8px;
  --top: 52px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden; -webkit-tap-highlight-color: transparent;
}
button, select, input { font: inherit; color: inherit; }
button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 34px; padding: 0 12px; border-radius: var(--radius);
  background: var(--raised); border: 1px solid var(--line-strong); cursor: pointer;
  touch-action: manipulation;
}
button:hover { border-color: #4b5262; }
button:active { transform: scale(0.98); }
button.primary { background: var(--accent-bg); color: var(--accent); border-color: #2f4d7d; }
button.small { min-height: 30px; font-size: 13px; }
button.icon { width: 36px; padding: 0; font-size: 18px; }
button.icon.tiny { width: 24px; min-height: 24px; font-size: 14px; border: none; background: none; }
button i { font-size: 17px; }
select, input[type=text] {
  height: 34px; padding: 0 10px; border-radius: var(--radius);
  background: var(--raised); border: 1px solid var(--line-strong); min-width: 0;
}
input[type=text].bad { border-color: var(--danger); }
input[type=range] { width: 100%; accent-color: var(--accent); margin: 4px 0 8px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }

/* Top bar */
.top {
  height: var(--top); display: flex; align-items: center; gap: 12px;
  padding: 0 14px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 6px; font-weight: 600; }
.brand i { font-size: 22px; color: var(--accent); }
.seg { display: flex; border: 1px solid var(--line-strong); border-radius: var(--radius); overflow: hidden; }
.seg button { border: none; border-radius: 0; background: none; color: var(--muted); min-height: 32px; }
.seg button.on { background: var(--accent-bg); color: var(--accent); }
.top-right { margin-left: auto; display: flex; min-width: 0; }
.top-right select, .top-right input { width: 200px; max-width: 100%; }

main { height: calc(100dvh - var(--top)); }
.view { height: 100%; }

/* Explore */
#explore { display: flex; }
.stage { position: relative; flex: 1; min-width: 0; background: #000; overflow: hidden; }
#view { display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; }
#view:active { cursor: grabbing; }
.hud { position: absolute; bottom: 36px; display: flex; gap: 6px; pointer-events: none; }
.hud > * { pointer-events: auto; }
.hud-left { left: 10px; flex-wrap: wrap; right: 190px; }
.hud-right { right: 10px; }
.hud button { background: rgba(21,23,29,.85); }
.chip {
  display: inline-flex; align-items: center; gap: 5px; height: 30px; padding: 0 10px;
  border-radius: var(--radius); background: rgba(21,23,29,.85); border: 1px solid var(--line);
  font-size: 12px; white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis;
}
.chip.warn { color: #ffc46b; }
.hint {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 7px 12px; font-size: 12px;
  color: var(--muted); background: rgba(14,15,19,.8); border-top: 1px solid var(--line);
}

.panel {
  width: 270px; flex-shrink: 0; overflow-y: auto; padding: 14px;
  border-left: 1px solid var(--line); background: var(--panel);
  display: flex; flex-direction: column; gap: 16px;
}
.field { display: flex; flex-direction: column; gap: 4px; }
.lbl { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: 12px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; padding: 3px 0; cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); margin: 0; }
.actions { display: flex; flex-direction: column; gap: 8px; }
.res-row { display: flex; align-items: center; gap: 6px; color: var(--muted); margin-bottom: 4px; }
.res-row input {
  flex: 1; min-width: 0; height: 34px; padding: 0 8px; border-radius: var(--radius);
  background: var(--raised); border: 1px solid var(--line-strong); font-family: ui-monospace, Menlo, Consolas, monospace;
}
.res-row input.bad { border-color: var(--danger); }
.res-row .unit { font-size: 12px; }
.num-row { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.num-row input[type=range] { flex: 1; margin: 0; }
.num-row input[type=number] {
  width: 64px; height: 30px; padding: 0 6px; border-radius: var(--radius); text-align: right;
  background: var(--raised); border: 1px solid var(--line-strong); font-family: ui-monospace, Menlo, Consolas, monospace;
}
.num-row input.bad { border-color: var(--danger); }
.num-row .unit { width: 12px; font-size: 12px; }
#resPreset { margin-bottom: 4px; }
.res-err { color: var(--danger); font-size: 12px; }
.progress { height: 4px; border-radius: 2px; background: var(--raised); overflow: hidden; margin-top: 4px; }
.progress div { height: 100%; width: 0; background: var(--accent); transition: width .15s; }
.actions .row { display: flex; gap: 8px; }
.actions .row button { flex: 1; }

/* Hex colour picker */
.hex { display: flex; align-items: center; gap: 6px; }
.hex input[type=color] {
  width: 34px; height: 34px; padding: 0; border: 1px solid var(--line-strong);
  border-radius: var(--radius); background: none; cursor: pointer; flex-shrink: 0;
}
.hex input[type=color]::-webkit-color-swatch-wrapper { padding: 3px; }
.hex input[type=color]::-webkit-color-swatch { border: none; border-radius: 4px; }
.hex input[type=color]::-moz-color-swatch { border: none; border-radius: 4px; }
.hex input[type=text] { width: 96px; font-family: ui-monospace, Menlo, Consolas, monospace; text-transform: lowercase; }
.stop-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 6px; }
.stop-list .hex button { width: 30px; min-height: 30px; padding: 0; margin-left: auto; background: none; border-color: transparent; color: var(--muted); }

/* Create */
#create { display: grid; grid-template-columns: 58px minmax(0, 1fr) 290px; }
.tools {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 10px 8px; border-right: 1px solid var(--line); background: var(--panel); overflow-y: auto;
}
.tool { width: 40px; height: 40px; padding: 0; font-size: 20px; background: none; border-color: transparent; color: var(--muted); flex-shrink: 0; }
.tool i { font-size: 20px; }
.tool.on, .tool[aria-pressed=true] { background: var(--accent-bg); color: var(--accent); }
.tool-sep { width: 24px; height: 1px; background: var(--line); margin: 4px 0; flex-shrink: 0; }

.editor { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 14px; min-height: 0; }
.ed-bar { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; align-items: center; }
label.inline { display: flex; align-items: center; gap: 8px; color: var(--muted); }
label.inline select { color: var(--text); }
.ed-area { flex: 1; min-height: 0; width: 100%; display: flex; align-items: center; justify-content: center; }
#edCanvas { touch-action: none; cursor: crosshair; border: 1px solid var(--line-strong); image-rendering: pixelated; }
.ed-colour { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 12px; }
.recent { display: flex; gap: 5px; flex-wrap: wrap; }
.recent button { width: 24px; min-height: 24px; height: 24px; padding: 0; border-radius: 5px; border: 1px solid var(--line-strong); }

.preview { width: 100%; aspect-ratio: 1; border-radius: var(--radius); image-rendering: pixelated; background: #000; }
.saved { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.saved li { display: flex; align-items: center; gap: 8px; padding: 6px; border: 1px solid var(--line); border-radius: var(--radius); }
.saved li.cur { border-color: #2f4d7d; }
.saved canvas { width: 32px; height: 32px; image-rendering: pixelated; border-radius: 4px; background: #000; flex-shrink: 0; }
.saved .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.saved button { width: 30px; min-height: 30px; padding: 0; background: none; border-color: transparent; color: var(--muted); }
.saved .empty { color: var(--muted); font-size: 13px; border: none; padding: 0; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0;
  background: var(--raised); border: 1px solid var(--line-strong); border-radius: var(--radius);
  padding: 8px 14px; font-size: 13px; pointer-events: none; transition: .2s; z-index: 50;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

.mobile-only { display: none !important; }

/* Mobile */
@media (max-width: 760px) {
  .top { gap: 8px; padding: 0 10px; }
  .brand span { display: none; }
  .top-right { flex: 1; }
  .top-right select, .top-right input { width: 100%; }
  .mobile-only { display: flex !important; }
  .hint { display: none; }
  .hud { bottom: max(10px, env(safe-area-inset-bottom)); }
  .hud-left { flex-direction: column; align-items: flex-start; right: 170px; }
  .hud button.icon { width: 42px; min-height: 42px; }

  #explore .panel {
    position: fixed; left: 0; right: 0; bottom: 0; width: auto; max-height: 62dvh; z-index: 20;
    border-left: none; border-top: 1px solid var(--line-strong); border-radius: 16px 16px 0 0;
    padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
    transform: translateY(105%); transition: transform .25s ease;
  }
  #explore .panel.open { transform: none; }
  .sheet-handle { width: 100%; min-height: 22px; height: 22px; padding: 0; border: none; background: none; flex-shrink: 0; margin-bottom: -8px; }
  .sheet-handle::before { content: ''; width: 40px; height: 4px; border-radius: 2px; background: var(--line-strong); }

  #create { display: flex; flex-direction: column; overflow-y: auto; }
  .tools {
    flex-direction: row; justify-content: center; flex-wrap: wrap; padding: 6px;
    border-right: none; border-bottom: 1px solid var(--line); overflow: visible;
  }
  .tool { width: 38px; height: 38px; }
  .tool-sep { width: 1px; height: 22px; margin: 0 2px; }
  .editor { padding: 12px 16px; flex: none; }
  .ed-area { flex: none; }
  .panel { width: auto; border-left: none; border-top: 1px solid var(--line); overflow: visible; padding: 16px; }
  .preview { max-width: 320px; align-self: center; }
}
