:root {
  --brand: #105A38; --brand-2: #0B4A2D; --brand-soft: #E7F1EC; --accent: #EB2529;
  --bg: #F6F7F6; --surface: #FFFFFF; --surface-2: #F0F2F1; --line: #E1E5E3;
  --text: #17201B; --muted: #66716B; --user-bubble: #E4EFE9; --code-bg: #F2F4F3;
  --danger: #C62828; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.06);
  --radius: 14px; --side-w: 290px;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --brand: #3FA877; --brand-2: #2F8F62; --brand-soft: #16291F; --accent: #FF5A5E;
    --bg: #0F1412; --surface: #161C19; --surface-2: #1D2521; --line: #28322D;
    --text: #E7ECE9; --muted: #93A099; --user-bubble: #1F3329; --code-bg: #1B2320;
    --danger: #FF6B6B; --shadow: 0 1px 2px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --brand: #3FA877; --brand-2: #2F8F62; --brand-soft: #16291F; --accent: #FF5A5E;
  --bg: #0F1412; --surface: #161C19; --surface-2: #1D2521; --line: #28322D;
  --text: #E7ECE9; --muted: #93A099; --user-bubble: #1F3329; --code-bg: #1B2320;
  --danger: #FF6B6B; --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
a { color: var(--brand); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.ic { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.ic.fill { fill: currentColor; stroke: none; }

/* ---------- Nút ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-2); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn {
  display: inline-grid; place-items: center; width: 44px; height: 44px; border-radius: 12px;
  border: 0; background: transparent; color: var(--muted); cursor: pointer; flex: none;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
input, textarea, select {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; padding: 11px 13px; width: 100%; min-height: 44px;
}
input:focus, textarea:focus, select:focus { outline: 2px solid color-mix(in srgb, var(--brand) 45%, transparent); outline-offset: 0; border-color: var(--brand); }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row input { flex: 1 1 180px; width: auto; }
.stack { display: grid; gap: 8px; }

/* ---------- Màn đăng nhập ---------- */
.screen.center { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 20px; box-shadow: var(--shadow); }
.auth { width: min(400px, 100%); padding: 28px 24px; display: grid; gap: 14px; }
.auth h1 { margin: 0 0 4px; font-size: 22px; text-align: center; }
.auth label { display: grid; gap: 6px; font-size: 14px; font-weight: 600; }
.logo-full { width: 150px; margin: 0 auto; display: block; }
.logo-mark { width: 72px; margin: 0 auto; display: block; }
.err { color: var(--danger); margin: 0; min-height: 1em; font-size: 14px; }
.or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }

/* ---------- Bố cục chính ---------- */
#app { display: flex; height: 100dvh; overflow: hidden; }
#sidebar {
  width: var(--side-w); flex: none; background: var(--surface); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: calc(12px + var(--safe-t)) 12px calc(12px + var(--safe-b));
}
.side-head { display: flex; align-items: center; gap: 10px; padding: 4px 6px 12px; font-size: 17px; }
.side-logo { width: 34px; height: 34px; }
.side-head strong { flex: 1; }
.new-chat { width: 100%; margin-bottom: 10px; }
#convList { flex: 1; overflow-y: auto; margin: 0 -4px; padding: 0 4px; }
.conv {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; border: 0; background: none;
  color: var(--text); font: inherit; font-size: 15px; padding: 10px 12px; border-radius: 10px; cursor: pointer; min-height: 44px;
}
.conv span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv:hover { background: var(--surface-2); }
.conv.active { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.conv .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s infinite; flex: none; }
.group-label { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; padding: 12px 12px 4px; }
.side-foot { border-top: 1px solid var(--line); padding-top: 10px; margin-top: 8px; }
.who { display: flex; gap: 10px; align-items: center; padding: 4px 6px 8px; }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--brand); color: #fff; display: grid; place-items: center; font-weight: 700; flex: none; }
.side-actions { display: flex; flex-wrap: wrap; gap: 2px; }
.side-actions .btn { min-height: 38px; padding: 0 10px; font-size: 14px; }

