:root {
  --bg-light: #f5f7fb;
  --bg-dark: #0f131a;
  --surface-light: #ffffff;
  --surface-dark: #171c26;
  --surface-2-light: #eef2f8;
  --surface-2-dark: #1f2633;
  --border-light: #d7dfec;
  --border-dark: #2c3648;
  --text-light: #131926;
  --text-dark: #eef2f9;
  --muted-light: #526078;
  --muted-dark: #97a6bf;

  --brand-light: #2457d6;
  --brand-dark: #6ea0ff;
  --brand-text-light: #ffffff;
  --brand-text-dark: #091326;

  --ok-light: #137a46;
  --ok-dark: #4ade80;
  --warn-light: #b45309;
  --warn-dark: #fbbf24;
  --err-light: #c92a2a;
  --err-dark: #ff7b7b;
  --err-bg-light: #fdecec;
  --err-bg-dark: #3a191e;

  --bg: var(--bg-light);
  --surface: var(--surface-light);
  --surface-2: var(--surface-2-light);
  --border: var(--border-light);
  --text: var(--text-light);
  --muted: var(--muted-light);
  --brand: var(--brand-light);
  --brand-text: var(--brand-text-light);
  --ok: var(--ok-light);
  --warn: var(--warn-light);
  --err: var(--err-light);
  --err-bg: var(--err-bg-light);

  color-scheme: light dark;
  accent-color: var(--brand);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--bg-dark);
    --surface: var(--surface-dark);
    --surface-2: var(--surface-2-dark);
    --border: var(--border-dark);
    --text: var(--text-dark);
    --muted: var(--muted-dark);
    --brand: var(--brand-dark);
    --brand-text: var(--brand-text-dark);
    --ok: var(--ok-dark);
    --warn: var(--warn-dark);
    --err: var(--err-dark);
    --err-bg: var(--err-bg-dark);
  }
}

@supports (color: light-dark(white, black)) {
  :root {
    --bg: light-dark(var(--bg-light), var(--bg-dark));
    --surface: light-dark(var(--surface-light), var(--surface-dark));
    --surface-2: light-dark(var(--surface-2-light), var(--surface-2-dark));
    --border: light-dark(var(--border-light), var(--border-dark));
    --text: light-dark(var(--text-light), var(--text-dark));
    --muted: light-dark(var(--muted-light), var(--muted-dark));
    --brand: light-dark(var(--brand-light), var(--brand-dark));
    --brand-text: light-dark(var(--brand-text-light), var(--brand-text-dark));
    --ok: light-dark(var(--ok-light), var(--ok-dark));
    --warn: light-dark(var(--warn-light), var(--warn-dark));
    --err: light-dark(var(--err-light), var(--err-dark));
    --err-bg: light-dark(var(--err-bg-light), var(--err-bg-dark));
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}
.login-card { width: min(420px, 100%); }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.85rem 1rem;
  padding: 0.75rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 0.75rem; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 10px;
  display: grid; place-items: center;
  background: var(--brand); color: var(--brand-text);
  font-weight: 800; font-size: 0.95rem;
}
.brand h1 { margin: 0; font-size: 1.05rem; }
.subtitle { margin: 0; font-size: 0.78rem; color: var(--muted); }

.main-nav { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.nav-btn {
  padding: 0.45rem 0.75rem; border: 1px solid transparent; border-radius: 8px;
  background: transparent; color: var(--muted); font: inherit; font-size: 0.84rem; font-weight: 600;
  cursor: pointer;
}
.nav-btn:hover { background: var(--surface-2); color: var(--text); }
.nav-btn.is-active { background: var(--brand); color: var(--brand-text); }

.topbar-status { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }

.page-section { padding: 1.25rem 1.5rem 2.5rem; max-width: 1680px; margin: 0 auto; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.2rem;
}
.panel-head {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 0.75rem; margin-bottom: 1rem;
}
.panel-head h2 { margin: 0; font-size: 1.02rem; }
.hint { margin: 0; font-size: 0.8rem; color: var(--muted); }
.hint-inline { font-size: 0.78rem; color: var(--muted); }

/* Studio bar & layout */
.studio-bar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 0.75rem; margin-bottom: 1rem;
}
.studio-bar-right { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }

