/* =====================================================
   NEUROAPEX LABS — Chat Widget V2
   Version: V2.1 (ACE-Approved, 4-Adjustment Build)
   Date: 14 June 2026
   ===================================================== */

/* CSS Variables — Brand Colors */
:root {
  --chat-primary: #1a237e;
  --chat-primary-light: #3949ab;
  --chat-gold: #c9a227;
  --chat-gold-light: #f57f17;
  --chat-ava-bubble: #E3F2FD;
  --chat-ava-text: #1565C0;
  --chat-clara-bubble: #FFF8E1;
  --chat-clara-text: #F57F17;
  --chat-user-bubble: #ffffff;
  --chat-user-border: #e0e0e0;
  --chat-bg: #ffffff;
  --chat-header-bg: #1a237e;
  --chat-header-text: #ffffff;
  --chat-input-bg: #f9f9f9;
  --chat-meta: #9e9e9e;
  --chat-error-bg: #ffebee;
  --chat-error-border: #e53935;
  --chat-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  --chat-radius-top: 16px;
  --chat-transition: 300ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* =====================================================
   RESET & BASE
   ===================================================== */
#neuroapex-chat-widget,
#neuroapex-chat-widget * {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* =====================================================
   FLOATING BUTTON
   ===================================================== */
#chat-toggle-btn {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--chat-primary);
  border: none;
  cursor: pointer;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(26, 35, 126, 0.4);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  outline: none;
}

#chat-toggle-btn:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 20px rgba(26, 35, 126, 0.5);
}

#chat-toggle-btn:focus-visible {
  outline: 3px solid var(--chat-gold);
  outline-offset: 3px;
}

#chat-toggle-btn svg {
  width: 26px;
  height: 26px;
  fill: #ffffff;
  pointer-events: none;
}

/* Pulse animation — first visit only */
@keyframes chat-pulse {
  0%   { box-shadow: 0 0 0 0 rgba(26, 35, 126, 0.5); }
  70%  { box-shadow: 0 0 0 14px rgba(26, 35, 126, 0); }
  100% { box-shadow: 0 0 0 0 rgba(26, 35, 126, 0); }
}

#chat-toggle-btn.pulse-once {
  animation: chat-pulse 2s ease-out 1;
}

/* New message badge */
#chat-badge {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #e53935;
  border: 2px solid #ffffff;
  display: none;
}

#chat-badge.visible {
  display: block;
}

/* =====================================================
   CHAT WINDOW
   ===================================================== */
#chat-window {
  position: fixed;
  bottom: 88px;
  right: 20px;
  width: 380px;
  height: 520px;
  background: var(--chat-bg);
  border-radius: var(--chat-radius-top) var(--chat-radius-top) 0 0;
  box-shadow: var(--chat-shadow);
  z-index: 9998;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: translateY(calc(100% + 88px));
  opacity: 0;
  pointer-events: none;
  transition: transform var(--chat-transition), opacity var(--chat-transition);
  will-change: transform, opacity;
}

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

/* =====================================================
   HEADER
   ===================================================== */
#chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  background: var(--chat-header-bg);
  color: var(--chat-header-text);
  flex-shrink: 0;
  min-height: 60px;
}

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

#header-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid rgba(255, 255, 255, 0.3);
  flex-shrink: 0;
}

#header-avatar.clara-border {
  border-color: var(--chat-gold);
}

.header-text {
  display: flex;
  flex-direction: column;
}

#header-name {
  font-size: 14px;
  font-weight: 600;
  color: #ffffff;
  line-height: 1.2;
}

#header-role {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.75);
  line-height: 1.2;
}

.chat-header-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ffffff;
  flex-shrink: 0;
  transition: background 0.15s ease;
  outline: none;
}

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

.chat-header-btn:focus-visible {
  outline: 2px solid var(--chat-gold);
  outline-offset: 2px;
}

.chat-header-btn svg {
  width: 16px;
  height: 16px;
  fill: #ffffff;
  pointer-events: none;
}

/* =====================================================
   MESSAGES AREA
   ===================================================== */
#chat-messages {
  flex: 1;
  min-height: 0; /* Prevent flex overflow — critical for mobile */
  overflow-y: auto;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
}

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

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

#chat-messages::-webkit-scrollbar-thumb {
  background: #e0e0e0;
  border-radius: 2px;
}

/* Message wrapper */
.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin: 4px 0;
}

.msg-row.user {
  flex-direction: row-reverse;
}

/* Avatar */
.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  align-self: flex-end;
  margin-bottom: 18px;
}

.msg-avatar.clara {
  border: 2px solid var(--chat-gold-light);
}

/* Bubble container */
.msg-container {
  display: flex;
  flex-direction: column;
  max-width: 80%;
}

.msg-row.user .msg-container {
  align-items: flex-end;
}

/* Name label */
.msg-name {
  font-size: 11px;
  font-weight: 600;
  margin-bottom: 3px;
  padding-left: 4px;
}

