:root {
  --bg: #f6f8f8;
  --surface: #ffffff;
  --surface-2: #eef3f2;
  --text: #13201e;
  --muted: #5d6b69;
  --line: #dde5e3;
  --brand: #0f766e;
  --brand-ink: #ffffff;
  --brand-soft: #d9f0ec;
  --danger: #b42318;
  --danger-soft: #fdeceb;
  --danger-ink: #ffffff;
  --warn: #9a5b00;
  --warn-soft: #fff3dc;
  --ok: #1d6f42;
  --ok-soft: #e3f4ea;
  --user-bubble: #0f766e;
  --user-ink: #ffffff;
  --shadow: 0 1px 2px rgba(16, 40, 36, .06), 0 4px 16px rgba(16, 40, 36, .06);
  --radius: 14px;
  --font-size: 17px;
  --tabbar-h: 62px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1615; --surface: #16211f; --surface-2: #1d2a28; --text: #e4ecea; --muted: #94a5a2;
    --line: #2a3936; --brand: #2bb3a3; --brand-ink: #04211d; --brand-soft: #173a35;
    --danger: #ff8a80; --danger-soft: #3a1d1b; --danger-ink: #2b0b08; --warn: #ffc56b; --warn-soft: #3a2c12;
    --ok: #7fd6a2; --ok-soft: #173327; --user-bubble: #1f6f66; --user-ink: #eafffb;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1615; --surface: #16211f; --surface-2: #1d2a28; --text: #e4ecea; --muted: #94a5a2;
  --line: #2a3936; --brand: #2bb3a3; --brand-ink: #04211d; --brand-soft: #173a35;
  --danger: #ff8a80; --danger-soft: #3a1d1b; --danger-ink: #2b0b08; --warn: #ffc56b; --warn-soft: #3a2c12;
  --ok: #7fd6a2; --ok-soft: #173327; --user-bubble: #1f6f66; --user-ink: #eafffb;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: var(--font-size)/1.55 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
button, input, textarea { font: inherit; color: inherit; }
/* iOS tự phóng to trang khi chạm vào ô nhập có chữ nhỏ hơn 16px. */
input, textarea { font-size: max(16px, 1em); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
button { cursor: pointer; }
a { color: var(--brand); }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }
body.no-ai .ai-only { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85em; }

/* Thanh trên */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px; min-height: 52px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.05em; flex: 1; min-width: 0; }
.brand img { border-radius: 8px; flex: none; }
.brand span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.net-badge { font-size: .75em; padding: 2px 10px; border-radius: 99px; background: var(--warn-soft); color: var(--warn); }
.install-btn { border: 0; background: var(--brand); color: var(--brand-ink); padding: 6px 14px; border-radius: 99px; font-size: .85em; font-weight: 600; }
.icon-btn { border: 0; background: none; padding: 8px; border-radius: 10px; display: grid; place-items: center; color: var(--muted); }
.icon-btn:active { background: var(--surface-2); }

main { max-width: 760px; margin: 0 auto; }
.view { display: none; padding: 16px; }
.view.active { display: block; }
.view-title { margin: 4px 0 4px; font-size: 1.3em; }

/* Thanh tab dưới */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10; display: flex;
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom); height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  text-decoration: none; color: var(--muted); font-size: 12px; font-weight: 500;
}
.tabbar a.active { color: var(--brand); }
.tabbar a.active svg { stroke-width: 2.3; }

