:root {
  color-scheme: dark;
  --paper: #181818; --surface: #222222; --ink: #ededed; --ink-deep: #e6e6e6;
  --muted: #aaaaaa; --line: #444444; --success: #d2d2d2; --danger: #ffaaa2;
  --control-border: #777777; --hover: #303030; --button-ink: #181818;
  --font-body: "Microsoft JhengHei UI", "Noto Sans TC", "PingFang TC", sans-serif;
  --font-display: Georgia, "Noto Serif TC", "PMingLiU", serif;
  --font-mono: "Cascadia Code", Consolas, monospace;
}
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; width: 100%; min-width: 320px; min-height: 100%; }
body { background: var(--paper); color: var(--ink-deep); font-family: var(--font-body); -webkit-font-smoothing: antialiased; }
button, input, textarea { font: inherit; }
button { color: inherit; cursor: pointer; }
button, a { -webkit-tap-highlight-color: transparent; }
button:disabled { cursor: wait; opacity: .5; }
[hidden] { display: none !important; }
button:focus-visible, a:focus-visible, input:focus-visible, textarea:focus-visible, summary:focus-visible { outline: 2px solid var(--success); outline-offset: 4px; }
.visually-hidden {
  position: absolute !important; width: 1px !important; height: 1px !important; padding: 0 !important; margin: -1px !important;
  overflow: hidden !important; clip: rect(0, 0, 0, 0) !important; white-space: nowrap !important; border: 0 !important;
}
svg { fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* Charcoal workspace: only the conversation area scrolls. */
.app-shell { height: 100dvh; display: grid; grid-template-rows: auto minmax(0, 1fr); }
.topbar { min-height: 80px; display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 16px clamp(20px, 4vw, 64px); border-bottom: 1px solid var(--line); }
.brand { display: inline-flex; align-items: center; gap: 22px; min-width: 0; color: var(--ink); text-decoration: none; }
.brand-wordmark { font-family: Georgia, serif; font-size: 34px; font-weight: 500; letter-spacing: -1.8px; line-height: 1; }
.brand-caption { border-left: 1px solid var(--line); padding-left: 22px; font-size: 12px; color: var(--muted); white-space: nowrap; }
.topbar-actions { display: flex; align-items: center; gap: 18px; }
.service-state { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); font-size: 12px; white-space: nowrap; }
.status-dot { width: 6px; height: 6px; flex: 0 0 auto; border-radius: 50%; background: currentColor; }
.service-state.is-ready, .service-state.is-live { color: var(--success); }
.service-state.is-live .status-dot { animation: thinking 1.2s ease-in-out infinite; }
.service-state.is-pending { color: #ecc5aa; }
.service-state.is-error { color: var(--danger); font-size: 12px; }
.new-chat-button, .logout-button { display: inline-flex; align-items: center; justify-content: center; gap: 7px; min-height: 42px; padding: 0 12px; background: transparent; border: 1px solid transparent; border-radius: 6px; font-size: 13px; }
.new-chat-button { border-color: var(--control-border); }
.logout-button { color: var(--muted); }
.new-chat-button:hover, .logout-button:hover { background: var(--hover); color: var(--ink); }
.new-chat-button svg, .logout-button svg { width: 17px; height: 17px; }
.logout-form { margin: 0; }
.chat-workspace { min-height: 0; display: flex; flex-direction: column; padding: 0 clamp(24px, 4vw, 64px); }
.conversation { width: 100%; min-height: 0; flex: 1; display: grid; grid-template-rows: minmax(0, 1fr) auto; margin: 0 auto; }
.conversation-scroll { min-height: 0; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; scrollbar-color: #777777 transparent; }
.welcome { min-height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: flex-start; padding: 40px 10px 100px; }
.welcome.is-hidden { display: none; }
.welcome h1 { margin: 0; font-family: var(--font-display); font-size: clamp(28px, 3.3vw, 40px); font-weight: 500; line-height: 1.5; letter-spacing: .02em; }
.messages { display: grid; gap: 40px; padding: 36px 8px 40px; }
.messages:empty { display: none; }
.message-row { min-width: 0; width: 100%; }
.message-row-user { display: grid; justify-items: end; gap: 8px; padding-left: min(12vw, 100px); }
.message-label { color: var(--muted); font-size: 11px; }
.message-bubble { max-width: 100%; padding: 13px 18px; border-radius: 12px; }
.user-bubble { color: var(--ink-deep); background: #303030; }
.user-bubble p { margin: 0; font-size: 15px; line-height: 1.8; white-space: pre-wrap; overflow-wrap: anywhere; }
.message-row-assistant { display: grid; grid-template-columns: minmax(0, 1fr); gap: 15px; }
.assistant-rail { color: var(--ink); font-family: Georgia, serif; font-size: 19px; letter-spacing: -.7px; }
.assistant-content { min-width: 0; }
.thinking { height: 28px; display: flex; align-items: center; gap: 5px; }
.thinking i, .loading-bars i { display: block; width: 4px; height: 4px; border-radius: 50%; background: var(--success); animation: thinking 1.2s ease-in-out infinite; }
.thinking i:nth-child(2), .loading-bars i:nth-child(2) { animation-delay: .15s; }
.thinking i:nth-child(3), .loading-bars i:nth-child(3) { animation-delay: .3s; }
@keyframes thinking { 0%, 70%, 100% { opacity: .3; } 35% { opacity: 1; } }

.markdown-body { font-size: 15px; line-height: 1.95; overflow-wrap: anywhere; }
.markdown-body > :first-child { margin-top: 0; }
.markdown-body > :last-child { margin-bottom: 0; }
.markdown-body p { margin: 0 0 18px; }
.markdown-body h1, .markdown-body h2, .markdown-body h3 { margin: 24px 0 10px; font-weight: 650; line-height: 1.5; }
.markdown-body h1 { font-size: 22px; }
.markdown-body h2 { font-size: 19px; }
.markdown-body h3 { font-size: 16px; }
.markdown-body ul, .markdown-body ol { padding-left: 24px; margin: 12px 0 18px; }
.markdown-body li + li { margin-top: 5px; }
.markdown-body a { color: var(--success); text-decoration-thickness: 1px; text-underline-offset: 3px; }
.markdown-body blockquote { margin: 18px 0; padding: 4px 16px; border-left: 2px solid #777777; color: var(--muted); }
.markdown-body code { padding: 2px 5px; background: #292929; border-radius: 3px; font-family: var(--font-mono); font-size: .85em; }
.markdown-body pre { max-width: 100%; overflow-x: auto; padding: 16px; background: #292929; border: 1px solid var(--line); border-radius: 6px; line-height: 1.6; }
.markdown-body pre code { padding: 0; background: none; }

.source-panel { margin-top: 24px; }
.source-toggle { display: inline-flex; align-items: center; gap: 7px; min-height: 40px; padding: 0; border: 0; background: transparent; color: var(--success); font-size: 12px; }
.source-toggle svg { width: 14px; height: 14px; transition: transform .15s; }
.source-toggle[aria-expanded="true"] svg { transform: rotate(90deg); }
.source-list { display: none; }
.source-list.is-open { display: grid; gap: 8px; margin-top: 8px; }
.source-card { display: grid; grid-template-columns: 24px minmax(0, 1fr) auto; gap: 12px; padding: 16px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); }
.source-index { color: var(--muted); font-family: var(--font-mono); font-size: 11px; line-height: 1.8; }
.source-copy { min-width: 0; }
.source-copy strong { display: block; font-size: 12px; font-weight: 600; overflow-wrap: anywhere; }
.source-copy small { display: block; margin-top: 3px; color: var(--muted); font-size: 11px; }
.source-copy p { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; margin: 10px 0 0; color: var(--muted); font-size: 12px; line-height: 1.8; overflow-wrap: anywhere; }
.source-score { color: var(--muted); font-family: var(--font-mono); font-size: 10px; }
.source-excerpts { margin-top: 12px; color: var(--success); font-size: 12px; }
.source-excerpts summary { cursor: pointer; padding: 4px 0; }
.source-copy .source-excerpts p { display: block; overflow: visible; -webkit-line-clamp: unset; white-space: pre-wrap; }

.feedback-panel { margin-top: 20px; padding-top: 12px; border-top: 1px solid var(--line); }
.feedback-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.feedback-actions > span { color: var(--muted); font-size: 11px; margin-right: 4px; }
.feedback-button, .feedback-undo { min-height: 36px; padding: 7px 10px; border: 1px solid transparent; border-radius: 5px; background: transparent; font-size: 12px; }
.feedback-button { color: var(--ink); }
.feedback-undo { color: var(--muted); }
.feedback-button:hover, .feedback-undo:hover { background: var(--hover); }
.feedback-button[aria-pressed="true"] { background: #3a3a3a; border-color: #999999; }
.feedback-editor { margin-top: 12px; padding: 16px; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; }
.feedback-editor label { display: block; color: var(--ink); font-size: 13px; }
.feedback-editor textarea { display: block; width: 100%; min-height: 96px; margin: 12px 0; padding: 12px; resize: vertical; border: 1px solid var(--control-border); border-radius: 5px; background: var(--paper); color: var(--ink-deep); line-height: 1.7; font-size: 14px; }
.feedback-status { font-size: 12px; color: var(--muted); margin: 8px 0 0; }
.feedback-status:empty { display: none; }
.inline-error, .setup-notice { display: flex; align-items: flex-start; gap: 10px; padding: 13px 16px; border: 1px solid #71554b; border-radius: 6px; background: #302622; color: #ecc5aa; font-size: 13px; line-height: 1.7; }
.inline-error { margin-top: 12px; }
.inline-error svg, .setup-notice svg { width: 18px; height: 18px; flex: 0 0 auto; margin-top: 2px; }
.setup-notice { width: 100%; margin: 18px auto 0; }
.setup-notice strong, .setup-notice span { display: block; }

.composer-wrap { padding: 16px 0 22px; background: var(--paper); }
.composer { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: end; gap: 12px; min-height: 64px; padding: 10px 10px 10px 18px; background: var(--surface); border: 1px solid var(--control-border); border-radius: 10px; transition: border-color .15s; }
.composer:focus-within { border-color: var(--success); }
.composer textarea { width: 100%; min-height: 42px; max-height: 150px; padding: 10px 0 8px; resize: none; overflow-y: auto; border: 0; outline: none; background: transparent; color: var(--ink-deep); font-size: 16px; line-height: 1.5; }
.composer textarea::placeholder { color: #aaaaaa; }
.send-button { width: 42px; height: 42px; display: grid; place-items: center; padding: 0; border: 0; border-radius: 6px; background: var(--ink); color: var(--button-ink); }
.send-button:hover:not(:disabled) { background: #d0d0d0; }
.send-button svg { width: 19px; height: 19px; }
.send-button .loading-bars { display: none; align-items: center; gap: 3px; }
.send-button .loading-bars i { background: var(--button-ink); }
.composer.is-busy .send-icon { display: none; }
.composer.is-busy .loading-bars { display: flex; }
.composer-note { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 4px 12px; margin: 10px 2px 0; color: var(--muted); font-size: 11px; line-height: 1.6; }

.login-page { min-height: 100dvh; display: grid; place-items: center; padding: 32px 24px; }
.login-card { width: min(100%, 380px); }
.login-card .brand-wordmark { color: var(--ink); font-size: 42px; margin-bottom: 48px; }
.login-card h1 { margin: 0 0 12px; font-family: var(--font-display); font-weight: 500; font-size: 28px; letter-spacing: .03em; }
.login-copy { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.8; }
.login-form { display: grid; gap: 16px; margin-top: 36px; }
.login-field { display: grid; gap: 8px; }
.login-field label { font-size: 12px; }
.login-field input { width: 100%; min-height: 50px; padding: 10px 14px; border: 1px solid var(--control-border); border-radius: 6px; background: var(--surface); color: var(--ink-deep); }
.login-error { color: var(--danger); font-size: 12px; line-height: 1.6; }
.login-error:empty { display: none; }
.login-submit { min-height: 48px; color: var(--button-ink); background: var(--ink); border: 0; border-radius: 6px; font-size: 14px; }
.login-submit:hover { background: #d0d0d0; }
.login-security { margin: 26px 0 0; color: var(--muted); font-size: 11px; line-height: 1.8; }

@media (max-width: 560px) {
  .topbar { min-height: 68px; padding: 12px 16px; gap: 8px; }
  .brand { gap: 12px; } .brand-wordmark { font-size: 29px; } .brand-caption { padding-left: 12px; font-size: 11px; }
  .topbar-actions { gap: 4px; }
  .new-chat-button, .logout-button { width: 40px; padding: 0; }
  .new-chat-button span, .logout-button span { display: none; }
  .service-state { font-size: 11px; gap: 5px; margin-right: 6px; }
  .chat-workspace { padding: 0 16px; }
  .welcome { padding: 32px 0 64px; } .welcome h1 { font-size: 30px; }
  .messages { padding: 28px 0; gap: 32px; } .message-row-user { padding-left: 24px; }
  .source-card { padding: 12px; gap: 8px; grid-template-columns: 22px minmax(0, 1fr); } .source-score { display: none; }
  .composer-wrap { padding: 12px 0 max(12px, env(safe-area-inset-bottom)); } .composer { padding-left: 12px; }
  .keyboard-hint { display: none; } .composer-note { justify-content: center; font-size: 10px; }
}
@media (max-width: 420px) { .brand-caption { display: none; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation: none !important; transition: none !important; }
}