.msg-name.ava  { color: var(--chat-ava-text); }
.msg-name.clara { color: var(--chat-clara-text); }

/* Bubble */
.msg-bubble {
  padding: 10px 14px;
  border-radius: 16px 16px 16px 4px;
  font-size: 14px;
  line-height: 1.55;
  word-break: break-word;
  white-space: pre-wrap;
}

.msg-bubble.ava {
  background: var(--chat-ava-bubble);
  color: #1a1a2e;
  border-radius: 16px 16px 16px 4px;
}

.msg-bubble.clara {
  background: var(--chat-clara-bubble);
  color: #1a1a2e;
  border-radius: 16px 16px 16px 4px;
}

.msg-bubble.user {
  background: var(--chat-user-bubble);
  color: #1a1a2e;
  border: 1px solid var(--chat-user-border);
  border-radius: 16px 16px 4px 16px;
}

/* Links inside bubbles */
.msg-bubble a {
  color: var(--chat-primary);
  text-decoration: underline;
  word-break: break-all;
}

/* Timestamp */
.msg-time {
  font-size: 11px;
  color: var(--chat-meta);
  margin-top: 4px;
  padding: 0 4px;
}

/* Date separator */
.date-separator {
  text-align: center;
  font-size: 11px;
  color: var(--chat-meta);
  margin: 8px 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.date-separator::before,
.date-separator::after {
  content: '';
  flex: 1;
  height: 1px;
  background: #e0e0e0;
}

/* System message (persona transition) */
.system-msg {
  text-align: center;
  font-size: 12px;
  font-style: italic;
  color: var(--chat-meta);
  margin: 8px 0;
  padding: 4px 12px;
}

/* =====================================================
   TYPING INDICATOR
   ===================================================== */
#chat-typing {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
}

#chat-typing.visible {
  display: flex;
}

.typing-dots {
  display: flex;
  align-items: center;
  gap: 4px;
  background: #f5f5f5;
  border-radius: 16px;
  padding: 8px 12px;
  min-width: 60px;
}

.typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #90caf9;
  animation: typing-bounce 1.2s ease-in-out infinite;
}

.typing-dot.clara-dot {
  background: #ffe082;
}

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

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30%            { transform: translateY(-6px); }
}

.typing-label {
  font-size: 11px;
  color: var(--chat-meta);
}

/* =====================================================
   SCROLL TO BOTTOM BUTTON
   ===================================================== */
#chat-scroll-btn {
  position: absolute;
  bottom: 130px;
  right: 16px;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--chat-primary);
  border: none;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  z-index: 10;
  transition: opacity 0.2s ease;
}

#chat-scroll-btn.visible {
  display: flex;
}

#chat-scroll-btn svg {
  width: 16px;
  height: 16px;
  fill: #ffffff;
}

/* =====================================================
   QUICK BUTTONS
   ===================================================== */
#chat-quick-buttons {
  padding: 8px 12px;
  border-top: 1px solid #f0f0f0;
  flex-shrink: 0;
  background: #fafafa;
}

#chat-quick-buttons.hidden {
  display: none;
}

.quick-buttons-header {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 6px;
}

.quick-buttons-dismiss {
  font-size: 11px;
  color: var(--chat-meta);
  text-decoration: underline;
  cursor: pointer;
  background: none;
  border: none;
  padding: 0;
}

.quick-buttons-dismiss:hover {
  color: #616161;
}

.quick-buttons-scroll {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
}

.quick-buttons-scroll::-webkit-scrollbar {
  height: 3px;
}

.quick-buttons-scroll::-webkit-scrollbar-thumb {
  background: #e0e0e0;
  border-radius: 2px;
}

.quick-btn {
  flex-shrink: 0;
  padding: 7px 14px;
  border: 1px solid #e0e0e0;
  border-radius: 20px;
  background: #f5f5f5;
  font-size: 13px;
  color: #333333;
  cursor: pointer;
  white-space: nowrap;
  scroll-snap-align: start;
  transition: background 0.15s ease, border-color 0.15s ease;
  outline: none;
}

.quick-btn:hover {
  background: #e8eaf6;
  border-color: var(--chat-primary);
  color: var(--chat-primary);
}

.quick-btn:focus-visible {
  outline: 2px solid var(--chat-primary);
  outline-offset: 2px;
}

/* =====================================================
   INPUT AREA
   ===================================================== */
#chat-input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid #f0f0f0;
  background: var(--chat-input-bg);
  flex-shrink: 0;
}

#chat-input {
  flex: 1;
  border: 1px solid #e0e0e0;
  border-radius: 20px;
  padding: 9px 14px;
  font-size: 14px;
  line-height: 1.4;
  resize: none;
  outline: none;
  background: #ffffff;
  color: #1a1a2e;
  max-height: 96px;
  overflow-y: auto;
  transition: border-color 0.15s ease;
}

