:root {
  color-scheme: dark;
  --bg: #16181d;
  --surface: #1f232b;
  --surface-2: #292e38;
  --text: #e8eaf0;
  --muted: #9aa3b2;
  --line: #343b47;
  --accent: #4f8cff;
  --accent-2: #7aa5ff;
  --ok: #3ecf8e;
  --warn: #f0b429;
  --danger: #f06a6a;
  --radius: 10px;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #eef0f4;
  --surface: #ffffff;
  --surface-2: #e8ebf1;
  --text: #1b1f27;
  --muted: #5a6472;
  --line: #d3d8e0;
  --accent: #2f6bdb;
  --accent-2: #6f97f2;
  --ok: #147a52;
  --warn: #9a6b00;
  --danger: #c93d3d;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}

/* ---------- header and tabs ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
}

.brand h1 {
  margin: 0;
  font-size: 1.15rem;
  letter-spacing: 0.02em;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-size: 0.78rem;
  color: var(--muted);
}

.badge::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}

.badge[data-state="live"]::before,
.badge[data-state="ok"]::before {
  background: var(--ok);
}

.badge[data-state="reconnecting"]::before,
.badge[data-state="unavailable"]::before {
  background: var(--danger);
}

.badge[data-state="live"],
.badge[data-state="ok"] {
  color: var(--text);
}

.tabs {
  display: flex;
  gap: 6px;
  padding: 10px 20px 0;
  flex-wrap: wrap;
}

.tab {
  appearance: none;
  border: 1px solid transparent;
  border-bottom: none;
  background: none;
  color: var(--muted);
  font: inherit;
  padding: 8px 16px;
  border-radius: var(--radius) var(--radius) 0 0;
  cursor: pointer;
}

.tab:hover {
  color: var(--text);
}

.tab.active {
  background: var(--surface);
  border-color: var(--line);
  color: var(--text);
}

.tab .count {
  display: inline-block;
  margin-left: 6px;
  padding: 0 7px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 0.75rem;
}

main {
  padding: 0 20px 40px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  padding: 20px;
  min-height: 55vh;
}

.panel h2 {
  margin: 4px 0 12px;
  font-size: 1rem;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

/* ---------- forms ---------- */

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

.form {
  max-width: 640px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

label {
  font-size: 0.85rem;
  color: var(--muted);
  margin-top: 8px;
}

/* Label + "i" help toggle. The explanation floats under the label without
   shifting the form, and is dismissed by clicking elsewhere or pressing Esc. */
.label-row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
}

.label-row label {
  margin-top: 0;
}

.info-toggle {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  padding: 0;
  border: 1px solid #999;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font: 700 0.7rem/1 Georgia, 'Times New Roman', serif;
  font: 700 0.5rem/1 monospace;
  cursor: help;
}

.info-toggle:hover,
.info-toggle[aria-expanded='true'] {
  color: var(--accent);
  border-color: var(--accent);
}

.info-toggle:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.field-help {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 30;
  width: max-content;
  max-width: min(340px, calc(100vw - 48px));
  margin: 0;
  padding: 8px 10px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.35);
  color: var(--text);
  font-size: 0.8rem;
  line-height: 1.4;
}

.field-help[hidden] {
  display: none;
}

input,
select,
textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 10px;
  width: 100%;
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
}

button:hover {
  border-color: var(--accent);
}

