:root {
  --page: #eef1f0; --dot: #d5dbd9;
  --bg: #f8faf9; --side: #f1f3f2; --card: #ffffff;
  --fg: #1f2523; --muted: #6b7571; --line: #e1e6e4; --hover: #e7ebe9; --active: #e3e8e6;
  --accent: #0b5b51; --accent-hover: #084a42; --on-accent: #ffffff;
  --bubble: #eceeed;
  --ok: #15803d; --warn: #a16207; --warn-bg: #fdf6e3;
  --err: #8f2a2a; --err-bg: #fdeceb; --err-line: #f6d4d1;
  --shadow: 0 1px 2px rgba(20, 30, 27, .06), 0 8px 24px rgba(20, 30, 27, .08);
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --page: #0c0f0e; --dot: #1d2321;
    --bg: #131716; --side: #181d1b; --card: #1b211f;
    --fg: #e8edeb; --muted: #94a09b; --line: #2a312f; --hover: #222927; --active: #262e2c;
    --accent: #2fa392; --accent-hover: #43b9a7; --on-accent: #06201c;
    --bubble: #232a28;
    --ok: #4ade80; --warn: #fbbf24; --warn-bg: #2a2310;
    --err: #f5a3a3; --err-bg: #2c1717; --err-line: #4a2626;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0; padding: 8px; color: var(--fg);
  background-color: var(--page);
  background-image: radial-gradient(var(--dot) 1px, transparent 1px);
  background-size: 14px 14px;
  font: 13px/1.55 "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.ico { width: 15px; height: 15px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* ---- shell ---------------------------------------------------------------- */
.shell {
  height: calc(100dvh - 16px); display: grid; grid-template-columns: 220px minmax(0, 1fr);
  background: var(--bg); border: 1px solid var(--line); border-radius: 16px;
  box-shadow: var(--shadow); overflow: hidden;
}
.shell.collapsed { grid-template-columns: minmax(0, 1fr); }
.shell.collapsed .sidebar { display: none; }
.icon-btn.open-side { display: none; }
.shell.collapsed .icon-btn.open-side { display: inline-flex; }

/* ---- sidebar -------------------------------------------------------------- */
.sidebar { background: var(--side); display: flex; flex-direction: column; padding: 14px 10px 12px; min-height: 0; border-right: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 7px; padding: 2px 4px 14px; }
.brand-ico { width: 18px; height: 18px; color: var(--accent); }
.brand-name { flex: 1; font-size: 13px; font-weight: 500; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; width: 26px; height: 26px; border: 0; border-radius: 7px; background: transparent; color: var(--muted); }
.icon-btn:hover { background: var(--hover); color: var(--fg); }

.nav-btn { display: flex; align-items: center; gap: 8px; width: 100%; padding: 6px 8px; border: 0; border-radius: 8px; background: transparent; text-align: left; }
.nav-btn:hover { background: var(--hover); }
.side-label { margin: 18px 8px 4px; font-size: 10px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.history { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 1px; }
.hist-row { position: relative; display: flex; align-items: center; border-radius: 7px; }
.hist-row:hover, .hist-row:focus-within { background: var(--hover); }
.history .hist-open {
  flex: 1; min-width: 0; padding: 5px 8px; border: 0; border-radius: 7px; background: transparent; text-align: left;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--fg);
}
.history .hist-open[aria-current="true"] { background: var(--active); }
.hist-del {
  flex: none; display: inline-flex; align-items: center; justify-content: center; min-width: 26px; height: 26px; margin-right: 2px;
  padding: 0 6px; border: 0; border-radius: 6px; background: transparent; color: var(--muted); font-size: 12px; opacity: 0;
}
.hist-del .ico { width: 14px; height: 14px; }
.hist-row:hover .hist-del, .hist-row:focus-within .hist-del, .hist-del.armed { opacity: 1; }
.hist-del:hover { color: var(--err); background: var(--err-bg); }
.hist-del.armed { color: var(--err); background: var(--err-bg); font-weight: 600; }
@media (hover: none) { .hist-del { opacity: 1; } }

.side-foot { border-top: 1px solid var(--line); padding: 12px 6px 0; margin-top: 8px; }
.foot-title { font-size: 12px; color: var(--fg); }
.foot-sub { font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.foot-link { display: flex; align-items: center; gap: 8px; padding: 5px 2px; color: var(--fg); text-decoration: none; border-radius: 6px; }
.foot-link:hover { background: var(--hover); }
.foot-link .ico:first-child { color: var(--accent); }
.foot-link span { flex: 1; }
.foot-link .ext { width: 12px; height: 12px; color: var(--muted); }

/* ---- chat column ---------------------------------------------------------- */
.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 12px 22px 10px; min-height: 44px; }
.topbar h1 { flex: 1; margin: 0; font-size: 13px; font-weight: 400; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn.open-side { margin-left: -8px; }
.conn { font-size: 11px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.conn::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--muted); }
.conn.ok::before { background: var(--ok); }
.conn.bad { color: var(--err); }
.conn.bad::before { background: var(--err); }

main { flex: 1; min-height: 0; overflow-y: auto; padding: 14px 22px 8px; }
.col { max-width: 720px; margin: 0 auto; display: flex; flex-direction: column; gap: 14px; }

.msg { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; max-width: 80%; padding: 6px 14px; border-radius: 16px; background: var(--bubble); }
.msg.assistant { padding: 2px 0; white-space: normal; line-height: 1.6; }

/* Markdown inside assistant answers */
.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.assistant p { margin: 0 0 .8em; }
.msg.assistant .md-h { margin: 1.2em 0 .5em; line-height: 1.3; font-weight: 600; }
.msg.assistant h3.md-h { font-size: 1.15em; }
.msg.assistant h4.md-h { font-size: 1.05em; }
.msg.assistant h5.md-h, .msg.assistant h6.md-h { font-size: 1em; }
.msg.assistant ul, .msg.assistant ol { margin: 0 0 .8em; padding-left: 1.5em; }
.msg.assistant li { margin: .2em 0; }
.msg.assistant a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; overflow-wrap: anywhere; }
.msg.assistant a:hover { color: var(--accent-hover); }
.msg.assistant code { padding: .1em .35em; border-radius: 5px; background: var(--hover); font-size: .9em; }
.msg.assistant pre { margin: 0 0 .8em; padding: 10px 12px; border-radius: 8px; background: var(--hover); overflow-x: auto; white-space: pre; }
.msg.assistant pre code { padding: 0; background: none; }
.msg.assistant hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.msg.assistant blockquote { margin: 0 0 .8em; padding: 0 0 0 12px; border-left: 3px solid var(--line); color: var(--muted); }
.msg.assistant .table-wrap { margin: 0 0 .9em; overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; }
.msg.assistant table { border-collapse: collapse; width: 100%; font-size: .93em; }
.msg.assistant table { overflow-wrap: break-word; } /* "anywhere" would shrink columns and split words */
.msg.assistant th, .msg.assistant td { padding: 6px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.msg.assistant th { background: var(--hover); font-weight: 600; }
.msg.assistant tr:last-child td { border-bottom: 0; }
.note { align-self: center; max-width: 100%; display: flex; align-items: flex-start; gap: 8px; padding: 6px 12px; border-radius: 10px; font-size: 12px; overflow-wrap: anywhere; }
.note .ico { width: 13px; height: 13px; margin-top: 3px; }
.note.error { background: var(--err-bg); color: var(--err); border: 1px solid var(--err-line); }
.note.notice { background: var(--warn-bg); color: var(--warn); border: 1px solid var(--line); }

details { border: 1px solid var(--line); border-radius: 10px; background: var(--card); }
details > summary { cursor: pointer; padding: 7px 12px; font-size: 12px; color: var(--muted); display: flex; gap: 8px; align-items: center; list-style: none; }
details > summary::-webkit-details-marker { display: none; }
details > summary::before { content: "›"; display: inline-block; width: 8px; transition: transform .12s; }
details[open] > summary::before { transform: rotate(90deg); }
details > div { padding: 0 12px 12px; font-size: 12px; }
details.reasoning { background: transparent; border-style: dashed; }
details.reasoning > div { white-space: pre-wrap; color: var(--muted); }
details.tool > summary span:first-of-type { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--fg); }
.badge { font-size: 10px; border-radius: 99px; padding: 0 8px; border: 1px solid currentColor; }
.badge.pending, .badge.running { color: var(--warn); }
.badge.completed { color: var(--ok); }
.badge.error { color: var(--err); }
.meta { color: var(--muted); font-size: 11px; margin-top: 8px; }
pre { margin: 4px 0 0; padding: 8px 10px; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; overflow-x: auto; font: 11.5px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; white-space: pre-wrap; overflow-wrap: anywhere; }

