/* =====================================================================
   brand-support.css v1.6
   1) Header thương hiệu ở đầu Sidebar trái (logo Hoa Sen Home + font)
   2) Hộp hỗ trợ nổi "Phòng Kiểm soát nội bộ" (#chatWidgetBox) — thu gọn
   3) (v1.1) Hộp hỗ trợ 2 tab: Trợ lý ảo AI | Liên hệ KSNB (js/support-ai.js)
   4) (v1.2) Nút nổi "Trợ lý KSNB" (đỏ + chấm online), avatar KS, typing 3 chấm, tab icon SVG
   5) (v1.4) Ảnh linh vật KSNB thay chữ "KS": header 42px + chấm online, tin nhắn 28px, nút nổi 26px
   6) (v1.6) Nút nổi là <button> thật; ảnh linh vật lấy từ assets/mascot-ksnb.webp (không còn Base64 trong index.html)
   Chỉ dùng selector riêng (.cv-brand, #chatWidgetBox) nên không ảnh hưởng trang / modal khác.
   Nạp SAU cv.css và sidebar-compact.css.
   ===================================================================== */

/* ---------- 1. HEADER THƯƠNG HIỆU SIDEBAR ---------- */
.cv-side .cv-brand .cv-logo,
.cv-side .cv-brand img.cv-logo {
  display: block; width: 38px; height: 38px; flex: none; flex-shrink: 0;
  border-radius: 8px; object-fit: cover; background: #B91C1C;
}
.cv-side .cv-brand .cv-brand-title,
.cv-side .cv-brand .cv-brand-sub {
  font-family: 'Be Vietnam Pro', sans-serif;
  -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}
.cv-side .cv-brand .cv-brand-title {
  font-size: 12.5px; font-weight: 700; color: var(--ink, #0f172a);
  letter-spacing: -0.01em; line-height: 1.3;
}
.cv-side .cv-brand .cv-brand-sub {
  font-size: 11px; font-weight: 400; color: var(--muted, #64748b);
  line-height: 1.3; margin-top: 2px;
}
.cv-side .cv-brand .cv-brand-text { gap: 0; }

/* ---------- 2. HỘP HỖ TRỢ NỔI ---------- */
/* Không đặt display ở đây: Bootstrap .d-none vẫn đóng/mở được hộp bình thường. */
#chatWidgetBox.chat-widget-box {
  width: 360px; max-width: calc(100vw - 32px);
  top: auto; right: calc(20px + env(safe-area-inset-right, 0px)); bottom: calc(78px + env(safe-area-inset-bottom, 0px));
  max-height: calc(100vh - 100px); overflow-y: auto; overscroll-behavior: contain;
  border-radius: 14px; box-shadow: 0 10px 28px rgba(15, 23, 42, 0.16);
}

/* Header */
#chatWidgetBox .chat-header { padding: 12px 16px; gap: 8px; }
#chatWidgetBox .chat-head-text { gap: 10px; min-width: 0; }
#chatWidgetBox .chat-head-text .cv-avatar { width: 32px; height: 32px; flex: none; font-size: 11px; }
#chatWidgetBox .chat-header h6 { font-size: 14px; line-height: 1.3; }
#chatWidgetBox .chat-header p { margin: 0; font-size: 11.5px; line-height: 1.3; }

/* Thân hộp */
#chatWidgetBox .chat-body { padding: 12px 16px 14px; }
#chatWidgetBox .chat-welcome-msg { padding: 8px 12px; margin-bottom: 10px; font-size: 12px; line-height: 1.4; border-radius: 8px; }
#chatWidgetBox .chat-body form { gap: 0; }
#chatWidgetBox .cv-field { gap: 4px; margin: 0 0 10px; }
#chatWidgetBox .cv-field > span:first-child { font-size: 12px; margin: 0; line-height: 1.3; }
#chatWidgetBox .cv-field input { height: 36px; padding: 6px 12px; font-size: 13px; }
#chatWidgetBox .cv-field textarea {
  height: 64px; min-height: 64px; max-height: 120px; padding: 8px 12px;
  font-size: 13px; line-height: 1.45; resize: vertical;
}
#chatWidgetBox #btnSendChat { height: 38px; padding: 0 16px; margin-top: 2px; font-size: 13.5px; border-radius: 8px; }

