/* ── BB Chat Popup ─────────────────────────────────────────
   Bloney Bears · Blonsk chat widget
   Matches the site's dark design system tokens from marketplace.css
──────────────────────────────────────────────────────────── */

/* Launcher button (bottom-right) — safe-area aware to avoid scrollbar/notch clipping */
.bb-chat-launcher {
  position: fixed;
  bottom: max(1.75rem, env(safe-area-inset-bottom, 0px) + 1rem);
  right: max(1.75rem, env(safe-area-inset-right, 0px) + 1rem);
  z-index: 9998;
  width: 3.5rem;
  height: 3.5rem;
  border-radius: 50%;
  background: linear-gradient(135deg, #8b5cf6 0%, #ff5035 100%);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  box-shadow: 0 4px 24px rgba(139, 92, 246, 0.45);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  color: #fff;
}
.bb-chat-launcher:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 32px rgba(139, 92, 246, 0.6);
}
.bb-chat-launcher.hidden {
  display: none;
}

/* Popup container */
.bb-chat-popup {
  position: fixed;
  bottom: 1.75rem;
  right: 1.75rem;
  z-index: 9999;
  width: min(420px, calc(100vw - 2rem));
  /* dvh (dynamic viewport height) adjusts when mobile address bar shows/hides */
  height: min(580px, calc(100dvh - 5rem));
  background: #0f0f11;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 1.25rem;
  display: flex;
  flex-direction: column;
  box-shadow:
    0 0 0 1px rgba(139, 92, 246, 0.2),
    0 24px 64px rgba(0, 0, 0, 0.7);
  overflow: hidden;
  transform: translateY(20px) scale(0.96);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1),
              opacity 0.2s ease;
}
.bb-chat-popup.open {
  transform: translateY(0) scale(1);
  opacity: 1;
  pointer-events: all;
}

/* Header */
.bb-chat-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.875rem 1rem 0.875rem 1.1rem;
  background: rgba(139, 92, 246, 0.08);
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
  flex-shrink: 0;
}
.bb-chat-avatar {
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid rgba(139, 92, 246, 0.5);
  flex-shrink: 0;
}
.bb-chat-avatar-placeholder {
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 50%;
  background: linear-gradient(135deg, #8b5cf6 0%, #ff5035 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  flex-shrink: 0;
  border: 2px solid rgba(255, 255, 255, 0.1);
}
.bb-chat-identity {
  flex: 1;
  min-width: 0;
}
.bb-chat-name {
  font-family: 'Outfit', sans-serif;
  font-weight: 600;
  font-size: 0.9rem;
  color: #f1f1f3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bb-chat-status {
  font-size: 0.65rem;
  color: #9ca3af;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  margin-top: 0.1rem;
}
.bb-chat-status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #22c55e;
  animation: bb-pulse 2s ease-in-out infinite;
}
.bb-chat-status-dot.thinking {
  background: #f59e0b;
}
@keyframes bb-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}
.bb-chat-powered {
  font-size: 0.6rem;
  color: rgba(139, 92, 246, 0.7);
  font-family: 'JetBrains Mono', monospace;
  text-decoration: none;
  white-space: nowrap;
}
.bb-chat-close {
  background: none;
  border: none;
  cursor: pointer;
  color: #6b7280;
  font-size: 1.1rem;
  padding: 0.25rem;
  border-radius: 0.4rem;
  line-height: 1;
  transition: color 0.15s, background 0.15s;
}
.bb-chat-close:hover {
  color: #f1f1f3;
  background: rgba(255, 255, 255, 0.06);
}

/* Messages pane */
.bb-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  scroll-behavior: smooth;
}
.bb-chat-messages::-webkit-scrollbar {
  width: 4px;
}
.bb-chat-messages::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
}

/* Empty / welcome state */
.bb-chat-welcome {
  text-align: center;
  padding: 2rem 1rem;
  color: #6b7280;
  font-size: 0.82rem;
  line-height: 1.6;
}
.bb-chat-welcome .welcome-icon {
  font-size: 2.5rem;
  margin-bottom: 0.75rem;
}
.bb-chat-welcome .welcome-name {
  font-family: 'Outfit', sans-serif;
  font-weight: 700;
  font-size: 1.05rem;
  color: #f1f1f3;
  margin-bottom: 0.4rem;
}
.bb-chat-welcome .welcome-hint {
  color: #6b7280;
  font-size: 0.78rem;
}

