/* AI Chat Advisor Widget — TopSolarServices.com
   Self-contained, namespaced --chat-* tokens (solar palette: warm orange/gold + green),
   so it never collides with the site's :root vars. Dark navy panel matches the site's
   --color-primary navy with solar accents. */

#chat-toggle, #chat-panel {
  --chat-accent:      #f97316;   /* solar orange (matches site --color-accent) */
  --chat-accent-2:    #fbbf24;   /* gold (links, highlights) */
  --chat-success:     #10b981;   /* green online dot */
  --chat-danger:      #ef4444;
  --chat-gradient:    linear-gradient(135deg, #f97316 0%, #fbbf24 100%);
  --chat-bg:          #0a1628;   /* navy panel (site --color-primary) */
  --chat-bg-2:        #0f1f38;
  --chat-card:        #15294a;
  --chat-border:      rgba(255,255,255,0.10);
  --chat-text:        #ffffff;
  --chat-text-light:  #dde5f0;
  --chat-text-muted:  #8b97ad;
  --chat-radius-xl:   16px;
  --chat-radius-md:   10px;
  --chat-glow:        rgba(249,115,22,0.30);
  --chat-font:        'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  --chat-mono:        ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

/* Launcher pill (bottom-right) */
#chat-toggle {
  position: fixed;
  right: 24px;
  bottom: 80px; /* sit above the site's floating "Get Matched — Free" CTA (bottom ~16–60px) */
  z-index: 9999;
  height: 56px;
  padding: 0 22px 0 18px;
  border-radius: 100px;
  background: var(--chat-gradient);
  color: var(--chat-text);
  border: 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-family: var(--chat-font);
  box-shadow: 0 8px 24px rgba(249,115,22,0.45), 0 0 32px var(--chat-glow);
  transition: transform 200ms ease;
}
#chat-toggle:hover { transform: scale(1.05); }
#chat-toggle i { font-size: 1.25rem; line-height: 1; }
#chat-toggle .chat-toggle-label { font-weight: 600; font-size: 0.95rem; white-space: nowrap; }
#chat-toggle .chat-badge {
  position: absolute;
  top: 2px; right: 2px;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--chat-danger);
  border: 2px solid var(--chat-bg);
  display: none;
}
#chat-toggle.has-unread .chat-badge { display: block; }
/* When the panel is open, hide the launcher — the panel's own close button handles closing. */
#chat-toggle.is-open { display: none; }

/* Panel */
#chat-panel {
  position: fixed;
  right: 24px;
  bottom: 96px;
  z-index: 9998;
  width: 380px;
  height: 540px;
  max-height: calc(100vh - 120px);
  background: var(--chat-bg);
  border: 1px solid var(--chat-border);
  border-radius: var(--chat-radius-xl);
  box-shadow: 0 20px 60px rgba(0,0,0,0.55), 0 0 40px rgba(249,115,22,0.16);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-family: var(--chat-font);
  transform-origin: bottom right;
  transform: scale(0.95) translateY(8px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 250ms ease, transform 250ms ease;
}
#chat-panel.is-open { opacity: 1; pointer-events: auto; transform: scale(1) translateY(0); }
/* Maximized state — toggled by the header expand button. */
#chat-panel.is-expanded {
  width: min(720px, calc(100vw - 32px));
  height: calc(100vh - 100px);
}

/* Drag-to-resize grip (top-left corner, since the panel is anchored bottom-right). */
.chat-resize-handle {
  position: absolute;
  top: 0; left: 0;
  width: 24px; height: 24px;
  cursor: nwse-resize;
  z-index: 6;
  touch-action: none;
}
.chat-resize-handle::before {
  content: '';
  position: absolute;
  top: 8px; left: 8px;
  width: 9px; height: 9px;
  border-top: 2px solid var(--chat-text-muted);
  border-left: 2px solid var(--chat-text-muted);
  border-top-left-radius: 5px;
  transition: border-color 200ms ease;
}
.chat-resize-handle:hover::before { border-color: var(--chat-accent); }

@media (max-width: 540px) {
  #chat-panel { right: 12px; left: 12px; width: auto; bottom: 84px; height: calc(100vh - 110px); }
  #chat-toggle { right: 16px; bottom: 72px; } /* clear the full-width Get Matched bar */
}

