:root {
  --bg-page: #f4f5f6;
  --bg-surface: #ffffff;
  --bg-subtle: #eff1f3;
  --bg-muted: #e6e8eb;
  --bg-canvas: #141517;
  --bg-sidebar: #161719;
  --bg-sidebar-hover: #222428;
  --bg-sidebar-active: #2b2e33;

  --text-primary: #15171a;
  --text-secondary: #495057;
  --text-muted: #6c757d;
  --text-sidebar: #d8dce0;
  --text-sidebar-muted: #8a9199;
  --text-inverse: #ffffff;

  --border-color: #dcdfe4;
  --border-strong: #c4c9d0;
  --border-sidebar: #282b30;

  --accent-red: #c93b2b;
  --accent-red-hover: #b13122;
  --accent-red-subtle: #fbeae8;
  --accent-green: #1f7a4c;
  --accent-green-hover: #18633d;
  --accent-green-subtle: #e6f4ed;
  --accent-amber: #b45309;
  --accent-amber-subtle: #fef3c7;

  --danger: #c02a2a;
  --danger-hover: #9e2020;
  --danger-subtle: #fdecec;

  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 8px;

  --shadow-sm: 0 1px 2px rgba(15, 17, 20, 0.05);
  --shadow-modal: 0 12px 32px rgba(15, 17, 20, 0.22);
  --focus-ring: 0 0 0 2px rgba(201, 59, 43, 0.35);

  --font-sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "Source Han Sans SC", sans-serif;
  --font-mono: ui-monospace, "SFMono-Regular", "JetBrains Mono", Menlo, Consolas, monospace;
}

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

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  min-height: 100%;
  background-color: var(--bg-page);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow-x: hidden;
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Common layout & utility classes */
.hidden,
[hidden] {
  display: none !important;
}

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

.stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.spacer {
  flex: 1 1 auto;
}

.divider {
  height: 1px;
  width: 100%;
  background-color: var(--border-color);
  border: 0;
  margin: 12px 0;
}

.muted {
  color: var(--text-muted);
  font-size: 12px;
}

.mono {
  font-family: var(--font-mono);
  font-size: 12px;
  overflow-wrap: anywhere;
}

.error,
.help-error,
.inline-error {
  color: var(--danger);
  background-color: var(--danger-subtle);
  border: 1px solid rgba(192, 42, 42, 0.25);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.error:empty,
.help-error:empty,
.inline-error:empty {
  display: none;
}

h1,
h2,
h3,
h4,
p {
  margin: 0;
  letter-spacing: 0;
}

a {
  color: var(--text-primary);
  text-decoration: none;
}

a:hover {
  color: var(--accent-red);
}

/* SVG & Lucide Icon Sizing */
svg,
.icon svg {
  width: 18px;
  height: 18px;
  stroke-width: 1.85;
  flex-shrink: 0;
  vertical-align: middle;
}

.icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.spin,
svg.spin,
.icon svg.spin,
.lucide-loader-circle,
.status-badge.running svg,
.status-badge.queued svg {
  animation: wb-spin 0.85s linear infinite;
}

/* Buttons & Interactive Controls */
button,
input,
select,
textarea {
  font-family: inherit;
  font-size: 13px;
  line-height: 1.4;
  letter-spacing: 0;
  color: inherit;
}

.btn,
.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 34px;
  padding: 6px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  background-color: var(--bg-surface);
  color: var(--text-primary);
  font-weight: 500;
  font-size: 13px;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  transition: background-color 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}

.btn:hover:not(:disabled),
.icon-button:hover:not(:disabled) {
  background-color: var(--bg-subtle);
  border-color: #aeb5be;
}

.btn:focus-visible,
.icon-button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
.nav-item:focus-visible,
.segmented button:focus-visible,
.workflow-switch button:focus-visible,
.reference-upload:focus-visible,
.reference-option:focus-visible,
.scene-option:focus-visible,
.advanced-settings > summary:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-color: var(--accent-red);
}

.btn:disabled,
.icon-button:disabled,
input:disabled,
select:disabled,
textarea:disabled {
  opacity: 0.52;
  cursor: not-allowed;
}

.btn.primary {
  background-color: var(--accent-red);
  border-color: var(--accent-red);
  color: var(--text-inverse);
}

