:root {
  color-scheme: light;
  --bg: #f7f8fa;
  --panel: #ffffff;
  --panel-2: #f2f4f7;
  --text: #15181d;
  --muted: #69707d;
  --line: #dde2ea;
  --accent: #2563eb;
  --accent-soft: #e8f0ff;
  --danger: #dc2626;
  --ok: #15803d;
  --warning: #b45309;
  --shadow: 0 10px 26px rgba(18, 24, 40, .07);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101216;
  --panel: #181b20;
  --panel-2: #20242b;
  --text: #eef2f7;
  --muted: #a7b0bf;
  --line: #303641;
  --accent: #60a5fa;
  --accent-soft: #132338;
  --danger: #f87171;
  --ok: #4ade80;
  --warning: #fbbf24;
  --shadow: 0 12px 32px rgba(0, 0, 0, .26);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background: var(--bg);
}

button, input, textarea, select { font: inherit; }
button {
  min-height: 34px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  transition: border-color .15s ease, background .15s ease, transform .08s ease;
}
button:hover { border-color: color-mix(in srgb, var(--accent) 44%, var(--line)); }
button:active { transform: translateY(1px); }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 700;
}
button.secondary {
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 28%, var(--line));
  color: var(--accent);
  font-weight: 650;
}
button.danger {
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 30%, var(--line));
  background: color-mix(in srgb, var(--danger) 7%, var(--panel));
}
button.icon {
  width: 34px;
  padding: 0;
  display: inline-grid;
  place-items: center;
}
button:disabled { opacity: .55; cursor: not-allowed; }

.app {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
}
header {
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 10px clamp(14px, 3vw, 28px);
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}
.mark {
  width: 44px;
  height: 30px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 850;
  flex: 0 0 auto;
}
h1 {
  font-size: 16px;
  margin: 0;
  letter-spacing: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

main {
  display: grid;
  grid-template-columns: minmax(360px, 1fr) minmax(340px, 430px);
  gap: 12px;
  padding: 12px;
  min-height: 0;
  overflow: hidden;
}
.tasks, .controls {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  min-height: 0;
  overflow: hidden;
}
.tasks { display: grid; grid-template-rows: auto auto 1fr; }
.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.panel-title h2 { font-size: 16px; margin: 0; }
.hint { color: var(--muted); font-size: 12px; line-height: 1.45; }
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
}
.toolbar .hint { flex: 1 1 260px; align-self: center; }
.task-list {
  padding: 10px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(142px, 1fr));
  gap: 10px;
  overflow: auto;
  align-content: start;
}
.empty {
  grid-column: 1 / -1;
  min-height: 230px;
  display: grid;
  place-items: center;
  color: var(--muted);
  text-align: center;
  padding: 24px;
}
.task-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 9px;
  overflow: hidden;
  min-width: 0;
}
.thumb {
  width: 100%;
  aspect-ratio: 1 / 1;
  background: var(--panel-2);
  display: grid;
  place-items: center;
  color: var(--muted);
  overflow: hidden;
  cursor: zoom-in;
  position: relative;
}
.thumb img, .thumb video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.thumb .badge {
  position: absolute;
  left: 8px;
  top: 8px;
  border-radius: 999px;
  padding: 3px 7px;
  background: rgba(15, 23, 42, .74);
  color: #fff;
  font-size: 11px;
}
.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid color-mix(in srgb, var(--accent) 15%, transparent);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.task-meta { padding: 8px; display: grid; gap: 4px; min-width: 0; }
.task-name {
  font-size: 12px;
  font-weight: 750;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.task-sub {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.task-progress {
  height: 4px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--panel-2);
}
.task-progress span {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width .18s ease;
}
.status-ok { color: var(--ok); }
.status-error { color: var(--danger); }
.status-running { color: var(--warning); }

.controls { display: grid; grid-template-rows: auto 1fr; }
.control-scroll {
  overflow: auto;
  padding: 12px;
  display: grid;
  gap: 12px;
  align-content: start;
}
.section { display: grid; gap: 10px; }
.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.section-title h3 { margin: 0; font-size: 14px; }
.dropzone {
  border: 1.5px dashed var(--line);
  border-radius: 10px;
  min-height: 132px;
  display: block;
  padding: 9px 9px 44px;
  text-align: center;
  background: var(--panel-2);
  position: relative;
  overflow: hidden;
}
.dropzone.dragging { border-color: var(--accent); background: var(--accent-soft); }
.ref-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  min-height: 68px;
  position: relative;
  z-index: 1;
}
.drop-hint {
  position: absolute;
  left: 9px;
  right: 9px;
  bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}
.ref-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  aspect-ratio: 1 / 1;
  position: relative;
  background: var(--panel);
}
.ref-item img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ref-item button {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 24px;
  min-height: 24px;
  padding: 0;
  border-radius: 999px;
  background: color-mix(in srgb, var(--panel) 92%, transparent);
}
.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-2);
}
.segmented button { border: 0; background: transparent; min-height: 30px; }
.segmented button.active {
  background: var(--panel);
  color: var(--accent);
  font-weight: 750;
  box-shadow: 0 1px 5px rgba(18, 24, 40, .08);
}
.choice-field > span { color: var(--muted); }
.option-slider {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 3px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--panel-2);
  min-height: 34px;
}
.option-slider.wrap { display: flex; flex-wrap: wrap; }
.option-slider.wrap button { flex: 1 1 auto; }
.option-slider button {
  min-height: 26px;
  padding: 0 8px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}
