/* ==========================================
   AST AI CHATBOT WIDGET (custom UI on top of Dify API)
   Bố cục tham khảo widget SaleAI trên matbao.net: nút tròn có vòng sáng xoay,
   khung kính mờ, header viên thuốc nổi, khung nhập là thẻ kính đè lên đáy.
   Màu lấy theo landing page AST (xanh lá); mascot xanh dương chỉ là điểm nhấn ở avatar.
   ========================================== */

@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap&subset=vietnamese");

.ast-chat-toggle,
.ast-chat-window {
  --ast-brand: var(--primary-dark, #16a34a);
  --ast-brand-light: var(--primary-color, #22c55e);
  --ast-brand-rgb: 22, 163, 74;
  --ast-text: #1f2937;
  --ast-muted: #6b7280;
  font-family: "Inter", var(--font-primary, "Segoe UI", Tahoma, sans-serif);
}

/* ---------- Nút tròn mở chat ---------- */

.ast-chat-toggle {
  position: fixed;
  bottom: 160px;
  right: 28px;
  width: 64px;
  height: 64px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  overflow: hidden;
  z-index: 9997;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15), 0 0 0 0 rgba(var(--ast-brand-rgb), 0.18);
  animation: astBubblePulse 2.4s ease-in-out infinite;
  transition: transform 0.25s ease;
}

.ast-chat-toggle:hover {
  transform: scale(1.06);
}

/* Vệt sáng quét quanh viền (vòng xoay conic-gradient phía sau avatar). */
.ast-chat-toggle-spinner {
  position: absolute;
  inset: -32px;
  background: conic-gradient(
    transparent 0deg,
    transparent 80deg,
    rgba(var(--ast-brand-rgb), 0.95) 180deg,
    transparent 180deg
  );
  animation: astSpinBorder 2.6s linear infinite;
}

.ast-chat-toggle-avatar {
  position: absolute;
  inset: 2px;
  z-index: 1;
  font-size: 1.5rem;
}

.ast-chat-toggle.open {
  animation: none;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.ast-chat-toggle.open .ast-chat-badge {
  display: none;
}

.ast-chat-badge {
  position: absolute;
  top: 3px;
  right: 3px;
  z-index: 2;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #ef4444;
  border: 2px solid #fff;
}

/* Avatar dùng chung cho nút tròn và header: nền trắng, ảnh đã chừa lề sẵn nên phủ vừa khung. */
.ast-chat-avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background-color: #fff;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  color: var(--ast-brand);
}

.ast-chat-avatar.has-img i {
  display: none;
}

@keyframes astBubblePulse {
  0%, 100% { box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15), 0 0 0 0 rgba(var(--ast-brand-rgb), 0.22); }
  50% { box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15), 0 0 0 13px rgba(var(--ast-brand-rgb), 0); }
}

@keyframes astSpinBorder {
  to { transform: rotate(360deg); }
}

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

.ast-chat-window {
  position: fixed;
  right: 104px;
  bottom: 24px;
  width: 440px;
  max-width: calc(100vw - 124px);
  height: min(620px, calc(100vh - 110px));
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.82) 0%, rgba(240, 253, 244, 0.62) 95%);
  -webkit-backdrop-filter: blur(14px) saturate(140%);
  backdrop-filter: blur(14px) saturate(140%);
  border: 1px solid rgba(255, 255, 255, 0.85);
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.28);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 9998;
  color: var(--ast-text);
  opacity: 0;
  transform: translateY(16px) scale(0.96);
  transform-origin: bottom right;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease, width 0.3s ease, height 0.3s ease;
}

.ast-chat-window.open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

.ast-chat-window.expanded {
  width: min(820px, calc(100vw - 140px));
  height: calc(100vh - 110px);
}

/* ---------- Header: viên thuốc nổi ---------- */

.ast-chat-header {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  max-width: calc(100% - 28px);
  padding: 7px 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--ast-brand);
  color: #fff;
  border-radius: 26px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3), inset 0 3px 8px rgba(0, 0, 0, 0.28);
}