/* Header */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  height: 60px;
  border-bottom: 1px solid var(--chat-border);
  background: var(--chat-bg-2);
  flex-shrink: 0;
}
.chat-header-left { display: flex; align-items: center; gap: 10px; }
.chat-header-left .avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--chat-gradient);
  display: inline-flex; align-items: center; justify-content: center;
  font-family: var(--chat-mono); font-size: 0.66rem; font-weight: 700;
  color: var(--chat-text); box-shadow: 0 0 12px var(--chat-glow);
}
.chat-header-info { display: flex; flex-direction: column; gap: 2px; line-height: 1.2; }
.chat-header-info .name { color: var(--chat-text); font-size: 0.92rem; font-weight: 600; }
.chat-header-info .status { color: var(--chat-text-muted); font-size: 0.72rem; display: inline-flex; align-items: center; gap: 6px; }
.chat-header-info .status::before {
  content: ''; width: 6px; height: 6px; border-radius: 50%;
  background: var(--chat-success); animation: chatPulse 2s ease-in-out infinite;
}
.chat-header-actions { display: flex; gap: 4px; }
.chat-header-actions button {
  width: 30px; height: 30px; border-radius: 50%;
  background: transparent; border: 0; color: var(--chat-text-muted);
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
  transition: background 200ms ease, color 200ms ease;
}
.chat-header-actions button:hover { background: rgba(255,255,255,0.06); color: var(--chat-text); }

/* Messages */
.chat-messages {
  flex: 1; overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth;
  position: relative; /* scroll reference for scrolling a new answer's top into view */
}
.chat-messages::-webkit-scrollbar { width: 6px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--chat-border); border-radius: 3px; }
.chat-messages::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.15); }

.chat-msg { display: flex; gap: 8px; align-items: flex-end; max-width: 100%; animation: chatMsgIn 240ms ease; }
.chat-msg.user { justify-content: flex-end; }
@keyframes chatMsgIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }
@keyframes chatPulse { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }
@keyframes chatTyping { 0%,60%,100% { transform: translateY(0); opacity: 0.5; } 30% { transform: translateY(-4px); opacity: 1; } }

.chat-msg .avatar {
  flex-shrink: 0; width: 28px; height: 28px; border-radius: 50%;
  background: var(--chat-gradient);
  display: inline-flex; align-items: center; justify-content: center;
  font-family: var(--chat-mono); font-size: 0.62rem; font-weight: 600;
  color: var(--chat-text); letter-spacing: 0.04em; box-shadow: 0 0 12px var(--chat-glow);
}
.chat-bubble {
  padding: 10px 14px; font-size: 0.92rem; line-height: 1.5;
  border-radius: 18px 18px 18px 4px; word-wrap: break-word; max-width: 78%;
}
.chat-msg.user .chat-bubble {
  background: var(--chat-gradient); color: var(--chat-text);
  border-radius: 18px 18px 4px 18px; box-shadow: 0 4px 16px rgba(249,115,22,0.25);
}
.chat-msg.assistant .chat-bubble {
  background: rgba(255,255,255,0.04); border: 1px solid var(--chat-border); color: var(--chat-text-light);
}
.chat-bubble a { color: var(--chat-accent-2); text-decoration: underline; text-underline-offset: 2px; }
.chat-bubble strong { color: var(--chat-text); }
.chat-bubble ul { margin: 6px 0 6px 18px; padding: 0; }
.chat-bubble li { margin-bottom: 2px; }

.chat-time { font-family: var(--chat-mono); font-size: 0.62rem; color: var(--chat-text-muted); margin-top: 4px; letter-spacing: 0.04em; }

.chat-typing {
  display: inline-flex; align-items: center; gap: 4px; padding: 12px 14px;
  background: rgba(255,255,255,0.04); border: 1px solid var(--chat-border); border-radius: 18px 18px 18px 4px;
}
.chat-typing span { width: 6px; height: 6px; border-radius: 50%; background: var(--chat-accent); animation: chatTyping 1.4s ease-in-out infinite; }
.chat-typing span:nth-child(2) { animation-delay: 0.2s; }
.chat-typing span:nth-child(3) { animation-delay: 0.4s; }