@media (max-width: 480px) {
  #chatWidgetBox.chat-widget-box { right: calc(16px + env(safe-area-inset-right, 0px)); width: calc(100vw - 32px); }
}

/* ---------- 3. HỘP HỖ TRỢ 2 TAB (v1.1) ---------- */
/* Thanh tab */
#chatWidgetBox .sp-tabs {
  flex: none; display: grid; grid-template-columns: 1fr 1fr; gap: 2px;
  margin: 10px 14px 0; padding: 3px; border-radius: 10px;
  background: var(--bg-soft, #F8FAFC); border: 1px solid var(--line, #E2E8F0);
}
#chatWidgetBox .sp-tab {
  border: 0; background: transparent; height: 32px; padding: 0 8px; border-radius: 8px;
  font: 600 12.5px/1 'Be Vietnam Pro', system-ui, sans-serif; color: var(--muted, #64748B);
  cursor: pointer; white-space: nowrap; transition: background .15s, color .15s, box-shadow .15s;
}
#chatWidgetBox .sp-tab:hover { color: var(--ink, #0F172A); }
#chatWidgetBox .sp-tab.active {
  background: var(--surface, #fff); color: var(--accent, #DC2626);
  box-shadow: 0 1px 4px rgba(15, 23, 42, .12);
}
#chatWidgetBox .sp-tab:focus-visible { outline: 2px solid var(--accent, #DC2626); outline-offset: 1px; }

/* Panel: chỉ hiện panel đang active */
#chatWidgetBox .sp-panel { display: none; }
#chatWidgetBox .sp-panel.active { display: flex; }
#chatWidgetBox .sp-contact.active { display: block; margin-top: 4px; }

/* Tab 1: khung chat */
#chatWidgetBox .sp-ai { flex-direction: column; min-height: 0; height: min(420px, calc(100vh - 270px)); min-height: 260px; }
#chatWidgetBox .sp-msgs {
  flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  padding: 14px 14px 12px; margin-top: 8px; display: flex; flex-direction: column; gap: 10px; scroll-padding-top: 14px;
  background: var(--bg-soft, #F8FAFC); border-top: 1px solid var(--line-2, #F1F5F9);
}
#chatWidgetBox .sp-msg {
  max-width: 100%; padding: 8px 12px; border-radius: 12px; font-size: 13px; line-height: 1.5;
  overflow-wrap: anywhere; word-break: break-word;
}
#chatWidgetBox .sp-msg.bot {
  align-self: flex-start; background: var(--surface, #fff); color: var(--ink-2, #334155);
  border: 1px solid var(--line, #E2E8F0); border-bottom-left-radius: 4px;
}
#chatWidgetBox .sp-msg.user {
  align-self: flex-end; background: var(--accent, #DC2626); color: #fff; border-bottom-right-radius: 4px;
}
#chatWidgetBox .sp-msg p { margin: 0 0 6px; }
#chatWidgetBox .sp-msg p:last-child { margin-bottom: 0; }
#chatWidgetBox .sp-msg ul { margin: 2px 0 6px; padding-left: 18px; }
#chatWidgetBox .sp-msg li { margin: 0 0 3px; }
#chatWidgetBox .sp-msg strong { color: var(--ink, #0F172A); font-weight: 700; }

/* "Đang trả lời…" với 3 chấm nhấp nháy */
#chatWidgetBox .sp-typing { display: flex; align-items: center; gap: 3px; color: var(--muted, #64748B); font-size: 12.5px; }
#chatWidgetBox .sp-typing span { margin-right: 3px; }
#chatWidgetBox .sp-typing i {
  width: 5px; height: 5px; border-radius: 50%; background: var(--muted, #64748B);
  animation: spDot 1.1s infinite ease-in-out both;
}
#chatWidgetBox .sp-typing i:nth-of-type(2) { animation-delay: .15s; }
#chatWidgetBox .sp-typing i:nth-of-type(3) { animation-delay: .3s; }
@keyframes spDot { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-2px); } }

