/* ===========================================================================
   TAOLIS Knowledge Base — Claude-inspired warm, minimal light theme
   =========================================================================== */
:root {
  /* Warm paper palette */
  --bg: #f5f4ef;            /* app background (warm cream) */
  --surface: #ffffff;       /* cards / panels */
  --surface-2: #faf9f5;     /* subtle raised / inputs */
  --surface-3: #f0eee6;     /* user bubble / hover */
  --border: #e6e3d9;        /* hairline borders */
  --border-strong: #d8d4c8;

  --text: #1f1d1a;          /* near-black, warm */
  --text-soft: #46433d;
  --muted: #8a857a;         /* secondary text */

  --accent: #1f3b5e;        /* navy blue */
  --accent-hover: #162c47;
  --accent-soft: #e7edf4;   /* navy tint backgrounds */

  --danger: #be4b49;
  --ok: #5a9b78;

  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(40,35,25,.04), 0 4px 16px rgba(40,35,25,.05);
  --shadow-lg: 0 12px 40px rgba(40,35,25,.14);

  --serif: "Georgia", "Iowan Old Style", "Palatino Linotype", "Times New Roman", serif;
  --sans: ui-sans-serif, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;

  /* True visible height on mobile (set from the VisualViewport API in JS). */
  --app-height: 100dvh;
}

/* ---------- Dark theme ---------- */
html[data-theme="dark"] {
  --bg: #1f1e1c;
  --surface: #2a2825;
  --surface-2: #242320;
  --surface-3: #34322d;
  --border: #3a3833;
  --border-strong: #4a4641;
  --text: #ece9e3;
  --text-soft: #cdc9c1;
  --muted: #9a958b;
  --accent: #7298c4;
  --accent-hover: #8aabd4;
  --accent-soft: #2c3a4c;
  --danger: #e06a68;
  --ok: #6bbf93;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.35);
  --shadow-lg: 0 12px 40px rgba(0,0,0,.5);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
html { transition: background .2s ease; }
body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}
.hidden { display: none !important; }
h1, h2, h3 { font-weight: 600; }

/* ---------- Buttons ---------- */
.btn {
  border: none; border-radius: 999px; padding: 9px 18px; font-size: 14px;
  font-weight: 500; cursor: pointer; font-family: var(--sans);
  transition: background .15s, color .15s, border-color .15s, box-shadow .15s;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { background: transparent; color: var(--text-soft); border: 1px solid var(--border-strong); }
.btn.ghost:hover { background: var(--surface-2); border-color: var(--muted); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid transparent; }
.btn.danger:hover { background: rgba(190,75,73,.08); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.tiny { padding: 5px 12px; font-size: 12.5px; }

/* ---------- Auth ---------- */
.auth-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.auth-card {
  width: 100%; max-width: 400px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 20px; padding: 40px 36px;
  box-shadow: var(--shadow);
}
.brand { text-align: center; margin-bottom: 28px; }
.brand h1 { font-family: var(--serif); font-size: 34px; letter-spacing: 1px; color: var(--text); font-weight: 600; }
.brand p { color: var(--muted); letter-spacing: 4px; font-size: 11px; text-transform: uppercase; margin-top: 4px; }
.auth-tabs { margin-bottom: 22px; }
.tabs { display: flex; gap: 4px; background: var(--surface-3); padding: 4px; border-radius: 999px; }
.tab {
  flex: 1; background: transparent; border: none; color: var(--muted);
  padding: 9px; border-radius: 999px; cursor: pointer; font-weight: 500; font-size: 14px;
  font-family: var(--sans); transition: background .15s, color .15s;
}
.tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form input, .modal select, .modal input {
  background: var(--surface-2); border: 1px solid var(--border-strong); color: var(--text);
  padding: 12px 14px; border-radius: var(--radius-sm); font-size: 14px; width: 100%;
  font-family: var(--sans); transition: border-color .15s, box-shadow .15s;
}
.auth-form input::placeholder { color: var(--muted); }
.auth-form input:focus, .modal input:focus, .modal select:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.auth-form .btn { margin-top: 4px; padding: 12px; }
.hint { color: var(--muted); font-size: 12.5px; text-align: center; }
.auth-error { color: var(--danger); font-size: 13px; min-height: 16px; text-align: center; }

/* ---------- Layout + left sidebar ---------- */
.layout { display: flex; min-height: 100vh; }

/* Mobile top bar + drawer overlay (shown only on small screens) */
.mobile-topbar { display: none; align-items: center; gap: 10px; height: 54px; padding: 0 12px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: fixed; top: 0; left: 0; right: 0; z-index: 50; }
.mobile-topbar .logo { font-family: var(--serif); font-weight: 600; font-size: 19px; color: var(--text); }
.mobile-topbar .logo span { color: var(--accent); }
.hamburger { background: transparent; border: none; color: var(--text); width: 40px; height: 40px;
  border-radius: 9px; display: flex; align-items: center; justify-content: center; cursor: pointer; }
.hamburger:hover { background: var(--surface-2); }
.hamburger svg { width: 22px; height: 22px; }
.sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 55; }
.sidebar {
  width: 244px; flex-shrink: 0; background: var(--surface);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
  padding: 16px 14px; position: sticky; top: 0; height: 100vh; height: 100dvh;
  transition: width .22s ease, padding .22s ease;
}
.side-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 4px 16px; }
.sidebar .logo { font-family: var(--serif); font-weight: 600; font-size: 21px; letter-spacing: .5px; color: var(--text); padding: 0 6px; white-space: nowrap; overflow: hidden; }
.sidebar .logo span { color: var(--accent); }
.collapse-btn {
  background: transparent; border: none; color: var(--muted); cursor: pointer;
  width: 32px; height: 32px; border-radius: 8px; display: flex; align-items: center;
  justify-content: center; flex-shrink: 0; transition: background .15s, color .15s;
}
.collapse-btn:hover { background: var(--surface-2); color: var(--text); }
.collapse-btn svg { width: 18px; height: 18px; transition: transform .22s ease; }
.nav-label { font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted);
  font-weight: 600; padding: 4px 12px 8px; white-space: nowrap; overflow: hidden; }
