/* Custom variables for Novel AI Manager */
:root {
  --primary-color: #6366f1;
  --primary-hover: #4f46e5;
  --bg-sidebar: #0f172a;
  --bg-sidebar-hover: #1e293b;
  --text-sidebar: #94a3b8;
  --text-sidebar-active: #ffffff;
  --border-sidebar: #1e293b;
  --topbar-height: 70px;
  --sidebar-width: 260px;
}

body {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background-color: #f8fafc;
  color: #1e293b;
  margin: 0;
  padding: 0;
}

.auth-page { min-height: 100vh; background: #eef2ff; }
.auth-shell { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.auth-card { width: min(100%, 430px); padding: 2rem; background: #fff; border: 1px solid #e2e8f0; border-radius: 18px; box-shadow: 0 24px 60px rgba(15, 23, 42, .12); }
.auth-brand { display: flex; align-items: center; margin-bottom: 2rem; font-size: 1.1rem; font-weight: 800; }
.auth-card h1 { margin-bottom: .5rem; font-size: 1.5rem; font-weight: 750; color: #0f172a; }
.btn-primary { --bs-btn-bg: var(--primary-color); --bs-btn-border-color: var(--primary-color); --bs-btn-hover-bg: var(--primary-hover); --bs-btn-hover-border-color: var(--primary-hover); }
.form-control:focus { border-color: #a5b4fc; box-shadow: 0 0 0 .2rem rgba(99, 102, 241, .14); }
.page-heading { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: 1.5rem; }
.page-heading h2 { margin: 0 0 .35rem; font-size: 1.65rem; font-weight: 750; color: #0f172a; }
.page-heading p { margin: 0; color: #64748b; }
.page-actions { flex-wrap: wrap; justify-content: flex-end; }
.eyebrow { margin-bottom: .35rem !important; color: var(--primary-color) !important; font-size: .75rem; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.metric-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.workspace-filter { display: grid; grid-template-columns: minmax(180px, 1fr) minmax(180px, 1fr) 140px auto auto; gap: .75rem; margin-bottom: 1rem; }
.workspace-table td { vertical-align: middle; }
.workspace-table td:first-child small { display: block; color: #64748b; }
.workspace-table tbody tr { cursor: pointer; }
.agent-readiness { margin: 0; color: #64748b; font-size: .78rem; }
.agent-run-config { display: grid; gap: .35rem; padding: .75rem; border: 1px solid #e2e8f0; border-radius: 8px; background: #f8fafc; }
.agent-run-config small { color: #64748b; }
.ai-agent-settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .85rem; }
.ai-agent-setting-card { display: grid; gap: .55rem; align-content: start; padding: 1rem; border: 1px solid #dbe3ec; border-radius: 12px; background: #f8fafc; }
.ai-agent-setting-card > header { display: flex; align-items: flex-start; justify-content: space-between; gap: .75rem; }
.ai-agent-setting-card h4 { margin: .12rem 0 0; color: #1e293b; font-size: 1rem; }
.ai-agent-setting-card > p { min-height: 2.8em; margin: 0; color: #64748b; font-size: .8rem; line-height: 1.4; }
.agent-model-advice { display: grid; gap: .15rem; min-height: 72px; padding: .65rem; border-left: 3px solid #22c55e; border-radius: 6px; background: #f0fdf4; }
.agent-model-advice strong { color: #166534; font-size: .74rem; }
.agent-model-advice span, .agent-model-note { color: #64748b; font-size: .73rem; line-height: 1.4; }
.agent-model-note { display: block; }
.agent-effective-config { display: flex; align-items: center; justify-content: space-between; gap: .7rem; padding: .5rem .6rem; border: 1px solid #e2e8f0; border-radius: 6px; background: #fff; font-size: .72rem; }
.agent-effective-config span { color: #64748b; }
.agent-effective-config strong { color: #334155; text-align: right; }
.loading-overlay { position: fixed; inset: 0; z-index: 9999; display: none; flex-direction: column; align-items: center; justify-content: center; padding: 1.5rem; color: #fff; text-align: center; background: rgba(31, 41, 55, .78); backdrop-filter: blur(2px); }
.loading-overlay.show { display: flex; }
.loading-overlay .spinner-border { width: 3rem; height: 3rem; }
.loading-overlay h5 { margin: 0 0 .4rem; font-size: 1.05rem; }
.loading-overlay p, .loading-overlay small { margin: 0; opacity: .78; }
.workflow-notice { margin-bottom: .75rem; }
.workflow-guide { display: flex; flex-wrap: wrap; gap: .4rem .75rem; margin-bottom: .75rem; padding: .65rem .75rem; border-left: 3px solid #6366f1; background: #f8fafc; color: #475569; font-size: .78rem; }
.workflow-guide strong { color: #1e293b; }
.workflow-guide small { width: 100%; color: #64748b; line-height: 1.45; }
.workflow-guide-clear { border-left-color: #0ea5e9; background: #f0f9ff; }
.plan-history { margin: .75rem 0; padding: .7rem .8rem; border: 1px solid #dbe3ec; border-radius: 6px; background: #f8fafc; }
.plan-history summary { cursor: pointer; font-weight: 600; }
.plan-history-list { display: grid; gap: .5rem; margin: .7rem 0; }
.plan-history-list span, .plan-history > small { color: #64748b; font-size: .82rem; }
.plan-version-detail { overflow: hidden; border: 1px solid #dbe3ec; border-radius: 6px; background: #fff; }
.plan-version-detail > summary { display: flex; align-items: center; justify-content: space-between; gap: .75rem; padding: .65rem .75rem; cursor: pointer; list-style-position: inside; }
.plan-version-detail > summary span { display: inline-grid; gap: .15rem; color: #1e293b; }
.plan-version-detail > summary small { color: #64748b; font-weight: 400; }
.plan-version-detail > summary em { flex: 0 0 auto; color: #475569; font-size: .75rem; font-style: normal; font-weight: 700; }
.archived-plan-scenes { display: grid; gap: .55rem; padding: .7rem; border-top: 1px solid #e2e8f0; }
.archived-plan-overview { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .5rem; }
.archived-plan-overview > div { padding: .6rem; border: 1px solid #e2e8f0; background: #fff; }
.archived-plan-overview p { margin-bottom: 0; }
.archived-plan-overview ul { margin: .35rem 0 0; padding-left: 1.1rem; color: #475569; font-size: .78rem; }
.archived-plan-scenes article { padding: .65rem; border-left: 3px solid #c7d2fe; background: #f8fafc; }
.archived-plan-scenes header { display: flex; justify-content: space-between; gap: .75rem; }
.archived-plan-scenes header small { color: #64748b; }
.archived-plan-scenes p { margin: .35rem 0; color: #475569; font-size: .8rem; }
.archived-plan-scenes ol { display: grid; gap: .35rem; margin: .55rem 0 0; padding-left: 1.35rem; }
.archived-plan-scenes li { padding-left: .2rem; color: #475569; font-size: .78rem; }
.archived-plan-scenes li strong { margin-right: .35rem; color: #334155; }
.metric-card, .content-card { border: 1px solid #e2e8f0; border-radius: 14px; background: #fff; box-shadow: 0 6px 20px rgba(15, 23, 42, .04); }
.metric-card { display: flex; align-items: center; gap: 1rem; padding: 1.2rem; }
.metric-icon { display: grid; width: 42px; height: 42px; place-items: center; border-radius: 10px; background: #eef2ff; color: var(--primary-color); }
.metric-icon svg { width: 20px; height: 20px; }
.metric-card strong, .metric-card span { display: block; }
.metric-card strong { font-size: 1.35rem; color: #0f172a; }
.metric-card div > span { color: #64748b; font-size: .82rem; }
.content-card { padding: 1.25rem; }
.content-card h3 { font-size: 1rem; font-weight: 700; }
.fs-xs { font-size: .75rem; }
.fw-600 { font-weight: 600; }
.empty-state { padding: 3rem 1.5rem; text-align: center; }
.empty-state > svg { width: 42px; height: 42px; margin-bottom: 1rem; color: var(--primary-color); }
.empty-state p { color: #64748b; }
.novel-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1rem; }
.novel-card { display: grid; grid-template-columns: 112px 1fr; min-height: 168px; overflow: hidden; border: 1px solid #e2e8f0; border-radius: 8px; background: #fff; color: inherit; text-decoration: none; transition: border-color .15s ease, box-shadow .15s ease; }
.novel-card:hover, .novel-card:focus-visible { border-color: #a5b4fc; color: inherit; box-shadow: 0 8px 24px rgba(15, 23, 42, .1); outline: none; }
.novel-cover { display: grid; min-height: 100%; place-items: center; overflow: hidden; background: linear-gradient(145deg, #e0e7ff, #f5f3ff); color: var(--primary-color); }
.novel-cover img { width: 100%; height: 100%; object-fit: cover; }
.novel-cover svg { width: 34px; height: 34px; }
.novel-card-body { min-width: 0; padding: 1rem; }
.novel-card-body h3 { margin: .7rem 0 .35rem; font-size: 1rem; }
.novel-card-body h3 a { color: #0f172a; text-decoration: none; }
.novel-card-body p { overflow: hidden; margin-bottom: 1rem; color: #64748b; font-size: .82rem; text-overflow: ellipsis; white-space: nowrap; }
.novel-stats { display: flex; flex-wrap: wrap; gap: .75rem; color: #64748b; font-size: .75rem; }
.status-badge { display: inline-flex; padding: .2rem .5rem; border-radius: 999px; background: #f1f5f9; color: #475569; font-size: .68rem; font-weight: 700; }
.status-active, .status-published { background: #dcfce7; color: #166534; }
.status-hiatus { background: #fef3c7; color: #92400e; }
.status-completed { background: #dbeafe; color: #1e40af; }
.status-draft, .status-planning { background: #f1f5f9; color: #475569; }
.status-reviewed { background: #fef3c7; color: #92400e; }
.status-approved { background: #dbeafe; color: #1e40af; }
.status-running, .status-queued { background: #dbeafe; color: #1e40af; }
.status-completed, .status-success { background: #dcfce7; color: #166534; }
.status-failed { background: #fee2e2; color: #991b1b; }
.novel-form { padding: 1.5rem; }
.form-section { padding-bottom: 1.5rem; margin-bottom: 1.5rem; border-bottom: 1px solid #e2e8f0; }
.form-section h3 { margin-bottom: 1rem; }
.form-actions { display: flex; justify-content: flex-end; gap: .75rem; padding-top: 1.5rem; }
.wizard-stepper { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: .75rem; margin-bottom: 1.5rem; }
.wizard-step { display: flex; align-items: center; gap: .75rem; min-width: 0; padding: .8rem; border: 1px solid #e2e8f0; border-radius: 8px; background: #fff; color: #64748b; text-align: left; }
.wizard-step span { display: grid; flex: 0 0 auto; width: 30px; height: 30px; place-items: center; border-radius: 50%; background: #f1f5f9; color: #64748b; font-size: .78rem; font-weight: 800; }
.wizard-step strong { min-width: 0; overflow: hidden; font-size: .82rem; text-overflow: ellipsis; white-space: nowrap; }
.wizard-step.active { border-color: #a5b4fc; background: #eef2ff; color: var(--primary-color); }
.wizard-step.active span, .wizard-step.done span { background: var(--primary-color); color: #fff; }
.wizard-step.done { color: #334155; }
.novel-detail-head { display: grid; grid-template-columns: 180px 1fr; gap: 1.5rem; margin-bottom: 1rem; padding: 1.5rem; }
.novel-cover-large { width: 180px; min-height: 250px; border-radius: 10px; }
.novel-meta { display: flex; flex-wrap: wrap; gap: 1.5rem; padding-top: 1rem; color: #64748b; }
.novel-meta strong { display: block; color: #0f172a; font-size: 1.2rem; }
.detail-tabs { display: flex; gap: .25rem; overflow-x: auto; margin-bottom: 1rem; padding: .5rem; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
.detail-tabs a, .detail-tabs span, .detail-tabs button { flex: 0 0 auto; padding: .55rem .75rem; border: 0; border-radius: 8px; color: #64748b; background: transparent; font-size: .78rem; text-decoration: none; }
.detail-tabs .active { background: #eef2ff; color: var(--primary-color); font-weight: 700; }
.detail-list { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin: 0; }
.chapter-detail-list { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.detail-list div { padding: 1rem; border-radius: 10px; background: #f8fafc; }
.detail-list dt { margin-bottom: .3rem; color: #64748b; font-size: .72rem; text-transform: uppercase; }
.detail-list dd { margin: 0; color: #0f172a; font-weight: 600; }
.danger-zone { padding-top: 1.5rem; margin-top: 1.5rem; border-top: 1px solid #fee2e2; }
.agent-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1rem; }
.agent-card { display: flex; flex-direction: column; gap: 1rem; }
.agent-card-head { display: flex; justify-content: space-between; gap: 1rem; align-items: flex-start; }
.agent-card-head h3 { margin-bottom: .25rem; }
.agent-card-head p { margin: 0; color: #64748b; font-size: .82rem; }
.agent-run-meta { display: grid; grid-template-columns: repeat(3, 1fr); gap: .5rem; margin: 0; }
.agent-run-meta div { padding: .65rem; border-radius: 8px; background: #f8fafc; }
.agent-run-meta dt { color: #64748b; font-size: .68rem; text-transform: uppercase; }
.agent-run-meta dd { margin: 0; color: #0f172a; font-size: .78rem; font-weight: 700; word-break: break-word; }
.agent-output { max-height: 280px; min-height: 120px; overflow: auto; padding: .85rem; border: 1px solid #e2e8f0; border-radius: 10px; background: #0f172a; color: #e2e8f0; font-size: .78rem; white-space: pre-wrap; }
.agent-history { display: grid; gap: .65rem; }
.agent-history article { display: grid; grid-template-columns: 180px 1fr auto; gap: 1rem; align-items: center; padding: .8rem; border: 1px solid #e2e8f0; border-radius: 10px; }
.agent-history small { color: #64748b; }
.credential-selector-form { display: grid; grid-template-columns: minmax(220px, .8fr) minmax(300px, 1.4fr) auto; gap: .75rem; align-items: end; }
.credential-selection-info { grid-column: 1 / -1; min-height: 2.5rem; padding: .65rem .75rem; border-left: 3px solid #6366f1; background: #f8fafc; color: #475569; font-size: .82rem; }
.credential-status-list { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .5rem; margin-top: 1rem; }
.credential-status-list > div { display: flex; justify-content: space-between; align-items: center; gap: .5rem; padding: .6rem .7rem; border: 1px solid #e2e8f0; border-radius: 6px; }
.credential-status-list strong { overflow: hidden; font-size: .78rem; text-overflow: ellipsis; white-space: nowrap; }
.provider-settings-list { display: grid; gap: 1rem; }
.provider-settings-card { overflow: hidden; padding: 0; }
.provider-settings-head { display: flex; justify-content: space-between; gap: 1rem; align-items: center; padding: 1rem 1.25rem; cursor: pointer; list-style: none; }
.provider-settings-head::-webkit-details-marker { display: none; }
.provider-settings-head > span { display: flex; gap: .65rem; align-items: center; }
.provider-settings-head svg { width: 18px; transition: transform .15s ease; }
.provider-settings-card[open] .provider-settings-head svg { transform: rotate(180deg); }
.provider-settings-head h3 { margin-bottom: .25rem; }
.provider-settings-head p { margin: 0; color: #64748b; font-size: .82rem; }
.provider-settings-body { padding: 1rem 1.25rem 1.25rem; border-top: 1px solid #e2e8f0; }
.provider-active-control { margin-bottom: 1rem; }
.module-option-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .65rem; }
.module-option { position: relative; display: flex; gap: .7rem; padding: .85rem; border: 1px solid #e2e8f0; border-radius: 8px; }
.module-option span, .module-option small { display: block; }
.module-option small { margin-top: .2rem; color: #64748b; }
.module-option em { position: absolute; top: .6rem; right: .7rem; color: #166534; font-size: .68rem; font-style: normal; font-weight: 700; }

/* App Shell Layout */
.app-shell {
  display: flex;
  min-height: 100vh;
  position: relative;
  overflow-x: hidden;
}

/* Sidebar Styling */
.app-sidebar {
  width: var(--sidebar-width);
  background-color: var(--bg-sidebar);
  border-right: 1px solid var(--border-sidebar);
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
  transition: transform 0.3s ease;
}

.app-sidebar--dark {
  background-color: #0f172a;
}

.sidebar-brand {
  height: var(--topbar-height);
  display: flex;
  align-items: center;
  padding: 0 1.5rem;
  font-size: 1.25rem;
  font-weight: 700;
  color: #ffffff;
  text-decoration: none;
  border-bottom: 1px solid var(--border-sidebar);
}

.brand-mark {
  background: linear-gradient(135deg, #6366f1, #a855f7);
  color: white;
  padding: 0.25rem 0.6rem;
  border-radius: 6px;
  font-size: 0.9rem;
  margin-right: 0.5rem;
  font-weight: 800;
}

.text-brand {
  color: var(--primary-color);
}

.sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 0.75rem;
}

.sidebar-scroll::-webkit-scrollbar {
  width: 5px;
}
.sidebar-scroll::-webkit-scrollbar-thumb {
  background: #334155;
  border-radius: 3px;
}

.nav-link {
  display: flex;
  align-items: center;
  padding: 0.75rem 1rem;
  color: var(--text-sidebar);
  text-decoration: none;
  border-radius: 8px;
  margin-bottom: 0.25rem;
  font-size: 0.9rem;
  font-weight: 500;
  transition: all 0.2s ease;
}

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

.nav-link.active {
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.15), rgba(168, 85, 247, 0.15));
  border-left: 3px solid var(--primary-color);
  color: var(--text-sidebar-active);
  font-weight: 600;
}

.nav-link i, .nav-link svg {
  width: 18px;
  height: 18px;
  margin-right: 0.75rem;
  stroke-width: 2px;
}

.nav-section {
  margin-top: 1.5rem;
}

.nav-section-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: #475569;
  padding: 0.25rem 1rem 0.5rem;
}

/* App Content Area */
.app-content {
  flex: 1;
  margin-left: var(--sidebar-width);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: #f8fafc;
  transition: margin-left 0.3s ease;
}

/* Topbar Styling */
.app-topbar {
  height: var(--topbar-height);
  background-color: #ffffff;
  border-bottom: 1px solid #e2e8f0;
  display: flex;
  align-items: center;
  padding: 0 1.5rem;
  position: sticky;
  top: 0;
  z-index: 999;
}

.sidebar-toggle {
  background: none;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #64748b;
  margin-right: 1rem;
}

.sidebar-toggle:hover {
  background-color: #f1f5f9;
}

.topbar-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: #0f172a;
  margin: 0;
}

.topbar-user {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.25rem 0.5rem;
  border-radius: 8px;
  color: #1e293b;
  text-decoration: none !important;
  transition: background-color 0.2s;
}

.topbar-user:hover {
  background-color: #f1f5f9;
}

.avatar-initial {
  background: linear-gradient(135deg, #6366f1, #a855f7);
  color: white;
  font-weight: 600;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.user-meta {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.u-name {
  font-size: 0.875rem;
  font-weight: 600;
  color: #0f172a;
}

.u-role {
  font-size: 0.75rem;
  color: #64748b;
}

/* App Body Area */
.app-body {
  padding: 1.5rem;
  flex: 1;
}

/* Footer */
.app-footer {
  padding: 1rem 1.5rem;
  background-color: #ffffff;
  border-top: 1px solid #e2e8f0;
  text-align: center;
  font-size: 0.85rem;
  color: #64748b;
}

/* Sidebar Backdrop */
.sidebar-backdrop {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(15, 23, 42, 0.4);
  backdrop-filter: blur(4px);
  z-index: 998;
}

/* Responsive Handling */
@media (max-width: 991.98px) {
  .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .novel-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .detail-list { grid-template-columns: repeat(2, 1fr); }
  .app-sidebar {
    transform: translateX(-100%);
  }
  .app-content {
    margin-left: 0;
  }
  .app-shell.sidebar-open .app-sidebar {
    transform: translateX(0);
  }
  .app-shell.sidebar-open .sidebar-backdrop {
    display: block;
  }
}

.character-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1rem; }
.character-card { display: grid; grid-template-columns: 88px 1fr; gap: 1rem; align-items: center; margin: 0; }
.card-link { color: inherit; text-decoration: none; cursor: pointer; transition: border-color .15s ease, box-shadow .15s ease; }
.card-link:hover, .card-link:focus-visible { color: inherit; border-color: #94a3b8; box-shadow: 0 4px 14px rgba(15, 23, 42, .08); }
.card-title, .card-description { display: block; }
.card-title { margin: .45rem 0 .15rem; color: #0f172a; font-size: 1rem; }
.card-description { margin-bottom: .35rem; color: #64748b; }
.character-card h3 { margin: .45rem 0 .15rem; }
.character-card h3 a { color: #0f172a; text-decoration: none; }
.character-card p { margin-bottom: .35rem; color: #64748b; }
.character-avatar { width: 88px; height: 88px; border-radius: 50%; overflow: hidden; background: #eef2ff; color: #6366f1; display: flex; align-items: center; justify-content: center; }
.character-avatar img { width: 100%; height: 100%; object-fit: cover; }
.character-avatar svg { width: 36px; height: 36px; }
.character-avatar-large { width: 150px; height: 150px; }
.character-detail { display: grid; grid-template-columns: 170px 1fr; gap: 1.5rem; align-items: center; }
.bible-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.bible-grid article { padding: 1rem; border: 1px solid #e2e8f0; border-radius: 10px; }
.bible-grid h4 { font-size: 1rem; margin-bottom: .5rem; }
.bible-grid p { margin: 0; color: #475569; }
.linked-novels { display: grid; gap: .6rem; }
.linked-novels a { display: flex; justify-content: space-between; padding: .8rem 1rem; border: 1px solid #e2e8f0; border-radius: 8px; color: #0f172a; text-decoration: none; }
.linked-novels span { color: #64748b; }

@media (max-width: 575.98px) {
  .app-body { padding: 1rem; }
  .metric-grid { grid-template-columns: 1fr; }
  .auth-card { padding: 1.35rem; }
  .page-heading { gap: 1rem; }
  .novel-grid { grid-template-columns: 1fr; }
  .novel-detail-head { grid-template-columns: 1fr; }
  .novel-cover-large { width: 130px; min-height: 180px; }
  .detail-list { grid-template-columns: 1fr; }
  .character-grid, .bible-grid { grid-template-columns: 1fr; }
  .character-detail { grid-template-columns: 1fr; }
}

.context-audit-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.context-audit-card { min-width: 0; margin: 0; }
.context-audit-card.empty { opacity: .7; }
.context-audit-card pre { max-height: 360px; overflow: auto; margin: 0; padding: .8rem; border-radius: 8px; background: #0f172a; color: #e2e8f0; font-size: .72rem; white-space: pre-wrap; word-break: break-word; }
@media (max-width: 991.98px) { .context-audit-grid { grid-template-columns: 1fr; } }

@media (min-width: 576px) and (max-width: 1199.98px) { .character-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

.filter-bar { display: flex; align-items: center; gap: .75rem; }
.filter-bar .form-select { width: min(360px, 100%); }
.relationship-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.relationship-card { margin: 0; }
.relationship-people, .relationship-heading { display: flex; align-items: center; justify-content: space-between; gap: .75rem; }
.relationship-people { font-weight: 700; color: #0f172a; }
.relationship-people svg { width: 18px; color: #6366f1; }
.relationship-heading { margin: 1rem 0 .6rem; }
.relationship-card h3 { font-size: 1rem; }
.relationship-card h3 a { color: #334155; text-decoration: none; }
.tension-meter { display: block; width: 100%; height: 7px; margin: .8rem 0 .35rem; border-radius: 99px; overflow: hidden; background: #e2e8f0; }
.tension-meter > span { display: block; height: 100%; min-width: 2px; border-radius: inherit; background: linear-gradient(90deg, #22c55e, #f59e0b, #ef4444); }
.relationship-detail-head { display: grid; grid-template-columns: 1fr 180px 1fr; align-items: center; text-align: center; gap: 1rem; }
.relationship-detail-head > div { display: flex; flex-direction: column; align-items: center; gap: .75rem; }
.relationship-core svg { color: #6366f1; }
.relationship-directions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.relationship-directions .content-card { margin: 0; }

@media (max-width: 767.98px) {
  .relationship-grid, .relationship-directions { grid-template-columns: 1fr; }
  .relationship-detail-head { grid-template-columns: 1fr; }
  .filter-bar { align-items: stretch; flex-direction: column; }
  .filter-bar .form-select { width: 100%; }
}

.structure-section { margin-top: 1.5rem; }
.section-heading { display: flex; justify-content: space-between; align-items: end; margin-bottom: .8rem; }
.section-heading h3, .section-heading p { margin-bottom: .2rem; }
.arc-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1rem; }
.arc-card { margin: 0; }
.arc-card h3 { font-size: 1.1rem; }
.arc-card p { color: #64748b; }
.arc-meta { display: flex; justify-content: space-between; padding: .7rem 0; color: #475569; font-size: .85rem; border-top: 1px solid #e2e8f0; }
.card-actions { display: flex; align-items: center; gap: .45rem; }
.card-actions form { margin: 0; }
.structure-table { margin: 0; }
.structure-table td { vertical-align: middle; }
.structure-table td small { display: block; max-width: 520px; color: #64748b; margin-top: .2rem; }
.empty-state.compact { padding: 1.5rem; }

@media (max-width: 991.98px) { .arc-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 575.98px) { .arc-grid { grid-template-columns: 1fr; } }

.chapter-table a { color: #0f172a; text-decoration: none; }
.chapter-table-tools { padding: 0; overflow: hidden; }
.chapter-search-row { display: grid; grid-template-columns: minmax(220px, 1fr) minmax(180px, .6fr) 100px auto; gap: .75rem; align-items: end; padding: 1rem; border-bottom: 1px solid #e2e8f0; }
.chapter-search-row > span { align-self: center; color: #64748b; font-size: .82rem; }
.chapter-table tbody tr { cursor: pointer; }
.chapter-table tbody tr:focus { outline: 2px solid #818cf8; outline-offset: -2px; }
.chapter-table-footer { display: flex; align-items: center; justify-content: space-between; padding: .8rem 1rem; border-top: 1px solid #e2e8f0; color: #64748b; font-size: .82rem; }
.form-check-wrap { display: flex; align-items: center; min-height: 40px; }
.override-fields-disabled { opacity: .48; pointer-events: none; }
.manuscript-input { min-height: 360px; font-family: Georgia, 'Times New Roman', serif; line-height: 1.75; }
.manuscript-preview { max-width: 980px; margin-left: auto; margin-right: auto; }
.manuscript-content { height: min(62vh, 720px); min-height: 360px; overflow-y: auto; padding: 1.5rem; background: #fffdf8; border: 1px solid #eee9dc; border-radius: 8px; color: #26221b; font-family: Georgia, 'Times New Roman', serif; font-size: 1.05rem; line-height: 1.9; white-space: normal; }
.version-list { display: grid; gap: .7rem; margin-top: 1rem; }
.version-row { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding: 1rem; border: 1px solid #e2e8f0; border-radius: 9px; }
.version-row.active { border-color: #818cf8; background: #eef2ff; }
.version-row small { display: block; margin-top: .25rem; color: #64748b; }
.version-row p { margin: .5rem 0 0; color: #475569; }
.version-beat-plan { display: grid; gap: .75rem; }
.version-beat-plan article { padding: .85rem; border: 1px solid #e2e8f0; border-radius: 8px; }
.version-beat-plan article > p { margin: .35rem 0; color: #64748b; }
.version-beat-plan ol { margin: .5rem 0 0; padding-left: 1.25rem; }
.version-beat-plan li { padding: .2rem 0; }
.version-beat-plan li span { margin-right: .35rem; color: #4f46e5; font-size: .72rem; font-weight: 700; text-transform: uppercase; }
.chapter-workflow-card { max-width: 980px; margin-right: auto; margin-left: auto; }
.workflow-action-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: .75rem; }
.workflow-action-grid form { display: grid; gap: .45rem; align-content: start; margin: 0; padding: .85rem; border: 1px solid #e2e8f0; border-radius: 10px; background: #f8fafc; }
.workflow-action-grid button { width: 100%; }
.workflow-action-grid small { color: #64748b; line-height: 1.35; }
.workflow-tool-card > header { display: grid; gap: .2rem; }
.workflow-tool-card > p { min-height: 4.2em; margin: 0; color: #475569; font-size: .8rem; line-height: 1.4; }
.workflow-step-label { color: #6366f1; font-size: .66rem; font-weight: 800; letter-spacing: .04em; text-transform: uppercase; }
.workflow-result { display: grid; gap: .18rem; min-height: 66px; padding: .6rem; border: 1px solid #dbe3ec; border-radius: 8px; background: #fff; font-size: .76rem; }
.workflow-result strong { color: #334155; line-height: 1.35; }
.workflow-result span { color: #64748b; line-height: 1.35; }
.workflow-result.success { border-color: #bbf7d0; background: #f0fdf4; }
.workflow-result.warning { border-color: #fde68a; background: #fffbeb; }
.workflow-result.danger { border-color: #fecaca; background: #fff7f7; }
.workflow-result.neutral { background: #f8fafc; }
.workflow-result-detail { overflow: hidden; border: 1px solid #dbe3ec; border-radius: 8px; background: #fff; }
.workflow-result-detail > summary { padding: .5rem .6rem; cursor: pointer; color: #475569; font-size: .74rem; font-weight: 700; }
.retrieval-candidate-list { display: grid; gap: .45rem; max-height: 320px; overflow-y: auto; margin: 0; padding: .6rem .6rem .6rem 1.9rem; border-top: 1px solid #e2e8f0; }
.retrieval-candidate-list li { padding-left: .15rem; color: #475569; font-size: .75rem; }
.retrieval-candidate-list li > div { display: flex; align-items: baseline; justify-content: space-between; gap: .5rem; }
.retrieval-candidate-list small { flex: 0 0 auto; }
.retrieval-candidate-list p { margin: .2rem 0 0; color: #64748b; line-height: 1.35; }
.workflow-count-warning { color: #b45309 !important; font-weight: 700; }
.canon-lock-card { border-color: #bbf7d0; background: linear-gradient(135deg, #fff 0%, #f0fdf4 100%); }
.section-intro { margin: .35rem 0 0; color: #64748b; font-size: .82rem; }
.workflow-action-grid-compact { grid-template-columns: 1fr; gap: .5rem; }
.workflow-action-grid-compact form { padding: .65rem; }
.workflow-action-grid-compact .btn { --bs-btn-padding-y: .25rem; --bs-btn-padding-x: .5rem; --bs-btn-font-size: .875rem; }
.workflow-action-grid-compact .workflow-tool-card > p { min-height: 0; }

@media (max-width: 767.98px) { .version-row { align-items: flex-start; flex-direction: column; } }
@media (max-width: 767.98px) { .ai-agent-settings-grid { grid-template-columns: 1fr; } }

.chapter-editor-heading { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: 1rem; }
.chapter-editor-heading h2 { margin: 0; font-size: 1.35rem; }
.chapter-editor { display: grid; grid-template-columns: minmax(210px, .75fr) minmax(480px, 2fr) minmax(260px, 1fr); gap: .85rem; align-items: start; }
.editor-panel { min-width: 0; overflow: hidden; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
.editor-panel-head { display: flex; justify-content: space-between; align-items: center; min-height: 52px; padding: .8rem 1rem; border-bottom: 1px solid #e2e8f0; background: #f8fafc; }
.editor-panel-head > div { display: flex; align-items: center; gap: .7rem; }
.editor-panel-head small { color: #64748b; }
.editor-panel-body { max-height: calc(100vh - 195px); overflow-y: auto; padding: 1rem; }
.editor-navigation .editor-panel-body, .editor-context .editor-panel-body { padding: .75rem; }
.editor-navigation h3 { margin: .5rem .35rem .65rem; color: #64748b; font-size: .72rem; letter-spacing: .06em; text-transform: uppercase; }
.editor-chapter-list { display: grid; gap: .35rem; margin-bottom: 1.2rem; }
.editor-chapter-list > a, .editor-chapter-list > span { position: relative; display: grid; gap: .15rem; padding: .65rem .7rem; border: 1px solid transparent; border-radius: 8px; color: #334155; text-decoration: none; }
.editor-chapter-list > a:hover, .editor-chapter-list > span.active { border-color: #c7d2fe; background: #eef2ff; }
.editor-chapter-list small { overflow: hidden; padding-right: 3rem; color: #64748b; text-overflow: ellipsis; white-space: nowrap; }
.editor-chapter-list em { position: absolute; right: .5rem; bottom: .5rem; font-style: normal; }
.editor-arc-list { display: grid; gap: .4rem; }
.editor-arc-list article { display: grid; gap: .2rem; padding: .65rem .7rem; border-left: 3px solid #e2e8f0; }
.editor-arc-list article.active { border-color: #6366f1; background: #f5f3ff; }
.editor-arc-list small { color: #64748b; }
.editor-manuscript .editor-panel-body { display: flex; flex-direction: column; max-height: none; overflow: visible; }
.editor-textarea { height: min(72vh, 820px); min-height: 560px; resize: vertical; }
.scene-beat-builder { order: -1; }
.editor-save-row { display: grid; grid-template-columns: minmax(130px, .6fr) minmax(180px, 1fr) auto; gap: .7rem; align-items: end; margin-top: .8rem; }
.save-indicator { color: #64748b; font-size: .78rem; }
.save-indicator.changed { color: #b45309; font-weight: 700; }
.editor-subsection { padding-top: 1.1rem; margin-top: 1.2rem; border-top: 1px solid #e2e8f0; }
.editor-section-title { display: flex; justify-content: space-between; align-items: center; margin-bottom: .7rem; }
.editor-section-title h3 { margin: 0; font-size: 1rem; }
.editor-section-title span { color: #64748b; font-size: .78rem; }
.scene-list, .beat-list, .context-items { display: grid; gap: .55rem; }
.scene-beat-builder { display: grid; gap: .75rem; }
.scene-beat-builder[data-locked="1"] .scene-builder-item button, .scene-beat-builder[data-locked="1"] .scene-builder-form, .scene-beat-builder[data-locked="1"] .beat-builder-form { pointer-events: none; opacity: .55; }
.generation-mode-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.generation-mode-grid > section { display: grid; gap: .55rem; align-content: start; padding: .85rem; border: 1px solid #e2e8f0; border-radius: 8px; }
.generation-mode-grid small { color: #64748b; }
.generation-config { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .5rem; margin: 0; }
.generation-config div { padding: .6rem; background: #f8fafc; }
.generation-config dt { color: #64748b; font-size: .68rem; text-transform: uppercase; }
.generation-config dd { margin: .2rem 0 0; font-size: .78rem; font-weight: 700; }
.generation-config-link { display: inline-flex; align-items: center; gap: .35rem; margin-top: .55rem; font-size: .78rem; font-weight: 700; text-decoration: none; }
.generation-config-link svg { width: 14px; height: 14px; }
.scene-builder-form, .beat-builder-form { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem; padding: .9rem; border: 1px solid var(--app-border, #d9dee7); background: #f8fafc; }
.builder-wide { grid-column: 1 / -1; }
.builder-actions { display: flex; align-items: center; justify-content: flex-end; gap: .5rem; }
.scene-builder-list { display: grid; gap: .75rem; }
.scene-builder-item { border: 1px solid var(--app-border, #d9dee7); padding: .9rem; background: #fff; }
.scene-builder-item > header { display: flex; align-items: flex-start; justify-content: space-between; gap: .75rem; }
.scene-builder-item > header small { display: block; margin-top: .2rem; }
.beat-builder-form { margin: .75rem 0; }
.beat-list > article { display: grid; grid-template-columns: 28px minmax(0, 1fr) auto; align-items: start; gap: .75rem; cursor: grab; text-align: left; }
.beat-list > article > div { min-width: 0; text-align: left; }
.beat-list > article strong, .beat-list > article small, .beat-list > article p { text-align: left; overflow-wrap: anywhere; }
.beat-list > article.dragging { opacity: .45; }
.beat-drag-handle { border: 0; background: transparent; color: #94a3b8; cursor: grab; }
.beat-drag-handle svg { width: 1rem; height: 1rem; }
.builder-message { margin: 0; padding: .65rem .8rem; border-left: 3px solid #2563eb; background: #eff6ff; }
.builder-message.error { border-color: #dc2626; background: #fef2f2; color: #991b1b; }
.icon-button { width: 2rem; height: 2rem; padding: .35rem; display: inline-grid; place-items: center; flex: 0 0 auto; }
.icon-button svg, .scene-beat-builder button svg { width: 1rem; height: 1rem; }
@media (max-width: 767px) { .scene-builder-form, .beat-builder-form { grid-template-columns: 1fr; } .builder-wide { grid-column: auto; } }
.lore-section-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.lore-section { margin: 0; align-self: start; }
.lore-section h3 { display: flex; align-items: center; gap: .45rem; }
.lore-section h3 svg { width: 18px; height: 18px; }
.lore-form { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem; padding: .8rem; margin-bottom: .8rem; border: 1px solid #d9dee7; background: #f8fafc; }
.lore-form-wide { grid-column: 1 / -1; }
.lore-items { display: grid; gap: .55rem; }
.lore-items article { display: flex; justify-content: space-between; gap: .75rem; padding: .7rem; border: 1px solid #e2e8f0; }
.lore-items article p { margin: .25rem 0 0; color: #64748b; font-size: .82rem; white-space: pre-line; }
@media (max-width: 991px) { .generation-config, .lore-section-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 575px) { .generation-config, .lore-section-grid, .lore-form { grid-template-columns: 1fr; } .lore-form-wide { grid-column: auto; } }
.scene-list article, .beat-list article, .context-items article { padding: .75rem; border: 1px solid #e2e8f0; border-radius: 8px; }
.scene-list article { display: grid; grid-template-columns: 1fr auto; gap: .2rem .7rem; }
.scene-list article p { grid-column: 1 / -1; }
.scene-list small, .beat-list small, .context-items small { display: block; color: #64748b; }
.scene-list p, .beat-list p, .context-items p, .verifier-result p { margin: .35rem 0 0; color: #475569; font-size: .82rem; }
.editor-empty { margin: .35rem 0; color: #94a3b8; font-size: .8rem; }
.context-empty-explainer { padding: .7rem; border: 1px dashed #cbd5e1; border-radius: 8px; background: #f8fafc; }
.context-empty-explainer strong { color: #475569; font-size: .8rem; }
.context-empty-explainer p { margin: .3rem 0 0; color: #64748b; font-size: .76rem; line-height: 1.45; }
.editor-context-body { display: grid; gap: .55rem; }
.editor-context details { border: 1px solid #e2e8f0; border-radius: 8px; background: #fff; }
.editor-context summary { padding: .7rem; cursor: pointer; color: #334155; font-size: .84rem; font-weight: 700; }
.editor-context details > :not(summary) { margin-right: .7rem; margin-left: .7rem; }
.editor-context details > :last-child { margin-bottom: .7rem; }
.context-list { display: grid; gap: .45rem; margin-bottom: .65rem; }
.context-list div { display: flex; justify-content: space-between; gap: .5rem; }
.context-list dt { color: #64748b; font-size: .75rem; font-weight: 500; }
.context-list dd { margin: 0; color: #334155; font-size: .75rem; text-align: right; }
.context-note { padding: .6rem; border-radius: 6px; background: #f8fafc; color: #64748b; font-size: .74rem; }
.context-items .alert-critical, .context-items .alert-high { border-color: #fecaca; background: #fff7f7; }

@media (max-width: 1399.98px) {
  .chapter-editor { grid-template-columns: 220px minmax(460px, 1fr); }
  .editor-context { grid-column: 1 / -1; }
  .editor-context .editor-panel-body { grid-template-columns: repeat(3, minmax(0, 1fr)); max-height: none; }
}
@media (max-width: 991.98px) {
  .chapter-editor { grid-template-columns: 1fr; }
  .editor-context { grid-column: auto; }
  .editor-panel-body { max-height: none; }
  .editor-context .editor-panel-body { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .credential-selector-form { grid-template-columns: 1fr 1fr; }
  .credential-selector-form > button { grid-column: 1 / -1; }
  .credential-status-list { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 575.98px) {
  .chapter-editor-heading { align-items: flex-start; flex-direction: column; }
  .page-heading { flex-direction: column; }
  .page-actions { justify-content: flex-start; }
  .editor-save-row, .editor-context .editor-panel-body { grid-template-columns: 1fr; }
  .credential-selector-form, .credential-status-list { grid-template-columns: 1fr; }
  .credential-selector-form > button { grid-column: auto; }
  .provider-settings-head { align-items: flex-start; flex-direction: column; }
  .chapter-search-row { grid-template-columns: 1fr; }
}
