/* ============================================
   FlexPRO AI Chat Widget — Sophia
   Premium, calm, non-intrusive
   v3.0 — Mobile keyboard-safe, dynamic viewport
   ============================================ */

/* ── CSS Custom Properties for viewport handling ── */
:root {
  --chat-vh: 1vh;
}

/* ── Chat Trigger Button ── */
.chat-trigger {
  position: fixed;
  bottom: 100px;
  right: 24px;
  z-index: 999;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: linear-gradient(135deg, #0a1628 0%, #1a2a4a 100%);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
  transition: all 0.3s ease;
}

.chat-trigger:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 28px rgba(0, 0, 0, 0.35);
}

.chat-trigger svg {
  width: 26px;
  height: 26px;
}

.chat-trigger__badge {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #22c55e;
  border: 2px solid #fff;
  animation: chatPulse 2s infinite;
}

@keyframes chatPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

/* ── Chat Window — Desktop ── */
.chat-window {
  position: fixed;
  bottom: 100px;
  right: 24px;
  z-index: 1000;
  width: 380px;
  max-width: calc(100vw - 32px);
  height: 520px;
  max-height: calc(100vh - 140px);
  background: #fff;
  border-radius: 20px;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.18);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(20px) scale(0.95);
  pointer-events: none;
  transition: opacity 0.3s cubic-bezier(0.16, 1, 0.3, 1),
              transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

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

/* ── Chat Header ── */
.chat-header {
  background: linear-gradient(135deg, #0a1628 0%, #1a2a4a 100%);
  color: #fff;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.chat-header__avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 700;
  color: #0a1628;
  flex-shrink: 0;
}

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

.chat-header__name {
  font-weight: 600;
  font-size: 0.9375rem;
  line-height: 1.2;
}

.chat-header__status {
  font-size: 0.75rem;
  opacity: 0.75;
  display: flex;
  align-items: center;
  gap: 4px;
}

.chat-header__status::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #22c55e;
  display: inline-block;
}

.chat-header__close {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
  flex-shrink: 0;
}

.chat-header__close:hover {
  background: rgba(255, 255, 255, 0.2);
}

.chat-header__close svg {
  width: 14px;
  height: 14px;
}

/* ── Chat Messages ── */
.chat-messages {
  flex: 1 1 0%;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  min-height: 0;
}

.chat-message {
  display: flex;
  gap: 8px;
  max-width: 88%;
  animation: chatMsgIn 0.3s ease;
}

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

.chat-message--bot {
  align-self: flex-start;
}

.chat-message--user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.chat-message__avatar {
  width: 28px;
  height: 28px;
  min-width: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: #0a1628;
  margin-top: 2px;
}

.chat-message--user .chat-message__avatar {
  background: #e2e8f0;
  color: #64748b;
}

.chat-message__bubble {
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 0.875rem;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.chat-message--bot .chat-message__bubble {
  background: #f1f5f9;
  color: #1e293b;
  border-bottom-left-radius: 4px;
}

.chat-message--user .chat-message__bubble {
  background: #0a1628;
  color: #fff;
  border-bottom-right-radius: 4px;
}

/* Typing indicator */
.chat-typing {
  display: flex;
  gap: 4px;
  padding: 10px 14px;
  background: #f1f5f9;
  border-radius: 16px;
  border-bottom-left-radius: 4px;
  align-self: flex-start;
  margin-left: 36px;
}

.chat-typing__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #94a3b8;
  animation: typingBounce 1.4s infinite;
}

.chat-typing__dot:nth-child(2) { animation-delay: 0.2s; }
.chat-typing__dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-4px); }
}

/* Quick reply buttons */
.chat-quick-replies {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 4px;
  margin-left: 36px;
}

.chat-quick-reply {
  padding: 6px 14px;
  border: 1px solid #cbd5e1;
  border-radius: 20px;
  background: #fff;
  color: #334155;
  font-size: 0.8125rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  font-family: inherit;
}

.chat-quick-reply:hover {
  background: #f1f5f9;
  border-color: #94a3b8;
}

/* ── Chat Input ── */
.chat-input-area {
  padding: 12px 16px;
  border-top: 1px solid #e2e8f0;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-shrink: 0;
  background: #fff;
}

.chat-input {
  flex: 1;
  border: 1px solid #e2e8f0;
  border-radius: 20px;
  padding: 10px 16px;
  font-size: 16px; /* Prevents iOS zoom on focus */
  font-family: inherit;
  resize: none;
  max-height: 80px;
  line-height: 1.4;
  outline: none;
  transition: border-color 0.2s;
  -webkit-appearance: none;
  appearance: none;
}