.articles { list-style: none; margin: 6px 0 0; padding: 0; display: grid; gap: 6px; }
.articles li { border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; background: var(--bg); }
.articles .meta { margin-top: 0; }
a { color: var(--accent); }

.permission { border: 1px solid var(--warn); border-radius: 12px; padding: 10px 14px; background: var(--card); }
.permission.done { border-color: var(--line); color: var(--muted); }
.permission .row { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.permission button { padding: 4px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); }
.permission button:hover:not(:disabled) { border-color: var(--accent); }
.permission button:first-child { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.permission button:disabled { opacity: .5; }

/* ---- composer ------------------------------------------------------------- */
.composer-wrap { padding: 8px 22px 22px; }
.composer {
  max-width: 720px; margin: 0 auto; background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 10px 14px 10px; box-shadow: 0 1px 2px rgba(20, 30, 27, .04);
}
.composer:focus-within { border-color: var(--accent); }
.status { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); padding-bottom: 8px; }
.status .ico { width: 12px; height: 12px; }
.spin { animation: spin 1.1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spin { animation: none; } }
textarea { display: block; width: 100%; resize: none; border: 0; outline: 0; background: transparent; padding: 2px 0 8px; line-height: 1.5; max-height: 160px; }
textarea::placeholder { color: var(--muted); }
.composer-row { display: flex; align-items: center; justify-content: space-between; padding-top: 6px; }
.attach { display: inline-flex; align-items: center; gap: 6px; padding: 2px 0; border: 0; background: transparent; color: var(--muted); font-size: 12px; }
.attach:disabled { opacity: .7; }
.round { display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; border: 0; border-radius: 50%; background: var(--accent); color: var(--on-accent); }
.round:hover:not(:disabled) { background: var(--accent-hover); }
.round:disabled { opacity: .4; }
.round .ico { width: 15px; height: 15px; stroke-width: 2; }
.round.stop svg { width: 16px; height: 16px; fill: currentColor; }

