:root {
  --bg: #f7f7f8;
  --panel: #ffffff;
  --ink: #1a1a1e;
  --muted: #6b6b76;
  --line: #e4e4e9;
  --accent: #4f46e5;
  --accent-soft: #eef0fe;
  --chip: #f0f0f3;
  --source: #0e7490;
  --query: #4f46e5;
  --reason: #a16207;
  --answer: #15803d;
  --error: #dc2626;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131316; --panel: #1c1c21; --ink: #e9e9ee; --muted: #9a9aa6;
    --line: #2c2c33; --accent: #8b83ff; --accent-soft: #23223a; --chip: #26262d;
    --source: #38bdf8; --query: #a5a0ff; --reason: #fbbf24; --answer: #4ade80;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--sans); background: var(--bg); color: var(--ink); font-size: 14px; }
button { font: inherit; cursor: pointer; }
code { font-family: var(--mono); font-size: .92em; }
.muted { color: var(--muted); }
.error { color: var(--error); }
.spacer { flex: 1; }

/* login */
.login-body { display: grid; place-items: center; min-height: 100vh; }
.login-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 28px; width: min(380px, 92vw); display: flex; flex-direction: column; gap: 12px;
  box-shadow: 0 8px 30px rgba(0,0,0,.06);
}
.login-card h1 { margin: 0; font-style: italic; font-size: 24px; }
.login-card label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--muted); }
.login-card input {
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--ink); font: inherit;
}
.login-card details summary { cursor: pointer; font-size: 12px; }
.login-card button, .topbar button {
  background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: 10px 14px; font-weight: 600;
}
.login-card button:disabled { opacity: .6; cursor: default; }

/* topbar */
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 10px 16px;
  border-bottom: 1px solid var(--line); background: var(--panel); position: sticky; top: 0; z-index: 5;
}
.brand { font-style: italic; font-weight: 700; }
.controls { display: flex; align-items: center; gap: 12px; flex: 1; }
.inline { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.inline select, .inline input {
  font: inherit; padding: 5px 8px; border: 1px solid var(--line); border-radius: 7px;
  background: var(--bg); color: var(--ink);
}
button.ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); border-radius: 7px; padding: 6px 10px; }

/* layout */
.layout { display: grid; grid-template-columns: 320px 1fr; height: calc(100vh - 53px); }
.sidebar { border-right: 1px solid var(--line); background: var(--panel); overflow: auto; padding: 12px; }
.content { overflow: auto; padding: 16px 20px; }

.breadcrumbs { font-family: var(--mono); font-size: 12px; margin-bottom: 8px; word-break: break-all; }
.breadcrumbs a { color: var(--accent); text-decoration: none; cursor: pointer; }
.breadcrumbs a:hover { text-decoration: underline; }

.tree { list-style: none; margin: 0; padding: 0; }
.tree li { padding: 6px 8px; border-radius: 7px; cursor: pointer; display: flex; align-items: center; gap: 8px; }
.tree li:hover { background: var(--accent-soft); }
.tree li.file.selected { background: var(--accent-soft); outline: 1px solid var(--accent); }
.tree .ic { width: 16px; text-align: center; opacity: .8; }
.tree .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree .sz { font-size: 11px; color: var(--muted); }
.tree .gen-open {
  margin-left: 6px; font-size: 11px; border: 1px solid var(--accent); color: var(--accent);
  background: transparent; border-radius: 6px; padding: 1px 7px;
}

/* file bar / pager */
.filebar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.filepath { font-family: var(--mono); font-weight: 600; }
.fileinfo { display: flex; flex-direction: column; gap: 2px; }
.viewtoggle button {
  background: transparent; border: 1px solid var(--line); color: var(--muted);
  padding: 5px 12px; border-radius: 7px;
}
.viewtoggle button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.pager { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.pager button { background: var(--panel); border: 1px solid var(--line); color: var(--ink); border-radius: 7px; padding: 6px 12px; }
.pager button:disabled { opacity: .4; cursor: default; }

.empty { padding: 40px 0; text-align: center; }

/* record cards */
.record {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 16px; margin-bottom: 14px;
}
.doc-title { font-weight: 700; font-size: 15px; margin-bottom: 8px; line-height: 1.3; }
.viewtoggle button[hidden] { display: none; }
.record-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  font-size: 11px; background: var(--chip); border-radius: 999px; padding: 2px 9px; color: var(--ink);
  white-space: nowrap;
}
.chip b { color: var(--muted); font-weight: 600; }
.seg { padding: 8px 0; border-top: 1px dashed var(--line); }
.seg:first-of-type { border-top: 0; }
.seg .label { font-size: 11px; letter-spacing: .06em; text-transform: uppercase; font-weight: 700; margin-bottom: 3px; }
.seg .body { white-space: pre-wrap; line-height: 1.5; }
.seg.source .label { color: var(--source); }
.seg.query  .label { color: var(--query); }
.seg.query  .body { font-size: 16px; font-weight: 600; }
.seg.reason .label { color: var(--reason); }
.seg.reason .body { color: var(--muted); font-size: 13px; }
.seg.answer .label { color: var(--answer); }
.seg.match  .label { color: var(--source); }
.seg.match  .body { color: var(--muted); font-size: 13px; }
.tokrow { margin-top: 8px; font-size: 11px; color: var(--muted); font-family: var(--mono); }

/* generic table */
.tablewrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
table.grid { border-collapse: collapse; width: 100%; font-size: 12px; }
table.grid th, table.grid td { border-bottom: 1px solid var(--line); padding: 7px 10px; text-align: left; vertical-align: top; }
table.grid th { position: sticky; top: 0; background: var(--panel); font-family: var(--mono); font-weight: 700; white-space: nowrap; }
table.grid td { max-width: 380px; }
table.grid td .cell { max-height: 5.5em; overflow: hidden; white-space: pre-wrap; cursor: pointer; }
table.grid td .cell.expanded { max-height: none; }