.chat-input:focus {
  border-color: #94a3b8;
}

.chat-input::placeholder {
  color: #94a3b8;
}

.chat-send {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #0a1628;
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
}

.chat-send:hover {
  background: #1a2a4a;
}

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

.chat-send svg {
  width: 16px;
  height: 16px;
}

/* ── Powered By ── */
.chat-footer-note {
  text-align: center;
  font-size: 0.6875rem;
  color: #94a3b8;
  padding: 4px 0 8px;
  background: #fff;
  flex-shrink: 0;
}

/* ══════════════════════════════════════════════
   MOBILE RESPONSIVE — Keyboard-Safe Layout
   Uses dynamic viewport units (dvh) with fallback.
   The JS companion sets --chat-available-h via
   visualViewport API for keyboard handling.
   ══════════════════════════════════════════════ */

@media (max-width: 480px) {
  /* Chat window: bottom-anchored, uses JS-driven height var */
  .chat-window {
    position: fixed;
    bottom: 0;
    right: 0;
    left: 0;
    top: auto;
    width: 100%;
    max-width: 100%;
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -4px 32px rgba(0, 0, 0, 0.2);

    /* Height strategy:
       1. JS sets --chat-available-h based on visualViewport
       2. Fallback to dvh (dynamic viewport height)
       3. Final fallback to vh */
    height: calc(var(--chat-available-h, 70vh) - 0px);
    height: calc(var(--chat-available-h, 70dvh) - 0px);
    max-height: none;
    min-height: 260px;
  }

  .chat-window.open {
    transform: translateY(0) scale(1);
  }

  /* Compact header on mobile */
  .chat-header {
    padding: 12px 16px;
    gap: 10px;
  }

  .chat-header__avatar {
    width: 34px;
    height: 34px;
    font-size: 15px;
  }

  .chat-header__name {
    font-size: 0.875rem;
  }

  .chat-header__status {
    font-size: 0.6875rem;
  }

  /* Messages area — flex child, scrollable, takes remaining space */
  .chat-messages {
    flex: 1 1 0%;
    min-height: 0;
    padding: 14px 12px;
    gap: 10px;
    max-height: none; /* Let flex handle it */
  }

  .chat-message {
    max-width: 90%;
  }

  .chat-message__avatar {
    width: 24px;
    height: 24px;
    min-width: 24px;
    font-size: 10px;
  }

  .chat-message__bubble {
    padding: 8px 12px;
    font-size: 0.8125rem;
    line-height: 1.45;
  }

  /* Quick replies — smaller on mobile */
  .chat-quick-replies {
    margin-left: 32px;
    gap: 6px;
  }

  .chat-quick-reply {
    padding: 5px 12px;
    font-size: 0.75rem;
  }

  /* Typing indicator */
  .chat-typing {
    margin-left: 32px;
    padding: 8px 12px;
  }

  /* Input area — pinned at bottom, safe area aware */
  .chat-input-area {
    padding: 10px 12px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    gap: 6px;
  }

  .chat-input {
    padding: 8px 14px;
    font-size: 16px; /* MUST be 16px to prevent iOS zoom */
    max-height: 60px;
  }

  .chat-send {
    width: 36px;
    height: 36px;
  }

  .chat-send svg {
    width: 14px;
    height: 14px;
  }

  /* Footer note */
  .chat-footer-note {
    font-size: 0.625rem;
    padding: 2px 0 max(6px, env(safe-area-inset-bottom));
  }

  /* Position trigger above WhatsApp button */
  .chat-trigger {
    bottom: 92px;
    right: 16px;
    width: 48px;
    height: 48px;
  }

  .chat-trigger svg {
    width: 22px;
    height: 22px;
  }
}

/* ── Small phones (iPhone SE, etc.) ── */
@media (max-width: 375px) {
  .chat-window {
    min-height: 240px;
  }

  .chat-messages {
    padding: 12px 10px;
  }

  .chat-header {
    padding: 10px 12px;
  }

  .chat-header__avatar {
    width: 30px;
    height: 30px;
    font-size: 13px;
  }
}

/* ── Hide trigger when chat is open ── */
.chat-trigger.hidden {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.8);
}

/* ── Overlay backdrop for mobile ── */
.chat-overlay {
  display: none;
}

@media (max-width: 480px) {
  .chat-overlay {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 999;
    background: rgba(0, 0, 0, 0.4);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
  }

  .chat-overlay.active {
    opacity: 1;
    pointer-events: all;
  }
}
