/* ============================================================
   XPOTEC Chatbot Widget — chatbot.css
   ============================================================ */

/* ── CSS custom properties (overridden inline by JS) ───────── */
:root {
  --xcb-accent:       #0ea5e9;
  --xcb-accent-dark:  #0284c7;
  --xcb-accent-glow:  rgba(14, 165, 233, 0.35);
  --xcb-bg:           #0f172a;
  --xcb-surface:      #1e293b;
  --xcb-surface-2:    #273348;
  --xcb-border:       rgba(255,255,255,0.08);
  --xcb-text:         #f1f5f9;
  --xcb-text-muted:   #94a3b8;
  --xcb-user-bubble:  var(--xcb-accent);
  --xcb-bot-bubble:   #1e293b;
  --xcb-radius-lg:    18px;
  --xcb-radius-sm:    10px;
  --xcb-shadow:       0 24px 64px rgba(0,0,0,0.55);
  --xcb-font:         -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --xcb-z:            99999;
  --xcb-width:        380px;
  --xcb-height:       560px;
}

/* ── Reset ─────────────────────────────────────────────────── */
/* Only box-sizing — do NOT zero out padding/margin here as
   #xcb-root * has higher specificity (ID+universal = 100) than
   any class selector (10), which would silently kill all padding. */
#xcb-root *,
#xcb-root *::before,
#xcb-root *::after {
  box-sizing: border-box;
}

/* Neutralise browser UA margins/padding only on elements that need it */
#xcb-root h1, #xcb-root h2, #xcb-root h3,
#xcb-root h4, #xcb-root h5, #xcb-root h6,
#xcb-root p, #xcb-root ul, #xcb-root ol,
#xcb-root figure, #xcb-root blockquote {
  margin: 0;
  padding: 0;
}

/* ── Root wrapper (positioned fixed) ──────────────────────── */
#xcb-root {
  position: fixed;
  bottom: 28px;
  right: 28px;
  z-index: var(--xcb-z);
  font-family: var(--xcb-font);
  font-size: 14px;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 12px;
  /* The root box expands to hold xcb-window even when visually hidden,
     covering the full viewport on mobile with z-index 99999. Setting
     pointer-events: none here prevents it from swallowing taps meant
     for underlying elements (e.g. the mobile hamburger menu). Each
     interactive child restores pointer-events explicitly. */
  pointer-events: none;
}

/* ── FAB launch button ─────────────────────────────────────── */
#xcb-fab {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(145deg, var(--xcb-accent), var(--xcb-accent-dark));
  box-shadow: 0 4px 20px var(--xcb-accent-glow), 0 2px 8px rgba(0,0,0,0.4);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.22s cubic-bezier(0.34,1.56,0.64,1),
              box-shadow 0.22s ease,
              opacity 0.22s ease;
  color: #fff;
  position: relative;
  flex-shrink: 0;
  pointer-events: auto; /* re-enable: parent root has pointer-events: none */
  touch-action: manipulation;
}

#xcb-fab:hover {
  transform: scale(1.1);
  box-shadow: 0 6px 28px var(--xcb-accent-glow), 0 4px 12px rgba(0,0,0,0.5);
}

#xcb-fab:focus-visible {
  outline: 3px solid var(--xcb-accent);
  outline-offset: 4px;
}

#xcb-fab svg {
  width: 26px;
  height: 26px;
  flex-shrink: 0;
}

/* Icon spans fill the button and handle transitions */
#xcb-fab .xcb-icon-chat,
#xcb-fab .xcb-icon-close {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.3s cubic-bezier(0.34,1.56,0.64,1), opacity 0.2s;
}

#xcb-fab .xcb-icon-chat  { opacity: 1;  transform: rotate(0deg)   scale(1); }
#xcb-fab .xcb-icon-close { opacity: 0;  transform: rotate(-90deg) scale(0.6); }

#xcb-root.xcb-open #xcb-fab .xcb-icon-chat  { opacity: 0;  transform: rotate(90deg) scale(0.6); }
#xcb-root.xcb-open #xcb-fab .xcb-icon-close { opacity: 1;  transform: rotate(0deg)  scale(1); }

/* Notification badge */
#xcb-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #ef4444;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #fff;
  transition: transform 0.3s cubic-bezier(0.34,1.56,0.64,1), opacity 0.2s;
}

#xcb-badge[hidden] { display: none; }

