/* ================================================================
   Shared Chat Widget — Emma (AI Insurance Assistant)
   Standalone CSS — not processed by Tailwind.
   Phase 6.2 consolidation: one source of truth for 5 sites. Per-site
   theming via CSS custom properties set on :root or .chat-widget in
   each site's main.css / base.njk. site-whole uses its own emma-*
   fork in packages/site-whole/src/assets/css/chat-widget.css —
   preserved as a code fork because its NJK, JS, and CSS all use the
   emma-* naming convention and cannot be easily reconciled without
   rewriting all three layers.

   Theme contract (override any of these in the consuming site):
     --chat-primary      Primary accent (FAB, user bubble, buttons)
     --chat-primary-deep Hover / pressed state for --chat-primary
     --chat-primary-glow rgba() focus-ring / outer pulse tint
     --chat-on-primary   Foreground for text on primary background
     --chat-ink          Body text / message text color
     --chat-charcoal     Header background
     --chat-warm         Messages area background
     --chat-mist         Muted text (status, placeholder)
     --chat-cloud        Input borders / dividers
     --chat-white        Surface / panel background
     --chat-radius       Panel corner radius
     --chat-font         Primary font family for the widget

   Defaults below use the green theme (4 of 5 sites). site-term
   overrides to navy/gold; see packages/site-term/src/css/main.css.
   ================================================================ */

.chat-widget {
  --chat-primary: #047857;
  --chat-primary-deep: #065f46;
  --chat-primary-glow: rgba(4, 120, 87, 0.12);
  --chat-on-primary: #ffffff;
  --chat-ink: #0c1220;
  --chat-charcoal: #1a2335;
  --chat-warm: #faf8f5;
  --chat-mist: #8b95a7;
  --chat-cloud: #e2e8f0;
  --chat-white: #ffffff;
  --chat-radius: 20px;
  --chat-font: "Geist", system-ui, -apple-system, sans-serif;
  font-family: var(--chat-font);
}

/* ── FAB (Floating Action Button) ─────────────────────────────── */

.chat-trigger {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--chat-primary);
  color: var(--chat-on-primary);
  border: none;
  cursor: pointer;
  z-index: 88;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px var(--chat-primary-glow);
  transition: transform 0.2s, box-shadow 0.2s;
  animation: chatPulse 2s infinite;
}

.chat-trigger:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 24px var(--chat-primary-glow);
}

.chat-trigger.is-open,
.chat-widget.is-open .chat-trigger {
  animation: none;
}

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

.chat-trigger-icon,
.chat-trigger-close {
  transition: opacity 0.15s, transform 0.15s;
  position: absolute;
}

.chat-trigger-icon { opacity: 1; transform: scale(1); }
.chat-trigger-close { opacity: 0; transform: scale(0.5); }
.chat-trigger.is-open .chat-trigger-icon,
.chat-widget.is-open .chat-trigger-icon { opacity: 0; transform: scale(0.5); }
.chat-trigger.is-open .chat-trigger-close,
.chat-widget.is-open .chat-trigger-close { opacity: 1; transform: scale(1); }

@keyframes chatPulse {
  0%, 100% { box-shadow: 0 4px 16px var(--chat-primary-glow); }
  50%     { box-shadow: 0 4px 24px var(--chat-primary-glow), 0 0 0 8px var(--chat-primary-glow); }
}

/* ── Nudge Tooltip ────────────────────────────────────────────── */

.chat-nudge {
  position: fixed;
  bottom: 92px;
  right: 24px;
  background: var(--chat-white);
  color: var(--chat-ink);
  padding: 12px 36px 12px 16px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
  z-index: 88;
  max-width: 260px;
  font-size: 14px;
  line-height: 1.4;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.3s, transform 0.3s;
  pointer-events: none;
}

.chat-nudge.is-visible,
.chat-nudge.visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.chat-nudge::after {
  content: "";
  position: absolute;
  bottom: -6px;
  right: 28px;
  width: 12px;
  height: 12px;
  background: var(--chat-white);
  transform: rotate(45deg);
  box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.06);
}

.chat-nudge-close {
  position: absolute;
  top: 4px;
  right: 8px;
  background: none;
  border: none;
  color: var(--chat-mist);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 2px;
}

/* ── Chat Panel ───────────────────────────────────────────────── */

.chat-panel {
  position: fixed;
  bottom: 96px;
  right: 24px;
  width: 380px;
  max-height: 560px;
  background: var(--chat-white);
  border-radius: var(--chat-radius);
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.15);
  z-index: 89;
  display: flex;
  flex-direction: column;
  opacity: 0;
  transform: scale(0.95) translateY(8px);
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
  overflow: hidden;
}

.chat-panel.is-open,
.chat-panel.open,
.chat-widget.is-open .chat-panel {
  opacity: 1;
  transform: scale(1) translateY(0);
  pointer-events: auto;
}

