/* ==========================================================================
   TechnAre AI Assistant — Premium Chat UI
   Loaded independently. Uses CSS custom properties from main.css.
   ========================================================================== */

/* ---------- Backdrop ---------- */
.chatbot-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 9998;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}
.chatbot-backdrop.active { opacity: 1; visibility: visible; }

.chatbot-open { overflow: hidden; touch-action: none; }

/* ---------- Toggle Button ---------- */
.chatbot-btn {
  position: fixed;
  bottom: max(20px, env(safe-area-inset-bottom, 0px));
  right: max(20px, env(safe-area-inset-right, 0px));
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #00b9d2, #009eb3);
  color: white;
  border: none;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 20px rgba(0, 185, 210, 0.35);
  cursor: pointer;
  z-index: 9999;
  transition: transform 0.35s cubic-bezier(0.16, 1, 0.3, 1);
  animation: ta-chat-glow 2.8s ease-in-out infinite;
}
.chatbot-btn::before {
  content: "";
  position: absolute;
  inset: -3px;
  border-radius: inherit;
  border: 2px solid rgba(0, 185, 210, 0.45);
  pointer-events: none;
  animation: ta-soft-ring 2.8s ease-out infinite;
}
.chatbot-btn > * {
  position: relative;
  z-index: 1;
}
.chatbot-btn:hover,
body.chatbot-open .chatbot-btn {
  animation: none;
}
.chatbot-btn:hover::before,
body.chatbot-open .chatbot-btn::before {
  animation: none;
  opacity: 0;
}
.chatbot-btn:hover {
  transform: scale(1.08) translateY(-2px);
  box-shadow: 0 8px 28px rgba(0, 185, 210, 0.5);
}
@keyframes ta-chat-glow {
  0%, 100% { box-shadow: 0 4px 18px rgba(0, 185, 210, 0.32); }
  50% { box-shadow: 0 6px 26px rgba(0, 185, 210, 0.55); }
}
@keyframes ta-soft-ring {
  0% { transform: scale(0.94); opacity: 0.55; }
  75% { transform: scale(1.28); opacity: 0; }
  100% { transform: scale(1.28); opacity: 0; }
}
.chatbot-btn i { font-size: 26px; }

/* Unread badge */
.chatbot-btn .chatbot-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 18px;
  height: 18px;
  background: #ef4444;
  border-radius: 50%;
  border: 2px solid #ffffff;
  display: none;
}
.chatbot-btn .chatbot-badge.show { display: block; }

/* ---------- Chat Window ---------- */
.chatbot-window {
  position: fixed;
  bottom: calc(90px + env(safe-area-inset-bottom, 0px));
  right: max(16px, env(safe-area-inset-right, 0px));
  width: min(400px, calc(100vw - 32px));
  max-height: min(600px, calc(100dvh - 120px - env(safe-area-inset-bottom, 0px)));
  height: min(560px, calc(100dvh - 120px - env(safe-area-inset-bottom, 0px)));
  background: #ffffff;
  border: 1px solid rgba(0, 0, 0, 0.08);
  border-radius: 20px;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.12), 0 2px 8px rgba(0, 0, 0, 0.06);
  display: flex;
  flex-direction: column;
  z-index: 9999;
  overflow: hidden;
  opacity: 0;
  visibility: hidden;
  transform: translateY(24px) scale(0.92);
  transition: all 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}
.chatbot-window.active {
  opacity: 1;
  visibility: visible;
  transform: translateY(0) scale(1);
}

/* ---------- Header ---------- */
.chatbot-header {
  background: linear-gradient(135deg, #00b9d2 0%, #0891b2 100%);
  color: white;
  padding: 16px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}

.chatbot-header-info {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  flex: 1;
}

.chatbot-header-info > div {
  min-width: 0;
}

.chatbot-avatar {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  flex-shrink: 0;
}
.chatbot-avatar img {
  width: 28px;
  height: 28px;
  object-fit: contain;
}

.chatbot-header-name {
  font-family: var(--heading-font, 'Inter', sans-serif);
  font-weight: 700;
  font-size: 15px;
  display: block;
  letter-spacing: -0.01em;
  overflow-wrap: anywhere;
}

.chatbot-header-status {
  font-size: 12px;
  opacity: 0.85;
  display: flex;
  align-items: center;
  gap: 5px;
  font-weight: 500;
}

.chatbot-status-dot {
  width: 7px;
  height: 7px;
  background: #34d399;
  border-radius: 50%;
  display: inline-block;
  animation: chatbotPulse 2s ease-in-out infinite;
}
@keyframes chatbotPulse {
  0%, 100% { opacity: 1; box-shadow: 0 0 0 0 rgba(52, 211, 153, 0.4); }
  50% { opacity: 0.8; box-shadow: 0 0 0 4px rgba(52, 211, 153, 0); }
}

.chatbot-close {
  background: rgba(255, 255, 255, 0.15);
  border: none;
  color: white;
  font-size: 20px;
  cursor: pointer;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.25s ease;
}
.chatbot-close:hover {
  background: rgba(255, 255, 255, 0.25);
  transform: rotate(90deg);
}

.chatbot-header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
  margin-right: 8px;
}