.btn.primary:hover:not(:disabled) {
  background-color: var(--accent-red-hover);
  border-color: var(--accent-red-hover);
}

.btn.secondary {
  background-color: var(--bg-surface);
  border-color: var(--border-strong);
  color: var(--text-primary);
}

.btn.secondary:hover:not(:disabled) {
  background-color: var(--bg-subtle);
}

.btn.danger {
  background-color: var(--bg-surface);
  border-color: rgba(192, 42, 42, 0.4);
  color: var(--danger);
}

.btn.danger:hover:not(:disabled) {
  background-color: var(--danger);
  border-color: var(--danger);
  color: var(--text-inverse);
}

.btn.small {
  min-height: 28px;
  padding: 4px 9px;
  font-size: 12px;
  border-radius: var(--radius-sm);
}

.btn.loading {
  position: relative;
  pointer-events: none;
  opacity: 0.8;
}

.btn.loading::after {
  content: "";
  width: 13px;
  height: 13px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  display: inline-block;
  animation: wb-spin 0.65s linear infinite;
}

.icon-button {
  width: 32px;
  min-width: 32px;
  height: 32px;
  min-height: 32px;
  padding: 0;
  border-color: var(--border-color);
  color: var(--text-secondary);
}

.icon-button:hover:not(:disabled) {
  color: var(--text-primary);
}

@keyframes wb-spin {
  to {
    transform: rotate(360deg);
  }
}

/* Form Inputs & Fields */
.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
}

.field > label,
.field label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.input,
.field input:not([type="checkbox"]):not([type="radio"]),
.field select,
.field textarea,
input[type="text"],
input[type="password"],
input[type="url"],
input[type="number"],
input[type="search"],
select,
textarea {
  width: 100%;
  min-height: 34px;
  padding: 6px 10px;
  background-color: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 13px;
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

textarea {
  resize: vertical;
  line-height: 1.55;
}

input[type="checkbox"] {
  width: 15px;
  height: 15px;
  margin: 0;
  accent-color: var(--accent-red);
  cursor: pointer;
  flex-shrink: 0;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px 12px;
}

.checkbox-row {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 13px;
  color: var(--text-primary);
  cursor: pointer;
  user-select: none;
}

/* Segmented Control */
.segmented {
  display: inline-grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  background-color: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 2px;
  gap: 2px;
}

.segmented button {
  min-height: 28px;
  padding: 4px 12px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  white-space: nowrap;
}

.segmented button:hover:not(.active):not(:disabled) {
  color: var(--text-primary);
  background-color: rgba(255, 255, 255, 0.55);
}

.segmented button.active {
  background-color: var(--bg-surface);
  color: var(--text-primary);
  font-weight: 600;
  box-shadow: var(--shadow-sm);
}

/* Login Page */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  background-color: var(--bg-page);
  background-image:
    linear-gradient(to right, rgba(21, 23, 26, 0.035) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(21, 23, 26, 0.035) 1px, transparent 1px);
  background-size: 24px 24px;
}