/* ── Header ───────────────────────────────────────────────────── */

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  background: var(--chat-charcoal);
  color: var(--chat-white);
  flex-shrink: 0;
  border-radius: var(--chat-radius) var(--chat-radius) 0 0;
}

.chat-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--chat-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  color: var(--chat-on-primary);
  flex-shrink: 0;
  overflow: hidden;
}

.chat-avatar img,
.chat-header-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.chat-header-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
}

.chat-header-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

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

.chat-header-name,
.chat-header-title {
  font-weight: 600;
  font-size: 15px;
  line-height: 1.2;
}

.chat-header-status {
  font-size: 12px;
  color: #94a3b8;
  display: flex;
  align-items: center;
  gap: 4px;
}

.chat-status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #22c55e;
  display: inline-block;
}

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

.chat-header-btn,
.chat-header-actions button,
.chat-new-convo,
.chat-close {
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, background 0.15s;
  font-size: 22px;
  line-height: 1;
}

.chat-header-btn:hover,
.chat-header-actions button:hover,
.chat-new-convo:hover,
.chat-close:hover {
  color: var(--chat-white);
  background: rgba(255, 255, 255, 0.1);
}

.chat-header-btn svg,
.chat-header-actions button svg {
  width: 18px;
  height: 18px;
}

/* ── Messages Area ────────────────────────────────────────────── */

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: var(--chat-warm);
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 200px;
  max-height: 380px;
}

.chat-msg,
.chat-message,
.chat-bubble {
  max-width: 85%;
  padding: 10px 14px;
  font-size: 14px;
  line-height: 1.5;
  animation: chatMsgIn 0.15s ease-out;
  word-wrap: break-word;
}

.chat-msg-user,
.chat-msg.user,
.chat-message-user,
.chat-bubble-user {
  align-self: flex-end;
  background: var(--chat-primary);
  color: var(--chat-on-primary);
  border-radius: 16px 16px 4px 16px;
}

.chat-msg-assistant,
.chat-msg.assistant,
.chat-message-assistant,
.chat-bubble-assistant {
  align-self: flex-start;
  background: var(--chat-white);
  color: var(--chat-ink);
  border-radius: 16px 16px 16px 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

.chat-msg-assistant strong,
.chat-message-assistant strong { font-weight: 600; }
.chat-msg-assistant em,
.chat-message-assistant em { font-style: italic; }
.chat-msg-assistant code,
.chat-message-assistant code {
  background: #f1f5f9;
  padding: 1px 4px;
  border-radius: 3px;
  font-size: 13px;
}

.chat-msg-assistant p,
.chat-message-assistant p { margin: 0 0 8px 0; }
.chat-msg-assistant p:last-child,
.chat-message-assistant p:last-child { margin-bottom: 0; }

.chat-msg-assistant ul,
.chat-msg-assistant ol,
.chat-message-assistant ul,
.chat-message-assistant ol {
  margin: 4px 0 8px;
  padding-left: 18px;
}

.chat-msg-assistant li,
.chat-message-assistant li {
  margin-bottom: 2px;
}

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

/* ── Typing Indicator ─────────────────────────────────────────── */

.chat-typing {
  align-self: flex-start;
  display: flex;
  gap: 4px;
  padding: 12px 16px;
  background: var(--chat-white);
  border-radius: 16px 16px 16px 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

.chat-typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--chat-mist);
  animation: chatDotBounce 0.6s infinite alternate;
}

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

@keyframes chatDotBounce {
  0%   { transform: translateY(0);    opacity: 0.4; }
  100% { transform: translateY(-4px); opacity: 1; }
}

/* ── Lead Capture Card ────────────────────────────────────────── */

.chat-lead-card {
  align-self: flex-start;
  margin: 8px 0;
  padding: 14px;
  background: var(--chat-white);
  border: 1px solid var(--chat-cloud);
  border-radius: 12px;
  max-width: 90%;
  animation: chatMsgIn 0.2s ease-out;
}

.chat-lead-card h4 {
  font-size: 14px;
  font-weight: 600;
  margin: 0 0 10px;
  color: var(--chat-ink);
}

.chat-lead-card p {
  font-size: 13px;
  color: var(--chat-mist);
  margin-bottom: 10px;
}

.chat-lead-input,
.chat-lead-card input,
.chat-lead-card select {
  display: block;
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--chat-cloud);
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 8px;
  outline: none;
  font-family: inherit;
  transition: border-color 0.15s;
  box-sizing: border-box;
  background: var(--chat-white);
  color: var(--chat-ink);
}

.chat-lead-input:focus,
.chat-lead-card input:focus,
.chat-lead-card select:focus {
  border-color: var(--chat-primary);
  box-shadow: 0 0 0 2px var(--chat-primary-glow);
}

.chat-lead-input.has-error,
.chat-lead-card input.has-error,
.chat-lead-card input.border-red-500,
.chat-lead-card select.border-red-500 {
  border-color: #ef4444;
}

