// Chrome — Colleen × Sam
// Built for clarity. A large friendly state banner makes who's-talking
// unmistakable. Header has her name + Sam. Calm, warm, rounded.

// ── Header ──────────────────────────────────────────────────────────────
function ColleenHeader({ state }) {
  const meta = CL_STATE_META[state];
  const dim = 'rgba(245,238,226,0.6)';
  const lbl = '#f6efe2';

  return (
    <div style={{
      position: 'absolute', top: 56, left: 0, right: 0, zIndex: 30,
      padding: '10px 24px',
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      fontFamily: '"Nunito", "Inter Tight", system-ui, sans-serif',
    }}>
      {/* Left — small status dot + soft time */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
        <span style={{
          width: 8, height: 8, borderRadius: 99, background: meta.dot,
          boxShadow: state === 'offline' ? 'none' : `0 0 10px ${meta.dot}`,
          animation: (state === 'connecting' || state === 'idle') ? 'pulseDot 2s ease-in-out infinite' : 'none',
        }} />
        <span style={{
          color: dim, fontSize: 11, fontWeight: 600, letterSpacing: '0.02em',
        }}>Today</span>
      </div>

      {/* Right — Colleen + Sam */}
      <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-end', gap: 1 }}>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 7 }}>
          <span style={{
            fontFamily: '"Caveat", "Nunito", cursive',
            fontSize: 22, fontWeight: 700, color: COAST.goldHi, lineHeight: 1,
            textShadow: `0 1px 8px ${COAST.goldLo}66`,
          }}>Colleen</span>
        </div>
        <span style={{ color: dim, fontSize: 9.5, fontWeight: 600, letterSpacing: '0.16em', textTransform: 'uppercase' }}>
          with Sam
        </span>
      </div>
    </div>
  );
}

// ── BIG STATE BANNER — the clarity centerpiece ─────────────────────────
// Sits just under the header. Large, warm, plain language. Color-coded:
// teal when Colleen talks, gold when Sam talks.
function StateBanner({ state }) {
  const meta = CL_STATE_META[state];
  const who = meta.who;
  const accent = who === 'sam' ? COAST.goldHi : who === 'you' ? COAST.aqua : COAST.cream;

  // A small leading icon clarifies direction: ear when listening, speech when Sam talks
  const icon = who === 'you' ? (
    // ear / listening
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none">
      <path d="M7 9a5 5 0 0110 0c0 2.5-2 3.5-2 6a3 3 0 01-3 3" stroke={accent} strokeWidth="2" strokeLinecap="round"/>
      <circle cx="12" cy="9" r="1.6" fill={accent}/>
    </svg>
  ) : who === 'sam' ? (
    // speech waves
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none">
      <path d="M4 12h2M9 7v10M14 4v16M19 9v6" stroke={accent} strokeWidth="2" strokeLinecap="round"/>
    </svg>
  ) : (
    // soft sun
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none">
      <circle cx="12" cy="12" r="4" stroke={accent} strokeWidth="2"/>
      <path d="M12 3v2M12 19v2M3 12h2M19 12h2M5.6 5.6l1.4 1.4M17 17l1.4 1.4M18.4 5.6L17 7M7 17l-1.4 1.4" stroke={accent} strokeWidth="2" strokeLinecap="round"/>
    </svg>
  );

  return (
    <div style={{
      position: 'absolute', top: 118, left: 0, right: 0, zIndex: 28,
      display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 8,
      pointerEvents: 'none',
    }}>
      {/* Pill with icon + state word — light white-sand box, dark text */}
      <div style={{
        display: 'inline-flex', alignItems: 'center', gap: 8,
        padding: '7px 16px 7px 12px',
        borderRadius: 999,
        background: `${COAST.cream}f5`,
        border: `2px solid ${accent}`,
        backdropFilter: 'blur(16px)', WebkitBackdropFilter: 'blur(16px)',
        boxShadow: `0 4px 18px ${accent}44`,
      }}>
        {icon}
        <span style={{
          fontFamily: '"Nunito", system-ui, sans-serif',
          fontSize: 14, fontWeight: 800, color: '#123a39',
          letterSpacing: '0.01em', whiteSpace: 'nowrap',
        }}>{meta.label}</span>
      </div>
      {/* Sub line — friendly guidance, larger */}
      <span style={{
        fontFamily: '"Nunito", system-ui, sans-serif',
        fontSize: 16.5, fontWeight: 700, color: '#fbf6ec',
        textShadow: '0 1px 8px rgba(0,0,0,0.55)',
        maxWidth: 300, textAlign: 'center', lineHeight: 1.4,
      }}>{meta.sub}</span>
    </div>
  );
}

// ── Captions ────────────────────────────────────────────────────────────
// Sam's bubble is a fixed-height, scrollable "backup" panel — for sensitive
// info (a code/password he can't say aloud) or to double-check she heard him.
// It never grows tall enough to cover the orb; a chunky scrollbar + "scroll"
// hint let her read a long reply if she wants.
if (typeof document !== 'undefined' && !document.getElementById('sam-scroll-style')) {
  const s = document.createElement('style');
  s.id = 'sam-scroll-style';
  s.textContent = `
    .sam-scroll { scrollbar-width: thin; scrollbar-color: #12B8CE rgba(18,184,206,0.18); }
    .sam-scroll::-webkit-scrollbar { width: 12px; }
    .sam-scroll::-webkit-scrollbar-track { background: rgba(18,184,206,0.14); border-radius: 8px; margin: 4px 0; }
    .sam-scroll::-webkit-scrollbar-thumb { background: #12B8CE; border-radius: 8px; border: 3px solid #F7F2E6; }
    .sam-scroll::-webkit-scrollbar-thumb:hover { background: #0A7E98; }
  `;
  document.head.appendChild(s);
}

function TranscriptPanel({ lines, onReplay }) {
  const ref = React.useRef(null);
  const [overflow, setOverflow] = React.useState(false);
  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;
    setOverflow(el.scrollHeight > el.clientHeight + 2);
    el.scrollTop = 0;
  }, [lines]);

  return (
    <div style={{
      pointerEvents: 'auto',
      padding: '12px 14px 12px 16px',
      borderRadius: '22px 22px 22px 6px',
      background: `${COAST.cream}f7`,
      backdropFilter: 'blur(18px)', WebkitBackdropFilter: 'blur(18px)',
      border: `2.5px solid ${COAST.gold}`,
      boxShadow: `0 8px 28px ${COAST.gold}44`,
      position: 'relative',
      opacity: 0, animation: 'captionIn 0.35s ease forwards',
    }}>
      <div style={{
        fontSize: 13, fontWeight: 800, letterSpacing: '0.08em', textTransform: 'uppercase',
        color: COAST.goldLo, marginBottom: 5,
      }}>Sam</div>
      <div ref={ref} className="sam-scroll" style={{
        height: 112, overflowY: 'auto', paddingRight: 10,
        fontFamily: '"Nunito", system-ui, sans-serif',
        fontSize: 18.5, lineHeight: 1.45, color: '#123a39', fontWeight: 700,
      }}>
        {lines.map((line, i) => (
          <div key={i} style={{
            marginBottom: i === lines.length - 1 ? 0 : 8,
            color: line.who === 'sam' ? '#123a39' : COAST.tealDeep,
            fontWeight: line.who === 'sam' ? 800 : 700,
          }}>
            {line.who === 'sam' ? line.text : `You: ${line.text}`}
          </div>
        ))}
      </div>
      {onReplay && (
        <button onClick={onReplay} style={{
          marginTop: 9, padding: '8px 13px',
          display: 'inline-flex', alignItems: 'center', gap: 7,
          borderRadius: 999, cursor: 'pointer',
          background: `${COAST.cream}e8`,
          border: `2px solid ${COAST.gold}`,
          boxShadow: `0 4px 14px ${COAST.gold}2f`,
          fontFamily: '"Nunito", system-ui, sans-serif',
          fontSize: 14, fontWeight: 800, color: '#123a39',
        }}>
          <svg width="15" height="15" viewBox="0 0 24 24" fill="none">
            <path d="M20 11a8 8 0 10-2.3 5.6M20 5v6h-6" stroke={COAST.goldLo} strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"/>
          </svg>
          Say that again
        </button>
      )}
    </div>
  );
}

