/* =========================================================
   ADV Live Chat — Ly Ly
   Desktop: compact card · Mobile: full-screen app sheet
   ========================================================= */

.adv-chat {
  position: fixed;
  right: max(0.75rem, env(safe-area-inset-right, 0px));
  bottom: calc(12.75rem + env(safe-area-inset-bottom, 0px));
  z-index: 99990;
  font-family: var(--font, "Be Vietnam Pro", system-ui, -apple-system, sans-serif);
  pointer-events: none;
  width: auto;
  height: auto;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
.adv-chat > * {
  pointer-events: auto;
}

/* --- FAB --- */
.adv-chat-fab {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 0;
  cursor: pointer;
  color: #fff;
  background: transparent;
  box-shadow: none;
  padding: 0;
  display: grid;
  place-items: center;
  position: relative;
  margin-left: auto;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overflow: visible;
  transition: transform 0.18s ease;
}
.adv-chat-fab-img {
  width: 52px;
  height: 52px;
  display: block;
  border-radius: 50%;
  object-fit: contain;
  pointer-events: none;
  filter: drop-shadow(0 6px 14px rgba(29, 110, 245, 0.4));
}
.adv-chat-fab:active {
  transform: scale(0.94);
}
.adv-chat-fab-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: #ef4444;
  color: #fff;
  font-size: 0.68rem;
  font-weight: 800;
  display: grid;
  place-items: center;
  border: 2px solid #fff;
}
.adv-chat-fab-badge[hidden] {
  display: none !important;
}

.adv-chat-backdrop {
  display: none !important;
  pointer-events: none !important;
}

/* --- Panel (desktop card) --- */
.adv-chat-panel {
  position: absolute;
  right: 0;
  bottom: 64px;
  width: min(92vw, 380px);
  max-height: min(70vh, 520px);
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 18px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.16);
  display: none;
  flex-direction: column;
  overflow: hidden;
  color: #0f172a;
  z-index: 4;
}
.adv-chat-fab {
  z-index: 5;
}
.adv-chat-panel.is-open {
  display: flex;
}
.adv-chat-panel[hidden] {
  display: none !important;
}
.adv-chat-panel.is-room {
  height: min(72vh, 560px);
  max-height: min(72vh, 560px);
}