#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.top {
  display: flex; align-items: center; gap: 4px; padding: calc(6px + var(--safe-t)) 10px 6px 16px;
  border-bottom: 1px solid var(--line); background: var(--surface); min-height: 56px;
}
.top h2 { flex: 1; margin: 0; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#scroller { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
#messages, #welcome { max-width: 820px; margin: 0 auto; padding: 16px; }
#welcome { text-align: center; padding-top: 12vh; }
.welcome-mark { width: 84px; }
#welcome h3 { margin: 10px 0 4px; font-size: 24px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.chip { border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 999px; padding: 9px 14px; font: inherit; font-size: 14px; cursor: pointer; }
.chip:hover { border-color: var(--brand); color: var(--brand); }

/* ---------- Tin nhắn ---------- */
.msg { margin: 18px 0; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .bubble { background: var(--user-bubble); padding: 10px 14px; border-radius: 18px 18px 4px 18px; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.assistant { display: grid; grid-template-columns: 30px 1fr; gap: 10px; }
.msg.assistant .mark { width: 30px; height: 30px; margin-top: 2px; }
.md { overflow-wrap: anywhere; min-width: 0; }
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p { margin: .55em 0; }
.md pre { background: var(--code-bg); padding: 12px; border-radius: 10px; overflow-x: auto; font-size: 13.5px; }
.md code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; font-size: .9em; }
.md pre code { background: none; padding: 0; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; font-size: 14.5px; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.md th { background: var(--surface-2); }
.md blockquote { margin: .6em 0; padding-left: 12px; border-left: 3px solid var(--brand); color: var(--muted); }
.md img { max-width: 100%; }
.cursor::after { content: "▍"; color: var(--brand); animation: blink 1s steps(2) infinite; }
.msg-err { color: var(--danger); background: color-mix(in srgb, var(--danger) 8%, transparent); border-radius: 10px; padding: 8px 12px; margin-top: 8px; font-size: 14.5px; }
.meta { font-size: 12.5px; color: var(--muted); margin-top: 6px; }

.tools { margin: 0 0 8px; font-size: 14px; }
.tools summary { cursor: pointer; color: var(--muted); list-style: none; display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); }
.tools summary::-webkit-details-marker { display: none; }
.tools ol { margin: 8px 0 0; padding-left: 22px; color: var(--muted); }
.tools li { margin: 2px 0; overflow-wrap: anywhere; }
.tools li b { color: var(--text); font-weight: 600; }
.live-step { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; margin-bottom: 6px; }
.spinner { width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; flex: none; }

.files { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.msg.user .files { justify-content: flex-end; }
.file {
  display: inline-flex; align-items: center; gap: 10px; max-width: 100%; padding: 8px 12px 8px 8px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--text); text-decoration: none; font-size: 14px;
}
.file:hover { border-color: var(--brand); }
.file .ext { width: 36px; height: 36px; border-radius: 8px; display: grid; place-items: center; background: var(--brand-soft); color: var(--brand); font-size: 11px; font-weight: 800; text-transform: uppercase; flex: none; }
.file .fn { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.file img { width: 36px; height: 36px; object-fit: cover; border-radius: 8px; }

/* ---------- Ô soạn tin ---------- */
#composer { padding: 8px 12px calc(10px + var(--safe-b)); background: linear-gradient(transparent, var(--bg) 30%); }
#composer .bar {
  max-width: 820px; margin: 0 auto; display: flex; align-items: flex-end; gap: 4px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 22px; padding: 5px; box-shadow: var(--shadow);
}
#composer .bar:focus-within { border-color: var(--brand); }
#input { border: 0; outline: 0; resize: none; max-height: 40vh; padding: 10px 4px; min-height: 44px; background: transparent; }
#input:focus { outline: 0; }
.send { width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--brand); color: #fff; display: grid; place-items: center; cursor: pointer; flex: none; }
.send:disabled { opacity: .4; }
.send.stop { background: var(--text); color: var(--surface); }
#micBtn.rec { color: #fff; background: var(--accent); animation: pulse 1.2s infinite; }
#attachList { max-width: 820px; margin: 0 auto 6px; display: flex; flex-wrap: wrap; gap: 6px; }
#attachList:empty { display: none; }
.att { display: inline-flex; align-items: center; gap: 4px; background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 4px 4px 4px 12px; font-size: 13.5px; max-width: 100%; }
.att span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 200px; }
.att button { width: 28px; height: 28px; border: 0; background: none; color: var(--muted); border-radius: 50%; cursor: pointer; }

/* ---------- Hộp thoại, thông báo ---------- */
dialog { border: 0; border-radius: 20px; padding: 0; width: min(520px, calc(100vw - 32px)); max-height: 88dvh; background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(10,20,15,.45); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 4px 20px; position: sticky; top: 0; background: var(--surface); }
.dlg-head h3 { margin: 0; }
.dlg-body { padding: 4px 20px 20px; }
.dlg-body section { padding: 12px 0; border-bottom: 1px solid var(--line); }
.dlg-body section:last-child { border-bottom: 0; }
.dlg-body h4 { margin: 0 0 8px; font-size: 15px; }
.pk { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 0; font-size: 14.5px; }
#toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--surface); padding: 10px 16px; border-radius: 12px; font-size: 14.5px;
  opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100vw - 32px); z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes blink { 50% { opacity: 0; } }
