/* ===== AI CHAT WIDGET ===== */

.chat-container {
  position: fixed;
  bottom: 100px;
  right: 24px;
  width: 720px;
  max-width: calc(100% - 16px);
  max-height: 85vh;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
  z-index: 1040;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: chatSlideUp 0.3s ease;
}

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

/* Header */
.chat-header {
  background: var(--primary-gradient);
  color: white;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

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

.chat-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
}

.chat-status {
  display: block;
  font-size: 0.75rem;
  opacity: 0.85;
}

.chat-header-actions {
  display: flex;
  gap: 4px;
}

.chat-header-btn {
  background: none;
  border: none;
  color: white;
  padding: 8px;
  min-width: 36px;
  min-height: 36px;
  cursor: pointer;
  border-radius: 6px;
  transition: background 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.chat-header-btn:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

/* Phase progress bar */
.chat-progress {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 16px;
  background: #faf5ff;
  border-bottom: 1px solid #ede9fe;
  flex-shrink: 0;
  gap: 0;
}

/* Legal disclaimer banner */
.chat-legal-banner {
  background: #fef3cd;
  padding: 6px 12px;
  font-size: 0.72rem;
  color: #856404;
  border-bottom: 1px solid #f0e2a0;
  flex-shrink: 0;
  text-align: center;
  line-height: 1.3;
}

.chat-progress-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  opacity: 0.4;
  transition: opacity 0.3s, transform 0.3s;
}

.chat-progress-step span {
  font-size: 0.6rem;
  font-weight: 500;
  color: #6b7280;
  white-space: nowrap;
}

.chat-progress-step.active {
  opacity: 1;
  transform: scale(1.05);
}