/* ── Chat Window ───────────────────────────────────────────── */
#xcb-window {
  width: var(--xcb-width);
  max-width: calc(100vw - 24px);
  height: var(--xcb-height);
  max-height: calc(100vh - 120px);
  background: var(--xcb-bg);
  border-radius: var(--xcb-radius-lg);
  box-shadow: var(--xcb-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--xcb-border);

  /* Animation states */
  transform-origin: bottom right;
  transform: scale(0.85) translateY(20px);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.3s cubic-bezier(0.34,1.56,0.64,1),
              opacity 0.25s ease;
}

#xcb-root.xcb-open #xcb-window {
  transform: scale(1) translateY(0);
  opacity: 1;
  pointer-events: auto; /* re-enable for open state; children inherit */
}

/* ── Header ────────────────────────────────────────────────── */
#xcb-header {
  background: linear-gradient(135deg, var(--xcb-accent-dark) 0%, var(--xcb-accent) 100%);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
  position: relative;
}

#xcb-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.5px;
  flex-shrink: 0;
  border: 2px solid rgba(255,255,255,0.3);
}

#xcb-avatar svg {
  width: 22px;
  height: 22px;
}

.xcb-header-info {
  flex: 1;
  min-width: 0;
}

.xcb-header-name {
  font-weight: 700;
  font-size: 15px;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.xcb-header-status {
  font-size: 12px;
  color: rgba(255,255,255,0.8);
  display: flex;
  align-items: center;
  gap: 5px;
  margin-top: 1px;
}

.xcb-status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #4ade80;
  flex-shrink: 0;
  animation: xcbPulse 2.5s ease-in-out infinite;
}

@keyframes xcbPulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%       { opacity: 0.6; transform: scale(0.85); }
}

#xcb-header-close {
  background: rgba(255,255,255,0.15);
  border: none;
  border-radius: 8px;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #fff;
  transition: background 0.2s;
  flex-shrink: 0;
}

#xcb-header-close:hover { background: rgba(255,255,255,0.25); }
#xcb-header-close svg { width: 16px; height: 16px; }

/* Clear conversation button */
#xcb-clear {
  background: rgba(255,255,255,0.1);
  border: none;
  border-radius: 8px;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: rgba(255,255,255,0.7);
  transition: background 0.2s, color 0.2s;
  flex-shrink: 0;
}

#xcb-clear:hover {
  background: rgba(239,68,68,0.25);
  color: #fca5a5;
}

#xcb-clear svg { width: 15px; height: 15px; }

/* ── Messages area ─────────────────────────────────────────── */
#xcb-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  scroll-behavior: smooth;
}

/* Scrollbar */
#xcb-messages::-webkit-scrollbar { width: 4px; }
#xcb-messages::-webkit-scrollbar-track { background: transparent; }
#xcb-messages::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.12);
  border-radius: 4px;
}

/* Date-stamp dividers */
.xcb-date-stamp {
  text-align: center;
  font-size: 11px;
  color: var(--xcb-text-muted);
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.xcb-date-stamp::before,
.xcb-date-stamp::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--xcb-border);
}

/* Message rows */
.xcb-msg-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  animation: xcbSlideIn 0.25s ease;
}

@keyframes xcbSlideIn {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

.xcb-msg-row.xcb-user {
  flex-direction: row-reverse;
}

/* Tiny bot avatar on messages */
.xcb-msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--xcb-accent), var(--xcb-accent-dark));
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  flex-shrink: 0;
  letter-spacing: 0.5px;
  margin-top: 4px;
  box-shadow: 0 2px 8px var(--xcb-accent-glow);
}

.xcb-msg-avatar svg { width: 14px; height: 14px; }
.xcb-msg-row.xcb-user .xcb-msg-avatar { display: none; }

/* Bubble group */
.xcb-bubble-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 78%;
}

.xcb-bubble {
  padding: 13px 16px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.6;
  word-break: break-word;
  color: var(--xcb-text);
  white-space: pre-wrap;
}

/* Bot bubble */
.xcb-bot .xcb-bubble {
  background: var(--xcb-surface-2);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 18px 18px 18px 4px;
}

.xcb-bot .xcb-bubble-group { align-items: flex-start; }

