:root {
  --bg: #0b0b10;
  --panel: #13131a;
  --panel-2: #1a1a23;
  --line: #262632;
  --line-2: #33333f;
  --text: #ececf1;
  --muted: #8d8d9c;
  --accent: #a26bff;
  --accent-2: #ff5fa2;
  --accent-soft: rgba(162, 107, 255, .14);
  --ok: #3ccf91;
  --warn: #f5b041;
  --err: #ff5c6c;
  --radius: 12px;
  --radius-sm: 8px;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f5f5f8;
    --panel: #ffffff;
    --panel-2: #f0f0f5;
    --line: #e3e3ea;
    --line-2: #d2d2dc;
    --text: #17171f;
    --muted: #6b6b7b;
    --accent: #7c3aed;
    --accent-2: #db2777;
    --accent-soft: rgba(124, 58, 237, .1);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
h2 { font-size: 16px; margin: 0; font-weight: 650; }
h3 { font-size: 15px; margin: 0; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.pad { padding: 16px; }
[hidden] { display: none !important; }

button, .filebtn, select, input, textarea { font: inherit; color: inherit; }
button, .filebtn {
  cursor: pointer;
  border: 1px solid var(--line-2);
  background: var(--panel-2);
  border-radius: var(--radius-sm);
  padding: 8px 14px;
  transition: background .15s, border-color .15s, opacity .15s;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  white-space: nowrap;
}
button:hover, .filebtn:hover { border-color: var(--muted); }
button:disabled { opacity: .45; cursor: not-allowed; }
.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent;
  color: #fff;
  font-weight: 600;
}
.primary:hover { border-color: transparent; filter: brightness(1.08); }
.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.ghost:hover { color: var(--text); border-color: var(--line-2); }
.sm { padding: 5px 10px; font-size: 13px; }
.danger { color: var(--err); }

input[type=text], input[type=search], input[type=password], input[type=number], input[type=url], select, textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  outline: none;
}
textarea { resize: vertical; min-height: 60px; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px; padding: 3px 9px; border-radius: 99px;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
}

/* ---------- header ---------- */
.top {
  height: 56px; display: flex; align-items: center; gap: 18px;
  padding: 0 16px; border-bottom: 1px solid var(--line); background: var(--panel);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; font-size: 15px; }
