/* =====================================================================
 * chatbot.css — style cho widget "Trợ lý AI" (góc dưới-phải, mở từ #chatbotBtn)
 * ===================================================================== */

/* Khi panel đang mở: ẩn luôn 2 nút góc dưới-phải (#chatbotBtn/#scrollTopBtn) để tránh
   chồng lên panel — cùng cách các panel khác (Bộ nhớ nhân vật/Cài đặt) đã ẩn #scrollTopBtn. */
body.chatbot-open #chatbotBtn,
body.chatbot-open #scrollTopBtn {
  display: none;
}

/* ---------- Chatbot Reviewer nhúng (xem window.NT_REVIEWER_EMBED trong reviewer-chatbot.js) ----------
 * Panel #rvChatPanel do reviewer-chatbot.js dựng, mở thay chỗ #chatbotPanel qua chip "Review truyện".
 * Giữ nguyên kích thước/bố cục panel đó (chạy y như bên blog) — chỉ: ẩn nút tròn + scrollTop của công
 * cụ dịch trong lúc panel Reviewer đang mở (giống cách #chatbotPanel đã làm ở trên), nâng z-index ngang
 * #chatbotPanel để không bị các panel khác của trang che mất, canh lề phải/dưới khớp #chatbotPanel (chỉ
 * ở dạng nổi trên desktop/tablet — xem khối @media bên dưới, không đụng bottom sheet mobile vốn đã
 * left:0/right:0 sẵn khớp nhau rồi), và ẩn/né panel Reviewer giống #chatbotPanel khi panel khác (Cài
 * đặt/Bộ nhớ nhân vật) hoặc thanh nghe TTS đang mở.
 */
body.rvc-open #chatbotBtn,
body.rvc-open #scrollTopBtn {
  display: none;
}
#rvChatPanel {
  z-index: 870;
}
body.tts-open #rvChatPanel {
  bottom: calc(var(--tts-bar-h, 130px) + 33px + env(safe-area-inset-bottom, 0px));
}
body.charmem-open #rvChatPanel,
body.settings-open #rvChatPanel {
  display: none;
}
/* Bên blog, panel nổi cách lề phải 20px/dưới 30px (reviewer-chatbot.css); bên công cụ dịch đổi cho
   khớp đúng lề của #chatbotPanel (42px/42px) — chỉ ở >=681px vì dưới mức đó cả 2 panel đều là bottom
   sheet full-width (left:0;right:0) nên không còn "lề phải/dưới" nào để canh nữa. */
@media (min-width: 681px) {
  #rvChatPanel {
    right: calc(42px + env(safe-area-inset-right, 0px));
    bottom: calc(42px + env(safe-area-inset-bottom, 0px));
  }
  body.tts-open #rvChatPanel {
    bottom: calc(var(--tts-bar-h, 130px) + 45px + env(safe-area-inset-bottom, 0px));
  }
}

#chatbotPanel {
  position: fixed;
  bottom: calc(42px + env(safe-area-inset-bottom, 0px));
  right: calc(42px + env(safe-area-inset-right, 0px));
  z-index: 870;
  width: 366px;
  max-width: 92vw;
  height: 540px;
  max-height: 72vh;
  max-height: 72dvh; /* dvh = chiều cao vùng nhìn thấy thật (trừ thanh địa chỉ mobile); trình duyệt cũ bỏ qua dòng này, dùng vh ở trên */
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.21);
  display: none;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(14px) scale(0.97);
  transform-origin: bottom right;
  transition: opacity 0.18s ease, transform 0.18s ease, bottom 0.15s ease;
}
#chatbotPanel.open {
  display: flex;
  opacity: 1;
  transform: translateY(0) scale(1);
  animation: cbPanelSlideUp 0.26s ease;
}
/* Đẩy từ dưới lên khi mở panel — chạy song song với transition ở trên nên vẫn mượt khi
   đóng lại (đóng chỉ dựa vào transition, không cần animation ngược). */