/* Message bubbles */
.bb-msg {
  display: flex;
  gap: 0.5rem;
  max-width: 88%;
}
.bb-msg.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}
.bb-msg.agent {
  align-self: flex-start;
}
.bb-msg-bubble {
  padding: 0.6rem 0.85rem;
  border-radius: 1rem;
  font-size: 0.83rem;
  line-height: 1.6;
  word-break: break-word;
  font-family: 'Inter', sans-serif;
}
.bb-msg.user .bb-msg-bubble {
  background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%);
  color: #fff;
  border-bottom-right-radius: 0.25rem;
}
.bb-msg.agent .bb-msg-bubble {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.08);
  color: #e5e7eb;
  border-bottom-left-radius: 0.25rem;
}
.bb-msg.agent .bb-msg-bubble strong {
  color: #f1f1f3;
}
.bb-msg.agent .bb-msg-bubble em {
  color: #a78bfa;
}
.bb-msg-avatar {
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 50%;
  flex-shrink: 0;
  align-self: flex-end;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

/* Typing indicator */
.bb-typing {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.5rem 0.85rem;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 1rem;
  border-bottom-left-radius: 0.25rem;
  width: fit-content;
}
.bb-typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #8b5cf6;
  animation: bb-bounce 1.2s ease-in-out infinite;
}
.bb-typing-dot:nth-child(2) { animation-delay: 0.2s; }
.bb-typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bb-bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-5px); }
}

/* Error bubble */
.bb-msg-error {
  color: #f87171;
  font-size: 0.75rem;
  text-align: center;
  padding: 0.4rem;
}

/* Input row */
.bb-chat-input-row {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.75rem 0.875rem;
  border-top: 1px solid rgba(255, 255, 255, 0.07);
  background: rgba(255, 255, 255, 0.02);
  flex-shrink: 0;
}
.bb-chat-input {
  flex: 1;
  resize: none;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 0.75rem;
  color: #f1f1f3;
  font-family: 'Inter', sans-serif;
  font-size: 0.82rem;
  line-height: 1.5;
  padding: 0.55rem 0.75rem;
  outline: none;
  max-height: 6rem;
  min-height: 2.4rem;
  transition: border-color 0.15s;
}
.bb-chat-input::placeholder {
  color: #6b7280;
}
.bb-chat-input:focus {
  border-color: rgba(139, 92, 246, 0.5);
}
.bb-chat-send {
  flex-shrink: 0;
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 0.65rem;
  background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  color: #fff;
  transition: opacity 0.15s, transform 0.15s;
}
.bb-chat-send:hover:not(:disabled) {
  opacity: 0.88;
  transform: scale(1.05);
}
.bb-chat-send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Quick prompts */
.bb-chat-prompts {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  padding: 0 1rem 0.6rem;
}
.bb-prompt-chip {
  font-size: 0.7rem;
  padding: 0.3rem 0.6rem;
  border-radius: 999px;
  border: 1px solid rgba(139, 92, 246, 0.35);
  background: rgba(139, 92, 246, 0.08);
  color: #a78bfa;
  cursor: pointer;
  font-family: 'Inter', sans-serif;
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
}
.bb-prompt-chip:hover {
  background: rgba(139, 92, 246, 0.18);
  border-color: rgba(139, 92, 246, 0.6);
}

/* ═══════════════════════════════════════════════════════════
   VOICE BUBBLE  — WhatsApp-style audio messages
   Bear replies arrive as voice bubbles. Tap ▶ to play,
   tap the transcript icon to reveal the text.
   Voice parameters are derived from the bear's DNA codons.
═══════════════════════════════════════════════════════════ */

/* Wrapper column: bubble row + transcript panel */
.bb-voice-col {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 0;
}

/* The bubble itself */
.bb-voice-bubble {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.09);
  border-radius: 1rem 1rem 1rem 0.2rem;
  padding: 0.5rem 0.65rem;
  min-width: min-content;
  max-width: 272px;
  transition: border-color 0.15s;
}
.bb-voice-bubble.playing {
  border-color: rgba(139, 92, 246, 0.35);
}
.bb-voice-bubble.loading {
  opacity: 0.7;
}