.login-panel {
  width: 100%;
  max-width: 380px;
  background-color: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-top: 3px solid var(--accent-red);
  border-radius: var(--radius-lg);
  padding: 28px 24px 24px;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.login-mark {
  width: 38px;
  height: 38px;
  border-radius: var(--radius-md);
  background-color: var(--bg-sidebar);
  color: var(--text-inverse);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.login-mark svg {
  width: 20px;
  height: 20px;
  color: var(--accent-red);
}

.login-panel h1 {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-primary);
}

.login-panel form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-panel .btn.primary {
  width: 100%;
  min-height: 38px;
}

/* Application Shell */
.app-shell {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  min-height: 100vh;
  width: 100%;
}

/* Sidebar */
.sidebar {
  width: 220px;
  background-color: var(--bg-sidebar);
  color: var(--text-sidebar);
  border-right: 1px solid var(--border-sidebar);
  display: flex;
  flex-direction: column;
  padding: 14px 10px;
  gap: 14px;
  user-select: none;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  border-bottom: 1px solid var(--border-sidebar);
  padding-bottom: 12px;
}

.brand-mark,
.brand .mark,
.brand > :first-child {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  background-color: var(--accent-red);
  color: var(--text-inverse);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  flex-shrink: 0;
}

.brand-text,
.brand .text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-inverse);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.brand-text strong {
  color: var(--text-inverse);
  font-size: 14px;
  font-weight: 600;
  line-height: 1.25;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.brand-sub {
  display: block;
  font-size: 11px;
  font-weight: 400;
  line-height: 1.2;
  color: var(--text-sidebar-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 3px;
  flex: 1 1 auto;
}

.nav-item {
  width: 100%;
  min-height: 36px;
  padding: 7px 10px;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--text-sidebar);
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: 13px;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease;
}

.nav-item:hover {
  background-color: var(--bg-sidebar-hover);
  color: var(--text-inverse);
}

.nav-item.active {
  background-color: var(--bg-sidebar-active);
  color: var(--text-inverse);
  border-left: 3px solid var(--accent-red);
  padding-left: 8px;
  font-weight: 600;
}

.nav-item span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-bottom {
  margin-top: auto;
  padding-top: 10px;
  border-top: 1px solid var(--border-sidebar);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.sidebar-bottom .btn,
.sidebar-bottom a {
  width: 100%;
  justify-content: flex-start;
  background-color: transparent;
  border-color: var(--border-sidebar);
  color: var(--text-sidebar);
  font-size: 12px;
  min-height: 32px;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.sidebar-bottom .btn:hover,
.sidebar-bottom a:hover {
  background-color: var(--bg-sidebar-hover);
  color: var(--text-inverse);
  border-color: #3a3f46;
}

/* Main Content Area */
.main-wrap {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 100vh;
  background-color: var(--bg-page);
}

.topbar {
  min-height: 52px;
  padding: 8px 20px;
  background-color: var(--bg-surface);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.topbar h1 {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

main#content {
  flex: 1 1 auto;
  padding: 16px 20px 24px;
  min-width: 0;
}

/* Workspace Grid: Composer + Output Panel */
.workspace-grid {
  display: grid;
  grid-template-columns: minmax(340px, 460px) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.composer,
.output-panel {
  background-color: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
}

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

.composer > .section-header {
  flex-wrap: wrap;
}

#routing-status {
  flex: 1 1 200px;
  min-width: 0;
  overflow-wrap: anywhere;
}

.section-header h2,
.section-header label {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-primary);
}

.resource-editor .section-header #resource-name {
  flex: 1 1 auto;
  min-width: 0;
  width: auto;
}

/* Worldbook / Multi-choice Checkboxes */
.choice-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 112px;
  overflow-y: auto;
  padding: 6px;
  background-color: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
}

.check-choice {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px;
  background-color: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  font-size: 12px;
  color: var(--text-primary);
  cursor: pointer;
  user-select: none;
  max-width: 100%;
}

.check-choice:hover {
  border-color: var(--border-strong);
}

.check-choice span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Prompt Editor */
.prompt-editor {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.prompt-editor textarea {
  min-height: 180px;
  font-family: var(--font-sans);
  font-size: 13px;
  line-height: 1.55;
  padding: 10px;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  flex-wrap: wrap;
}

.toolbar select,
#format {
  width: auto;
  min-width: 116px;
  flex: 0 0 auto;
}

.editor-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
  flex-wrap: wrap;
}

/* Multi-scene Output Switcher */
.scene-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 8px;
  width: 100%;
  min-width: 0;
}

.scene-list:empty {
  display: none;
}

.scene-option {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  width: 100%;
  min-height: 44px;
  padding: 8px 10px;
  background-color: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  text-align: left;
  cursor: pointer;
  min-width: 0;
  transition: border-color 0.12s ease, background-color 0.12s ease;
}

.scene-option:hover:not(:disabled) {
  background-color: var(--bg-surface);
  border-color: var(--border-strong);
}

.scene-option.active,
.scene-option[aria-pressed="true"] {
  background-color: var(--accent-red-subtle);
  border-color: var(--accent-red);
}

.scene-number {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
  line-height: 1.4;
  padding: 1px 5px;
  border-radius: var(--radius-sm);
  background-color: var(--bg-surface);
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  flex-shrink: 0;
}

.scene-option[aria-pressed="true"] .scene-number,
.scene-option.active .scene-number {
  background-color: var(--accent-red);
  border-color: var(--accent-red);
  color: var(--text-inverse);
}