@keyframes cbPanelSlideUp {
  from {
    opacity: 0;
    transform: translateY(36px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}
/* Thanh audio TTS mở: đẩy panel lên khớp --tts-bar-h (biến do tts.js đo/ghi động), y hệt cách
   #scrollTopBtn/#chatbotBtn đã được đẩy — xem tts.css. */
body.tts-open #chatbotPanel {
  bottom: calc(var(--tts-bar-h, 130px) + 45px + env(safe-area-inset-bottom, 0px));
}
/* Ẩn hẳn khi panel khác đang mở (Bộ nhớ nhân vật / Cài đặt) — tránh 2 lớp nổi chồng nhau. */
body.charmem-open #chatbotPanel,
body.settings-open #chatbotPanel {
  display: none;
}

/* ---------- Nút "Mở rộng" (#cbExpandBtn ở header): desktop phủ hết chiều cao ở bên phải màn hình
   (neo sát mép phải, từ trên xuống dưới); mobile/tablet phủ TOÀN màn hình — xem khối @media
   (max-width: 680px) bên dưới cho phần mobile. Đặt SAU khối body.tts-open ở trên để khi cả 2 cùng
   bật (đang mở thanh nghe truyện + bấm Mở rộng) thì Mở rộng thắng, không bị lệch theo thanh audio. */
@media (min-width: 681px) {
  body.chatbot-expanded #chatbotPanel {
    top: 0;
    right: 0;
    bottom: 0;
    width: min(480px, 42vw);
    max-width: 92vw;
    height: auto;
    max-height: none;
    border-radius: 0;
    padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) 0;
  }
}

