:root {
  --mineral: #E7ECEA;
  --surface: #FFFFFF;
  --raised: #F3F6F5;
  --ink: #14201E;
  --muted: #5A6A67;
  --line: #D5DDDA;
  --teal: #0E6B66;
  --teal-ink: #FFFFFF;
  --teal-soft: #DCEBE8;
  --amber: #E2B23C;
  --amber-ink: #2B1F00;
  --amber-soft: #FBF1D6;
  --danger: #A8321F;
  --ok: #1F7A48;
  --sans: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", "Noto Sans Bengali", Roboto, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sidebar-w: 272px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --mineral: #101816;
    --surface: #151F1D;
    --raised: #1C2826;
    --ink: #DDE7E4;
    --muted: #8FA29E;
    --line: #283633;
    --teal: #5BBDB4;
    --teal-ink: #06221F;
    --teal-soft: #1C3532;
    --amber: #E2B23C;
    --amber-ink: #241900;
    --amber-soft: #3A3018;
    --danger: #F08C78;
    --ok: #6CC793;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.55;
  color: var(--ink);
  background: var(--mineral);
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
a { color: var(--teal); }
svg { stroke: currentColor; fill: none; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
:focus-visible { outline: 2px solid var(--teal); 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; }
[hidden] { display: none !important; }

.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; height: 100dvh; }
.scrim { display: none; }

/* ---------- sidebar ---------- */
.sidebar {
  display: flex; flex-direction: column; gap: 4px;
  padding: 18px 12px 12px;
  border-right: 1px solid var(--line);
  min-height: 0;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px 14px; font-weight: 600; font-size: 15px; }
.brand-mark { width: 24px; height: 24px; flex: none; stroke: none; }
.brand-mark rect { fill: var(--teal); }
.brand-mark .tab { fill: var(--amber); }

.btn-new {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 12px; margin-bottom: 6px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  cursor: pointer; font-weight: 500; text-align: left;
}
.btn-new svg { width: 16px; height: 16px; }
.btn-new:hover { border-color: var(--teal); }

.nav { display: flex; flex-direction: column; gap: 2px; padding-bottom: 12px; border-bottom: 1px solid var(--line); }
.nav a { padding: 7px 12px; border-radius: 8px; text-decoration: none; color: var(--ink); }
.nav a:hover { background: var(--raised); }
.nav a.active { background: var(--teal-soft); color: var(--ink); font-weight: 500; }

.conv-label { margin: 14px 12px 4px; font-size: 12.5px; color: var(--muted); }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 0; }
.conv { position: relative; border-radius: 8px; }
.conv a {
  display: block; padding: 7px 34px 7px 12px; border-radius: 8px;
  color: var(--ink); text-decoration: none; font-size: 14px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv:hover a { background: var(--raised); }
.conv.active a { background: var(--surface); box-shadow: inset 2px 0 0 var(--teal); }
.conv-del {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 26px; height: 26px; border: 0; background: transparent; border-radius: 6px;
  color: var(--muted); cursor: pointer; opacity: 0;
}
.conv:hover .conv-del, .conv-del:focus-visible, .conv.active .conv-del { opacity: 1; }
.conv-del:hover { color: var(--danger); background: var(--mineral); }
.conv-del svg { width: 14px; height: 14px; }
.conv-empty { padding: 6px 12px; font-size: 13.5px; color: var(--muted); }

.kb-status { padding: 10px 12px 2px; border-top: 1px solid var(--line); font-size: 12.5px; color: var(--muted); }
.kb-status .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; background: var(--ok); vertical-align: 1px; }
.kb-status .dot.warn { background: var(--amber); }
.kb-status .dot.bad { background: var(--danger); }

