:root {
  color-scheme: light;
  --bg: #faf7f2;
  --surface: #fffefb;
  --surface-muted: #f5f1e8;
  --border: #e3dccd;
  --border-strong: #d8cfba;
  --text: #2b2a26;
  --muted: #6b6353;
  --faint: #a39a87;
  --accent: #1a3a5c;
  --accent-strong: #142d47;
  --accent-contrast: #faf7f2;
  --success: #3c5c34;
  --success-bg: #e4ede0;
  --warning: #7d6322;
  --warning-bg: #f3ecd9;
  --danger: #8a3a2e;
  --danger-bg: #f3e1dd;
  --user-bubble: #e8f0e4;
  --user-bubble-border: #d4e2cc;
  --radius-sm: 7px;
  --radius: 10px;
  --radius-lg: 14px;
  --radius-pill: 999px;
  --shadow: 0 2px 8px rgb(60 50 30 / 5%);
  --shadow-raised: 0 4px 16px rgb(60 50 30 / 8%);
  --font-display: "Noto Serif TC", "Songti TC", "PMingLiU", Georgia, serif;
  --font-body: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", -apple-system,
    "Segoe UI", sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px;
  --space-5: 24px; --space-6: 32px; --space-7: 48px;
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #171d28;
  --surface: #1c2330;
  --surface-muted: #131823;
  --border: #2a3242;
  --border-strong: #354056;
  --text: #cfc9bc;
  --muted: #828a9c;
  --faint: #5a6272;
  --accent: #d4b876;
  --accent-strong: #e2cc94;
  --accent-contrast: #171d28;
  --success: #9fc587;
  --success-bg: rgb(140 180 120 / 12%);
  --warning: #d9b96a;
  --warning-bg: rgb(217 185 106 / 12%);
  --danger: #d98a7a;
  --danger-bg: rgb(217 138 122 / 12%);
  --user-bubble: #243240;
  --user-bubble-border: #2e3d4e;
  --shadow: 0 2px 8px rgb(0 0 0 / 25%);
  --shadow-raised: 0 4px 16px rgb(0 0 0 / 35%);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    /* same custom-property assignments as [data-theme="dark"] — keep in sync */
    color-scheme: dark;
    --bg: #171d28;
    --surface: #1c2330;
    --surface-muted: #131823;
    --border: #2a3242;
    --border-strong: #354056;
    --text: #cfc9bc;
    --muted: #828a9c;
    --faint: #5a6272;
    --accent: #d4b876;
    --accent-strong: #e2cc94;
    --accent-contrast: #171d28;
    --success: #9fc587;
    --success-bg: rgb(140 180 120 / 12%);
    --warning: #d9b96a;
    --warning-bg: rgb(217 185 106 / 12%);
    --danger: #d98a7a;
    --danger-bg: rgb(217 138 122 / 12%);
    --user-bubble: #243240;
    --user-bubble-border: #2e3d4e;
    --shadow: 0 2px 8px rgb(0 0 0 / 25%);
    --shadow-raised: 0 4px 16px rgb(0 0 0 / 35%);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
}

h1, h2,
.workspace-header h2,
.inspector h2,
.brand h1 {
  font-family: var(--font-display);
}

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

button {
  cursor: pointer;
}

[hidden] {
  display: none !important;
}

button:disabled,
input:disabled,
select:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.62;
}

.workbench {
  display: grid;
  /* 右欄以 30vw 隨視窗加寬（上限隨螢幕變大），閱讀引用原文不再擁擠 */
  grid-template-columns: minmax(220px, 260px) minmax(0, 1fr) minmax(300px, 30vw);
  min-height: 100vh;
}

.workbench.is-auth-blocked {
  display: none;
}

/* ── Landing page ────────────────────────────────────── */
.landing {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background: var(--bg);
}

.landing[hidden] {
  display: none;
}

.landing-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: 14px clamp(20px, 5vw, 56px);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.landing-brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  font-family: var(--font-display);
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
}

.landing-hero {
  display: grid;
  justify-items: center;
  text-align: center;
  gap: var(--space-4);
  width: min(960px, 100%);
  margin: 0 auto;
  padding: clamp(48px, 10vh, 110px) clamp(20px, 5vw, 56px) clamp(40px, 8vh, 88px);
}