.nav { display: flex; flex-direction: column; gap: 3px; }
.navbtn {
  position: relative; display: flex; align-items: center; gap: 11px; width: 100%; text-align: left;
  background: transparent; border: none; color: var(--text-soft); padding: 10px 12px;
  border-radius: 10px; cursor: pointer; font-weight: 500; font-size: 14.5px; font-family: var(--sans);
  transition: background .15s, color .15s;
}
.navbtn span { white-space: nowrap; overflow: hidden; }
.navbtn svg { width: 18px; height: 18px; flex-shrink: 0; opacity: .75; }
.navbtn:hover { background: var(--surface-2); color: var(--text); }
.navbtn.active { background: var(--accent-soft); color: var(--accent-hover); }
.navbtn.active svg { opacity: 1; }
.navbtn.active::before {
  content: ""; position: absolute; left: -14px; top: 50%; transform: translateY(-50%);
  width: 3px; height: 20px; background: var(--accent); border-radius: 0 3px 3px 0;
}
/* Recent chats list */
.chat-history { display: flex; flex-direction: column; flex: 1; min-height: 0; margin-top: 16px; }
.ch-head { display: flex; align-items: center; justify-content: space-between; padding: 0 8px 4px; }
.ch-new { background: transparent; border: none; color: var(--muted); width: 28px; height: 28px;
  border-radius: 8px; display: flex; align-items: center; justify-content: center; cursor: pointer; transition: background .15s, color .15s; }
.ch-new:hover { background: var(--surface-2); color: var(--text); }
.ch-new svg { width: 16px; height: 16px; }
.ch-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 1px; }
.ch-item { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 8px;
  cursor: pointer; color: var(--text-soft); font-size: 13.5px; }
.ch-item:hover { background: var(--surface-2); }
.ch-item.active { background: var(--accent-soft); color: var(--accent-hover); }
.ch-item .ch-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-del { background: transparent; border: none; color: var(--muted); cursor: pointer; opacity: 0;
  padding: 2px 5px; border-radius: 6px; font-size: 13px; line-height: 1; flex-shrink: 0; }
.ch-item:hover .ch-del { opacity: .75; }
.ch-del:hover { color: var(--danger); background: var(--surface-3); }
.ch-empty { color: var(--muted); font-size: 12.5px; padding: 6px 10px; }
.ch-section { font-size: 10px; text-transform: uppercase; letter-spacing: .7px;
  color: var(--muted); padding: 10px 8px 4px; }
.ch-search {
  width: 100%; margin: 2px 0 8px; padding: 7px 10px; font: inherit; font-size: 13px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text);
}
.ch-search:focus { outline: none; border-color: var(--accent); }
.sidebar.collapsed .ch-search { display: none; }
.ch-tools { display: flex; align-items: center; gap: 2px; margin-left: auto; }
.ch-star, .ch-ren { background: transparent; border: none; cursor: pointer; color: var(--muted);
  font-size: 13px; line-height: 1; padding: 2px 4px; border-radius: 6px; opacity: 0; }
.ch-item:hover .ch-star, .ch-item:hover .ch-ren { opacity: .75; }
.ch-star.on { opacity: 1; color: var(--accent); }
.ch-star:hover, .ch-ren:hover { background: var(--surface-3); color: var(--accent); opacity: 1; }

