/* Ly Ly Chat AI — LUÔN nằm TRÊN 3 nút liên hệ (Gọi / FB / Lên đầu) */
.adv-chat {
  position: fixed;
  right: max(0.85rem, env(safe-area-inset-right));
  /*
   * Stack float: 3 nút × 52px + 2 gap + bottom 1.5rem + đệm
   * ≈ 156 + 21 + 24 + 12 ≈ 213px → ~13.5rem
   */
  bottom: calc(13.5rem + env(safe-area-inset-bottom, 0px));
  z-index: 1000; /* float-actions = 900 → AI luôn trên */
  font-family: var(--font, "Be Vietnam Pro", system-ui, sans-serif);
  pointer-events: none;
}

/* Ẩn chat Zalo thường (nút + khung) */
body.has-ai-chat .float-btn.zalo,
body.has-ai-chat #zaloChatToggle,
body.has-ai-chat #zaloChatPanel,
body.has-ai-chat .zalo-chat,
.float-btn.zalo,
#zaloChatToggle,
#zaloChatPanel.zalo-chat,
.zalo-chat {
  display: none !important;
  visibility: hidden !important;
  pointer-events: none !important;
}

@media (max-width: 520px) {
  .adv-chat {
    right: max(0.65rem, env(safe-area-inset-right));
    /* Mobile: nút nhỏ hơn / gap nhỏ hơn một chút */
    bottom: calc(12.25rem + env(safe-area-inset-bottom, 0px));
  }
}
.adv-chat > * { pointer-events: auto; }

.adv-chat-fab {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: 0;
  cursor: pointer;
  color: #fff;
  background: transparent;
  box-shadow: none;
  padding: 0;
  font-size: 1.4rem;
  display: grid;
  place-items: center;
  position: relative;
  transition: transform 0.2s;
  margin-left: auto;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overflow: visible;
}
.adv-chat-fab-img {
  width: 56px;
  height: 56px;
  display: block;
  border-radius: 50%;
  object-fit: contain;
  pointer-events: none;
  filter: drop-shadow(0 6px 14px rgba(56, 189, 248, 0.45));
}
.adv-chat-fab:hover { transform: scale(1.08); }
.adv-chat-fab:active { transform: scale(0.96); }
.adv-chat-fab.is-open .adv-chat-fab-img {
  filter: drop-shadow(0 6px 16px rgba(15, 23, 42, 0.35));
}
.adv-chat-fab-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: 999px;
  background: #ef4444;
  color: #fff;
  font-size: 0.7rem;
  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;
  visibility: hidden !important;
}
.adv-chat-panel { z-index: 2; }
.adv-chat-fab { z-index: 3; }

.adv-chat-panel {
  position: absolute;
  right: 0;
  bottom: 68px;
  width: min(94vw, 400px);
  height: auto;
  max-height: min(62vh, 460px);
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 20px;
  box-shadow: 0 24px 60px rgba(15, 23, 42, 0.18);
  display: none;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.25s, height 0.25s, max-height 0.25s;
  color: #0f172a;
  /* GPU layer for smoother open on mobile */
  transform: translateZ(0);
}
.adv-chat-panel.is-open { display: flex; }
.adv-chat-panel[hidden] { display: none !important; }
.adv-chat-panel.is-room {
  width: min(96vw, 460px);
  height: min(78vh, 640px);
  max-height: min(78vh, 640px);
}

.adv-chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem 1.1rem;
  background: linear-gradient(135deg, #0b1d36, #1e3a5f);
  color: #fff;
  flex-shrink: 0;
}
.adv-chat-head strong { display: block; font-size: 1.05rem; }
.adv-chat-head small {
  display: block;
  opacity: 0.78;
  font-size: 0.78rem;
  margin-top: 0.2rem;
  line-height: 1.35;
}
.adv-chat-close {
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  font-size: 1.1rem;
}

