:root {
  --accent: #0A66FF;
  --bg: #f4f6fb;
  --panel-bg: #ffffff;
  --border: #e2e6ee;
  --text: #1c2333;
  --text-muted: #6b7280;
  --danger: #d92d20;
  --success: #12805c;
  --warning: #b7791f;
  --radius: 12px;
  --sidebar-w: 264px;
  --split-w: 420px;
  --sidebar-bg: #12172b;
  --sidebar-bg-hover: #1b2140;
  --sidebar-bg-active: #23294a;
  --sidebar-text: #cfd6e6;
  --sidebar-text-muted: #838ba8;
  --sidebar-border: #232a44;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }

/* ---------- Icon set (see svgIcon()/ICON_PATHS in app.js) ----------
   Every icon is a 1em x 1em outline SVG that inherits the surrounding
   text/button color and font-size, so it drops into any existing button,
   nav item or label without per-usage sizing. */
.ico { display: inline-block; vertical-align: -0.15em; flex-shrink: 0; }
.ico-inline { margin-right: 5px; }
.ico-after { margin-left: 5px; margin-right: 0; }
.ico-correct { color: var(--success); }
.ico-wrong { color: var(--danger); }

/* ---------- Login screen (Round 9) ---------- */
#login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  background: var(--panel-bg);
  padding: 40px;
  border-radius: 16px;
  box-shadow: 0 12px 40px rgba(20, 30, 60, 0.08);
  width: 340px;
}
.login-card h1 { font-size: 20px; margin: 0 0 6px; }
.login-card p.subtitle { color: var(--text-muted); margin: 0 0 20px; font-size: 13px; }
.login-logo { display: block; width: 72px; height: auto; margin: 0 auto 14px; border-radius: 16px; }
.login-card input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
  margin-bottom: 10px; font-size: 14px; box-sizing: border-box;
}
.login-card button {
  width: 100%; padding: 10px; border: none; background: var(--accent); color: #fff;
  border-radius: 8px; font-weight: 600; cursor: pointer; font-size: 14px;
}
.login-error { color: var(--danger); font-size: 13px; margin-bottom: 8px; min-height: 16px; }

/* ---------- Role picker ---------- */
#role-picker {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.role-card {
  background: var(--panel-bg);
  border-radius: 20px;
  box-shadow: 0 12px 40px rgba(20, 30, 60, 0.08);
  padding: 40px;
  max-width: 620px;
  width: 100%;
}

.role-card h1 { font-size: 22px; margin: 0 0 6px; }
.role-card p.subtitle { color: var(--text-muted); margin: 0 0 28px; font-size: 14px; }

.role-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
}

.role-option {
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 12px;
  text-align: center;
  cursor: pointer;
  background: #fbfcfe;
  transition: all .15s ease;
}
.role-option:hover { border-color: var(--accent); transform: translateY(-2px); }
.role-badge-preview {
  width: 40px; height: 40px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: white; font-weight: 700; font-size: 13px;
  margin: 0 auto 10px;
}
.role-option .role-name { font-weight: 600; font-size: 14px; }

/* ---------- App shell: left sidebar + main area ---------- */
#app-shell { display: none; height: 100vh; }
#app-shell.active { display: flex; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-w); flex: 0 0 auto; background: var(--sidebar-bg); color: var(--sidebar-text);
  display: flex; flex-direction: column; overflow: hidden;
}

