/* ComfyUI dark theme palette */
:root {
  --bg: #202020;
  --surface: #353535;
  --surface-2: #303030;
  --input-bg: #222222;
  --border: #4e4e4e;
  --text: #dddddd;
  --muted: #999999;
  --accent: #6a9955;
  --accent-hover: #7eb368;
  --accent-dim: rgba(106, 153, 85, 0.2);
  --link: #6eb5ff;
  --success: #6a9955;
  --danger: #ff4444;
  --radius: 4px;
  --gap: 12px;

  --font-sans: "Segoe UI", "Microsoft YaHei UI", "PingFang SC", system-ui, sans-serif;
  --font-mono: "Consolas", "Cascadia Mono", monospace;

  --text-xs: 11px;
  --text-sm: 12px;
  --text-base: 13px;
  --text-lg: 14px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  font-size: var(--text-base);
  -webkit-font-smoothing: antialiased;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

[hidden] {
  display: none !important;
}

.app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 12px 16px 20px;
}

/* ── Top bar ── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.topbar-left h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.topbar-sub {
  margin: 2px 0 0;
  font-size: var(--text-xs);
  color: var(--muted);
}

.server-bar,
.server-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: var(--text-xs);
  color: var(--muted);
  flex-wrap: wrap;
  justify-content: flex-end;
}

.server-select {
  min-width: 140px;
  max-width: 220px;
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--text);
  border-radius: var(--radius);
  padding: 5px 8px;
  font-size: var(--text-xs);
}

.server-btn {
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--text);
  border-radius: var(--radius);
  padding: 5px 10px;
  font-size: var(--text-xs);
  cursor: pointer;
}

.server-btn:hover {
  border-color: #666;
}

.server-btn-primary {
  width: 100%;
  margin-top: 4px;
  background: var(--accent);
  border-color: #5a8a47;
  color: #fff;
}

.server-dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  background: var(--surface);
  color: var(--text);
  width: min(420px, calc(100vw - 32px));
}

.server-dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.server-dialog-body {
  padding: 14px;
}

.server-dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.server-dialog-head h2 {
  margin: 0;
  font-size: var(--text-sm);
  font-weight: 600;
}

.server-dialog-desc {
  margin: 0 0 12px;
  font-size: var(--text-xs);
  color: var(--muted);
  line-height: 1.45;
}

.server-dialog-desc code {
  font-family: var(--font-mono);
  font-size: 10px;
}

.server-list {
  list-style: none;
  margin: 0 0 14px;
  padding: 0;
  display: grid;
  gap: 6px;
  max-height: 200px;
  overflow: auto;
}

.server-list-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px 8px;
  align-items: center;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--input-bg);
}

.server-list-item.active {
  border-color: var(--accent);
}

.server-list-name {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--text);
}

.server-list-url {
  grid-column: 1 / -1;
  font-size: 10px;
  color: var(--muted);
  word-break: break-all;
  font-family: var(--font-mono);
}

.server-list-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.server-add {
  border-top: 1px solid var(--border);
  padding-top: 12px;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #666;
  flex-shrink: 0;
}

.dot.online {
  background: var(--success);
}

.dot.offline {
  background: var(--danger);
}

/* ── Layout ── */
.workspace {
  display: grid;
  grid-template-columns: 380px 1fr;
  gap: 12px;
  align-items: start;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/* ── Sidebar ── */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.mode-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 10px 10px 0;
}

.mode-card {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--input-bg);
  color: var(--muted);
  font-size: var(--text-xs);
  cursor: pointer;
  text-align: center;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.mode-card:hover:not(.disabled) {
  color: var(--text);
  border-color: #666;
}

.mode-card.active {
  color: var(--text);
  background: var(--surface-2);
  border-color: #777;
}

.mode-card.disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.mode-tag {
  display: block;
  font-size: 10px;
  color: var(--muted);
  margin-top: 1px;
}

.mode-hint {
  margin: 8px 10px 0;
  padding: 0;
  font-size: var(--text-xs);
  color: var(--muted);
  line-height: 1.45;
}

.model-status {
  margin: 6px 10px 0;
  padding: 6px 8px;
  border-radius: var(--radius);
  font-size: var(--text-xs);
  line-height: 1.4;
  border: 1px solid var(--border);
}

.model-status-ok {
  color: var(--success);
  background: rgba(106, 153, 85, 0.12);
  border-color: rgba(106, 153, 85, 0.35);
}