.chat-lead-error,
.chat-lead-card .field-error,
.chat-lead-card .form-error {
  color: #dc2626;
  font-size: 12px;
  margin: -4px 0 6px;
}

.chat-lead-submit {
  width: 100%;
  padding: 9px;
  background: var(--chat-primary);
  color: var(--chat-on-primary);
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  font-family: inherit;
}

.chat-lead-submit:hover {
  background: var(--chat-primary-deep);
}

.chat-lead-submit:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.chat-lead-success {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  color: var(--chat-primary-deep);
  font-size: 14px;
  font-weight: 500;
}

.chat-lead-success svg {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

/* Fallback CTA (used by some sites' error responses) */
.chat-fallback-btn {
  display: inline-block;
  margin-top: 8px;
  padding: 8px 16px;
  background: var(--chat-primary);
  color: var(--chat-on-primary);
  border: none;
  border-radius: 8px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s;
}

.chat-fallback-btn:hover {
  background: var(--chat-primary-deep);
}

/* ── Input Area ───────────────────────────────────────────────── */

.chat-input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--chat-cloud);
  background: var(--chat-white);
  border-radius: 0 0 var(--chat-radius) var(--chat-radius);
  flex-shrink: 0;
}

.chat-input,
.chat-input-area textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--chat-cloud);
  border-radius: 12px;
  outline: none;
  font-size: 14px;
  line-height: 1.4;
  min-height: 40px;
  max-height: 100px;
  padding: 8px 12px;
  font-family: inherit;
  color: var(--chat-ink);
  background: var(--chat-warm);
  transition: border-color 0.15s;
}

.chat-input:focus,
.chat-input-area textarea:focus {
  border-color: var(--chat-primary);
  box-shadow: 0 0 0 3px var(--chat-primary-glow);
}

.chat-input::placeholder,
.chat-input-area textarea::placeholder {
  color: var(--chat-mist);
}

.chat-send,
.chat-input-area button[id="chatSend"] {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--chat-primary);
  color: var(--chat-on-primary);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, opacity 0.15s;
}

.chat-send:hover:not(:disabled),
.chat-input-area button[id="chatSend"]:hover:not(:disabled) {
  background: var(--chat-primary-deep);
}

.chat-send:disabled,
.chat-input-area button[id="chatSend"]:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

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

/* ── Toast (used by site-mortprot) ────────────────────────────── */

.chat-toast {
  position: fixed;
  bottom: 160px;
  right: 24px;
  z-index: 90;
  pointer-events: none;
}

.chat-toast-msg {
  background: var(--chat-charcoal);
  color: var(--chat-white);
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 13px;
  animation: chatMsgIn 0.2s ease;
}

/* ── Bridge: shared-form class states inside lead card ────────── */
/* quote-form.js toggles Tailwind-named classes — this CSS isn't
   Tailwind-processed, so provide explicit rules. Matches the bridge
   block in site-whole's emma-lead-card styling. */
.chat-lead-card .hidden { display: none !important; }
.chat-lead-card .btn-disabled {
  opacity: 0.5;
  cursor: default;
}

/* ── Hidden utility (parity w/ site-whole's emma-hidden) ──────── */

.chat-hidden { display: none !important; }

/* ── Mobile (below 1280px) ────────────────────────────────────── */

@media (max-width: 1279px) {
  .chat-trigger {
    bottom: 80px; /* clear fixed CTA bar */
  }

  .chat-nudge {
    bottom: 148px;
  }
}

@media (max-width: 640px) {
  .chat-panel {
    inset: 0;
    width: 100%;
    max-height: 100%;
    border-radius: 0;
    bottom: 0;
    right: 0;
  }

  .chat-header {
    border-radius: 0;
  }

  .chat-input-area {
    border-radius: 0;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  .chat-messages {
    max-height: none;
    flex: 1;
  }

  /* Hide FAB when panel is open on mobile */
  .chat-trigger.is-open,
  .chat-widget.is-open .chat-trigger {
    display: none;
  }

  .chat-nudge {
    bottom: 146px;
    right: 16px;
  }

  .chat-toast {
    bottom: auto;
    top: 80px;
    right: 16px;
    left: 16px;
  }
}

/* Hide chat trigger when a site-level sidebar form is open (mobile) */
@media (max-width: 1200px) {
  .sidebar-form.open ~ .chat-widget .chat-trigger {
    display: none;
  }
}

/* ── Scrollbar ────────────────────────────────────────────────── */

.chat-messages::-webkit-scrollbar {
  width: 4px;
}

.chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: var(--chat-cloud);
  border-radius: 2px;
}

/* ── Reduced Motion ───────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
  .chat-trigger,
  .chat-panel,
  .chat-msg,
  .chat-message,
  .chat-bubble,
  .chat-nudge,
  .chat-typing-dot,
  .chat-lead-card {
    animation: none !important;
    transition: none !important;
  }
}
