/* Khung chat cho khách trên web (luồng 13): bong bóng nổi + khung hội thoại. Lớp .bhw-chat-*, mobile-first.
   Khối #bhw-chat nằm NGOÀI khối .bhw của trang (in ở chân trang) nên tự mang biến màu: mẫu templates/chat/widget.php in
   --bhw-c / -c-d / -on-c / -a / -on-a / -focus inline theo mẫu màu của cửa hàng; các dòng dưới chỉ là dự phòng (= mẫu mặc định).
   Không che nút giỏ hàng / thanh toán: chat.js gắn .is-tucked khi bong bóng đè lên nút chính, --bhw-chat-lift khi có dock. */

.bhw-chat {
  --bhw-c: var(--bh-primary, #1d5fbf);
  --bhw-c-d: var(--bh-primary-d, color-mix(in srgb, var(--bhw-c) 88%, #000));
  --bhw-on-c: var(--bh-on-primary, #fff);
  --bhw-a: var(--bh-accent, #ffab2e);
  --bhw-on-a: var(--bh-on-accent, #1b2420);
  --bhw-focus: var(--bh-focus, color-mix(in srgb, var(--bhw-c) 50%, transparent));
  --bhw-chat-ink: var(--vc-ink, #1b2420);
  --bhw-chat-muted: var(--vc-muted, #5b6661);
  --bhw-chat-line: var(--vc-line, #dfe4e1);
  --bhw-chat-bg: var(--vc-bg, #fff);
  --bhw-chat-soft: color-mix(in srgb, var(--bhw-c) 8%, #fff);
  --bhw-chat-panel: #f4f6f5;
  --bhw-chat-warn: #b4232a;
  --bhw-chat-warn-l: #fdecec;
  --bhw-chat-info-l: #fff6e5;
  --bhw-chat-r: var(--vc-r, 12px);
  --bhw-chat-r-lg: var(--vc-r-lg, 16px);
  --bhw-chat-gap: 16px;
  --bhw-chat-size: 56px;
  --bhw-chat-lift: 0px;
  --bhw-chat-bottom: calc(var(--bhw-chat-gap) + var(--bhw-chat-lift) + env(safe-area-inset-bottom, 0px));
  position: relative;
  z-index: 9980;
  color: var(--bhw-chat-ink);
  font-size: 15px;
  line-height: 1.45;
}

.bhw-chat[hidden],
.bhw-chat [hidden] {
  display: none !important;
}

.bhw-chat *,
.bhw-chat *::before,
.bhw-chat *::after {
  box-sizing: border-box;
}

.bhw-chat .bhw-i {
  flex: none;
  display: inline-block;
  vertical-align: middle;
}

/* ---------- Bong bóng ---------- */

.bhw-chat-bubble {
  position: fixed;
  right: var(--bhw-chat-gap);
  bottom: var(--bhw-chat-bottom);
  z-index: 9981;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: var(--bhw-chat-size);
  height: var(--bhw-chat-size);
  padding: 0 16px;
  border: 0;
  border-radius: 999px;
  background: var(--bhw-c);
  color: var(--bhw-on-c);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.22);
  transition: transform 0.2s, opacity 0.2s, background-color 0.15s;
}

.bhw-chat-bubble:hover {
  background: var(--bhw-c-d);
}

.bhw-chat-bubble:focus-visible,
.bhw-chat button:focus-visible,
.bhw-chat a:focus-visible,
.bhw-chat-log:focus-visible {
  outline: 3px solid var(--bhw-focus);
  outline-offset: 2px;
}

.bhw-chat-bubble .bhw-i {
  width: 26px;
  height: 26px;
}

/* Điện thoại: chỉ icon, để bong bóng nhỏ gọn. */
.bhw-chat-bubble-label {
  display: none;
}

.bhw-chat.is-left .bhw-chat-bubble {
  right: auto;
  left: var(--bhw-chat-gap);
}

/* Đè lên nút chính của trang (thêm vào giỏ, đặt hàng): lùi ra mép, mờ đi rồi visibility: hidden (ra khỏi thứ tự Tab và cây trợ năng,
   trễ bằng thời gian mờ để còn thấy hiệu ứng) cho tới khi cuộn khỏi nút. */
.bhw-chat.is-tucked .bhw-chat-bubble {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateY(12px) scale(0.9);
  transition: opacity 0.2s ease, transform 0.2s ease, visibility 0s linear 0.2s;
}

.bhw-chat.is-open .bhw-chat-bubble {
  display: none;
}

.bhw-chat-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--bhw-a);
  color: var(--bhw-on-a);
  font-size: 12px;
  font-weight: 700;
  line-height: 22px;
  text-align: center;
  box-shadow: 0 0 0 2px var(--bhw-chat-bg);
}

/* ---------- Khung ---------- */

.bhw-chat-panel {
  position: fixed;
  inset: 0;
  z-index: 9982;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  height: 100dvh;
  background: var(--bhw-chat-bg);
  color: var(--bhw-chat-ink);
  overflow: hidden;
}

/* Điện thoại: khung phủ cả màn, khóa cuộn trang phía sau. */
@media (max-width: 639.98px) {
  html.bhw-chat-lock,
  html.bhw-chat-lock body {
    overflow: hidden;
  }
}

.bhw-chat-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 8px 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top, 0px));
  background: var(--bhw-c);
  color: var(--bhw-on-c);
}

.bhw-chat-avatar {
  display: grid;
  place-items: center;
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--bhw-on-c) 18%, transparent);
}

.bhw-chat-titles {
  min-width: 0;
  flex: 1;
}

.bhw-chat-title {
  margin: 0;
  overflow: hidden;
  color: inherit;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.3;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.bhw-chat-sub {
  margin: 0;
  font-size: 13px;
  opacity: 0.9;
}

.bhw-chat-close {
  display: grid;
  place-items: center;
  flex: none;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: inherit;
  cursor: pointer;
}

.bhw-chat-close:hover {
  background: color-mix(in srgb, var(--bhw-on-c) 15%, transparent);
}

.bhw-chat-head .bhw-chat-close:focus-visible {
  outline-color: var(--bhw-on-c);
}

/* ---------- Luồng tin ---------- */

.bhw-chat-log {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 16px;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--bhw-chat-panel);
}

.bhw-chat-loading,
.bhw-chat-sys {
  margin: 8px auto;
  color: var(--bhw-chat-muted);
  font-size: 13px;
  text-align: center;
}

.bhw-chat-msg {
  position: relative;
  display: flex;
  flex-direction: column;
  max-width: 82%;
  padding: 8px 12px;
  border-radius: var(--bhw-chat-r-lg);
  background: var(--bhw-chat-bg);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.06);
  overflow-wrap: anywhere;
}

.bhw-chat-msg--shop {
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}

.bhw-chat-msg--me {
  align-self: flex-end;
  border-bottom-right-radius: 4px;
  background: var(--bhw-c);
  color: var(--bhw-on-c);
}

.bhw-chat-msg--auto {
  background: var(--bhw-chat-info-l);
}

.bhw-chat-who {
  /* Đọc cho trình đọc màn hình, không cần thấy: bên trái / phải đã nói ai gửi. */
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.bhw-chat-text {
  margin: 0;
  white-space: pre-wrap;
}

.bhw-chat-msg--me .bhw-chat-text a {
  color: inherit;
  text-decoration: underline;
}

.bhw-chat-text a {
  color: var(--bhw-c);
}

.bhw-chat-meta {
  align-self: flex-end;
  margin-top: 2px;
  font-size: 11px;
  opacity: 0.75;
}

.bhw-chat-meta:empty {
  display: none;
}

.bhw-chat-msg.is-pending {
  opacity: 0.7;
}

.bhw-chat-msg.is-failed {
  background: var(--bhw-chat-warn-l);
  color: var(--bhw-chat-ink);
}

.bhw-chat-retry {
  align-self: flex-end;
  margin-top: 4px;
  padding: 4px 10px;
  border: 1px solid var(--bhw-chat-warn);
  border-radius: 999px;
  background: var(--bhw-chat-bg);
  color: var(--bhw-chat-warn);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

/* ---------- Dải báo, nút liên hệ, form ---------- */

.bhw-chat-notice,
.bhw-chat-help,
.bhw-chat-intro,
.bhw-chat-ask {
  padding: 12px 16px;
  border-top: 1px solid var(--bhw-chat-line);
  background: var(--bhw-chat-bg);
  font-size: 14px;
}

.bhw-chat-notice {
  background: var(--bhw-chat-info-l);
}

.bhw-chat-notice p,
.bhw-chat-help p {
  margin: 0 0 8px;
}

.bhw-chat-notice p:last-child,
.bhw-chat-help p:last-child {
  margin-bottom: 0;
}

.bhw-chat-intro {
  flex: none;
  max-height: 70%;
  overflow-y: auto;
}

.bhw-chat-form {
  display: grid;
  gap: 10px;
}

.bhw-chat-lead {
  margin: 0;
}

.bhw-chat-label {
  display: grid;
  gap: 4px;
  font-size: 13px;
  font-weight: 600;
}

.bhw-chat-in {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--bhw-chat-line);
  border-radius: var(--bhw-chat-r);
  background: var(--bhw-chat-bg);
  color: var(--bhw-chat-ink);
  font: inherit;
  font-size: 16px; /* dưới 16px thì Safari điện thoại tự phóng to trang khi chạm ô */
  font-weight: 400;
}

.bhw-chat-in:focus,
.bhw-chat-input:focus {
  border-color: var(--bhw-c);
  outline: 2px solid var(--bhw-focus);
  outline-offset: 0;
}

.bhw-chat-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.bhw-chat-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 8px 14px;
  border: 1px solid var(--bhw-c);
  border-radius: var(--bhw-chat-r);
  background: var(--bhw-chat-bg);
  color: var(--bhw-c);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}

.bhw-chat-btn:hover {
  background: var(--bhw-chat-soft);
}

.bhw-chat-btn--primary {
  background: var(--bhw-c);
  color: var(--bhw-on-c);
}

.bhw-chat-btn--primary:hover {
  background: var(--bhw-c-d);
}

.bhw-chat-btn--alt {
  border-color: var(--bhw-a);
  background: var(--bhw-a);
  color: var(--bhw-on-a);
}

.bhw-chat-btn--alt:hover {
  background: color-mix(in srgb, var(--bhw-a) 85%, #000);
}

.bhw-chat-btn--ghost {
  border-color: transparent;
  color: var(--bhw-chat-muted);
}

.bhw-chat-btn:disabled {
  opacity: 0.6;
  cursor: default;
}

.bhw-chat-error {
  margin: 0;
  padding: 6px 16px;
  color: var(--bhw-chat-warn);
  font-size: 13px;
}

.bhw-chat-form .bhw-chat-error {
  padding: 0;
}

/* ---------- Ô nhập ---------- */

.bhw-chat-composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--bhw-chat-line);
  background: var(--bhw-chat-bg);
}

.bhw-chat-field {
  position: relative;
  flex: 1;
  min-width: 0;
}

.bhw-chat-input {
  display: block;
  width: 100%;
  min-height: 44px;
  max-height: 120px;
  padding: 10px 12px;
  border: 1px solid var(--bhw-chat-line);
  border-radius: 22px;
  background: var(--bhw-chat-bg);
  color: var(--bhw-chat-ink);
  font: inherit;
  font-size: 16px;
  line-height: 1.4;
  resize: none;
}

.bhw-chat-count {
  position: absolute;
  right: 12px;
  bottom: -2px;
  color: var(--bhw-chat-muted);
  font-size: 11px;
}

.bhw-chat-send {
  display: grid;
  place-items: center;
  flex: none;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--bhw-c);
  color: var(--bhw-on-c);
  cursor: pointer;
}

