:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --border: #dde1e7;
  --text: #16181d;
  --muted: #5d6573;
  --accent: #5b5bd6;
  --accent-text: #ffffff;
  --ok: #2e9d5b;
  --warn: #c98a12;
  --bad: #d2463c;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1f232b;
    --border: #2a2f39;
    --text: #e8eaee;
    --muted: #949cab;
    --accent: #7c7cf0;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --surface: #171a20; --surface-2: #1f232b; --border: #2a2f39;
  --text: #e8eaee; --muted: #949cab; --accent: #7c7cf0; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--bad); min-height: 1.2em; margin: 8px 0 0; font-size: 14px; }
h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}

/* buttons & inputs */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 10px; padding: 9px 14px; font: inherit; font-weight: 500; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px;
}
.btn:hover { filter: brightness(1.05); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 32px; padding: 5px 10px; font-size: 13px; }
.btn.wide { width: 100%; }
input[type=text], input[type=password], input[type=number], select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 10px; padding: 9px 11px;
}
textarea { resize: vertical; min-height: 96px; }
input:focus, select:focus, textarea:focus, .btn:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
input[type=range] { width: 100%; accent-color: var(--accent); }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; display: grid; gap: 12px; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; letter-spacing: .2px; }
.status { display: flex; align-items: center; gap: 6px; margin-left: auto; flex-wrap: wrap; }
.st-label { font-size: 13px; color: var(--muted); margin-right: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.ok { background: var(--ok); }
.dot.bad { background: var(--bad); }

/* tabs */
.tabs {
  display: flex; gap: 6px; overflow-x: auto; padding: 10px 16px; scrollbar-width: none;
  border-bottom: 1px solid var(--border); background: var(--surface);
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: none; border: 1px solid var(--border); background: transparent; color: var(--text);
  border-radius: 999px; padding: 6px 13px; font: inherit; font-size: 14px; cursor: pointer; white-space: nowrap;
}
.tab[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

/* layout */
.layout {
  display: grid; gap: 16px; padding: 16px; max-width: 1400px; margin: 0 auto;
  grid-template-columns: minmax(320px, 440px) 1fr; align-items: start;
}
@media (max-width: 860px) { .layout { grid-template-columns: 1fr; } }
#create .card { position: sticky; top: 76px; }
@media (max-width: 860px) { #create .card { position: static; } }

.feature-head { margin-bottom: 14px; }
.feature-head p { margin: 4px 0 0; font-size: 14px; }
.field { margin-bottom: 14px; }
.field > label, .field .label { display: flex; justify-content: space-between; align-items: center; font-weight: 500; font-size: 14px; margin-bottom: 6px; }
.field .hint { margin: 5px 0 0; font-size: 13px; color: var(--muted); }
.prompt-actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.range-row { display: flex; gap: 10px; align-items: center; }
.range-row output { min-width: 3.5em; text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 14px; }
.check { display: flex; gap: 8px; align-items: center; font-size: 14px; }
.advanced { margin: 4px 0 16px; border-top: 1px solid var(--border); padding-top: 10px; }
.advanced summary { cursor: pointer; color: var(--muted); font-size: 14px; margin-bottom: 10px; }

/* media drop zone */
.drop {
  border: 1.5px dashed var(--border); border-radius: 10px; background: var(--surface-2);
  min-height: 120px; display: grid; place-items: center; text-align: center; cursor: pointer;
  position: relative; overflow: hidden; color: var(--muted); font-size: 14px; padding: 10px;
}
.drop.drag { border-color: var(--accent); }
.drop img, .drop video { max-width: 100%; max-height: 260px; border-radius: 6px; display: block; }
.drop .clear { position: absolute; top: 6px; right: 6px; }
.drop input { display: none; }
.drop.busy::after {
  content: "Uploading…"; position: absolute; inset: 0; display: grid; place-items: center;
  background: color-mix(in srgb, var(--surface) 75%, transparent); color: var(--text);
}

/* results */
.results-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; gap: 10px; }
.jobs { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.job { padding: 0; overflow: hidden; display: flex; flex-direction: column; }
.job .media {
  background: var(--surface-2); aspect-ratio: 1 / 1; display: grid; place-items: center; cursor: zoom-in; position: relative;
}
.job .media img, .job .media video { width: 100%; height: 100%; object-fit: contain; display: block; }
.job .media .count { position: absolute; bottom: 8px; right: 8px; background: rgba(0,0,0,.6); color: #fff; font-size: 12px; padding: 2px 7px; border-radius: 999px; }
.job .placeholder { color: var(--muted); font-size: 14px; text-align: center; padding: 16px; }
.job .meta { padding: 10px 12px; display: grid; gap: 4px; }
.job .meta .title { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; font-weight: 500; font-size: 14px; }
.job .meta .title > span:first-child { min-width: 0; }
.job .meta .title .badge { flex: none; }
.job .meta .prompt { font-size: 13px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.job .actions { display: flex; gap: 6px; padding: 0 12px 12px; flex-wrap: wrap; }
.badge { font-size: 12px; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.badge.running, .badge.queued { color: var(--warn); border-color: var(--warn); }
.badge.done { color: var(--ok); border-color: var(--ok); }
.badge.error, .badge.cancelled { color: var(--bad); border-color: var(--bad); }
.bar { height: 4px; background: var(--surface-2); }
.bar > div { height: 100%; background: var(--accent); width: 0; transition: width .3s; }
.job .err { color: var(--bad); font-size: 13px; }
.empty { color: var(--muted); padding: 40px 0; text-align: center; grid-column: 1 / -1; }

/* live workflow view */
.pipeline-wrap { margin: 0 12px 10px; border-top: 1px solid var(--border); padding-top: 8px; font-size: 13px; }
.pipeline-wrap > summary { cursor: pointer; color: var(--muted); }
.pipeline-head { font-weight: 500; margin-bottom: 6px; }
.pipeline { list-style: none; margin: 6px 0 0; padding: 0; display: grid; gap: 1px; }
.stage { display: grid; grid-template-columns: 18px 1fr auto; align-items: center; gap: 6px; padding: 3px 0; position: relative; }
.stage .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stage .time, .node .time { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.dot {
  width: 16px; height: 16px; border-radius: 50%; display: grid; place-items: center; font-size: 10px; line-height: 1;
  border: 1.5px solid var(--border); color: var(--muted); box-sizing: border-box;
}
/* connector line between stage dots */
.stage:not(:last-child)::after {
  content: ""; position: absolute; left: 7.25px; top: calc(50% + 9px); height: calc(100% - 16px); width: 1.5px; background: var(--border);
}
.stage.pending { color: var(--muted); }
.stage.done .dot, .node.done .dot { background: var(--ok); border-color: var(--ok); color: #fff; }
.stage.cached .dot, .node.cached .dot { border-color: var(--ok); color: var(--ok); }
.stage.skipped { color: var(--muted); }
.stage.error .dot { background: var(--bad); border-color: var(--bad); color: #fff; }
.stage.running { font-weight: 600; }
.stage.running .dot, .node.running .dot { border-color: var(--accent); border-top-color: transparent; animation: spin 0.9s linear infinite; }
.stage.running .time { color: var(--accent); }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .stage.running .dot, .node.running .dot { animation: none; border-top-color: var(--accent); } }

.nodes { margin-top: 6px; }
.nodes > summary { cursor: pointer; color: var(--muted); }
.node-group { margin-top: 6px; }
.node-group-title { font-weight: 500; margin-bottom: 2px; }
.node { display: grid; grid-template-columns: 16px auto 1fr auto; align-items: center; gap: 6px; padding: 1px 0 1px 4px; }
.node .dot { width: 12px; height: 12px; font-size: 8px; }
.node code { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node.pending, .node.skipped { color: var(--muted); }

/* viewer */
.viewer {
  border: none; padding: 0; background: var(--surface); color: var(--text); border-radius: var(--radius);
  max-width: min(96vw, 1200px); max-height: 94vh; width: auto;
}
.viewer::backdrop { background: rgba(0,0,0,.75); }
#viewer-body { display: grid; gap: 8px; padding: 12px; max-height: 80vh; overflow: auto; }
#viewer-body img, #viewer-body video { max-width: 100%; max-height: 74vh; display: block; margin: 0 auto; border-radius: 6px; }
.viewer .close { position: absolute; top: 8px; right: 8px; z-index: 2; }
.viewer-actions { display: flex; gap: 8px; flex-wrap: wrap; padding: 0 12px 12px; }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: all .2s; max-width: calc(100vw - 32px); z-index: 20;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 520px) {
  .st-label, #st-gpu { display: none; }
  .row2 { grid-template-columns: 1fr; }
  .jobs { grid-template-columns: 1fr 1fr; gap: 8px; }
  /* Running jobs take the full width on phones so the live workflow is readable. */
  .job.running, .job.queued, .job:has(details.pipeline-wrap[open]) { grid-column: 1 / -1; }
  .job.running .media, .job.queued .media { aspect-ratio: 3 / 1; }
  .job .actions { display: none; }
  .job .meta .prompt { display: none; }
}