.brand img { width: 26px; height: 26px; }
.brand b { background: linear-gradient(135deg, var(--accent), var(--accent-2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.top-right form { margin: 0; }
.tabs { display: flex; gap: 2px; }
.tabs button { background: transparent; border-color: transparent; color: var(--muted); }
.tabs button.on { color: var(--text); background: var(--panel-2); border-color: var(--line); }

.seg { display: inline-flex; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; color: var(--muted); padding: 5px 12px; border-radius: 7px; }
.seg button.on { background: var(--panel-2); color: var(--text); box-shadow: 0 1px 0 var(--line-2) inset; }
.seg.sm button { padding: 3px 9px; font-size: 12.5px; }
.seg .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); display: inline-block; }
.seg .dot.ok { background: var(--ok); }

/* ---------- generate layout ---------- */
.gen {
  display: grid;
  grid-template-columns: 280px minmax(360px, 460px) 1fr;
  height: calc(100vh - 56px);
}
.models, .form-pane, .results { min-height: 0; overflow: auto; }
.models { border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; }
.models-toggle { display: none; }
.models-body { display: flex; flex-direction: column; min-height: 0; flex: 1; padding: 12px; gap: 10px; }
.cats { display: flex; flex-wrap: wrap; gap: 5px; }
.cats button { padding: 3px 9px; font-size: 12px; border-radius: 99px; background: transparent; color: var(--muted); }
.cats button.on { background: var(--accent-soft); border-color: var(--accent); color: var(--text); }
.model-list { overflow: auto; flex: 1; margin: 0 -6px; }
.model-item {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; align-items: center;
  width: 100%; text-align: left; border: 1px solid transparent; background: transparent;
  padding: 8px 10px; border-radius: var(--radius-sm); white-space: normal;
}
.model-item:hover { background: var(--panel-2); border-color: transparent; }
.model-item.on { background: var(--accent-soft); border-color: var(--accent); }
.model-item .mname { font-weight: 550; overflow-wrap: anywhere; }
.model-item .mid { grid-column: 1 / -1; font-size: 11.5px; color: var(--muted); overflow-wrap: anywhere; }
.model-item .mprice { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.star { border: 0; background: none; padding: 0 2px; color: var(--muted); font-size: 14px; }
.star.on { color: var(--warn); }
.list-label { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 10px 10px 4px; }

.form-pane { border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.form-head { padding: 16px 18px 6px; }
.form-head .t { font-size: 17px; font-weight: 650; }
.form-head .sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.form-head .desc { color: var(--muted); font-size: 12.5px; margin-top: 8px; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.char-bar { padding: 8px 18px 0; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.char-pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px 3px 3px; border-radius: 99px; font-size: 12.5px; }
.char-pill img { width: 24px; height: 24px; border-radius: 50%; object-fit: cover; }

#genForm { padding: 8px 18px 18px; display: flex; flex-direction: column; gap: 14px; flex: 1; }
.field label.lbl { display: flex; justify-content: space-between; gap: 8px; font-weight: 550; font-size: 13px; margin-bottom: 6px; }
.field .req { color: var(--accent-2); margin-left: 2px; }
.field .hint { font-weight: 400; color: var(--muted); font-size: 12px; }
.field .help { color: var(--muted); font-size: 12px; margin-top: 5px; }
.group { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); }
.group > summary { cursor: pointer; padding: 10px 14px; font-weight: 600; list-style: none; display: flex; justify-content: space-between; }
.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: 4px 14px 14px; display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.group-body .field.wide { grid-column: 1 / -1; }

.pills { display: flex; flex-wrap: wrap; gap: 5px; }
.pills button { padding: 5px 10px; font-size: 12.5px; background: var(--bg); color: var(--muted); }
.pills button.on { background: var(--accent-soft); border-color: var(--accent); color: var(--text); }

.slider { display: flex; align-items: center; gap: 10px; }
.slider input[type=range] { flex: 1; accent-color: var(--accent); }
.slider input[type=number] { width: 84px; }

.switch { position: relative; display: inline-flex; align-items: center; gap: 10px; cursor: pointer; }
.switch input { appearance: none; width: 38px; height: 22px; background: var(--line-2); border-radius: 99px; position: relative; margin: 0; cursor: pointer; transition: background .15s; }
.switch input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; background: #fff; border-radius: 50%; transition: transform .15s; }
.switch input:checked { background: var(--accent); }
.switch input:checked::after { transform: translateX(16px); }

.seed { display: flex; gap: 6px; }
.seed input { flex: 1; }

/* media dropzone */
.drop {
  border: 1.5px dashed var(--line-2); border-radius: var(--radius); padding: 10px;
  background: var(--panel); transition: border-color .15s, background .15s;
}
.drop.over { border-color: var(--accent); background: var(--accent-soft); }
.drop .thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(74px, 1fr)); gap: 8px; }
.drop .thumbs:empty { display: none; }
.drop .thumbs:not(:empty) + .drop-actions { margin-top: 10px; }
.thumb { position: relative; aspect-ratio: 1; border-radius: var(--radius-sm); overflow: hidden; background: var(--panel-2); border: 1px solid var(--line); }
.thumb img, .thumb video { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .x { position: absolute; top: 3px; right: 3px; width: 22px; height: 22px; padding: 0; border-radius: 50%; background: rgba(0,0,0,.65); border: 0; color: #fff; font-size: 12px; }
.thumb .badge { position: absolute; left: 4px; bottom: 4px; font-size: 10px; padding: 1px 5px; border-radius: 4px; background: rgba(0,0,0,.65); color: #fff; }
.thumb.audio { display: grid; place-items: center; font-size: 22px; }
.thumb.loading::after { content: ""; position: absolute; inset: 0; background: rgba(0,0,0,.45) ; }
.drop-actions { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.drop-actions .muted { font-size: 12px; margin-left: auto; }

.gen-bar {
  position: sticky; bottom: 0; display: flex; align-items: center; gap: 12px;
  padding: 12px 18px; border-top: 1px solid var(--line); background: var(--panel);
}
.gen-bar .count { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.gen-bar .count select { width: auto; padding: 6px 8px; }
.gen-bar .cost { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.gen-bar .primary { margin-left: auto; padding: 10px 26px; font-size: 15px; }
.form-error { margin: 0 18px 14px; padding: 10px 12px; border-radius: var(--radius-sm); background: rgba(255, 92, 108, .12); color: var(--err); font-size: 13px; }

/* results */
.results { padding: 16px 18px; }
.results-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; }
.jobs { display: flex; flex-direction: column; gap: 14px; }
.job { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); overflow: hidden; }
.job-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; flex-wrap: wrap; }
.job-head .jm { font-weight: 600; }
.job-head .jt { color: var(--muted); font-size: 12px; margin-left: auto; font-variant-numeric: tabular-nums; }
.status { font-size: 11.5px; padding: 2px 8px; border-radius: 99px; display: inline-flex; align-items: center; gap: 5px; }
.status.running { background: var(--accent-soft); color: var(--accent); }
.status.completed { background: rgba(60, 207, 145, .13); color: var(--ok); }
.status.failed { background: rgba(255, 92, 108, .13); color: var(--err); }
.spin { width: 10px; height: 10px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.job-prompt { padding: 0 12px 10px; color: var(--muted); font-size: 12.5px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.job-err { margin: 0 12px 10px; color: var(--err); font-size: 12.5px; overflow-wrap: anywhere; }
.outs { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 8px; padding: 0 12px 12px; }
.out { position: relative; border-radius: var(--radius-sm); overflow: hidden; background: var(--panel-2); }
.out img, .out video { width: 100%; display: block; max-height: 520px; object-fit: contain; background: #000; cursor: zoom-in; }
.out .out-actions { position: absolute; right: 6px; top: 6px; display: flex; gap: 4px; opacity: 0; transition: opacity .15s; }
.out:hover .out-actions, .out:focus-within .out-actions { opacity: 1; }
.out .out-actions a, .out .out-actions button { background: rgba(0,0,0,.7); border: 0; color: #fff; padding: 4px 8px; border-radius: 6px; font-size: 12px; text-decoration: none; }
.out pre { padding: 10px; margin: 0; white-space: pre-wrap; font-size: 12px; }
.placeholder { aspect-ratio: 9 / 16; max-height: 360px; display: grid; place-items: center; color: var(--muted); font-size: 12.5px; background: linear-gradient(110deg, var(--panel-2) 30%, var(--line) 50%, var(--panel-2) 70%); background-size: 200% 100%; animation: shimmer 1.6s linear infinite; border-radius: var(--radius-sm); }
@keyframes shimmer { to { background-position: -200% 0; } }
.job-foot { display: flex; gap: 4px; padding: 0 8px 8px; flex-wrap: wrap; }
.empty { text-align: center; color: var(--muted); padding: 60px 20px; border: 1px dashed var(--line-2); border-radius: var(--radius); }

/* library / settings */
.page { max-width: 1200px; margin: 0 auto; padding: 22px 16px 60px; display: flex; flex-direction: column; gap: 30px; }
.page.narrow { max-width: 620px; gap: 12px; }
.sec-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 6px; }
.sec-head h2 { margin-right: auto; }
.chars { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; margin-top: 10px; }
.char { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.char .row { display: flex; gap: 8px; align-items: center; }
.char .row input { font-weight: 600; }
.char .thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); gap: 6px; }
.asset-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; margin-top: 10px; }
.asset-grid .thumb { cursor: pointer; }
.asset-grid .thumb.sel { outline: 2px solid var(--accent); outline-offset: 2px; }
.asset-grid .thumb .check { position: absolute; top: 5px; left: 5px; width: 20px; height: 20px; border-radius: 50%; background: var(--accent); color: #fff; display: none; place-items: center; font-size: 12px; }
.asset-grid .thumb.sel .check { display: grid; }
.keys { display: flex; flex-direction: column; gap: 16px; }
.key-card { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); padding: 14px; display: flex; flex-direction: column; gap: 8px; }
.key-card .row { display: flex; gap: 8px; }
.key-card label.field { display: flex; flex-direction: column; }
.key-card .field .lbl { font-weight: 550; font-size: 13px; margin-bottom: 6px; }

/* modal, lightbox, toast */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; width: min(860px, 100%); max-height: 88vh; display: flex; flex-direction: column; }
.modal-head, .modal-foot { padding: 12px 16px; display: flex; align-items: center; gap: 8px; }
.modal-head { border-bottom: 1px solid var(--line); justify-content: space-between; }
.modal-foot { border-top: 1px solid var(--line); justify-content: flex-end; }
.modal-foot:empty { display: none; }
.modal-body { padding: 14px 16px; overflow: auto; }
.modal-body h4 { margin: 14px 0 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.modal-body h4:first-child { margin-top: 0; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 60; display: grid; place-items: center; padding: 20px; cursor: zoom-out; }
.lightbox img, .lightbox video { max-width: 100%; max-height: 100%; object-fit: contain; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--panel-2); border: 1px solid var(--line-2); padding: 10px 16px; border-radius: 10px; z-index: 70; box-shadow: 0 8px 30px rgba(0,0,0,.35); max-width: calc(100% - 32px); }

/* ---------- responsive ---------- */
@media (max-width: 1180px) {
  .gen { grid-template-columns: 260px 1fr; grid-template-rows: auto; height: auto; }
  .results { grid-column: 1 / -1; border-top: 1px solid var(--line); }
  .models { max-height: calc(100vh - 56px); position: sticky; top: 56px; }
  .form-pane { border-right: 0; }
}
@media (max-width: 760px) {
  .top { height: auto; flex-wrap: wrap; padding: 10px 16px; gap: 10px; }
  .top .seg#providerSeg { order: 3; }
  .tabs { order: 4; width: 100%; overflow-x: auto; }
  .gen { display: block; }
  .models { position: static; max-height: none; border-right: 0; border-bottom: 1px solid var(--line); }
  .models-toggle { display: flex; justify-content: space-between; width: 100%; border: 0; border-radius: 0; padding: 12px 16px; background: var(--panel); }
  .models:not(.open) .models-body { display: none; }
  .models.open .model-list { max-height: 55vh; }
  .models-body { padding: 0 16px 12px; }
  #genForm, .form-head, .char-bar { padding-left: 16px; padding-right: 16px; }
  .gen-bar { padding: 10px 16px; }
  .group-body { grid-template-columns: 1fr; }
  .results { padding: 16px; }
  .outs { grid-template-columns: repeat(2, 1fr); }
  .out .out-actions { opacity: 1; }
}
