:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efede6;
  --ink: #1c2230;
  --ink-2: #545b69;
  --ink-3: #8a8f99;
  --line: #e2dfd6;
  --accent: #1f4e79;
  --accent-ink: #ffffff;
  --accent-soft: #e5edf5;
  --cite: #1f4e79;
  --cite-bg: #e8eff7;
  --warn: #8a5a00;
  --shadow: 0 1px 2px rgba(20, 24, 33, .06), 0 8px 24px rgba(20, 24, 33, .06);
  --radius: 14px;
  --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --serif: "Source Serif 4", Georgia, "Times New Roman", serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --surface: #1a1e26;
    --surface-2: #222733;
    --ink: #e8eaee;
    --ink-2: #aab0bb;
    --ink-3: #747b88;
    --line: #2c3240;
    --accent: #7fb0e0;
    --accent-ink: #0d1520;
    --accent-soft: #1e2a3a;
    --cite: #9cc3ea;
    --cite-bg: #1f2b3b;
    --warn: #e0b45c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.6 var(--font); -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--accent); }

/* ---------- layout ---------- */
.app { display: grid; grid-template-rows: auto 1fr; height: 100vh; height: 100dvh; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 10px 20px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand .mark {
  width: 32px; height: 32px; border-radius: 8px; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font: 600 15px var(--serif); flex: none;
}
.brand h1 { font: 600 16px/1.2 var(--font); margin: 0; white-space: nowrap; }
.brand p { margin: 0; font-size: 12px; color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .spacer { flex: 1; }
.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 9px; padding: 7px 12px;
  font-size: 13px; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; color: var(--ink);
}
.btn:hover { background: var(--surface-2); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--ink-2); }
.btn.ghost:hover { background: var(--surface-2); color: var(--ink); }
.btn svg { width: 16px; height: 16px; }

.main { display: grid; grid-template-columns: 1fr; min-height: 0; }
.main.with-pdf { grid-template-columns: minmax(0, 1fr) minmax(380px, 46%); }

.chat { display: grid; grid-template-rows: 1fr auto; min-height: 0; }
.scroll { overflow-y: auto; padding: 28px 20px 12px; }
.thread { max-width: 780px; margin: 0 auto; }

/* ---------- empty state ---------- */
.welcome { padding: 6vh 0 20px; }
.welcome h2 { font: 600 30px/1.25 var(--serif); margin: 0 0 8px; letter-spacing: -.01em; }
.welcome p { color: var(--ink-2); margin: 0 0 22px; max-width: 560px; }
.examples { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 10px; }
.example {
  text-align: left; background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 14px; font-size: 14px; color: var(--ink); line-height: 1.45;
}
.example:hover { border-color: var(--accent); box-shadow: var(--shadow); }
.example small { display: block; color: var(--ink-3); font-size: 12px; margin-bottom: 3px; }

/* ---------- messages ---------- */
.msg { margin: 0 0 22px; }
.msg.user .bubble {
  margin-left: auto; max-width: 85%; width: fit-content; background: var(--accent); color: var(--accent-ink);
  padding: 10px 14px; border-radius: 14px 14px 4px 14px; white-space: pre-wrap;
}
.msg.bot .card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px 20px; box-shadow: var(--shadow);
}
.answer { font-size: 15px; }
.answer > :first-child { margin-top: 0; }
.answer > :last-child { margin-bottom: 0; }
.answer h1, .answer h2, .answer h3 { font: 600 16px/1.35 var(--font); margin: 18px 0 6px; }
.answer p { margin: 0 0 10px; }
.answer ul, .answer ol { margin: 0 0 10px; padding-left: 22px; }
.answer li { margin: 3px 0; }
.answer blockquote {
  margin: 10px 0; padding: 6px 14px; border-left: 3px solid var(--accent); background: var(--surface-2);
  border-radius: 0 8px 8px 0; color: var(--ink-2); font-family: var(--serif);
}
.answer code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 13px; }
.answer table { border-collapse: collapse; margin: 10px 0; font-size: 14px; display: block; overflow-x: auto; }
.answer th, .answer td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
.answer th { background: var(--surface-2); }
.answer a.cite {
  display: inline; text-decoration: none; background: var(--cite-bg); color: var(--cite);
  font-size: 12.5px; font-weight: 500; padding: 2px 7px; border-radius: 6px; line-height: 1.9;
  -webkit-box-decoration-break: clone; box-decoration-break: clone;
}
.answer a.cite:hover { outline: 1px solid var(--cite); }

