:root {
  color-scheme: light;
  --bg: #F7F9FE;
  --surface: #FFFFFF;
  --bubble-user: #1B3FCC;
  --bubble-user-text: #FFFFFF;
  --bubble-agent: #F1F2F4;
  --text: #1A1A1A;
  --text-muted: #6B7280;
  --accent: #1B3FCC;
  --accent-soft: #E4E9FB;
  --border: #E3E6EE;
  --ok: #22C55E;
  --warn: #EF4444;
  --code-bg: #E9EBF0;
  --r-bubble: 16px;
  --r-input: 12px;
  --r-btn: 10px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0F1420;
  --surface: #161C2B;
  --bubble-user: #3355E0;
  --bubble-agent: #1E2536;
  --text: #ECEFF6;
  --text-muted: #9AA3B5;
  --accent: #8FA5FF;
  --accent-soft: #24304F;
  --border: #283047;
  --code-bg: #232B3D;
}

* { box-sizing: border-box; }
/* Các khối có display:flex (paste-hint, followups, bubble-footer) phải vẫn ẩn được bằng attribute hidden */
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}
button, textarea { font: inherit; color: inherit; }
.muted { color: var(--text-muted); }

/* ---------------------------------------------------------------- header */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-name { font-weight: 700; font-size: 17px; }
.badge {
  font-size: 12px; font-weight: 500;
  padding: 2px 8px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
}
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot-ok { background: var(--ok); }
.dot-bad { background: var(--warn); }
.dot-unknown { background: var(--text-muted); }
.dot-slow { background: #F59E0B; }
.actions { display: flex; gap: 8px; align-items: center; }

.btn {
  border: 1px solid var(--border); background: var(--surface);
  border-radius: var(--r-btn); padding: 6px 12px; cursor: pointer;
}
.btn:hover { background: var(--accent-soft); }
.btn:focus-visible, textarea:focus-visible, .cite:focus-visible, .suggestion:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.btn-icon { padding: 6px; display: inline-flex; }
.icon-moon { display: none; }
:root[data-theme="dark"] .icon-sun { display: none; }
:root[data-theme="dark"] .icon-moon { display: inline; }

/* ---------------------------------------------------------------- chat */
.chat {
  flex: 1;
  width: 100%; max-width: 900px; margin: 0 auto;
  padding: 20px 16px 8px;
  display: flex; flex-direction: column; gap: 14px;
}
.empty { margin: auto; text-align: center; max-width: 560px; padding: 40px 0; }
.empty h1 { font-size: 22px; font-weight: 600; margin: 0 0 6px; }
.suggestions { display: flex; flex-direction: column; gap: 8px; margin-top: 20px; }
.suggestion {
  text-align: left; padding: 10px 14px; border-radius: var(--r-input);
  border: 1px solid var(--border); background: var(--surface); cursor: pointer;
}
.suggestion:hover { border-color: var(--accent); }

.msg { display: flex; }
.msg-user { justify-content: flex-end; }
.bubble { padding: 10px 14px; border-radius: var(--r-bubble); word-wrap: break-word; overflow-wrap: anywhere; }
.msg-user .bubble {
  background: var(--bubble-user); color: var(--bubble-user-text);
  max-width: 60%; white-space: pre-wrap;
  border-bottom-right-radius: 6px;
}
.msg-agent .bubble {
  background: var(--bubble-agent); max-width: min(72ch, 100%);
  border-bottom-left-radius: 6px;
}

.retrieving { font-size: 14px; color: var(--text-muted); font-weight: 600; display: flex; align-items: center; }
#input:disabled { opacity: 0.7; cursor: wait; }
.btn-send .icon-stop { display: none; }
.btn-send.waiting .icon-send { display: none; }
.btn-send.waiting .icon-stop { display: inline; }
.btn-send.waiting { background: var(--warn); border-color: var(--warn); }
.stopped { font-size: 13px; color: var(--text-muted); font-style: italic; margin-top: 6px; }

.answer > :first-child { margin-top: 0; }
.answer > :last-child { margin-bottom: 0; }
.answer h1, .answer h2, .answer h3, .answer h4 { color: var(--accent); font-size: 1em; font-weight: 700; margin: 12px 0 4px; }
.answer p { margin: 6px 0; }
.answer ul, .answer ol { margin: 6px 0; padding-left: 22px; }
.answer li { margin: 2px 0; }
.answer code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; font-size: 0.92em; }
.answer pre { background: var(--code-bg); padding: 10px; border-radius: 8px; overflow-x: auto; }
.answer table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; }
.answer th, .answer td { border: 1px solid var(--border); padding: 4px 8px; }
.answer th { white-space: nowrap; } /* "Bước" không bị bẻ thành "Bư / ớc" */
.answer a { color: var(--accent); }