/* ---------- Header ---------- */
.cb-head {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 14px 14px 12px 16px;
  border-bottom: 1px solid var(--border);
}
.cb-head-icon {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
}
.cb-head-icon svg {
  width: 100%;
  height: 100%;
  display: block;
}
.cb-head-text {
  flex: 1;
  min-width: 0;
}
.cb-title {
  font-size: 14.5px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.2px;
}
.cb-beta {
  font-size: 10.5px;
  font-style: italic;
  font-weight: 500;
  color: var(--text3);
  letter-spacing: 0;
}
.cb-subtitle {
  font-size: 11.5px;
  color: var(--text3);
  margin-top: 1px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cb-close {
  width: 30px;
  height: 30px;
  flex-shrink: 0;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 50%;
  color: var(--text3);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.cb-close:hover {
  background: var(--btn-hover);
  color: var(--text);
}

/* ---------- Messages ---------- */
.cb-msgs {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain; /* cuộn hết khung chat không "kéo" theo trang phía sau */
  padding: 14px 14px 6px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.cb-msgs::-webkit-scrollbar {
  width: 6px;
}
.cb-msgs::-webkit-scrollbar-thumb {
  background: var(--border2);
  border-radius: 8px;
}
.cb-msgs::-webkit-scrollbar-track {
  background: transparent;
}
.cb-row {
  display: flex;
  flex-direction: column;
}
.cb-row.user {
  align-items: flex-end;
}
.cb-row.model {
  align-items: flex-start;
}
.cb-bubble {
  max-width: 82%;
  padding: 9px 13px;
  border-radius: 14px;
  font-size: 13.5px;
  line-height: 1.55;
  word-break: break-word;
  white-space: normal;
}
.cb-bubble.user {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.cb-bubble.model {
  background: var(--bg2);
  border: 1px solid var(--border);
  color: var(--text);
  border-bottom-left-radius: 4px;
}
.cb-bubble a {
  color: var(--accent);
  text-decoration: underline;
  word-break: break-all;
}
/* Bong bóng của user có nền accent sẵn — giữ link màu trắng để không bị chìm vào nền */
.cb-bubble.user a {
  color: #fff;
}

/* ---------- Câu hỏi gợi ý (chip) — hiện dưới tin nhắn chào mừng lúc mới mở panel ---------- */
.cb-suggest-wrap {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  margin-top: 2px;
}
.cb-suggest-btn {
  max-width: 82%;
  padding: 8px 14px;
  border-radius: 14px;
  border-bottom-right-radius: 4px;
  border: 1px solid var(--accent);
  background: transparent;
  color: var(--accent);
  font-size: 12.5px;
  font-family: inherit;
  font-weight: 600;
  line-height: 1.4;
  text-align: right;
  cursor: pointer;
  white-space: normal;
  transition: background 0.15s, color 0.15s;
}
.cb-suggest-btn:hover {
  background: var(--accent);
  color: #fff;
}
/* Darkmode: chữ màu accent trên nền tối kém rõ hơn — đổi sang var(--text) cho dễ đọc, viền vẫn
   giữ màu accent để phân biệt với bong bóng chat thường. */
[data-theme="dark"] .cb-suggest-btn {
  color: var(--text);
}
[data-theme="dark"] .cb-suggest-btn:hover {
  color: #fff;
}

.cb-typing {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 12px 14px;
}
.cb-typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text4);
  animation: cbTypingBounce 1.1s infinite ease-in-out;
}
.cb-typing span:nth-child(2) {
  animation-delay: 0.15s;
}
.cb-typing span:nth-child(3) {
  animation-delay: 0.3s;
}
@keyframes cbTypingBounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.5;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

/* ---------- Input row ---------- */
.cb-input-row {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg3);
}
/* Bọc textarea để đặt nút mic NẰM ĐÈ vào bên trong ô nhập (góc dưới-phải), giống kiểu ô chat
   của Claude — thay vì 1 nút rời đứng cạnh ô nhập. */
.cb-input-wrap {
  /* Dùng grid stack: textarea và #cbMicBtn cùng nằm chung 1 ô lưới (thay vì mic định vị
     absolute theo top/bottom/margin — cách cũ vẫn có thể lệch nếu chiều cao thật của
     textarea khác con số ta tưởng). Nhờ chung 1 ô lưới, mic luôn được canh giữa CHÍNH XÁC
     theo chiều cao THẬT của textarea tại mọi thời điểm, kể cả khi textarea giãn nhiều dòng. */
  display: grid;
  flex: 1;
  min-width: 0;
}
.cb-input-wrap textarea,
.cb-input-wrap #cbMicBtn {
  grid-area: 1 / 1;
}
.cb-input-wrap textarea {
  width: 100%;
  box-sizing: border-box;
  resize: none;
  height: 36px; /* khớp đúng chiều cao nút Gửi (36px) */
  min-height: 36px;
  max-height: 110px;
  padding: 9px 38px 9px 12px; /* chừa chỗ bên phải cho nút mic đè lên, tránh đè lên chữ đang gõ */
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--input-bg);
  color: var(--text);
  font-size: 13.5px;
  font-family: inherit;
  line-height: 1.45;
  outline: none;
  transition: border-color 0.15s;
  /* Ẩn thanh cuộn dọc riêng cho ô này — CSS toàn app có ::-webkit-scrollbar{width:6px} áp dụng
     chung cho mọi phần tử cuộn được, khiến textarea hiện 1 vệt cuộn dọc mảnh ngay cả lúc chỉ 1
     dòng (do scrollHeight lệch 1px so với clientHeight vì bo tròn line-height/padding) — vệt này
     biến mất khi ô cao lên vừa khít nội dung, đúng hiện tượng bạn thấy. Vẫn cuộn được bình thường
     khi vượt max-height, chỉ là không hiện thanh cuộn (giữ overflow-y mặc định auto).
  */
  scrollbar-width: none; /* Firefox */
}
.cb-input-wrap textarea::-webkit-scrollbar {
  display: none; /* Chrome/Edge/Safari */
}
.cb-input-wrap textarea:focus {
  border-color: var(--accent);
}
.cb-input-wrap textarea::placeholder {
  color: var(--text4);
}
#cbSendBtn {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, opacity 0.15s;
}
/* Icon mũi tên gửi lệch tâm hình học 1 chút do hình mũi tên (polygon) không cân — nắn lại
   xuống 2px, sang trái 1px cho có vẻ nằm đúng giữa nút tròn. */
