/* ════════════════════════════════════════════════════════════════════
   KNOWLEDGE BASE — redesign (2026-11-23).
   Detail page: header (back + title + Chat), left column (Website /
   Upload file, SOURCE filter, files list), right chat panel.
   The app topbar is hidden for this route in navigate() (core/utils.js).
   Scoped under .kb-wrap so nothing leaks onto other pages.
   ════════════════════════════════════════════════════════════════════ */

.kb-wrap {
  --kb-border: #e3dfec;
  --kb-text: #1b1722;
  --kb-body: #2a2433;
  --kb-muted: #6b6580;
  --kb-pill-text: #5b5470;
  --kb-pill-bg: #efedf5;
  --kb-soft: #f6f5fa;
  --kb-placeholder: #8f86a8;
  --kb-green: #2f6b2b;
  --kb-green-hover: #275a24;
  --kb-mint: #e3f3ea;
  --kb-danger: #c2362e;

  display: flex;
  flex-direction: column;
  gap: 24px;
  height: 100%;
  min-height: 0;
  padding: 32px 40px 40px;
  background: #f8f7fb;
  color: var(--kb-text);
  font-family: 'Geist', 'Inter', system-ui, sans-serif;
}
.kb-wrap * { box-sizing: border-box; }
.kb-wrap--list { height: auto; min-height: 100%; }