.scene-option > span:not(.scene-number) {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 12px;
  line-height: 1.4;
  color: var(--text-primary);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
  white-space: normal;
}

/* Collapsible Advanced Parameters */
.advanced-settings {
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  background-color: var(--bg-subtle);
  min-width: 0;
}

.advanced-settings > summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border-radius: var(--radius-md);
}

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

.advanced-settings > summary::after {
  content: "+";
  font-family: var(--font-mono);
  font-size: 14px;
  font-weight: 600;
  line-height: 1;
  color: var(--text-muted);
}

.advanced-settings[open] > summary {
  border-bottom: 1px solid var(--border-color);
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  color: var(--text-primary);
}

.advanced-settings[open] > summary::after {
  content: "-";
}

.advanced-settings > .form-grid {
  padding: 10px 12px;
  background-color: var(--bg-surface);
  border-bottom-left-radius: var(--radius-md);
  border-bottom-right-radius: var(--radius-md);
}

/* Workflow Switch (4 modes: 4-col on wide, 2x2 on narrow) */
.workflow-switch {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  padding: 4px;
  background-color: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
}

.workflow-switch button {
  min-height: 36px;
  padding: 6px 8px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 0;
  white-space: nowrap;
  transition: background-color 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}

.workflow-switch button span:not(.icon) {
  overflow: hidden;
  text-overflow: ellipsis;
}

.workflow-switch button:hover:not(.active):not([aria-pressed="true"]):not(:disabled) {
  background-color: var(--bg-surface);
  color: var(--text-primary);
}

.workflow-switch button.active,
.workflow-switch button[aria-pressed="true"] {
  background-color: var(--bg-surface);
  border-color: var(--accent-red);
  color: var(--accent-red);
  font-weight: 600;
  box-shadow: var(--shadow-sm);
}

/* Reference Input & Preview */
.reference-input {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 12px;
  background-color: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  min-width: 0;
}

.reference-input > .section-header {
  padding-bottom: 6px;
}

.reference-upload {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  background-color: var(--bg-surface);
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: border-color 0.12s ease, background-color 0.12s ease, color 0.12s ease;
}

.reference-upload:hover:not(:disabled) {
  border-color: var(--accent-red);
  color: var(--accent-red);
  background-color: var(--accent-red-subtle);
}

.reference-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 8px 10px;
  background-color: var(--bg-subtle);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-md);
}

.reference-preview {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background-color: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  width: 100%;
  min-width: 0;
}

.reference-preview img {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  display: block;
  background-color: var(--bg-muted);
  flex-shrink: 0;
}

.reference-preview span {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  color: var(--text-primary);
}

.reference-preview .row {
  flex-wrap: nowrap;
  flex-shrink: 0;
  gap: 6px;
}

.generation-settings {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--border-color);
}

.actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding-top: 6px;
}

.actions .btn.primary {
  flex: 1 1 auto;
}

/* Output Panel */
.output-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-color);
  flex-wrap: wrap;
}

.output-head h2 {
  font-size: 13px;
  font-weight: 700;
}

.canvas {
  position: relative;
  width: 100%;
  min-height: 430px;
  background-color: var(--bg-canvas);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 12px;
}

.canvas.empty {
  background-color: var(--bg-subtle);
  border-style: dashed;
  border-color: var(--border-strong);
}

.canvas img,
.canvas video {
  max-width: 100%;
  max-height: 620px;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
  border-radius: var(--radius-sm);
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 6px;
  padding: 24px 16px;
  color: var(--text-muted);
  max-width: 320px;
}

.empty-state svg {
  width: 28px;
  height: 28px;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.empty-state h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
}

.empty-state p {
  font-size: 12px;
  color: var(--text-muted);
}

/* Status Badges */
.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  font-size: 11px;
  font-weight: 600;
  border: 1px solid var(--border-color);
  background-color: var(--bg-subtle);
  color: var(--text-secondary);
  white-space: nowrap;
}

.status-badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: currentColor;
}

.status-badge:has(svg)::before {
  display: none;
}

.status-badge svg {
  width: 13px;
  height: 13px;
}

.status-badge.running,
.status-badge.queued {
  background-color: var(--accent-amber-subtle);
  border-color: rgba(180, 83, 9, 0.3);
  color: var(--accent-amber);
}