.provider-switch {
  display: inline-flex; gap: 0.25rem; padding: 0.25rem;
  border-radius: 11px; background: var(--surface); border: 1px solid var(--border);
}
.provider-btn {
  display: inline-flex; align-items: center; gap: 0.4rem;
  padding: 0.4rem 0.75rem; border: 0; border-radius: 8px;
  background: transparent; color: var(--muted); font: inherit; font-size: 0.82rem; font-weight: 600; cursor: pointer;
}
.provider-btn.is-active { background: var(--brand); color: var(--brand-text); }
.provider-dot { width: 7px; height: 7px; border-radius: 50%; background: color-mix(in srgb, var(--muted) 55%, transparent); }
.provider-btn.has-key .provider-dot { background: var(--ok); }
.provider-btn.is-active.has-key .provider-dot { background: #86efac; }

.studio-layout {
  display: grid;
  grid-template-columns: minmax(420px, 640px) minmax(0, 1fr);
  gap: 1.25rem;
  align-items: start;
}
@media (max-width: 1080px) { .studio-layout { grid-template-columns: 1fr; } }

/* Form fields */
.field { display: flex; flex-direction: column; gap: 0.35rem; margin-bottom: 0.9rem; }
.label-row { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
label { font-size: 0.84rem; font-weight: 600; }
input, select, textarea {
  width: 100%; padding: 0.55rem 0.75rem;
  border: 1px solid var(--border); border-radius: 9px;
  background: var(--surface-2); color: var(--text); font: inherit; font-size: 0.88rem;
}
textarea { resize: vertical; min-height: 220px; }
textarea.is-expanded { min-height: 460px; }
input[type="range"] { padding: 0; background: transparent; border: 0; }
input[type="checkbox"] { width: auto; }
.checkbox-label { display: inline-flex; align-items: center; gap: 0.45rem; font-size: 0.85rem; }

.options-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.75rem; }
.upload-row { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.upload-row input[type="url"] { flex: 1; min-width: 180px; }
.ref-head-right { display: flex; align-items: center; gap: 0.6rem; }

/* Buttons & Pills */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.35rem;
  padding: 0.55rem 0.95rem; border: 1px solid var(--border); border-radius: 9px;
  background: var(--surface-2); color: var(--text); font: inherit; font-size: 0.85rem; font-weight: 600;
  cursor: pointer; text-decoration: none;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: var(--brand-text); }
.btn-secondary { background: var(--surface-2); }
.btn-ghost { background: transparent; }
.btn-danger { color: var(--err); border-color: color-mix(in srgb, var(--err) 40%, var(--border)); background: transparent; }
.btn-sm { padding: 0.35rem 0.65rem; font-size: 0.78rem; }
.btn-link { background: transparent; border: 0; color: var(--brand); font: inherit; font-size: 0.78rem; font-weight: 600; cursor: pointer; padding: 0; }
.btn-link-danger { color: var(--err); }

.pill {
  display: inline-flex; align-items: center; gap: 0.35rem;
  padding: 0.25rem 0.65rem; border-radius: 999px; font-size: 0.76rem; font-weight: 600;
  background: var(--surface-2); color: var(--muted);
}
.pill-ok { color: var(--ok); background: color-mix(in srgb, var(--ok) 15%, var(--surface-2)); }
.pill-err { color: var(--err); background: var(--err-bg); }
.pill-warn { color: var(--warn); background: color-mix(in srgb, var(--warn) 15%, var(--surface-2)); }
.pill-accent { color: var(--brand); background: color-mix(in srgb, var(--brand) 15%, var(--surface-2)); }
.price-tag { font-size: 0.8rem; font-weight: 600; color: var(--ok); }
.value-badge { font-size: 0.8rem; font-weight: 700; color: var(--brand); }
.model-desc { margin: 0.3rem 0 0; font-size: 0.78rem; color: var(--muted); }

.feedback {
  padding: 0.65rem 0.85rem; border-radius: 10px; font-size: 0.84rem;
  background: var(--surface-2); border: 1px solid var(--border); margin-bottom: 0.75rem;
}
.feedback.is-error { background: var(--err-bg); color: var(--err); border-color: color-mix(in srgb, var(--err) 40%, transparent); }
.feedback.is-ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }

