:root {
  --green: #20B759; --green-dark: #0E7A3A; --green-bg: #ECFBF2;
  --blue: #285CE1; --ink: #111827; --ink-2: #374151; --muted: #6B7280;
  --line: #E5E7EB; --bg: #F6F7F9; --card: #FFFFFF;
  --warn-bg: #FFF7E6; --warn: #A15C07;
  --top: 60px; --chat-w: 430px; --side-w: 290px;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; color: var(--ink); background: var(--bg); line-height: 1.6;
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--blue); }
button { font: inherit; cursor: pointer; }
.muted { color: var(--muted); }
.only-mobile { display: none !important; }

/* ---------- Thanh trên ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20; height: var(--top);
  display: flex; align-items: center; gap: 16px; padding: 0 20px;
  background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; white-space: nowrap; }
.brand img { height: 28px; }
.brand span { font-size: 15px; color: var(--ink-2); padding-left: 10px; border-left: 1px solid var(--line); }
.search {
  flex: 1; max-width: 560px; margin: 0 auto; display: flex; align-items: center; gap: 8px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 0 12px; color: var(--muted);
}
.search:focus-within { border-color: var(--green); background: #fff; box-shadow: 0 0 0 3px rgba(32,183,89,.15); }
.search input { flex: 1; border: 0; outline: 0; background: transparent; height: 38px; font: inherit; font-size: 14.5px; color: var(--ink); }
.contact { font-size: 14px; color: var(--ink-2); text-decoration: none; white-space: nowrap; }
.contact:hover { color: var(--green-dark); }
.icon-btn { border: 0; background: none; padding: 6px; color: var(--ink-2); }

/* ---------- Bố cục 3 cột ---------- */
.layout { display: grid; grid-template-columns: var(--side-w) minmax(0, 1fr) var(--chat-w); height: calc(100vh - var(--top)); }
.sidebar { overflow-y: auto; border-right: 1px solid var(--line); background: var(--card); padding: 14px 10px 40px; }
.content { overflow-y: auto; padding: 28px 40px 80px; outline: none; }
.chat { display: flex; flex-direction: column; min-height: 0; border-left: 1px solid var(--line); background: var(--card); }

/* ---------- Mục lục ---------- */
.toc-group { margin-bottom: 10px; }
.toc-group-title { font-size: 11.5px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); font-weight: 600; padding: 8px 10px 4px; }
.toc a {
  display: block; padding: 6px 10px; border-radius: 8px; color: var(--ink-2); text-decoration: none; font-size: 14px; line-height: 1.4;
}
.toc a:hover { background: var(--bg); }
.toc a.active { background: var(--green-bg); color: var(--green-dark); font-weight: 600; }
.search-hit { display: block; padding: 10px; border-radius: 8px; text-decoration: none; color: var(--ink-2); }
.search-hit:hover { background: var(--bg); }
.search-hit .h { font-weight: 600; font-size: 14px; color: var(--ink); }
.search-hit .d { font-size: 12px; color: var(--green-dark); }
.search-hit .s { font-size: 13px; color: var(--muted); margin-top: 2px; }
.search-empty { padding: 12px 10px; font-size: 14px; color: var(--muted); }