/* Nút chuyển sang tab Liên hệ trong câu trả lời */
#chatWidgetBox .sp-handoff {
  display: inline-flex; align-items: center; margin-top: 8px; padding: 5px 10px; border-radius: 8px;
  border: 1px solid var(--accent, #DC2626); background: transparent; color: var(--accent, #DC2626);
  font: 600 12px/1.2 'Be Vietnam Pro', system-ui, sans-serif; cursor: pointer;
}
#chatWidgetBox .sp-handoff:hover { background: var(--accent-soft, #FEF2F2); }

/* Gợi ý nhanh */
/* v1.5: cụm gợi ý nằm trong khung gọn (tiêu đề + nút ×), ẩn khi bấm × hoặc ngay khi gửi câu hỏi */
#chatWidgetBox .sp-quick {
  flex: none; background: var(--surface, #fff); border-top: 1px solid #f1f5f9; padding: 4px 12px 6px;
}
#chatWidgetBox .sp-quick-head { display: flex; align-items: center; justify-content: space-between; min-height: 20px; margin-bottom: 4px; }
#chatWidgetBox .sp-quick-title { font-size: 11px; font-weight: 600; color: #94a3b8; line-height: 1; }
#chatWidgetBox .sp-quick-close {
  width: 20px; height: 20px; padding: 0; border: none; border-radius: 50%; background: #f1f5f9; color: #64748b;
  cursor: pointer; font-size: 14px; line-height: 20px; text-align: center; flex: none;
}
#chatWidgetBox .sp-quick-close:hover { background: #e2e8f0; color: #334155; }
[data-theme="dark"] #chatWidgetBox .sp-quick-close { background: rgba(148, 163, 184, .18); color: #cbd5e1; }
#chatWidgetBox .sp-chips {
  flex: none; display: flex; flex-wrap: wrap; gap: 5px; padding: 0;
  background: transparent; border-top: 0;
}
/* Sau câu hỏi đầu tiên: gợi ý thu thành 1 hàng cuộn ngang để nhường chỗ cho hội thoại */
#chatWidgetBox .sp-chips.compact {
  flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; -ms-overflow-style: none;
  -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 28px), transparent);
          mask-image: linear-gradient(90deg, #000 calc(100% - 28px), transparent);
  padding-right: 28px;
}
#chatWidgetBox .sp-chips.compact::-webkit-scrollbar { display: none; width: 0; height: 0; }
#chatWidgetBox .sp-chips.compact .sp-chip { flex: none; white-space: nowrap; }
#chatWidgetBox .sp-chip {
  border: 1px solid var(--accent-line, #FECACA); background: var(--accent-soft, #FEF2F2); color: var(--accent-2, #B91C1C);
  border-radius: 14px; padding: 4px 10px; font: 500 12px/1.3 'Be Vietnam Pro', system-ui, sans-serif; cursor: pointer;
  transition: background .15s;
}
#chatWidgetBox .sp-chip:hover:not(:disabled) { filter: brightness(.97); }
#chatWidgetBox .sp-chip:disabled { opacity: .55; cursor: not-allowed; }

/* Ô nhập + nút gửi */
#chatWidgetBox .sp-input {
  flex: none; display: flex; gap: 8px; padding: 10px 12px; margin: 0;
  background: var(--surface, #fff); border-top: 1px solid var(--line, #E2E8F0);
}
#chatWidgetBox .sp-input input {
  flex: 1 1 auto; min-width: 0; height: 38px; padding: 6px 12px; font-size: 13px;
  border: 1px solid var(--line, #CBD5E1); border-radius: 10px; background: var(--surface, #fff); color: var(--ink, #0F172A); outline: none;
}
#chatWidgetBox .sp-input input:focus { border-color: var(--accent, #DC2626); box-shadow: 0 0 0 3px var(--accent-soft, rgba(220, 38, 38, .12)); }
#chatWidgetBox .sp-send {
  flex: none; width: 40px; height: 38px; border: 0; border-radius: 10px; cursor: pointer;
  background: var(--accent, #DC2626); color: #fff; display: grid; place-items: center; font-size: 16px;
}
#chatWidgetBox .sp-send:hover:not(:disabled) { background: var(--accent-2, #B91C1C); }
#chatWidgetBox .sp-send:disabled { background: #94A3B8; cursor: not-allowed; }