.chatbot-clear-btn {
  background: rgba(255, 255, 255, 0.15);
  border: none;
  color: white;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease, transform 0.2s ease;
}
.chatbot-clear-btn:hover {
  background: rgba(255, 255, 255, 0.28);
  transform: scale(1.05);
}

.chatbot-status-dot--thinking {
  background: #fbbf24;
  animation: chatbotThinking 1s ease-in-out infinite;
}
.chatbot-status-dot--offline {
  background: #94a3b8;
  animation: none;
  box-shadow: none;
}
@keyframes chatbotThinking {
  0%, 100% { opacity: 0.5; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1); }
}

/* ---------- Messages Area ---------- */
.chatbot-messages {
  flex: 1;
  min-height: 0;
  padding: 20px 16px;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #f8fafc;
  scrollbar-width: thin;
  scroll-behavior: smooth;
}
.chatbot-messages::-webkit-scrollbar { width: 5px; }
.chatbot-messages::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 10px; }
.chatbot-messages::-webkit-scrollbar-track { background: transparent; }

/* ---------- Welcome State ---------- */
.chatbot-welcome {
  text-align: center;
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  animation: chatFadeUp 0.4s ease-out;
}
.chatbot-welcome-icon {
  width: 52px;
  height: 52px;
  border-radius: 16px;
  background: linear-gradient(135deg, rgba(0, 185, 210, 0.1), rgba(0, 185, 210, 0.2));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
}
.chatbot-welcome h4 {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
  color: #0f172a;
  letter-spacing: -0.02em;
}
.chatbot-welcome p {
  margin: 0;
  font-size: 13.5px;
  color: #64748b;
  line-height: 1.5;
  max-width: 280px;
}
.chatbot-welcome-hint {
  font-size: 12px !important;
  color: #94a3b8 !important;
}

/* ---------- Message rows ---------- */
.chatbot-message-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px;
  width: 100%;
  animation: chatFadeUp 0.28s ease-out;
}
.chatbot-message-row.user {
  justify-content: flex-end;
}
.chatbot-message-row.bot {
  justify-content: flex-start;
}

.chatbot-msg-avatar {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  border-radius: 8px;
  background: #ffffff;
  border: 1px solid #e2e8f0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  align-self: flex-end;
  margin-bottom: 2px;
}
.chatbot-msg-avatar img {
  width: 20px;
  height: 20px;
  object-fit: contain;
}

.chatbot-msg-time {
  flex-basis: 100%;
  font-size: 10px;
  color: #94a3b8;
  padding: 0 4px 2px;
  line-height: 1;
}
.chatbot-message-row.user .chatbot-msg-time {
  text-align: right;
}
.chatbot-message-row.bot .chatbot-msg-time {
  text-align: left;
  padding-left: 36px;
}

.chatbot-retry-btn {
  flex-basis: 100%;
  margin: 2px 0 0 36px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 600;
  color: #00b9d2;
  background: transparent;
  border: 1px solid rgba(0, 185, 210, 0.35);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  font-family: var(--default-font, 'Inter', sans-serif);
}
.chatbot-retry-btn:hover {
  background: rgba(0, 185, 210, 0.08);
}

/* ---------- Message Bubbles ---------- */
.chatbot-message-row .message {
  max-width: min(85%, calc(100% - 40px));
}
.message {
  max-width: 85%;
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.6;
  position: relative;
  animation: chatFadeUp 0.3s ease-out;
  word-break: break-word;
}