.steps { font-size: 13px; color: var(--ink-3); margin: 0 0 10px; }
.steps summary { cursor: pointer; list-style: none; display: inline-flex; align-items: center; gap: 6px; }
.steps summary::-webkit-details-marker { display: none; }
.steps ol { margin: 6px 0 0; padding-left: 20px; }
.live { display: flex; align-items: center; gap: 10px; color: var(--ink-2); font-size: 14px; }
.spinner {
  width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite; flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }
.tools { display: flex; gap: 4px; margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--line); }
.error { color: var(--warn); }

/* ---------- composer ---------- */
.composer { padding: 10px 20px 18px; background: linear-gradient(to top, var(--bg) 70%, transparent); }
.composer form {
  max-width: 780px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end;
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 8px 8px 8px 16px;
  box-shadow: var(--shadow);
}
.composer form:focus-within { border-color: var(--accent); }
.composer textarea {
  flex: 1; border: 0; outline: 0; resize: none; background: transparent; color: var(--ink);
  font: 15px/1.5 var(--font); padding: 7px 0; max-height: 200px;
}
.send {
  border: 0; background: var(--accent); color: var(--accent-ink); border-radius: 11px; width: 40px; height: 40px;
  display: grid; place-items: center; flex: none;
}
.send:disabled { opacity: .4; cursor: default; }
.send svg { width: 18px; height: 18px; }
.fineprint { max-width: 780px; margin: 7px auto 0; font-size: 11.5px; color: var(--ink-3); text-align: center; }

/* ---------- pdf pane ---------- */
.pdfpane { display: none; border-left: 1px solid var(--line); background: var(--surface); min-height: 0; grid-template-rows: auto 1fr; }
.main.with-pdf .pdfpane { display: grid; }
.pdfbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); font-size: 13px; }
.pdfbar strong { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pdfpane iframe { width: 100%; height: 100%; border: 0; background: #525659; }

/* ---------- sections drawer ---------- */
.drawer-bg { position: fixed; inset: 0; background: rgba(10, 14, 20, .35); display: none; z-index: 20; }
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0; width: min(420px, 92vw); background: var(--surface);
  border-right: 1px solid var(--line); z-index: 21; transform: translateX(-102%); transition: transform .2s ease;
  display: grid; grid-template-rows: auto auto 1fr;
}
.drawer.open { transform: none; }
.drawer.open + .drawer-bg, .drawer-bg.show { display: block; }
.drawer header { display: flex; align-items: center; padding: 14px 16px 6px; }
.drawer header h3 { margin: 0; font-size: 15px; flex: 1; }
.drawer input {
  margin: 4px 16px 10px; padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--ink); font: 14px var(--font); outline: 0;
}
.drawer input:focus { border-color: var(--accent); }
.seclist { overflow-y: auto; padding: 0 8px 16px; }
.seclist .group { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); padding: 12px 8px 4px; }
.sec { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 7px 8px; border-radius: 8px; font-size: 13.5px; }
.sec:hover { background: var(--surface-2); }
.sec b { font-weight: 600; margin-right: 6px; }
.sec span { color: var(--ink-2); }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form {
  width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 18px;
  padding: 28px; box-shadow: var(--shadow);
}
.login h1 { font: 600 22px/1.3 var(--serif); margin: 14px 0 4px; }
.login p { color: var(--ink-2); margin: 0 0 18px; font-size: 14px; }
.login input {
  width: 100%; padding: 11px 13px; border: 1px solid var(--line); border-radius: 10px; font: 15px var(--font);
  background: var(--bg); color: var(--ink); outline: 0; margin-bottom: 12px;
}
.login input:focus { border-color: var(--accent); }
.login button { width: 100%; border: 0; background: var(--accent); color: var(--accent-ink); padding: 11px; border-radius: 10px; font-weight: 600; }
.login .err { color: var(--warn); font-size: 13px; margin: -4px 0 12px; }

/* ---------- small screens ---------- */
@media (max-width: 900px) {
  .main.with-pdf { grid-template-columns: 1fr; }
  .main.with-pdf .pdfpane { position: fixed; inset: 0; z-index: 15; }
}
@media (max-width: 600px) {
  .topbar { padding: 8px 12px; }
  .brand p, .hide-sm { display: none; }
  .scroll { padding: 18px 12px 8px; }
  .composer { padding: 8px 12px 12px; }
  .msg.bot .card { padding: 14px; }
  .welcome h2 { font-size: 24px; }
}