#cbSendBtn svg {
  position: relative;
  top: 1px;
  left: 0px;
}
#cbSendBtn:hover {
  background: #4f52d9;
}
#cbSendBtn:disabled {
  opacity: 0.5;
  cursor: default;
}

/* ---------- Nút Micro (nhập bằng giọng nói) — nằm ĐÈ bên trong ô nhập, góc dưới-phải ---------- */
#cbMicBtn {
  /* Canh giữa bằng grid (align-self/justify-self) thay vì absolute+top/bottom — xem
     comment ở .cb-input-wrap phía trên. */
  align-self: center;
  justify-self: end;
  margin-right: 4px;
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text3);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}
#cbMicBtn:hover {
  background: var(--btn-hover);
  color: var(--text);
}
#cbMicBtn:disabled {
  opacity: 0.4;
  cursor: default;
}
/* Đang ghi âm: nền đỏ + chấm nhấp nháy nhẹ để người dùng biết mic đang mở, bấm lại để dừng. */
#cbMicBtn.recording {
  background: #e5484d;
  color: #fff;
  animation: cbMicPulse 1.3s infinite ease-in-out;
}
@keyframes cbMicPulse {
  0%, 100% {
    box-shadow: 0 0 0 0 rgba(229, 72, 77, 0.45);
  }
  50% {
    box-shadow: 0 0 0 6px rgba(229, 72, 77, 0);
  }
}

/* ---------- Mobile: bottom sheet full-width thay vì cửa sổ nhỏ góc dưới-phải ---------- */
@media (max-width: 680px) {
  #chatbotPanel {
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-width: none;
    height: min(78vh, 620px);
    height: min(78dvh, 620px);
    max-height: 82vh;
    max-height: 82dvh;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
    padding-bottom: env(safe-area-inset-bottom);
    transform: translateY(100%);
    transform-origin: bottom center;
  }
  #chatbotPanel.open {
    transform: translateY(0);
  }
  body.tts-open #chatbotPanel {
    bottom: 0; /* full-width sheet đứng dưới cùng, không cần né thanh audio (audio bar nằm TRÊN sheet khi mở) */
  }
  /* Bấm "Mở rộng" trên mobile/tablet: phủ toàn màn hình thay vì bottom sheet — cùng cơ chế
     .chatbot-expanded như bản desktop ở trên, chỉ khác cách áp dụng cho layout mobile. */
  body.chatbot-expanded #chatbotPanel {
    top: 0;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    border-radius: 0;
    padding-top: env(safe-area-inset-top, 0px);
  }
}