/* ---------- main ---------- */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--surface); }
.topbar { display: none; }
.view { flex: 1; min-height: 0; }
.chat-view { display: flex; flex-direction: column; }
.thread-scroll { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
.thread { max-width: 46rem; margin: 0 auto; padding: 32px 24px 24px; }

.empty { padding-top: 14vh; }
.empty h1 { font-size: 30px; line-height: 1.2; font-weight: 600; letter-spacing: -0.01em; margin: 0 0 10px; }
.empty p { margin: 0; color: var(--muted); max-width: 36rem; }
.empty .repo { color: var(--ink); font-weight: 500; }
.empty a { font-weight: 500; }

.msg { margin: 0 0 28px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  max-width: 85%; padding: 10px 14px;
  background: var(--teal-soft); border-radius: 14px 14px 4px 14px;
  white-space: pre-wrap; overflow-wrap: anywhere;
}

/* rendered answer */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md table, .md blockquote { margin: 0 0 12px; }
.md h3, .md h4, .md h5, .md h6 { margin: 20px 0 8px; line-height: 1.3; }
.md h3 { font-size: 18px; } .md h4 { font-size: 16px; } .md h5, .md h6 { font-size: 15px; }
.md ul, .md ol { padding-left: 22px; }
.md li { margin: 3px 0; }
.md code { font-family: var(--mono); font-size: 0.88em; background: var(--raised); padding: 1px 5px; border-radius: 5px; }
.md pre { position: relative; background: var(--raised); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; overflow-x: auto; }
.md pre code { background: none; padding: 0; font-size: 13px; line-height: 1.6; }
.md blockquote { border-left: 3px solid var(--line); padding-left: 12px; color: var(--muted); }
.md .table-wrap { overflow-x: auto; margin: 0 0 12px; }
.md table { border-collapse: collapse; font-size: 14px; margin: 0; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--raised); font-weight: 600; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }

/* the signature element: citation tabs */
.cite {
  display: inline-block; min-width: 1.45em; padding: 0 4px; margin: 0 1px;
  font: 500 11px/1.55 var(--mono); text-align: center; vertical-align: 0.35em;
  background: var(--amber); color: var(--amber-ink);
  border-radius: 4px 4px 4px 1px; cursor: default;
  transition: transform .12s ease;
}
.cite.hl { transform: translateY(-1px); box-shadow: 0 0 0 2px var(--amber-soft); }

