:root {
  --bg: #171b20;
  --panel: #21262d;
  --inset: #1a1f25;
  --side: #262c34;
  --side-active: #1e2329;
  --border: #2f363e;
  --text: #e3e7eb;
  --muted: #95a0ab;
  --dim: #5f6973;
  --green: #3bb54a;
  --yellow: #e8d52a;
  --red: #ec3a55;
  --blue: #5aa0e8;
  --grey: #5f6973;
  --ink: #111418;
  --mono: 'Geist Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans: 'Geist', ui-sans-serif, system-ui, -apple-system, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 13px/1.45 var(--sans); }
a { color: var(--blue); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
.mono { font-family: var(--mono); }
.dim { color: var(--dim); }
.muted { color: var(--muted); }
.warn { color: var(--yellow); }
.err { color: var(--red); }
.ok { color: var(--green); }
[hidden] { display: none !important; }

/* ---------- shell ---------- */
.app { display: grid; grid-template-columns: 220px 1fr; height: 100vh; }
.sidebar { background: var(--side); display: flex; flex-direction: column; min-height: 0; }
.brand { display: flex; align-items: center; gap: 8px; padding: 22px 18px; font-size: 15px; font-weight: 500; }
.brand-mark { width: 20px; height: 20px; border-radius: 3px; background: var(--yellow); }
.sidebar nav { display: flex; flex-direction: column; }
.sidebar nav a { padding: 11px 18px; color: var(--muted); }
.sidebar nav a:hover { color: var(--text); }
.sidebar nav a.active { background: var(--side-active); color: var(--text); }
.sidebar-foot { margin-top: auto; padding: 16px 18px; display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); }
.dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 8px; background: var(--dim); }
.dot.on { background: var(--green); }
.dot.off { background: var(--red); }
main { overflow: auto; padding: 18px; min-width: 0; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 4px 4px 14px; }
.page-head h1 { margin: 0; font-size: 28px; font-weight: 300; }

/* ---------- panels ---------- */
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 3px; display: flex; flex-direction: column; min-width: 0; }
.panel-title { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 14px; color: var(--muted); font-size: 14px; font-weight: 300; }
.panel-body { padding: 0 14px 14px; display: flex; flex-direction: column; gap: 10px; min-width: 0; }