.ast-chat-header-avatar {
  position: relative;
  flex-shrink: 0;
}

.ast-chat-header-avatar .ast-chat-avatar {
  width: 34px;
  height: 34px;
  font-size: 0.95rem;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.15);
}

.ast-chat-status-dot {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #4ade80;
  border: 2px solid var(--ast-brand);
}

.ast-chat-header-info {
  min-width: 0;
  padding-right: 6px;
}

.ast-chat-header-info .ast-chat-name {
  font-size: 13px;
  font-weight: 600;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ast-chat-header-info .ast-chat-status {
  font-size: 11px;
  font-weight: 500;
  line-height: 1;
  color: rgba(255, 255, 255, 0.7);
}

.ast-chat-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.ast-chat-hbtn {
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.12);
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3);
  color: rgba(255, 255, 255, 0.7);
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease;
}

.ast-chat-hbtn:hover,
.ast-chat-hbtn:focus-visible {
  background: rgba(0, 0, 0, 0.22);
  color: #fff;
  outline: none;
}

.ast-chat-hbtn.spin i {
  animation: astSpinBorder 0.6s ease;
}

/* ---------- Tin nhắn ---------- */

.ast-chat-body {
  flex: 1;
  overflow-y: auto;
  /* Chừa chỗ cho header nổi ở trên và mép thẻ nhập đè lên ở dưới. */
  padding: 88px 16px 40px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scrollbar-width: thin;
  scrollbar-color: rgba(0, 0, 0, 0.15) transparent;
}

.ast-chat-body::-webkit-scrollbar {
  width: 5px;
}

.ast-chat-body::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.15);
  border-radius: 3px;
}

.ast-chat-msg {
  position: relative;
  max-width: 85%;
  padding: 14px 16px 24px;
  border-radius: 20px;
  font-size: 14px;
  line-height: 1.6;
  word-wrap: break-word;
  white-space: pre-wrap;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  animation: astFadeInUp 0.35s ease both;
}

.ast-chat-msg.bot {
  align-self: flex-start;
  background: linear-gradient(90deg, #fff 0%, rgba(255, 255, 255, 0.66) 100%);
  color: var(--ast-text);
  border-bottom-left-radius: 0;
}

.ast-chat-msg.user {
  align-self: flex-end;
  background: var(--ast-brand);
  color: #fff;
  border-bottom-right-radius: 0;
}

.ast-chat-time {
  position: absolute;
  left: 16px;
  bottom: 6px;
  font-size: 11px;
  color: #9ca3af;
  white-space: nowrap;
}

.ast-chat-msg.user .ast-chat-time {
  left: auto;
  right: 16px;
  color: rgba(255, 255, 255, 0.7);
}

/* Ảnh sản phẩm từ CRM: bỏ padding của bong bóng để ảnh sát viền. */
.ast-chat-msg.ast-chat-img {
  padding: 4px;
  max-width: 70%;
}

.ast-chat-msg.ast-chat-img img {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 16px;
}

@keyframes astFadeInUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Chip gợi ý: cột nút trắng căn phải, trượt vào lần lượt (delay đặt trong JS). */
.ast-chat-chips {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  align-self: flex-end;
  max-width: 92%;
  padding: 6px 0 8px;
}

.ast-chat-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border: none;
  border-radius: 16px;
  background: #fff;
  color: #111827;
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  line-height: 1.4;
  text-align: left;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
  animation: astChipSlideIn 0.35s ease both;
  transition: color 0.15s ease, box-shadow 0.15s ease;
  /* Chip là vùng chạm trên di động: giữ đủ cao để không bấm trượt. */
  min-height: 34px;
}

.ast-chat-chip i {
  font-size: 11px;
  opacity: 0.6;
  transition: transform 0.15s ease, opacity 0.15s ease;
}