@keyframes chatFadeUp {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.message.bot {
  background: #ffffff;
  color: #334155;
  align-self: flex-start;
  border: 1px solid #e2e8f0;
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
}

.message.user {
  background: linear-gradient(135deg, #00b9d2, #0891b2);
  color: white;
  align-self: flex-end;
  border-bottom-right-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 185, 210, 0.2);
}

/* Markdown rendering */
.message.bot .chatbot-md { word-break: break-word; }
.message.bot .chatbot-md a { color: #00b9d2; text-decoration: underline; }
.message.bot .chatbot-md p { margin: 0 0 0.5em; }
.message.bot .chatbot-md p:last-child { margin-bottom: 0; }
.chatbot-md-list {
  margin: 0.35em 0 0.5em;
  padding-left: 1.15em;
}
.chatbot-md-list li { margin: 0.2em 0; }
.message.message--error {
  border-color: #fecaca;
  background: #fef2f2;
}

.message code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.92em;
  background: rgba(15, 23, 42, 0.06);
  padding: 2px 6px;
  border-radius: 6px;
}

.message pre.chatbot-code {
  margin: 10px 0 0 0;
  padding: 12px;
  border-radius: 12px;
  background: #0f172a;
  color: #e2e8f0;
  overflow: auto;
  position: relative;
}
.message pre.chatbot-code code {
  background: transparent;
  padding: 0;
  color: inherit;
  display: block;
  white-space: pre;
}

.message pre.chatbot-code .code-copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  border: 1px solid rgba(226, 232, 240, 0.2);
  background: rgba(255, 255, 255, 0.08);
  color: #e2e8f0;
  padding: 4px 10px;
  border-radius: 8px;
  font-size: 11px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.2s ease;
}
.message pre.chatbot-code:hover .code-copy-btn { opacity: 1; }

/* ---------- Quick Action Chips ---------- */
.chatbot-quick-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 10px 16px 6px;
  background: #f8fafc;
  border-top: 1px solid #f1f5f9;
  flex-shrink: 0;
  max-height: 120px;
  overflow-y: auto;
}
.chatbot-quick-actions.hidden { display: none; }

.chatbot-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 7px 14px;
  background: #ffffff;
  border: 1px solid #e2e8f0;
  border-radius: 100px;
  font-size: 12.5px;
  font-weight: 500;
  color: #334155;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.25s ease;
  font-family: var(--default-font, 'Inter', sans-serif);
  line-height: 1.3;
}
.chatbot-chip:hover {
  background: #00b9d2;
  color: white;
  border-color: #00b9d2;
  transform: translateY(-1px);
  box-shadow: 0 3px 12px rgba(0, 185, 210, 0.2);
}
.chatbot-chip:active { transform: scale(0.97); }

/* ---------- Input Area ---------- */
.chatbot-input {
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom, 0px));
  background: #ffffff;
  border-top: 1px solid #e2e8f0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
}

.chatbot-input-status {
  flex: 1 0 100%;
  margin: 0;
  font-size: 12.5px;
  line-height: 1.4;
  color: #e11d48;
}

.chatbot-input-status[hidden] {
  display: none;
}