.status-badge.succeeded {
  background-color: var(--accent-green-subtle);
  border-color: rgba(31, 122, 76, 0.3);
  color: var(--accent-green);
}

.status-badge.failed {
  background-color: var(--danger-subtle);
  border-color: rgba(192, 42, 42, 0.3);
  color: var(--danger);
}

.status-badge.interrupted {
  background-color: var(--accent-red-subtle);
  border-color: rgba(201, 59, 43, 0.3);
  color: var(--accent-red);
}

.output-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.output-info {
  padding: 10px 0 2px;
  background-color: transparent;
  border: 0;
  border-top: 1px solid var(--border-color);
  border-radius: 0;
  font-size: 12px;
  color: var(--text-secondary);
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-wrap: anywhere;
}

.output-info:empty {
  display: none;
}

/* Task Strip (Recent Private Tasks) */
.task-strip {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 16px;
  padding-top: 8px;
  border-top: 1px solid var(--border-color);
  min-width: 0;
}

.task-strip:empty {
  display: none;
}

.task-strip > .row {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 8px;
  width: 100%;
}

.mini-task {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background-color: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  cursor: pointer;
  min-width: 0;
  text-align: left;
  transition: border-color 0.12s ease, background-color 0.12s ease;
}

.mini-task:hover {
  background-color: var(--bg-surface);
  border-color: var(--border-strong);
}

.mini-task.active {
  background-color: var(--bg-surface);
  border-color: var(--accent-red);
}

.mini-task img,
.mini-task video {
  width: 36px;
  height: 36px;
  object-fit: cover;
  border-radius: 3px;
  background-color: var(--bg-muted);
  flex-shrink: 0;
}

.mini-task span {
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Resource Management Pages (Connections, Presets, Worldbooks) */
.page-section {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
}

.page-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border-color);
}

.page-heading h2 {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
}

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

.resource-list {
  width: 280px;
  background-color: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.resource-list > p.muted {
  padding: 14px 12px;
  text-align: center;
}

.resource-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-color);
  background-color: var(--bg-surface);
  cursor: pointer;
  min-width: 0;
  transition: background-color 0.12s ease;
}

.resource-row:last-child {
  border-bottom: 0;
}

.resource-row:hover {
  background-color: var(--bg-subtle);
}

.resource-row.active {
  background-color: var(--accent-red-subtle);
  border-left: 3px solid var(--accent-red);
  padding-left: 9px;
}

.resource-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1 1 auto;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 0;
  cursor: pointer;
}

.resource-main strong,
.resource-main span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.resource-editor {
  background-color: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
}

/* Flat Entry Blocks inside Resource Editor (no nested cards) */
.entry-block {
  padding: 12px 0;
  border-top: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: transparent;
  border-radius: 0;
  box-shadow: none;
}

.entry-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.entry-top input:not([type="checkbox"]):not([type="radio"]) {
  flex: 1 1 160px;
  min-width: 0;
  width: auto;
}

.entry-fields {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Task History View */
.filter-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 12px;
  background-color: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
}

#history-search {
  flex: 1 1 200px;
  max-width: 320px;
  width: auto;
}

.history-list,
.bundle-list,
.artist-list {
  display: flex;
  flex-direction: column;
  background-color: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.bundle-list:empty,
.artist-list:empty,
.anchor-grid:empty {
  display: none;
}

.history-row {
  display: grid;
  grid-template-columns: 64px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-color);
}

.bundle-list .history-row,
.artist-list .history-row,
.history-row:not(:has(.thumb)) {
  grid-template-columns: minmax(0, 1fr) auto;
}

.history-row:last-child {
  border-bottom: 0;
}

.history-row:hover {
  background-color: var(--bg-subtle);
}

.thumb {
  width: 64px;
  height: 64px;
  border-radius: var(--radius-sm);
  background-color: var(--bg-muted);
  border: 1px solid var(--border-color);
  object-fit: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  flex-shrink: 0;
}