/* ---- attachment chip, evidence cards, article pane ------------------------- */
.chip { display: flex; align-items: center; gap: 6px; width: fit-content; max-width: 100%; margin-bottom: 8px; padding: 2px 4px 2px 9px; border: 1px solid var(--line); border-radius: 999px; background: var(--bg); font-size: 12px; }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip.busy span { color: var(--muted); }
.chip .icon-btn { width: 20px; height: 20px; }
.chip .ico { width: 13px; height: 13px; }
.attach { cursor: pointer; }
.row-left { display: flex; align-items: center; gap: 14px; min-width: 0; flex-wrap: wrap; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); cursor: pointer; }
.toggle:hover { color: var(--fg); }
.toggle input { margin: 0; accent-color: var(--accent); }

.sources { display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px; padding: 8px 10px; border: 1px dashed var(--line); border-radius: 10px; }
.sources .meta { margin-top: 0; }
.attach:hover { color: var(--fg); }

.msg-paper { display: flex; align-items: center; gap: 6px; max-width: 100%; margin-top: 4px; padding: 0; border: 0; background: transparent; color: var(--muted); font-size: 12px; text-align: left; }
.msg-paper:hover { color: var(--accent); }
.msg-paper span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-paper .ico { width: 13px; height: 13px; }

.evidence { display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px; margin: 10px 0 0; }
.ev { display: block; width: 100%; padding: 7px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); text-align: left; line-height: 1.45; }
button.ev.clickable:hover { border-color: var(--accent); }
.ev .claim { font-weight: 500; }
.ev .where { font-size: 11px; color: var(--accent); }
.ev .quote { margin-top: 2px; font-size: 11.5px; color: var(--muted); font-style: italic; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.ev.bad { border-color: var(--warn); background: var(--warn-bg); }
.ev.bad .where { color: var(--warn); }

/* references panel: one card per cited article, with a badge saying whether its PDF is available */
.refs { flex: 1; min-height: 0; overflow-y: auto; padding: 12px; display: grid; align-content: start; gap: 8px; }
.refs-sum { display: flex; flex-wrap: wrap; gap: 6px 12px; padding: 0 2px 4px; font-size: 12px; color: var(--muted); }
.ref { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 2px 10px; padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); text-align: left; width: 100%; line-height: 1.4; }
button.ref:hover { border-color: var(--accent); }
.ref-n { grid-row: 1 / span 3; width: 20px; height: 20px; border-radius: 50%; background: var(--line); color: var(--muted); font-size: 11px; display: grid; place-items: center; }
.ref-title { font-weight: 500; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.ref-meta { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.ref-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; margin-top: 3px; font-size: 12px; }
.pdf-badge { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 500; border: 1px solid var(--line); color: var(--muted); }
.pdf-badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pdf-badge.yes { color: var(--ok); border-color: currentColor; }
.pdf-badge.no { color: var(--muted); }
.pdf-badge.maybe { color: var(--warn); border-color: currentColor; background: var(--warn-bg); }
.ref-foot a { color: var(--accent); }
.ref-hint { color: var(--muted); }
.open-refs { display: inline-flex; align-items: center; gap: 6px; margin-top: 2px; padding: 4px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); font-size: 12.5px; }
.open-refs:hover { border-color: var(--accent); }