/* Reference & Library cards */
.ref-list, .library-list {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(112px, 1fr)); gap: 0.55rem; margin-top: 0.5rem;
}
.ref-item, .lib-item {
  position: relative; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--surface-2);
}
.ref-item img, .lib-item img { width: 100%; height: 84px; object-fit: cover; display: block; }
.ref-meta { padding: 0.3rem 0.45rem; font-size: 0.72rem; display: flex; justify-content: space-between; align-items: center; }
.ref-remove-float {
  position: absolute; top: 4px; right: 4px; width: 22px; height: 22px; border-radius: 50%;
  border: 0; background: rgba(0, 0, 0, 0.72); color: #fff; font-size: 0.75rem; cursor: pointer;
}
.lib-item.is-selected { outline: 2px solid var(--brand); }
.library-box { margin-top: 0.75rem; padding-top: 0.65rem; border-top: 1px dashed var(--border); }
.library-title { font-size: 0.8rem; font-weight: 600; color: var(--muted); }

/* Studio Queue cards */
.filter-tabs { display: flex; gap: 0.25rem; background: var(--surface-2); padding: 0.2rem; border-radius: 9px; }
.tab {
  border: 0; background: transparent; color: var(--muted); padding: 0.35rem 0.7rem;
  border-radius: 7px; font: inherit; font-size: 0.8rem; font-weight: 600; cursor: pointer;
}
.tab.is-active { background: var(--surface); color: var(--text); }

.jobs-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(310px, 1fr)); gap: 0.9rem; }
.job-card {
  border: 1px solid var(--border); border-radius: 12px; padding: 0.9rem; background: var(--surface-2);
  display: flex; flex-direction: column; gap: 0.55rem;
}
.job-top { display: flex; justify-content: space-between; align-items: center; }
.job-prompt { margin: 0; font-size: 0.84rem; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.job-tags { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.chip { padding: 0.15rem 0.5rem; border-radius: 6px; font-size: 0.73rem; background: var(--surface); border: 1px solid var(--border); }
.chip-provider { color: var(--brand); font-weight: 700; }
.job-progress { width: 100%; height: 6px; }
.job-video { width: 100%; max-height: 280px; border-radius: 8px; background: #000; }
.job-error { margin: 0; font-size: 0.78rem; color: var(--err); white-space: pre-wrap; }
.job-ids { font-size: 0.72rem; color: var(--muted); font-family: ui-monospace, SFMono-Regular, monospace; overflow-wrap: anywhere; }
.job-actions { display: flex; flex-wrap: wrap; gap: 0.4rem; }

/* Admin Tables, KPI, Forms */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0.9rem; }
.kpi-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 1rem; }
.kpi-label { font-size: 0.78rem; color: var(--muted); }
.kpi-val { font-size: 1.4rem; font-weight: 800; margin-top: 0.25rem; }

.providers-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1rem; }
.provider-card { border: 1px solid var(--border); border-radius: 12px; padding: 1rem; background: var(--surface-2); }

.inline-form {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 0.65rem; align-items: end;
}
.search-row { display: flex; gap: 0.5rem; align-items: center; }
.search-row input, .search-row select { width: auto; min-width: 150px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.83rem; }
th, td { text-align: left; padding: 0.6rem 0.7rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { background: var(--surface-2); font-weight: 700; white-space: nowrap; }
code { font-family: ui-monospace, SFMono-Regular, monospace; font-size: 0.8em; }
.empty-state { padding: 1.5rem; text-align: center; color: var(--muted); font-size: 0.88rem; }
pre.log-pre {
  background: var(--surface-2); padding: 0.75rem; border-radius: 8px;
  overflow: auto; font-size: 0.76rem; max-height: 260px; border: 1px solid var(--border);
}
