:root {
  --bg: #F4F5F7; --panel: #FFFFFF; --ink: #0F1115; --ink2: #4A5060; --ink3: #7B8190; --line: #E1E4EA;
  --accent: #F55036; --accent-ink: #C5321A; --accent-soft: #FDEBE7; --ok: #12805C; --ok-soft: #E4F4EC; --bad: #B3302D; --bad-soft: #FBEAE9;
  --mark: #FFE9A8;
  --ui: "Instrument Sans", "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, "Cascadia Code", Consolas, monospace;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.55 var(--ui); -webkit-font-smoothing: antialiased; display: flex; flex-direction: column; }
button, select, textarea, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; border-radius: 6px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.mono { font-family: var(--mono); }

/* ---------- top bar ---------- */
.top { display: flex; align-items: center; gap: 18px; padding: 10px 20px; background: var(--panel); border-bottom: 1px solid var(--line); flex: none; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand b { display: block; font-size: 17px; letter-spacing: -0.02em; line-height: 1.1; }
.brand span { display: block; font-size: 12px; color: var(--ink3); white-space: nowrap; }
.modes { display: flex; gap: 4px; background: var(--bg); padding: 4px; border-radius: 12px; margin: 0 auto; }
.modes button { border: 0; background: none; padding: 7px 16px; border-radius: 9px; font-weight: 600; color: var(--ink2); white-space: nowrap; }
.modes button[aria-selected="true"] { background: var(--ink); color: #fff; }
.modes button:not([aria-selected="true"]):hover { background: #E9EBF0; }
.tools { display: flex; align-items: center; gap: 8px; }
select { border: 1px solid var(--line); background: #fff; border-radius: 10px; padding: 7px 10px; max-width: 210px; font-family: var(--mono); font-size: 12.5px; }
.ghost { border: 1px solid transparent; background: none; padding: 7px 10px; border-radius: 10px; color: var(--ink2); font-weight: 500; }
.ghost:hover { background: var(--bg); color: var(--ink); }
.pill { display: inline-flex; align-items: center; gap: 7px; font-size: 12.5px; font-weight: 600; padding: 6px 11px; border-radius: 20px; background: var(--bad-soft); color: var(--bad); white-space: nowrap; }
.pill i { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.pill.ok { background: var(--ok-soft); color: var(--ok); }

/* ---------- layout ---------- */
.body { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr); }
body[data-mode="rag"] .body, body[data-mode="compare"] .body { grid-template-columns: minmax(0, 1fr) 390px; }
body[data-mode="chat"] .side { display: none; }
.stage { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.feed { flex: 1; overflow-y: auto; padding: 26px 28px 8px; scroll-behavior: smooth; }
.wrap { max-width: 800px; margin: 0 auto; }
body[data-mode="compare"] .wrap { max-width: 1120px; }

/* ---------- intro ---------- */
.intro h1 { font-size: clamp(28px, 3.6vw, 42px); line-height: 1.05; letter-spacing: -0.035em; margin: 18px 0 10px; max-width: 18ch; }
.intro p { color: var(--ink2); font-size: 17px; max-width: 58ch; margin: 0 0 18px; }
.intro .eq { display: inline-flex; gap: 8px; align-items: center; font-family: var(--mono); font-size: 12.5px; color: var(--ink2); background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; margin-bottom: 10px; }
.intro .eq b { color: var(--accent-ink); }
.ideas { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; margin-top: 10px; }
.idea { text-align: left; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; color: var(--ink2); transition: border-color .15s, transform .15s; }
.idea:hover { border-color: var(--ink); color: var(--ink); transform: translateY(-1px); }

/* ---------- messages ---------- */
.row { margin-bottom: 22px; }
.you { display: flex; justify-content: flex-end; }
.you div { background: var(--ink); color: #fff; padding: 10px 15px; border-radius: 16px 16px 4px 16px; max-width: 85%; white-space: pre-wrap; word-break: break-word; }
.q-banner { background: var(--ink); color: #fff; padding: 12px 16px; border-radius: 12px; margin-bottom: 12px; font-weight: 500; }
.bot { max-width: 100%; }
.bot .who { display: flex; align-items: center; gap: 8px; font-size: 12.5px; font-weight: 600; color: var(--ink3); margin-bottom: 5px; font-family: var(--mono); text-transform: uppercase; letter-spacing: .04em; }
.bot .who i { width: 8px; height: 8px; border-radius: 2px; background: var(--ink3); }
.bot.rag .who i { background: var(--accent); }
.content { font-size: 16px; line-height: 1.65; }
.content p { margin: 0 0 .8em; }
.content ul, .content ol { margin: 0 0 .9em; padding-left: 1.3em; }
.content li { margin-bottom: .25em; }
.content h3, .content h4 { font-size: 15px; margin: 1.1em 0 .3em; }
.content code { font-family: var(--mono); font-size: .86em; background: var(--bg); padding: 1px 5px; border-radius: 5px; }
.content pre { background: var(--ink); color: #E9ECF2; border-radius: 10px; padding: 12px 14px; overflow-x: auto; margin: 0 0 .9em; }
.content pre code { background: none; padding: 0; color: inherit; font-size: 13px; }
.content table { border-collapse: collapse; margin: 0 0 .9em; font-size: 14px; display: block; overflow-x: auto; }
.content th, .content td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.content th { background: var(--bg); }
.cursor::after { content: ""; display: inline-block; width: 7px; height: 1em; background: var(--accent); margin-left: 2px; vertical-align: -2px; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.wait { color: var(--ink3); font-family: var(--mono); font-size: 13px; }
.cite { font: 600 11px/1 var(--mono); min-width: 20px; height: 20px; padding: 0 5px; margin: 0 1px; border: 0; border-radius: 6px; background: var(--accent-soft); color: var(--accent-ink); vertical-align: 1px; }
.cite:hover { background: var(--accent); color: #fff; }
.err { background: var(--bad-soft); color: var(--bad); border-radius: 10px; padding: 10px 14px; font-size: 14.5px; }
.stats { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.stat { font: 500 12px/1 var(--mono); padding: 5px 8px; border-radius: 6px; background: var(--panel); border: 1px solid var(--line); color: var(--ink2); }
.stat b { color: var(--ink); font-weight: 600; }
.stat.hot { border-color: var(--accent); }
.linkish { background: none; border: 0; padding: 0; color: var(--accent-ink); font-weight: 600; text-decoration: underline; text-underline-offset: 3px; font-size: 13px; }

/* compare */
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px 14px; min-width: 0; }
.card.rag { border-color: #F7B7AA; box-shadow: 0 0 0 3px var(--accent-soft); }
.card .tag { font: 600 11px/1 var(--mono); text-transform: uppercase; letter-spacing: .06em; margin-bottom: 8px; color: var(--ink3); }
.card.rag .tag { color: var(--accent-ink); }
.card .note { font-size: 12.5px; color: var(--ink3); margin-bottom: 8px; }

/* ---------- dock ---------- */
.dock { padding: 6px 28px 16px; flex: none; }
.dock > * { max-width: 800px; margin-left: auto; margin-right: auto; }
body[data-mode="compare"] .dock > * { max-width: 1120px; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 10px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 5px 12px; font-size: 13px; color: var(--ink2); white-space: nowrap; max-width: 340px; overflow: hidden; text-overflow: ellipsis; }
.chip:hover { border-color: var(--ink); color: var(--ink); }
.composer { display: flex; align-items: flex-end; gap: 8px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 7px 7px 7px 15px; transition: border-color .15s, box-shadow .15s; }
.composer:focus-within { border-color: var(--ink); box-shadow: 0 0 0 4px rgba(15,17,21,.07); }
.composer textarea { flex: 1; border: 0; outline: 0; resize: none; background: none; padding: 8px 0; max-height: 150px; font-size: 16px; line-height: 1.45; }
.send, .stop { height: 38px; border-radius: 11px; border: 0; font-weight: 600; flex: none; }
.send { width: 38px; background: var(--accent); color: #fff; display: grid; place-items: center; }
.send:hover:not(:disabled) { background: var(--ink); }
.send:disabled { background: #C9CDD6; cursor: not-allowed; }
.stop { background: var(--ink); color: #fff; padding: 0 16px; }
.hint { margin: 6px 0 0; min-height: 18px; font-size: 13px; color: var(--ink3); }
.hint.warn { color: var(--bad); }

/* ---------- side panel ---------- */
.side { background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; border-bottom: 1px solid var(--line); gap: 8px; }
.tabs { display: flex; gap: 4px; }
.tabs button { border: 0; background: none; padding: 7px 12px; border-radius: 9px; font-weight: 600; color: var(--ink2); }
.tabs button[aria-selected="true"] { background: var(--bg); color: var(--ink); }
.side-body { flex: 1; overflow-y: auto; padding: 14px; }
.empty { color: var(--ink3); font-size: 14px; }
.qline { font: 12.5px/1.5 var(--mono); color: var(--ink2); background: var(--bg); border-radius: 8px; padding: 8px 10px; margin-bottom: 12px; word-break: break-word; }
.qline b { color: var(--ink); }
.chunk { border: 1px solid var(--line); border-radius: 12px; padding: 11px 12px; margin-bottom: 10px; opacity: 0; transform: translateY(6px); animation: rise .45s ease forwards; }
.chunk.flash { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
@keyframes rise { to { opacity: 1; transform: none; } }
.chunk-top { display: flex; align-items: center; gap: 8px; font-size: 12.5px; font-weight: 600; }
.chunk-top .n { font: 600 11px/1 var(--mono); background: var(--ink); color: #fff; border-radius: 5px; padding: 4px 6px; flex: none; }
.chunk-top .sec { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.used { font: 600 10.5px/1 var(--mono); color: var(--ok); background: var(--ok-soft); padding: 4px 6px; border-radius: 5px; flex: none; }
.bar { height: 5px; border-radius: 3px; background: var(--bg); margin: 8px 0 3px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 3px; animation: fill .8s .15s ease forwards; }
@keyframes fill { to { width: var(--w); } }
.score { font: 11.5px var(--mono); color: var(--ink3); margin-bottom: 6px; }
.chunk-text { font-size: 13.5px; line-height: 1.55; color: #2A2E38; white-space: pre-wrap; word-break: break-word; }
.chunk-text mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 1px; }
.doc { display: flex; align-items: center; gap: 10px; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; margin-bottom: 8px; }
.doc div { flex: 1; min-width: 0; }
.doc b { display: block; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc small { color: var(--ink3); font-family: var(--mono); font-size: 11.5px; }
.x { border: 0; background: none; width: 28px; height: 28px; border-radius: 8px; color: var(--ink3); font-size: 18px; line-height: 1; }
.x:hover { background: var(--bad-soft); color: var(--bad); }
.sec-title { font: 600 11px/1 var(--mono); text-transform: uppercase; letter-spacing: .06em; color: var(--ink3); margin: 18px 0 8px; }
.field { width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: #fff; margin-bottom: 8px; }
textarea.field { resize: vertical; min-height: 90px; }
.btn { border: 1px solid var(--ink); background: var(--ink); color: #fff; border-radius: 10px; padding: 8px 14px; font-weight: 600; }
.btn.alt { background: #fff; color: var(--ink); }
.btn:hover { background: var(--accent); border-color: var(--accent); color: #fff; }
.btnrow { display: flex; gap: 8px; flex-wrap: wrap; }
.up { margin-top: 8px; font-size: 13px; }
.up .ok { color: var(--ok); } .up .bad { color: var(--bad); }

/* ---------- dialog ---------- */
dialog { border: 0; border-radius: 16px; padding: 22px; width: min(440px, 92vw); box-shadow: 0 20px 60px rgba(15,17,21,.3); }
dialog::backdrop { background: rgba(15,17,21,.4); }
dialog h2 { margin: 0 0 12px; font-size: 20px; }
dialog label { display: block; margin-bottom: 14px; font-weight: 600; font-size: 14px; }
dialog output { font-family: var(--mono); color: var(--accent-ink); margin-left: 4px; }
dialog small { display: block; font-weight: 400; color: var(--ink3); }
dialog input[type=range] { width: 100%; accent-color: var(--accent); }
dialog textarea { width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; margin-top: 4px; }
dialog .check { display: block; }
.primary { background: var(--ink); color: #fff; border: 0; border-radius: 11px; padding: 10px 20px; font-weight: 600; }
.primary:hover { background: var(--accent); }

.only-mobile, .fab { display: none; }

@media (max-width: 1100px) {
  .brand span { display: none; }
  body[data-mode="rag"] .body, body[data-mode="compare"] .body { grid-template-columns: minmax(0, 1fr) 340px; }
  .cols { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .top { flex-wrap: wrap; gap: 8px 12px; padding: 8px 12px; }
  .modes { order: 3; width: 100%; margin: 0; }
  .modes button { flex: 1; padding: 7px 8px; }
  .tools { margin-left: auto; }
  .tools .ghost { padding: 7px 8px; }
  select { max-width: 150px; }
  .feed { padding: 18px 14px 6px; }
  .dock { padding: 6px 14px 12px; }
  body[data-mode="rag"] .body, body[data-mode="compare"] .body { grid-template-columns: minmax(0, 1fr); }
  .side { position: fixed; inset: 0; z-index: 30; transform: translateY(102%); transition: transform .25s ease; border-left: 0; }
  .side.open { transform: none; }
  .only-mobile { display: inline-block; }
  .fab { display: block; position: fixed; right: 16px; bottom: 120px; z-index: 20; background: var(--accent); color: #fff; border: 0; border-radius: 22px; padding: 10px 16px; font-weight: 600; box-shadow: 0 6px 20px rgba(15,17,21,.25); }
  .pill span { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
  .chunk { opacity: 1; transform: none; }
  .bar i { width: var(--w); }
}