.bhw-chat-send:hover {
  background: var(--bhw-c-d);
}

/* Ô bẫy cho máy: người thật không thấy. Không dùng display:none vì một số máy bỏ qua ô ẩn hẳn. */
.bhw-chat-hp {
  position: absolute !important;
  left: -9999px !important;
  width: 1px !important;
  height: 1px !important;
  opacity: 0 !important;
}

.bhw-chat-foot {
  margin: 0;
  padding: 6px 16px calc(8px + env(safe-area-inset-bottom, 0px));
  background: var(--bhw-chat-bg);
  color: var(--bhw-chat-muted);
  font-size: 12px;
  text-align: center;
}

/* ---------- Màn rộng: khung nổi góc dưới, không phủ trang ---------- */

@media (min-width: 640px) {
  .bhw-chat-bubble-label {
    display: inline;
  }

  .bhw-chat-panel {
    inset: auto var(--bhw-chat-gap) var(--bhw-chat-bottom) auto;
    width: 370px;
    height: min(580px, calc(100vh - 120px));
    height: min(580px, calc(100dvh - 120px));
    border: 1px solid var(--bhw-chat-line);
    border-radius: var(--bhw-chat-r-lg);
    box-shadow: 0 16px 48px rgb(0 0 0 / 0.22);
  }

  .bhw-chat.is-left .bhw-chat-panel {
    right: auto;
    left: var(--bhw-chat-gap);
  }

  .bhw-chat-head {
    padding-top: 12px;
  }

  .bhw-chat-foot {
    padding-bottom: 8px;
  }
}

@media (min-width: 1024px) {
  .bhw-chat {
    --bhw-chat-gap: 24px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .bhw-chat-bubble {
    transition: none;
  }
}

@media print {
  .bhw-chat {
    display: none !important;
  }
}