/* Chat */
#view-chat.active { display: flex; flex-direction: column; min-height: calc(100dvh - 52px - var(--tabbar-h) - env(safe-area-inset-bottom) - env(safe-area-inset-top)); padding-bottom: 0; }
.mode-bar { display: flex; gap: 6px; background: var(--surface-2); padding: 4px; border-radius: 12px; }
.mode-bar button { flex: 1; border: 0; background: none; padding: 7px 4px; border-radius: 9px; font-size: .85em; color: var(--muted); font-weight: 600; }
.mode-bar button.on { background: var(--surface); color: var(--brand); box-shadow: var(--shadow); }
.chat-log { flex: 1; padding: 12px 0 8px; display: flex; flex-direction: column; gap: 14px; }
.chat-empty { text-align: center; padding: 24px 8px; }
.chat-empty h2 { margin: 0 0 6px; font-size: 1.25em; }
.chat-empty p { margin: 0 auto 18px; color: var(--muted); max-width: 30em; }
.suggest { display: flex; flex-direction: column; gap: 8px; text-align: left; }
.suggest button {
  border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 10px 14px; text-align: left; font-size: .92em;
}
.suggest button:active { background: var(--surface-2); }
.msg { max-width: 100%; }
.msg.user { align-self: flex-end; max-width: 85%; }
.msg.user .bubble { background: var(--user-bubble); color: var(--user-ink); border-radius: 18px 18px 4px 18px; padding: 10px 14px; white-space: pre-wrap; word-wrap: break-word; }
.msg.user img { display: block; max-width: 180px; border-radius: 12px; margin: 0 0 6px auto; }
.msg.ai .bubble { background: var(--surface); border: 1px solid var(--line); border-radius: 4px 18px 18px 18px; padding: 12px 16px; box-shadow: var(--shadow); overflow-wrap: anywhere; }
.msg.ai .bubble > :first-child { margin-top: 0; }
.msg.ai .bubble > :last-child { margin-bottom: 0; }
.msg.ai h3, .msg.ai h4 { margin: 14px 0 4px; font-size: 1.02em; color: var(--brand); }
.msg.ai p { margin: 6px 0; }
.msg.ai ul, .msg.ai ol { margin: 6px 0; padding-left: 1.3em; }
.msg.ai li { margin: 3px 0; }
.msg.ai .src-tag { font-size: .78em; background: var(--brand-soft); color: var(--brand); padding: 1px 7px; border-radius: 6px; white-space: nowrap; }
.msg.ai hr { border: 0; border-top: 1px solid var(--line); margin: 10px 0; }
.table-wrap { overflow-x: auto; margin: 8px 0; }
.msg.ai table { border-collapse: collapse; font-size: .9em; min-width: 100%; }
.msg.ai th, .msg.ai td { border: 1px solid var(--line); padding: 5px 8px; text-align: left; vertical-align: top; }
.msg.ai th { background: var(--surface-2); }
.msg-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; font-size: .8em; color: var(--muted); align-items: center; }
.msg-meta a { text-decoration: none; background: var(--surface-2); padding: 2px 9px; border-radius: 99px; color: var(--text); }
.typing span { display: inline-block; width: 7px; height: 7px; margin-right: 4px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; } .typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.alert { border-radius: 12px; padding: 10px 14px; margin-bottom: 8px; font-size: .92em; }
.alert.emergency { background: var(--danger-soft); color: var(--danger); border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent); }
.alert.support { background: var(--warn-soft); color: var(--text); }
.alert a.call { display: inline-block; margin-top: 6px; background: var(--danger); color: var(--danger-ink); padding: 6px 14px; border-radius: 99px; text-decoration: none; font-weight: 700; }
.alert.error { background: var(--warn-soft); color: var(--warn); }

.composer {
  position: sticky; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); display: flex; align-items: flex-end; gap: 6px;
  background: var(--bg); padding: 8px 0 10px; margin-top: auto;
}
.composer-main { flex: 1; background: var(--surface); border: 1px solid var(--line); border-radius: 20px; padding: 6px 12px; min-width: 0; }
.composer-main:focus-within { border-color: var(--brand); }
.composer textarea { width: 100%; border: 0; outline: 0; background: none; resize: none; max-height: 140px; padding: 4px 0; display: block; }
.image-preview { position: relative; display: inline-block; margin: 4px 0; }
.image-preview img { height: 64px; border-radius: 8px; display: block; }
.image-preview button { position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: var(--text); color: var(--bg); line-height: 1; }
.send-btn { flex: none; width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--brand); color: var(--brand-ink); display: grid; place-items: center; }
.send-btn.stop { background: var(--danger); color: var(--danger-ink); }
.send-btn:disabled { opacity: .45; }