button.primary {
  margin-top: 14px;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

button.primary:disabled {
  opacity: 0.6;
  cursor: wait;
}

button.danger {
  color: var(--danger);
  border-color: var(--danger);
}

.hint {
  margin: 2px 0 4px;
  font-size: 0.8rem;
  color: var(--muted);
}

.notice {
  margin: 8px 0 4px;
  padding: 10px 12px;
  border: 1px solid var(--warn);
  border-radius: 8px;
  background: rgba(240, 180, 41, 0.1);
  color: var(--text);
  font-size: 0.85rem;
}

.notice code {
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 4px;
}

.form.blocked {
  opacity: 0.75;
}

.muted {
  color: var(--muted);
  font-weight: 400;
}

.message {
  margin: 10px 0 0;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 0.9rem;
}

.message[data-kind="ok"] {
  background: rgba(62, 207, 142, 0.12);
  color: var(--ok);
}

.message[data-kind="error"] {
  background: rgba(240, 106, 106, 0.12);
  color: var(--danger);
}

/* ---------- prompt builder ---------- */

.pb-intro {
  max-width: 760px;
  margin-bottom: 16px;
}

.pb-intro p {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.pb-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.pb-column {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.pb-column h2,
.pb-explainer h2 {
  margin-top: 0;
}

#pb-prompt {
  min-height: 150px;
  resize: vertical;
  line-height: 1.5;
}

.pb-meta {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 6px;
  color: var(--muted);
  font-size: 0.8rem;
}

.pb-actions,
.pb-builder-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.pb-actions .primary {
  margin-top: 0;
}

.pb-field {
  margin-top: 14px;
}

.pb-field label {
  display: block;
  margin-top: 0;
  color: var(--text);
  font-weight: 600;
}

.pb-field .hint {
  margin: 1px 0 6px;
}

.pb-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 7px;
}

.pb-chip {
  padding: 3px 9px;
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.75rem;
}

.pb-chip:hover {
  color: var(--text);
  border-color: var(--accent);
}

.pb-checks {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.pb-checks li {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.pb-mark {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  margin-top: 1px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
}

.pb-checks li[data-status="good"] .pb-mark {
  color: var(--ok);
  border-color: var(--ok);
}

.pb-checks li[data-status="tip"] .pb-mark {
  color: var(--warn);
  border-color: var(--warn);
}

.pb-checks strong {
  display: block;
  font-size: 0.88rem;
}

.pb-detail {
  display: block;
  color: var(--muted);
  font-size: 0.8rem;
}

.pb-review h3 {
  margin: 16px 0 6px;
  font-size: 0.85rem;
}

.pb-warnings {
  margin: 0;
  padding-left: 18px;
  display: flex;
  flex-direction: column;
  gap: 7px;
  font-size: 0.83rem;
}

.pb-fix {
  display: block;
  color: var(--muted);
}

.pb-good {
  margin: 12px 0 0;
  color: var(--ok);
  font-size: 0.84rem;
}

#pb-model-guide h3,
.pb-basics h3 {
  margin: 16px 0 5px;
  font-size: 0.85rem;
}

#pb-model-guide ul {
  margin: 0;
  padding-left: 18px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.84rem;
}

.pb-badge-line {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 10px;
  font-size: 0.8rem;
}

.pb-example {
  margin: 6px 0 12px;
  padding: 10px 12px;
  border-left: 3px solid var(--accent);
  border-radius: 0 8px 8px 0;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.84rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.pb-structured {
  margin-top: 12px;
}

.pb-structured summary {
  color: var(--accent-2);
  cursor: pointer;
  font-size: 0.84rem;
}

.pb-structured pre {
  max-height: 280px;
  overflow: auto;
  margin: 8px 0 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  font-size: 0.75rem;
}

.pb-structured button {
  margin-top: 8px;
}

.pb-explainer {
  margin-top: 16px;
}

.pb-basics {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px;
  margin-top: 12px;
}

.pb-basic {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-2);
}

.pb-basic h3 {
  margin: 0 0 4px;
  font-size: 0.9rem;
}

.pb-basic p {
  margin: 0;
  color: var(--muted);
  font-size: 0.83rem;
}

@media (max-width: 960px) {
  .pb-layout {
    grid-template-columns: 1fr;
  }
}

/* ---------- queue ---------- */

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
}

.job-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  background: var(--surface-2);
}

.job-card .row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.progress {
  width: 100%;
  height: 8px;
  border-radius: 999px;
  background: var(--line);
  overflow: hidden;
  margin: 8px 0 4px;
  appearance: none;
  -webkit-appearance: none;
  border: none;
}

.progress::-webkit-progress-bar {
  background: var(--line);
  border-radius: 999px;
}

