:root {
  --bg: #f4f6f8; --surface: #ffffff; --surface-2: #eef2f5; --border: #dbe1e7; --text: #17202a; --muted: #5b6876;
  --accent: #0f766e; --accent-strong: #0b5f58; --accent-soft: #e2f3f1; --warn: #8a5a00; --warn-soft: #fff4d6;
  --bad: #b42318; --bad-soft: #fdecea; --ok: #1a7f37; --ok-soft: #e6f4ea; --user-bubble: #0f766e; --user-text: #fff;
  --radius: 12px; --shadow: 0 1px 2px rgba(16,24,40,.05), 0 2px 8px rgba(16,24,40,.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1215; --surface: #161b20; --surface-2: #1d242b; --border: #2b343d; --text: #e5eaef; --muted: #98a4b0;
    --accent: #3fb8aa; --accent-strong: #5fd0c2; --accent-soft: #15302d; --warn: #e3b341; --warn-soft: #2d2512;
    --bad: #ff7b72; --bad-soft: #3a1714; --ok: #56d364; --ok-soft: #12291a; --user-bubble: #1f6f67; --user-text: #fff;
    --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.55 Vazirmatn, system-ui, -apple-system, "Segoe UI", Roboto, Tahoma, sans-serif; }
/* Persian: self-hosted Vazirmatn (SIL OFL, see Vazirmatn-OFL.txt), used only for Arabic-script characters so
   Latin terms and digits keep the system font. Blocks get dir="rtl" lang="fa" from the renderer (common.js). */
@font-face { font-family: "Vazirmatn"; src: url("/static/Vazirmatn-wght.woff2") format("woff2"); font-weight: 100 900;
  font-display: swap; unicode-range: U+0600-06FF, U+0750-077F, U+08A0-08FF, U+200C-200F, U+FB50-FDFF, U+FE70-FEFF; }
:lang(fa), [dir="rtl"], .rtl { line-height: 1.9; }
.msg .body [dir="auto"], .msg.user { unicode-bidi: plaintext; text-align: start; }
.msg .body [dir="rtl"], .msg .body [dir="ltr"] { text-align: start; }
bdi { unicode-bidi: isolate; }
.understood { font-size: 12.5px; color: var(--muted); background: var(--surface-2); border-radius: 8px; padding: 6px 10px; margin-bottom: 8px; }
.understood b { color: var(--text); font-weight: 600; }
.understood .terms { margin-top: 2px; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); border-radius: 8px; padding: 7px 12px; cursor: pointer; font-weight: 600; font-size: 14px; text-decoration: none; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-strong); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 3px 8px; font-size: 12.5px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
input[type=text], input[type=password], input[type=number], select, textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; width: 100%; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label.field { display: grid; gap: 4px; font-size: 13px; color: var(--muted); font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.badge { display: inline-block; font-size: 11.5px; font-weight: 700; padding: 1px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); text-transform: capitalize; }
.badge.admin { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge.editor { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.ready { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.failed { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.badge.queued, .badge.indexing { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.notice { border-radius: 8px; padding: 9px 12px; font-size: 13.5px; }
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.bad { background: var(--bad-soft); color: var(--bad); }
.notice.ok { background: var(--ok-soft); color: var(--ok); }

/* top bar */
.topbar { height: 56px; display: flex; align-items: center; gap: 12px; padding: 0 16px; background: var(--surface);
  border-bottom: 1px solid var(--border); position: sticky; top: env(safe-area-inset-top, 0px); z-index: 5; }
.logo { display: flex; align-items: center; gap: 8px; font-weight: 800; font-size: 17px; letter-spacing: -.01em; text-decoration: none; color: var(--text); }
.logo .mark { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); display: grid; place-items: center; color: #fff; font-size: 16px; }
.spacer { flex: 1; min-width: 0; }
.usermenu { display: flex; align-items: center; gap: 8px; font-size: 14px; flex-shrink: 0; }
.btn { white-space: nowrap; }
.model-badge { text-transform: none; max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
@media (max-width: 1000px) { .model-badge, .usermenu .uname { display: none; } }
.iconbtn { border: 0; background: transparent; cursor: pointer; padding: 6px; border-radius: 8px; color: var(--muted); }
.iconbtn:hover { background: var(--surface-2); color: var(--text); }

/* login */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  box-shadow: var(--shadow); padding: 28px; display: grid; gap: 14px; }
.login-card h1 { margin: 0; font-size: 22px; }

/* chat layout */
.layout { display: grid; grid-template-columns: 260px minmax(0, 1fr) 320px; height: calc(100% - 56px); }
.sidebar, .docpanel { background: var(--surface); border-right: 1px solid var(--border); overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.docpanel { border-right: 0; border-left: 1px solid var(--border); }
.sidebar h3, .docpanel h3 { margin: 6px 4px 0; font-size: 12px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); }
.convlist { display: grid; gap: 2px; }
.conv { display: flex; align-items: center; gap: 4px; border-radius: 8px; padding: 7px 8px; cursor: pointer; font-size: 14px; }
.conv:hover { background: var(--surface-2); }
.conv.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.conv span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv button { visibility: hidden; }
.conv:hover button { visibility: visible; }
.main { display: flex; flex-direction: column; min-width: 0; }
.messages { flex: 1; overflow-y: auto; padding: 24px 16px; }
.thread { max-width: 820px; margin: 0 auto; display: grid; gap: 18px; grid-template-columns: minmax(0, 1fr); }
.thread > *, .msg .body, .sources, .source, .source details { min-width: 0; }
.messages { overflow-x: hidden; }
.msg.user { justify-self: end; max-width: 80%; background: var(--user-bubble); color: var(--user-text); padding: 10px 14px;
  border-radius: 14px 14px 4px 14px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.assistant { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; box-shadow: var(--shadow); }
.msg .body { overflow-wrap: anywhere; }
.msg .body p { margin: 0 0 8px; }
.msg .body p:last-child { margin-bottom: 0; }
.msg .body ul, .msg .body ol { margin: 4px 0 8px; padding: 0; padding-inline-start: 22px; }
.msg .body h4 { margin: 10px 0 4px; font-size: 15px; }
.msg .body code { background: var(--surface-2); padding: 0 4px; border-radius: 4px; font-size: 13px; }
.cite { display: inline-block; border: 0; font-size: 11.5px; font-weight: 800; background: var(--accent-soft); color: var(--accent);
  border-radius: 5px; padding: 0 5px; margin: 0 1px; cursor: pointer; vertical-align: 1px; }
.steps { display: grid; gap: 3px; margin-bottom: 10px; }
.step { font-size: 12.5px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.step::before { content: "⌕"; color: var(--accent); font-weight: 700; }
.step.info::before { content: "ℹ"; }
.sources { margin-top: 12px; border-top: 1px solid var(--border); padding-top: 10px; display: grid; gap: 6px; }
.source { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 13px; background: var(--bg); scroll-margin-top: 80px; }
.source.flash { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.source summary { cursor: pointer; display: flex; gap: 6px; align-items: baseline; list-style: none; }
.source summary::-webkit-details-marker { display: none; }
.source .sid { font-weight: 800; color: var(--accent); flex-shrink: 0; white-space: nowrap; }
.source .text { white-space: pre-wrap; margin-top: 6px; overflow-x: auto; }
.source .text.md { white-space: normal; }
.source .mdtable { font-size: 12.5px; }

/* Tables in answers and sources: a real table, one reading direction, a scroll area of its own, the first
   column kept in view while scrolling, and stacked "header: value" cards on phones. */
.mdtable-wrap { overflow-x: auto; margin: 8px 0 10px; border: 1px solid var(--border); border-radius: 10px;
  max-width: 100%; -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; }
.mdtable-wrap:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.mdtable { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 14px; line-height: 1.7; }
.mdtable th, .mdtable td { text-align: start; vertical-align: top; padding: 8px 12px; border-bottom: 1px solid var(--border);
  overflow-wrap: normal; word-break: normal; hyphens: auto; min-width: 7em; max-width: 28em; }
.mdtable thead th { background: var(--surface-2); color: var(--text); font-weight: 700; font-size: 13.5px; white-space: nowrap;
  position: sticky; top: 0; z-index: 1; }
.mdtable tbody th { font-weight: 600; font-size: inherit; color: var(--text); background: var(--surface); position: sticky; inset-inline-start: 0; z-index: 1; }
.mdtable thead th:first-child { inset-inline-start: 0; z-index: 2; }
.mdtable tbody tr:nth-child(even) td, .mdtable tbody tr:nth-child(even) th { background: var(--bg); }
.mdtable tbody tr:last-child > * { border-bottom: 0; }
.mdtable tbody tr:hover > * { background: var(--accent-soft); }
.mdtable .num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.mdtable .cite { vertical-align: 0; }
@media (max-width: 600px) {
  .mdtable-wrap { border: 0; overflow: visible; }
  .mdtable, .mdtable tbody, .mdtable tr, .mdtable th, .mdtable td { display: block; width: 100%; max-width: none; min-width: 0; }
  .mdtable thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .mdtable tbody tr { border: 1px solid var(--border); border-radius: 10px; margin-bottom: 8px; overflow: hidden; }
  .mdtable tbody th { position: static; background: var(--surface-2) !important; font-weight: 700; }
  .mdtable tbody td { border-bottom: 1px solid var(--border); white-space: normal; }
  .mdtable tbody td[data-label]::before { content: attr(data-label); display: block; color: var(--muted); font-weight: 600; font-size: 12px; line-height: 1.5; }
  .mdtable tbody tr:nth-child(even) td { background: var(--surface); }
  .mdtable tbody td:last-child { border-bottom: 0; }
}
.metaline { font-size: 12px; color: var(--muted); margin-top: 8px; display: flex; gap: 10px; flex-wrap: wrap; }
.composer { border-top: 1px solid var(--border); background: var(--surface); padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px)); }
.composer form { max-width: 820px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: none; min-height: 46px; max-height: 200px; border-radius: 12px; padding: 11px 14px; }
.composer .hint { max-width: 820px; margin: 6px auto 0; font-size: 11.5px; color: var(--muted); text-align: center; }
.welcome { max-width: 640px; margin: 8vh auto 0; text-align: center; display: grid; gap: 12px; }
.welcome h2 { margin: 0; font-size: 26px; letter-spacing: -.01em; }
.suggest { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.suggest button { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 12px; cursor: pointer; font-size: 13.5px; }
.suggest button:hover { border-color: var(--accent); color: var(--accent); }
.typing { display: inline-flex; gap: 4px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* documents */
.dropzone { border: 2px dashed var(--border); border-radius: 12px; padding: 18px 12px; text-align: center; cursor: pointer;
  color: var(--muted); font-size: 13.5px; background: var(--bg); transition: border-color .15s, background .15s; }
.dropzone:hover, .dropzone.over { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.dropzone strong { display: block; color: var(--text); font-size: 14.5px; margin-bottom: 2px; }
.uploads { display: grid; gap: 4px; }
.upl { font-size: 12.5px; display: flex; justify-content: space-between; gap: 6px; }
.doclist { display: grid; gap: 6px; }
.doc { border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; font-size: 13px; display: grid; gap: 3px; }
.doc .name { font-weight: 600; overflow-wrap: anywhere; }
.doc .row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; color: var(--muted); font-size: 12px; }
.doc .err { color: var(--bad); font-size: 12px; }
.doc .warn { color: var(--warn); font-size: 12px; }

/* admin */
.admin-page { max-width: 1100px; margin: 0 auto; padding: 20px 16px 40px; display: grid; gap: 16px; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; border-bottom: 1px solid var(--border); }
.tabs button { border: 0; background: transparent; padding: 9px 14px; cursor: pointer; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button[aria-selected=true] { color: var(--accent); border-bottom-color: var(--accent); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.card h2 { margin: 0 0 12px; font-size: 16px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.stat b { display: block; font-size: 26px; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: 13px; }
.tablewrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
th, td { text-align: left; padding: 8px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); font-weight: 600; font-size: 12.5px; }
td select { width: auto; padding: 4px 6px; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.formrow { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.formrow > * { flex: 1 1 160px; }
.formrow > .btn { flex: 0 0 auto; }
.toast { position: fixed; bottom: calc(20px + env(safe-area-inset-bottom, 0px)); left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 50; box-shadow: var(--shadow); max-width: calc(100% - 32px); }
dialog { border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text); padding: 20px; width: min(440px, calc(100% - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog h3 { margin: 0 0 10px; }
dialog .dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
dialog form { display: grid; gap: 10px; }
.mobile-only { display: none; }

@media (max-width: 1100px) { .layout { grid-template-columns: 240px minmax(0, 1fr); } .docpanel { display: none; } .docpanel.open { display: flex; position: fixed; right: 0; top: 56px; bottom: 0; width: min(340px, 100%); z-index: 6; box-shadow: var(--shadow); } .mobile-docs { display: inline-flex; } }
@media (min-width: 1101px) { .mobile-docs { display: none; } }
@media (max-width: 760px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar { display: none; }
  .sidebar.open { display: flex; position: fixed; left: 0; top: 56px; bottom: 0; width: min(300px, 100%); z-index: 6; box-shadow: var(--shadow); }
  .mobile-only { display: inline-flex; }
  .usermenu .uname { display: none; }
  .msg.user { max-width: 92%; }
}
.check { display: flex; gap: 6px; align-items: center; margin-top: 4px; font-size: 12.5px; font-weight: 400; color: var(--muted); }
.check input { width: auto; margin: 0; }