.chat-progress-step.active span {
  color: var(--ai-purple-dark, #6d28d9);
  font-weight: 600;
}

.chat-progress-step.completed {
  opacity: 0.8;
}

.chat-progress-step.completed .chat-progress-icon {
  background: var(--ai-purple, #8b5cf6);
  color: white;
}

.chat-progress-icon {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #e5e7eb;
  color: #6b7280;
  transition: background 0.3s, color 0.3s;
}

.chat-progress-step.active .chat-progress-icon {
  background: var(--ai-purple, #8b5cf6);
  color: white;
  box-shadow: 0 2px 8px rgba(139, 92, 246, 0.3);
}

.chat-progress-line {
  flex: 1;
  height: 2px;
  background: #e5e7eb;
  margin: 0 4px;
  margin-bottom: 16px;
  transition: background 0.3s;
}

.chat-progress-line.completed {
  background: var(--ai-purple, #8b5cf6);
}

/* Quick replies */
.chat-quick-replies {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 14px 16px 14px;
  max-height: 120px;
  overflow-y: auto;
}

.chat-quick-replies:empty {
  display: none;
}

.chat-quick-reply-btn {
  background: #f3f0ff;
  border: 1px solid #ddd6fe;
  color: var(--ai-purple-dark, #6d28d9);
  font-size: 0.78rem;
  padding: 8px 14px;
  min-height: 36px;
  border-radius: 16px;
  cursor: pointer;
  white-space: normal;
  transition: background 0.2s, transform 0.15s;
  font-family: inherit;
}

.chat-quick-reply-btn:hover {
  background: #ede9fe;
  transform: translateY(-1px);
}

.chat-quick-reply-btn:focus-visible {
  outline: 2px solid var(--ai-purple, #8b5cf6);
  outline-offset: 2px;
}

.chat-quick-reply-btn:active {
  transform: scale(0.97);
}

/* Messages area */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  min-height: 400px;
  max-height: 640px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-messages.form-visible {
  min-height: 150px;
  max-height: 300px;
}

.chat-bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 0.9rem;
  line-height: 1.45;
  word-wrap: break-word;
}

.chat-bubble-assistant {
  background: #f3f4f6;
  color: #1f2937;
  border-bottom-left-radius: 4px;
  align-self: flex-start;
  position: relative;
}

.chat-bubble-assistant a {
  color: var(--ai-purple-dark, #6d28d9);
  text-decoration: underline;
  word-break: break-all;
}

.chat-bubble-assistant a:hover {
  color: var(--ai-purple, #8B5CF6);
}

.chat-bubble-user {
  background: var(--primary-gradient);
  color: white;
  border-bottom-right-radius: 4px;
  align-self: flex-end;
}

/* Typing indicator */
.chat-typing {
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: #6b7280;
  font-size: 0.8rem;
}

.typing-dots {
  display: flex;
  gap: 3px;
}

.typing-dots span {
  width: 6px;
  height: 6px;
  background: #9ca3af;
  border-radius: 50%;
  animation: typingBounce 1.4s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* Input area */
.chat-input-area {
  padding: 12px 16px;
  border-top: 1px solid #e5e7eb;
  flex-shrink: 0;
}

.chat-input-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #f9fafb;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 8px 12px;
  transition: border-color 0.2s;
}

.chat-input-wrapper:focus-within {
  border-color: var(--ai-purple);
}

#chatInput {
  flex: 1;
  border: none;
  background: none;
  resize: none;
  font-size: 0.9rem;
  line-height: 1.4;
  max-height: 80px;
  outline: none; /* parent .chat-input-wrapper:focus-within provides visual indicator */
  font-family: inherit;
}

.chat-send-btn {
  background: var(--primary-gradient);
  border: none;
  color: white;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: transform 0.2s, opacity 0.2s;
}

.chat-send-btn:hover {
  transform: scale(1.1);
}

.chat-send-btn:focus-visible {
  outline: 2px solid var(--ai-purple, #8b5cf6);
  outline-offset: 2px;
}

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

.chat-input-meta {
  display: flex;
  justify-content: space-between;
  padding: 6px 2px 0;
  font-size: 0.7rem;
  color: #6b7280;
}

.chat-disclaimer {
  font-style: italic;
}

/* Contact form */
.chat-contact-form {
  padding: 16px;
  border-top: 1px solid #e5e7eb;
  background: #faf5ff;
  overflow-y: auto;
  max-height: 50vh;
  flex-shrink: 0;
}

.chat-contact-form h6 {
  margin: 0 0 12px;
  color: var(--ai-purple-dark);
  font-size: 0.95rem;
}

.chat-contact-pdf-note {
  display: block;
  margin: -6px 0 12px;
  padding: 6px 10px;
  font-size: 0.72rem;
  line-height: 1.4;
  color: #6b21a8;
  background: #f3e8ff;
  border-left: 3px solid var(--ai-purple, #8b5cf6);
  border-radius: 4px;
}

.chat-form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 10px;
}

.chat-form-row input[type="text"],
.chat-form-row input[type="tel"] {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  font-size: 0.85rem;
  transition: border-color 0.2s;
  box-sizing: border-box;
}

.chat-form-row input:focus {
  outline: 2px solid var(--ai-purple, #8b5cf6);
  outline-offset: -1px;
  border-color: var(--ai-purple);
}

.chat-email-toggle {
  font-size: 0.8rem;
  color: var(--ai-purple);
  text-decoration: none;
  font-weight: 500;
}

.chat-email-toggle:hover {
  text-decoration: underline;
}

.chat-form-group {
  margin-bottom: 10px;
}

.chat-form-group input[type="text"],
.chat-form-group input[type="tel"],
.chat-form-group input[type="email"] {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  font-size: 0.85rem;
  transition: border-color 0.2s;
  box-sizing: border-box;
}

.chat-form-group input:focus {
  outline: 2px solid var(--ai-purple, #8b5cf6);
  outline-offset: -1px;
  border-color: var(--ai-purple);
}

.chat-checkbox {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 0.75rem;
  color: #6b7280;
  cursor: pointer;
}

.chat-checkbox input[type="checkbox"] {
  margin-top: 2px;
  flex-shrink: 0;
}

.chat-contact-submit-btn {
  width: 100%;
  padding: 10px;
  background: var(--primary-gradient);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s, box-shadow 0.2s;
}

.chat-contact-submit-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(139, 92, 246, 0.3);
}

.chat-contact-submit-btn:focus-visible {
  outline: 2px solid var(--ai-purple, #8b5cf6);
  outline-offset: 2px;
}

.chat-contact-submit-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.chat-contact-note {
  display: block;
  margin-top: 8px;
  font-size: 0.7rem;
  color: #6b7280;
}

.chat-contact-note a {
  color: var(--ai-purple);
}

/* Validation errors */
.chat-form-group input.chat-field-error {
  border-color: #dc2626;
  background-color: rgba(239, 68, 68, 0.05);
}

.chat-field-error-message {
  color: #dc2626;
  font-size: 0.72rem;
  margin-top: 3px;
  font-weight: 500;
}

.chat-checkbox.chat-consent-error {
  color: #dc2626;
}

.chat-contact-submit-btn.chat-btn-error {
  background: #dc2626;
}

/* Phase indicator badge */
.chat-phase-badge {
  text-align: center;
  margin: 4px 0;
}

.chat-phase-badge span {
  display: inline-block;
  background: #ede9fe;
  color: var(--ai-purple-dark);
  font-size: 0.7rem;
  padding: 3px 10px;
  border-radius: 10px;
  font-weight: 500;
}

/* Calculator mini-result cards */
.chat-calc-cards {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  margin-top: 4px;
}

.chat-calc-card {
  background: #f9fafb;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 10px 12px;
  transition: transform 0.15s;
}

.chat-calc-card-best {
  border-left: 3px solid var(--ai-purple, #8b5cf6);
  background: #faf5ff;
}

.chat-calc-card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 0.85rem;
}

.chat-calc-bank-logo {
  width: 24px;
  height: 24px;
  object-fit: contain;
  border-radius: 4px;
}

.chat-calc-badge {
  background: var(--ai-purple, #8b5cf6);
  color: white;
  font-size: 0.6rem;
  padding: 1px 6px;
  border-radius: 8px;
  font-weight: 600;
  margin-left: auto;
}

.chat-calc-card-body {
  display: flex;
  justify-content: space-between;
  gap: 6px;
  flex-wrap: wrap;
}

.chat-calc-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
}

.chat-calc-stat small {
  font-size: 0.65rem;
  color: #6b7280;
  margin-bottom: 2px;
}

.chat-calc-stat span {
  font-size: 0.8rem;
  font-weight: 600;
  color: #1f2937;
}

.chat-refi-positive span {
  color: #16a34a !important;
}

.chat-refi-negative span {
  color: #dc2626 !important;
}

/* Refinance decision badges */
.chat-decision-GREEN {
  background: #16a34a;
  color: #fff;
}

.chat-decision-YELLOW {
  background: #ca8a04;
  color: #fff;
}

.chat-decision-RED {
  background: #dc2626;
  color: #fff;
}

.chat-decision-NEUTRAL {
  background: #6b7280;
  color: #fff;
}

/* Screen reader only */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Scroll to bottom */
.chat-scroll-bottom {
  position: absolute;
  bottom: 120px;
  right: 20px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #fff;
  border: 1px solid #e5e7eb;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #6b7280;
  z-index: 1;
  transition: opacity 0.2s;
}

.chat-scroll-bottom:hover {
  background: #f3f4f6;
}

.chat-scroll-bottom:focus-visible {
  outline: 2px solid var(--ai-purple, #8b5cf6);
  outline-offset: 2px;
}

/* Offline banner */
.chat-offline-banner {
  background: #fef2f2;
  color: #991b1b;
  text-align: center;
  padding: 6px 12px;
  font-size: 0.75rem;
  border-bottom: 1px solid #fecaca;
  flex-shrink: 0;
}

/* Retry button */
.chat-retry-btn {
  display: inline-block;
  margin-top: 6px;
  padding: 4px 12px;
  background: #fff;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  font-size: 0.78rem;
  cursor: pointer;
  color: #374151;
  transition: background 0.2s;
}

.chat-retry-btn:hover {
  background: #f3f4f6;
}

.chat-retry-btn:focus-visible {
  outline: 2px solid var(--ai-purple, #8b5cf6);
  outline-offset: 2px;
}

/* New conversation button (shown after session end or successful contact submit) */
.chat-new-conv-wrapper {
  display: flex;
  justify-content: center;
  padding: 12px 8px 16px;
}

.chat-new-conv-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  background: var(--ai-purple, #8b5cf6);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  font-family: inherit;
}

.chat-new-conv-btn:hover:not(:disabled) {
  background: var(--ai-purple-dark, #7c3aed);
  transform: translateY(-1px);
}

.chat-new-conv-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.chat-new-conv-btn:focus-visible {
  outline: 2px solid var(--ai-purple, #8b5cf6);
  outline-offset: 2px;
}

/* ===== RESPONSIVE ===== */

@media (max-width: 991px) {
  .chat-container {
    bottom: 64px;
  }
}

@media (max-width: 576px) {
  .chat-container {
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    border-radius: 0;
    animation: none;
  }

  .chat-messages {
    max-height: none;
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
}

/* ===== FORM MODE (GRACEFUL DEGRADATION) ===== */

.chat-form-step {
  margin: 8px 12px;
  padding: 14px;
  background: #faf5ff;
  border: 1px solid #e9d5ff;
  border-radius: 12px;
  animation: chatFormFadeIn 0.3s ease;
}

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

.chat-form-step-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--ai-purple-dark);
  margin-bottom: 10px;
}

.chat-form-fields {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 10px;
}

.chat-form-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  font-size: 0.85rem;
  transition: border-color 0.2s;
  box-sizing: border-box;
  background: #fff;
}

.chat-form-input:focus {
  outline: 2px solid var(--ai-purple, #8b5cf6);
  outline-offset: -1px;
  border-color: var(--ai-purple);
}

.chat-form-next-btn {
  width: 100%;
  padding: 10px;
  background: var(--primary-gradient);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

.chat-form-next-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(139, 92, 246, 0.3);
}

.chat-form-next-btn:focus-visible {
  outline: 2px solid var(--ai-purple, #8b5cf6);
  outline-offset: 2px;
}

.chat-form-step-done {
  opacity: 0.5;
  pointer-events: none;
  position: relative;
}

.chat-form-step-done::after {
  content: '\2713';
  position: absolute;
  top: 10px;
  right: 12px;
  font-size: 1.1rem;
  color: #16a34a;
  font-weight: 700;
}

.chat-form-field-error {
  border-color: #dc2626 !important;
  background-color: rgba(239, 68, 68, 0.05);
}

.chat-form-error-msg {
  color: #dc2626;
  font-size: 0.72rem;
  margin-top: 3px;
  font-weight: 500;
}

/* ===== REDUCED MOTION ===== */

@media (prefers-reduced-motion: reduce) {
  .chat-container {
    animation: none;
  }

  .typing-dots span {
    animation: none;
    opacity: 1;
    transform: none;
  }

  .chat-form-step {
    animation: none;
  }

  .chat-progress-step,
  .chat-progress-icon,
  .chat-progress-line,
  .chat-quick-reply-btn,
  .chat-send-btn,
  .chat-contact-submit-btn,
  .chat-form-next-btn {
    transition: none;
  }
}
