:root {
  --bg: #0d0f14;
  --panel: #14171f;
  --panel-2: #1a1e28;
  --line: #262b38;
  --line-2: #323848;
  --text: #e4e7ee;
  --muted: #8b93a7;
  --faint: #5c6378;
  --accent: #7c8cff;
  --accent-2: #a28bff;
  --accent-ink: #0b0d18;
  --ok: #4fd1a5;
  --warn: #f2c14e;
  --err: #ff6b7a;
  --err-bg: rgba(255, 107, 122, 0.12);
  --radius: 10px;
  --mono: "JetBrains Mono", "Cascadia Code", ui-monospace, SFMono-Regular, Consolas, monospace;
  --sans: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.45 var(--sans);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
[hidden] { display: none !important; }

/* ---------- Controls ---------- */
button, select, input[type="number"] {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line-2);
  border-radius: 7px;
  height: 30px;
  padding: 0 10px;
  outline: none;
}
button { cursor: pointer; white-space: nowrap; }
button:hover:not(:disabled), select:hover:not(:disabled) { border-color: #444b60; background: #20252f; }
button:focus-visible, select:focus-visible, input:focus-visible { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(124, 140, 255, 0.25); }
button:disabled, select:disabled, input:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.primary:hover:not(:disabled) { background: #8f9cff; border-color: #8f9cff; }
button.danger { color: var(--err); border-color: rgba(255, 107, 122, 0.45); }
button.big { height: 36px; padding: 0 18px; font-size: 14px; }
button.icon { width: 32px; padding: 0; display: grid; place-items: center; }
button.icon svg { width: 16px; height: 16px; fill: currentColor; }
kbd {
  font: 10px var(--mono);
  padding: 1px 4px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.18);
  margin-left: 4px;
}
select { padding-right: 6px; }
input[type="number"] { width: 100%; }
.check { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); cursor: pointer; user-select: none; }
.check input { accent-color: var(--accent); margin: 0; }
.check.small { font-size: 12px; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 9px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 15px; letter-spacing: 0.01em; }
.logo {
  width: 22px; height: 22px; border-radius: 6px;
  background: conic-gradient(from 210deg, #4fd1a5, #7c8cff, #ff7ab6, #f2c14e, #4fd1a5);
  box-shadow: 0 0 18px rgba(124, 140, 255, 0.35);
}
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.sep { width: 1px; height: 20px; background: var(--line-2); margin: 0 4px; }

.banner {
  padding: 8px 16px;
  background: rgba(242, 193, 78, 0.12);
  color: var(--warn);
  border-bottom: 1px solid rgba(242, 193, 78, 0.3);
}

/* ---------- Layout ---------- */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  /* --editor-w is set in px by dragging the splitter */
  grid-template-columns: var(--editor-w, minmax(320px, 1fr)) 7px minmax(360px, 1.1fr);
}
.pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; }

.splitter {
  position: relative;
  cursor: col-resize;
  background: var(--panel);
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
  touch-action: none;
  outline: none;
}
.splitter::after {
  content: "";
  position: absolute; top: 50%; left: 50%;
  width: 3px; height: 36px; margin: -18px 0 0 -1.5px;
  border-radius: 2px;
  background: var(--line-2);
}
.splitter:hover, .splitter.dragging, .splitter:focus-visible { background: rgba(124, 140, 255, 0.18); }
.splitter:hover::after, .splitter.dragging::after, .splitter:focus-visible::after { background: var(--accent); }
body.resizing { cursor: col-resize; user-select: none; }
body.resizing iframe, body.resizing canvas { pointer-events: none; }
.side-pane { overflow-y: auto; padding: 14px; gap: 12px; }