.option-slider button.active { background: var(--panel); color: var(--accent); font-weight: 750; }
.batch-control { display: grid; grid-template-columns: minmax(0, 1fr) 64px; gap: 7px; align-items: center; }
.batch-custom { text-align: center; padding-left: 6px; padding-right: 6px; }
label { display: grid; gap: 6px; color: var(--muted); font-size: 12px; min-width: 0; }
input, select, textarea {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  min-height: 34px;
  padding: 7px 10px;
  width: 100%;
  min-width: 0;
}
textarea { resize: vertical; min-height: 150px; line-height: 1.5; }
.prompt-field textarea { min-height: 180px; }
.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.template-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: end; }
.form-actions {
  display: grid;
  gap: 7px;
  position: sticky;
  bottom: -12px;
  background: linear-gradient(transparent, var(--panel) 28%);
  padding-top: 12px;
}

.modal, .menu { display: none; }
.modal.open {
  display: grid;
  position: fixed;
  inset: 0;
  place-items: center;
  background: rgba(15, 23, 42, .48);
  z-index: 50;
  padding: 16px;
}
.dialog {
  width: min(760px, 100%);
  max-height: min(88vh, 760px);
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, .24);
  display: grid;
  grid-template-rows: auto 1fr auto;
}
.dialog.small { width: min(520px, 100%); }
.dialog-head, .dialog-foot {
  padding: 12px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.dialog-foot { border-bottom: 0; border-top: 1px solid var(--line); justify-content: flex-end; flex-wrap: wrap; }
.dialog-head h3 { margin: 0; font-size: 15px; }
.dialog-body { padding: 12px; overflow: auto; display: grid; gap: 12px; }
.viewer {
  width: 100%;
  min-height: 300px;
  display: grid;
  place-items: center;
  background: #0f1115;
  border-radius: 8px;
  overflow: hidden;
}
.viewer img, .viewer video { max-width: 100%; max-height: 68vh; display: block; }
.menu.open {
  display: grid;
  position: fixed;
  z-index: 60;
  min-width: 172px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 14px 38px rgba(0, 0, 0, .16);
  border-radius: 10px;
  padding: 6px;
  gap: 4px;
}
.menu button { text-align: left; background: transparent; border: 0; justify-content: start; }
.toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  background: #111827;
  color: #fff;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 70;
  max-width: min(520px, calc(100vw - 24px));
  display: none;
}
.toast.show { display: block; }
.mobile-blocker { display: none; }

@media (max-width: 900px) {
  body { overflow: auto; }
  .app { height: auto; min-height: 100dvh; overflow: visible; }
  header { align-items: flex-start; padding: 12px; }
  .header-actions { flex-wrap: wrap; justify-content: flex-end; }
  main {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(330px, 46vh) auto;
    overflow: visible;
  }
  .control-scroll { display: flex; flex-direction: column; }
  .settings-section { order: 1; }
  .ref-section { order: 2; }
}

@media (max-width: 540px) {
  button { min-height: 30px; padding: 0 9px; border-radius: 7px; font-size: 12px; }
  header { padding: 8px; gap: 8px; }
  h1 { font-size: 14px; }
  .mark { width: 38px; height: 26px; }
  .header-actions { gap: 5px; }
  main { grid-template-rows: minmax(260px, 38vh) auto; gap: 8px; padding: 8px; }
  .tasks, .controls { border-radius: 9px; }
  .panel-title { padding: 9px; }
  .panel-title h2 { font-size: 14px; }
  .toolbar { gap: 6px; padding: 7px 9px; }
  .toolbar .hint { flex-basis: 100%; }
  .task-list { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; padding: 8px; }
  .empty { min-height: 118px; padding: 16px; }
  .task-meta { padding: 7px; gap: 3px; }
  .task-name { font-size: 11px; }
  .task-sub { font-size: 10px; }
  .control-scroll { gap: 9px; padding: 9px; }
  .section, .settings-section { gap: 8px; }
  .settings-section { display: grid; }
  .prompt-field { order: 1; }
  .segmented { order: 2; }
  .model-field { order: 3; }
  .settings-grid { order: 4; }
  .template-row { order: 5; }
  .form-actions { order: 6; }
  .prompt-field textarea { min-height: 150px; }
  .settings-grid {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-areas:
      "ratio ratio"
      "resolution quality"
      "background batch"
      "duration duration";
    gap: 7px;
  }
  body[data-model-kind="gemini"] .settings-grid { grid-template-areas: "ratio ratio" "resolution batch"; }
  body[data-mode="video"] .settings-grid { grid-template-areas: "ratio resolution" "duration batch"; }
  .ratio-field { grid-area: ratio; }
  .resolution-field { grid-area: resolution; }
  .quality-field { grid-area: quality; }
  .background-field { grid-area: background; }
  .duration-field { grid-area: duration; }
  .batch-field { grid-area: batch; }
  input, select, textarea { min-height: 31px; border-radius: 7px; }
  .option-slider { min-height: 30px; gap: 2px; padding: 2px; }
  .option-slider button { min-height: 24px; padding: 0 5px; font-size: 11px; }
  .batch-control { grid-template-columns: minmax(0, 1fr) 52px; gap: 5px; }
  .dropzone { min-height: 108px; padding: 8px 8px 38px; }
  .drop-hint { bottom: 6px; gap: 6px; font-size: 11px; }
  .ref-grid { grid-template-columns: repeat(2, 1fr); }
  .form-actions { gap: 5px; padding-top: 8px; bottom: -8px; }
  .viewer { min-height: 220px; }
  .viewer img, .viewer video { max-height: 64vh; }
  .toast { bottom: 10px; font-size: 12px; }
}