@media (max-width: 480px) {
  #chatWidgetBox .sp-ai { height: min(420px, calc(100vh - 250px)); }
}

/* ---------- 4. v1.2: giao diện sắc nét hơn ---------- */
/* Tab: icon SVG nét mảnh */
#chatWidgetBox .sp-tab { display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
#chatWidgetBox .sp-tab svg { flex: none; display: block; }

/* Hàng tin nhắn: avatar KS + bong bóng */
#chatWidgetBox .sp-row { display: flex; align-items: flex-start; gap: 8px; max-width: 100%; animation: spIn .18s ease-out; }
#chatWidgetBox .sp-row.user { justify-content: flex-end; }
#chatWidgetBox .sp-row.bot .sp-msg { max-width: calc(100% - 36px); }
#chatWidgetBox .sp-row.user .sp-msg { max-width: 85%; }
#chatWidgetBox .sp-row .sp-msg.bot, #chatWidgetBox .sp-row .sp-msg.user { align-self: auto; }
#chatWidgetBox .sp-av {
  flex: none; width: 28px; height: 28px; margin-top: 0;
  box-shadow: 0 0 0 2px var(--bg-soft, #F8FAFC);
}
@keyframes spIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
#chatWidgetBox .sp-msg ol { margin: 2px 0 6px; padding-left: 20px; }

/* Typing: 3 chấm nhấp nhô (không còn chữ tĩnh) */
#chatWidgetBox .sp-typing { gap: 4px; padding: 11px 14px; min-height: 0; }
#chatWidgetBox .sp-typing span.sp-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; margin: 0; }
#chatWidgetBox .sp-typing i { width: 6px; height: 6px; animation: spBounce 1.2s infinite ease-in-out both; }
@keyframes spBounce { 0%, 70%, 100% { opacity: .35; transform: translateY(0); } 35% { opacity: 1; transform: translateY(-4px); } }

/* Nút "Chuyển sang Liên hệ KSNB" có icon SVG */
#chatWidgetBox .sp-handoff { gap: 6px; }
#chatWidgetBox .sp-handoff svg { flex: none; display: block; }