/* ---------- Rich markdown in answers (code blocks, tables, links) ---------- */
.code-block { border: 1px solid var(--border); border-radius: var(--radius-sm);
  overflow: hidden; margin: 10px 0; background: var(--surface-2); }
.code-head { display: flex; align-items: center; justify-content: space-between;
  padding: 5px 10px; background: var(--surface-3); border-bottom: 1px solid var(--border); }
.code-lang { font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
.code-copy { border: none; background: transparent; color: var(--muted); cursor: pointer;
  font-size: 12px; padding: 2px 6px; border-radius: 6px; }
.code-copy:hover { background: var(--surface); color: var(--text); }
.code-block pre { margin: 0; padding: 12px; overflow-x: auto; }
.code-block code { font-family: ui-monospace, "Cascadia Code", "Consolas", monospace;
  font-size: 13px; line-height: 1.5; white-space: pre; }
/* Answer tables: scroll sideways inside the message; columns keep readable widths. */
.md-table { overflow-x: auto; max-width: 100%; margin: 10px 0; -webkit-overflow-scrolling: touch; }
.md-table table { border-collapse: collapse; font-size: 14px; width: auto; }
.md-table th, .md-table td { border: 1px solid var(--border); padding: 7px 11px; text-align: left;
  vertical-align: top; min-width: 90px; max-width: 240px;
  white-space: normal; overflow-wrap: normal; word-break: normal; }
.md-table th { background: var(--surface-3); font-weight: 600; }
.msg .bubble a { color: var(--accent); text-decoration: underline; }

/* ---------- Library view ---------- */
.lib-filters { display: flex; gap: 8px; flex-wrap: wrap; }
.lib-folder { max-width: 280px; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap; color: var(--muted); font-size: 13px; }

/* ---------- Admin: memory + feedback review ---------- */
.mem-item { display: flex; align-items: center; gap: 10px; padding: 8px 12px; margin-bottom: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); }
.mem-item span { flex: 1; }
.fb-item { display: flex; gap: 12px; padding: 12px; margin-bottom: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  border-left: 3px solid var(--border-strong); }
.fb-item.up { border-left-color: var(--ok); }
.fb-item.down { border-left-color: var(--danger); }
.fb-rate { font-size: 18px; }
.fb-body { flex: 1; min-width: 0; }
.fb-q { font-weight: 600; margin-bottom: 4px; }
.fb-a { color: var(--text-soft); font-size: 13.5px; }
.fb-meta { color: var(--muted); font-size: 12px; margin-top: 6px; }

/* ---------- Per-user memory (Settings) ---------- */
.umem-h { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border); }
.umem-auto { display: flex; align-items: center; gap: 8px; margin: 8px 0 12px; font-size: 14px; cursor: pointer; }
.umem-auto input { width: 16px; height: 16px; }
.umem-list { max-height: 220px; overflow-y: auto; }
.umem-src { color: var(--muted); font-style: italic; font-size: 12px; }
.sidebar.collapsed .chat-history { display: none; }

.sidebar-foot { margin-top: auto; border-top: 1px solid var(--border); padding-top: 14px; display: flex; flex-direction: column; gap: 10px; }
#user-badge { color: var(--muted); font-size: 13px; padding: 4px 8px; display: flex; flex-wrap: wrap; align-items: center; gap: 3px; white-space: nowrap; overflow: hidden; }
#user-badge b { color: var(--text); font-weight: 600; }
.sidebar-foot .btn { width: 100%; display: flex; align-items: center; justify-content: center; gap: 9px; }
.sidebar-foot .btn svg { width: 17px; height: 17px; flex-shrink: 0; }
.role-pill {
  background: var(--accent-soft); border-radius: 999px; padding: 2px 9px;
  font-size: 10px; color: var(--accent-hover); margin-left: 4px; text-transform: uppercase;
  letter-spacing: .5px; font-weight: 600;
}