.model-status-warn {
  color: #e8b86d;
  background: rgba(232, 184, 109, 0.08);
  border-color: rgba(232, 184, 109, 0.35);
}

.model-summary {
  margin-bottom: 8px;
  padding: 8px;
  border-radius: var(--radius);
  font-size: var(--text-xs);
  line-height: 1.45;
  border: 1px solid var(--border);
}

.model-summary-ok {
  color: var(--success);
  background: rgba(106, 153, 85, 0.1);
}

.model-summary-warn {
  color: #e8b86d;
  background: rgba(232, 184, 109, 0.08);
}

.model-cards {
  display: grid;
  gap: 6px;
  margin-bottom: 10px;
}

.model-card {
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--input-bg);
}

.model-card.is-installed {
  border-color: rgba(106, 153, 85, 0.35);
}

.model-card.is-missing {
  border-color: rgba(232, 184, 109, 0.35);
}

.model-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}

.model-card-head strong {
  font-size: var(--text-xs);
  color: var(--text);
}

.model-card-meta,
.model-card-path,
.model-installed-name {
  font-size: 10px;
  color: var(--muted);
  word-break: break-all;
  line-height: 1.4;
}

.model-card-path code {
  font-family: var(--font-mono);
  font-size: 10px;
}

.model-card-actions {
  margin-top: 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.model-badge {
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 2px;
  border: 1px solid var(--border);
  white-space: nowrap;
}

.model-badge-ok {
  color: var(--success);
  border-color: rgba(106, 153, 85, 0.4);
}

.model-badge-miss {
  color: #e8b86d;
  border-color: rgba(232, 184, 109, 0.4);
}

.model-extras {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

/* ── Sections ── */
.section {
  padding: 10px 10px 0;
  border-top: 1px solid var(--border);
  margin-top: 10px;
}

.section-compact .field {
  margin-bottom: 8px;
}

.section-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.section-title {
  margin: 0;
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text);
}

.section-desc {
  margin: 0 0 8px;
  font-size: var(--text-xs);
  color: var(--muted);
}

.badge {
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 2px;
  background: var(--input-bg);
  border: 1px solid var(--border);
  color: var(--muted);
}

.badge.optional {
  color: var(--muted);
}

/* ── Form fields ── */
.field {
  margin-bottom: 10px;
}

.field-flush {
  margin-bottom: 0;
}

.field label,
.field-label {
  display: block;
  margin-bottom: 4px;
  font-size: var(--text-xs);
  color: var(--muted);
}

.field textarea,
.field input[type="text"],
.field input[type="number"],
.field select {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--text);
  border-radius: var(--radius);
  padding: 6px 8px;
  font-size: var(--text-sm);
}

.field textarea {
  min-height: 88px;
  resize: vertical;
  line-height: 1.5;
}

.field select {
  padding-right: 24px;
}

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.checkbox {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  font-size: var(--text-xs);
  color: var(--muted);
  margin-top: 6px;
}

.checkbox input {
  width: auto;
  margin-top: 2px;
  flex-shrink: 0;
}

.radio-row .checkbox {
  margin-top: 0;
}

/* ── Chips ── */
.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 4px;
}

.chip {
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--muted);
  padding: 4px 10px;
  border-radius: var(--radius);
  font-size: var(--text-xs);
  cursor: pointer;
  transition: 0.12s;
}

.chip:hover {
  color: var(--text);
  border-color: #666;
}

.chip.active {
  color: var(--text);
  background: var(--surface-2);
  border-color: #777;
}

.chip-example {
  font-size: var(--text-xs);
  padding: 3px 8px;
}

/* ── Upload ── */
.upload-panel {
  margin-top: 4px;
}

.upload-panel.is-required .upload-box {
  border-color: #666;
}

.reference-upload-panel,
.upload-panel {
  /* legacy alias */
}

.upload-box {
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  min-height: 120px;
  position: relative;
  overflow: hidden;
  background: var(--input-bg);
}

.upload-box-sm {
  min-height: 96px;
}

.upload-box.drag-over {
  border-color: var(--accent);
  background: var(--accent-dim);
}

.upload-box input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.upload-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--muted);
  padding: 12px;
  pointer-events: none;
}

.upload-placeholder[hidden] {
  display: none !important;
}

