:root {
  --bg: #f7f7f5; --card: #ffffff; --text: #1d2327; --muted: #6b7378; --line: #e1e3e0;
  --accent: #0f6b5c; --accent-text: #ffffff; --user: #e6f1ee; --warn: #b3261e; --link: #0b5f93;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #14181a; --card: #1c2225; --text: #e7eaeb; --muted: #98a2a7; --line: #2b3337;
    --accent: #3fb8a1; --accent-text: #0b1513; --user: #1f322e; --warn: #ff8a80; --link: #7cc4f0; }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.grow { flex: 1; }
.err { color: var(--warn); min-height: 1.4em; margin: 0; }

button { font: inherit; cursor: pointer; border: 0; border-radius: 8px; padding: 9px 16px; background: var(--accent); color: var(--accent-text); }
button:disabled { opacity: .5; cursor: default; }
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); padding: 6px 12px; }
input, textarea { font: inherit; color: var(--text); background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; width: 100%; }
input:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

#login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 28px; }
.login { width: min(380px, 100%); display: grid; gap: 12px; }
.login h1 { margin: 0; font-size: 1.3rem; }

#app { height: 100%; display: flex; flex-direction: column; max-width: 860px; margin: 0 auto; padding: 0 16px; }
header { display: flex; align-items: center; gap: 10px; padding: 14px 0; border-bottom: 1px solid var(--line); }
#count { font-size: .85rem; }
main { flex: 1; overflow-y: auto; padding: 18px 0; display: flex; flex-direction: column; gap: 16px; }
.empty { margin: auto 0; text-align: center; padding: 24px 0; }
.empty h2 { font-size: 1.25rem; margin: 0 0 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.chips button { background: var(--card); color: var(--text); border: 1px solid var(--line); text-align: left; font-size: .92rem; padding: 8px 12px; }
.chips button:hover { border-color: var(--accent); }

.msg { max-width: 100%; }
.msg.user { align-self: flex-end; background: var(--user); border-radius: 14px 14px 4px 14px; padding: 10px 14px; max-width: 85%; white-space: pre-wrap; }
.msg.bot { background: var(--card); border: 1px solid var(--line); border-radius: 14px 14px 14px 4px; padding: 14px 16px; }
.msg.bot.error { border-color: var(--warn); color: var(--warn); }
.sources { margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--line); font-size: .85rem; color: var(--muted); }
.sources button.wl { margin: 2px 4px 2px 0; }

#composer { display: flex; gap: 8px; align-items: flex-end; padding-top: 10px; }
#composer textarea { resize: none; max-height: 160px; }
.foot { font-size: .78rem; margin: 8px 0 12px; text-align: center; }

.md { overflow-wrap: anywhere; }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.25; margin: 1.1em 0 .4em; }
.md h1 { font-size: 1.3rem; } .md h2 { font-size: 1.15rem; } .md h3, .md h4 { font-size: 1.02rem; }
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p { margin: .5em 0; }
.md ul, .md ol { padding-left: 1.4em; margin: .5em 0; }
.md code { background: var(--bg); border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; font-size: .9em; }
.md blockquote { margin: .6em 0; padding: 6px 12px; border-left: 3px solid var(--accent); background: var(--bg); border-radius: 0 8px 8px 0; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.md a { color: var(--link); }
.md .tbl { overflow-x: auto; margin: .6em 0; }
.md table { border-collapse: collapse; font-size: .92rem; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; vertical-align: top; }
.md th { background: var(--bg); }
button.wl { background: transparent; color: var(--link); border: 1px solid var(--line); border-radius: 999px; padding: 0 9px; font-size: .9em; line-height: 1.6; display: inline; }
button.wl:hover { border-color: var(--link); }
button.wl.dead { color: var(--muted); cursor: default; }

#drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(640px, 100%); background: var(--card); border-left: 1px solid var(--line); box-shadow: -8px 0 24px rgba(0,0,0,.18); display: flex; flex-direction: column; z-index: 10; }
.drawer-head { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
#drawerBody { padding: 16px; overflow-y: auto; flex: 1; }