.cite {
  display: inline-block; font-size: 12px; font-weight: 600; line-height: 1.3;
  padding: 1px 7px; margin: 0 1px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
  border: 1px solid transparent; cursor: pointer; vertical-align: baseline;
}
.cite:hover { border-color: var(--accent); }

.spinner {
  display: inline-block; width: 14px; height: 14px; margin-right: 8px; vertical-align: -2px;
  border: 2px solid var(--accent-soft); border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ba chấm lượn sóng ở đuôi câu trả lời đang stream */
.typing { display: inline-flex; gap: 3px; margin-left: 6px; vertical-align: middle; }
.typing i { width: 5px; height: 5px; border-radius: 50%; background: var(--accent); opacity: 0.4; animation: wave 1.2s ease-in-out infinite; }
.typing i:nth-child(2) { animation-delay: 0.15s; }
.typing i:nth-child(3) { animation-delay: 0.3s; }
@keyframes wave { 0%, 60%, 100% { transform: translateY(0); opacity: 0.4; } 30% { transform: translateY(-4px); opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .typing i { animation: none; opacity: 0.7; } }
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; border-top-color: var(--accent-soft); border-color: var(--accent); } }

.sources { margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--border); font-size: 14px; }
.sources-title { font-weight: 600; color: var(--text-muted); margin-bottom: 4px; cursor: pointer; list-style: none; display: flex; align-items: center; gap: 6px; }
.sources-title::-webkit-details-marker { display: none; }
.sources-title::before { content: "▸"; color: var(--accent); font-size: 12px; transition: transform 0.15s; }
.sources-details[open] .sources-title::before { transform: rotate(90deg); }
.sources-title:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.source { padding: 3px 6px; border-radius: 6px; display: flex; gap: 8px; align-items: baseline; }
.source .sfile { font-weight: 600; color: var(--accent); text-decoration: none; }
.source a.sfile:hover { text-decoration: underline; }
.source .sdoc { color: var(--text-muted); font-size: 12px; white-space: nowrap; }
.source.hl { background: var(--accent-soft); }

.wait-elapsed { margin-left: 8px; font-weight: 400; font-size: 12px; opacity: 0.7; font-variant-numeric: tabular-nums; }

.followups { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.followups-title { font-size: 12px; font-weight: 600; color: var(--text-muted); margin-right: 2px; }
.chip-q {
  font-size: 13px; padding: 4px 10px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.chip-q:hover { border-color: var(--accent); color: var(--accent); }
.chip-q:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.bubble-footer { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.btn-copy {
  font-size: 12px; padding: 3px 10px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border); background: transparent; color: var(--text-muted);
}
.btn-copy:hover { color: var(--accent); border-color: var(--accent); }
.meta { font-size: 12px; color: var(--text-muted); }

.error { border-left: 3px solid var(--warn); padding-left: 10px; }
.error strong { color: var(--warn); }

/* ---------------------------------------------------------------- composer */
.composer {
  position: sticky; bottom: 0;
  background: var(--bg);
  padding: 8px 16px 12px;
  border-top: 1px solid var(--border);
}
.composer-inner {
  max-width: 900px; margin: 0 auto;
  display: flex; gap: 8px; align-items: flex-end;
}
#input {
  flex: 1; resize: none; min-height: 44px; max-height: calc(1.6em * 6 + 20px);
  padding: 10px 14px; border-radius: var(--r-input);
  border: 1px solid var(--border); background: var(--surface);
  overflow-y: hidden; /* JS bật auto khi vượt 6 dòng */
  scrollbar-width: thin; scrollbar-color: var(--border) transparent;
}
#input.scroll { overflow-y: auto; }
#input:focus { border-color: var(--accent); outline: none; }
.btn-send {
  width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent); color: #fff; border-color: var(--accent);
}
.btn-send:hover { filter: brightness(1.08); background: var(--accent); }
.btn-send:disabled { opacity: 0.5; cursor: not-allowed; }
.hint { max-width: 900px; margin: 6px auto 0; font-size: 12px; }