@keyframes pulse { 50% { opacity: .55; } }

/* ---------- Điện thoại ---------- */
.only-mobile { display: none; }
@media (max-width: 800px) {
  .only-mobile { display: inline-grid; }
  #sidebar { position: fixed; inset: 0 auto 0 0; z-index: 30; transform: translateX(-100%); transition: transform .22s ease; width: min(86vw, 320px); box-shadow: var(--shadow); }
  #app.side-open #sidebar { transform: none; }
  #scrim { position: fixed; inset: 0; background: rgba(10,20,15,.4); z-index: 20; display: none; }
  #app.side-open #scrim { display: block; }
  .top { padding-left: 4px; }
  #messages, #welcome { padding: 12px 16px; }
  .msg.user .bubble { max-width: 92%; }
  .file .fn { max-width: 160px; }
}

/* Logo xanh đậm khó đọc trên nền tối → đặt trên tấm nền trắng */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .logo-full, :root:not([data-theme="light"]) .logo-mark, :root:not([data-theme="light"]) .welcome-mark { background: #fff; border-radius: 18px; padding: 8px; }
  :root:not([data-theme="light"]) .msg .mark, :root:not([data-theme="light"]) .side-logo { background: #fff; border-radius: 50%; padding: 3px; }
}
:root[data-theme="dark"] .logo-full, :root[data-theme="dark"] .logo-mark, :root[data-theme="dark"] .welcome-mark { background: #fff; border-radius: 18px; padding: 8px; }
:root[data-theme="dark"] .msg .mark, :root[data-theme="dark"] .side-logo { background: #fff; border-radius: 50%; padding: 3px; }

/* ---------- Giọng nói ---------- */
.speak { border: 0; background: none; color: var(--brand); font: inherit; font-size: 12.5px; cursor: pointer; padding: 2px 4px; border-radius: 6px; }
.speak:hover { background: var(--brand-soft); }
.speak.on { color: var(--accent); }
#voiceBtn.on { color: #fff; background: var(--brand); }
#voicePanel {
  max-width: 820px; width: calc(100% - 24px); margin: 0 auto 6px; display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--brand); border-radius: 18px; padding: 10px 10px 10px 12px; box-shadow: var(--shadow);
}
.vp-orb { width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center; background: var(--brand-soft); color: var(--brand); flex: none; position: relative; }
.vp-orb.listen { background: var(--accent); color: #fff; }
.vp-orb.listen::after { content: ""; position: absolute; inset: -6px; border-radius: 50%; border: 2px solid var(--accent); animation: ring 1.4s ease-out infinite; }
.vp-orb.speak { background: var(--brand); color: #fff; animation: pulse 1s infinite; }
.vp-orb.work .ic { animation: spin 1.2s linear infinite; }
.vp-text { flex: 1; min-width: 0; display: grid; font-size: 14.5px; }
.vp-text span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13.5px; }
#vpAction { min-height: 40px; padding: 0 12px; font-size: 14px; }
@keyframes ring { from { transform: scale(.9); opacity: .9; } to { transform: scale(1.35); opacity: 0; } }
.check { display: flex; gap: 10px; align-items: center; cursor: pointer; }
.check input { width: 20px; height: 20px; min-height: 0; flex: none; accent-color: var(--brand); }