/* ---------- header ---------- */
.kb-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  flex: 0 0 auto;
}
.kb-head-left {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}
.kb-icon-btn {
  width: 42px;
  height: 42px;
  flex: 0 0 42px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  border: 1px solid var(--kb-border);
  border-radius: 12px;
  color: var(--kb-text);
  font-size: 20px;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.kb-icon-btn:hover { background: var(--kb-soft); }
.kb-title-block { min-width: 0; }
.kb-title {
  margin: 0;
  font-size: 30px;
  line-height: 1.15;
  font-weight: 700;
  color: var(--kb-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.kb-subtitle {
  margin: 4px 0 0;
  font-size: 13px;
  color: var(--kb-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.kb-head-actions { display: flex; gap: 10px; flex: 0 0 auto; }

/* ---------- buttons ---------- */
.kb-btn {
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 18px;
  background: #fff;
  border: 1px solid var(--kb-border);
  border-radius: 10px;
  color: var(--kb-text);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
}
.kb-btn .mdi { font-size: 17px; line-height: 1; }
.kb-btn[hidden] { display: none; }
.kb-btn:hover { background: var(--kb-soft); }
.kb-btn-primary {
  background: var(--kb-green);
  border-color: var(--kb-green);
  color: #fff;
  font-weight: 600;
  padding: 0 20px;
}
.kb-btn-primary:hover { background: var(--kb-green-hover); border-color: var(--kb-green-hover); }
.kb-btn-grow { flex: 1 1 0; padding: 0 12px; }
.kb-btn-sm {
  height: 38px;
  padding: 0 14px;
  font-size: 13px;
}
.kb-btn-sm .mdi { font-size: 15px; }
.kb-btn.is-selected {
  background: var(--kb-mint);
  border-color: var(--kb-green);
  color: var(--kb-green);
}

/* ---------- body ---------- */
.kb-body {
  display: flex;
  gap: 24px;
  flex: 1 1 auto;
  min-height: 0;
}

/* ---------- left column ---------- */
.kb-side {
  width: 300px;
  flex: 0 0 300px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 0;
}
.kb-side-actions { display: flex; gap: 10px; }
.kb-side-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 0;
  overflow-y: auto;
}
.kb-card {
  background: #fff;
  border: 1px solid var(--kb-border);
  border-radius: 16px;
  padding: 12px;
}
.kb-muted-pad { padding: 18px; color: var(--kb-muted); font-size: 13px; }
.kb-eyebrow {
  padding: 6px 10px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .04em;
  color: var(--kb-muted);
}
.kb-filters { display: flex; flex-direction: column; gap: 4px; }
.kb-filter {
  height: 40px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 12px;
  background: transparent;
  border: 0;
  border-radius: 10px;
  color: var(--kb-text);
  font: inherit;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  transition: background .15s;
}
.kb-filter:hover { background: var(--kb-soft); }
.kb-filter-label { flex: 1 1 auto; }
.kb-filter-count { font-size: 13px; font-weight: 500; color: var(--kb-muted); }
.kb-filter.is-active { background: var(--kb-mint); }
.kb-filter.is-active .kb-filter-label { font-weight: 600; color: var(--kb-green); }
.kb-filter.is-active .kb-filter-count { color: var(--kb-green); }
.kb-dot {
  width: 8px;
  height: 8px;
  flex: 0 0 8px;
  border-radius: 4px;
}

.kb-files { display: flex; flex-direction: column; gap: 2px; }
.kb-files-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-right: 10px;
}
.kb-files-meta { font-size: 11px; color: var(--kb-muted); }
.kb-files-empty { padding: 12px 10px 8px; font-size: 13px; color: var(--kb-muted); line-height: 1.5; }
.kb-source-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 10px;
}
.kb-source-row:hover { background: var(--kb-soft); }
.kb-source-row .kb-dot { margin-top: 6px; }
.kb-source-main { flex: 1 1 auto; min-width: 0; }
.kb-source-title {
  font-size: 13px;
  font-weight: 500;
  color: var(--kb-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.kb-source-meta { margin-top: 2px; font-size: 11px; color: var(--kb-muted); }
.kb-status--indexed { color: var(--kb-green); }
.kb-status--failed { color: var(--kb-danger); }
.kb-status--processing { color: #b7791f; }
.kb-source-error { margin-top: 3px; font-size: 11px; color: var(--kb-danger); word-break: break-word; }
.kb-row-delete {
  width: 28px;
  height: 28px;
  flex: 0 0 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 0;
  border-radius: 8px;
  color: var(--kb-muted);
  font-size: 16px;
  cursor: pointer;
  opacity: 0;
  transition: opacity .15s, background .15s, color .15s;
}
.kb-source-row:hover .kb-row-delete,
.kb-row-delete:focus-visible { opacity: 1; }
.kb-row-delete:hover { background: #fbeceb; color: var(--kb-danger); }

/* ---------- chat panel ---------- */
.kb-chat {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 480px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px;
  background: #fff;
  border: 1px solid var(--kb-border);
  border-radius: 12px;
}
.kb-chat-messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.kb-chat-empty {
  margin: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
  padding: 40px 0;
}
.kb-chat-empty-title { font-size: 15px; font-weight: 600; color: var(--kb-text); }
.kb-chat-empty-sub { font-size: 13px; color: var(--kb-muted); }

.kb-msg-user {
  display: flex;
  justify-content: flex-end;
  align-items: flex-end;
  gap: 8px;
}
.kb-msg-bot {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}
.kb-msg-bot > .kb-answer { flex: 1 1 auto; min-width: 0; }

/* avatars */
.kb-avatar {
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid var(--kb-border);
  background: #fff;
}
.kb-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.kb-avatar--initials {
  background: var(--kb-green);
  border-color: var(--kb-green);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
}
.kb-avatar--bot { background: var(--kb-mint); border-color: var(--kb-mint); padding: 6px; }
.kb-avatar--bot img { object-fit: contain; }
.kb-msg-bot.is-typing .kb-avatar--bot { animation: kb-avatar-pulse 1.4s ease-in-out infinite; }
@keyframes kb-avatar-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(47, 107, 43, .35); }
  50% { box-shadow: 0 0 0 6px rgba(47, 107, 43, 0); }
}

/* light markdown inside answers */
.kb-md-p { margin: 0 0 8px; }
.kb-md-p:last-child { margin-bottom: 0; }
.kb-md-h { margin: 12px 0 6px; font-weight: 600; color: var(--kb-text); }
.kb-md-h:first-child { margin-top: 0; }
.kb-md-list { margin: 0 0 8px; padding-left: 20px; }
.kb-md-list li { margin: 2px 0; }
.kb-bubble-user {
  max-width: 75%;
  padding: 12px 20px;
  background: var(--kb-green);
  border-radius: 18px;
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  line-height: 1.45;
  word-break: break-word;
}

.kb-answer {
  display: flex;
  flex-direction: column;
  background: #fff;
}
.kb-answer--featured {
  gap: 14px;
  padding: 24px;
  border: 2px solid var(--kb-green);
  border-radius: 20px;
}
.kb-answer--compact {
  gap: 10px;
  padding: 18px 20px;
  border: 1px solid var(--kb-border);
  border-radius: 18px;
}
.kb-answer-head {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.kb-answer-head--compact { gap: 8px; }
.kb-answer-icon {
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--kb-mint);
  border-radius: 999px;
  color: var(--kb-green);
  font-size: 18px;
}
.kb-answer-icon--sm { width: 26px; height: 26px; flex-basis: 26px; font-size: 13px; }
.kb-answer-title { font-size: 14px; font-weight: 600; color: var(--kb-green); }
.kb-answer-title--sm { font-size: 13px; }
.kb-grow { flex: 1 1 auto; }
.kb-pill {
  height: 26px;
  display: inline-flex;
  align-items: center;
  padding: 0 12px;
  background: var(--kb-pill-bg);
  border-radius: 999px;
  color: var(--kb-pill-text);
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}
.kb-pill--sm { height: 22px; padding: 0 10px; font-size: 11px; }
.kb-answer-text {
  font-size: 15px;
  line-height: 24px;
  color: var(--kb-body);
  word-break: break-word;
}
.kb-answer-text--sm { font-size: 14px; line-height: 21px; }
.kb-source-box {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  background: var(--kb-soft);
  border-radius: 14px;
}
.kb-source-line { display: flex; align-items: center; gap: 10px; min-width: 0; }
.kb-source-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--kb-text);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.kb-cite {
  width: 22px;
  height: 20px;
  flex: 0 0 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--kb-mint);
  border-radius: 999px;
  color: var(--kb-green);
  font-size: 11px;
  font-weight: 700;
  cursor: default;
}
.kb-feedback { display: flex; gap: 10px; flex-wrap: wrap; }
.kb-warn {
  padding: 10px 12px;
  border-radius: 10px;
  background: #fdf5e6;
  color: #9a6a10;
  font-size: 12px;
}
.kb-error-text { color: var(--kb-danger); font-size: 13px; }
.kb-debug summary { cursor: pointer; color: var(--kb-muted); font-size: 11px; }
.kb-debug-body { margin-top: 8px; font-size: 10px; color: var(--kb-muted); }
.kb-debug-row { padding: 6px 0; border-bottom: 1px solid var(--kb-border); }

.kb-thinking {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: var(--kb-muted);
}
.kb-thinking span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--kb-green);
  opacity: .3;
  animation: kb-blink 1.2s infinite;
}
.kb-thinking span:nth-child(2) { animation-delay: .2s; }
.kb-thinking span:nth-child(3) { animation-delay: .4s; margin-right: 6px; }
@keyframes kb-blink { 0%, 80%, 100% { opacity: .3; } 40% { opacity: 1; } }