#chat-input:focus {
  border-color: var(--chat-primary);
}

#chat-input::placeholder {
  color: #bdbdbd;
}

#chat-send-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--chat-primary);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s ease, opacity 0.15s ease;
  outline: none;
}

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

#chat-send-btn:not(:disabled):hover {
  background: var(--chat-primary-light);
}

#chat-send-btn:focus-visible {
  outline: 2px solid var(--chat-gold);
  outline-offset: 2px;
}

#chat-send-btn svg {
  width: 18px;
  height: 18px;
  fill: #ffffff;
  pointer-events: none;
}

/* =====================================================
   ERROR MESSAGE
   ===================================================== */
.chat-error-msg {
  background: var(--chat-error-bg);
  border-left: 3px solid var(--chat-error-border);
  padding: 10px 14px;
  margin: 4px 0;
  border-radius: 0 8px 8px 0;
  font-size: 13px;
  color: #1a1a2e;
}

.chat-error-msg .error-actions {
  margin-top: 8px;
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.chat-retry-btn {
  padding: 6px 14px;
  background: var(--chat-primary);
  color: #ffffff;
  border: none;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.chat-retry-btn:hover {
  background: var(--chat-primary-light);
}

.chat-support-link {
  font-size: 12px;
  color: var(--chat-ava-text);
  text-decoration: underline;
}

/* =====================================================
   PERSONA TRANSITION OVERLAY
   ===================================================== */
.persona-transition-msg {
  text-align: center;
  font-size: 12px;
  font-style: italic;
  color: var(--chat-meta);
  padding: 8px 16px;
  animation: fadeInOut 3s ease-in-out forwards;
}

@keyframes fadeInOut {
  0%   { opacity: 0; }
  20%  { opacity: 1; }
  80%  { opacity: 1; }
  100% { opacity: 0.6; }
}

/* =====================================================
   CONFIRMATION DIALOG
   ===================================================== */
#chat-confirm-dialog {
  position: absolute;
  top: 60px;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.95);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 100;
  padding: 24px;
  text-align: center;
}

#chat-confirm-dialog.visible {
  display: flex;
}

#chat-confirm-dialog p {
  font-size: 14px;
  color: #1a1a2e;
  line-height: 1.5;
}

.confirm-actions {
  display: flex;
  gap: 12px;
}

.confirm-yes-btn {
  padding: 10px 24px;
  background: #e53935;
  color: #ffffff;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  font-weight: 600;
}

.confirm-no-btn {
  padding: 10px 24px;
  background: #f5f5f5;
  color: #333333;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
}

/* =====================================================
   RESPONSIVE — TABLET
   ===================================================== */
@media (max-width: 1024px) and (min-width: 768px) {
  #chat-window {
    width: 340px;
    height: 480px;
  }
}

/* =====================================================
   RESPONSIVE — MOBILE
   ===================================================== */
@media (max-width: 767px) {
  #chat-toggle-btn {
    width: 48px;
    height: 48px;
    bottom: 16px;
    right: 16px;
  }

  #chat-toggle-btn svg {
    width: 22px;
    height: 22px;
  }

  #chat-window {
    position: fixed;
    bottom: 0;
    right: 0;
    left: 0;
    top: 0;
    width: 100%;
    /* Use dvh (dynamic viewport height) so the window shrinks when
       the mobile browser toolbar appears. Fall back to -webkit-fill-available
       for older Safari, then 100vh as last resort. */
    height: 100vh;
    height: -webkit-fill-available;
    height: 100dvh;
    border-radius: 0;
    transform: translateY(100%);
    /* Remove padding-bottom from window — handled per-section below */
    padding-bottom: 0;
  }

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

  /* Messages area must scroll independently within the flex column */
  #chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .msg-avatar {
    width: 28px;
    height: 28px;
    margin-bottom: 16px;
  }

  .quick-buttons-scroll {
    flex-wrap: nowrap;
  }

  #chat-input-area {
    /* Stick input to bottom of the flex column; safe-area-inset-bottom
       keeps it above the iPhone home indicator / Android nav bar */
    position: sticky;
    bottom: 0;
    padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
    /* Ensure it is always on top of message scroll area */
    z-index: 2;
  }
}

/* =====================================================
   SMALL MOBILE
   ===================================================== */
@media (max-width: 374px) {
  #chat-messages {
    padding: 12px 8px;
  }

  .msg-bubble {
    font-size: 13px;
    padding: 9px 12px;
  }

  #chat-input {
    font-size: 13px;
  }

  .chat-header-btn,
  #chat-send-btn {
    min-width: 44px;
    min-height: 44px;
  }
}

/* =====================================================
   ACCESSIBILITY
   ===================================================== */
@media (prefers-reduced-motion: reduce) {
  #chat-window,
  #chat-toggle-btn,
  .typing-dot {
    animation: none !important;
    transition: none !important;
  }

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