/* "Want some suggested questions?" toggle — list hidden until the user opts in. */
.chat-prompts-toggle {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  width: calc(100% - 24px); margin: 8px 12px 0; padding: 8px 14px;
  background: var(--chat-bg-2); border: 1px solid var(--chat-border); border-radius: 100px;
  color: var(--chat-text-light); font-family: var(--chat-font); font-size: 0.82rem;
  cursor: pointer; flex-shrink: 0;
  transition: border-color 200ms ease, color 200ms ease, background 200ms ease;
}
.chat-prompts-toggle:hover { border-color: var(--chat-accent); color: var(--chat-text); background: rgba(249,115,22,0.10); }
.chat-prompts-toggle .fa-lightbulb { color: var(--chat-accent-2); margin-right: 4px; }
.chat-prompts-toggle .chevron { transition: transform 200ms ease; font-size: 0.72rem; }
.chat-prompts-toggle[aria-expanded="true"] .chevron { transform: rotate(180deg); }

.chat-prompts {
  display: flex; flex-wrap: wrap; gap: 8px; padding: 8px 12px 4px; flex-shrink: 0;
  max-height: 120px; overflow-y: auto; scrollbar-width: none;
}
.chat-prompts[hidden] { display: none; }
.chat-prompts::-webkit-scrollbar { display: none; }
.chat-prompts button {
  flex: 0 0 auto; background: var(--chat-bg-2); border: 1px solid var(--chat-border);
  color: var(--chat-text-light); font-size: 0.78rem; padding: 6px 12px; border-radius: 100px;
  cursor: pointer; white-space: nowrap; font-family: var(--chat-font);
  transition: border-color 200ms ease, color 200ms ease, background 200ms ease;
}
.chat-prompts button:hover { border-color: var(--chat-accent); color: var(--chat-text); background: rgba(249,115,22,0.10); }

/* Input row */
.chat-input-row {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px;
  border-top: 1px solid var(--chat-border); flex-shrink: 0; background: var(--chat-bg);
}
.chat-input {
  flex: 1; background: var(--chat-card); border: 1px solid var(--chat-border); border-radius: 18px;
  padding: 10px 14px; color: var(--chat-text); font-size: 0.92rem; resize: none; outline: none;
  font-family: var(--chat-font); line-height: 1.4; max-height: 90px; min-height: 40px;
}
.chat-input:focus { border-color: var(--chat-accent); }
.chat-input::placeholder { color: var(--chat-text-muted); }

.chat-send {
  width: 38px; height: 38px; border-radius: 50%; background: var(--chat-gradient);
  border: 0; color: var(--chat-text); cursor: pointer; display: inline-flex;
  align-items: center; justify-content: center; flex-shrink: 0;
  transition: transform 150ms ease, opacity 200ms ease;
}
.chat-send:hover:not(:disabled) { transform: scale(1.05); }
.chat-send:disabled { background: rgba(255,255,255,0.08); cursor: not-allowed; opacity: 0.5; }

/* Lead capture inline */
.chat-lead {
  background: rgba(249,115,22,0.10); border: 1px solid rgba(249,115,22,0.25);
  border-radius: var(--chat-radius-md); padding: 12px; margin: 8px 0;
}
.chat-lead p { margin: 0 0 8px; color: var(--chat-text-light); font-size: 0.85rem; }
.chat-lead form { display: flex; gap: 6px; }
.chat-lead input {
  flex: 1; padding: 8px 12px; border-radius: 100px; background: var(--chat-bg);
  border: 1px solid var(--chat-border); color: var(--chat-text); font-size: 0.85rem; outline: none;
}
.chat-lead input:focus { border-color: var(--chat-accent); }
.chat-lead button { padding: 8px 14px; border-radius: 100px; border: 0; background: var(--chat-gradient); color: var(--chat-text); font-size: 0.82rem; cursor: pointer; }
.chat-lead button:disabled { opacity: 0.5; cursor: not-allowed; }
.chat-lead-note { margin: 8px 0 0; font-size: 0.82rem; }

/* Respect reduced-motion preferences */
@media (prefers-reduced-motion: reduce) {
  #chat-toggle, #chat-panel, .chat-msg, .chat-send, .chat-header-actions button,
  .chat-prompts-toggle, .chat-prompts button, .chat-resize-handle::before, .chat-prompts-toggle .chevron {
    transition: none !important; animation: none !important;
  }
  .chat-header-info .status::before, .chat-typing span { animation: none !important; }
}