/* Collapsed (retracted) rail */
.sidebar.collapsed { width: 72px; padding: 16px 12px; }
.sidebar.collapsed .side-head { justify-content: center; padding: 6px 0 16px; }
.sidebar.collapsed .logo,
.sidebar.collapsed .nav-label,
.sidebar.collapsed .navbtn span,
.sidebar.collapsed .new-chat-side span,
.sidebar.collapsed #user-badge,
.sidebar.collapsed .sidebar-foot .btn span { display: none; }
.sidebar.collapsed .navbtn { justify-content: center; gap: 0; padding: 11px; }
.sidebar.collapsed .navbtn.active::before { left: -12px; }
.sidebar.collapsed .new-chat-side { padding: 11px; gap: 0; }
.sidebar.collapsed .sidebar-foot .btn { padding: 11px; }
.sidebar.collapsed .collapse-btn svg { transform: rotate(180deg); }
.main-area { flex: 1; min-width: 0; height: 100vh; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
.content { flex: 1; overflow-y: auto; overflow-x: hidden; }
#tab-documents, #tab-admin { max-width: 900px; margin: 0 auto; width: 100%; padding: 30px 26px; }

/* ---------- Tablet / mobile: sidebar becomes a slide-in drawer ---------- */
@media (max-width: 760px) {
  /* Lock the page: only inner regions scroll, so there's a single scrollbar. */
  html, body { height: var(--app-height); overflow: hidden; }
  #auth-view { height: var(--app-height); overflow-y: auto; }
  .auth-wrap { min-height: 100%; }

  /* The whole app is one flex column at exactly the visible height. */
  #app-view { display: flex; flex-direction: column; height: var(--app-height); overflow: hidden; }
  .mobile-topbar { display: flex; position: static; transform: none; flex-shrink: 0; }
  .layout { display: block; flex: 1; min-height: 0; overflow: hidden; }

  .sidebar {
    position: fixed; top: 0; left: 0; height: 100vh; height: var(--app-height); width: 270px;
    transform: translateX(-100%); transition: transform .25s ease; z-index: 60;
  }
  .sidebar.open { transform: none; box-shadow: var(--shadow-lg); }
  .collapse-btn { display: none; }
  .side-head { justify-content: flex-start; }
  .sidebar-overlay.show { display: block; }

  .main-area { margin-top: 0; height: 100%; overflow: hidden; }
  #tab-chat { height: 100%; padding: 0; }
  #tab-documents, #tab-admin { padding: 20px 14px; }

  /* Documents toolbar stacks; search grows to fill the row */
  .doc-toolbar { flex-direction: column; align-items: stretch; }
  .doc-tools { width: 100%; }
  .doc-search { flex: 1; }
  .doc-search input { width: 100%; }
  .doc-table-wrap { overflow-x: auto; }

  /* Inputs >=16px so iOS Safari doesn't auto-zoom on focus */
  .auth-form input, .chat-input input, .chat-input textarea, .doc-search input, .modal input,
  .modal select, .inline-form input, .inline-form select, #status-filter { font-size: 16px; }

  /* Document preview = full screen on phones; other modals get a small margin. */
  .modal { padding: 0; }
  .preview-card { width: 100vw; height: var(--app-height); max-width: none; border-radius: 0; }
  .preview-body img { max-width: 100%; height: auto; }
  .modal-card:not(.preview-card) { margin: 12px; max-height: 88vh; }
}

/* ---------- Small phones: trim table columns + stack forms ---------- */
@media (max-width: 560px) {
  .brand h1 { font-size: 28px; }
  .auth-card { padding: 28px 22px; }
  .content { }
  .stat-card .num { font-size: 24px; }

  /* Hide Size + Uploaded columns in the documents table to fit */
  #tab-documents .doc-table thead th:nth-child(3),
  #tab-documents .doc-table thead th:nth-child(5),
  #tab-documents .doc-table tbody td:nth-child(3),
  #tab-documents .doc-table tbody td:nth-child(5) { display: none; }
  .doc-table th, .doc-table td { padding: 11px 12px; }
  .fic { width: 24px; height: 24px; margin-right: 8px; }
  .btn.tiny { padding: 5px 9px; }

  /* Create-user form: full-width stacked fields */
  .inline-form { flex-direction: column; align-items: stretch; }
  .inline-form input, .inline-form select { width: 100%; min-width: 0; }
  .inline-form .btn { width: 100%; }

  .sync-bar { flex-direction: column; align-items: stretch; }
  .sync-bar .btn { width: 100%; }

  .chat-empty h2 { font-size: 25px; }
  .msg .bubble { max-width: 94%; }
}

/* ---------- Chat ---------- */
#tab-chat { display: flex; flex-direction: column; height: 100%; width: 100%; }
/* Full-width scroll area so the wheel works anywhere; content is centred at 780px
   via horizontal padding (the side "margins" are now part of the scroll box). */
.chat-window { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 28px max(20px, calc((100% - 780px) / 2)) 16px; }
.chat-empty { text-align: center; margin-top: min(15vh, 120px); }
.chat-empty .spark { font-size: 26px; color: var(--accent); display: block; margin-bottom: 14px; }
.chat-empty h2 { font-family: var(--serif); font-size: 30px; font-weight: 600; color: var(--text); margin-bottom: 10px; }
.chat-empty p { color: var(--muted); max-width: 460px; margin: 0 auto 24px; }

/* Segmented control: Ask · Look up · Generate */
.mode-switch {
  display: inline-flex; gap: 3px; margin: 0 auto 22px; padding: 4px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px;
}
.mode-seg {
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
  border: none; background: transparent; cursor: pointer; padding: 7px 18px;
  border-radius: 999px; font-size: 13.5px; font-family: var(--sans);
  color: var(--muted); transition: color .15s, background .15s;
}
.mode-seg .seg-ico { font-size: 14px; line-height: 1; }
.mode-seg:hover { color: var(--text-soft); }
.mode-seg.active {
  background: var(--surface); color: var(--text); font-weight: 600;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .12);
}
.mode-panels { max-width: 460px; margin: 0 auto; }
.mode-hint { color: var(--muted); font-size: 12.5px; margin: 14px auto 0; }