.upload-title {
  margin: 0 0 4px;
  font-size: var(--text-sm);
  color: var(--text);
  text-align: center;
  width: 100%;
}

.upload-subhint {
  margin: 0;
  font-size: var(--text-xs);
  color: var(--muted);
  text-align: center;
  width: 100%;
}

.upload-status {
  margin: 6px 0 0;
  font-size: var(--text-xs);
  color: var(--muted);
}

.upload-status.has-files {
  color: var(--success);
}

.upload-preview {
  width: 100%;
  max-height: 200px;
  object-fit: contain;
}

.preview-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  width: 100%;
  min-height: 120px;
  padding: 8px;
  position: relative;
  z-index: 1;
}

.preview-grid[hidden] {
  display: none !important;
}

.preview-thumb {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

/* ── Advanced / sub-panels ── */
.advanced-panel {
  margin: 10px 10px 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--input-bg);
}

.advanced-panel > summary {
  padding: 7px 10px;
  cursor: pointer;
  font-size: var(--text-xs);
  color: var(--muted);
  list-style: none;
  user-select: none;
}

.advanced-panel > summary::-webkit-details-marker {
  display: none;
}

.advanced-body {
  padding: 0 8px 8px;
  border-top: 1px solid var(--border);
}

.sub-panel,
.model-section {
  margin-top: 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}

.sub-panel summary,
.model-section summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px;
  cursor: pointer;
  font-size: var(--text-xs);
  color: var(--muted);
  list-style: none;
}

.sub-panel summary::-webkit-details-marker,
.model-section summary::-webkit-details-marker {
  display: none;
}

.sub-panel-body,
.model-section-body {
  padding: 0 8px 8px;
  border-top: 1px solid var(--border);
}

.model-inventory {
  padding: 8px;
  display: grid;
  gap: 6px;
}

.inventory-group {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 8px;
  background: var(--input-bg);
}

.inventory-group h4 {
  margin: 0 0 4px;
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--text);
}

.inventory-group ul {
  margin: 0;
  padding-left: 1em;
  color: var(--muted);
  font-size: var(--text-xs);
  word-break: break-all;
}

.inventory-group li {
  font-family: var(--font-mono);
  font-size: 10px;
}

.inventory-empty,
.hint {
  font-size: var(--text-xs);
  color: var(--muted);
  margin: 4px 0 0;
}

.slider-row {
  margin-top: 4px;
}

.slider-row label {
  display: block;
  margin-bottom: 4px;
  font-size: var(--text-xs);
  color: var(--muted);
}

.slider-row input[type="range"],
.field input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

/* ── Footer / generate ── */
.sidebar-footer {
  padding: 10px;
  margin-top: 10px;
  border-top: 1px solid var(--border);
}

.generate-btn {
  flex: 1;
  min-width: 0;
  border: 1px solid #5a8a47;
  border-radius: var(--radius);
  padding: 8px 12px;
  font-size: var(--text-sm);
  font-weight: 600;
  color: #fff;
  cursor: pointer;
  background: var(--accent);
  transition: background 0.15s;
}

.generate-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

.generate-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.action-row {
  display: flex;
  gap: 6px;
}

.cancel-btn {
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--danger);
  border-radius: var(--radius);
  padding: 8px 10px;
  font-size: var(--text-xs);
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}

.progress-wrap {
  margin-top: 8px;
}

.progress-track {
  height: 4px;
  background: var(--input-bg);
  border-radius: 2px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.3s ease;
}

.status-text {
  margin-top: 6px;
  font-size: var(--text-xs);
  color: var(--muted);
  word-break: break-word;
}

.node-status {
  margin-top: 2px;
  font-size: var(--text-xs);
  color: var(--link);
  min-height: 14px;
  word-break: break-word;
}

.link-btn {
  border: 0;
  background: transparent;
  color: var(--link);
  font-size: var(--text-xs);
  cursor: pointer;
  padding: 0;
  margin-left: auto;
}

.link-btn:hover {
  text-decoration: underline;
}

.batch-queue {
  margin-top: 8px;
  display: grid;
  gap: 4px;
  max-height: 120px;
  overflow: auto;
}

.batch-item {
  display: grid;
  grid-template-columns: 24px 1fr auto;
  gap: 6px;
  align-items: center;
  padding: 4px 6px;
  border-radius: var(--radius);
  background: var(--input-bg);
  font-size: var(--text-xs);
  border: 1px solid transparent;
}