/* --- Header --- */
.adv-chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
  padding: 0.75rem 0.9rem;
  background: linear-gradient(135deg, #0b1d36 0%, #163a66 100%);
  color: #fff;
  flex-shrink: 0;
  min-height: 52px;
}
.adv-chat-head > div {
  min-width: 0;
  flex: 1;
}
.adv-chat-head strong {
  display: block;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.adv-chat-head small {
  display: block;
  opacity: 0.8;
  font-size: 0.72rem;
  margin-top: 0.15rem;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.adv-chat-close {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  font-size: 1rem;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
}

/* --- Gate (phone form) --- */
.adv-chat-gate {
  padding: 0.95rem 1rem 1.05rem;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  overflow: auto;
  min-height: 0;
  flex: 1;
}
.adv-chat-gate-note {
  font-size: 0.84rem;
  color: #64748b;
  line-height: 1.45;
  margin: 0;
}
.adv-chat-field {
  display: flex;
  flex-direction: column;
  gap: 0.28rem;
  font-size: 0.75rem;
  font-weight: 600;
  color: #64748b;
}
.adv-chat-field input {
  border: 1px solid #e2e8f0;
  border-radius: 11px;
  padding: 0.7rem 0.85rem;
  background: #f8fafc;
  color: #0f172a;
  font-weight: 500;
  font: inherit;
  font-size: 0.92rem;
  min-height: 44px;
  box-sizing: border-box;
  width: 100%;
}
.adv-chat-field input:focus {
  outline: none;
  border-color: rgba(29, 110, 245, 0.55);
  box-shadow: 0 0 0 3px rgba(29, 110, 245, 0.1);
}
.adv-chat-err {
  color: #dc2626;
  font-size: 0.8rem;
  margin: 0;
}
.adv-chat-err[hidden] {
  display: none !important;
}
.adv-chat-btn {
  border: 0;
  border-radius: 11px;
  padding: 0.8rem 1rem;
  font-weight: 700;
  cursor: pointer;
  color: #fff;
  background: linear-gradient(135deg, #1d6ef5, #1e40af);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  font: inherit;
  font-size: 0.92rem;
  min-height: 46px;
  -webkit-tap-highlight-color: transparent;
}
.adv-chat-btn:disabled {
  opacity: 0.65;
  cursor: wait;
}

/* --- Room --- */
.adv-chat-room {
  display: none;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}
.adv-chat-panel.is-room .adv-chat-room {
  display: flex;
}
.adv-chat-panel.is-room .adv-chat-gate {
  display: none !important;
}

.adv-chat-msgs {
  flex: 1;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 0.75rem 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  background: #f1f5f9;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.adv-chat-msg {
  display: flex;
  max-width: 100%;
}
.adv-chat-msg.me {
  justify-content: flex-end;
}
.adv-chat-bubble {
  max-width: min(86%, 320px);
  padding: 0.55rem 0.75rem;
  border-radius: 14px;
  background: #fff;
  border: 1px solid #e2e8f0;
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.04);
  box-sizing: border-box;
}
.adv-chat-msg.me .adv-chat-bubble {
  max-width: min(80%, 280px);
  background: #dbeafe;
  border-color: rgba(29, 110, 245, 0.22);
}
.adv-chat-msg.admin .adv-chat-bubble {
  background: #ecfdf5;
  border-color: rgba(16, 185, 129, 0.25);
}
.adv-chat-who {
  display: block;
  font-size: 0.65rem;
  font-weight: 700;
  color: #64748b;
  margin-bottom: 0.2rem;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}
.adv-chat-text {
  font-size: 0.88rem;
  line-height: 1.45;
  color: #0f172a;
  word-break: break-word;
  overflow-wrap: anywhere;
}
.adv-chat-text br + br {
  display: none; /* bớt khoảng trắng kép */
}
.adv-chat-more {
  display: inline-block;
  margin-top: 0.35rem;
  border: 0;
  background: none;
  color: #1d6ef5;
  font-size: 0.78rem;
  font-weight: 700;
  padding: 0;
  cursor: pointer;
  font-family: inherit;
}

.adv-chat-compose {
  display: flex;
  gap: 0.45rem;
  padding: 0.6rem 0.75rem;
  border-top: 1px solid #e2e8f0;
  background: #fff;
  flex-shrink: 0;
  align-items: center;
}
.adv-chat-compose input {
  flex: 1;
  min-width: 0;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 0.65rem 0.8rem;
  background: #f8fafc;
  font: inherit;
  font-size: 0.9rem;
  min-height: 42px;
  box-sizing: border-box;
}
.adv-chat-compose input:focus {
  outline: none;
  border-color: rgba(29, 110, 245, 0.5);
}
.adv-chat-send {
  width: 42px;
  height: 42px;
  border: 0;
  border-radius: 12px;
  background: linear-gradient(135deg, #1d6ef5, #1e40af);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  font-size: 1rem;
  -webkit-tap-highlight-color: transparent;
}
.adv-chat-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.35rem 0.85rem 0.5rem;
  font-size: 0.7rem;
  color: #64748b;
  background: #fff;
  border-top: 1px solid rgba(15, 23, 42, 0.04);
  flex-shrink: 0;
  gap: 0.5rem;
}
.adv-chat-foot #advChatStatus {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.adv-chat-link {
  background: none;
  border: 0;
  color: #1d6ef5;
  font-weight: 600;
  cursor: pointer;
  font-size: 0.72rem;
  padding: 0.25rem 0;
  font-family: inherit;
  flex-shrink: 0;
}

/* Typing / status */
.adv-chat-typing {
  font-size: 0.78rem;
  color: #64748b;
  padding: 0.2rem 0.4rem;
  font-style: italic;
}

/* =========================================================
   MOBILE — full-screen sheet, gọn, không méo
   ========================================================= */
@media (max-width: 768px) {
  .adv-chat {
    right: max(0.6rem, env(safe-area-inset-right, 0px));
    bottom: calc(12rem + env(safe-area-inset-bottom, 0px));
  }
  .adv-chat-fab {
    width: 50px;
    height: 50px;
  }
  .adv-chat-fab-img {
    width: 48px;
    height: 48px;
  }

  /* Full screen independent of FAB width */
  .adv-chat-panel.is-open,
  .adv-chat-panel.is-open.is-room,
  .adv-chat-panel.is-open:not(.is-room) {
    position: fixed !important;
    top: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    left: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
    max-height: 100% !important;
    /* Prefer dynamic viewport when supported */
    height: 100dvh !important;
    max-height: 100dvh !important;
    margin: 0 !important;
    border: 0 !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    z-index: 2147483000 !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    transform: none !important;
    background: #fff !important;
  }

  .adv-chat-head {
    padding: 0.65rem 0.85rem;
    padding-top: max(0.65rem, env(safe-area-inset-top, 0px));
    min-height: 48px;
  }
  .adv-chat-head strong {
    font-size: 0.9rem;
  }
  .adv-chat-head small {
    font-size: 0.68rem;
  }
  .adv-chat-close {
    width: 40px;
    height: 40px;
  }

  .adv-chat-gate {
    padding: 0.85rem 1rem;
    padding-bottom: max(1rem, env(safe-area-inset-bottom, 0px));
    gap: 0.65rem;
  }
  .adv-chat-gate-note {
    font-size: 0.8rem;
    line-height: 1.4;
  }
  .adv-chat-field input {
    font-size: 16px; /* no iOS zoom */
    min-height: 46px;
  }
  .adv-chat-btn {
    min-height: 48px;
    font-size: 0.95rem;
  }

  .adv-chat-room {
    flex: 1 1 auto;
    min-height: 0;
  }
  .adv-chat-msgs {
    padding: 0.65rem 0.75rem;
    gap: 0.45rem;
  }
  .adv-chat-bubble {
    max-width: 88%;
    padding: 0.5rem 0.7rem;
    border-radius: 12px;
  }
  .adv-chat-msg.me .adv-chat-bubble {
    max-width: 82%;
  }
  .adv-chat-text {
    font-size: 0.86rem;
    line-height: 1.42;
  }
  .adv-chat-who {
    font-size: 0.62rem;
    margin-bottom: 0.15rem;
  }

  .adv-chat-compose {
    padding: 0.55rem 0.7rem;
    padding-bottom: max(0.55rem, env(safe-area-inset-bottom, 0px));
    gap: 0.4rem;
  }
  .adv-chat-compose input {
    font-size: 16px;
    min-height: 44px;
    padding: 0.6rem 0.75rem;
  }
  .adv-chat-send {
    width: 44px;
    height: 44px;
  }
  .adv-chat-foot {
    padding: 0.3rem 0.75rem 0.4rem;
    padding-bottom: max(0.35rem, calc(env(safe-area-inset-bottom, 0px) * 0.35));
    font-size: 0.68rem;
  }

  body.adv-chat-open {
    overflow: hidden !important;
    touch-action: none;
  }
  body.adv-chat-open .adv-chat-fab {
    opacity: 0 !important;
    pointer-events: none !important;
    visibility: hidden !important;
  }
  /* Ẩn Zalo/hotline float khi chat mở (nếu dùng class phổ biến) */
  body.adv-chat-open .zalo-float,
  body.adv-chat-open .hotline-float,
  body.adv-chat-open .float-hotline,
  body.adv-chat-open .float-zalo,
  body.adv-chat-open a[href*="zalo.me"][class*="float"],
  body.adv-chat-open a[href^="tel:"][class*="float"] {
    opacity: 0 !important;
    pointer-events: none !important;
  }
}

@media (max-width: 380px) {
  .adv-chat {
    bottom: calc(12rem + env(safe-area-inset-bottom, 0px));
  }
  .adv-chat-text {
    font-size: 0.84rem;
  }
}

/* Desktop polish */
@media (min-width: 769px) {
  .adv-chat-fab:hover {
    transform: translateY(-1px) scale(1.04);
  }
}


/* === FIX: Chat AI luôn trên cùng, không chồng nút liên hệ (Zalo/Phone/Top) === */
.adv-chat {
  position: fixed !important;
  right: max(0.75rem, env(safe-area-inset-right, 0px)) !important;
  /* 3 nút ~52px + gap → chừa khoảng phía dưới */
  bottom: calc(13rem + env(safe-area-inset-bottom, 0px)) !important;
  z-index: 99990 !important;
  pointer-events: none;
}
.adv-chat > * {
  pointer-events: auto;
}
.adv-chat-fab {
  z-index: 99991 !important;
  position: relative;
}
.adv-chat-panel {
  z-index: 99992 !important;
}
.adv-chat-panel.is-open,
.adv-chat-panel.is-open.is-room,
.adv-chat-panel.is-open:not(.is-room) {
  z-index: 2147483000 !important;
}
@media (max-width: 768px) {
  .adv-chat {
    right: max(0.6rem, env(safe-area-inset-right, 0px)) !important;
    bottom: calc(12.25rem + env(safe-area-inset-bottom, 0px)) !important;
    z-index: 99990 !important;
  }
}
@media (max-width: 480px) {
  .adv-chat {
    bottom: calc(11.75rem + env(safe-area-inset-bottom, 0px)) !important;
  }
}

/* Không chồng Zalo/Phone khi chat mở */
body.adv-chat-open .float-actions,
body.adv-chat-open .float-hotline {
  visibility: hidden !important;
  pointer-events: none !important;
  opacity: 0 !important;
}

/* Desktop panel: fixed to viewport corner, không phụ thuộc width FAB */
@media (min-width: 769px) {
  .adv-chat-panel.is-open {
    position: fixed !important;
    right: max(0.75rem, env(safe-area-inset-right, 0px)) !important;
    bottom: calc(4.5rem + env(safe-area-inset-bottom, 0px)) !important;
    left: auto !important;
    top: auto !important;
    width: min(94vw, 400px) !important;
    max-height: min(72vh, 640px) !important;
    z-index: 99995 !important;
  }
  .adv-chat-panel.is-open.is-room {
    width: min(96vw, 440px) !important;
    height: min(82vh, 720px) !important;
    max-height: min(82vh, 720px) !important;
  }
  .adv-chat {
    bottom: calc(11.5rem + env(safe-area-inset-bottom, 0px));
  }
}

/* Mobile: chat FAB luôn trên cụm Zalo/Phone, không full-width container */
@media (max-width: 768px) {
  .adv-chat {
    left: auto !important;
    right: max(0.6rem, env(safe-area-inset-right, 0px)) !important;
    bottom: calc(11.25rem + env(safe-area-inset-bottom, 0px)) !important;
    width: auto !important;
  }
}