/* Suggestion lists — low visual weight (filled, borderless at rest) */
.suggestions { display: flex; flex-direction: column; gap: 8px; max-width: 460px; margin: 0 auto; }
.chip-row { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.suggestion {
  background: var(--surface-2); border: 1px solid transparent; color: var(--text-soft);
  padding: 13px 16px; border-radius: var(--radius); cursor: pointer; text-align: left;
  font-size: 14px; font-family: var(--sans); transition: border-color .15s, background .15s;
}
.suggestion:hover { border-color: var(--border-strong); background: var(--surface-3); }
.suggestion .tag { color: var(--accent); font-weight: 600; margin-right: 8px; font-size: 12px; }

.msg { margin-bottom: 24px; display: flex; }
.msg.user { justify-content: flex-end; }
.bubble { max-width: 88%; line-height: 1.6; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; min-width: 0; }
.msg.user .bubble {
  background: var(--surface-3); color: var(--text); padding: 12px 16px;
  border-radius: 18px 18px 4px 18px;
}
.msg.bot .bubble { color: var(--text); padding: 2px 0; }
.sources { margin-top: 14px; display: flex; flex-wrap: wrap; gap: 8px; }
.source-chip {
  display: inline-flex; align-items: center; gap: 7px; background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: 999px; padding: 6px 13px; font-size: 12.5px;
  color: var(--text-soft); cursor: pointer; text-decoration: none; transition: border-color .15s, background .15s;
  max-width: 100%; white-space: normal; word-break: break-word;
}
.source-chip:hover { border-color: var(--accent); background: var(--surface-2); }
.source-chip .score { color: var(--accent); font-weight: 600; }
.source-chip .score.sim { color: var(--muted); font-weight: 500; }
.typing { color: var(--muted); }
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; }
@keyframes blink { 0%,50%{opacity:1} 51%,100%{opacity:0} }

/* Composer */
.chat-input {
  display: flex; align-items: flex-end; gap: 8px; margin: 8px max(20px, calc((100% - 780px) / 2)) 22px;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 26px; padding: 7px 7px 7px 18px; box-shadow: var(--shadow);
  transition: border-color .15s, box-shadow .15s;
}
.chat-input:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.chat-input input {
  flex: 1; background: transparent; border: none; color: var(--text);
  padding: 8px 0; font-size: 15px; font-family: var(--sans);
}
.chat-input input::placeholder { color: var(--muted); }
.chat-input input:focus { outline: none; }
.chat-input textarea {
  flex: 1; background: transparent; border: none; color: var(--text); resize: none;
  padding: 8px 0; font-size: 16px; font-family: var(--sans); line-height: 1.5;
  max-height: 160px; overflow-y: auto;
}
.chat-input textarea::placeholder { color: var(--muted); }
.chat-input textarea:focus { outline: none; }
#chat-send {
  width: 38px; height: 38px; border-radius: 50%; padding: 0; font-size: 18px;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
#chat-send svg { width: 17px; height: 17px; }
#chat-send.stop { background: var(--text); color: var(--surface); }
#chat-send.stop:hover { background: var(--text-soft); }

/* ---------- Documents / tables ---------- */
.doc-toolbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; gap: 12px; flex-wrap: wrap; }
.cat-filters { display: flex; gap: 7px; flex-wrap: wrap; }
.cat-pill {
  background: var(--surface); border: 1px solid var(--border); color: var(--muted);
  padding: 6px 14px; border-radius: 999px; cursor: pointer; font-size: 13px; font-weight: 500;
  font-family: var(--sans); transition: background .15s, color .15s, border-color .15s;
}
.cat-pill:hover { color: var(--text); border-color: var(--border-strong); }
.cat-pill.active { background: var(--text); color: var(--surface); border-color: var(--text); }
.doc-table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.doc-table { width: 100%; border-collapse: collapse; }
.doc-table th { text-align: left; padding: 13px 18px; font-size: 11.5px; text-transform: uppercase;
  color: var(--muted); border-bottom: 1px solid var(--border); letter-spacing: .6px; font-weight: 600; }
