/* AccTube — khung Live Chat (AI + nhân viên) dùng chung cho nút nổi, nút "Live Chat" ở header và trang /live-chat.
   Chỉ dùng token --at-* (acctube-tokens.css). Desktop: popup ~380×560 neo góc phải dưới; < 576px: tấm toàn màn hình.
   Trang /live-chat: .at-chat--page (nhúng trong nội dung, không cố định). */

.at-chat {
  position: fixed; right: 24px; bottom: 24px; z-index: 1016; /* trên nút nổi (1015), dưới drawer (1100) / modal (1055) */
  width: 380px; height: 560px; max-height: calc(100vh - 48px); max-height: calc(100dvh - 48px);
  display: flex; flex-direction: column; overflow: hidden;
  background: var(--at-card); color: var(--at-text);
  border: 1px solid var(--at-border-strong); border-radius: var(--at-radius-lg);
  box-shadow: var(--at-shadow-lg);
  font-family: var(--at-font); font-size: var(--at-fs-sm); line-height: var(--at-lh);
  transform-origin: bottom right;
  animation: at-chat-in 160ms ease-out;
}
.at-chat[hidden] { display: none !important; }
/* ar: nội dung RTL nhưng khung vẫn neo góc phải (cùng góc với nút nổi) */
@keyframes at-chat-in { from { opacity: 0; transform: translateY(8px) scale(.98); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .at-chat { animation: none; } .at-chat-typing i { animation: none !important; } }
body.pd-has-buybar .at-chat:not(.at-chat--page) { bottom: calc(84px + env(safe-area-inset-bottom)); max-height: calc(100dvh - 108px); }

/* thu nhỏ: chỉ còn thanh tiêu đề */
.at-chat.is-min { height: auto; }
.at-chat.is-min .at-chat-body, .at-chat.is-min .at-chat-foot, .at-chat.is-min .at-chat-sub { display: none; }

/* --- đầu khung --- */
.at-chat-head {
  display: flex; align-items: center; gap: 10px; padding: 12px 8px 12px 14px;
  background: var(--at-bg-elev); border-bottom: 1px solid var(--at-border); flex: 0 0 auto;
}
.at-chat-head-av {
  position: relative; flex: 0 0 36px; width: 36px; height: 36px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--at-accent-soft); color: var(--at-accent); border: 1px solid var(--at-accent-soft-strong);
}
.at-chat-head-av::after { /* chấm "đang trực" */
  content: ""; position: absolute; right: -1px; bottom: -1px; width: 10px; height: 10px; border-radius: 50%;
  background: var(--at-success); border: 2px solid var(--at-bg-elev);
}
.at-chat-head-txt { min-width: 0; flex: 1 1 auto; }
.at-chat-title { margin: 0; font-size: 15px; font-weight: var(--at-fw-semibold); color: var(--at-text); line-height: 1.25;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.at-chat-sub { margin: 2px 0 0; font-size: var(--at-fs-xs); color: var(--at-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.at-chat-hbtn {
  flex: 0 0 auto; width: 36px; height: 36px; border-radius: var(--at-radius-sm); border: 0; padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  background: transparent; color: var(--at-muted); cursor: pointer;
}
.at-chat-hbtn:hover { background: var(--at-surface-hover); color: var(--at-text); }
.at-chat-hbtn:focus-visible, .at-chat-send:focus-visible, .at-chat-link:focus-visible { outline: none; box-shadow: var(--at-ring); }
.at-chat--page .at-chat-hbtn { display: none; }

/* --- thân: danh sách tin --- */
.at-chat-body { position: relative; flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; background: var(--at-card); }
.at-chat-log {
  flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  padding: 14px 14px 6px; display: flex; flex-direction: column; gap: 10px;
  scrollbar-width: thin; scrollbar-color: var(--at-border-strong) transparent;
}
.at-chat-log:focus-visible { outline: none; box-shadow: inset 0 0 0 2px var(--at-accent); }
.at-chat-more { align-self: center; }

.at-chat-day { align-self: center; font-size: 11px; color: var(--at-faint); padding: 2px 10px; border-radius: var(--at-radius-pill); background: var(--at-raised); }

.at-chat-msg { display: flex; gap: 8px; max-width: 100%; align-items: flex-start; }
.at-chat-msg:not(.is-me) > .at-chat-av { margin-top: 19px; } /* ngang bong bóng (dưới dòng tên) */
.at-chat-typing-row > .at-chat-av { margin-top: 2px; }
.at-chat-msg.is-me { flex-direction: row-reverse; }
.at-chat-av {
  flex: 0 0 28px; width: 28px; height: 28px; border-radius: 50%; overflow: hidden;
  display: inline-flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 600;
  background: var(--at-raised); color: var(--at-text-2); border: 1px solid var(--at-border);
}
.at-chat-av img { width: 100%; height: 100%; object-fit: cover; }
.at-chat-av.is-ai { background: var(--at-accent-soft); color: var(--at-accent); border-color: var(--at-accent-soft-strong); }
.at-chat-col { min-width: 0; max-width: 82%; display: flex; flex-direction: column; align-items: flex-start; }
.at-chat-msg.is-me .at-chat-col { align-items: flex-end; }
.at-chat-who { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--at-muted); margin: 0 0 3px 2px; max-width: 100%; }
.at-chat-who b { font-weight: 600; color: var(--at-text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.at-chat-badge {
  display: inline-flex; align-items: center; gap: 3px; padding: 1px 6px; border-radius: var(--at-radius-pill);
  font-size: 10px; font-weight: 600; letter-spacing: .02em; white-space: nowrap;
  background: var(--at-accent-soft); color: var(--at-accent); border: 1px solid var(--at-accent-soft-strong);
}
.at-chat-badge.is-staff { background: var(--at-success-soft); color: var(--at-success); border-color: var(--at-success-border); }
.at-chat-bubble {
  padding: 8px 12px; border-radius: 14px; border-bottom-left-radius: 4px;
  background: var(--at-raised); color: var(--at-text); border: 1px solid var(--at-border);
  white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word; font-size: var(--at-fs-sm);
}
.at-chat-msg.is-ai .at-chat-bubble { background: var(--at-bg-elev); border-color: var(--at-accent-soft-strong); }
.at-chat-msg.is-me .at-chat-bubble {
  background: var(--at-accent); color: var(--at-accent-ink); border-color: var(--at-accent);
  border-bottom-left-radius: 14px; border-bottom-right-radius: 4px;
}
[dir="rtl"] .at-chat-bubble { border-bottom-left-radius: 14px; border-bottom-right-radius: 4px; }
[dir="rtl"] .at-chat-msg.is-me .at-chat-bubble { border-bottom-right-radius: 14px; border-bottom-left-radius: 4px; }
.at-chat-bubble a { color: inherit; text-decoration: underline; }
.at-chat-bubble img { display: block; max-width: 100%; max-height: 220px; border-radius: var(--at-radius-sm); margin-top: 4px; }
.at-chat-time { font-size: 10.5px; color: var(--at-faint); margin: 3px 4px 0; }
.at-chat-msg.is-pending .at-chat-bubble { opacity: .7; }
.at-chat-msg.is-failed .at-chat-bubble { border-color: var(--at-danger-border); }
.at-chat-retry { border: 0; background: none; padding: 0; color: var(--at-danger); font-size: 11px; cursor: pointer; text-decoration: underline; margin: 3px 4px 0; }

/* tin hệ thống (chuyển nhân viên…) */
.at-chat-sys {
  align-self: center; max-width: 92%; text-align: center;
  font-size: var(--at-fs-xs); color: var(--at-warning); background: var(--at-warning-soft);
  border: 1px solid var(--at-warning-border); border-radius: var(--at-radius); padding: 6px 12px;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.at-chat-sys.is-info { color: var(--at-info); background: var(--at-info-soft); border-color: var(--at-info-border); }

/* lời chào khi chưa có tin */
.at-chat-empty { margin: auto 4px; text-align: center; color: var(--at-text-2); padding: 18px 8px; }
.at-chat-empty-ic { width: 48px; height: 48px; margin: 0 auto 10px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  background: var(--at-accent-soft); color: var(--at-accent); }
.at-chat-empty h3 { font-size: 16px; font-weight: 600; color: var(--at-text); margin: 0 0 6px; }
.at-chat-empty p { margin: 0; font-size: 13px; color: var(--at-muted); }
.at-chat-chips { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; margin-top: 14px; }
.at-chat-chip {
  border: 1px solid var(--at-border-strong); background: var(--at-bg-elev); color: var(--at-text-2);
  border-radius: var(--at-radius-pill); padding: 6px 12px; font-size: 12.5px; cursor: pointer; min-height: 32px;
}
.at-chat-chip:hover { border-color: var(--at-accent); color: var(--at-accent); }
.at-chat-chip:focus-visible { outline: none; box-shadow: var(--at-ring); }

/* đang soạn (AI) */
.at-chat-typing { display: inline-flex; gap: 4px; align-items: center; padding: 10px 12px; }
.at-chat-typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--at-muted); animation: at-chat-dot 1.2s infinite ease-in-out; }
.at-chat-typing i:nth-child(2) { animation-delay: .15s; }
.at-chat-typing i:nth-child(3) { animation-delay: .3s; }
@keyframes at-chat-dot { 0%, 80%, 100% { opacity: .3; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }

.at-chat-jump {
  position: absolute; left: 50%; bottom: 8px; transform: translateX(-50%);
  border: 1px solid var(--at-border-strong); background: var(--at-bg-elev); color: var(--at-text);
  border-radius: var(--at-radius-pill); padding: 4px 12px; font-size: 12px; cursor: pointer; box-shadow: var(--at-shadow);
}
.at-chat-jump[hidden] { display: none; }

.at-chat-err { margin: 0 14px 6px; font-size: 12px; color: var(--at-danger); }
.at-chat-err[hidden] { display: none; }

/* --- chân: ô soạn --- */
.at-chat-foot { flex: 0 0 auto; border-top: 1px solid var(--at-border); background: var(--at-bg-elev); padding: 10px 10px calc(10px + env(safe-area-inset-bottom)); }
.at-chat-form { display: flex; align-items: flex-end; gap: 8px; }
.at-chat-input {
  flex: 1 1 auto; min-width: 0; resize: none; max-height: 120px; min-height: 40px;
  padding: 9px 12px; border-radius: var(--at-radius); border: 1px solid var(--at-border-strong);
  background: var(--at-card); color: var(--at-text); font: inherit; font-size: var(--at-fs-sm); line-height: 1.4;
}
.at-chat-input::placeholder { color: var(--at-faint); }
.at-chat-input:focus { outline: none; border-color: var(--at-accent); box-shadow: var(--at-ring-input); }
.at-chat-send {
  flex: 0 0 40px; width: 40px; height: 40px; border-radius: var(--at-radius); border: 0; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--at-accent); color: var(--at-accent-ink);
}
.at-chat-send:hover { background: var(--at-accent-strong); }
.at-chat-send:disabled { background: var(--at-raised); color: var(--at-disabled-text); cursor: not-allowed; }
[dir="rtl"] .at-chat-send svg { transform: scaleX(-1); }
.at-chat-note { margin: 6px 2px 0; font-size: 11.5px; color: var(--at-faint); display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.at-chat-link { color: var(--at-accent); text-decoration: none; border-radius: 4px; }
.at-chat-link:hover { text-decoration: underline; color: var(--at-accent); }

.at-chat-sr { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

/* khung mở ⇒ ẩn nút nổi + nút lên đầu trang (desktop vẫn thấy được nút chat ở header) */
html.at-chat-open .at-chat-fab, html.at-chat-open .circle-container { opacity: 0; pointer-events: none; }

/* --- mobile: tấm toàn màn hình --- */
@media (max-width: 575.98px) {
  .at-chat:not(.at-chat--page) {
    inset: 0; right: 0; left: 0; bottom: 0; top: 0; width: 100%; height: 100%; max-height: none;
    border-radius: 0; border: 0; z-index: 1101; /* trên drawer mobile */
  }
  body.pd-has-buybar .at-chat:not(.at-chat--page) { bottom: 0; max-height: none; }
  .at-chat:not(.at-chat--page) .at-chat-min { display: none; }
  html.at-chat-open.at-chat-sheet, html.at-chat-open.at-chat-sheet body { overflow: hidden; }
  .at-chat-input { font-size: var(--at-fs-md); } /* chống zoom iOS */
  .at-chat-col { max-width: 86%; }
}

/* --- trang /live-chat: nhúng trong trang --- */
.at-chat-page-wrap { max-width: 760px; margin: 0 auto; padding: 16px; }
.at-chat.at-chat--page {
  position: relative; right: auto; left: auto; bottom: auto; z-index: auto;
  width: 100%; height: calc(100vh - 170px); height: calc(100dvh - 170px); min-height: 420px; max-height: none; animation: none;
}
@media (max-width: 575.98px) {
  .at-chat-page-wrap { padding: 0; }
  .at-chat.at-chat--page { border-radius: 0; border-left: 0; border-right: 0; height: calc(100dvh - 120px); }
}