/* ---------- Nút nổi "Trợ lý KSNB" ---------- */
#chatWidgetBtn.sp-fab {
  position: fixed; display: inline-flex; align-items: center; gap: 0; height: 44px; padding: 0 16px 0 9px;
  /* v1.6.0: nút là <button type="button"> thật -> bỏ kiểu mặc định của trình duyệt */
  margin: 0; font-family: inherit; line-height: 1; text-align: left; -webkit-appearance: none; appearance: none; cursor: pointer;
  border: 0; border-radius: 999px; color: #fff; user-select: none; -webkit-tap-highlight-color: transparent;
  background: linear-gradient(135deg, #d32f2f, #b71c1c);
  box-shadow: 0 8px 22px rgba(183, 28, 28, .38), 0 2px 6px rgba(15, 23, 42, .18);
  transition: padding .22s ease, background .2s ease, box-shadow .2s ease, transform .2s ease;
}
#chatWidgetBtn.sp-fab:hover { background: linear-gradient(135deg, #e53935, #c62828); transform: translateY(-2px); box-shadow: 0 12px 26px rgba(183, 28, 28, .45), 0 2px 6px rgba(15, 23, 42, .2); }
[data-theme="dark"] #chatWidgetBtn.sp-fab, [data-theme="dark"] #chatWidgetBtn.sp-fab:hover { border: 0; }
#chatWidgetBtn.sp-fab:focus-visible { outline: 3px solid rgba(255, 255, 255, .85); outline-offset: 2px; box-shadow: 0 0 0 5px rgba(211, 47, 47, .55); }
#chatWidgetBtn .sp-fab-ic { display: inline-flex; align-items: center; justify-content: center; flex: none; line-height: 0; color: #fff; }
#chatWidgetBtn .sp-ic-down { display: none; }
#chatWidgetBtn .sp-fab-txt {
  margin-left: 8px; max-width: 150px; overflow: hidden; white-space: nowrap;
  transition: max-width .24s ease, opacity .18s ease, margin .24s ease;
}
/* Chấm online xanh lá, nhịp thở */
#chatWidgetBtn .sp-fab-dot {
  position: absolute; top: -2px; right: -2px; width: 11px; height: 11px; border-radius: 50%;
  background: #22c55e; box-shadow: 0 0 0 2px #fff; animation: spPulse 2.2s ease-in-out infinite;
}
@keyframes spPulse {
  0%   { box-shadow: 0 0 0 2px #fff, 0 0 0 2px rgba(34, 197, 94, .55); }
  70%  { box-shadow: 0 0 0 2px #fff, 0 0 0 9px rgba(34, 197, 94, 0); }
  100% { box-shadow: 0 0 0 2px #fff, 0 0 0 2px rgba(34, 197, 94, 0); }
}
/* Khi khung hỗ trợ đang mở: thu thành nút tròn có mũi tên đóng xuống */
#chatWidgetBtn.sp-fab.is-open { padding: 0 11px; }
#chatWidgetBtn.is-open .sp-ic-spark { display: none; }
#chatWidgetBtn.is-open .sp-ic-down { display: inline-flex; }
#chatWidgetBtn.is-open .sp-fab-txt { max-width: 0; opacity: 0; margin-left: 0; }
#chatWidgetBtn.is-open .sp-fab-dot { display: none; }

@media (max-width: 767px) {
  /* cv.css đã ẩn nhãn ở màn hình nhỏ → nút tròn chỉ có icon */
  #chatWidgetBtn.sp-fab { padding: 0 9px; }
  #chatWidgetBtn.sp-fab.is-open { padding: 0 11px; }
  #chatWidgetBtn .sp-fab-txt { margin-left: 0; }
}
@media (prefers-reduced-motion: reduce) {
  #chatWidgetBtn .sp-fab-dot, #chatWidgetBox .sp-typing i, #chatWidgetBox .sp-row { animation: none; }
}

/* ---------- 5. v1.3: nhãn mã văn bản trong câu trả lời, vd [QC-KHO-01] ---------- */
#chatWidgetBox .sp-ref {
  display: inline-block; margin: 0 2px 0 0; padding: 1px 7px; border-radius: 6px; vertical-align: baseline;
  background: var(--accent-soft, #FEF2F2); border: 1px solid var(--accent-line, #FECACA); color: var(--accent-2, #B91C1C);
  font: 700 11.5px/1.5 'Be Vietnam Pro', system-ui, sans-serif; letter-spacing: .01em; white-space: nowrap;
}
#chatWidgetBox .sp-msg strong .sp-ref { font-weight: 700; }

/* ---------- 6. v1.4: ảnh linh vật Trợ lý ảo KSNB ---------- */
/* Khung tròn cắt gọn góc trắng của ảnh gốc: phóng nhẹ 6% để vòng vàng ôm khít khung */
.sp-mascot { display: inline-block; flex: none; position: relative; border-radius: 50%; overflow: hidden; line-height: 0; background: #FFE27A; }
.sp-mascot img { display: block; width: 100%; height: 100%; object-fit: cover; transform: scale(1.06); border-radius: 50%; }
/* Header 42px + chấm online xanh */
#chatWidgetBox .sp-head-av { position: relative; flex: none; width: 42px; height: 42px; display: inline-block; }
#chatWidgetBox .sp-head-av .sp-mascot { width: 42px; height: 42px; box-shadow: 0 3px 10px rgba(211, 47, 47, .25); }
#chatWidgetBox .sp-head-dot {
  position: absolute; right: -2px; bottom: -2px; width: 10px; height: 10px; border-radius: 50%;
  background: #22c55e; border: 2px solid #fff; box-sizing: content-box;
}
/* Avatar nhỏ cạnh tin nhắn bot (kể cả lúc "đang trả lời") */
#chatWidgetBox .sp-av.sp-mascot { width: 28px; height: 28px; }
/* Nút nổi: avatar 26px, chữ "Trợ lý KSNB" */
#chatWidgetBtn .sp-fab-ic.sp-mascot { width: 26px; height: 26px; box-shadow: 0 0 0 1.5px rgba(255, 255, 255, .9); }