@media (max-width: 600px) {
  .msg-user .bubble { max-width: 85%; }
  .btn-ghost { font-size: 14px; padding: 6px 10px; }
  .hint { display: none; }
  .brand-name { font-size: 16px; }
}

/* ---------------------------------------------------------------- gập section */
.answer details { margin: 6px 0; }
.answer summary.sec-summary { list-style: none; cursor: pointer; display: flex; align-items: center; gap: 6px; }
.answer summary.sec-summary::-webkit-details-marker { display: none; }
.answer summary.sec-summary::before { content: "▸"; color: var(--accent); font-size: 12px; transition: transform 0.15s; }
.answer details[open] summary.sec-summary::before { transform: rotate(90deg); }
.answer summary.sec-summary h3 { margin: 0; }
.answer summary.sec-summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.answer .sec-body > :first-child { margin-top: 4px; }
@media (prefers-reduced-motion: reduce) { .answer summary.sec-summary::before { transition: none; } }

/* ---------------------------------------------------------------- popover mục trích dẫn */
.section-dialog {
  border: 1px solid var(--border); border-radius: var(--r-bubble); padding: 16px 18px;
  width: min(720px, 92vw); max-height: 80vh; background: var(--surface); color: var(--text);
}
.section-dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
.dlg-head { display: flex; align-items: center; gap: 10px; }
.dlg-title { font-size: 16px; font-weight: 600; margin: 0; flex: 1; }
.dlg-file { display: inline-block; font-size: 12px; margin: 4px 0 10px; color: var(--accent); }
.dlg-file[aria-disabled="true"] { color: var(--text-muted); pointer-events: none; }
.dlg-body { font-size: 15px; max-height: 60vh; overflow: auto; }

/* ---------------------------------------------------------------- feedback */
.feedback { display: inline-flex; align-items: center; gap: 4px; }
.btn-vote { border: 1px solid transparent; background: transparent; cursor: pointer; font-size: 14px; padding: 2px 6px; border-radius: 999px; opacity: 0.7; }
.btn-vote:hover { opacity: 1; border-color: var(--border); background: var(--surface); }
.vote-done { font-size: 14px; }
.vote-thanks { font-size: 12px; color: var(--text-muted); }
.vote-comment { font-size: 13px; padding: 3px 8px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); width: 220px; }
.vote-comment:focus { border-color: var(--accent); outline: none; }

/* ---------------------------------------------------------------- gợi ý khi dán đoạn dài */
.paste-hint {
  max-width: 900px; margin: 0 auto 8px; padding: 8px 12px; border-radius: var(--r-input);
  background: var(--accent-soft); color: var(--text); font-size: 13px;
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
}
.paste-hint .chip-q { background: var(--surface); }
.paste-hint .btn-copy { margin-left: auto; }

/* ---------------------------------------------------------------- mobile */
@media (max-width: 600px) {
  .chat { padding: 12px 10px 8px; }
  .msg-agent .bubble { max-width: 100%; padding: 10px 12px; }
  .answer table { font-size: 14px; }
  .answer th, .answer td { padding: 4px 6px; }
  .bubble-footer { flex-wrap: wrap; row-gap: 6px; }
  .bubble-footer .meta { width: 100%; }
  .vote-comment { width: 100%; }
  .section-dialog { width: 96vw; padding: 12px; max-height: 88vh; }
  .dlg-body { max-height: 70vh; }
  .composer { padding: 8px 10px 10px; }
}

/* ---------------------------------------------------------------- cổng truy cập */
.gate {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
  background: var(--bg); padding: 16px;
}
.gate-card {
  width: min(420px, 100%); background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-bubble); padding: 24px; display: flex; flex-direction: column; gap: 6px;
}
.gate-title { font-size: 20px; font-weight: 700; margin: 0 0 4px; }
.gate-label { font-size: 13px; font-weight: 600; margin-top: 10px; }
.gate-input {
  padding: 10px 12px; border-radius: var(--r-input); border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 15px;
}
.gate-input:focus { border-color: var(--accent); outline: none; }
.gate-error { color: var(--warn); font-size: 13px; margin: 6px 0 0; }
.gate-submit { width: 100%; height: 44px; margin-top: 14px; font-weight: 600; }
.visitor { font-size: 13px; max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 600px) { .visitor { display: none; } }