.chatbot-input input {
  flex: 1 1 auto;
  min-width: 0;
  width: auto;
  height: 46px;
  margin: 0;
  box-sizing: border-box;
  background: #f1f5f9;
  border: 1.5px solid #e2e8f0;
  color: #1e293b;
  padding: 0 16px;
  border-radius: 14px;
  outline: none;
  font-size: 14px;
  font-family: var(--default-font, 'Inter', sans-serif);
  transition: border-color 0.2s ease, background 0.2s ease, box-shadow 0.2s ease;
}
.chatbot-input input:focus,
.chatbot-input input:focus-visible {
  border-color: #22d3ee;
  background: #ffffff;
  box-shadow: 0 0 0 3px rgba(34, 211, 238, 0.18);
  outline: none;
}
.chatbot-input input::placeholder { color: #94a3b8; }
.chatbot-input input:disabled { opacity: 0.6; cursor: not-allowed; }

.chatbot-input button {
  background: #22d3ee;
  color: #042028;
  border: none;
  width: 46px;
  height: 46px;
  padding: 0;
  margin: 0;
  border-radius: 14px;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex: 0 0 46px;
  transition: background 0.2s ease, box-shadow 0.2s ease;
  box-shadow: 0 8px 20px rgba(34, 211, 238, 0.22);
}
.chatbot-input button:hover:not(:disabled) {
  background: #67e8f9;
  box-shadow: 0 10px 24px rgba(34, 211, 238, 0.3);
}
.chatbot-input button:focus-visible {
  outline: 2px solid #22d3ee;
  outline-offset: 2px;
}
.chatbot-input button:disabled { opacity: 0.5; cursor: not-allowed; }
.chatbot-input button.is-sending {
  opacity: 0.85;
}
.chatbot-input button svg,
.chatbot-input button i {
  width: 18px;
  height: 18px;
  display: block;
}

/* ---------- Typing Indicator ---------- */
.message.chatbot-typing {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  min-width: 0;
}
.chatbot-typing-logo {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  object-fit: cover;
  flex-shrink: 0;
  animation: chatbotTypingLogo 1.2s ease-in-out infinite;
}
.chatbot-typing-label {
  font-size: 13px;
  font-weight: 600;
  color: #0891b2;
  letter-spacing: 0.01em;
  white-space: nowrap;
}
@keyframes chatbotTypingLogo {
  0%, 100% { opacity: 0.55; }
  50% { opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .chatbot-typing-logo { animation: none; }
}

/* ---------- Timestamp ---------- */
.chatbot-timestamp {
  font-size: 10.5px;
  color: #94a3b8;
  text-align: center;
  padding: 4px 0;
}

/* ---------- Lead Capture Form ---------- */
.chatbot-lead-form {
  background: #ffffff;
  border: 1px solid #e2e8f0;
  border-radius: 16px;
  padding: 18px;
  align-self: flex-start;
  max-width: 90%;
  animation: chatFadeUp 0.3s ease-out;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
}
.chatbot-lead-form h5 {
  margin: 0 0 4px 0;
  font-size: 14px;
  font-weight: 700;
  color: #0f172a;
}
.chatbot-lead-form p {
  margin: 0 0 14px 0;
  font-size: 12.5px;
  color: #64748b;
  line-height: 1.4;
}
.chatbot-lead-form input {
  width: 100%;
  box-sizing: border-box;
  padding: 10px 14px;
  border: 1.5px solid #e2e8f0;
  border-radius: 10px;
  font-size: 13px;
  margin-bottom: 8px;
  font-family: var(--default-font, 'Inter', sans-serif);
  transition: border-color 0.2s ease;
  outline: none;
  background: #f8fafc;
  color: #1e293b;
}
.chatbot-lead-form input:focus {
  border-color: #00b9d2;
  background: #ffffff;
}
.chatbot-lead-form input::placeholder { color: #94a3b8; }

.chatbot-lead-submit {
  width: 100%;
  padding: 10px;
  background: linear-gradient(135deg, #00b9d2, #0891b2);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.25s ease;
  font-family: var(--default-font, 'Inter', sans-serif);
}
.chatbot-lead-submit:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(0, 185, 210, 0.3);
}

/* ---------- Intro Popup ---------- */
.chatbot-intro-popup {
  position: fixed;
  bottom: calc(90px + env(safe-area-inset-bottom, 0px));
  right: max(16px, env(safe-area-inset-right, 0px));
  z-index: 9998;
  max-width: 260px;
  background: white;
  padding: 14px 18px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(0, 0, 0, 0.06);
  display: none;
  animation: chatFadeUp 0.4s ease-out;
}
.chatbot-intro-popup.show { display: block; }

.chatbot-intro-popup .chatbot-intro-text {
  font-size: 14px;
  color: #334155;
  line-height: 1.5;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  font-weight: 500;
}

.chatbot-intro-popup .chatbot-intro-close {
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  font-size: 18px;
  padding: 0;
  flex-shrink: 0;
  transition: color 0.2s ease;
}
.chatbot-intro-popup .chatbot-intro-close:hover { color: #ef4444; }

.chatbot-intro-popup::after {
  content: '';
  position: absolute;
  bottom: -6px;
  right: 28px;
  width: 12px;
  height: 12px;
  background: white;
  border-right: 1px solid rgba(0, 0, 0, 0.06);
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  transform: rotate(45deg);
}

/* ---------- Mobile Responsive ---------- */
@media (max-width: 640px), (max-height: 520px) {
  .chatbot-window {
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-width: 100%;
    height: min(85dvh, calc(100dvh - 24px));
    max-height: min(85dvh, calc(100dvh - 24px));
    border-radius: 20px 20px 0 0;
    transform: translateY(100%);
  }
  .chatbot-window.active {
    transform: translateY(0);
  }

  .chatbot-btn {
    bottom: max(16px, env(safe-area-inset-bottom, 0px));
    right: max(16px, env(safe-area-inset-right, 0px));
    width: 56px;
    height: 56px;
  }
  .chatbot-btn i { font-size: 24px; }

  .chatbot-input input { font-size: 16px; }
  .chatbot-input { padding: 10px 12px max(10px, env(safe-area-inset-bottom, 0px)) 12px; }

  .chatbot-quick-actions { padding: 8px 12px 4px; }
  .chatbot-chip { min-height: 44px; font-size: 12px; padding: 6px 12px; }
  .chatbot-close,
  .chatbot-clear-btn,
  .chatbot-intro-close {
    width: 44px;
    height: 44px;
  }

  .chatbot-intro-popup { display: none !important; }
}

/* ---------- Reduced Motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .chatbot-btn, .chatbot-btn::before, .chatbot-window, .message, .chatbot-welcome, .chatbot-intro-popup {
    transition-duration: 0.001ms !important;
    animation: none !important;
  }
  .chatbot-status-dot, .chatbot-typing-logo {
    animation: none;
    opacity: 0.7;
  }
}