.sidebar-identity {
  display: flex; align-items: center; gap: 10px; padding: 16px 14px;
  border-bottom: 1px solid var(--sidebar-border); flex: 0 0 auto;
}
.agent-badge {
  width: 36px; height: 36px; border-radius: 10px; flex: 0 0 auto;
  object-fit: cover;
}
.agent-id-text { min-width: 0; flex: 1; }
.agent-name { font-weight: 700; font-size: 14px; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.scope-pill {
  display: inline-block; margin-top: 2px; font-size: 10.5px; padding: 1px 7px; border-radius: 999px;
  background: rgba(255,255,255,0.08); color: var(--sidebar-text); font-weight: 600;
}

.icon-btn {
  border: 1px solid var(--sidebar-border); background: transparent; color: var(--sidebar-text);
  width: 28px; height: 28px; border-radius: 8px; cursor: pointer; font-size: 13px; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center; transition: background .15s ease;
}
.icon-btn:hover { background: var(--sidebar-bg-hover); }

.btn-new-chat {
  margin: 12px 14px; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--sidebar-border);
  background: rgba(255,255,255,0.04); color: #fff; font-weight: 600; font-size: 13px; cursor: pointer;
  display: flex; align-items: center; gap: 6px; justify-content: center; flex: 0 0 auto;
  transition: background .15s ease, border-color .15s ease;
}
.btn-new-chat:hover { background: var(--sidebar-bg-hover); border-color: var(--accent); }
.btn-new-chat .plus { font-size: 15px; line-height: 1; }

.sidebar-nav { flex: 0 0 auto; padding: 4px 8px 10px; display: flex; flex-direction: column; gap: 2px; }
.sidebar-nav .nav-item {
  display: flex; align-items: center; gap: 9px; width: 100%; text-align: left; background: none; border: none;
  color: var(--sidebar-text); padding: 9px 10px; border-radius: 8px; font-size: 13px; cursor: pointer; font-weight: 500;
}
.nav-ico { font-size: 14px; }
.sidebar-nav .nav-item:hover { background: var(--sidebar-bg-hover); }
.sidebar-nav .nav-item.active { background: var(--sidebar-bg-active); color: #fff; }

.sidebar-history {
  flex: 1; min-height: 0; display: flex; flex-direction: column; border-top: 1px solid var(--sidebar-border);
  padding-top: 8px;
}
.history-label-row {
  display: flex; align-items: center; justify-content: space-between; padding: 4px 8px 4px 16px;
}
.history-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--sidebar-text-muted);
  font-weight: 700;
}
#btn-clear-all-history { width: 24px; height: 24px; font-size: 12px; }
.history-groups { flex: 1; overflow-y: auto; padding: 0 8px 12px; }
.history-empty { color: var(--sidebar-text-muted); font-size: 12px; padding: 8px; }

.history-group { margin-bottom: 6px; }
.history-group-label {
  font-size: 10.5px; color: var(--sidebar-text-muted); padding: 8px 8px 2px; font-weight: 600;
}
.history-item-nav {
  padding: 7px 10px; border-radius: 8px; cursor: pointer; font-size: 12.5px; color: var(--sidebar-text);
  display: flex; align-items: baseline; gap: 6px; margin-bottom: 1px;
}
.history-item-nav:hover { background: var(--sidebar-bg-hover); }
.history-item-nav.active { background: var(--sidebar-bg-active); color: #fff; }
.history-item-text {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0;
  display: block;
}
.history-item-delete {
  flex: 0 0 auto; border: none; background: transparent; color: inherit; cursor: pointer;
  font-size: 12px; opacity: 0; padding: 0 2px; line-height: 1; border-radius: 4px;
  transition: opacity .1s ease, background .1s ease;
}
.history-item-nav:hover .history-item-delete { opacity: .7; }
.history-item-delete:hover { opacity: 1 !important; background: rgba(255,255,255,0.15); }

/* ---------- Main area ---------- */
.main-area { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.tab-panel { flex: 1; overflow: hidden; display: none; }
.tab-panel.active { display: flex; flex-direction: column; height: 100%; }

/* ---------- Chat + Split-view (Source panel) ---------- */
#chat-panel { flex-direction: row; }

.chat-col {
  flex: 1 1 auto; display: flex; flex-direction: column; min-width: 320px; background: var(--bg);
}

/* Split view is closed (width 0) by default; opens on demand with a soft
   slide/fade animation and can be closed again to return to full-width chat. */
.doc-col {
  flex: 0 0 auto; width: 0; min-width: 0; overflow: hidden; display: flex; flex-direction: column;
  background: #fbfcfe; border-left: 0px solid var(--border); opacity: 0;
  transition: width .28s cubic-bezier(.4,0,.2,1), opacity .2s ease, border-color .28s ease;
}
.doc-col.open {
  width: var(--split-w); min-width: 320px; opacity: 1; border-left: 1px solid var(--border);
}

.messages { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 14px; }

/* ---------- Chat landing page (shown before the first message) ---------- */
.chat-landing { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px; }
.landing-inner { max-width: 640px; width: 100%; text-align: center; }
.landing-badge {
  width: 52px; height: 52px; border-radius: 50%; color: #fff; font-weight: 700; font-size: 16px;
  display: flex; align-items: center; justify-content: center; margin: 0 auto 16px;
}
.landing-inner h2 { font-size: 20px; margin: 0 0 8px; }
.landing-sub { color: var(--text-muted); font-size: 13.5px; margin: 0 0 22px; }
.prompt-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; text-align: left;
}
.prompt-card {
  border: 1px solid var(--border); background: #fff; border-radius: var(--radius); padding: 14px 16px;
  font-size: 13px; color: var(--text); cursor: pointer; font-family: inherit; text-align: left;
  transition: border-color .15s ease, transform .15s ease, box-shadow .15s ease;
}
.prompt-card:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: 0 6px 16px rgba(20,30,60,.06); }

