:root {
  color-scheme: dark;
  --bg: #000;
  --panel: rgba(28, 28, 30, 0.94);
  --panel2: #2c2c2e;
  --panel3: #3a3a3c;
  --label: #fff;
  --label2: rgba(255, 255, 255, 0.62);
  --label3: rgba(255, 255, 255, 0.34);
  --blue: #0a84ff;
  --green: #30d158;
  --orange: #ff9f0a;
  --red: #ff453a;
  --purple: #bf5af2;
  --teal: #64d2ff;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Display", Inter, "Noto Sans SC", sans-serif;
  --mono: "SF Mono", "JetBrains Mono", ui-monospace, Menlo, monospace;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body {
  min-height: 100%;
  margin: 0;
  color: var(--label);
  font-family: var(--sans);
  background:
    radial-gradient(circle at 8% 0, rgba(10, 132, 255, 0.2), transparent 30rem),
    radial-gradient(circle at 92% 0, rgba(191, 90, 242, 0.18), transparent 30rem),
    var(--bg);
}
button, input, select, textarea { font: inherit; }
button, a, input[type="file"] { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.45; }
h1, h2, p { margin: 0; }
.hidden { display: none !important; }
.mono { font-family: var(--mono); }
.subtle, .muted { color: var(--label2); }

.app-shell { max-width: 1920px; min-height: 100vh; margin: 0 auto; padding: 20px; }
.topbar {
  position: sticky;
  top: 12px;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 62px;
  margin-bottom: 16px;
  padding: 10px 14px;
}
.glass-panel {
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 18px;
  background: rgba(28, 28, 30, 0.74);
  box-shadow: 0 18px 56px rgba(0, 0, 0, 0.36);
  backdrop-filter: blur(20px) saturate(180%);
}
.brand-block, .topbar-meta, .panel-heading, .key-row, .upload-row, .actions, .history-actions, .result-title-row, .task-title-row, .result-actions, .action-buttons, .task-actions, .field-heading {
  display: flex;
  align-items: center;
}
.brand-block { gap: 12px; }
.brand-mark {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  border-radius: 14px;
  color: #050505;
  font: 800 13px var(--mono);
  background: linear-gradient(135deg, var(--purple), var(--teal));
}
h1 { display: flex; align-items: baseline; gap: 7px; font-size: 18px; letter-spacing: -0.02em; }
h2 { font-size: 18px; letter-spacing: -0.02em; }
.brand-block p { color: var(--label2); font-size: 13px; }
.app-version { color: var(--label3); font: 500 11px var(--mono); }
.topbar-meta { gap: 8px; }

.workspace {
  display: grid;
  grid-template-columns: minmax(300px, 340px) minmax(0, 1fr) minmax(280px, 320px);
  gap: 12px;
  align-items: start;
}
.card { border: 1px solid rgba(255, 255, 255, 0.1); border-radius: 16px; background: var(--panel); }
.control-panel, .result-panel, .task-panel, .action-panel { padding: 14px; }
.control-panel, .side-panel { position: sticky; top: 90px; max-height: calc(100dvh - 106px); }
.control-panel { overflow-y: auto; }
.side-panel { display: grid; grid-template-rows: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; }
.task-panel, .action-panel { min-height: 0; overflow-y: auto; }
.panel-heading { justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.eyebrow { margin-bottom: 4px; color: var(--label3); font: 11px var(--mono); letter-spacing: 0.15em; }

.api-key-section { margin-bottom: 12px; }
.tool-tabs { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; margin-bottom: 14px; padding: 4px; border-radius: 12px; background: rgba(0, 0, 0, 0.32); }
.tool-tab { min-width: 0; min-height: 34px; padding: 4px 5px; border: 0; border-radius: 9px; color: var(--label2); font-size: 11px; line-height: 1.2; background: transparent; }
.tool-tab:hover { color: var(--label); background: rgba(255, 255, 255, 0.06); }
.tool-tab.is-active { color: #fff; font-weight: 700; background: var(--blue); }
.tool-tab-panel > h2 { margin-bottom: 12px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.span-full { grid-column: 1 / -1; }
.field { display: grid; gap: 5px; min-width: 0; }
.field > span, .field-heading label, .field-heading > span { color: var(--label2); font-size: 13px; }
.field-heading { justify-content: space-between; gap: 8px; }
.field-heading small, .field > small, .drop-zone > small { color: var(--label3); font-size: 11px; }
input, select, textarea {
  width: 100%;
  min-width: 0;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  outline: none;
  color: var(--label);
  background: rgba(0, 0, 0, 0.34);
}
input, select { min-height: 36px; padding: 0 10px; }
textarea { min-height: 116px; padding: 10px; resize: vertical; line-height: 1.55; }
input:focus, select:focus, textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.14); }
.key-row, .upload-row { gap: 7px; }
.key-row input { font-family: var(--mono); }
.api-key-link { color: var(--blue); font-size: 12px; text-decoration: none; }
.api-key-link:hover { text-decoration: underline; }

.primary-button, .secondary-button, .ghost-button, .danger-button, .file-button, .preview-nav {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  padding: 0 14px;
  border: 0;
  border-radius: 999px;
  color: var(--label);
  text-decoration: none;
  transition: transform 150ms ease, border-color 150ms ease, background 150ms ease;
}
.primary-button:hover, .secondary-button:hover, .ghost-button:hover, .danger-button:hover, .result-card:hover { transform: translateY(-1px); }
.primary-button { flex: 1; color: #fff; font-weight: 700; background: var(--blue); }
.secondary-button { width: 100%; margin-top: 10px; color: #fff; font-weight: 700; background: var(--purple); }
.ghost-button, .file-button { border: 1px solid rgba(255, 255, 255, 0.12); background: rgba(255, 255, 255, 0.05); }
.danger-button { color: var(--red); border: 1px solid rgba(255, 69, 58, 0.3); background: rgba(255, 69, 58, 0.1); }
.compact-button { min-height: 30px; padding: 0 10px; font-size: 12px; }
.file-button { position: relative; flex: 1; overflow: hidden; }
.file-button input { position: absolute; inset: 0; opacity: 0; }
.actions { gap: 8px; }

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 999px;
  color: var(--label2);
  font: 11px var(--mono);
  background: rgba(255, 255, 255, 0.05);
  white-space: nowrap;
}
.badge-ok { color: var(--green); border-color: rgba(48, 209, 88, 0.3); background: rgba(48, 209, 88, 0.1); }
.badge-warn { color: var(--orange); border-color: rgba(255, 159, 10, 0.3); background: rgba(255, 159, 10, 0.1); }
.badge-error { color: var(--red); border-color: rgba(255, 69, 58, 0.3); background: rgba(255, 69, 58, 0.1); }

.advanced-panel, .drop-zone { border: 1px solid rgba(255, 255, 255, 0.1); border-radius: 12px; background: rgba(0, 0, 0, 0.18); }
.advanced-panel { padding: 9px; }
.advanced-panel summary { color: var(--label2); font-size: 13px; cursor: pointer; }
.advanced-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 9px; }
.check-field { align-content: end; }
.check-field input { width: 20px; min-height: 20px; accent-color: var(--blue); }
.inline-check { display: flex; align-items: center; justify-content: space-between; }
.drop-zone { padding: 10px; border-style: dashed; transition: border-color 150ms ease, background 150ms ease; }
.drop-zone.is-dragover { border-color: var(--blue); background: rgba(10, 132, 255, 0.1); }
.reference-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(62px, 1fr)); gap: 7px; }
.reference-thumb { position: relative; height: 62px; overflow: hidden; border: 1px solid rgba(255, 255, 255, 0.1); border-radius: 10px; background: var(--panel2); }
.reference-thumb img { width: 100%; height: 100%; object-fit: cover; }
.reference-remove { position: absolute; top: 3px; right: 3px; width: 20px; height: 20px; padding: 0; border: 0; border-radius: 50%; color: #fff; background: rgba(0, 0, 0, 0.7); }

.tool-images-field { margin-top: 8px; }
.tool-reference-list { margin-top: 7px; }

.result-panel { min-height: calc(100dvh - 98px); }
.result-heading { align-items: flex-start; }
.history-warning { margin-top: 4px; color: var(--orange); font-size: 11px; }
.library-tabs { display: flex; gap: 4px; }
.library-tab { min-height: 34px; padding: 0 12px; border: 0; border-radius: 999px; color: var(--label2); font-size: 18px; font-weight: 700; background: transparent; }
.library-tab.is-active { color: var(--label); background: rgba(255, 255, 255, 0.1); }
.history-actions { gap: 7px; flex-wrap: wrap; justify-content: flex-end; }
.history-actions select { width: auto; min-height: 30px; font-size: 12px; }
.result-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); grid-auto-rows: max-content; align-content: start; align-items: start; gap: 10px; }
.empty-state { display: grid; min-height: 180px; place-items: center; border: 1px dashed rgba(255, 255, 255, 0.1); border-radius: 13px; color: var(--label3); }
.result-card { position: relative; min-width: 0; overflow: hidden; border: 1px solid rgba(255, 255, 255, 0.1); border-radius: 13px; background: rgba(0, 0, 0, 0.25); transition: transform 150ms ease, border-color 150ms ease; }
.result-card:hover { border-color: rgba(255, 255, 255, 0.24); }
.result-card.is-selected { border-color: var(--blue); box-shadow: 0 0 0 2px rgba(10, 132, 255, 0.18); }
.result-media { display: block; width: 100%; padding: 0; overflow: hidden; border: 0; background: var(--panel2); }
.result-media img { display: block; width: 100%; height: auto; object-fit: contain; }
.result-media.no-image { display: none; }
.delete-result-button { position: absolute; top: 6px; right: 6px; z-index: 3; width: 24px; height: 24px; padding: 0; border: 0; border-radius: 50%; color: #fff; background: rgba(0, 0, 0, 0.72); }
.result-body { display: grid; gap: 7px; padding: 9px; }
.result-title-row { justify-content: space-between; gap: 7px; min-width: 0; }
.result-title { overflow: hidden; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.result-meta { color: var(--label2); font-size: 10px; }
.result-prompt { display: -webkit-box; overflow: hidden; color: var(--label2); font-size: 11px; line-height: 1.45; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
.text-result { max-height: 260px; margin: 0; padding: 9px; overflow: auto; border-radius: 9px; color: var(--label2); font: 11px/1.6 var(--mono); white-space: pre-wrap; background: rgba(255, 255, 255, 0.04); }
.action-buttons { flex-wrap: wrap; gap: 7px; }
.action-button { flex: 1 0 calc(25% - 7px); min-width: 48px; min-height: 34px; padding: 0 7px; border: 1px solid rgba(10, 132, 255, 0.36); border-radius: 8px; color: #b9dcff; font: 11px var(--mono); background: rgba(10, 132, 255, 0.12); }
.action-button.auxiliary { flex: 0 1 auto; padding: 0 10px; color: var(--label2); border-color: rgba(255, 255, 255, 0.12); background: rgba(255, 255, 255, 0.05); }
.action-button.reroll { flex: 1 0 100%; }
.selected-action-content { display: grid; align-content: start; gap: 10px; }
.selected-action-content.empty-state { min-height: 120px; align-content: center; }
.selected-result-heading { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.selected-result-heading strong { overflow: hidden; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.selected-result-prompt { display: -webkit-box; overflow: hidden; color: var(--label2); font-size: 11px; line-height: 1.5; -webkit-box-orient: vertical; -webkit-line-clamp: 3; }
.selected-action-buttons { align-items: stretch; }
.single-action-groups { display: grid; gap: 12px; }
.single-action-group { display: grid; gap: 6px; }
.single-action-group h3 { margin: 0; color: var(--label3); font-size: 11px; font-weight: 600; letter-spacing: 0.08em; }
.single-action-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; }
.single-action-grid .action-button { width: 100%; min-width: 0; padding: 0 8px; }
.single-action-group.reroll .single-action-grid { grid-template-columns: 1fr; }
.single-action-group.pan .single-action-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.selected-action-empty { color: var(--label3); font-size: 12px; }
.result-actions { flex-wrap: wrap; gap: 5px; }
.result-actions .ghost-button { min-height: 27px; padding: 0 8px; font-size: 11px; }
.pagination { display: flex; align-items: center; justify-content: center; gap: 10px; margin-top: 12px; }
.pagination span { color: var(--label2); font-size: 12px; }

.task-list { display: grid; gap: 9px; }
.task-card { display: grid; gap: 7px; padding: 10px; border: 1px solid rgba(255, 255, 255, 0.1); border-radius: 12px; background: rgba(0, 0, 0, 0.24); }
.task-card.has-error { border-color: rgba(255, 69, 58, 0.35); }
.task-title-row { justify-content: space-between; gap: 7px; }
.task-title { overflow: hidden; font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.progress-track { height: 5px; overflow: hidden; border-radius: 999px; background: var(--panel3); }
.task-progress-bar { display: block; width: 0; height: 100%; background: linear-gradient(90deg, var(--blue), var(--purple)); transition: width 250ms ease; }
.task-detail, .task-error { color: var(--label2); font-size: 10px; line-height: 1.5; }
.task-error { display: none; color: var(--red); }
.task-card.has-error .task-error { display: block; }
.task-actions { justify-content: flex-end; }
.task-stop-button { min-height: 27px; padding: 0 8px; color: var(--red); font-size: 11px; }

.toast { position: fixed; top: 20px; left: 50%; z-index: 1000; max-width: min(440px, calc(100vw - 32px)); padding: 10px 15px; border: 1px solid rgba(255, 255, 255, 0.14); border-radius: 999px; color: var(--label); font-size: 13px; text-align: center; background: rgba(44, 44, 46, 0.94); opacity: 0; pointer-events: none; transform: translate(-50%, -10px); transition: opacity 180ms ease, transform 180ms ease; }
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }

.preview-dialog { width: 100vw; max-width: none; height: 100dvh; max-height: none; margin: 0; padding: 0; overflow: hidden; border: 0; color: var(--label); background: rgba(0, 0, 0, 0.87); }
.preview-dialog::backdrop, .form-dialog::backdrop { background: rgba(0, 0, 0, 0.78); backdrop-filter: blur(10px); }
.preview-stage { position: absolute; inset: 0; overflow: hidden; cursor: grab; touch-action: none; }
.preview-stage.is-dragging { cursor: grabbing; }
.preview-canvas { position: absolute; inset: 0; pointer-events: none; transform-origin: 0 0; }
.preview-canvas img { position: absolute; top: 50%; left: 50%; display: block; max-width: calc(100vw - 48px); max-height: calc(100dvh - 150px); border-radius: 12px; object-fit: contain; transform: translate(-50%, -50%); pointer-events: auto; user-select: none; }
.preview-toolbar { position: absolute; top: 16px; right: 16px; z-index: 4; display: flex; gap: 7px; }
.preview-toolbar .ghost-button { background: rgba(0, 0, 0, 0.7); }
.preview-nav { position: absolute; top: 50%; z-index: 4; width: 44px; height: 44px; padding: 0; font-size: 34px; background: rgba(0, 0, 0, 0.68); transform: translateY(-50%); }
.preview-previous { left: 14px; }
.preview-next { right: 14px; }
.preview-meta { position: absolute; right: 16px; bottom: 16px; left: 16px; z-index: 3; max-height: 86px; padding: 9px 11px; overflow: auto; border: 1px solid rgba(255, 255, 255, 0.1); border-radius: 10px; color: var(--label2); font: 11px/1.6 var(--mono); white-space: pre-wrap; background: rgba(0, 0, 0, 0.68); }

.form-dialog { width: min(600px, calc(100vw - 28px)); padding: 0; border: 0; color: var(--label); background: transparent; }
.dialog-card { display: grid; gap: 12px; padding: 16px; }

@media (min-width: 1280px) {
  html, body { height: 100%; overflow: hidden; }
  .app-shell { display: grid; grid-template-rows: auto minmax(0, 1fr); gap: 12px; height: 100dvh; min-height: 0; padding: 14px; }
  .topbar { position: static; min-height: 56px; margin: 0; padding: 8px 12px; }
  .workspace { min-height: 0; }
  .control-panel, .side-panel { position: static; top: auto; height: 100%; max-height: none; min-height: 0; }
  .side-panel { grid-template-rows: minmax(0, 3fr) minmax(0, 1fr); }
  .result-panel { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; height: 100%; min-height: 0; overflow: hidden; }
  .result-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); grid-template-rows: repeat(3, minmax(0, 1fr)); min-height: 0; overflow: hidden; align-items: stretch; }
  .result-card { display: grid; grid-template-rows: minmax(48px, 1fr) auto; min-height: 0; height: 100%; }
  .result-media { min-height: 48px; height: 100%; }
  .result-media img { width: 100%; height: 100%; object-fit: contain; }
  .result-body { gap: 4px; min-height: 0; padding: 6px; overflow: hidden; }
  .result-prompt { display: none; }
  .result-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px; }
  .result-actions .ghost-button { min-width: 0; min-height: 24px; padding: 0 5px; font-size: 10px; }
  .pagination { margin-top: 8px; }
}

@media (max-width: 1180px) {
  .workspace { grid-template-columns: minmax(290px, 340px) minmax(0, 1fr); }
  .side-panel { position: static; grid-column: 1 / -1; grid-template-columns: 1fr 1fr; grid-template-rows: none; max-height: none; }
  .task-list { grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); }
}

@media (max-width: 820px) {
  .workspace { grid-template-columns: 1fr; }
  .control-panel, .side-panel { position: static; max-height: none; }
  .side-panel { grid-template-columns: 1fr; grid-template-rows: none; }
  .result-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 560px) {
  .app-shell { padding: 12px; }
  .topbar { position: static; display: grid; gap: 10px; align-items: stretch; }
  .topbar-meta { flex-wrap: wrap; }
  .form-grid, .advanced-grid { grid-template-columns: 1fr; }
  .span-full { grid-column: auto; }
  .result-grid { grid-template-columns: 1fr; }
  .result-heading { display: grid; align-items: stretch; }
  .history-actions { justify-content: flex-start; }
  .action-button { flex-basis: calc(25% - 5px); }
  .preview-canvas img { max-width: calc(100vw - 20px); max-height: calc(100dvh - 166px); }
  .preview-toolbar { top: 10px; right: 10px; }
  .preview-nav { width: 40px; height: 40px; }
  .preview-previous { left: 7px; }
  .preview-next { right: 7px; }
  .preview-meta { right: 10px; bottom: 10px; left: 10px; }
}