.adv-chat-gate {
  padding: 1.15rem 1.15rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  overflow: auto;
}
.adv-chat-gate-note {
  font-size: 0.92rem;
  color: #64748b;
  line-height: 1.55;
  margin: 0;
}
.adv-chat-field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.82rem;
  font-weight: 600;
  color: #64748b;
}
.adv-chat-field input {
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 0.8rem 0.95rem;
  background: #f8fafc;
  color: #0f172a;
  font-weight: 500;
  font: inherit;
}
.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.12);
}
.adv-chat-err { color: #dc2626; font-size: 0.85rem; margin: 0; }
.adv-chat-err[hidden] { display: none !important; }

.adv-chat-btn {
  border: 0;
  border-radius: 12px;
  padding: 0.9rem 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.45rem;
  font: inherit;
}
.adv-chat-btn:disabled { opacity: 0.65; cursor: wait; }

.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-y: auto;
  overflow-x: hidden;
  padding: 1rem 1.05rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  background: #f1f5f9;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}
.adv-chat-msg { display: flex; }
.adv-chat-msg.me { justify-content: flex-end; }
.adv-chat-bubble {
  max-width: 92%;
  padding: 0.75rem 0.95rem;
  border-radius: 16px;
  background: #fff;
  border: 1px solid #e2e8f0;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.05);
}
.adv-chat-msg.me .adv-chat-bubble {
  background: linear-gradient(135deg, #eff6ff, #dbeafe);
  border-color: rgba(29, 110, 245, 0.25);
}
.adv-chat-msg.admin .adv-chat-bubble {
  background: #ecfdf5;
  border-color: rgba(16, 185, 129, 0.28);
}
.adv-chat-who {
  display: block;
  font-size: 0.68rem;
  font-weight: 700;
  color: #64748b;
  margin-bottom: 0.3rem;
  letter-spacing: 0.02em;
}
.adv-chat-text {
  font-size: 0.95rem;
  line-height: 1.58;
  color: #0f172a;
  word-break: break-word;
}
.adv-chat-compose {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 0.9rem;
  border-top: 1px solid #e2e8f0;
  background: #fff;
  flex-shrink: 0;
}
.adv-chat-compose input {
  flex: 1;
  border: 1px solid #e2e8f0;
  border-radius: 14px;
  padding: 0.8rem 0.95rem;
  background: #f8fafc;
  font-size: 0.95rem;
  font: inherit;
}
.adv-chat-compose input:focus {
  outline: none;
  border-color: rgba(29, 110, 245, 0.5);
}
.adv-chat-send {
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 14px;
  background: linear-gradient(135deg, #1d6ef5, #1e40af);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  font-size: 1.1rem;
}
.adv-chat-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.4rem 1rem 0.55rem;
  font-size: 0.75rem;
  color: #64748b;
  background: #fff;
  border-top: 1px solid rgba(15, 23, 42, 0.04);
  flex-shrink: 0;
}
.adv-chat-link {
  background: none;
  border: 0;
  color: #1d6ef5;
  font-weight: 600;
  cursor: pointer;
  font-size: 0.75rem;
  padding: 0;
  font: inherit;
  font-weight: 600;
}

@media (max-width: 768px) {
  .adv-chat {
    right: max(0.65rem, env(safe-area-inset-right));
    /* Stack: bottom safe + phone + zalo + gaps ≈ đẩy AI lên */
    bottom: calc(9.75rem + env(safe-area-inset-bottom, 0px));
    left: auto;
  }
  .adv-chat-fab {
    width: 52px;
    height: 52px;
    font-size: 1.25rem;
  }
  .adv-chat-panel {
    bottom: 62px;
  }
  .adv-chat-panel.is-open,
  .adv-chat-panel.is-open.is-room {
    right: 0;
    left: auto;
    width: min(calc(100vw - 1.2rem), 100%);
    max-width: calc(100vw - 1.2rem);
    height: min(72vh, 560px);
    max-height: min(72vh, 560px);
    border-radius: 16px;
  }
  .adv-chat-panel.is-open:not(.is-room) {
    height: auto;
    max-height: min(68vh, 420px);
  }
  .adv-chat-compose input,
  .adv-chat-field input {
    font-size: 16px; /* tránh iOS zoom khi focus */
  }
  .adv-chat-msgs {
    overscroll-behavior: contain;
  }
}

@media (max-width: 380px) {
  .adv-chat {
    bottom: calc(9.25rem + env(safe-area-inset-bottom, 0px));
  }
}