.thumb img,
.thumb video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.history-detail {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.history-detail h3 {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-prompt {
  font-size: 13px;
  color: var(--text-primary);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
}

.row-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

/* Configurations Page: Character Anchors & Bundles */
.anchor-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px;
  width: 100%;
  min-width: 0;
}

.anchor-item {
  display: grid;
  grid-template-columns: 64px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 12px;
  background-color: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  min-width: 0;
  transition: border-color 0.12s ease, background-color 0.12s ease;
}

.anchor-item:hover {
  border-color: var(--border-strong);
  background-color: var(--bg-subtle);
}

.anchor-portrait {
  width: 64px;
  height: 64px;
  border-radius: var(--radius-sm);
  background-color: var(--bg-muted);
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  object-fit: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  flex-shrink: 0;
}

img.anchor-portrait,
.anchor-portrait img {
  width: 64px;
  height: 64px;
  object-fit: cover;
  display: block;
}

.anchor-detail {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.anchor-detail h3 {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.anchor-detail p {
  font-size: 12px;
  line-height: 1.45;
  color: var(--text-secondary);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
}

.anchor-detail small {
  font-size: 11px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#reference-library {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#reference-library:empty {
  display: none;
}

#reference-library .history-row {
  background-color: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
}

/* Modal / Dialog */
dialog#modal {
  width: min(640px, calc(100vw - 24px));
  max-height: calc(100vh - 32px);
  margin: auto;
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  background-color: var(--bg-surface);
  color: var(--text-primary);
  box-shadow: var(--shadow-modal);
  overflow: hidden;
}

dialog#modal[open] {
  display: flex;
  flex-direction: column;
}

dialog#modal::backdrop,
.modal-backdrop {
  background-color: rgba(15, 17, 20, 0.55);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  background-color: var(--bg-subtle);
}

.modal-header h2,
.modal-header h3 {
  font-size: 14px;
  font-weight: 700;
}

.modal-body {
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1 1 auto;
}

.modal-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid var(--border-color);
  background-color: var(--bg-subtle);
}

.confirm-copy {
  font-size: 13px;
  color: var(--text-primary);
  line-height: 1.55;
  overflow-wrap: anywhere;
}

/* Bundle Import Summary & Warnings inside Dialog */
.import-summary {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  width: 100%;
  min-width: 0;
}

.import-summary > div {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 10px 8px;
  background-color: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  text-align: center;
  min-width: 0;
}