/* ---------- controls ---------- */
.btn { border: 1px solid var(--border); background: var(--side); border-radius: 3px; padding: 6px 12px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--blue); border-color: var(--blue); color: #0f1418; font-weight: 500; padding: 9px 18px; }
.btn.primary:disabled { background: var(--side); border-color: var(--border); color: var(--dim); cursor: not-allowed; }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn.ghost { background: none; }
.link { background: none; border: none; padding: 0; color: var(--blue); cursor: pointer; font-family: var(--mono); font-size: 12px; }
.link:hover { text-decoration: underline; }
.link.danger { color: var(--red); }
.input, .select, .textarea { background: var(--inset); border: 1px solid var(--border); border-radius: 3px; padding: 7px 10px; outline: none; min-width: 0; }
.input:focus, .select:focus, .textarea:focus { border-color: var(--blue); }
.textarea { resize: vertical; width: 100%; min-height: 64px; }
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > label { font-size: 12px; color: var(--muted); display: flex; gap: 8px; align-items: baseline; }
.field > label .mono { font-size: 11px; color: var(--dim); }
.chip { display: inline-flex; align-items: center; gap: 8px; padding: 5px 10px; border-radius: 3px; background: var(--side); font-size: 12px; cursor: pointer; border: 1px solid transparent; }
.chip.active { border-color: var(--blue); }
.chip .x { color: var(--dim); }
.chip .x:hover { color: var(--red); }
.chip.add { background: none; border: 1px dashed var(--border); color: var(--muted); }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 3px; overflow: hidden; }
.seg button { border: none; background: none; padding: 5px 12px; color: var(--muted); cursor: pointer; font-family: var(--mono); font-size: 12px; }
.seg button.on { background: var(--side); color: var(--text); }
.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--side); border: 1px solid var(--border); padding: 9px 14px; border-radius: 3px; z-index: 100; max-width: 80vw; }
.toast.error { border-color: var(--red); color: #ffb3c0; }

/* ---------- run view ---------- */
.run-grid { display: grid; grid-template-columns: minmax(0, 1fr) 560px; gap: 14px; align-items: start; }
.col { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.shared-row { display: flex; flex-wrap: wrap; gap: 10px; }
.shared-row .field { flex: 1 1 120px; }
.shared-row .field.wide { flex: 2 1 220px; }
.media-input { display: flex; gap: 6px; align-items: center; }
.media-input .input { flex: 1; }
.thumb { width: 36px; height: 24px; border-radius: 2px; object-fit: cover; background: var(--side); flex: none; }
.models-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.models-row > .lbl { color: var(--dim); font-size: 12px; margin-right: 2px; }
.empty { color: var(--dim); padding: 24px 14px; text-align: center; }

.popover { position: absolute; z-index: 50; width: 520px; max-height: 460px; display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--border); border-radius: 3px; box-shadow: 0 16px 40px rgba(0,0,0,.45); }
.popover-head { display: flex; gap: 8px; padding: 10px; border-bottom: 1px solid var(--border); }
.popover-head .input { flex: 1; }
.popover-list { overflow: auto; }
.result { display: grid; grid-template-columns: 40px 1fr auto; gap: 10px; padding: 8px 10px; cursor: pointer; align-items: center; }
.result:hover { background: var(--inset); }
.result.added { opacity: .5; }
.result.added:hover { opacity: .75; }
.result.added .r-prov { color: var(--blue); }
.result img, .result .noimg { width: 40px; height: 40px; border-radius: 3px; object-fit: cover; background: var(--side); }
.result .r-name { font-weight: 500; }
.result .r-sub { font-family: var(--mono); font-size: 11px; color: var(--dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result .r-prov { font-family: var(--mono); font-size: 11px; color: var(--muted); }
.popover-section { padding: 8px 10px 4px; font-size: 11px; color: var(--dim); font-family: var(--mono); text-transform: uppercase; }

/* options (CLI) */
.tabs { display: flex; gap: 4px; flex-wrap: wrap; padding: 0 14px 10px; }
.tabs:empty { display: none; }
.tabs button { border: none; background: none; padding: 3px 8px; border-radius: 3px; color: var(--dim); font-family: var(--mono); font-size: 11px; cursor: pointer; }
.tabs button.on { background: var(--side); color: var(--text); }
.endpoint { font-family: var(--mono); font-size: 12px; color: var(--dim); display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.endpoint select { font-size: 12px; padding: 2px 6px; }
.cli { display: flex; flex-direction: column; font-family: var(--mono); font-size: 12px; }
.cli-row { display: grid; grid-template-columns: 172px 172px minmax(0, 1fr); align-items: center; padding: 5px 8px; gap: 8px; min-height: 32px; }
.cli-row:nth-child(even) { background: var(--inset); }
.cli-row.head { color: var(--dim); font-size: 10px; border-bottom: 1px solid var(--border); background: none !important; min-height: 0; }
.cli-name { display: flex; align-items: center; gap: 6px; color: var(--blue); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cli-name::before { content: ''; flex: none; width: 5px; height: 5px; border-radius: 50%; }
.cli-name.changed::before { background: var(--yellow); }
.cli-name.req { color: #8cc4ff; }
.cli-meta { color: var(--dim); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cli-val { min-width: 0; display: flex; align-items: center; gap: 4px; }
.cli-val .input, .cli-val .select { width: 100%; padding: 3px 6px; font-family: var(--mono); font-size: 12px; }
.cli-val .input.unset, .cli-val .select.unset { color: var(--dim); }
.cli-val .shared { color: var(--dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.cli-val .shared:hover { color: var(--muted); }
.cli-row.missing .cli-name { color: var(--red); }
.cli-json { grid-column: 1 / -1; }
.cli-json .textarea { font-family: var(--mono); font-size: 12px; min-height: 90px; }
.cli-foot { display: flex; gap: 16px; padding: 10px 8px 0; color: var(--dim); font-size: 11px; flex-wrap: wrap; }
.notices { display: flex; flex-direction: column; gap: 4px; font-family: var(--mono); font-size: 11px; padding: 6px 8px 0; }

.runbar { flex-direction: row; align-items: center; justify-content: space-between; padding: 12px 14px; gap: 14px; }
.runbar .total { font-family: var(--mono); font-size: 20px; font-weight: 500; }
.runbar .breakdown { font-family: var(--mono); font-size: 11px; color: var(--dim); }

/* results */
.result-card { border: 1px solid; border-radius: 3px; overflow: hidden; }
.result-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 8px 12px; color: var(--ink); font-weight: 500; }
.result-head .rh-status { font-family: var(--mono); font-size: 12px; }
.result-body { display: flex; gap: 14px; padding: 12px; }
.preview { width: 240px; height: 135px; flex: none; border: 1px solid var(--border); border-radius: 2px; background: #12161a; display: flex; align-items: center; justify-content: center; color: var(--muted); overflow: hidden; }
.preview video, .preview img { width: 100%; height: 100%; object-fit: contain; background: #000; }
.preview audio { width: 92%; }
.metrics { display: flex; flex-direction: column; gap: 3px; font-family: var(--mono); font-size: 12px; min-width: 0; flex: 1; }
.metric { display: grid; grid-template-columns: 86px minmax(0, 1fr); gap: 8px; }
.metric .k { color: var(--dim); }
.metric .v { overflow-wrap: anywhere; }
.metric .v small { color: var(--dim); font-size: 11px; }
.actions { display: flex; gap: 14px; margin-top: auto; padding-top: 6px; flex-wrap: wrap; }
.st-succeeded { --c: var(--green); }
.st-queued, .st-running, .st-pending, .st-finalizing { --c: var(--yellow); }
.st-failed { --c: var(--red); }
.st-canceled { --c: var(--grey); }
.result-card { border-color: color-mix(in srgb, var(--c) 55%, transparent); }
.result-card .result-head { background: var(--c); }
.result-card .result-body { background: color-mix(in srgb, var(--c) 5%, transparent); }
.raw { margin: 0 12px 12px; padding: 10px; background: var(--inset); border: 1px solid var(--border); border-radius: 3px; font-family: var(--mono); font-size: 11px; white-space: pre-wrap; word-break: break-all; max-height: 260px; overflow: auto; }

/* ---------- log ---------- */
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: 3px; }
table.log { width: 100%; border-collapse: collapse; font-size: 12px; }
table.log th { text-align: left; font-family: var(--mono); font-weight: 400; font-size: 10px; color: var(--dim); background: var(--side); padding: 8px 10px; position: sticky; top: 0; white-space: nowrap; }
table.log td { padding: 9px 10px; border-top: 1px solid var(--border); white-space: nowrap; }
table.log td.num { font-family: var(--mono); text-align: right; }
table.log th.num { text-align: right; }
table.log tr.row { cursor: pointer; }
table.log tr.row:hover td { background: var(--inset); }
table.log tr.sel td { background: color-mix(in srgb, var(--blue) 10%, transparent); }
table.log td.status::before { content: '●'; margin-right: 6px; color: var(--c); }
table.log tr.detail td { white-space: normal; background: var(--inset); }
.detail-grid { display: grid; grid-template-columns: 260px 1fr; gap: 16px; padding: 4px 0; }
.detail-grid pre { margin: 0; font-family: var(--mono); font-size: 11px; white-space: pre-wrap; word-break: break-all; max-height: 240px; overflow: auto; }
.log-foot { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 2px; color: var(--muted); }

/* ---------- models ---------- */
.models-grid { display: flex; flex-direction: column; gap: 14px; }
.model-card .mc-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 14px; }
.mc-toggle { display: flex; align-items: center; gap: 10px; min-width: 0; background: none; border: none; padding: 0; color: inherit; text-align: left; cursor: pointer; font: inherit; }
.mc-toggle .chev { color: var(--dim); width: 12px; flex: none; }
.model-card:not(.open) .mc-head { padding-block: 10px; }
.mc-title { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.mc-title b { font-weight: 500; font-size: 14px; }
.mc-title span { font-family: var(--mono); font-size: 11px; color: var(--dim); }
table.prices { width: 100%; border-collapse: collapse; font-size: 12px; font-family: var(--mono); }
table.prices th { text-align: left; font-weight: 400; color: var(--dim); font-size: 10px; padding: 6px 8px; border-bottom: 1px solid var(--border); }
table.prices td { padding: 6px 8px; border-bottom: 1px solid var(--border); }
table.prices tr.future td { color: var(--dim); }
table.prices tr.expired td { color: var(--dim); text-decoration: line-through; }
.price-form { display: grid; grid-template-columns: 110px 100px 120px 90px 120px 120px 1fr auto; gap: 6px; align-items: center; }
.price-form .input, .price-form .select { padding: 4px 6px; font-family: var(--mono); font-size: 12px; width: 100%; }
.keys { display: flex; gap: 18px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.login { max-width: 360px; margin: 18vh auto; }

@media (max-width: 1180px) {
  .run-grid { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { flex-direction: row; align-items: center; }
  .sidebar nav { flex-direction: row; }
  .sidebar-foot { display: none; }
  .result-body { flex-direction: column; }
  .preview { width: 100%; height: auto; aspect-ratio: 16 / 9; }
  .cli-row { grid-template-columns: 130px 1fr; }
  .cli-meta { grid-column: 1 / -1; }
  .price-form { grid-template-columns: 1fr 1fr; }
}

/* library */
.prompt-wrap { position: relative; display: flex; flex-direction: column; gap: 4px; }
.prompt-tools { display: flex; gap: 14px; justify-content: flex-end; align-items: baseline; }
.prompt-tools > .link { white-space: nowrap; }
.tag-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.tag { font-family: var(--mono); font-size: 11px; padding: 2px 8px; border-radius: 2px; background: var(--side); color: var(--muted); border: 1px solid transparent; cursor: pointer; }
span.tag { cursor: default; }
.tag.on { border-color: var(--blue); color: var(--text); }
.lib-drop { display: none; border: 1px dashed var(--blue); border-radius: 3px; padding: 18px; text-align: center; color: var(--blue); margin-bottom: 10px; }
.lib-drop.on { display: block; }
.lib-prompts { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 12px; margin-bottom: 16px; }
.lib-card { padding: 12px; gap: 8px; }
.lib-card.editing { margin-bottom: 12px; }
.lib-card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.lib-card-head b { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-text { margin: 0; font-family: var(--mono); font-size: 11px; color: var(--muted); white-space: pre-wrap; max-height: 120px; overflow: auto; }
.lib-card .tag-row { margin: 0; }
.lib-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.lib-grid.small { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; padding: 10px; }
.lib-tile { background: var(--panel); border: 1px solid var(--border); border-radius: 3px; overflow: hidden; display: flex; flex-direction: column; min-width: 0; }
.lib-grid.small .lib-tile { cursor: pointer; background: var(--inset); }
.lib-grid.small .lib-tile:hover { border-color: var(--muted); }
.lib-tile.picked { border-color: var(--blue); box-shadow: 0 0 0 1px var(--blue); }
.lib-media { aspect-ratio: 1; background: #000; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.lib-media img, .lib-media video { width: 100%; height: 100%; object-fit: cover; }
.lib-tile.big .lib-media img { object-fit: contain; }
.lib-media.audio { aspect-ratio: auto; padding: 14px 8px; background: var(--inset); }
.lib-media audio { width: 100%; }
.lib-cap { font-size: 11px; padding: 4px 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.lib-tile.big .input { border: none; background: transparent; padding: 6px 8px 0; }
.lib-tile.big .lib-tags { font-size: 11px; padding: 0 8px 8px; color: var(--muted); }
.lib-meta { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; padding: 0 8px; font-size: 11px; gap: 2px 6px; }
.lib-pop { width: 560px; max-height: 520px; }
.lib-pop .tag-row { padding: 8px 10px 0; margin: 0; }
.lib-pick-foot { display: flex; justify-content: flex-end; align-items: center; gap: 10px; padding: 8px 10px; border-top: 1px solid var(--border); }
.lib-pick-foot:empty { display: none; }
.lib-prompt { grid-template-columns: 1fr auto; }
.media-input .btn.small { flex: none; }

/* references */
.ref-rows { display: flex; flex-direction: column; gap: 10px; }
.ref-strip { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-start; }
.ref-tile { position: relative; width: 92px; height: 92px; border-radius: 3px; overflow: hidden; background: var(--inset); border: 1px solid var(--border); display: flex; align-items: center; justify-content: center; }
.ref-tile img, .ref-tile video { width: 100%; height: 100%; object-fit: cover; }
.ref-tag { position: absolute; top: 4px; left: 4px; z-index: 1; font-family: var(--mono); font-size: 10px; padding: 1px 5px; border-radius: 2px; background: var(--yellow); color: var(--ink); cursor: pointer; }
.ref-tools { position: absolute; left: 0; right: 0; bottom: 0; display: none; justify-content: space-between; background: rgba(0,0,0,.6); }
.ref-tile:hover .ref-tools { display: flex; }
.ref-tools button { background: none; border: none; color: var(--text); cursor: pointer; padding: 3px 6px; font-size: 11px; }
.ref-tools button:disabled { opacity: .3; cursor: default; }
.ref-add { display: flex; gap: 4px; align-items: center; align-self: center; }
.ref-add .ref-url { width: 150px; }
.ref-url { font-size: 11px !important; padding: 3px 6px !important; }
.snippets { display: inline-flex; flex-wrap: wrap; gap: 4px 12px; margin-right: auto; }
.snippets .link { white-space: nowrap; }
