/* SyncBrainAI Site Widget — floating brain + chat panel
 * Spec: workspace/.../specs/syncbrainai-site-widget/SPEC-site-widget.md §9
 * Scope: anonymous visitors on syncbrainai.com only (injected conditionally
 * in templates/base.html). Logged-in users have their own /chat page.
 * 2026-05-07 mac-claude — Phase 2 frontend.
 */

:root {
  --sb-widget-theme: #1a3a5c;
  --sb-widget-accent: #2a5a8c;
  --sb-widget-bg: #ffffff;
  --sb-widget-fg: #1a1a1a;
  --sb-widget-muted: #6b7280;
  --sb-widget-shadow: 0 6px 24px rgba(0, 0, 0, 0.18);
  --sb-widget-radius: 14px;
}

/* Container — fixed, no pointer-events so it doesn't block page below.
 * Children re-enable pointer-events where interactive. */
.sb-widget-root {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 2147483600;  /* below browser UI but above all page content */
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--sb-widget-fg);
}

/* ── Floating brain icon ─────────────────────────────────────────── */

.sb-widget-brain {
  position: absolute;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: transparent;
  border: none;
  padding: 0;
  box-shadow: var(--sb-widget-shadow);
  cursor: pointer;
  pointer-events: auto;
  overflow: hidden;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  user-select: none;
  -webkit-user-select: none;
  will-change: transform, left, top;
}

.sb-widget-brain:hover {
  transform: scale(1.08);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25);
}

.sb-widget-brain:active {
  transform: scale(0.96);
}

.sb-widget-brain img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
  pointer-events: none;
  display: block;
  transform: scale(1.6);
}

/* Pulsing ring when there's an unseen nudge */
.sb-widget-brain.has-nudge::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 2px solid var(--sb-widget-theme);
  opacity: 0.7;
  animation: sb-widget-pulse 1.6s ease-out infinite;
  pointer-events: none;
}

@keyframes sb-widget-pulse {
  0%   { transform: scale(1);   opacity: 0.7; }
  70%  { transform: scale(1.4); opacity: 0;   }
  100% { transform: scale(1.4); opacity: 0;   }
}

/* ── Nudge bubble (speech bubble pointing at brain) ──────────────── */

.sb-widget-nudge {
  position: absolute;
  background: var(--sb-widget-bg);
  color: var(--sb-widget-fg);
  padding: 12px 16px;
  border-radius: 14px;
  box-shadow: var(--sb-widget-shadow);
  max-width: 240px;
  pointer-events: auto;
  cursor: pointer;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.3s ease, transform 0.3s ease;
  font-weight: 500;
  line-height: 1.4;
}

.sb-widget-nudge.visible {
  opacity: 1;
  transform: translateY(0);
}

.sb-widget-nudge::after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  border: 8px solid transparent;
}

/* Default: bubble above brain, tail points down */
.sb-widget-nudge.bubble-above::after {
  bottom: -14px;
  right: 22px;
  border-top-color: var(--sb-widget-bg);
}

/* Bubble below brain, tail points up */
.sb-widget-nudge.bubble-below::after {
  top: -14px;
  right: 22px;
  border-bottom-color: var(--sb-widget-bg);
}

.sb-widget-nudge-close {
  position: absolute;
  top: 4px;
  right: 6px;
  background: none;
  border: none;
  color: var(--sb-widget-muted);
  font-size: 16px;
  cursor: pointer;
  padding: 2px 6px;
  line-height: 1;
}

/* ── Chat panel ──────────────────────────────────────────────────── */

.sb-widget-panel {
  position: absolute;
  width: 360px;
  height: 540px;
  background: var(--sb-widget-bg);
  border-radius: var(--sb-widget-radius);
  box-shadow: var(--sb-widget-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  pointer-events: auto;
  opacity: 0;
  transform: scale(0.85);
  transform-origin: bottom right;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.sb-widget-panel.visible {
  opacity: 1;
  transform: scale(1);
}

.sb-widget-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: var(--sb-widget-theme);
  color: white;
  border-top-left-radius: var(--sb-widget-radius);
  border-top-right-radius: var(--sb-widget-radius);
}

.sb-widget-header img {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: white;
  padding: 2px;
}

.sb-widget-header-name {
  flex: 1;
  font-weight: 600;
  font-size: 15px;
}

.sb-widget-close {
  background: none;
  border: none;
  color: white;
  font-size: 22px;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
  opacity: 0.85;
}

.sb-widget-close:hover {
  opacity: 1;
}

.sb-widget-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #f7f8fa;
}

