:root {
  --ink: #111111;
  --ink-soft: #2f2f2f;
  --canvas: #ffffff;
  --surface: #ffffff;
  --sidebar: #f9f9f9;
  --line: #e6e6e6;
  --muted: #727272;
  --accent: #0f8a6c;
  --accent-dark: #08725a;
  --accent-soft: #e6f5f0;
  --warning: #c94f42;
  --warning-soft: #fbe9e6;
  --focus: #2f6fca;
  --sidebar-width: 254px;
  --drawer-width: 420px;
  --radius: 8px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Noto Sans TC", sans-serif;
  color: var(--ink);
  background: var(--canvas);
  letter-spacing: 0;
}

* { box-sizing: border-box; letter-spacing: 0; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { overflow: hidden; background: var(--canvas); }
button, input, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button:focus-visible, a:focus-visible, input:focus-visible, textarea:focus-visible { outline: 3px solid color-mix(in srgb, var(--focus) 28%, transparent); outline-offset: 2px; }
.app-shell { display: grid; grid-template-columns: var(--sidebar-width) minmax(0, 1fr); height: 100%; }
.app-shell:has(.sidebar.desktop-hidden) { grid-template-columns: 0 minmax(0, 1fr); }

.sidebar { min-width: 0; display: flex; flex-direction: column; padding: 10px 8px 8px; color: var(--ink); background: var(--sidebar); border-right: 1px solid #f0f0f0; z-index: 30; transition: margin-left 180ms ease; }
.sidebar.desktop-hidden { margin-left: calc(var(--sidebar-width) * -1); }
.brand-row { min-height: 48px; display: flex; align-items: center; gap: 4px; padding: 0 4px 0 7px; }
.brand-row strong { min-width: 0; flex: 1; font-size: 18px; line-height: 1.2; }
.sidebar-close { display: none; margin-left: auto; }
.sidebar-search, .sidebar-panel { flex: 0 0 34px; }
.icon-button { width: 34px; height: 34px; display: inline-grid; place-items: center; padding: 0; color: var(--ink); background: transparent; border: 0; border-radius: 7px; cursor: pointer; text-decoration: none; }
.icon-button:hover { background: #ececec; }
.icon-button svg { width: 19px; height: 19px; stroke-width: 1.8; }
.sidebar-close { display: none; }
.conversation-search { height: 40px; display: flex; align-items: center; gap: 8px; margin: 4px 4px 7px; padding: 0 10px; background: #fff; border: 1px solid #dcdcdc; border-radius: 8px; }
.conversation-search svg { width: 16px; color: var(--muted); }
.conversation-search input { min-width: 0; width: 100%; border: 0; outline: 0; background: transparent; }
.primary-nav { display: grid; gap: 2px; margin: 12px 0 25px; }
.primary-nav-item { width: 100%; min-height: 42px; display: flex; align-items: center; gap: 12px; padding: 8px 10px; color: #252525; background: transparent; border: 0; border-radius: 8px; text-decoration: none; text-align: left; cursor: pointer; font-size: 14px; }
.primary-nav-item:hover, .primary-nav-item.active { background: #ececec; }
.primary-nav-item svg { width: 20px; height: 20px; flex: 0 0 20px; stroke-width: 1.8; }
.sidebar-label { margin: 0 10px 8px; color: #8c8c8c; font-size: 12px; font-weight: 500; }
.conversation-list { flex: 1; min-height: 0; overflow-y: auto; }
.conversation-item { width: 100%; min-height: 38px; display: flex; align-items: center; gap: 9px; padding: 8px 10px; margin-bottom: 2px; color: #303030; background: transparent; border: 0; border-radius: 8px; text-align: left; cursor: pointer; font-size: 13px; }
.conversation-item:hover, .conversation-item.active { background: #ececec; color: #111; }
.conversation-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversation-item svg { display: none; }
.sidebar-footer { min-height: 58px; display: flex; align-items: center; gap: 10px; padding: 8px 7px 4px; border-top: 1px solid var(--line); }
.profile-avatar { width: 31px; height: 31px; flex: 0 0 31px; display: grid; place-items: center; color: #fff; background: #222; border-radius: 50%; font-size: 13px; font-weight: 700; }
.profile-copy { min-width: 0; flex: 1; }
.profile-copy strong, .profile-copy span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.profile-copy strong { font-size: 12px; }
.profile-copy span { margin-top: 3px; color: #7b7b7b; font-size: 10px; }

.chat-main { position: relative; min-width: 0; height: 100%; display: grid; grid-template-rows: 66px minmax(0, 1fr) auto; background: var(--surface); }
.topbar { position: relative; min-height: 66px; display: flex; align-items: center; justify-content: space-between; padding: 8px 18px; }
.topbar h1 { max-width: min(32vw, 420px); margin: 0; overflow: hidden; color: var(--ink); font-size: 14px; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; opacity: 1; }
.is-empty .topbar h1 { opacity: 0; }
.menu-button { display: none; }
.sidebar.desktop-hidden + .chat-main .menu-button { display: inline-grid; }
.mode-switch { position: absolute; left: 50%; top: 10px; display: grid; grid-template-columns: repeat(2, 108px); padding: 3px; background: #f5f5f5; border-radius: 26px; transform: translateX(-50%); }
.mode-option { height: 42px; display: grid; place-items: center; color: #696969; background: transparent; border: 0; border-radius: 23px; text-decoration: none; cursor: pointer; font-size: 14px; }
.mode-option.active { color: #111; background: #fff; box-shadow: 0 1px 5px rgba(0, 0, 0, 0.12); font-weight: 600; }
.service-status { min-width: 34px; height: 34px; display: flex; align-items: center; justify-content: center; gap: 7px; margin-left: auto; color: #222; white-space: nowrap; font-size: 11px; }
.service-status svg { width: 21px; height: 21px; }
.service-status > span:last-child, .status-dot { display: none; }
.service-status.online svg { color: var(--accent); }
.service-status.offline svg { color: var(--warning); }

.messages { min-height: 0; overflow-y: auto; padding: 0 24px 42px; scroll-behavior: smooth; }
.welcome { width: min(760px, 100%); margin: 0 auto; padding-top: min(28vh, 310px); }
.welcome h2 { margin: 0; color: #1f1f1f; font-size: 24px; line-height: 1.35; text-align: center; font-weight: 500; }
.prompt-list { width: min(640px, 100%); display: grid; gap: 2px; margin: 130px auto 0; }
.prompt-suggestion { min-height: 48px; display: flex; align-items: center; gap: 14px; padding: 8px 12px; color: #9a9a9a; background: transparent; border: 0; border-radius: 8px; text-align: left; cursor: pointer; font-size: 14px; }
.prompt-suggestion:hover { color: #333; background: #f5f5f5; }
.prompt-suggestion svg { width: 18px; height: 18px; flex: 0 0 18px; color: #111; fill: #111; stroke-width: 1.6; }

.message-row { width: min(760px, 100%); display: grid; grid-template-columns: 34px minmax(0, 1fr); gap: 14px; margin: 30px auto 0; }
.message-row:last-child { animation: message-in 180ms ease both; }
@keyframes message-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
.message-avatar { width: 34px; height: 34px; display: grid; place-items: center; border-radius: 6px; }
.message-avatar svg { width: 17px; height: 17px; }
.message-row.user .message-avatar { color: #fff; background: var(--ink-soft); }
.message-row.assistant .message-avatar { color: #fff; background: var(--ink); }
.message-content { min-width: 0; padding-top: 3px; }
.message-role { margin-bottom: 7px; color: var(--ink); font-size: 13px; font-weight: 700; }
.message-text { color: #30332e; font-size: 15px; line-height: 1.8; overflow-wrap: anywhere; white-space: pre-wrap; }
.message-status { display: inline-flex; align-items: center; gap: 6px; min-height: 26px; margin-top: 13px; padding: 4px 8px; color: var(--accent-dark); background: var(--accent-soft); border: 1px solid #b8dfd7; border-radius: 5px; font-size: 11px; font-weight: 700; }
.message-status.escalated { color: #9c3f2e; background: var(--warning-soft); border-color: #efc2b9; }
.message-status svg { width: 13px; height: 13px; }
.citation-list { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 12px; }
.citation-button { min-height: 31px; display: inline-flex; align-items: center; gap: 6px; padding: 5px 9px; color: #3b4b45; background: #f3f6f3; border: 1px solid var(--line); border-radius: 5px; cursor: pointer; font-size: 12px; }
.citation-button:hover { border-color: var(--accent); color: var(--accent-dark); }
.citation-number { display: grid; place-items: center; width: 18px; height: 18px; color: #fff; background: var(--accent); border-radius: 3px; font-size: 10px; font-weight: 800; }
.typing { display: flex; gap: 5px; padding: 8px 0; }
.typing span { width: 6px; height: 6px; background: #8a938b; border-radius: 50%; animation: pulse 900ms ease-in-out infinite; }
.typing span:nth-child(2) { animation-delay: 150ms; }
.typing span:nth-child(3) { animation-delay: 300ms; }
@keyframes pulse { 0%, 100% { opacity: .3; transform: translateY(0); } 50% { opacity: 1; transform: translateY(-3px); } }

.composer-shell { position: sticky; bottom: 0; z-index: 5; padding: 12px 24px 16px; background: rgba(255, 255, 255, 0.97); border-top: 1px solid transparent; }
.is-empty .composer-shell { position: absolute; top: 42%; left: 0; right: 0; bottom: auto; padding: 0 40px; background: transparent; }
.composer { width: min(820px, 100%); min-height: 58px; display: flex; align-items: center; gap: 8px; margin: 0 auto; padding: 7px 8px 7px 10px; background: var(--surface); border: 1px solid #e2e2e2; border-radius: 30px; box-shadow: 0 4px 18px rgba(0, 0, 0, 0.08); }
.is-empty .composer { width: min(980px, 100%); }
.composer:focus-within { border-color: #c8c8c8; box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1); }
.composer textarea { min-width: 0; max-height: 160px; flex: 1; resize: none; padding: 9px 4px; color: var(--ink); background: transparent; border: 0; outline: 0; line-height: 1.5; }
.composer textarea::placeholder { color: #8c8c8c; }
.composer-tool, .composer-action { width: 40px; height: 40px; flex: 0 0 40px; display: grid; place-items: center; padding: 0; border: 0; border-radius: 50%; cursor: pointer; }
.composer-tool { color: #222; background: transparent; }
.composer-tool:hover { background: #f0f0f0; }
.composer-tool svg { width: 23px; height: 23px; }
.composer-action svg { width: 17px; height: 17px; }
.retrieval-mode { color: #777; font-size: 12px; white-space: nowrap; }
.send-action { color: #fff; background: var(--ink); }
.send-action:hover { background: #333; }
.send-action:disabled { color: #a9ada9; background: #e3e6e3; cursor: default; }
.stop-action { color: #fff; background: var(--warning); }
.composer-meta { width: min(820px, 100%); display: flex; justify-content: space-between; margin: 7px auto 0; color: #878787; font-size: 10px; }
.is-empty .composer-meta { display: none; }

.source-drawer { position: fixed; top: 0; right: 0; bottom: 0; z-index: 50; width: min(var(--drawer-width), 92vw); padding: 22px; overflow-y: auto; background: var(--surface); border-left: 1px solid var(--line); box-shadow: -18px 0 50px rgba(24, 27, 24, 0.13); transform: translateX(104%); transition: transform 180ms ease; }
.source-drawer.open { transform: translateX(0); }
.drawer-header { display: flex; align-items: flex-start; justify-content: space-between; padding-bottom: 18px; border-bottom: 1px solid var(--line); }
.eyebrow { display: block; color: var(--accent-dark); font-family: "SFMono-Regular", Consolas, monospace; font-size: 10px; font-weight: 800; }
.drawer-header h2, .admin-topbar h1 { margin: 4px 0 0; font-family: "Songti TC", "Noto Serif TC", serif; font-size: 23px; }
.source-block { padding: 18px 0; border-bottom: 1px solid var(--line); }
.source-block:last-child { border-bottom: 0; }
.source-topline { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.source-index { color: var(--accent-dark); font-family: "SFMono-Regular", Consolas, monospace; font-weight: 800; }
.source-score { color: var(--muted); font-size: 11px; }
.source-block h3 { margin: 10px 0 5px; font-size: 14px; }
.source-locator { color: var(--muted); font-family: "SFMono-Regular", Consolas, monospace; font-size: 11px; overflow-wrap: anywhere; }
.source-excerpt { margin: 14px 0 0; padding-left: 13px; color: #444943; border-left: 3px solid #b9dcd4; font-size: 13px; line-height: 1.75; white-space: pre-wrap; }
.drawer-overlay { position: fixed; inset: 0; z-index: 40; background: rgba(20, 22, 20, .32); border: 0; }

/* Administration */
.admin-body { overflow: auto; }
.admin-shell { min-height: 100%; display: grid; grid-template-columns: 240px minmax(0, 1fr); }
.admin-nav { position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; padding: 20px 14px; color: #f7f8f5; background: var(--ink); }
.admin-brand { padding: 0 6px; color: #f7f8f5; text-decoration: none; }
.admin-nav-links { display: grid; gap: 3px; margin-top: 32px; }
.admin-nav-links a { min-height: 40px; display: flex; align-items: center; gap: 10px; padding: 9px 11px; color: #cdd2cc; border-radius: 6px; text-decoration: none; font-size: 13px; }
.admin-nav-links a:hover, .admin-nav-links a.active { color: #fff; background: #33352f; }
.admin-nav-links svg { width: 17px; }
.admin-chat-link { margin-top: auto; color: #cdd2cc; text-decoration: none; }
.admin-chat-link:hover { color: #fff; }
.admin-main { min-width: 0; padding: 0 36px 70px; }
.admin-topbar { min-height: 90px; display: flex; align-items: center; justify-content: space-between; gap: 24px; border-bottom: 1px solid var(--line); }
.admin-auth label { display: block; margin-bottom: 5px; color: var(--muted); font-size: 11px; }
.token-field { display: flex; align-items: center; background: #fff; border: 1px solid var(--line); border-radius: 6px; }
.token-field input { width: 210px; height: 36px; padding: 0 10px; border: 0; outline: 0; border-radius: 6px; }
.admin-section { padding: 30px 0; border-bottom: 1px solid var(--line); scroll-margin-top: 10px; }
.section-heading { display: flex; align-items: center; justify-content: space-between; gap: 18px; margin-bottom: 18px; }
.section-heading h2 { margin: 0; font-size: 18px; }
.section-heading p { margin: 5px 0 0; color: var(--muted); font-size: 12px; }
.command-button { min-height: 38px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 8px 13px; color: #fff; background: var(--ink); border: 0; border-radius: 6px; cursor: pointer; font-weight: 650; font-size: 12px; }
.command-button:hover { background: var(--accent-dark); }
.command-button svg { width: 16px; height: 16px; }
.stats-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.stat-card { min-width: 0; padding: 18px; background: #fff; border-right: 1px solid var(--line); }
.stat-card:last-child { border-right: 0; }
.stat-card span { display: block; color: var(--muted); font-size: 11px; }
.stat-card strong { display: block; margin-top: 8px; font-family: "SFMono-Regular", Consolas, monospace; font-size: 26px; }
.alert-stat strong { color: var(--warning); }
.pipeline-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; margin: 22px 0 10px; }
.pipeline-heading h3 { margin: 0; font-size: 13px; }
.pipeline-heading p { margin: 0; color: var(--muted); font-size: 11px; }
.admin-search-form { display: flex; gap: 8px; }
.admin-search-form input { min-width: 0; height: 40px; flex: 1; padding: 0 12px; background: #fff; border: 1px solid var(--line); border-radius: 6px; outline: 0; }
.admin-search-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.compact-search { max-width: 600px; margin-bottom: 14px; }
.bordered-icon { color: var(--ink-soft); background: #fff; border: 1px solid var(--line); }
.result-list { display: grid; gap: 8px; margin-top: 14px; }
.result-row { padding: 14px 0; border-top: 1px solid var(--line); }
.result-row:first-child { border-top: 0; }
.result-row h3 { margin: 0; font-size: 13px; }
.result-meta { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 5px; color: var(--muted); font-family: "SFMono-Regular", Consolas, monospace; font-size: 10px; }
.result-row p { margin: 9px 0 0; color: #4a4f49; font-size: 12px; line-height: 1.65; }
.table-shell { max-width: 100%; overflow-x: auto; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); }
.data-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.data-table th { padding: 10px 12px; color: var(--muted); background: #f5f7f4; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.data-table td { max-width: 460px; padding: 11px 12px; border-bottom: 1px solid #e7e9e6; vertical-align: top; overflow-wrap: anywhere; }
.data-table tr:last-child td { border-bottom: 0; }
.table-status { display: inline-flex; padding: 3px 6px; color: var(--accent-dark); background: var(--accent-soft); border-radius: 4px; font-size: 10px; font-weight: 700; }
.table-status.escalated { color: #9c3f2e; background: var(--warning-soft); }
.empty-state { padding: 24px; color: var(--muted); text-align: center; font-size: 12px; }
.toast { position: fixed; right: 22px; bottom: 22px; z-index: 80; max-width: min(360px, calc(100vw - 44px)); padding: 11px 14px; color: #fff; background: var(--ink); border-radius: 6px; box-shadow: 0 10px 30px rgba(0,0,0,.2); opacity: 0; pointer-events: none; transform: translateY(8px); transition: opacity 140ms ease, transform 140ms ease; font-size: 12px; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: #9e3e2d; }

@media (max-width: 980px) {
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stat-card:nth-child(2) { border-right: 0; }
  .stat-card:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
}

@media (max-width: 760px) {
  .app-shell { display: block; }
  .sidebar, .sidebar.desktop-hidden { position: fixed; inset: 0 auto 0 0; z-index: 60; width: min(300px, 88vw); margin-left: 0; transform: translateX(-104%); transition: transform 180ms ease; box-shadow: 18px 0 42px rgba(0,0,0,.16); }
  .sidebar.open { transform: translateX(0); }
  .sidebar-close, .menu-button, .sidebar.desktop-hidden + .chat-main .menu-button { display: inline-grid; }
  .sidebar-panel { display: none; }
  .chat-main { height: 100dvh; grid-template-rows: 58px minmax(0, 1fr) auto; }
  .topbar { min-height: 58px; padding: 8px 13px; }
  .topbar h1 { display: none; }
  .mode-switch { top: 8px; grid-template-columns: repeat(2, 84px); }
  .mode-option { height: 36px; font-size: 13px; }
  .service-status { min-width: 32px; }
  .messages { padding: 0 14px 26px; }
  .welcome { padding-top: 22vh; }
  .welcome h2 { font-size: 22px; }
  .prompt-list { margin-top: 150px; }
  .prompt-suggestion { font-size: 13px; }
  .message-row { grid-template-columns: 30px minmax(0, 1fr); gap: 10px; margin-top: 24px; }
  .message-avatar { width: 30px; height: 30px; }
  .message-text { font-size: 14px; }
  .composer-shell { padding: 9px 11px max(10px, env(safe-area-inset-bottom)); }
  .is-empty .composer-shell { top: 35%; padding: 0 14px; }
  .composer { min-height: 52px; padding: 5px 6px; }
  .composer-tool, .composer-action { width: 38px; height: 38px; flex-basis: 38px; }
  .retrieval-mode { display: none; }
  .composer-meta span:first-child { display: none; }
  .composer-meta { justify-content: flex-end; }
  .admin-shell { display: block; }
  .admin-nav { position: static; width: 100%; height: auto; padding: 12px; }
  .admin-brand { display: none; }
  .admin-nav-links { grid-template-columns: repeat(4, minmax(0, 1fr)); margin: 0; overflow-x: auto; }
  .admin-nav-links a { justify-content: center; padding: 8px; }
  .admin-nav-links span { display: none; }
  .admin-chat-link { justify-content: center; margin-top: 8px; }
  .admin-main { padding: 0 16px 50px; }
  .admin-topbar { align-items: flex-start; flex-direction: column; padding: 20px 0; }
  .admin-auth, .token-field, .token-field input { width: 100%; }
  .section-heading { align-items: flex-start; }
  .pipeline-heading { align-items: flex-start; flex-direction: column; gap: 4px; }
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .admin-search-form { align-items: stretch; }
  .admin-search-form .command-button span { display: none; }
}

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