/* ---------- Nội dung tài liệu ---------- */
.doc { max-width: 860px; margin: 0 auto; }
.doc .crumb { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.doc h1 { font-size: 28px; line-height: 1.25; margin: 0 0 4px; }
.doc .meta { font-size: 13px; color: var(--muted); display: flex; gap: 14px; flex-wrap: wrap; align-items: center; margin-bottom: 18px; }
.doc .ask-doc { border: 1px solid var(--green); color: var(--green-dark); background: #fff; border-radius: 999px; padding: 3px 12px; font-size: 13px; }
.doc .ask-doc:hover { background: var(--green-bg); }
.doc .doc-toc { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 18px; margin-bottom: 24px; font-size: 14px; }
.doc .doc-toc b { display: block; margin-bottom: 4px; font-size: 13px; color: var(--muted); font-weight: 600; }
.doc .doc-toc a { display: block; color: var(--ink-2); text-decoration: none; padding: 2px 0; }
.doc .doc-toc a.l3 { padding-left: 16px; font-size: 13.5px; }
.doc .doc-toc a:hover { color: var(--green-dark); }
.md h2 { font-size: 21px; margin: 34px 0 10px; padding-top: 12px; border-top: 1px solid var(--line); scroll-margin-top: 16px; }
.md h3 { font-size: 17px; margin: 24px 0 8px; scroll-margin-top: 16px; }
.md h4 { font-size: 15px; margin: 18px 0 6px; scroll-margin-top: 16px; }
.md p, .md li { font-size: 15px; color: var(--ink-2); }
.md ul, .md ol { padding-left: 22px; }
.md img {
  display: block; max-width: 100%; height: auto; margin: 10px 0 4px; border: 1px solid var(--line);
  border-radius: 10px; cursor: zoom-in; background: #fff;
}
.md em { color: var(--muted); font-size: 13.5px; }
.md table { width: 100%; border-collapse: collapse; font-size: 14px; margin: 12px 0 20px; background: #fff; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--line); padding: 7px 10px; text-align: left; vertical-align: top; }
.md th { background: #F3F4F6; font-weight: 600; }
.md :target, .md .flash { animation: flash 2s ease-out; }
@keyframes flash { from { background: #FFF3B0; } to { background: transparent; } }

/* ---------- Trang chủ ---------- */
.home { max-width: 900px; margin: 0 auto; }
.hero { background: linear-gradient(135deg, #0E7A3A, #20B759); color: #fff; border-radius: 18px; padding: 30px 32px; margin-bottom: 26px; }
.hero h1 { margin: 0 0 6px; font-size: 26px; }
.hero p { margin: 0 0 16px; opacity: .92; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { border: 1px solid rgba(255,255,255,.5); background: rgba(255,255,255,.12); color: #fff; border-radius: 999px; padding: 6px 14px; font-size: 13.5px; }
.chip:hover { background: rgba(255,255,255,.25); }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 16px 18px; }
.card h3 { margin: 0 0 8px; font-size: 15.5px; }
.card a { display: block; font-size: 14px; color: var(--ink-2); text-decoration: none; padding: 3px 0; }
.card a:hover { color: var(--green-dark); text-decoration: underline; }

/* ---------- Chat ---------- */
.chat-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.chat-title { font-weight: 700; }
.chat-sub { font-size: 12.5px; color: var(--muted); }
.text-btn { border: 0; background: none; color: var(--blue); font-size: 13px; padding: 4px 6px; border-radius: 6px; }
.text-btn:hover { background: var(--bg); }
.chat-log { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.msg { max-width: 100%; font-size: 14.5px; }
.msg.user { align-self: flex-end; background: var(--green); color: #fff; padding: 9px 13px; border-radius: 14px 14px 4px 14px; max-width: 88%; white-space: pre-wrap; word-wrap: break-word; }
.msg.bot { align-self: stretch; }
.msg.bot .bubble { background: var(--bg); border-radius: 4px 14px 14px 14px; padding: 10px 14px; }
.msg.bot .bubble > :first-child { margin-top: 0; }
.msg.bot .bubble > :last-child { margin-bottom: 0; }
.msg.bot p, .msg.bot li { font-size: 14.5px; color: var(--ink); margin: 6px 0; }
.msg.bot ol, .msg.bot ul { padding-left: 20px; margin: 6px 0; }
.msg.bot img { display: block; max-width: 100%; border: 1px solid var(--line); border-radius: 8px; margin: 8px 0; cursor: zoom-in; background: #fff; }
.msg.bot table { border-collapse: collapse; font-size: 13px; display: block; overflow-x: auto; }
.msg.bot th, .msg.bot td { border: 1px solid var(--line); padding: 4px 8px; }
.msg.bot .error { color: #B42318; }
.msg-actions { display: flex; align-items: center; gap: 6px; margin-top: 6px; font-size: 12.5px; color: var(--muted); flex-wrap: wrap; }
.msg-actions button { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 2px 10px; color: var(--ink-2); font-size: 12.5px; }
.msg-actions button:hover { border-color: var(--muted); }
.msg-actions button.on { border-color: var(--green); color: var(--green-dark); background: var(--green-bg); }
.handoff-card { margin-top: 8px; background: var(--warn-bg); border: 1px solid #F5D9A8; border-radius: 10px; padding: 10px 12px; font-size: 13.5px; color: var(--warn); }
.handoff-card button { margin-top: 6px; }
.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.welcome { font-size: 14px; color: var(--ink-2); }
.welcome .chips-light { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }
.welcome .chips-light button { text-align: left; border: 1px solid var(--line); background: #fff; border-radius: 10px; padding: 8px 12px; font-size: 13.5px; color: var(--ink-2); }
.welcome .chips-light button:hover { border-color: var(--green); color: var(--green-dark); }
.chat-input { display: flex; gap: 8px; align-items: flex-end; padding: 12px 14px 6px; border-top: 1px solid var(--line); }
.chat-input textarea {
  flex: 1; resize: none; max-height: 160px; border: 1px solid var(--line); border-radius: 12px; padding: 9px 12px;
  font: inherit; font-size: 14.5px; outline: none; line-height: 1.45;
}
.chat-input textarea:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(32,183,89,.15); }
.chat-input button { width: 42px; height: 42px; border: 0; border-radius: 12px; background: var(--green); color: #fff; display: grid; place-items: center; flex: none; }
.chat-input button:disabled { background: #A7DDBB; cursor: default; }
.chat-foot { font-size: 11.5px; color: var(--muted); padding: 0 16px 10px; }

/* ---------- Dialog ---------- */
dialog { border: 0; border-radius: 16px; padding: 22px 24px; width: min(480px, 94vw); box-shadow: 0 20px 50px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(17,24,39,.45); }
dialog h2 { margin: 0 0 4px; font-size: 19px; }
dialog p { margin: 0 0 14px; font-size: 13.5px; }
dialog label { display: block; font-size: 13.5px; color: var(--ink-2); margin-bottom: 10px; }
dialog input, dialog textarea { display: block; width: 100%; margin-top: 4px; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font: inherit; font-size: 14.5px; }
dialog input:focus, dialog textarea:focus { outline: none; border-color: var(--green); }
.req { color: #B42318; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.primary { border: 0; background: var(--green); color: #fff; border-radius: 10px; padding: 8px 18px; font-weight: 600; }
.primary:disabled { opacity: .6; }
.lightbox { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.8); display: grid; place-items: center; cursor: zoom-out; padding: 20px; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; background: #fff; }
.lightbox[hidden] { display: none; }


/* ---------- Màn hình hẹp ---------- */
@media (max-width: 1180px) { :root { --chat-w: 380px; --side-w: 250px; } .content { padding: 24px 24px 80px; } }
@media (max-width: 900px) {
  .only-mobile { display: initial !important; }
  .contact { display: none; }
  .brand span { display: none; }
  .layout { grid-template-columns: 1fr; height: calc(100vh - var(--top) - 52px); }
  .sidebar { position: fixed; z-index: 30; top: var(--top); bottom: 52px; left: 0; width: 86vw; max-width: 320px; transform: translateX(-100%); transition: transform .2s; box-shadow: 4px 0 20px rgba(0,0,0,.12); }
  body.menu-open .sidebar { transform: none; }
  .chat { display: none; border-left: 0; }
  body.tab-chat .chat { display: flex; }
  body.tab-chat .content { display: none; }
  .content { padding: 18px 16px 40px; }
  .mobile-tabs { position: fixed; bottom: 0; left: 0; right: 0; height: 52px; display: flex !important; background: #fff; border-top: 1px solid var(--line); z-index: 25; }
  .mobile-tabs button { flex: 1; border: 0; background: none; font-weight: 600; color: var(--muted); }
  .mobile-tabs button.active { color: var(--green-dark); box-shadow: inset 0 3px 0 var(--green); }
  .hero { padding: 22px 20px; }
}

/* ---------- Chế độ nhúng (widget) — đặt CUỐI file để thắng media query màn hẹp (iframe chỉ rộng ~420px) ---------- */
body.embed .topbar, body.embed .sidebar, body.embed .content, body.embed .mobile-tabs { display: none !important; }
body.embed .layout { grid-template-columns: 1fr; height: 100vh; }
body.embed .chat { display: flex; border-left: 0; }
