:root {
  --bg: #f4f5f8;
  --panel: #ffffff;
  --ink: #16181d;
  --muted: #646b78;
  --line: #dde1e8;
  --accent: #e5202e;
  --accent-ink: #ffffff;
  --ok: #138a4b;
  --warn: #b26a00;
  --err: #c62828;
  --info: #2457c5;
  --chip: #eef0f4;
  --radius: 10px;
  --font: "Kanit", "Leelawadee UI", "Segoe UI", "Noto Sans Thai", Tahoma, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #101216;
    --panel: #181b21;
    --ink: #e9ebef;
    --muted: #9aa2b1;
    --line: #2a2f38;
    --chip: #222730;
    --ok: #3ccf7f;
    --warn: #f0a640;
    --err: #ff6b6b;
    --info: #7aa2ff;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101216;
  --panel: #181b21;
  --ink: #e9ebef;
  --muted: #9aa2b1;
  --line: #2a2f38;
  --chip: #222730;
  --ok: #3ccf7f;
  --warn: #f0a640;
  --err: #ff6b6b;
  --info: #7aa2ff;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.55 var(--font);
}
h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 1.15rem; }
h2 { font-size: 1.05rem; }
.th { color: var(--muted); font-weight: 400; font-size: .88em; }
.muted { color: var(--muted); font-weight: 400; font-size: .85em; }
.hint { color: var(--muted); font-size: .85rem; margin: .4rem 0 0; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .8rem 1rem; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: .7rem; min-width: 0; }
.logo {
  display: grid; place-items: center; width: 34px; height: 34px; flex: none;
  border-radius: 8px; background: var(--accent); color: var(--accent-ink); font-size: 14px;
}
.sub { margin: 0; color: var(--muted); font-size: .8rem; }
.tabs-nav { display: flex; gap: .25rem; margin-left: auto; }
.tabs-nav a {
  padding: .4rem .75rem; border-radius: 8px; color: var(--ink); text-decoration: none; font-size: .9rem;
  white-space: nowrap;
}
.tabs-nav a.active { background: var(--chip); font-weight: 500; }
.tabs-nav a:hover { background: var(--chip); }
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; }
  .tabs-nav { order: 3; width: 100%; margin-left: 0; }
  .brand .sub { display: none; }
}
.badge { padding: .25rem .6rem; border-radius: 99px; font-size: .8rem; background: var(--chip); white-space: nowrap; }
.badge.ok { color: var(--ok); }
.badge.err { color: var(--err); }

.layout {
  display: grid; gap: 1rem; padding: 1rem;
  grid-template-columns: minmax(0, 520px) minmax(0, 1fr);
  max-width: 1500px; margin: 0 auto;
}
@media (max-width: 1000px) { .layout { grid-template-columns: minmax(0, 1fr); } }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; min-width: 0; }
fieldset { border: 0; border-top: 1px solid var(--line); margin: 0 0 .6rem; padding: .7rem 0 0; min-width: 0; }
fieldset:first-of-type { border-top: 0; padding-top: 0; }
legend { font-weight: 600; padding: 0 .3rem 0 0; }
label { display: block; font-size: .86rem; margin-bottom: .5rem; min-width: 0; }
label > input, label > select, label > textarea { display: block; margin-top: .25rem; }
label.check { display: flex; align-items: center; gap: .4rem; }
label.check > input { margin-top: 0; }

input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  width: 100%; padding: .45rem .55rem; border: 1px solid var(--line); border-radius: 7px;
  background: var(--bg); min-width: 0;
}
input[type="checkbox"] { width: auto; }
textarea { resize: vertical; }
#finalPrompt { background: var(--chip); }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--info); outline-offset: 1px;
}

.row { display: flex; gap: .5rem; align-items: flex-end; }
.row.wrap { flex-wrap: wrap; }
.row .grow, .grow { flex: 1 1 0; }
.row label { margin-bottom: .4rem; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 .6rem; }
@media (max-width: 560px) { .grid2 { grid-template-columns: 1fr; } .row { flex-wrap: wrap; } }

button {
  border: 1px solid var(--line); background: var(--chip); border-radius: 7px; padding: .45rem .8rem;
  cursor: pointer; white-space: nowrap;
}
a.btn {
  display: inline-block; border: 1px solid var(--line); background: var(--chip); border-radius: 7px;
  padding: .45rem .8rem; color: inherit; text-decoration: none; white-space: nowrap;
}
button:disabled { opacity: .5; cursor: not-allowed; }
button.small, a.btn.small { padding: .25rem .6rem; font-size: .82rem; }
button.ghost { background: transparent; }
button.danger { color: var(--err); }
button.danger.armed { background: var(--err); color: #fff; border-color: var(--err); }
button.primary {
  width: 100%; padding: .75rem; font-size: 1.05rem; font-weight: 600;
  background: var(--accent); color: var(--accent-ink); border-color: var(--accent);
}
button.primary .th { color: rgba(255, 255, 255, .85); }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; margin-bottom: .4rem; }
.seg-btn { border: 0; border-radius: 0; background: transparent; }
.seg-btn.active { background: var(--ink); color: var(--panel); }
.seg-btn.active .th { color: inherit; opacity: .8; }