/* User bubble */
.xcb-user .xcb-bubble {
  background: linear-gradient(135deg, var(--xcb-accent), var(--xcb-accent-dark));
  color: #fff;
  border-radius: 18px 18px 4px 18px;
  align-self: flex-end;
  box-shadow: 0 2px 12px var(--xcb-accent-glow);
}

.xcb-user .xcb-bubble-group { align-items: flex-end; }

/* Timestamp under bubble */
.xcb-msg-time {
  font-size: 11px;
  color: var(--xcb-text-muted);
  margin-top: 4px;
  padding: 0 4px;
}

.xcb-user .xcb-msg-time { text-align: right; }

/* ── Typing indicator ──────────────────────────────────────── */
.xcb-typing .xcb-bubble {
  background: var(--xcb-surface-2);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 18px 18px 18px 4px;
  padding: 14px 18px;
}

.xcb-typing-dots {
  display: flex;
  gap: 5px;
  align-items: center;
  height: 16px;
}

.xcb-typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--xcb-text-muted);
  animation: xcbBounce 1.2s ease-in-out infinite;
}

.xcb-typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.xcb-typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes xcbBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30%            { transform: translateY(-5px); opacity: 1; }
}

/* ── Quick replies ─────────────────────────────────────────── */
#xcb-quick-replies {
  padding: 4px 16px 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  flex-shrink: 0;
}

.xcb-quick-btn {
  padding: 7px 14px;
  border-radius: 20px;
  border: 1.5px solid var(--xcb-accent);
  background: rgba(14, 165, 233, 0.08);
  color: var(--xcb-accent);
  font-size: 12px;
  font-family: var(--xcb-font);
  cursor: pointer;
  transition: background 0.2s, color 0.2s, transform 0.15s;
  white-space: nowrap;
}

.xcb-quick-btn:hover {
  background: var(--xcb-accent);
  color: #fff;
  transform: translateY(-1px);
}

/* ── Input area ────────────────────────────────────────────── */
#xcb-input-area {
  background: var(--xcb-surface);
  border-top: 1px solid var(--xcb-border);
  padding: 12px 14px 16px;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  flex-shrink: 0;
}

#xcb-input {
  flex: 1;
  background: var(--xcb-surface-2);
  border: 1px solid var(--xcb-border);
  border-radius: 12px;
  padding: 11px 15px;
  color: var(--xcb-text);
  font-size: 14px;
  font-family: var(--xcb-font);
  resize: none;
  max-height: 120px;
  min-height: 44px;
  height: 44px;
  line-height: 1.5;
  transition: border-color 0.2s, box-shadow 0.2s;
  outline: none;
  overflow-y: hidden;
  scrollbar-width: none;
}

#xcb-input::placeholder { color: var(--xcb-text-muted); }
#xcb-input::-webkit-scrollbar { display: none; }
#xcb-input:focus {
  border-color: var(--xcb-accent);
  box-shadow: 0 0 0 3px var(--xcb-accent-glow);
}

#xcb-send {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: var(--xcb-accent);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
  transition: background 0.2s, transform 0.15s, opacity 0.2s;
}

#xcb-send:hover:not(:disabled) {
  background: var(--xcb-accent-dark);
  transform: scale(1.05);
}

#xcb-send:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

#xcb-send svg { width: 18px; height: 18px; }

/* ── Error banner ──────────────────────────────────────────── */
#xcb-error {
  background: rgba(239, 68, 68, 0.1);
  border-top: 1px solid rgba(239, 68, 68, 0.3);
  color: #fca5a5;
  font-size: 12px;
  padding: 8px 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

#xcb-error svg { width: 14px; height: 14px; flex-shrink: 0; }
#xcb-error[hidden] { display: none; }

/* ── Responsive ────────────────────────────────────────────── */
@media (max-width: 480px) {
  #xcb-root {
    bottom: 16px;
    right: 16px;
  }

  #xcb-window {
    --xcb-width: calc(100vw - 32px);
    /* Keep the window below the fixed header (76px) even when open.
       16px bottom offset + 60px FAB + 12px gap + window = viewport height.
       Subtract header (76px) + small safe gap (8px) → 100vh - 172px. */
    --xcb-height: calc(100vh - 172px);
    max-height: calc(100vh - 172px);
  }
}

/* ── Accessibility: reduce motion ──────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  #xcb-window,
  #xcb-fab,
  .xcb-msg-row,
  .xcb-typing-dots span,
  .xcb-status-dot {
    animation: none !important;
    transition: none !important;
  }
}