.import-summary strong {
  font-size: 18px;
  font-weight: 700;
  line-height: 1.2;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

.import-summary span {
  font-size: 12px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.import-warnings {
  margin: 0;
  padding: 8px 12px 8px 26px;
  background-color: var(--accent-amber-subtle);
  border: 1px solid rgba(180, 83, 9, 0.3);
  border-radius: var(--radius-sm);
  color: var(--accent-amber);
  font-size: 12px;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow-wrap: anywhere;
}

.import-warnings:empty {
  display: none;
}

/* Character Modal Outfit Rows */
.outfit-row {
  display: grid;
  grid-template-columns: minmax(110px, 160px) minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  min-width: 0;
}

/* Reference Picker inside Modal */
.reference-picker {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(124px, 1fr));
  gap: 10px;
  width: 100%;
  min-width: 0;
}

.reference-option {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  padding: 6px;
  background-color: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  text-align: left;
  cursor: pointer;
  min-width: 0;
  transition: border-color 0.12s ease, background-color 0.12s ease;
}

.reference-option:hover:not(:disabled) {
  background-color: var(--bg-subtle);
  border-color: var(--accent-red);
}

.reference-option.active,
.reference-option[aria-pressed="true"] {
  border-color: var(--accent-red);
  background-color: var(--accent-red-subtle);
}

.reference-option img {
  width: 100%;
  aspect-ratio: 1 / 1;
  height: auto;
  max-height: 120px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  background-color: var(--bg-muted);
  display: block;
  flex-shrink: 0;
}

.reference-option span {
  font-size: 12px;
  line-height: 1.35;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

/* Previews & JSON Code Editor */
pre.preview-text {
  margin: 0;
  padding: 12px;
  background-color: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.55;
  color: var(--text-primary);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 380px;
  overflow-y: auto;
}

.code-editor textarea,
textarea.code-editor {
  min-height: 240px;
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.55;
  background-color: #16181b;
  color: #e6edf3;
  border-color: #2f353d;
  padding: 10px 12px;
}

/* Toasts */
#toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: min(360px, calc(100vw - 32px));
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  background-color: var(--bg-sidebar);
  color: var(--text-inverse);
  border-left: 4px solid var(--border-strong);
  box-shadow: var(--shadow-modal);
  font-size: 13px;
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.toast.success {
  border-left-color: var(--accent-green);
}

.toast.error {
  border-left-color: var(--danger);
}

/* Medium Viewport Layout (800px - 1099px): stack workspace columns & compact sidebar */
@media (max-width: 1099px) {
  .app-shell {
    grid-template-columns: 188px minmax(0, 1fr);
  }

  .sidebar {
    width: 188px;
  }

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

/* Responsive Mobile Layout (<800px) */
@media (max-width: 799px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .sidebar {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas:
      "brand bottom"
      "nav nav";
    align-items: center;
    padding: 8px 10px;
    gap: 6px 8px;
    border-right: 0;
    border-bottom: 1px solid var(--border-sidebar);
    overflow: hidden;
  }

  .brand {
    grid-area: brand;
    border-bottom: 0;
    padding: 0;
    min-width: 0;
  }

  .brand-sub {
    display: none;
  }

  .nav {
    grid-area: nav;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    gap: 4px;
    width: 100%;
    padding-top: 6px;
    border-top: 1px solid var(--border-sidebar);
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .nav::-webkit-scrollbar {
    display: none;
  }

  .nav-item {
    flex: 1 0 60px;
    width: auto;
    min-height: 44px;
    padding: 4px 6px;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 2px;
    text-align: center;
    font-size: 11px;
    white-space: nowrap;
  }

  .nav-item .icon {
    width: 18px;
    height: 18px;
  }

  .nav-item span:not(.icon) {
    max-width: 100%;
    font-size: 11px;
    line-height: 1.15;
  }

  .nav-item.active {
    border-left: 1px solid transparent;
    border-bottom: 2px solid var(--accent-red);
    padding-left: 6px;
  }

  .sidebar-bottom {
    grid-area: bottom;
    margin-top: 0;
    padding-top: 0;
    border-top: 0;
    flex-direction: row;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
  }

  .sidebar-bottom .btn,
  .sidebar-bottom a,
  .sidebar-bottom .nav-item {
    width: auto;
    min-height: 44px;
    padding: 4px 8px;
    flex-direction: row;
    gap: 4px;
    font-size: 12px;
  }

  .topbar {
    padding: 10px 12px;
  }

  main#content {
    padding: 12px;
  }

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

  .workflow-switch {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

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

  .canvas {
    min-height: 240px;
  }

  .resource-layout {
    grid-template-columns: 1fr;
  }

  .resource-list {
    width: 100%;
    max-height: 220px;
    overflow-y: auto;
  }

  .history-row {
    grid-template-columns: 64px minmax(0, 1fr);
  }

  .bundle-list .history-row,
  .artist-list .history-row,
  .history-row:not(:has(.thumb)) {
    grid-template-columns: minmax(0, 1fr);
  }

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

  .anchor-item {
    grid-template-columns: 56px minmax(0, 1fr);
  }

  .anchor-item > .row {
    grid-column: 1 / -1;
    justify-content: flex-start;
  }

  .import-summary {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .outfit-row {
    grid-template-columns: 1fr auto;
  }

  .outfit-row > input:first-child {
    grid-column: 1 / -1;
  }

  .scene-list {
    grid-template-columns: 1fr;
  }

  #history-search {
    max-width: 100%;
    flex-basis: 100%;
  }

  .row-actions {
    grid-column: 1 / -1;
    justify-content: flex-start;
    padding-top: 4px;
  }

  .reference-picker {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  /* Touch target >= 44px & input 16px on mobile to prevent iOS zoom */
  .btn,
  .btn.small,
  .icon-button,
  .segmented button,
  .workflow-switch button,
  .reference-upload,
  .resource-row,
  .mini-task,
  .check-choice,
  .scene-option,
  .advanced-settings > summary {
    min-height: 44px;
  }

  .icon-button {
    width: 44px;
    min-width: 44px;
    height: 44px;
  }

  .input,
  .field input:not([type="checkbox"]):not([type="radio"]),
  .field select,
  .field textarea,
  input[type="text"],
  input[type="password"],
  input[type="url"],
  input[type="number"],
  input[type="search"],
  select,
  textarea {
    min-height: 44px;
    font-size: 16px;
  }
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