.model-list {
  max-height: 260px; overflow: auto; border: 1px solid var(--line); border-radius: 8px;
  margin-top: .4rem; background: var(--bg);
}
.model-item { display: grid; grid-template-columns: auto 1fr; gap: .1rem .5rem; padding: .45rem .6rem; border-bottom: 1px solid var(--line); margin: 0; cursor: pointer; }
.model-item:last-child { border-bottom: 0; }
.model-item input { margin-top: .3rem; }
.model-item .name { font-weight: 500; font-size: .9rem; }
.model-item .caps { grid-column: 2; color: var(--muted); font-size: .76rem; }
.model-item.selected { background: var(--chip); }

.refs { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .5rem; }
@media (max-width: 560px) { .refs { grid-template-columns: 1fr; } }
.ref-slot { display: flex; flex-direction: column; gap: .3rem; font-size: .84rem; }
.drop {
  border: 1px dashed var(--line); border-radius: 8px; padding: .6rem; text-align: center; cursor: pointer;
  margin: 0; color: var(--muted);
}
.drop input { display: none; }
.thumbs { display: flex; flex-wrap: wrap; gap: .3rem; }
.thumb { position: relative; width: 64px; height: 64px; border-radius: 6px; overflow: hidden; border: 1px solid var(--line); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb button {
  position: absolute; top: 2px; right: 2px; padding: 0 .35rem; font-size: .75rem; line-height: 1.4;
  background: rgba(0, 0, 0, .65); color: #fff; border: 0;
}

.alerts { display: flex; flex-direction: column; gap: .4rem; margin-bottom: .6rem; }
.alert { padding: .55rem .7rem; border-radius: 8px; font-size: .88rem; border: 1px solid; display: flex; justify-content: space-between; gap: .5rem; }
.alert.error { color: var(--err); border-color: var(--err); }
.alert.warning { color: var(--warn); border-color: var(--warn); }
.alert.info { color: var(--info); border-color: var(--info); }
.alert button { padding: 0 .4rem; background: transparent; border: 0; color: inherit; }

.jobs-head { display: flex; justify-content: space-between; align-items: center; gap: .6rem; flex-wrap: wrap; margin-bottom: .8rem; }
.job-list { display: flex; flex-direction: column; gap: 1rem; }
.group { border: 1px solid var(--line); border-radius: var(--radius); padding: .7rem; }
.group-head { display: flex; justify-content: space-between; gap: .5rem; font-size: .82rem; color: var(--muted); margin-bottom: .5rem; flex-wrap: wrap; }
.group-grid { display: grid; gap: .7rem; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.group.compare .group-head strong { color: var(--info); }

.job { border: 1px solid var(--line); border-radius: 8px; padding: .6rem; background: var(--bg); min-width: 0; }
.job-head { display: flex; gap: .5rem; align-items: center; margin-bottom: .4rem; min-width: 0; }
.job-head .model { font-size: .88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status { font-size: .74rem; padding: .1rem .5rem; border-radius: 99px; background: var(--chip); white-space: nowrap; }
.status.queued { color: var(--muted); }
.status.processing { color: var(--info); }
.status.completed { color: var(--ok); }
.status.failed { color: var(--err); }
.status.submit_unknown { color: var(--warn); }
.media video { width: 100%; border-radius: 6px; background: #000; display: block; max-height: 420px; }
.media .placeholder {
  display: grid; place-items: center; aspect-ratio: 16 / 9; border-radius: 6px; background: var(--chip);
  color: var(--muted); font-size: .85rem;
}
.media .placeholder.portrait { aspect-ratio: 9 / 16; max-height: 300px; margin: 0 auto; }
.media .placeholder.slim { aspect-ratio: auto; padding: .5rem; }
.prompt { font-size: .82rem; margin: .4rem 0; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.meta { display: flex; flex-wrap: wrap; gap: .1rem .8rem; font-size: .76rem; color: var(--muted); margin: 0; }
.meta div { display: flex; gap: .25rem; }
.meta dt { font-weight: 500; }
.meta dd { margin: 0; }
.error { color: var(--err); font-size: .8rem; margin: .3rem 0 0; white-space: pre-wrap; word-break: break-word; }
.error:empty { display: none; }
.actions { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: .5rem; }
.actions a { text-decoration: none; }
.imported { font-size: .76rem; color: var(--ok); word-break: break-all; }
.empty { color: var(--muted); text-align: center; padding: 2rem 0; }
.spin { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