.msg { max-width: 88%; }
.msg.user { align-self: flex-end; }
.msg.agent { align-self: flex-start; }
.msg-bubble {
  padding: 12px 14px; border-radius: 14px; font-size: 14px; line-height: 1.55; white-space: pre-wrap;
}
.msg.user .msg-bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg.agent .msg-bubble { background: #fff; border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.agent.fallback .msg-bubble { background: #fff7ed; border-color: #fed7aa; }
.msg.agent.conflict .msg-bubble { background: #fffbeb; border-color: #fde68a; }

.citations { margin-top: 8px; display: flex; gap: 6px; flex-wrap: wrap; }
.citation-tag {
  font-size: 11px; padding: 3px 8px; border-radius: 999px; background: #eef2ff; color: #3730a3;
  cursor: pointer; border: 1px solid #c7d2fe; font-weight: 600;
}
.citation-tag:hover { background: #e0e7ff; }

/* Inline citation markers (see renderAnswerHtml in app.js): replaces the raw
   [D11V1C3]-style anchor code an LLM-synthesized answer embeds mid-sentence
   with a small clickable footnote-style number, so the internal id is never
   shown to the user directly. */
.cite-marker {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 15px; height: 15px; padding: 0 4px; margin: 0 1px;
  font: 700 10px/1 inherit; background: #eef2ff; color: #3730a3; border: 1px solid #c7d2fe;
  border-radius: 999px; cursor: pointer; vertical-align: super;
}
.cite-marker:hover { background: #e0e7ff; }

.feedback-row { margin-top: 8px; display: flex; gap: 6px; align-items: center; }
.feedback-row button {
  border: 1px solid var(--border); background: #fff; border-radius: 6px; font-size: 11px; padding: 4px 8px; cursor: pointer;
}
.feedback-row button.active-helpful { background: #ecfdf5; border-color: #6ee7b7; color: #065f46; }
.feedback-row button.active-wrong { background: #fef2f2; border-color: #fca5a5; color: #991b1b; }
.feedback-row button.active-missing { background: #fffbeb; border-color: #fde68a; color: #92400e; }

/* ---------- Follow-up suggestions (shown after each agent answer) ---------- */
.followup-row { margin-top: 10px; }
.followup-label { font-size: 11px; color: var(--text-muted); margin-bottom: 6px; }
.followup-chips { display: flex; gap: 6px; flex-wrap: wrap; }
.followup-chip {
  border: 1px solid #c7d2fe; background: #fff; color: #3730a3; border-radius: 999px; padding: 6px 12px;
  font-size: 12px; cursor: pointer; font-family: inherit; transition: background .15s ease, border-color .15s ease;
}
.followup-chip:hover { background: #eef2ff; border-color: var(--accent); }

.composer { border-top: 1px solid var(--border); padding: 12px 16px; display: flex; gap: 10px; background: #fff; flex: 0 0 auto; }
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
  font-size: 14px; font-family: inherit; height: 44px;
}
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

button.btn {
  border: 1px solid var(--border); background: #fff; border-radius: 8px;
  padding: 8px 14px; font-size: 13px; cursor: pointer; font-weight: 600; color: var(--text);
}
button.btn:hover { background: #f2f4f8; }
button.btn:disabled { opacity: .5; cursor: not-allowed; filter: none; }
button.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.btn.primary:hover { filter: brightness(1.05); }
button.btn.small { padding: 5px 10px; font-size: 12px; }
button.btn.ghost { border-color: transparent; background: transparent; }

/* Send button while an answer is streaming in: a spinner ring around a
   small stop square, replacing the "Gửi" label -- clicking it aborts the
   in-flight request (see sendQuestion/setSendButtonStreaming in app.js). */
#btn-send.streaming {
  background: #fff; border-color: var(--border); color: var(--text);
  width: 40px; padding: 0; display: flex; align-items: center; justify-content: center;
}
#btn-send.streaming:hover { background: #f2f4f8; }
.stop-spinner { position: relative; width: 18px; height: 18px; display: inline-flex; align-items: center; justify-content: center; }
.stop-spinner::before {
  content: ""; position: absolute; inset: 0; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent);
  animation: stop-spinner-rotate .7s linear infinite;
}
.stop-spinner::after { content: ""; width: 7px; height: 7px; border-radius: 2px; background: var(--text); }
@keyframes stop-spinner-rotate { to { transform: rotate(360deg); } }

.stopped-note { font-size: 12px; color: var(--text-muted); font-style: italic; }

.doc-header {
  padding: 14px 18px; border-bottom: 1px solid var(--border); background: #fff;
  display: flex; align-items: flex-start; justify-content: space-between; gap: 10px;
}
.doc-header h3 { margin: 0 0 4px; font-size: 15px; }
.doc-header .meta { font-size: 11px; color: var(--text-muted); }
.doc-body { flex: 1; overflow-y: auto; padding: 18px; }
.doc-body .empty-state { color: var(--text-muted); font-size: 13px; text-align: center; margin-top: 60px; }

.doc-chunk {
  padding: 12px 14px; margin-bottom: 10px; border-radius: 10px; border: 1px solid transparent;
  font-size: 13.5px; line-height: 1.6; white-space: pre-wrap; scroll-margin-top: 20px;
}
.doc-chunk .heading-path { font-size: 11px; font-weight: 700; color: var(--accent); margin-bottom: 6px; }
.doc-chunk.highlight { background: #fff3cd; border-color: #ffe58f; animation: pulse-flash 1.6s ease; }

@keyframes pulse-flash {
  0% { background: #ffe58f; }
  100% { background: #fff3cd; }
}

/* ---------- Original document box (Section 7.1 / 7.2) ---------- */
.doc-original-box {
  background: #fff; border: 1px solid var(--border); border-radius: 12px; padding: 14px;
  margin-bottom: 16px; display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
}
.doc-original-box.muted-note {
  display: block; color: var(--text-muted); font-size: 12.5px; background: #fafbff; line-height: 1.6;
}
.doc-original-file { display: flex; align-items: center; gap: 10px; min-width: 0; }
.doc-original-icon { font-size: 26px; flex: 0 0 auto; }
.doc-original-info { min-width: 0; }
.doc-original-name { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.doc-original-type { font-size: 11px; color: var(--text-muted); }
.doc-original-actions { display: flex; gap: 8px; flex: 0 0 auto; }
.doc-chunks-title { font-size: 11px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: .03em; margin: 4px 0 10px; }

/* ---------- Inline PDF preview embedded directly in the split-view
   sidebar (Section 3.2) -- distinct from the full-screen viewer modal
   below, which stays available via "Xem toàn bộ tài liệu" for a larger
   look at the same file. ---------- */
.doc-inline-preview {
  height: 420px; border: 1px solid var(--border); border-radius: 12px; overflow: hidden;
  margin-bottom: 16px; background: #f4f6fb; display: flex; align-items: center; justify-content: center;
  color: var(--text-muted); font-size: 12.5px; text-align: center; padding: 12px;
}
.doc-inline-preview-iframe { width: 100%; height: 100%; border: none; display: block; }

/* ---------- Full document viewer modal (Section 7.2) ---------- */
.doc-viewer-modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; }
.doc-viewer-modal.hidden { display: none !important; }
.doc-viewer-backdrop { position: absolute; inset: 0; background: rgba(15, 20, 35, 0.55); }
.doc-viewer-dialog {
  position: relative; background: #fff; border-radius: 14px; width: min(920px, 92vw); height: min(85vh, 900px);
  display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 24px 60px rgba(10,15,30,.25);
}
.doc-viewer-header {
  display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.doc-viewer-title { font-weight: 700; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-viewer-actions { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }
.doc-viewer-body { flex: 1; min-height: 0; background: #f4f6fb; }
.doc-viewer-iframe { width: 100%; height: 100%; border: none; display: block; }
.doc-viewer-unsupported {
  height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  text-align: center; color: var(--text-muted); font-size: 13.5px; padding: 20px;
}
.doc-viewer-loading {
  height: 100%; display: flex; align-items: center; justify-content: center;
  color: var(--text-muted); font-size: 13.5px;
}

/* ---------- Access denied / Access Request (Round 4) ---------- */
.access-denied-box {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  text-align: center; color: var(--text-muted); font-size: 13.5px; padding: 40px 20px;
}
.access-denied-icon { font-size: 34px; }
.access-request-dialog {
  position: relative; background: #fff; border-radius: 14px; width: min(440px, 92vw);
  display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 24px 60px rgba(10,15,30,.25);
}
.access-request-body { padding: 16px 18px; }
.access-request-body textarea {
  width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font: inherit; resize: vertical;
}

/* ---------- Simple tables/lists shared across tabs ---------- */
.content-scroll { flex: 1; overflow-y: auto; padding: 20px; }
.card { background: #fff; border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; }
.card h4 { margin: 0 0 6px; font-size: 14px; }
.card .muted { color: var(--text-muted); font-size: 12px; }

.quiz-question { margin-bottom: 18px; }
.quiz-options label { display: block; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 6px; cursor: pointer; font-size: 13px; }
.quiz-options label:hover { background: #f8f9fc; }
.quiz-options input { margin-right: 8px; }

/* ---------- Quiz progress bar ---------- */
.quiz-progress-wrap { margin-bottom: 14px; }
.quiz-progress-label { font-size: 12px; font-weight: 700; color: var(--accent); margin-bottom: 6px; }
.quiz-progress-bar { height: 6px; border-radius: 999px; background: #e5e9f2; overflow: hidden; }
.quiz-progress-fill { height: 100%; background: var(--accent); border-radius: 999px; transition: width .25s ease; }

.quiz-nav-row { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }

/* ---------- Quiz result / summary screen ---------- */
.quiz-result-card {
  background: #fff; border: 1px solid var(--border); border-radius: 16px; padding: 28px 24px;
  text-align: center; margin-bottom: 20px;
}
.quiz-result-card.pass { border-color: #6ee7b7; background: #f0fdf6; }
.quiz-result-card.fail { border-color: #fde68a; background: #fffdf5; }
.quiz-result-emoji { font-size: 40px; line-height: 1; margin-bottom: 8px; }
.quiz-result-title { font-size: 13px; color: var(--text-muted); font-weight: 700; letter-spacing: .03em; text-transform: uppercase; }
.quiz-result-quizname { font-size: 16px; font-weight: 700; margin-top: 4px; }
.quiz-result-pct { font-size: 40px; font-weight: 800; margin: 10px 0 2px; }
.quiz-result-status { display: inline-block; font-size: 13px; font-weight: 700; padding: 4px 14px; border-radius: 999px; margin-bottom: 16px; }
.quiz-result-status.pass { background: #ecfdf5; color: #065f46; }
.quiz-result-status.fail { background: #fffbeb; color: #92400e; }
.quiz-result-stats { display: flex; justify-content: center; gap: 28px; margin-bottom: 14px; }
.quiz-result-stat { display: flex; flex-direction: column; align-items: center; }
.quiz-result-stat .num { font-size: 22px; font-weight: 800; }
.quiz-result-stat .lbl { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.quiz-result-meta { font-size: 12px; color: var(--text-muted); }