/* ── Play / Pause button ───────────────────────────────── */
.bb-vb-play {
  flex-shrink: 0;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s, opacity 0.12s;
  padding: 0;
}
.bb-vb-play:hover { transform: scale(1.1); }
.bb-vb-play:active { transform: scale(0.93); }

/* ── Waveform ──────────────────────────────────────────── */
.bb-vb-waveform {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 2px;
  height: 26px;
  cursor: pointer;
}

.bb-vb-bar {
  display: inline-block;
  flex-shrink: 0;
  width: 3px;
  border-radius: 1.5px;
  height: var(--h, 40%);
  background: rgba(255, 255, 255, 0.22);
  transition: background 0.12s;
}
.bb-vb-bar.played {
  background: rgba(139, 92, 246, 0.75);
}

/* Animate bars near playhead while playing */
.bb-voice-bubble.playing .bb-vb-bar {
  animation: bb-bar-wave 0.85s ease-in-out infinite alternate;
}
.bb-voice-bubble.playing .bb-vb-bar:nth-child(3n)   { animation-delay: 0s;      }
.bb-voice-bubble.playing .bb-vb-bar:nth-child(3n+1) { animation-delay: 0.14s;   }
.bb-voice-bubble.playing .bb-vb-bar:nth-child(3n+2) { animation-delay: 0.28s;   }
.bb-voice-bubble.playing .bb-vb-bar.played {
  animation: none;
  background: rgba(139, 92, 246, 0.85);
}

@keyframes bb-bar-wave {
  0%   { transform: scaleY(0.6); }
  100% { transform: scaleY(1.2); }
}

/* ── Timer ────────────────────────────────────────────── */
.bb-vb-timer {
  font-size: 0.63rem;
  color: rgba(255, 255, 255, 0.38);
  font-variant-numeric: tabular-nums;
  font-family: 'JetBrains Mono', monospace;
  min-width: 2.4rem;
  text-align: right;
  flex-shrink: 0;
}

/* ── Transcript toggle button ─────────────────────────── */
.bb-vb-tx-btn {
  flex-shrink: 0;
  width: 1.45rem;
  height: 1.45rem;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.04);
  color: rgba(255, 255, 255, 0.35);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.bb-vb-tx-btn:hover,
.bb-vb-tx-btn.active {
  background: rgba(139, 92, 246, 0.18);
  border-color: rgba(139, 92, 246, 0.45);
  color: #a78bfa;
}

/* ── Transcript panel ─────────────────────────────────── */
.bb-vb-transcript {
  display: none;
  font-size: 0.78rem;
  color: #c4b5fd;
  line-height: 1.6;
  padding: 0.35rem 0.55rem;
  max-width: 272px;
  border-left: 2px solid rgba(139, 92, 246, 0.3);
  margin-left: 0.2rem;
}
.bb-vb-transcript.open {
  display: block;
  animation: bb-tx-in 0.18s ease;
}
.bb-vb-transcript strong { color: #f1f1f3; }
.bb-vb-transcript em     { color: #a78bfa; }

@keyframes bb-tx-in {
  from { opacity: 0; transform: translateY(-3px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Loading spinner ──────────────────────────────────── */
.bb-vb-spin {
  animation: bb-spin 0.9s linear infinite;
  display: block;
}
@keyframes bb-spin {
  to { transform: rotate(360deg); }
}

/* ── Voice message row alignment ─────────────────────── */
.bb-msg--voice {
  align-items: flex-start;
}
.bb-msg--voice .bb-msg-avatar {
  margin-top: 0.3rem;
}

/* Mobile — catch tablets in portrait */
@media (max-width: 768px) {
  .bb-chat-popup {
    bottom: 0;
    right: 0;
    left: 0;
    width: 100%;
    /* dvh so address bar doesn't clip the chat header */
    height: min(92dvh, calc(100dvh - env(safe-area-inset-bottom, 0px)));
    border-radius: 1.25rem 1.25rem 0 0;
  }
  .bb-chat-launcher {
    bottom: max(1.25rem, env(safe-area-inset-bottom, 0px) + 0.75rem);
    right: 1.25rem;
  }
}