/* Tra cứu */
.search-box { display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 0 12px; }
.search-box:focus-within { border-color: var(--brand); }
.search-box svg { color: var(--muted); flex: none; }
.search-box input { flex: 1; border: 0; outline: 0; background: none; padding: 12px 0; min-width: 0; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 12px 0 4px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips button { flex: none; border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 5px 14px; font-size: .88em; }
.chips button.on { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.result-list { list-style: none; margin: 4px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.result-list a {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); text-decoration: none; color: inherit;
}
.result-list a:active { background: var(--surface-2); }
.kind-dot { flex: none; width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center; font-size: .72em; font-weight: 700; }
.kind-drugs { background: #dbeafe; color: #1e40af; } .kind-diseases { background: #fde2e2; color: #9b1c1c; }
.kind-labs { background: #ede9fe; color: #5b21b6; } .kind-herbs { background: #dcfce7; color: #166534; }
:root[data-theme="dark"] .kind-dot { filter: brightness(.8) saturate(1.2); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .kind-dot { filter: brightness(.8) saturate(1.2); } }
.result-main { min-width: 0; }
.result-main b { display: block; }
.result-main span { display: block; font-size: .85em; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Chi tiết */
.detail-head { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 8px; }
.detail-head h1 { margin: 0; font-size: 1.45em; line-height: 1.25; }
.detail-head .sub { color: var(--muted); font-size: .92em; margin-top: 2px; }
.detail-head .grow { flex: 1; min-width: 0; }
.star { border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 8px; color: var(--muted); }
.star.on { color: #d97706; border-color: #f6c46a; }
.star.on svg { fill: currentColor; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 12px; }
.tag { font-size: .8em; padding: 2px 10px; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.tag.rx { background: var(--danger-soft); color: var(--danger); font-weight: 600; }
.tag.otc { background: var(--ok-soft); color: var(--ok); font-weight: 600; }
.sec { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; margin-bottom: 10px; }
.sec h2 { margin: 0 0 6px; font-size: 1em; color: var(--brand); }
.sec.warn { border-color: color-mix(in srgb, var(--danger) 35%, var(--line)); background: var(--danger-soft); }
.sec.warn h2 { color: var(--danger); }
.sec ul { margin: 0; padding-left: 1.2em; }
.sec li { margin: 3px 0; }
.sec p { margin: 0; }
.sec dl { margin: 0; display: grid; grid-template-columns: minmax(90px, auto) 1fr; gap: 6px 12px; }
.sec dt { color: var(--muted); font-size: .9em; }
.sec dd { margin: 0; }
.ask-ai { width: 100%; border: 0; background: var(--brand); color: var(--brand-ink); padding: 12px; border-radius: 12px; font-weight: 600; margin: 6px 0 12px; }
.link-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.link-chips a { text-decoration: none; background: var(--brand-soft); color: var(--brand); padding: 3px 11px; border-radius: 99px; font-size: .9em; }

/* Tương tác */
.picker-list { list-style: none; margin: 6px 0 0; padding: 0; background: var(--surface); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); }
.picker-list:empty { display: none; }
.picker-list button { width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); background: none; padding: 10px 14px; }
.picker-list small { color: var(--muted); }
.chips.selected { flex-wrap: wrap; }
.chips.selected button { background: var(--brand-soft); border-color: transparent; color: var(--brand); font-weight: 600; }
.ix { border-radius: var(--radius); padding: 12px 14px; margin: 10px 0; border: 1px solid var(--line); background: var(--surface); }
.ix h3 { margin: 0 0 4px; font-size: 1em; }
.ix p { margin: 4px 0 0; font-size: .95em; }
.sev { display: inline-block; font-size: .75em; font-weight: 700; padding: 1px 9px; border-radius: 99px; margin-left: 6px; vertical-align: 2px; }
.sev-nang { background: var(--danger-soft); color: var(--danger); }
.sev-trung { background: var(--warn-soft); color: var(--warn); }
.sev-nhe { background: var(--ok-soft); color: var(--ok); }
.ix.none { background: var(--ok-soft); border-color: transparent; }

/* Cài đặt */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 16px; margin: 12px 0; }
.card .row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.card .row:last-child { border-bottom: 0; }
.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: none; padding: 5px 10px; border-radius: 8px; font-size: .85em; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--brand); font-weight: 600; box-shadow: var(--shadow); }
.link-btn { border: 0; background: none; color: var(--brand); font-weight: 600; padding: 4px; }
.link-btn.danger { color: var(--danger); }
.disclaimer-text { padding: 12px 16px; }
.disclaimer-text h3 { margin: 0 0 6px; }
.disclaimer-text p { margin: 6px 0; }

dialog { border: 0; border-radius: 18px; padding: 22px; max-width: min(420px, calc(100vw - 32px)); background: var(--surface); color: var(--text); }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog h2 { margin: 0 0 8px; }
dialog ul { padding-left: 1.2em; }
.primary { width: 100%; border: 0; background: var(--brand); color: var(--brand-ink); padding: 12px; border-radius: 12px; font-weight: 700; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom));
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 99px; font-size: .9em; z-index: 20; max-width: calc(100vw - 32px);
}
.empty-state { text-align: center; color: var(--muted); padding: 32px 12px; }