.progress::-webkit-progress-value {
  background: var(--accent);
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  border-radius: 999px;
  transition: width 0.25s ease;
}

.progress::-moz-progress-bar {
  background: var(--accent);
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  border-radius: 999px;
}

.queued {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 720px;
}

.queued li {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-2);
}

.position {
  flex: none;
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--line);
  font-size: 0.8rem;
  font-weight: 600;
}

.jobs {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.job-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-2);
}

.job-row .grow {
  flex: 1 1 200px;
  min-width: 0;
}

.job-row .error {
  color: var(--danger);
  font-size: 0.82rem;
}

.job-row .meta {
  color: var(--muted);
  font-size: 0.8rem;
}

.chip {
  padding: 2px 9px;
  border-radius: 999px;
  border: 1px solid var(--line);
  font-size: 0.75rem;
  color: var(--muted);
  white-space: nowrap;
}

.chip[data-status="running"],
.chip[data-status="preparing"] {
  color: var(--accent-2);
  border-color: var(--accent-2);
}

.chip[data-status="succeeded"] {
  color: var(--ok);
  border-color: var(--ok);
}

.chip[data-status="failed"] {
  color: var(--danger);
  border-color: var(--danger);
}

.chip[data-status="cancel_requested"],
.chip[data-status="cancelled"] {
  color: var(--warn);
  border-color: var(--warn);
}

.chip[data-priority="urgent"] {
  color: #fff;
  background: var(--danger);
  border-color: var(--danger);
}

.chip[data-priority="high"] {
  color: var(--warn);
  border-color: var(--warn);
}

/* ---------- gallery ---------- */

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 12px;
}

.thumb {
  margin: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface-2);
  cursor: pointer;
}

.thumb img {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
}

.thumb figcaption {
  padding: 6px 8px;
  font-size: 0.75rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
}

.thumb figcaption .owner {
  display: block;
  color: var(--text);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thumb figcaption .file {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.empty {
  color: var(--muted);
  padding: 14px 0;
}

/* ---------- system ---------- */

#panel-system .meta {
  margin: 0;
}

.meta {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 14px;
  font-size: 0.9rem;
}

.meta dt {
  color: var(--muted);
}

.meta dd {
  margin: 0;
  word-break: break-word;
}

/* ---------- dialog ---------- */

.image-dialog {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 20px;
  max-width: min(92vw, 860px);
}

.image-dialog::backdrop {
  background: rgba(0, 0, 0, 0.65);
}

.image-dialog img {
  display: block;
  max-width: 100%;
  max-height: 60vh;
  margin: 0 auto 14px;
  border-radius: 8px;
}

.image-dialog .prompt {
  margin: 0 0 14px;
  padding: 10px 14px;
  border-left: 3px solid var(--accent);
  border-radius: 0 8px 8px 0;
  background: var(--surface-2);
  font-style: italic;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
}

.image-dialog .meta {
  margin-bottom: 16px;
}

.dialog-close {
  position: absolute;
  top: 8px;
  right: 8px;
  border: none;
  background: none;
  font-size: 1.4rem;
  color: var(--muted);
  padding: 4px 10px;
}

/* ---------- settings ---------- */

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 4px;
  color: var(--text);
  font-size: 0.95rem;
}

.switch-row input {
  appearance: none;
  flex: none;
  width: 44px;
  height: 24px;
  margin: 0;
  padding: 0;
  border-radius: 999px;
  background: var(--line);
  position: relative;
  cursor: pointer;
  transition: background 0.2s ease;
}

.switch-row input::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  transition: left 0.2s ease;
}

.switch-row input:checked {
  background: var(--accent);
}

.switch-row input:checked::after {
  left: 23px;
}

/* ---------- small screens ---------- */

@media (max-width: 640px) {
  .topbar,
  .tabs,
  main {
    padding-left: 12px;
    padding-right: 12px;
  }

  .panel {
    padding: 14px;
  }

  .grid {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  }

  .cards {
    grid-template-columns: 1fr;
  }
}