.sources { margin-top: 14px; }
.sources ol { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.sources li {
  display: flex; align-items: baseline; gap: 10px; min-width: 0;
  padding: 5px 8px; margin-left: -8px; border-radius: 7px; font-size: 13.5px;
  transition: background .12s ease;
}
.sources li.hl { background: var(--amber-soft); }
.sources .tag { flex: none; min-width: 1.45em; text-align: center; font: 500 11px/1.55 var(--mono); background: var(--amber); color: var(--amber-ink); border-radius: 4px 4px 4px 1px; padding: 0 4px; }
.sources a { color: var(--ink); text-decoration: none; font-family: var(--mono); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sources a:hover { text-decoration: underline; color: var(--teal); }
.sources .sec { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }

.actions { display: flex; gap: 2px; margin-top: 8px; margin-left: -6px; }
.act {
  display: inline-flex; align-items: center; gap: 6px;
  height: 30px; padding: 0 8px; border: 0; border-radius: 7px;
  background: transparent; color: var(--muted); cursor: pointer; font-size: 13px;
}
.act svg { width: 16px; height: 16px; }
.act:hover { background: var(--raised); color: var(--ink); }
.act[aria-pressed="true"] { color: var(--teal); }
.act[aria-pressed="true"] svg { fill: var(--teal-soft); }

.msg-error { margin-top: 10px; padding: 10px 12px; border-left: 3px solid var(--danger); background: var(--raised); border-radius: 0 8px 8px 0; font-size: 14px; }

.typing { display: inline-flex; gap: 5px; padding: 6px 0; }
.typing i { width: 7px; height: 7px; border-radius: 2px; background: var(--amber); animation: blink 1.1s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .15s; }
.typing i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* composer */
.composer-wrap { padding: 8px 24px 14px; }
.composer {
  max-width: 46rem; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 8px 8px 16px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px;
  box-shadow: 0 1px 0 var(--line);
}
.composer:focus-within { border-color: var(--teal); }
.composer textarea {
  flex: 1; resize: none; border: 0; outline: none; background: transparent;
  padding: 7px 0; max-height: 220px; line-height: 1.5;
}
.send {
  flex: none; width: 36px; height: 36px; border: 0; border-radius: 10px;
  background: var(--teal); color: var(--teal-ink); cursor: pointer;
  display: grid; place-items: center;
}
.send svg { width: 18px; height: 18px; stroke-width: 2; }
.send:disabled { opacity: .4; cursor: not-allowed; }
.send .i-stop { display: none; fill: currentColor; stroke: none; }
.send.stop .i-send { display: none; }
.send.stop .i-stop { display: block; }
.hint { max-width: 46rem; margin: 8px auto 0; text-align: center; font-size: 12px; color: var(--muted); }

/* ---------- dashboard ---------- */
.dash-view { overflow-y: auto; }
.dash { max-width: 68rem; padding: 32px 32px 64px; }
.dash h1 { font-size: 26px; line-height: 1.2; margin: 0 0 4px; font-weight: 600; letter-spacing: -0.01em; }
.dash h2 { font-size: 17px; margin: 0 0 12px; font-weight: 600; }
.dash .sub { color: var(--muted); margin: 0 0 24px; }
.dash .sub a { font-family: var(--mono); font-size: 13.5px; }
.dash section { padding: 26px 0; border-top: 1px solid var(--line); }
.muted { color: var(--muted); }
.error { color: var(--danger); }

.stats { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); border: 1px solid var(--line); border-radius: 12px; background: var(--raised); margin-bottom: 8px; }
.stat { padding: 14px 16px; border-left: 1px solid var(--line); }
.stat:first-child { border-left: 0; }
.stat b { display: block; font-size: 24px; font-weight: 600; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat span { font-size: 13px; color: var(--muted); }

.sync-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 14px; }
.sync-head p { margin: 0; }
.sync-form { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.sync-form input[type="password"] { width: 200px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.sync-form label.check { display: inline-flex; gap: 6px; align-items: center; font-size: 14px; color: var(--muted); }
.btn { padding: 8px 14px; border-radius: 8px; border: 1px solid var(--teal); background: var(--teal); color: var(--teal-ink); font-weight: 500; cursor: pointer; }
.btn:disabled { opacity: .5; cursor: progress; }
.form-msg { font-size: 13.5px; min-height: 1.2em; margin: 0 0 8px; }

.table-scroll { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.data th { text-align: left; font-weight: 500; color: var(--muted); padding: 6px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.data td { padding: 7px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.data td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
table.data .mono { font-family: var(--mono); font-size: 12.5px; }
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 500; }
.pill.success { background: var(--teal-soft); color: var(--teal); }
.pill.failed { background: var(--amber-soft); color: var(--danger); }
.pill.running { background: var(--amber-soft); color: var(--amber-ink); }
@media (prefers-color-scheme: dark) { .pill.running { color: var(--amber); } }
.err-text { color: var(--danger); font-size: 12.5px; max-width: 28rem; }

.bars { display: grid; grid-template-columns: repeat(14, 1fr); gap: 6px; align-items: end; height: 140px; }
.bar { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; }
.bar i { display: block; width: 100%; max-width: 34px; background: var(--teal); border-radius: 4px 4px 1px 1px; min-height: 2px; }
.bar.zero i { background: var(--line); }
.bar span { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.bar b { font-size: 11px; font-weight: 500; font-variant-numeric: tabular-nums; }

.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 40px; }
.qlist { list-style: none; margin: 0; padding: 0; }
.qlist li { padding: 8px 0; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; gap: 16px; font-size: 14px; }
.qlist li a { color: var(--ink); text-decoration: none; }
.qlist li a:hover { color: var(--teal); text-decoration: underline; }
.qlist .when, .qlist .n { color: var(--muted); font-size: 12.5px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.qlist .path { font-family: var(--mono); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lead { margin: -6px 0 12px; color: var(--muted); font-size: 14px; }

.doc-search { width: 100%; max-width: 360px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); margin-bottom: 12px; }

/* ---------- responsive ---------- */
@media (max-width: 1000px) {
  .stats { grid-template-columns: repeat(3, 1fr); }
  .stat:nth-child(4) { border-left: 0; }
  .stat:nth-child(n+4) { border-top: 1px solid var(--line); }
  .cols { grid-template-columns: 1fr; gap: 28px; }
}
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(84vw, 300px); z-index: 20;
    background: var(--mineral); transform: translateX(-100%); transition: transform .2s ease;
    padding-top: calc(18px + env(safe-area-inset-top));
  }
  .sidebar.open { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 15; opacity: 0; pointer-events: none; transition: opacity .2s; }
  .scrim.show { opacity: 1; pointer-events: auto; }
  .topbar { display: flex; align-items: center; gap: 8px; padding: calc(8px + env(safe-area-inset-top)) 12px 8px; border-bottom: 1px solid var(--line); font-weight: 500; }
  .icon-btn { width: 36px; height: 36px; border: 0; background: transparent; border-radius: 8px; display: grid; place-items: center; cursor: pointer; }
  .icon-btn svg { width: 20px; height: 20px; }
  .thread { padding: 20px 16px; }
  .composer-wrap { padding: 6px 12px calc(10px + env(safe-area-inset-bottom)); }
  .dash { padding: 20px 16px 48px; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .stat:nth-child(odd) { border-left: 0; }
  .stat:nth-child(4) { border-left: 1px solid var(--line); }
  .stat:nth-child(n+3) { border-top: 1px solid var(--line); }
  .bars { gap: 3px; }
  .bar span { font-size: 9.5px; }
  .empty h1 { font-size: 24px; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