.doc-table td { padding: 13px 18px; border-bottom: 1px solid var(--border); font-size: 14px; color: var(--text-soft); }
.doc-table tr:last-child td { border-bottom: none; }
.doc-table tbody tr:hover td { background: var(--surface-2); }
.doc-name { color: var(--text); font-weight: 500; cursor: pointer; }
.doc-name:hover { color: var(--accent); }
.empty-msg { padding: 32px; text-align: center; color: var(--muted); }

.badge { padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge.indexed { background: rgba(90,155,120,.14); color: var(--ok); }
.badge.indexing, .badge.pending { background: var(--accent-soft); color: var(--accent-hover); }
.badge.error { background: rgba(190,75,73,.12); color: var(--danger); }
.badge.image, .badge.no-text { background: var(--surface-3); color: var(--muted); }

/* ---------- Admin ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px,1fr)); gap: 14px; margin-bottom: 28px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.stat-card .num { font-family: var(--serif); font-size: 30px; font-weight: 600; color: var(--text); }
.stat-card .lbl { color: var(--muted); font-size: 13px; margin-top: 2px; }
#tab-admin h3 { margin: 24px 0 12px; font-size: 16px; color: var(--text); }
.sync-bar { display: flex; align-items: center; justify-content: space-between; gap: 16px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; box-shadow: var(--shadow); flex-wrap: wrap; }
.sync-text strong { display: block; color: var(--text); font-size: 15px; }
.sync-text span { color: var(--muted); font-size: 13px; }
#sync-result { margin-top: 14px; }
.status-line { display: flex; gap: 16px; flex-wrap: wrap; color: var(--muted); background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; }
.status-line b { color: var(--text); }
select.role-select { background: var(--surface-2); border: 1px solid var(--border-strong); color: var(--text);
  border-radius: 8px; padding: 5px 9px; font-size: 13px; font-family: var(--sans); }

/* ---------- Modal + toast ---------- */
.modal { position: fixed; inset: 0; background: rgba(31,29,26,.4); backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal-card { background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 28px; width: 100%; max-width: 430px; box-shadow: var(--shadow-lg); max-height: 92vh; overflow-y: auto; }
.modal-card h3 { margin-bottom: 20px; font-size: 19px; }
.modal-card label { display: block; color: var(--text-soft); font-size: 13px; font-weight: 500; margin-bottom: 16px; }
.modal-card label select, .modal-card label input { margin-top: 7px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }
.toast { position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: #fff; padding: 12px 22px; border-radius: 999px; z-index: 60;
  box-shadow: var(--shadow-lg); font-size: 14px; }
.toast.error { background: var(--danger); }
.toast.ok { background: var(--text); }

/* ---------- Sidebar foot icon buttons (theme + settings) ---------- */
.foot-tools { display: flex; gap: 8px; }
.icon-btn {
  flex: 1; background: transparent; border: 1px solid var(--border); color: var(--muted);
  border-radius: 10px; padding: 9px; cursor: pointer; display: flex; align-items: center;
  justify-content: center; gap: 8px; font-size: 13px; font-family: var(--sans); transition: background .15s, color .15s, border-color .15s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); border-color: var(--border-strong); }
.icon-btn svg { width: 17px; height: 17px; }
.sidebar.collapsed .foot-tools { flex-direction: column; }
.sidebar.collapsed .icon-btn span { display: none; }

/* ---------- Markdown content in chat ---------- */
.bubble p { margin: 0 0 10px; } .bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 4px 0 10px 22px; } .bubble li { margin: 3px 0; }
.bubble strong { font-weight: 700; }
.bubble em { font-style: italic; }
.bubble code { background: var(--surface-3); padding: 1px 6px; border-radius: 5px; font-size: 13px; font-family: ui-monospace, "Cascadia Code", Consolas, monospace; }
.bubble h1, .bubble h2, .bubble h3 { font-size: 16px; margin: 8px 0 6px; }
.msg { animation: fadein .25s ease; }
@keyframes fadein { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.msg-actions { margin-top: 10px; display: flex; gap: 4px; flex-wrap: wrap; align-items: center; }
.msg.user .msg-actions { justify-content: flex-end; }
.act, .copy-btn { background: transparent; border: 1px solid transparent; color: var(--muted);
  border-radius: 8px; padding: 4px 10px; font-size: 12px; cursor: pointer; display: inline-flex;
  gap: 6px; align-items: center; font-family: var(--sans); }
.act:hover, .copy-btn:hover { color: var(--text); border-color: var(--border-strong); background: var(--surface-2); }
.act svg, .copy-btn svg { width: 13px; height: 13px; }
.act.on { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }

/* Export dropdown (PDF / Word) on the answer action row */
.export-menu { position: relative; display: inline-flex; }
.export-pop {
  position: absolute; bottom: calc(100% + 6px); left: 0; z-index: 30;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 10px; padding: 5px; min-width: 130px;
  display: flex; flex-direction: column; gap: 2px;
  box-shadow: 0 8px 24px rgba(0,0,0,.28);
}
.exp-opt {
  background: transparent; border: none; color: var(--text-soft); text-align: left;
  padding: 8px 10px; border-radius: 7px; font-size: 13px; cursor: pointer; font-family: var(--sans);
}
.exp-opt:hover { background: var(--surface-2); color: var(--text); }

/* Prominent document download buttons under a generated document */
.doc-actions { margin-top: 14px; display: flex; gap: 10px; flex-wrap: wrap; }
.doc-actions .btn { padding: 9px 16px; font-size: 13px; }

/* Preset quick-start chips on the empty chat screen */
/* Chips inside the Look up / Generate panels — light pill style */
.preset-chip, .inv-chip {
  background: var(--surface-2); border: 1px solid transparent; color: var(--text-soft);
  padding: 10px 15px; border-radius: 999px; cursor: pointer; text-align: center;
  font-size: 13.5px; font-family: var(--sans); transition: border-color .15s, background .15s;
}
.preset-chip:hover, .inv-chip:hover { border-color: var(--border-strong); background: var(--surface-3); }

/* Narrow screens: don't cram three equal segments. Size them to content with
   breathing room + a 44px touch target; if they overflow a very narrow phone,
   the control scrolls horizontally rather than squeezing the labels. */
@media (max-width: 560px) {
  .mode-switch {
    max-width: 100%; overflow-x: auto; flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  .mode-switch::-webkit-scrollbar { display: none; }
  .mode-seg { flex: 0 0 auto; min-height: 44px; padding: 0 15px; font-size: 13px; gap: 7px; }
  .suggestions, .mode-panels, .chip-row { max-width: 100%; }
}

/* Chat header bar (New chat) */
/* Sidebar "New chat" button */
.new-chat-side {
  width: 100%; display: flex; align-items: center; justify-content: center; gap: 9px;
  margin-bottom: 16px;
}
.new-chat-side svg { width: 17px; height: 17px; flex-shrink: 0; }

/* ---------- Document search + file icons + sortable ---------- */
.doc-search { display: flex; align-items: center; gap: 8px; background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: 999px; padding: 7px 14px; }
.doc-search input { border: none; background: transparent; color: var(--text); font-size: 14px; font-family: var(--sans); outline: none; width: 190px; }
.doc-search input::placeholder { color: var(--muted); }
.doc-search svg { width: 15px; height: 15px; color: var(--muted); flex-shrink: 0; }
.doc-tools { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.fic { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px;
  border-radius: 6px; font-size: 9px; font-weight: 700; color: #fff; margin-right: 11px; flex-shrink: 0;
  letter-spacing: .3px; vertical-align: middle; }
.fic.pdf { background: #d4493f; } .fic.doc { background: #2b6cb0; } .fic.xls { background: #2f855a; }
.fic.img { background: #805ad5; } .fic.txt { background: #718096; } .fic.csv { background: #38a169; }
.fic.other { background: #a0aec0; }
.doc-name-cell { display: flex; align-items: center; }

.doc-table th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
.doc-table th.sortable:hover { color: var(--text); }
.doc-table th .arrow { margin-left: 4px; font-size: 10px; opacity: .9; }

/* ---------- Drag & drop upload ---------- */
.dropzone { border: 2px dashed var(--border-strong); border-radius: var(--radius); padding: 28px 18px;
  text-align: center; color: var(--muted); cursor: pointer; transition: border-color .15s, background .15s; }
.dropzone:hover { border-color: var(--muted); }
.dropzone.drag { border-color: var(--accent); background: var(--accent-soft); color: var(--text); }
.dropzone strong { color: var(--text); display: block; margin-bottom: 4px; }
.dropzone .sub { font-size: 12.5px; }
.upload-list { margin-top: 14px; display: flex; flex-direction: column; gap: 7px; max-height: 220px; overflow: auto; }
.upload-item { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.upload-item .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-soft); }
.upload-item .stt { font-size: 12px; white-space: nowrap; }
.upload-item .stt.ok { color: var(--ok); } .upload-item .stt.err { color: var(--danger); } .upload-item .stt.go { color: var(--muted); }

/* ---------- Admin create-user form ---------- */
.inline-form { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 14px; box-shadow: var(--shadow); }
.inline-form input, .inline-form select { background: var(--surface-2); border: 1px solid var(--border-strong);
  color: var(--text); border-radius: 8px; padding: 9px 12px; font-size: 13.5px; font-family: var(--sans); }
.inline-form input { flex: 1; min-width: 140px; }
.inline-form input:focus, .inline-form select:focus { outline: none; border-color: var(--accent); }

/* ---------- Skeleton loaders ---------- */
.skeleton { background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 37%, var(--surface-2) 63%);
  background-size: 400% 100%; animation: shimmer 1.4s ease infinite; border-radius: 7px; }
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: -100% 0; } }
.skel-line { height: 14px; margin: 14px 18px; }

/* ---------- System health ---------- */
.health-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px,1fr)); gap: 12px; margin-bottom: 8px; }
.health-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); display: flex; align-items: center; gap: 12px; }
.health-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.health-dot.up { background: var(--ok); box-shadow: 0 0 0 3px rgba(90,155,120,.18); }
.health-dot.down { background: var(--danger); box-shadow: 0 0 0 3px rgba(190,75,73,.18); }
.health-card .ht { font-weight: 600; color: var(--text); font-size: 14px; }
.health-card .hs { color: var(--muted); font-size: 12.5px; }