function ColleenCaptions({ state, lines = [], onReplay }) {
  if (!lines.length) return null;
  const hasSam = lines.some(l => l.who === 'sam');
  return (
    <div style={{
      position: 'absolute', left: 18, right: 18, bottom: 196, zIndex: 25,
      display: 'flex', flexDirection: 'column', gap: 9, pointerEvents: 'none',
    }}>
      <TranscriptPanel lines={lines} onReplay={hasSam && state === 'speaking' ? onReplay : null} />
    </div>
  );
}

// ── Mic / turn status — the clearest turn-taking signal ────────────────
// Shows ONLY during a live conversation. Tells her plainly whose turn it is
// and whether her microphone is open, so no one talks over anyone.
function MicStatus({ state, muted }) {
  const live = state === 'listening' || state === 'thinking' || state === 'speaking';
  if (!live) return null;

  const micOn = state === 'listening' && !muted;
  const accent = micOn ? COAST.tealHi : COAST.gold;
  const label = micOn ? 'Your turn' : 'Sam\u2019s turn';
  const sub = micOn ? 'Your mic is on \u2014 go ahead' : 'Your mic is off \u2014 just listen';

  return (
    <div style={{
      position: 'absolute', top: 430, left: 0, right: 0, zIndex: 27,
      display: 'flex', justifyContent: 'center', pointerEvents: 'none',
    }}>
      <div style={{
        display: 'inline-flex', alignItems: 'center', gap: 11,
        padding: '10px 18px',
        borderRadius: 999,
        background: `${COAST.cream}f2`,
        border: `2.5px solid ${accent}`,
        boxShadow: `0 5px 22px ${accent}55`,
        backdropFilter: 'blur(14px)', WebkitBackdropFilter: 'blur(14px)',
      }}>
        <span style={{
          position: 'relative', width: 26, height: 26,
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          borderRadius: '50%', background: micOn ? `${COAST.tealHi}33` : `${COAST.khaki}33`,
        }}>
          <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
            <rect x="6" y="2" width="4" height="8" rx="2" stroke={accent} strokeWidth="1.6"/>
            <path d="M3.5 8a4.5 4.5 0 009 0M8 12.5v2" stroke={accent} strokeWidth="1.6" strokeLinecap="round"/>
            {!micOn && <path d="M3 3l10 10" stroke={accent} strokeWidth="1.8" strokeLinecap="round"/>}
          </svg>
          {micOn && (
            <span style={{
              position: 'absolute', inset: -4, borderRadius: '50%',
              border: `2px solid ${COAST.tealHi}`,
              animation: 'micPing 1.6s ease-out infinite', opacity: 0,
            }} />
          )}
        </span>
        <div style={{ display: 'flex', flexDirection: 'column', lineHeight: 1.15 }}>
          <span style={{
            fontFamily: '"Nunito", system-ui, sans-serif',
            fontSize: 17, fontWeight: 800, color: '#123a39',
          }}>{label}</span>
          <span style={{
            fontFamily: '"Nunito", system-ui, sans-serif',
            fontSize: 12.5, fontWeight: 600, color: '#3a6b66',
          }}>{sub}</span>
        </div>
      </div>
    </div>
  );
}

