:root {
  --bg: #f6f7f9; --panel: #ffffff; --text: #1d2430; --muted: #5f6b7a; --line: #dde2e8;
  --accent: #0b6e4f; --accent-text: #ffffff; --user: #e7f2ee; --error: #b42318; --code: #f1f3f6; --warn: #b54708; --warn-bg: #fff6e8;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181d; --panel: #1c2127; --text: #e6e9ed; --muted: #9aa5b1; --line: #2e353d;
    --accent: #3fb68b; --accent-text: #0d1512; --user: #1f3a30; --error: #ff8a80; --code: #232a31; --warn: #f5b04b; --warn-bg: #2e2416;
  }
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Microsoft JhengHei", sans-serif;
  background: var(--bg); color: var(--text); height: 100vh; display: flex; flex-direction: column; }
header { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 16px; padding: 10px 20px; background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { font-weight: 650; font-size: 17px; white-space: nowrap; }
.head-right { display: flex; align-items: center; gap: 16px; justify-content: flex-end; }
.data-info { justify-self: center; display: flex; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
.stat { display: flex; flex-direction: column; align-items: center; padding: 4px 12px; line-height: 1.25; white-space: nowrap; }
.stat + .stat { border-left: 1px solid var(--line); }
.stat-num { font-size: 16px; font-weight: 650; color: var(--accent); font-variant-numeric: tabular-nums; }
.stat-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat-note { font-size: 11px; color: var(--muted); }
.status { color: var(--muted); font-size: 13px; white-space: nowrap; }
.logout { color: var(--muted); font-size: 13px; }
main { flex: 1; display: flex; min-height: 0; }
aside { width: 300px; padding: 16px 20px; background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto; }
aside h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 18px 0 8px; }
aside h2:first-child { margin-top: 0; }
label { display: block; font-size: 13px; margin: 10px 0 4px; }
label.check { display: flex; gap: 6px; align-items: center; }
input[type=password], select, textarea { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--bg); color: var(--text); font: inherit; }
.hint { color: var(--muted); font-size: 12px; margin: 6px 0; }
button { padding: 8px 16px; border: 0; border-radius: 6px; background: var(--accent); color: var(--accent-text); font: inherit; font-weight: 600; cursor: pointer; }
button:disabled { opacity: .5; cursor: default; }
button.secondary { background: transparent; color: var(--text); border: 1px solid var(--line); font-weight: 500; }
button.small { padding: 5px 12px; font-size: 13px; }
dialog#settings { width: min(380px, calc(100vw - 32px)); padding: 22px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--panel); color: var(--text); box-shadow: 0 12px 32px rgba(0,0,0,.25); }
dialog#settings::backdrop { background: rgba(0,0,0,.4); }
dialog#settings h2 { font-size: 16px; margin: 0 0 12px; }
.key-tabs { display: flex; gap: 2px; margin-bottom: 12px; border-bottom: 1px solid var(--line); }
button.key-tab { flex: 1; padding: 8px 4px; background: transparent; color: var(--muted); border: 0; border-bottom: 2px solid transparent; border-radius: 0; font-size: 13px; font-weight: 500; margin-bottom: -1px; }
button.key-tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--text); font-weight: 700; }
.key-row { display: flex; gap: 6px; align-items: stretch; margin-top: 4px; }
.key-row input { flex: 1; min-width: 0; }
dialog#settings button[type=submit] { width: 100%; margin-top: 10px; }
#sidebar { display: flex; flex-direction: column; }
.sidebar-foot { margin-top: auto; padding-top: 12px; border-top: 1px solid var(--line); }
.sidebar-foot label { margin-top: 0; }
.key-notice { font-size: 12px; color: var(--muted); margin-bottom: 12px; }
.key-notice.missing { color: var(--text); padding: 10px 12px; border: 1px solid var(--accent); border-left-width: 4px; border-radius: 6px; background: var(--user); }
.provider-warning { font-size: 12px; margin: 8px 0 4px; padding: 8px 10px; border-radius: 6px; border: 1px solid var(--warn);
  border-left-width: 4px; background: var(--warn-bg); color: var(--text); }
.provider-warning[hidden] { display: none; }
.provider-warning b { color: var(--warn); }
button.link { background: none; border: 0; padding: 0; color: var(--accent); font-size: inherit; font-weight: 600; text-decoration: underline; cursor: pointer; }
#newChat { width: 100%; }
#chatList { list-style: none; padding: 0; margin: 0 0 8px; }
#chatList li { display: flex; align-items: center; border-radius: 6px; }
#chatList li:hover, #chatList li.active { background: var(--user); }
#chatList button { background: transparent; color: var(--text); font-weight: 400; border-radius: 6px; }
.chat-title { flex: 1; min-width: 0; text-align: left; padding: 7px 8px; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#chatList .chat-delete { padding: 4px 8px; color: var(--muted); font-size: 16px; line-height: 1; visibility: hidden; }
#chatList li:hover .chat-delete, #chatList li.active .chat-delete, #chatList .chat-delete:focus { visibility: visible; }
#chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 860px; padding: 12px 16px; border-radius: 10px; background: var(--panel); border: 1px solid var(--line); overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user); border-color: transparent; white-space: pre-wrap; }
.msg.pending { color: var(--muted); font-style: italic; }
.msg.error, .error { color: var(--error); }
.msg.intro { color: var(--muted); }
.msg p:first-child { margin-top: 0; } .msg p:last-child { margin-bottom: 0; }
.msg table { border-collapse: collapse; margin: 8px 0; font-size: 13px; }
.msg th, .msg td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; vertical-align: top; }
.msg th { background: var(--code); }
.working { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 8px; font-size: 13px; }
.working summary { cursor: pointer; color: var(--muted); }
.working h4 { margin: 12px 0 4px; font-size: 13px; }
.working pre { background: var(--code); padding: 8px; border-radius: 6px; overflow-x: auto; white-space: pre-wrap; font-size: 12px; }
.tablewrap { overflow-x: auto; }
form#askForm { display: flex; gap: 10px; padding: 14px 20px; background: var(--panel); border-top: 1px solid var(--line); }
form#askForm textarea { resize: vertical; }
.login { margin: auto; width: min(360px, calc(100% - 32px)); background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 28px; }
.login h1 { font-size: 20px; margin: 0 0 4px; }
.login input { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--text); font: inherit; }
.login button { width: 100%; margin-top: 18px; }
@media (max-width: 800px) {
  header { grid-template-columns: 1fr auto; }
  .data-info { display: none; }
  main { flex-direction: column; }
  aside { width: auto; border-right: 0; border-bottom: 1px solid var(--line); max-height: 40vh; }
  #messages { padding: 12px 16px; }
  form#askForm { padding: 12px 16px; }
}