.landing-kicker {
  width: fit-content;
  margin: 0;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-pill);
  background: var(--success-bg);
  color: var(--success);
  font-size: 12px;
  font-weight: 700;
  padding: 4px 12px;
}

.landing-hero h1 {
  margin: 0;
  color: var(--text);
  font-family: var(--font-display);
  font-size: clamp(30px, 4.4vw, 44px);
  line-height: 1.25;
  letter-spacing: 0;
}

.landing-lede {
  max-width: 600px;
  margin: 0;
  color: var(--muted);
  font-size: 16px;
  line-height: 1.65;
}

.landing-cta {
  margin-top: var(--space-2);
}

.landing-cta-button {
  font-size: 16px;
  padding: 12px 32px;
}

.landing-feature-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: var(--space-4);
  width: 100%;
  margin-top: clamp(28px, 6vh, 56px);
  text-align: left;
}

.landing-feature {
  display: grid;
  gap: var(--space-2);
  align-content: start;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: var(--space-5);
}

.landing-feature-icon {
  font-size: 26px;
  line-height: 1;
}

.landing-feature h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 17px;
  line-height: 1.3;
  color: var(--text);
}

.landing-feature p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.6;
}

/* 登入彈窗 */
.landing-login-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: color-mix(in srgb, var(--text) 38%, transparent);
  backdrop-filter: blur(2px);
}

.landing-login-overlay[hidden] {
  display: none;
}

.login-form {
  display: grid;
  gap: var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: var(--space-5);
  box-shadow: var(--shadow);
}

.landing-login-card {
  width: min(380px, 100%);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-raised);
}

.login-form-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.login-form h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 20px;
  line-height: 1.2;
}

.login-form p {
  margin: 4px 0 0;
  color: var(--muted);
}

.login-form input {
  font-size: 16px;
}

.sidebar,
.workspace,
.inspector {
  min-width: 0;
}

.sidebar,
.inspector {
  background: var(--surface);
  border-color: var(--border);
  border-style: solid;
}

.sidebar {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  gap: 16px;
  border-width: 0 1px 0 0;
  padding: 16px;
}

.sidebar .sidebar-footer {
  align-self: end;
  display: grid;
  gap: 8px;
}

.sidebar-footer .console-entry {
  width: 100%;
}