// ── Tagline + team nod ──────────────────────────────────────────────────
function ColleenTagline({ visible }) {
  if (!visible) return null;
  return (
    <div style={{
      position: 'absolute', top: 486, left: 0, right: 0, zIndex: 24,
      display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 12,
      pointerEvents: 'none',
    }}>
      <div style={{
        fontFamily: '"Caveat", "Nunito", cursive',
        fontSize: 30, fontWeight: 700, color: COAST.goldHi,
        textShadow: `0 2px 12px ${COAST.goldLo}55`,
      }}>Life is Good!</div>
      {/* Subtle team nod — three little hearts */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, opacity: 0.9 }}>
        {TEAM_NOD.map((tm, i) => (
          <svg key={i} width="15" height="15" viewBox="0 0 24 24" fill={tm.color}
               style={{ filter: `drop-shadow(0 1px 3px ${tm.color}88)` }}>
            <path d="M12 21s-7-4.5-9.5-9C1 9 2.5 5.5 6 5.5c2 0 3.2 1.3 4 2.3.8-1 2-2.3 4-2.3 3.5 0 5 3.5 3.5 6.5C19 16.5 12 21 12 21z"/>
          </svg>
        ))}
      </div>
    </div>
  );
}

// ── Dock — the button doubles as the live status ───────────────────────
function ColleenDock({ state, onToggleState, muted, onMute }) {
  const isLive = state !== 'offline' && state !== 'connecting' && state !== 'idle';
  const primaryActive = state === 'offline' || state === 'idle';

  // Status label when a channel is open — the button itself tells her what's
  // happening and she can tap it to close.
  const statusLabel =
    state === 'listening' ? 'Sam is listening' :
    state === 'thinking'  ? 'Sam is thinking\u2026' :
    state === 'speaking'  ? 'Sam is speaking' :
    state === 'muted'     ? 'Microphone off' : null;
  const primaryLabel =
    state === 'offline'    ? 'Open channel' :
    state === 'connecting' ? 'Engaging\u2026' :
    state === 'idle'       ? 'Open channel' :
    statusLabel;
  // Whose turn dot: teal while she talks (Sam listening), gold while Sam talks
  const turnDot = state === 'listening' ? COAST.tealHi : COAST.gold;

  const ctrlBg = 'rgba(8,40,40,0.55)';
  const iconColor = 'rgba(246,239,226,0.9)';

  const ctrl = (icon, label, onClick, active, disabled) => (
    <button onClick={onClick} disabled={disabled} style={{
      flex: 1, height: 46,
      background: active ? `${COAST.tealHi}33` : ctrlBg,
      backdropFilter: 'blur(20px)', WebkitBackdropFilter: 'blur(20px)',
      border: active ? `1px solid ${COAST.tealHi}` : `1px solid ${COAST.tealHi}22`,
      borderRadius: 15,
      display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
      gap: 2, cursor: disabled ? 'not-allowed' : 'pointer',
      color: active ? COAST.aqua : iconColor,
      fontFamily: '"Nunito", system-ui, sans-serif',
      fontSize: 10.5, fontWeight: 700, letterSpacing: '0.02em',
      opacity: disabled ? 0.4 : 1, transition: 'all 0.2s',
    }}>{icon}<span>{label}</span></button>
  );

  return (
    <div style={{
      position: 'absolute', left: 16, right: 16, bottom: 48, zIndex: 35,
      display: 'flex', flexDirection: 'column', gap: 11,
    }}>
      {/* Primary — gold "Open channel" when idle; live status (tap to close) when open */}
      <button onClick={onToggleState} style={{
        height: 60, borderRadius: 20, border: 'none', cursor: 'pointer',
        background: primaryActive
          ? `linear-gradient(180deg, ${COAST.goldHi} 0%, ${COAST.gold} 55%, ${COAST.goldLo} 100%)`
          : `${COAST.tealDeep}e0`,
        backdropFilter: !primaryActive ? 'blur(20px)' : 'none',
        WebkitBackdropFilter: !primaryActive ? 'blur(20px)' : 'none',
        boxShadow: primaryActive
          ? `0 10px 34px ${COAST.gold}66, inset 0 1px 0 ${COAST.goldHi}, 0 0 0 1px ${COAST.goldHi}88`
          : `0 0 0 1.5px ${turnDot}77`,
        color: primaryActive ? COAST.tealDeep : '#f6efe2',
        fontFamily: '"Nunito", system-ui, sans-serif',
        position: 'relative', overflow: 'hidden',
        display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 1,
      }}>
        {primaryActive ? (
          <span style={{ display: 'flex', alignItems: 'center', gap: 10, fontSize: 17, fontWeight: 800 }}>
            <svg width="22" height="22" viewBox="0 0 24 24" fill="none">
              <rect x="9" y="3" width="6" height="11" rx="3" fill={COAST.tealDeep}/>
              <path d="M5 11a7 7 0 0014 0M12 18v3" stroke={COAST.tealDeep} strokeWidth="2" strokeLinecap="round"/>
            </svg>
            {primaryLabel}
          </span>
        ) : (
          <>
            <span style={{ display: 'flex', alignItems: 'center', gap: 9, fontSize: 16.5, fontWeight: 800 }}>
              <span style={{
                width: 10, height: 10, borderRadius: 99, background: turnDot,
                boxShadow: `0 0 10px ${turnDot}`,
                animation: state === 'listening' ? 'pulseDot 1.6s ease-in-out infinite' : 'none',
              }} />
              {primaryLabel}
            </span>
            {state !== 'connecting' && (
              <span style={{ fontSize: 11, fontWeight: 600, color: 'rgba(246,239,226,0.6)' }}>
                Tap to close
              </span>
            )}
          </>
        )}
      </button>

      {/* Secondary — just two big clear options */}
      <div style={{ display: 'flex', gap: 10 }}>
        {ctrl(
          <svg width="20" height="20" viewBox="0 0 16 16" fill="none">
            {muted ? (
              <>
                <path d="M2 2l12 12" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
                <path d="M5 5v3a3 3 0 003 3" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
                <path d="M11 8V4a3 3 0 00-5.7-1.3" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
              </>
            ) : (
              <>
                <rect x="6" y="2" width="4" height="8" rx="2" stroke="currentColor" strokeWidth="1.6"/>
                <path d="M3 8a5 5 0 0010 0M8 13v2" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
              </>
            )}
          </svg>,
          muted ? 'Unmute' : 'Mute', onMute, muted, !isLive
        )}
        {ctrl(
          <svg width="20" height="20" viewBox="0 0 16 16" fill="none">
            <path d="M8 3v10M3 8h10" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
          </svg>,
          'New chat', () => window.dispatchEvent(new CustomEvent('sam-new-chat')), false, false
        )}
      </div>
    </div>
  );
}

Object.assign(window, { ColleenHeader, StateBanner, MicStatus, ColleenCaptions, ColleenTagline, ColleenDock });
