/* ====================================================
   BOX CHAT HỖ TRỢ KHÁCH HÀNG (TRỢ LÝ AI KiraAI)
   ==================================================== */

.chat-widget {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 1500;
  font-family: 'Be Vietnam Pro', sans-serif;
}

/* Nút tròn mở chat */
.chat-fab {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: none;
  background: var(--primary-gradient);
  color: #fff;
  font-size: 1.5rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  box-shadow: var(--shadow-glow), 0 8px 24px rgba(0, 0, 0, 0.45);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.chat-fab:hover {
  transform: translateY(-3px) scale(1.04);
  box-shadow: 0 0 45px rgba(99, 102, 241, 0.45), 0 10px 28px rgba(0, 0, 0, 0.5);
}

.chat-fab-dot {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #10b981;
  border: 2px solid var(--bg-main);
}

/* Cửa sổ chat */
.chat-panel {
  position: absolute;
  bottom: 76px;
  right: 0;
  width: min(380px, calc(100vw - 32px));
  height: min(540px, calc(100vh - 140px));
  background: var(--bg-card);
  border: 1px solid var(--border-glass);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: chatPopIn 0.22s ease;
}

/* attribute hidden phải thắng display:flex ở trên (nếu không panel luôn hiển thị) */
.chat-panel[hidden] {
  display: none !important;
}

@keyframes chatPopIn {
  from { opacity: 0; transform: translateY(14px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

/* Header */
.chat-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 16px;
  background: var(--primary-gradient);
  color: #fff;
}

.chat-header-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.chat-agent-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.15rem;
}

.chat-agent-name {
  font-weight: 600;
  font-size: 0.92rem;
  line-height: 1.2;
}

.chat-agent-status {
  font-size: 0.72rem;
  opacity: 0.9;
  display: flex;
  align-items: center;
  gap: 5px;
}

.chat-status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #34d399;
  display: inline-block;
  animation: chatPulse 1.6s infinite;
}

@keyframes chatPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(52, 211, 153, 0.6); }
  50% { box-shadow: 0 0 0 5px rgba(52, 211, 153, 0); }
}

.chat-header-close {
  background: rgba(255, 255, 255, 0.15);
  border: none;
  color: #fff;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease;
}

.chat-header-close:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* Khu vực tin nhắn */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background:
    radial-gradient(circle at 20% 0%, rgba(99, 102, 241, 0.07), transparent 55%),
    var(--bg-card);
}

.chat-msg {
  max-width: 82%;
  padding: 9px 13px;
  font-size: 0.86rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  animation: chatMsgIn 0.18s ease;
}

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

.chat-msg-user {
  align-self: flex-end;
  background: var(--primary);
  color: #fff;
  border-radius: 14px 14px 4px 14px;
}

.chat-msg-ai {
  align-self: flex-start;
  background: var(--bg-card-subtle);
  color: var(--text-main);
  border: 1px solid var(--border-glass);
  border-radius: 14px 14px 14px 4px;
}

.chat-msg-system {
  align-self: center;
  max-width: 92%;
  background: rgba(244, 63, 94, 0.1);
  border: 1px solid rgba(244, 63, 94, 0.3);
  color: #fda4af;
  border-radius: 10px;
  font-size: 0.8rem;
  text-align: center;
}

.chat-msg-time {
  display: block;
  font-size: 0.65rem;
  opacity: 0.65;
  margin-top: 4px;
}

/* Chấm "đang gõ..." */
.chat-typing {
  align-self: flex-start;
  display: flex;
  gap: 4px;
  padding: 12px 14px;
  background: var(--bg-card-subtle);
  border: 1px solid var(--border-glass);
  border-radius: 14px 14px 14px 4px;
}

.chat-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-dim);
  animation: chatTypingBounce 1.2s infinite;
}

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

@keyframes chatTypingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* Ô nhập + nút gửi */
.chat-input-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--border-glass);
  background: var(--bg-card-subtle);
}

.chat-input {
  flex: 1;
  resize: none;
  border: 1px solid var(--border-glass);
  background: var(--bg-card);
  color: var(--text-main);
  border-radius: 12px;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 0.86rem;
  line-height: 1.45;
  max-height: 110px;
  outline: none;
  transition: border-color 0.15s ease;
}

.chat-input:focus {
  border-color: var(--border-glass-hover);
}

.chat-input::placeholder {
  color: var(--text-dim);
}

.chat-send-btn {
  width: 42px;
  height: 42px;
  flex-shrink: 0;
  border: none;
  border-radius: 12px;
  background: var(--primary-gradient);
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s ease, opacity 0.15s ease;
}

.chat-send-btn:hover:not(:disabled) {
  transform: scale(1.05);
}

.chat-send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Khung yêu cầu đăng nhập */
.chat-login-gate {
  padding: 22px 18px;
  text-align: center;
  border-top: 1px solid var(--border-glass);
  background: var(--bg-card-subtle);
}

.chat-login-gate i {
  font-size: 1.7rem;
  color: var(--primary);
  display: block;
  margin-bottom: 8px;
}

.chat-login-gate p {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.chat-login-gate a {
  color: var(--primary);
  font-weight: 600;
}

.chat-login-btn {
  display: inline-block;
  padding: 9px 22px;
  border-radius: 10px;
  background: var(--primary-gradient);
  color: #fff !important;
  font-size: 0.85rem;
  font-weight: 600;
}

.chat-login-btn:hover {
  opacity: 0.9;
}

/* Di động */
@media (max-width: 576px) {
  .chat-widget {
    right: 14px;
    bottom: 14px;
  }

  .chat-fab {
    width: 54px;
    height: 54px;
    font-size: 1.35rem;
  }

  .chat-panel {
    bottom: 68px;
    height: min(500px, calc(100vh - 110px));
  }
}

/* ====================================================
   REALTIME: TIẾP QUẢN, ĐÃ XEM, PRESENCE
   ==================================================== */

/* Banner chế độ admin tiếp quản */
.chat-takeover-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 14px;
  background: rgba(16, 185, 129, 0.12);
  border-bottom: 1px solid rgba(16, 185, 129, 0.35);
  color: #34d399;
  font-size: 0.78rem;
  font-weight: 500;
}

.chat-takeover-banner[hidden] {
  display: none !important;
}

/* Biên nhận "Đã xem" */
.chat-msg-receipt {
  align-self: flex-end;
  font-size: 0.66rem;
  color: var(--text-dim);
  margin: -6px 4px 0 0;
  animation: chatMsgIn 0.18s ease;
}

/* Tin nhắn admin (khác màu nhẹ với AI để khách phân biệt) */
.chat-msg-admin {
  background: rgba(16, 185, 129, 0.14);
  border: 1px solid rgba(16, 185, 129, 0.35);
  color: #d1fae5;
}

/* Dot offline */
.chat-status-dot--offline {
  background: #64748b !important;
  animation: none !important;
}

/* Chấm "đang soạn" từ phía đối diện (ẩn theo attribute) */
.chat-typing-remote[hidden] {
  display: none !important;
}