/* ---------- composer ---------- */
.kb-composer {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  height: 56px;
  padding: 0 10px 0 18px;
  background: #fff;
  border: 1.5px solid var(--kb-border);
  border-radius: 16px;
  transition: border-color .15s;
}
.kb-composer:focus-within { border-color: var(--kb-green); }
.kb-composer-input {
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--kb-text);
  font: inherit;
  font-size: 15px;
}
.kb-composer-input::placeholder { color: var(--kb-placeholder); }
.kb-send {
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--kb-green);
  border: 0;
  border-radius: 999px;
  color: #fff;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
}
.kb-send:hover { background: var(--kb-green-hover); }
.kb-send:disabled { opacity: .5; cursor: default; }

/* ---------- files panel (default view) ---------- */
.kb-files-panel,
.kb-chat { flex: 1 1 auto; min-width: 0; }
.kb-files-panel[hidden],
.kb-chat[hidden] { display: none !important; }
.kb-files-panel {
  align-self: flex-start;
  min-height: 480px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  padding: 20px;
  background: #fff;
  border: 1px solid var(--kb-border);
  border-radius: 16px;
  max-height: 100%;
  overflow-y: auto;
}
.kb-files-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.kb-files-title-wrap { display: flex; align-items: center; gap: 16px; min-width: 0; }
.kb-files-icon {
  width: 56px;
  height: 56px;
  flex: 0 0 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 14px;
  font-size: 26px;
}
.kb-files-title { margin: 0; font-size: 24px; font-weight: 700; color: var(--kb-text); }
.kb-files-sub { margin-top: 2px; font-size: 14px; color: var(--kb-muted); }
.kb-files-empty-big {
  padding: 60px 20px;
  text-align: center;
  font-size: 14px;
  color: var(--kb-muted);
}
.kb-file-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 24px;
}
.kb-file-card {
  display: flex;
  flex-direction: column;
  background: #fff;
  border: 1px solid var(--kb-border);
  border-radius: 16px;
  overflow: hidden;
  transition: box-shadow .15s;
}
.kb-file-card:hover { box-shadow: 0 6px 18px rgba(27, 23, 34, .06); }
.kb-file-thumb {
  position: relative;
  height: 176px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-top: 14px;
}
.kb-file-badge {
  position: absolute;
  top: 16px;
  left: 16px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  padding: 0 11px;
  border-radius: 7px;
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .02em;
}
.kb-file-paper {
  width: 122px;
  height: 150px;
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 16px 14px;
  background: #fff;
  border: 1px solid var(--kb-border);
  border-radius: 8px;
}
.kb-file-paper span {
  display: block;
  height: 5px;
  width: 100%;
  border-radius: 3px;
  background: #ebe8f1;
}
.kb-file-body {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1 1 auto;
  padding: 18px 22px 22px;
  border-top: 1px solid var(--kb-border);
}
.kb-file-name {
  font-size: 16px;
  font-weight: 600;
  color: var(--kb-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.kb-file-meta {
  font-size: 14px;
  color: var(--kb-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.kb-file-status { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.kb-chip {
  height: 24px;
  display: inline-flex;
  align-items: center;
  padding: 0 10px;
  background: var(--kb-pill-bg);
  border-radius: 6px;
  color: var(--kb-text);
  font-size: 12px;
  font-weight: 500;
}
.kb-chip--busy { background: #fdf5e6; color: #9a6a10; }
.kb-chip--failed { background: #fbeceb; color: var(--kb-danger); }
.kb-file-updated { font-size: 14px; color: var(--kb-muted); }
.kb-file-body .kb-source-error {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.kb-file-actions { display: flex; gap: 12px; margin-top: auto; padding-top: 12px; }
.kb-file-actions .kb-btn { flex: 1 1 0; height: 44px; font-size: 15px; }
.kb-btn-danger { color: var(--kb-danger); border-color: #f0cfcc; }
.kb-btn-danger:hover { background: #fbeceb; }

/* ---------- source details modal ---------- */
.kb-modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(27, 23, 34, .45);
  font-family: 'Geist', 'Inter', system-ui, sans-serif;
}
.kb-modal {
  width: 480px;
  max-width: 100%;
  background: #fff;
  border: 1px solid #e3dfec;
  border-radius: 16px;
  color: #1b1722;
  overflow: hidden;
}
.kb-modal-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 16px 16px 20px;
  border-bottom: 1px solid #e3dfec;
}
.kb-modal-head .kb-dot { width: 8px; height: 8px; flex: 0 0 8px; border-radius: 4px; }
.kb-modal-head h3 {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.kb-icon-btn--sm {
  width: 32px; height: 32px; flex: 0 0 32px;
  display: flex; align-items: center; justify-content: center;
  background: #fff; border: 1px solid #e3dfec; border-radius: 8px;
  font-size: 16px; cursor: pointer;
}
.kb-modal-body { display: flex; flex-direction: column; padding: 8px 20px 18px; }
.kb-view-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 0;
  border-bottom: 1px solid #f0eef5;
  font-size: 14px;
}
.kb-view-row span:first-child { color: #6b6580; }
.kb-view-row span:last-child { text-align: right; word-break: break-all; }
.kb-view-row a { color: #2d63e8; }
.kb-modal .kb-source-error { margin-top: 10px; font-size: 12px; color: #c2362e; }

/* ---------- small screens ---------- */
@media (max-width: 900px) {
  .kb-wrap { padding: 20px 16px 24px; height: auto; min-height: 100%; }
  .kb-title { font-size: 24px; }
  .kb-body { flex-direction: column; }
  .kb-side { width: 100%; flex-basis: auto; }
  .kb-chat { min-height: 520px; }
  .kb-files-panel { align-self: stretch; max-height: none; }
  .kb-files-top { flex-wrap: wrap; }
  .kb-bubble-user { max-width: 90%; }
}