/* ---------- Pagination ---------- */
.pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 14px; color: var(--muted); font-size: 13px; flex-wrap: wrap; }
.pager .pbtns { display: flex; gap: 8px; }

/* ---------- Preview modal ---------- */
.preview-card { max-width: 920px; width: 92vw; height: 88vh; padding: 0; display: flex; flex-direction: column; overflow: hidden; }
.preview-head { display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 18px; border-bottom: 1px solid var(--border); }
.preview-name { font-weight: 600; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.preview-actions { display: flex; gap: 8px; flex-shrink: 0; }
.preview-body { flex: 1; overflow: auto; background: var(--surface-2); display: flex; align-items: center; justify-content: center; }
.preview-body iframe { width: 100%; height: 100%; border: none; background: #fff; }
.preview-body img { max-width: 100%; max-height: 100%; object-fit: contain; }
.preview-body pre { white-space: pre-wrap; word-wrap: break-word; padding: 22px; width: 100%; height: 100%;
  overflow: auto; color: var(--text); font-family: ui-monospace, Consolas, monospace; font-size: 13px; line-height: 1.5; }
.preview-body .pmsg { color: var(--muted); padding: 30px; text-align: center; }

/* Scrollbar */
::-webkit-scrollbar { width: 10px; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 999px; border: 3px solid var(--bg); }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* Inventory answers: collapsed unit rows that expand into source chips. */
.inv-headline { margin: 0 0 8px; }
.inv-unit { border-top: 1px solid var(--border, rgba(127,127,127,.25)); }
.inv-unit-head {
  display: flex; align-items: center; gap: 8px; width: 100%;
  min-height: 44px; padding: 6px 4px; background: none; border: 0;
  color: inherit; font: inherit; text-align: left; cursor: pointer;
}
.inv-unit-head:hover { background: var(--surface-2, rgba(127,127,127,.08)); }
.inv-caret { width: 1em; flex: 0 0 auto; }
.inv-files { padding: 2px 4px 10px 24px; display: flex; flex-wrap: wrap; gap: 6px; }
.inv-files .source-chip { max-width: 100%; word-break: break-word; }
.inv-def { opacity: .8; font-size: .92em; margin-top: 10px; }

/* ---------- Two-factor auth / enrollment UI (v21) ---------- */
.auth-info { color: var(--text-soft); font-size: 14px; margin-bottom: 10px; text-align: center; }
.enroll-qr { display: block; width: 200px; height: 200px; margin: 12px auto; background: #fff; border-radius: 12px; padding: 8px; }
.enroll-secret { text-align: center; font-size: 13px; word-break: break-all; color: var(--text-soft); }
.enroll-secret code { background: var(--surface-2); padding: 2px 6px; border-radius: 6px; }
.backup-panel { margin-top: 16px; padding: 14px; border: 1px solid var(--border); border-radius: 12px; }
.backup-list { list-style: none; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.backup-list li { font-family: ui-monospace, monospace; font-size: 15px; letter-spacing: 1px;
  background: var(--surface-2); padding: 8px; border-radius: 8px; text-align: center; }
.backup-ack { display: flex; align-items: center; gap: 8px; margin: 12px 0; font-size: 14px; }
@media (max-width: 480px) { .backup-list { grid-template-columns: 1fr; } }