/* Giải đáp & đọc file */
.solve-box { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin-top: 8px; display: flex; flex-direction: column; gap: 10px; }
.solve-box textarea, .card.pad textarea, .card.pad input[type="text"] {
  width: 100%; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); padding: 10px 12px; resize: vertical; outline: 0;
}
.solve-box textarea:focus, .card.pad textarea:focus, .card.pad input[type="text"]:focus { border-color: var(--brand); }
.file-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.file-btn { display: inline-flex; align-items: center; gap: 6px; border: 1px dashed var(--brand); color: var(--brand); border-radius: 10px; padding: 8px 12px; font-weight: 600; font-size: .92em; cursor: pointer; }
.file-btn:focus-within { outline: 2px solid var(--brand); outline-offset: 2px; }
.file-btn svg { width: 18px; height: 18px; }
.file-name { font-size: .88em; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 60%; }
.primary:disabled { opacity: .55; }
.history-head { display: flex; justify-content: space-between; align-items: center; margin-top: 18px; }
.history-head h3 { margin: 0; font-size: 1.05em; }
.result-summary { background: var(--brand-soft); color: var(--text); border-radius: 12px; padding: 10px 14px; margin: 12px 0 8px; }
.result-summary b { color: var(--brand); }
.qa { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin: 10px 0; }
.qa .q { font-weight: 600; margin-bottom: 6px; }
.qa ol.opts { list-style: none; padding: 0; margin: 6px 0; display: flex; flex-direction: column; gap: 4px; }
.qa ol.opts li { padding: 5px 10px; border-radius: 8px; background: var(--surface-2); font-size: .95em; }
.qa ol.opts li.correct { background: var(--ok-soft); color: var(--ok); font-weight: 600; }
.qa .ans { margin-top: 8px; padding: 8px 10px; border-radius: 10px; background: var(--ok-soft); }
.qa .ans b { color: var(--ok); }
.qa .none { margin-top: 8px; padding: 8px 10px; border-radius: 10px; background: var(--surface-2); color: var(--muted); font-size: .92em; }
.qa .warn-note { margin-top: 6px; font-size: .85em; color: var(--warn); }
.qa .expl { margin-top: 4px; font-size: .92em; }
.snip { margin-top: 8px; font-size: .88em; border-left: 3px solid var(--brand); padding: 4px 10px; color: var(--text); }
.snip b { display: block; color: var(--brand); font-size: .95em; }
.lab { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; margin: 8px 0; }
.lab .nm { font-weight: 600; } .lab .nm a { color: inherit; text-decoration: none; }
.lab .val { font-weight: 700; text-align: right; white-space: nowrap; }
.lab .ref { grid-column: 1 / -1; font-size: .85em; color: var(--muted); }
.lab ul { grid-column: 1 / -1; margin: 4px 0 0; padding-left: 1.2em; font-size: .9em; }
.st { display: inline-block; font-size: .75em; font-weight: 700; padding: 1px 9px; border-radius: 99px; margin-left: 6px; vertical-align: 2px; }
.st-cao { background: var(--danger-soft); color: var(--danger); } .st-thap { background: var(--warn-soft); color: var(--warn); }
.st-bt { background: var(--ok-soft); color: var(--ok); } .st-kr { background: var(--surface-2); color: var(--muted); }
details.read-text { margin: 10px 0; font-size: .88em; }
details.read-text pre { white-space: pre-wrap; background: var(--surface-2); padding: 10px; border-radius: 10px; max-height: 260px; overflow: auto; font: inherit; }
.loading { text-align: center; color: var(--muted); padding: 16px; }

/* Quản trị */
.card.pad { padding: 14px 16px; display: flex; flex-direction: column; gap: 10px; }
.card.pad h3 { margin: 0; }
.card.pad p { margin: 0; }
.admin-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.admin-list li { display: flex; gap: 10px; align-items: flex-start; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: .92em; }
.admin-list li:last-child { border-bottom: 0; }
.admin-list .grow { flex: 1; min-width: 0; }
.admin-list small { color: var(--muted); display: block; }
.admin-list .del { border: 0; background: none; color: var(--danger); font-weight: 600; padding: 2px 6px; }
.prev-q { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font-size: .9em; }
.prev-q.missing { border-color: var(--warn); background: var(--warn-soft); }
.prev-list { display: flex; flex-direction: column; gap: 6px; max-height: 420px; overflow: auto; }
.tabbar a span { white-space: nowrap; }
.prev-q .correct-opt { color: var(--ok); font-weight: 600; }
.sub-head { font-size: 1em; margin: 18px 0 6px; color: var(--muted); }
.card.pad input[type="file"] { font-size: .9em; max-width: 100%; }
.lab .warn-text { color: var(--warn); }
.alert .link-btn { display: block; padding: 4px 0 0; color: inherit; text-decoration: underline; }