/* ---------- Kết quả thao tác do Trợ lý tự thực hiện (dòng [[ACTION]] được chatbot.js bóc ra) ---------- */
.cb-actions {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 9px;
}
.cb-act {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 7px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text2);
  font-size: 12.5px;
  line-height: 1.4;
}
.cb-act.ok {
  border-color: rgba(29, 154, 91, 0.45);
  color: var(--good, #1d9a5b);
}
.cb-act.err {
  border-color: rgba(229, 72, 77, 0.45);
  color: var(--danger, #e5484d);
}
.cb-act.busy {
  color: var(--text3);
}
/* Vòng xoay nhỏ trước dòng "Đang ...": báo cho người dùng biết app đang chạy, không bị đơ */
.cb-act.busy .cb-act-msg::before {
  content: '';
  display: inline-block;
  width: 11px;
  height: 11px;
  margin-right: 7px;
  vertical-align: -1px;
  border: 2px solid var(--border2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: cbActSpin 0.8s linear infinite;
}
@keyframes cbActSpin {
  to {
    transform: rotate(360deg);
  }
}
.cb-act.skip {
  color: var(--text3);
}
.cb-act-msg {
  flex: 1;
  min-width: 140px;
}
.cb-act-btn {
  flex-shrink: 0;
  padding: 5px 12px;
  border-radius: 8px;
  border: 1px solid var(--border2);
  background: var(--btn-bg);
  color: var(--text);
  font-size: 12.5px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s, filter 0.15s;
}
.cb-act-btn:hover {
  background: var(--btn-hover);
}
.cb-act-btn.primary {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
}
.cb-act-btn.primary:hover {
  background: #4f52d9;
}

/* `code` trong tin nhắn (vd .txt, .epub) */
.cb-bubble code.cb-code {
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--btn-hover);
  font-size: 12.5px;
  font-family: 'Consolas', 'SFMono-Regular', monospace;
}
.cb-bubble.user code.cb-code {
  background: rgba(255, 255, 255, 0.22);
}

/* ---------- Nút phụ dưới mỗi bubble: sao chép (mọi bubble) + gửi lại (chỉ bubble người dùng) ----------
   Đặt NGOÀI .cb-bubble (anh em của nó trong .cb-row), không còn nằm chồng lên nền bubble nữa — xem
   attachCbTools() trong chatbot.js. .cb-row đã là cột (flex-direction:column) với align-items lệch theo
   role nên thanh nút tự canh đúng phía dưới bubble mà không cần class .cb-bubble.user riêng. */
.cb-tools {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-top: 4px;
}
.cb-tool-btn {
  width: 24px;
  height: 24px;
  flex-shrink: 0;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text4);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}
.cb-tool-btn:hover {
  background: var(--btn-hover);
  color: var(--text);
}
.cb-tool-btn.copied {
  color: #22c55e;
}
.cb-tool-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Nút đánh giá 👍/👎 dưới câu trả lời của bot + thẻ liên hệ admin ---------- */
.cb-fb {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-left: 4px;
}
.cb-fb-btn {
  width: 24px;
  height: 24px;
  border: none;
  background: transparent;
  border-radius: 6px;
  color: var(--text4);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}
.cb-fb-btn:hover {
  background: var(--btn-hover);
  color: var(--text);
}
.cb-fb-btn.active {
  color: var(--accent);
}
.cb-fb-note {
  margin-left: 6px;
  font-size: 11.5px;
  color: var(--text3);
}
.cb-contact {
  margin-top: 8px;
}
/* Dòng nhỏ báo bản ghi 👎/"chưa chắc" đã được gửi ẩn danh cho admin (xem renderContactCard trong chatbot.js) */
.cb-contact-note {
  flex-basis: 100%;
  font-size: 11.5px;
  line-height: 1.45;
  color: var(--text3);
}
.cb-bubble a.cb-act-btn {
  display: inline-block;
  text-decoration: none;
  word-break: normal;
  color: var(--text);
}
.cb-bubble a.cb-act-btn.primary {
  color: #fff;
}

/* ===== Chữ phụ trên điện thoại: nâng cỡ < 12px lên 12px+ cho dễ đọc ===== */
@media (max-width: 680px) {
  .cb-beta { font-size: 12px; }
  .cb-subtitle, .cb-fb-note, .cb-contact-note { font-size: 12.5px; }
}

/* ===== Vùng chạm lớn hơn trên thiết bị cảm ứng (icon giữ nguyên kích thước) ===== */
@media (pointer: coarse) {
  /* Copy và 👍/👎 nằm sát nhau nên phóng to hẳn khung nút thay vì dùng ::after chồng lấn */
  .cb-tool-btn, .cb-fb-btn { width: 36px; height: 36px; }
  .cb-tools { gap: 4px; margin-top: 2px; }
  .cb-close, #cbMicBtn { position: relative; }
  .cb-close::after { content: ''; position: absolute; inset: -7px; }
  #cbMicBtn::after { content: ''; position: absolute; inset: -4px; }
  .cb-act-btn { padding: 9px 14px; }
}