.ast-chat-chip:hover,
.ast-chat-chip:focus-visible {
  color: var(--ast-brand);
  box-shadow: 0 0 0 1px rgba(var(--ast-brand-rgb), 0.35), 0 2px 8px rgba(0, 0, 0, 0.08);
  outline: none;
}

.ast-chat-chip:hover i,
.ast-chat-chip:focus-visible i {
  opacity: 1;
  transform: translateX(3px);
}

@keyframes astChipSlideIn {
  from { opacity: 0; transform: translateY(8px) scale(0.92); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.ast-chat-msg.typing {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 14px 18px;
}

.ast-chat-msg.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ast-brand-light);
  animation: astTypingBounce 1.2s infinite ease-in-out;
}

.ast-chat-msg.typing span:nth-child(2) { animation-delay: 0.15s; }
.ast-chat-msg.typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes astTypingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* ---------- Thẻ nhập đè lên đáy ---------- */

.ast-chat-footer {
  position: relative;
  z-index: 2;
  margin-top: -24px;
  padding: 24px 16px 10px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.78) 0%, rgba(255, 255, 255, 0.5) 95%);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.85);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -8px 16px rgba(0, 0, 0, 0.08);
  flex-shrink: 0;
}

.ast-chat-inputbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 12px 12px 18px;
  background: #fff;
  border: 1px solid rgba(var(--ast-brand-rgb), 0.13);
  border-radius: 32px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.06);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.ast-chat-inputbar:focus-within {
  border-color: rgba(var(--ast-brand-rgb), 0.5);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.06), 0 0 0 3px rgba(var(--ast-brand-rgb), 0.1);
}

.ast-chat-input {
  flex: 1;
  border: none;
  background: transparent;
  padding: 6px 0;
  font-size: 14px;
  font-family: inherit;
  color: #24292e;
  line-height: 1.5;
  resize: none;
  max-height: 90px;
  outline: none;
}

.ast-chat-input::placeholder {
  color: #9ca3af;
}

.ast-chat-send {
  width: 36px;
  height: 36px;
  padding: 0;
  border: none;
  border-radius: 16px;
  background: var(--ast-brand);
  color: #fff;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  box-shadow: 0 2px 6px rgba(var(--ast-brand-rgb), 0.3);
  transition: transform 0.15s ease, opacity 0.15s ease, background 0.15s ease;
}

.ast-chat-send:hover {
  background: var(--ast-brand-light);
  transform: translateY(-1px);
}

.ast-chat-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.ast-chat-footer-note {
  text-align: center;
  font-size: 11px;
  font-weight: 500;
  color: var(--ast-muted);
  padding: 12px 0 2px;
}

.ast-chat-footer-note a {
  background: var(--gradient, linear-gradient(135deg, #22c55e 0%, #10b981 100%));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-weight: 700;
  text-decoration: none;
}

.ast-chat-footer-note a:hover,
.ast-chat-footer-note a:focus-visible {
  text-decoration: underline;
  text-decoration-color: var(--ast-brand);
}

/* ---------- Tablet / di động ---------- */

@media (max-width: 640px) {
  .ast-chat-toggle {
    bottom: 152px;
    right: 15px;
    width: 56px;
    height: 56px;
  }

  /* Không đủ chỗ đặt cạnh cột nút: khung chiếm ngang màn hình, nằm trên nút tròn. */
  .ast-chat-window,
  .ast-chat-window.expanded {
    left: 10px;
    right: 10px;
    bottom: 218px;
    width: auto;
    max-width: none;
    height: min(600px, calc(100vh - 238px));
  }

  .ast-chat-hbtn[data-action="expand"] {
    display: none;
  }

  .ast-chat-msg {
    max-width: 90%;
  }
}

@media (prefers-reduced-motion: reduce) {
  .ast-chat-toggle,
  .ast-chat-toggle-spinner,
  .ast-chat-msg,
  .ast-chat-chip,
  .ast-chat-hbtn.spin i {
    animation: none;
  }
}