.chat-toolbar {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.chat-toolbar .text-button[hidden] {
  display: none;
}

.inspector {
  display: grid;
  align-content: start;
  gap: 16px;
  border-width: 0 0 0 1px;
  padding: 16px;
  overflow: auto;
}

.workspace {
  display: grid;
  min-height: 100vh;
  padding: 18px;
  overflow: auto;
}

.brand {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  color: var(--accent-strong);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
}

.brand h1,
.workspace-header h2,
.inspector h2,
.sidebar-section h2 {
  margin: 0;
  letter-spacing: 0;
}

.brand h1 {
  font-size: 15px;
  line-height: 1.25;
}

.brand p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.tabs {
  display: grid;
  gap: 6px;
}

.tab {
  display: grid;
  grid-template-columns: 20px minmax(0, 1fr);
  align-items: center;
  width: 100%;
  min-height: 44px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  padding: 8px 10px;
  text-align: left;
}

.tab svg {
  width: 18px;
  height: 18px;
  color: var(--muted);
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

.tab:hover {
  background: var(--surface-muted);
}

/* Global focus ring — covers ALL interactive elements in both themes */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Console nav has a fixed dark slate background (#1f2a38) in both themes.
   The light-theme --accent (#1a3a5c) is nearly invisible on that surface,
   so use the gold (#d4b876) which achieves 7.55:1 on #1f2a38. */
.console-nav :focus-visible,
.console-nav-item:focus-visible,
.console-back:focus-visible {
  outline-color: #d4b876;
}

.tab.is-active {
  border-color: var(--border-strong);
  background: var(--surface-muted);
  color: var(--accent-strong);
  font-weight: 700;
}

.sidebar-section,
.inspector-section,
.workspace-panel {
  min-width: 0;
}

.section-heading,
.workspace-header,
.form-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.section-heading h2,
.section-heading h3,
.inspector h2 {
  font-size: 13px;
  line-height: 1.25;
}

.section-heading h3 {
  margin: 0;
}

.workspace-header {
  margin-bottom: 14px;
}

.workspace-header h2 {
  font-size: 20px;
  line-height: 1.2;
}

.workspace-subtitle {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.workspace-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-height: calc(100vh - 36px);
}

.workspace-panel[hidden] {
  display: none;
}

.selected-sources,
.source-table,
.import-jobs,
.background-jobs,
.worker-runtime,
.provider-budget,
.provider-usage,
.provider-calls,
.provider-traces,
.admin-documents,
.audit-events,
.eval-cases,
.eval-results,
.eval-report,
.feedback-promotions {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.job-row,
.document-row,
.audit-row,
.eval-row,
.report-row,
.feedback-row,
.tool-panel {
  display: grid;
  gap: 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 10px;
  box-shadow: var(--shadow);
}

.source-row {
  display: grid;
  gap: 4px;
  width: 100%;
  border: 1px solid var(--border);
  border-left: 3px solid transparent;
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: inherit;
  padding: var(--space-2) var(--space-3);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
}

.source-row:hover {
  border-color: var(--border-strong);
  border-left-color: var(--border-strong);
}

.source-row.is-active {
  border-color: var(--border-strong);
  border-left-color: var(--accent);
}

.source-row strong,
.job-row strong,
.document-row strong,
.eval-row strong,
.report-row strong,
.feedback-row strong {
  overflow-wrap: anywhere;
  font-size: 13px;
}

.source-title {
  display: block;
}

.preview-source-meta {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.preview-source-card {
  display: grid;
  gap: 3px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 10px;
}

.preview-source-card > span:first-child {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.tool-section {
  margin-top: 16px;
}

.document-row,
.job-row {
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
}

.ops-summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.ops-summary-card {
  display: grid;
  gap: 2px;
  min-height: 64px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 10px;
  box-shadow: var(--shadow);
}

.ops-summary-card span {
  color: var(--muted);
  font-size: 12px;
}

.ops-summary-card strong {
  font-size: 22px;
  line-height: 1.1;
}

.ops-summary-card.is-danger {
  border-color: var(--danger);
}

.ops-summary-card.is-warning {
  border-color: var(--warning);
}

.ops-summary-card.is-success {
  border-color: var(--success);
}

.job-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 10px;
}

.job-controls .inline-control {
  align-items: start;
  flex-direction: column;
  gap: 4px;
}

.job-controls select {
  min-height: 36px;
}

.job-row-heading,
.job-badges,
.job-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.job-row-heading {
  justify-content: space-between;
}

.job-badges {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.job-detail {
  color: var(--muted);
  font-size: 12px;
  overflow-wrap: anywhere;
}

.worker-runtime {
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

.worker-card {
  display: grid;
  gap: 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 10px;
  box-shadow: var(--shadow);
}

.status-badge {
  display: inline-grid;
  min-height: 24px;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-muted);
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  line-height: 1;
  padding: 4px 8px;
}

.status-badge.is-succeeded {
  border-color: var(--success);
  background: var(--success-bg);
  color: var(--success);
}

.status-badge.is-running,
.status-badge.is-stale,
.status-badge.is-warning {
  border-color: var(--warning);
  background: var(--warning-bg);
  color: var(--warning);
}

.status-badge.is-failed {
  border-color: var(--danger);
  background: var(--danger-bg);
  color: var(--danger);
}

.status-badge.is-canceled {
  border-color: var(--border-strong);
}

.job-body {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.job-row.is-succeeded,
.job-row.is-duplicate {
  border-color: var(--success);
}

.job-row.is-queued {
  border-color: var(--border-strong);
}

.job-row.is-running {
  border-color: var(--warning);
}

.job-row.is-failed,
.job-row.is-stale {
  border-color: var(--danger);
}

.job-row.is-canceled {
  border-color: var(--border-strong);
}

.provider-trace strong,
.provider-call strong,
.provider-budget-row strong,
.provider-budget-policy strong,
.provider-row strong {
  overflow-wrap: anywhere;
}

.document-row.is-indexed {
  border-color: var(--success);
}

.document-row.is-disabled {
  border-color: var(--warning);
}

.document-row.is-deleted,
.document-row.is-source_missing,
.document-row.is-not_indexed {
  border-color: var(--danger);
}

.audit-row.is-success {
  border-color: var(--success);
}

.audit-row.is-failure,
.audit-row.is-blocked {
  border-color: var(--danger);
}

.eval-row.is-passed {
  border-color: var(--success);
}

.eval-row.is-failed {
  border-color: var(--danger);
}

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

.chat-log {
  display: grid;
  align-content: start;
  gap: 12px;
  min-height: 320px;
  max-height: calc(100vh - 250px);
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 14px;
}

.chat-empty-state {
  display: grid;
  gap: 12px;
  max-width: 760px;
}

.chat-empty-state[hidden] {
  display: none;
}

.sample-prompt-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 8px;
}

.sample-prompt-grid button {
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  overflow-wrap: anywhere;
  padding: 9px 11px;
  text-align: left;
}

.sample-prompt-grid button:hover {
  border-color: var(--border-strong);
  background: var(--surface-muted);
}

.message {
  display: grid;
  gap: 4px;
  max-width: 860px;
  overflow-wrap: anywhere;
}

.message p {
  margin: 0;
  white-space: pre-wrap;
}

.message p.is-streaming-status {
  color: var(--muted);
  font-style: italic;
}

.message.user {
  justify-self: end;
  width: min(100%, 720px);
}

.message.user .message-label {
  justify-self: end;
}

.user-bubble {
  background: var(--user-bubble);
  border: 1px solid var(--user-bubble-border);
  border-radius: var(--radius-lg) var(--radius-lg) 4px var(--radius-lg);
  padding: var(--space-3) var(--space-4);
  margin-left: auto;
  max-width: 70%;
}

.answer-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px var(--radius-lg) var(--radius-lg) var(--radius-lg);
  box-shadow: var(--shadow);
  padding: var(--space-4) var(--space-5);
  line-height: 1.75;
  max-width: 92%;
}

.citation-pill {
  display: inline-block;
  min-width: 18px;
  text-align: center;
  border: 0;
  padding: 1px 8px;
  border-radius: var(--radius-pill);
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 12px;
  font-weight: 600;
  line-height: 1.4;
  cursor: pointer;
}

.citation-pill:hover {
  background: var(--accent-strong);
}

.message-label {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.answer-footer {
  display: grid;
  gap: var(--space-2);
  justify-items: start;
  border-top: 1px solid var(--border);
  margin-top: var(--space-3);
  padding-top: var(--space-2);
  font-size: 13px;
  color: var(--muted);
}

.trust-badge {
  display: inline-block;
  padding: 3px 10px;
  border: 1px solid transparent;
  border-radius: 5px;
  font-size: 13px;
  margin-bottom: var(--space-3);
}

.trust-badge.is-ok {
  background: var(--success-bg);
  color: var(--success);
  border-color: color-mix(in srgb, var(--success) 30%, transparent);
}

.trust-badge.is-warn {
  background: var(--warning-bg);
  color: var(--warning);
}

.trust-badge.is-danger {
  background: var(--danger-bg);
  color: var(--danger);
}

.source-chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.source-chip {
  max-width: min(100%, 360px);
  min-height: 32px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-muted);
  color: var(--accent-strong);
  overflow-wrap: anywhere;
  padding: 4px 8px;
  text-align: left;
  font-size: 12px;
  font-weight: 700;
}

.source-chip:hover {
  background: var(--border);
}

.answer-feedback {
  display: grid;
  gap: 8px;
  justify-items: start;
}

.feedback-actions,
.feedback-detail {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.feedback-button {
  min-height: 30px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--muted);
  padding: 2px 8px;
  font-size: 13px;
  font-weight: 600;
}

.feedback-button:hover {
  background: var(--surface-muted);
  color: var(--text);
}

.feedback-detail textarea {
  width: min(360px, 100%);
  min-height: 64px;
}

.feedback-detail select {
  min-height: 36px;
}

.feedback-status {
  min-height: 18px;
  color: var(--muted);
  font-size: 12px;
}

.answer-feedback.is-submitted .feedback-actions,
.answer-feedback.is-submitted .feedback-detail {
  opacity: 0.62;
}

.chat-form,
.upload-form,
.audit-form,
.eval-form {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.chat-form.is-busy {
  opacity: 0.88;
}

.composer-status {
  min-height: 18px;
  margin: -2px 0 0;
  color: var(--muted);
  font-size: 12px;
}

textarea,
input,
select {
  min-width: 0;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}

textarea {
  width: 100%;
  min-height: 112px;
  resize: vertical;
  padding: 10px 12px;
}

input,
select {
  min-height: 44px;
  padding: 7px 10px;
}

.inline-control {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}

.inline-control input {
  width: 64px;
}

.primary-button,
.secondary-button,
.text-button {
  min-height: 44px;
  border-radius: 8px;
  padding: 7px 12px;
  font-weight: 700;
}

.primary-button {
  border: 1px solid var(--accent-strong);
  background: var(--accent);
  color: var(--accent-contrast);
}

.primary-button:hover {
  background: var(--accent-strong);
}

.secondary-button {
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
}

.secondary-button:hover,
.text-button:hover {
  background: var(--surface-muted);
}

.text-button {
  border: 0;
  background: transparent;
  color: var(--accent-strong);
  padding-inline: 6px;
}

.count-pill {
  display: inline-grid;
  min-width: 24px;
  height: 24px;
  place-items: center;
  border-radius: 999px;
  background: var(--surface-muted);
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.operation-log,
.eval-status {
  max-height: 260px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.45;
  padding: 10px;
  white-space: pre-wrap;
}

/* ── Markdown 內文（引用預覽、教材閱讀器、概念面板共用） ── */
.markdown-preview {
  max-height: min(48vh, 520px);
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 12px 14px;
  margin-top: 10px;
}

.markdown-body {
  color: var(--text);
  font-size: 14px;
  line-height: 1.75;
  overflow-wrap: anywhere;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  font-family: var(--font-display);
  line-height: 1.3;
  margin: 1.2em 0 0.45em;
}

.markdown-body h1 { font-size: 22px; margin-top: 0.4em; }
.markdown-body h2 { font-size: 18px; }
.markdown-body h3 { font-size: 16px; }
.markdown-body h4 { font-size: 14px; }

.markdown-body p {
  margin: 0.55em 0;
}

.markdown-body ul,
.markdown-body ol {
  margin: 0.55em 0;
  padding-inline-start: 1.4em;
}

.markdown-body li {
  margin: 0.25em 0;
}

.markdown-body code {
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 0.85em;
  padding: 1px 5px;
}

.markdown-body pre {
  overflow: auto;
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}

.markdown-body pre code {
  border: 0;
  background: transparent;
  padding: 0;
  font-size: 12px;
  line-height: 1.55;
}

.markdown-body blockquote {
  margin: 0.7em 0;
  border-left: 3px solid var(--border-strong);
  color: var(--muted);
  padding: 2px 0 2px 12px;
}

.markdown-body table {
  border-collapse: collapse;
  margin: 0.7em 0;
  width: 100%;
  font-size: 13px;
}

.markdown-body th,
.markdown-body td {
  border: 1px solid var(--border);
  padding: 6px 9px;
  text-align: left;
  vertical-align: top;
}

.markdown-body th {
  background: var(--surface-muted);
}

.markdown-body hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 1em 0;
}

.mermaid-diagram {
  margin: 0.7em 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 12px;
  overflow-x: auto;
  text-align: center;
}

.mermaid-diagram svg {
  max-width: 100%;
  height: auto;
}

/* 渲染失敗時退回程式碼區塊，外框交給內部的 pre 呈現 */
.mermaid-diagram.is-fallback {
  border: 0;
  background: transparent;
  padding: 0;
  text-align: left;
}

.markdown-body a {
  color: var(--accent-strong);
}

.operation-log {
  max-height: 360px;
  margin: 4px 0 0;
}

.eval-status {
  max-width: 640px;
  margin: 12px 0 0;
}

.status-grid {
  display: grid;
  gap: 8px;
  margin: 10px 0 0;
}

.status-grid div {
  display: grid;
  grid-template-columns: 80px minmax(0, 1fr);
  gap: 10px;
}

.status-grid dt {
  color: var(--muted);
}

.status-grid dd {
  margin: 0;
  overflow-wrap: anywhere;
}

/* ── 右欄收合面板（引用來源） ─────────────────────────── */
.inspector-disclosure {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.inspector-disclosure summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  list-style: none;
  cursor: pointer;
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 700;
  font-family: var(--font-display);
}

.inspector-disclosure summary::-webkit-details-marker {
  display: none;
}

.inspector-disclosure summary::before {
  content: "▸";
  color: var(--muted);
  font-size: 11px;
  margin-right: 2px;
}

.inspector-disclosure[open] summary::before {
  content: "▾";
}

.inspector-disclosure .disclosure-title {
  flex: 1;
}

.inspector-disclosure .selected-sources {
  margin: 0;
  padding: 0 10px 10px;
}

/* ── 教材閱讀器（Sources tab 中欄全文） ───────────────── */
.source-reader {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
  min-height: 0;
}

.reader-toolbar {
  display: flex;
  justify-content: flex-start;
}

.reader-body {
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: clamp(16px, 4vw, 40px);
  max-width: 860px;
  width: 100%;
}

/* The graph panel rows: header / stats line / canvas (stretch) / empty / detail.
   The canvas keeps the stretch row so it fills the viewport height. */
.workspace-panel[data-panel="graph"] {
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
}

.graph-canvas {
  height: 100%;
  min-height: 480px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}

/* ── 圖譜概念詳情（渲染於右欄來源內容區） ─────────────── */
.graph-detail-ask {
  margin-top: 8px;
  min-height: 34px;
  font-size: 13px;
  justify-self: start;
}

.graph-detail-summary {
  margin: 4px 0 0;
  line-height: 1.7;
}

.graph-detail-sources-heading {
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.05em;
}

.concept-back-link {
  display: inline-block;
  margin-bottom: 8px;
}

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

.graph-search {
  min-width: 180px;
}

.graph-view-toggle {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}

.graph-view-button {
  border: 0;
  background: var(--surface);
  padding: 6px 12px;
  cursor: pointer;
}

.graph-view-button.is-active {
  background: var(--accent);
  color: var(--accent-contrast);
}

.graph-empty {
  color: var(--muted);
}

.graph-stats {
  font-size: 12px;
  color: var(--muted);
  margin: 8px 0 4px;
}

.concept-sources {
  display: grid;
  gap: 6px;
  margin-top: 8px;
}

.graph-cross-link {
  width: 100%;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-muted);
  color: var(--accent-strong);
  font-size: 13px;
  font-weight: 600;
  padding: var(--space-2) var(--space-3);
  text-align: left;
  cursor: pointer;
}

.graph-cross-link:hover {
  border-style: solid;
  background: var(--surface);
}

.upload-form,
.audit-form,
.eval-form {
  max-width: 640px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 14px;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

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

.eval-report {
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}

.report-sections {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
  margin-top: 12px;
}

.report-section {
  min-width: 0;
}

.report-section h4 {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.25;
}

/* ── 教材總覽 reading-list row ────────────────────────── */
.doc-row {
  display: grid;
  gap: var(--space-1);
  width: 100%;
  border: 1px solid var(--border);
  border-left: 3px solid transparent;
  border-radius: var(--radius);
  background: var(--surface);
  color: inherit;
  padding: var(--space-3) var(--space-4);
  text-align: left;
  cursor: pointer;
  transition: box-shadow 0.2s ease-out, border-color 0.15s ease-out;
}

.doc-row:hover {
  border-color: var(--border-strong);
  border-left-color: var(--border-strong);
  box-shadow: var(--shadow);
}

.doc-row.is-active {
  border-color: var(--border-strong);
  border-left-color: var(--accent);
}

.doc-row .source-title {
  font-family: var(--font-display);
  font-size: 14px;
  font-weight: 600;
  overflow-wrap: anywhere;
}

/* ── Admin ops grouping inside sources panel ─────────── */
.admin-ops-section {
  border-top: 1px solid var(--border);
  margin-top: var(--space-5);
  padding-top: var(--space-4);
}

.admin-ops-heading h3 {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

/* ── Admin console ───────────────────────────────────── */
.console-entry {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-muted);
  color: var(--text);
  padding: var(--space-2) var(--space-3);
  text-align: left;
  font-size: 13px;
}

.console-entry:hover {
  border-color: var(--border-strong);
  background: var(--surface);
}

.console {
  display: flex;
  min-height: 100vh;
  background: var(--bg);
}

/* Console nav stays dark slate in BOTH themes (per spec). */
.console-nav {
  width: 210px;
  flex-shrink: 0;
  background: #1f2a38;
  color: #aeb6c2;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--space-4) var(--space-3);
}

.console-brand {
  font-family: var(--font-display);
  color: #d4b876;
  font-weight: 700;
  padding: 0 var(--space-2) var(--space-3);
  border-bottom: 1px solid #2e3a4a;
  margin-bottom: var(--space-2);
}

.console-nav-item {
  text-align: left;
  border: 0;
  background: transparent;
  color: inherit;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 14px;
}

.console-nav-item:hover {
  background: #273445;
  color: #e7ebf2;
}

.console-nav-item.is-active {
  background: #2e3d52;
  color: #fff;
  font-weight: 600;
}

.console-section {
  font-size: 11px;
  letter-spacing: 0.12em;
  color: #6b7686;
  padding: var(--space-3) var(--space-3) var(--space-1);
}

.console-key {
  margin: 0 var(--space-2) var(--space-2);
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  border: 1px solid #2e3a4a;
  background: #16202c;
  color: #d5dae3;
  font-size: 12px;
}

.console-main {
  flex: 1;
  padding: var(--space-5) var(--space-6);
  overflow-y: auto;
}

.console-back {
  margin-top: auto;
  border: 0;
  background: transparent;
  color: #6b7686;
  text-align: left;
  padding: var(--space-2) var(--space-3);
  cursor: pointer;
}

.console-back:hover {
  color: #aeb6c2;
}

.console-panel {
  display: grid;
  align-content: start;
  gap: var(--space-4);
  max-width: 1100px;
}

/* Console 總覽 dashboard */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--space-3);
}

.stat-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: var(--space-3) var(--space-4);
  display: grid;
  gap: var(--space-1);
  align-content: start;
}

.stat-label {
  margin: 0;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.stat-number {
  margin: 0;
  font-family: var(--font-display);
  font-size: 34px;
  line-height: 1.15;
  color: var(--text);
}

.stat-note {
  margin: 0;
  font-size: 12px;
  color: var(--text);
}

.stat-note.muted {
  color: var(--muted);
}

.activity-list {
  margin: var(--space-2) 0 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: var(--space-1);
}

.activity-row {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text);
  padding: 2px 0;
  border-bottom: 1px dashed var(--border);
}

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

.activity-row.is-danger {
  color: var(--danger);
}

/* Shared console operation-log strip (visible across panels) */
.console-log-strip {
  max-width: 1100px;
  margin-top: var(--space-4);
  border-top: 1px solid var(--border);
  padding-top: var(--space-2);
  display: grid;
  gap: var(--space-1);
}

.console-log-title {
  margin: 0;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.console-log-strip .operation-log {
  max-height: 140px;
  margin: 0;
}

.graph-extract-status {
  max-width: 640px;
}

/* Console nav item and key focus-visible handled by the global rule +
   the .console-nav override block above. */

.muted {
  margin: 0;
  color: var(--muted);
}

.is-success {
  color: var(--success);
}

.is-warning {
  color: var(--warning);
}

.is-danger {
  color: var(--danger);
}

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

/* ── Motion — transitions on interactive components ─────── */
.tab,
.source-row,
.doc-row,
.source-chip,
.citation-pill,
.feedback-button,
.graph-view-button,
.graph-cross-link,
.primary-button,
.secondary-button,
.text-button,
.console-entry,
.console-nav-item,
.console-back,
.sample-prompt-grid button {
  transition: background-color 0.15s ease-out, border-color 0.15s ease-out,
    box-shadow 0.2s ease-out;
}

/* Panel fade-slide on tab activation */
.workspace-panel:not([hidden]) {
  animation: panel-in 0.2s ease-out;
}

@keyframes panel-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: none; }
}

/* Reduced-motion kill-switch — disable ALL animations and transitions */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* ── 系統設定面板 ─────────────────────────────────────── */
.runtime-settings-form {
  display: grid;
  gap: 16px;
  max-width: 860px;
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
}

.settings-field {
  display: grid;
  gap: 6px;
  align-content: start;
}

.settings-field > span {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

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

.settings-status {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.settings-status.is-danger {
  color: var(--danger, #b3261e);
}

/* ── Admin tables（審計日誌 / LLM 呼叫紀錄）──────────────── */
.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.admin-table th,
.admin-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.admin-table thead th {
  background: var(--surface-muted);
  font-weight: 700;
  white-space: nowrap;
}

.admin-table tbody tr.is-failed td,
.admin-table tbody tr.is-failure td,
.admin-table tbody tr.is-blocked td {
  background: color-mix(in srgb, var(--danger, #b3261e) 7%, transparent);
}

.table-sort-button {
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  padding: 0;
}

.audit-metadata-cell {
  max-width: 420px;
  overflow-wrap: anywhere;
  color: var(--muted);
}

.provider-log-detail pre {
  margin: 0;
  max-height: 320px;
  overflow: auto;
  font-size: 12px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* ── 服務狀態 ─────────────────────────────────────────── */
.health-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 14px;
}

.health-card {
  border: 1px solid var(--border);
  border-left-width: 4px;
  border-radius: 8px;
  background: var(--surface);
  padding: 14px 16px;
  display: grid;
  gap: 8px;
}

.health-card.is-ok {
  border-left-color: var(--success, #2e7d32);
}

.health-card.is-warning {
  border-left-color: #b58a00;
}

.health-card.is-failed {
  border-left-color: var(--danger, #b3261e);
}

.health-card-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.health-card p {
  margin: 0;
}

/* ── 教材編輯 ─────────────────────────────────────────── */
.editor-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 12px;
  margin-bottom: 10px;
}

.editor-doc-select select {
  min-width: 320px;
  max-width: 100%;
}

.editor-textarea {
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  line-height: 1.6;
  resize: vertical;
}

/* ── Responsive: 1120px breakpoint ────────────────────────── */
@media (max-width: 1120px) {
  .workbench {
    grid-template-columns: minmax(210px, 240px) minmax(0, 1fr);
  }

  .inspector {
    grid-column: 1 / -1;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    border-width: 1px 0 0;
    max-height: none;
  }
}

/* ── Responsive: 760px breakpoint ────────────────────────── */
@media (max-width: 760px) {
  .landing-topbar {
    padding: 12px 16px;
  }

  .landing-hero {
    padding: 48px 20px 40px;
  }

  .landing-feature-grid {
    grid-template-columns: 1fr;
  }

  .workbench {
    display: block;
  }

  .sidebar,
  .workspace,
  .inspector {
    min-height: auto;
    border-width: 0 0 1px;
  }

  /* Sidebar collapses to a compact icon row at <760px */
  .sidebar {
    grid-template-rows: auto auto auto;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
  }

  .brand p {
    display: none;
  }

  .tabs {
    display: flex;
    flex-direction: row;
    gap: var(--space-1);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

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

  .tab {
    flex: 0 0 auto;
    width: auto; /* base rule's width: 100% would give each tab the full row */
    grid-template-columns: 18px minmax(0, 1fr);
    min-height: 40px;
    padding: 6px 8px;
    white-space: nowrap;
  }

  .workspace {
    padding: 14px;
  }

  .workspace-panel {
    min-height: auto;
  }

  .inspector {
    display: grid;
    grid-template-columns: 1fr;
  }

  .workspace-header,
  .form-actions,
  .section-heading {
    align-items: stretch;
    flex-direction: column;
  }

  /* Console nav becomes a horizontal top bar at <760px */
  .console {
    flex-direction: column;
  }

  .console-nav {
    width: 100%;
    flex-direction: row;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: var(--space-1);
    padding: var(--space-2) var(--space-3);
    align-items: center;
  }

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

  .console-brand {
    padding: 0 var(--space-3) 0 0;
    border-bottom: 0;
    border-right: 1px solid #2e3a4a;
    margin-bottom: 0;
    margin-right: var(--space-2);
    white-space: nowrap;
    flex-shrink: 0;
  }

  .console-section {
    display: none;
  }

  .console-back {
    margin-top: 0;
    margin-left: auto;
    flex-shrink: 0;
    white-space: nowrap;
  }

  .console-key {
    flex-shrink: 0;
    margin: 0 var(--space-2) 0 0;
    width: 120px;
  }

  .console-main {
    padding: var(--space-4) var(--space-4);
    overflow-y: auto;
  }
}