.pane-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 7px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  color: var(--muted);
}
.file-name { font-family: var(--mono); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-name.modified::after { content: " •"; color: var(--accent); }
.badge {
  font-size: 11px; padding: 2px 8px; border-radius: 99px;
  background: rgba(124, 140, 255, 0.14); color: #b7c0ff; border: 1px solid rgba(124, 140, 255, 0.3);
}

/* ---------- Pass tabs ---------- */
.tab-bar {
  display: flex; align-items: flex-end; gap: 6px;
  padding: 6px 10px 0;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.tabs {
  display: flex; align-items: flex-end; gap: 2px;
  overflow-x: auto; overflow-y: hidden; min-width: 0;
  padding-top: 2px; margin-bottom: -1px; /* lets the selected tab sit over the bar's border */
  scrollbar-width: thin;
}
.tab {
  position: relative;
  display: inline-flex; align-items: center; gap: 6px;
  height: 30px; padding: 0 10px;
  border: 1px solid transparent; border-bottom: none;
  border-radius: 7px 7px 0 0;
  background: transparent;
  color: var(--muted);
  font: 12px var(--sans);
  cursor: pointer;
  white-space: nowrap;
}
.tab:hover { color: var(--text); background: var(--panel-2); border-color: transparent; }
.tab[aria-selected="true"] {
  color: var(--text);
  background: var(--bg);
  border-color: var(--line);
  height: 31px;
}
.tab[aria-selected="true"]::before {
  content: ""; position: absolute; left: 8px; right: 8px; top: -1px; height: 2px;
  border-radius: 2px; background: var(--accent);
}
.tab .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--err); }
.tab .kind { font: 10px var(--mono); color: var(--faint); }
.tab .close {
  display: inline-grid; place-items: center;
  width: 16px; height: 16px; border-radius: 4px;
  color: var(--faint); font-size: 11px; line-height: 1;
}
.tab .close:hover { background: var(--line-2); color: var(--text); }
.add-tab { height: 26px; margin-bottom: 4px; font-size: 12px; padding: 0 6px; flex: none; }
.log .tabtag {
  display: inline-block; font: 11px var(--sans); color: var(--muted);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 4px;
  padding: 0 5px; margin-right: 4px;
}
.thumb .buf {
  font: 600 22px var(--mono); color: #b7c0ff;
}
.thumb .buf small { display: block; font: 10px var(--sans); color: var(--muted); text-align: center; }