.batch-item.batch-running {
  border-color: var(--accent);
}

.batch-item.batch-error {
  border-color: var(--danger);
}

.batch-status {
  color: var(--muted);
  font-size: 10px;
  white-space: nowrap;
}

.batch-running .batch-status {
  color: var(--accent);
}

.batch-done .batch-status {
  color: var(--success);
}

.radio-row {
  display: grid;
  gap: 4px;
}

/* ── Preview panel ── */
.preview {
  min-height: 520px;
  display: flex;
  flex-direction: column;
}

.preview-header,
.result-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}

.result-tabs {
  display: flex;
  gap: 4px;
}

.result-tab {
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--muted);
  padding: 4px 12px;
  border-radius: var(--radius);
  font-size: var(--text-xs);
  cursor: pointer;
}

.result-tab.active {
  color: var(--text);
  background: var(--surface-2);
  border-color: #666;
}

.preview-body,
.result-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: center;
  padding: 12px;
  min-width: 0;
  min-height: 280px;
}

.result-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  text-align: center;
  color: var(--muted);
  font-size: var(--text-sm);
}

.result-empty p {
  margin: 0;
  width: 100%;
  text-align: center;
}

.history-list[hidden] {
  display: none !important;
}

.empty-title {
  margin: 0 0 4px;
  font-size: var(--text-sm);
  color: var(--text);
}

.empty-desc {
  margin: 0;
  font-size: var(--text-xs);
}

.result-box {
  width: 100%;
  min-width: 0;
  align-self: flex-start;
}

.result-grid {
  display: grid;
  grid-template-columns: 200px 1fr;
  gap: 10px;
}

.result-grid.image-only {
  grid-template-columns: 1fr;
  max-width: 640px;
  margin: 0 auto;
}

.result-grid.image-only .result-card-video {
  display: none;
}

.result-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  background: var(--input-bg);
  min-width: 0;
}

.result-card h3 {
  margin: 0 0 6px;
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--muted);
}

.result-card-main {
  border-color: #666;
}

.result-image {
  width: 100%;
  border-radius: var(--radius);
  background: #111;
  aspect-ratio: 16 / 9;
  object-fit: contain;
}

.result-image-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100px;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--muted);
  font-size: var(--text-xs);
}

.result-video {
  width: 100%;
  max-height: 480px;
  border-radius: var(--radius);
  background: #111;
}

.result-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.result-download-btn {
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--text);
  border-radius: var(--radius);
  padding: 8px 16px;
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
}

.result-download-btn:hover {
  border-color: #666;
}

.result-download-btn.primary {
  background: var(--accent);
  border-color: #5a8a47;
  color: #fff;
}

.result-download-btn.primary:hover {
  background: var(--accent-hover);
}

.result-meta {
  margin-top: 8px;
  font-size: var(--text-xs);
  color: var(--muted);
  word-break: break-word;
  line-height: 1.5;
}

.download-link {
  display: inline-block;
  margin-top: 6px;
  color: var(--link);
  text-decoration: none;
  font-size: var(--text-xs);
}

.download-link:hover {
  text-decoration: underline;
}

.history-list {
  width: 100%;
  display: grid;
  gap: 6px;
  max-height: 520px;
  overflow: auto;
  align-content: start;
}

.history-item {
  display: grid;
  grid-template-columns: 64px minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--input-bg);
}

.history-thumb-wrap {
  width: 64px;
  height: 48px;
  border-radius: var(--radius);
  overflow: hidden;
  background: #111;
}

.history-thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.history-thumb-empty {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  color: var(--muted);
}

.history-mode {
  font-size: var(--text-xs);
  color: var(--accent);
  margin-bottom: 2px;
}

.history-prompt {
  font-size: var(--text-xs);
  word-break: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.history-time {
  font-size: 10px;
  color: var(--muted);
  margin-top: 2px;
}

.history-actions {
  display: grid;
  gap: 4px;
}

.text-sub {
  margin: 4px 0 0;
  font-size: var(--text-xs);
  color: var(--muted);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ── Responsive ── */
@media (max-width: 900px) {
  .workspace {
    grid-template-columns: 1fr;
  }

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

  .topbar {
    flex-direction: column;
    align-items: flex-start;
  }

  .mode-nav {
    flex-direction: column;
  }

  .mode-card {
    text-align: left;
  }
}