.shell.has-paper { grid-template-columns: 220px minmax(0, 1fr) minmax(340px, 46%); }
.shell.has-paper.collapsed { grid-template-columns: minmax(0, 1fr) minmax(340px, 46%); }
.paper { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--side); border-left: 1px solid var(--line); }
.paper-top { display: flex; align-items: center; gap: 8px; padding: 10px 10px 10px 14px; border-bottom: 1px solid var(--line); }
.paper-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.paper-body { flex: 1; min-height: 0; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.pg { position: relative; flex: none; width: 100%; max-width: 900px; background: #fff; box-shadow: var(--shadow); }
.pg canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.hls { position: absolute; inset: 0; pointer-events: none; }
.pgno { position: absolute; right: 6px; bottom: 3px; font-size: 10px; color: #6b7280; }
.hl { position: absolute; background: rgba(250, 204, 21, .38); mix-blend-mode: multiply; border-radius: 2px; }
.hl.loose { background: rgba(251, 146, 60, .32); }
.hl.active { background: rgba(250, 204, 21, .7); outline: 2px solid #ca8a04; animation: flash 1s ease-out 2; }
@keyframes flash { 50% { background: rgba(250, 204, 21, 1); } }
@media (prefers-reduced-motion: reduce) { .hl.active { animation: none; } }

/* ---- small screens -------------------------------------------------------- */
@media (max-width: 720px) {
  body { padding: 0; }
  .shell { height: 100dvh; border-radius: 0; border: 0; position: relative; }
  .shell, .shell.collapsed { grid-template-columns: minmax(0, 1fr); }
  .shell.has-paper, .shell.has-paper.collapsed { grid-template-columns: minmax(0, 1fr); }
  .paper { position: absolute; inset: 0; z-index: 6; border-left: 0; }
  .shell.has-paper:not(.paper-open) .paper { display: none; }
  .sidebar { position: absolute; inset: 0 auto 0 0; width: min(280px, 85vw); z-index: 5; box-shadow: var(--shadow); }
  .topbar, main, .composer-wrap { padding-left: 14px; padding-right: 14px; }
  .icon-btn.open-side { display: inline-flex; margin-left: 0; }
}