/* ---------- Editor ---------- */
.editor { flex: 1; min-height: 0; position: relative; }
.editor .CodeMirror {
  position: absolute; inset: 0; height: auto;
  background: var(--bg); color: #d6dbe7;
  font: 13px/1.55 var(--mono);
}
.CodeMirror-gutters { background: var(--bg); border-right: 1px solid var(--line); }
.CodeMirror-linenumber { color: var(--faint); padding-right: 10px; }
.CodeMirror-cursor { border-left: 2px solid var(--accent); }
.CodeMirror-selected, .CodeMirror-focused .CodeMirror-selected { background: rgba(124, 140, 255, 0.22) !important; }
.CodeMirror-matchingbracket { color: var(--ok) !important; text-decoration: underline; }
.cm-s-default .cm-keyword { color: #c792ea; }
.cm-s-default .cm-type, .cm-s-default .cm-variable-3 { color: #7fd6ff; }
.cm-s-default .cm-builtin { color: #82aaff; }
.cm-s-default .cm-number { color: #f78c6c; }
.cm-s-default .cm-comment { color: #5f6884; font-style: italic; }
.cm-s-default .cm-string { color: #c3e88d; }
.cm-s-default .cm-meta { color: #ffcb6b; }
.cm-s-default .cm-def { color: #82aaff; }
.cm-s-default .cm-variable { color: #d6dbe7; }
.cm-s-default .cm-operator { color: #89ddff; }
.cm-error-line { background: var(--err-bg); }
.cm-error-gutter { color: var(--err) !important; font-weight: 700; }

.log {
  max-height: 32%;
  overflow: auto;
  border-top: 1px solid var(--line);
  background: var(--panel);
  font: 12px/1.5 var(--mono);
  padding: 7px 14px;
  color: var(--ok);
}
.log.has-errors { color: var(--text); }
.log .err { display: block; color: var(--err); cursor: pointer; padding: 1px 0; }
.log .err:hover { text-decoration: underline; }
.log .warn { color: var(--warn); }
.log .ln { color: var(--muted); }

/* ---------- Preview ---------- */
.preview-wrap { display: flex; justify-content: center; }
.preview-box {
  --ar: 1.7778;
  position: relative;
  width: min(100%, calc(56vh * var(--ar)));
  aspect-ratio: var(--ar);
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--line);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
}
.preview-box canvas { display: block; width: 100%; height: 100%; cursor: crosshair; touch-action: none; }
.hud {
  position: absolute; top: 8px; left: 8px;
  font-size: 11px; color: rgba(255, 255, 255, 0.85);
  background: rgba(0, 0, 0, 0.5); padding: 2px 7px; border-radius: 5px;
  pointer-events: none;
}
.overlay {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  background: rgba(8, 10, 15, 0.72);
  color: var(--muted); font-size: 13px;
  backdrop-filter: blur(2px);
}

.transport { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.transport input[type="range"] { flex: 1; min-width: 120px; accent-color: var(--accent); }
.time-label { min-width: 110px; color: var(--muted); font-size: 12px; text-align: right; }

/* ---------- Cards ---------- */
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px 14px;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.card h2 { margin: 0; font-size: 13px; font-weight: 650; letter-spacing: 0.02em; }
.hint { color: var(--faint); font-size: 12px; }

.grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.grid label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12px; min-width: 0; }
.grid select { width: 100%; }

#toDiskRow { margin-top: 12px; }
.summary { margin-top: 10px; color: var(--muted); font-size: 12px; }
.summary b { color: var(--text); font-weight: 600; }
.summary .warn { color: var(--warn); }
.actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; align-items: center; }

.progress { margin-top: 12px; }
.bar { height: 8px; border-radius: 99px; background: var(--panel-2); overflow: hidden; border: 1px solid var(--line); }
#progressFill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width 0.12s linear;
}
#progressText { margin-top: 6px; font-size: 12px; color: var(--muted); }
.msg { margin-top: 8px; font-size: 12px; min-height: 1em; }
.msg.ok { color: var(--ok); }
.msg.err { color: var(--err); }

/* ---------- Channels ---------- */
.channels { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.channel { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.thumb {
  position: relative;
  aspect-ratio: 16 / 9;
  max-height: 96px;
  border-radius: 8px;
  border: 1px dashed var(--line-2);
  background: var(--panel-2) center / cover no-repeat;
  display: grid; place-items: center;
  color: var(--faint); font: 11px var(--mono);
  cursor: pointer;
  overflow: hidden;
}
.thumb:hover, .thumb.drag { border-color: var(--accent); color: var(--muted); }
.thumb.filled { border-style: solid; }
.thumb .label {
  position: absolute; left: 5px; bottom: 4px;
  background: rgba(0, 0, 0, 0.6); color: #fff; padding: 0 5px; border-radius: 4px;
}
.thumb .clear {
  position: absolute; top: 4px; right: 4px;
  width: 20px; height: 20px; padding: 0; border-radius: 5px;
  background: rgba(0, 0, 0, 0.6); border: none; color: #fff; font-size: 12px; line-height: 1;
}
.channel select { height: 26px; font-size: 12px; padding: 0 4px; width: 100%; }
.ch-opts { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }
.ch-opts .check { grid-column: 1 / -1; font-size: 11px; }

/* ---------- Shader parameters ---------- */
.card-tools { display: inline-flex; align-items: center; gap: 8px; }
.small-btn { height: 24px; padding: 0 8px; font-size: 12px; }
.params { display: flex; flex-direction: column; gap: 8px; }
.params:empty { display: none; }
.params-empty { color: var(--muted); font-size: 12px; line-height: 1.6; }
.params-empty code, .builtins code {
  font: 11.5px var(--mono); color: #c9d0ff;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 4px; padding: 0 4px;
}
.param {
  display: grid;
  grid-template-columns: minmax(90px, 150px) minmax(0, 1fr);
  align-items: center;
  gap: 4px 12px;
}
.param-name { display: flex; flex-direction: column; min-width: 0; }
.param-name b { font: 600 12px var(--mono); color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.param-name span { font: 11px var(--mono); color: var(--faint); }
.param-controls { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.param-row { display: flex; align-items: center; gap: 8px; min-width: 0; }
.param-row .axis { width: 10px; font: 11px var(--mono); color: var(--faint); }
.param-row input[type="range"] { flex: 1; min-width: 60px; accent-color: var(--accent); }
.param-row input[type="number"] { width: 78px; height: 26px; padding: 0 6px; font: 12px var(--mono); }
.param-row input[type="color"] {
  width: 44px; height: 26px; padding: 2px; border: 1px solid var(--line-2); border-radius: 6px; background: var(--panel-2); cursor: pointer;
}
.param-row .hex { font: 12px var(--mono); color: var(--muted); }
.param-row input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; }
.param-note { color: var(--faint); font-size: 12px; }

.builtins { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 8px; }
.params:empty + .builtins { margin-top: 0; border-top: none; padding-top: 0; }
.builtins summary { cursor: pointer; color: var(--muted); font-size: 12px; user-select: none; }
.builtin-list {
  display: grid;
  grid-template-columns: max-content max-content minmax(0, 1fr);
  gap: 3px 14px;
  margin-top: 8px;
  font-size: 12px;
}
.builtin-list .t { color: var(--faint); }
.builtin-list .n { color: #b7c0ff; }
.builtin-list .v { color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.builtin-list .none { grid-column: 1 / -1; color: var(--faint); font-family: var(--sans); }

/* ---------- Image -> Shader dialog ---------- */
.modal {
  position: fixed; inset: 0; z-index: 40;
  display: grid; place-items: center;
  padding: 16px;
  background: rgba(6, 8, 12, 0.72);
  backdrop-filter: blur(3px);
}
.modal-card {
  width: min(920px, 100%);
  max-height: calc(100vh - 32px);
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--line-2);
  border-radius: 14px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.6);
  padding: 16px 18px 18px;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.modal-head h2 { margin: 0; font-size: 15px; font-weight: 650; }
.muted { color: var(--faint); }
.i2s-body { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr); gap: 16px; }
.i2s-drop {
  position: relative;
  aspect-ratio: 16 / 10;
  border: 1.5px dashed var(--line-2);
  border-radius: 10px;
  background: var(--panel-2);
  display: grid; place-items: center;
  cursor: pointer;
  overflow: hidden;
  outline: none;
}
.i2s-drop:hover, .i2s-drop.drag, .i2s-drop:focus-visible { border-color: var(--accent); }
.i2s-drop img { width: 100%; height: 100%; object-fit: contain; background: #000; }
.i2s-drop-hint { text-align: center; color: var(--muted); line-height: 1.7; padding: 12px; }
.i2s-drop-hint b { color: var(--text); }
.i2s-right { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.i2s-analysis { display: flex; flex-direction: column; gap: 10px; font-size: 12px; min-height: 120px; }
.i2s-row { display: grid; grid-template-columns: 58px minmax(0, 1fr); align-items: center; gap: 10px; }
.i2s-row .k { color: var(--muted); }
.i2s-analysis .palette { display: flex; gap: 4px; flex-wrap: wrap; }
.i2s-analysis .sw {
  position: relative; width: 44px; height: 30px; border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.12);
}
.i2s-analysis .sw i {
  position: absolute; right: 3px; bottom: 1px; font: 9px var(--mono); font-style: normal;
  color: #fff; text-shadow: 0 0 3px #000, 0 0 2px #000;
}
.i2s-analysis .layout { display: grid; gap: 2px; max-width: 220px; }
.i2s-analysis .layout span { aspect-ratio: 1.4; border-radius: 3px; }
.i2s-chips { display: flex; flex-wrap: wrap; gap: 5px; }
.i2s-chips span {
  padding: 2px 8px; border-radius: 99px; font-size: 11px;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
}
.i2s-notes-label { font-size: 12px; color: var(--muted); }
#i2sNotes {
  width: 100%; resize: vertical; min-height: 44px;
  font: 12px var(--sans); color: var(--text);
  background: var(--panel-2); border: 1px solid var(--line-2); border-radius: 7px; padding: 6px 8px;
  outline: none;
}
#i2sNotes:focus-visible { border-color: var(--accent); }
.i2s-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 14px; }
.i2s-status { margin-top: 12px; font-size: 12px; line-height: 1.55; min-height: 1.5em; color: var(--muted); overflow-wrap: anywhere; }
.i2s-status.ok { color: var(--ok); }
.i2s-status.err { color: var(--err); }
.i2s-status.busy { color: var(--text); }
.i2s-status code { font: 11px var(--mono); white-space: pre-wrap; color: var(--text); }
.i2s-claude-info {
  margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line);
  font-size: 11.5px; color: var(--faint); line-height: 1.6;
}
.i2s-claude-info code, .i2s-claude-info b { color: var(--muted); }
.i2s-claude-info code { font: 11px var(--mono); }
.spinner {
  display: inline-block; width: 11px; height: 11px; margin-right: 7px; vertical-align: -1px;
  border: 2px solid var(--line-2); border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (max-width: 720px) { .i2s-body { grid-template-columns: 1fr; } }

/* ---------- Drop hint ---------- */
.drop-hint {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  background: rgba(13, 15, 20, 0.8);
  pointer-events: none;
}
.drop-hint div {
  padding: 28px 40px; border: 2px dashed var(--accent); border-radius: 14px;
  font-size: 16px; color: var(--text);
}

/* ---------- Responsive ---------- */
@media (max-width: 980px) {
  body { overflow: auto; height: auto; }
  .layout { grid-template-columns: 1fr; }
  .splitter { display: none; }
  .editor-pane { border-bottom: 1px solid var(--line); height: 60vh; }
  .side-pane { overflow: visible; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .channels { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 520px) {
  .topbar, .side-pane { padding-left: 12px; padding-right: 12px; }
  .grid { grid-template-columns: 1fr 1fr; }
}

/* Claude-only controls are hidden unless the local server offers Claude (see image2shader.js) */
body:not(.ai-available) .ai-only { display: none !important; }

/* Image -> Shader: style matches */
.i2s-matches { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 10px; }
.i2s-matches-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 12px; color: var(--muted); margin-bottom: 8px; flex-wrap: wrap; }
.i2s-match-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 8px; }
.i2s-match {
  display: flex; flex-direction: column; align-items: stretch; gap: 6px;
  height: auto; padding: 8px 10px; text-align: left; white-space: normal;
}
.i2s-match .name { font-weight: 600; font-size: 12.5px; }
.i2s-match .meter { height: 4px; border-radius: 99px; background: var(--line); overflow: hidden; }
.i2s-match .meter i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); }
.i2s-match .pct { font: 11px var(--mono); color: var(--faint); }

/* Import dialog */
.imp-help { margin: 0 0 8px; font-size: 12.5px; color: var(--muted); line-height: 1.6; }
.imp-help code, .imp-how code { font: 11.5px var(--mono); color: #c9d0ff; }
.imp-how { font-size: 12px; color: var(--muted); margin-bottom: 10px; }
.imp-how summary { cursor: pointer; color: var(--text); }
.imp-how ol { margin: 8px 0; padding-left: 20px; line-height: 1.7; }
.imp-how p { margin: 4px 0 0; }
.imp-text {
  width: 100%; resize: vertical; min-height: 160px;
  font: 12px/1.5 var(--mono); color: var(--text);
  background: var(--bg); border: 1px solid var(--line-2); border-radius: 8px; padding: 8px 10px; outline: none;
}
.imp-text:focus-visible { border-color: var(--accent); }