.sb-widget-msg {
  max-width: 80%;
  padding: 9px 13px;
  border-radius: 14px;
  line-height: 1.45;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.sb-widget-msg.assistant {
  background: var(--sb-widget-bg);
  color: var(--sb-widget-fg);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

.sb-widget-msg.user {
  background: var(--sb-widget-theme);
  color: white;
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}

.sb-widget-msg.typing {
  color: var(--sb-widget-muted);
  font-style: italic;
}

.sb-widget-input-row {
  display: flex;
  gap: 8px;
  padding: 10px;
  border-top: 1px solid #e5e7eb;
  background: var(--sb-widget-bg);
}

.sb-widget-input {
  flex: 1;
  border: 1px solid #d1d5db;
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s ease;
  resize: none;
  min-height: 38px;
  max-height: 120px;
}

.sb-widget-input:focus {
  border-color: var(--sb-widget-theme);
}

.sb-widget-send {
  background: var(--sb-widget-theme);
  color: white;
  border: none;
  border-radius: 10px;
  padding: 0 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease;
}

.sb-widget-send:hover { background: var(--sb-widget-accent); }
.sb-widget-send:disabled { opacity: 0.5; cursor: not-allowed; }

.sb-widget-footer {
  text-align: center;
  font-size: 11px;
  color: var(--sb-widget-muted);
  padding: 6px;
  background: #f7f8fa;
  border-top: 1px solid #e5e7eb;
}

/* ── Projector — agent-pushed rich content (image/markdown/code/html) ──
 * Industry-standard pattern: slides up from inside the chat panel as a
 * dismissable card, NOT a fullscreen takeover. User can keep chatting while
 * looking at it; X to dismiss. */
.sb-widget-projector {
  position: absolute;
  inset: 0;
  background: var(--sb-widget-bg);
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.25s ease;
  z-index: 2;
  pointer-events: none;
}
.sb-widget-projector.visible {
  transform: translateY(0);
  pointer-events: auto;
}
.sb-widget-projector-header {
  padding: 10px 12px;
  background: var(--sb-widget-theme);
  color: white;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  font-weight: 600;
}
.sb-widget-projector-title { flex: 1; }
.sb-widget-projector-close {
  background: rgba(255,255,255,0.18);
  border: 0;
  color: white;
  padding: 3px 9px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
}
.sb-widget-projector-close:hover { background: rgba(255,255,255,0.28); }
.sb-widget-projector-body {
  flex: 1;
  overflow: auto;
  padding: 14px;
  background: #f7f8fa;
}
.sb-widget-projector-body img,
.sb-widget-projector-body video {
  max-width: 100%;
  border-radius: 8px;
  display: block;
  margin: 0 auto;
}
.sb-widget-projector-body pre {
  background: #1f2937;
  color: #f9fafb;
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  line-height: 1.5;
}
.sb-widget-projector-body iframe {
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: 8px;
}
.sb-widget-projector-body .pj-md p { margin: 0 0 8px; line-height: 1.5; }
.sb-widget-projector-body .pj-md h1,
.sb-widget-projector-body .pj-md h2,
.sb-widget-projector-body .pj-md h3 { margin: 12px 0 6px; }
.sb-widget-projector-body .pj-caption { font-size: 12px; color: var(--sb-widget-muted); text-align: center; margin-top: 8px; font-style: italic; }

/* Inline projector card (rendered as a rich-content message bubble in the
 * chat list — for small content that doesn't warrant fullscreen takeover) */
.sb-widget-msg.projector-card {
  background: var(--sb-widget-bg) !important;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  padding: 0;
  overflow: hidden;
  align-self: stretch;
  max-width: 100%;
}
.sb-widget-msg.projector-card img { width: 100%; display: block; }
.sb-widget-msg.projector-card .pj-card-cap { padding: 8px 12px; font-size: 12px; color: var(--sb-widget-muted); border-top: 1px solid #f3f4f6; }
.sb-widget-msg.projector-card pre { margin: 0; padding: 10px 12px; background: #1f2937; color: #f9fafb; font-family: ui-monospace, monospace; font-size: 11px; overflow-x: auto; }

/* ── Mobile: chat panel goes full-screen ─────────────────────────── */

@media (max-width: 640px) {
  .sb-widget-panel.visible {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    border-radius: 0;
    transform: none;
  }
  .sb-widget-header {
    border-radius: 0;
  }
  .sb-widget-nudge {
    max-width: 200px;
    font-size: 13px;
  }
}

/* ── Reduce motion: disable drift ────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
  .sb-widget-brain {
    transition: none;
  }
  .sb-widget-brain.has-nudge::after {
    animation: none;
    opacity: 0.5;
  }
  .sb-widget-panel,
  .sb-widget-nudge {
    transition: opacity 0.1s ease;
    transform: none !important;
  }
}
