// Caliber marketing — product mock components.
// These are what show up "inside the screenshot" — candidate lists, Scout demo, chat, etc.
// Designed to be dropped into various hero/section layouts.

/* ============================================================
   ScoutDemo — Caliber Scout product mock for the marketing page.
   Figma-aligned: floating blue header pill + status bar + filter
   rail + candidate list. Uses real face photos / company logos
   from public/scout-demo/.
   ============================================================ */
const SCOUT_DEMO_CANDIDATES = [
  { name: 'Jason Wu',       role: 'Senior Full Stack Engineer', company: 'Salesforce', img: 'public/scout-demo/candidate-1-jason-wu.jpg',      logo: 'public/scout-demo/company-salesforce.png' },
  { name: 'Liam Ryder',     role: 'Lead Software Developer',    company: 'Spotify',    img: 'public/scout-demo/candidate-2-liam-ryder.jpg',    logo: 'public/scout-demo/company-spotify.png'    },
  { name: 'Priya Paoma',    role: 'Senior Frontend Engineer',   company: 'Netflix',    img: 'public/scout-demo/candidate-3-priya-paoma.jpg',   logo: 'public/scout-demo/company-netflix.jpg'    },
  { name: 'Alex Henderson', role: 'Senior Web Developer',       company: 'Xero',       img: 'public/scout-demo/candidate-4-alex-henderson.jpg',logo: 'public/scout-demo/company-xero.png'       },
  { name: 'Markus Jensen',  role: 'Senior Full Stack Engineer', company: 'Mastercard', img: 'public/scout-demo/candidate-5-markus-jensen.jpg', logo: 'public/scout-demo/company-mastercard.jpg' },
  { name: 'Aroha Kereama',  role: 'Software Architect',         company: 'Canva',      img: 'public/scout-demo/candidate-6-aroha-kereama.jpg', logo: 'public/scout-demo/company-canva.png'      },
];

// Figma-extracted palette
const SCOUT_TOKENS = {
  grad: 'linear-gradient(to right, #0141D7, #105BE2)',
  gradSoft: 'linear-gradient(to right, rgba(1,65,215,0.08), rgba(16,91,226,0.08))',
  gradStroke: 'linear-gradient(to right, rgba(1,65,215,0.35), rgba(16,91,226,0.35))',
  bg: '#F8F8FB',
  panel: '#FFFFFF',
  line: '#EEEEEE',
  lineSoft: '#F5F5F5',
  ink: '#111111',
  muted: '#888888',
  liBlue: '#0084FF',
  amber: '#B28815',
  amberBg: '#FFEEC3',
  amberDot: '#FFC935',
  warnOrange: '#FE833B',
};

// Local icons not in the shared Icon set
const IconMail = ({ size = 12, color = 'currentColor' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
    <rect x="3" y="5" width="18" height="14" rx="2"/>
    <path d="m3 7 9 6 9-6"/>
  </svg>
);
const IconEdit = ({ size = 12, color = 'currentColor' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
    <path d="M12 20h9"/>
    <path d="M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4 12.5-12.5z"/>
  </svg>
);
const IconAlert = ({ size = 12, color = 'currentColor' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill={color} aria-hidden>
    <circle cx="12" cy="12" r="10"/>
    <path d="M12 7v6M12 16v.5" stroke="#fff" strokeWidth="2.5" strokeLinecap="round"/>
  </svg>
);

// ---------- Chips ---------------------------------------------------------

// Figma-style tinted chip: soft gradient fill + gradient stroke. Used for
// verified attribute chips AND the Advance button (same visual vocabulary).
// `tone` picks the palette — 'blue' (default) or 'gold' (post-unlock).
function TintedChip({ children, tone = 'blue', style = {} }) {
  const palette = tone === 'gold'
    ? {
        bg: 'linear-gradient(to right, rgba(255,201,53,0.18), rgba(255,169,53,0.18))',
        stroke: 'rgba(178,136,21,0.35)',
        ink: SCOUT_TOKENS.amber,
      }
    : {
        bg: SCOUT_TOKENS.gradSoft,
        stroke: 'rgba(1,65,215,0.22)',
        ink: SCOUT_TOKENS.ink,
      };
  return (
    <span style={{
      position: 'relative',
      display: 'inline-flex', alignItems: 'center', gap: 5,
      padding: '5px 12px 5px 7px',
      background: palette.bg,
      color: palette.ink,
      border: '1px solid transparent',
      borderRadius: 18,
      fontSize: 11.5, fontWeight: 500,
      whiteSpace: 'nowrap',
      backgroundClip: 'padding-box',
      boxShadow: `inset 0 0 0 1px ${palette.stroke}`,
      transition: 'background .35s ease, color .35s ease, box-shadow .35s ease',
      ...style,
    }}>{children}</span>
  );
}

function VerifiedChip({ label, tone = 'blue' }) {
  const dotBg = tone === 'gold' ? SCOUT_TOKENS.amberDot : SCOUT_TOKENS.liBlue;
  return (
    <TintedChip tone={tone}>
      <span style={{
        width: 14, height: 14, borderRadius: 999,
        background: dotBg, color: '#fff',
        display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
        flexShrink: 0,
        transition: 'background .35s ease',
      }}>
        <svg width="8" height="8" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3.8" strokeLinecap="round" strokeLinejoin="round">
          <path d="m4 12 5 5L20 6"/>
        </svg>
      </span>
      {label}
    </TintedChip>
  );
}

function PlainChip({ label, tone = 'plain' }) {
  if (tone === 'gold') {
    return (
      <span style={{
        display: 'inline-flex', alignItems: 'center',
        padding: '5px 12px',
        background: SCOUT_TOKENS.amberBg, color: SCOUT_TOKENS.amber,
        borderRadius: 18,
        fontSize: 11.5, fontWeight: 500,
        whiteSpace: 'nowrap',
        transition: 'background .35s ease, color .35s ease',
      }}>{label}</span>
    );
  }
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center',
      padding: '5px 12px',
      background: SCOUT_TOKENS.lineSoft, color: SCOUT_TOKENS.muted,
      borderRadius: 18,
      fontSize: 11.5, fontWeight: 500,
      whiteSpace: 'nowrap',
      transition: 'background .35s ease, color .35s ease',
    }}>{label}</span>
  );
}

function LockChip({ unlocked }) {
  return (
    <span style={{
      width: 28, height: 28, borderRadius: 18,
      background: unlocked ? SCOUT_TOKENS.amberBg : SCOUT_TOKENS.lineSoft,
      color: unlocked ? SCOUT_TOKENS.amber : SCOUT_TOKENS.muted,
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
      flexShrink: 0,
      transition: 'background .35s ease, color .35s ease',
    }}>
      {unlocked ? (
        <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
          {/* unlocked padlock: shackle shifted to the side */}
          <rect x="4" y="11" width="16" height="10" rx="2"/>
          <path d="M8 11V7a4 4 0 0 1 7-2.7"/>
        </svg>
      ) : (
        <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round">
          <rect x="4" y="11" width="16" height="10" rx="2"/>
          <path d="M8 11V7a4 4 0 0 1 8 0v4"/>
        </svg>
      )}
    </span>
  );
}

// Status-bar filter chip (applied filter)
function AppliedChip({ icon, label }) {
  return (
    <TintedChip style={{ fontSize: 11, padding: '3px 10px 3px 6px' }}>
      <span style={{ display: 'inline-flex', color: SCOUT_TOKENS.ink }}>{icon}</span>
      {label}
    </TintedChip>
  );
}

// ---------- Header pill ---------------------------------------------------

function ScoutHeaderPill({ typed, phase }) {
  const placeholder = 'Search for Candidates, Contacts, Companies or Jobs';
  const showTyped = phase !== 'idle';
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 8,
      background: SCOUT_TOKENS.grad,
      borderRadius: 32,
      padding: 6,
      color: '#fff',
      boxShadow: '0 10px 30px -14px rgba(1,65,215,0.55)',
    }}>
      {/* Left avatar ring */}
      <span style={{
        width: 44, height: 44, borderRadius: 999,
        background: '#fff',
        padding: 3,
        display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
        flexShrink: 0,
      }}>
        <img src="public/scout-demo/user-avatar.png" alt="" width={38} height={38}
          style={{ width: 38, height: 38, borderRadius: 999, objectFit: 'cover', display: 'block' }}
        />
      </span>

      {/* Date pill */}
      <span style={{
        display: 'inline-flex', alignItems: 'center', gap: 6,
        padding: '8px 14px',
        background: 'rgba(255,255,255,0.15)',
        border: '1px solid rgba(255,255,255,0.18)',
        borderRadius: 22,
        fontSize: 12, fontWeight: 500,
        flexShrink: 0,
      }}>
        {Icon.calendar({ size: 12, color: '#fff' })}
        11 Nov
      </span>

      {/* Search input (translucent) */}
      <span style={{
        flex: 1, minWidth: 0,
        display: 'inline-flex', alignItems: 'center', gap: 10,
        padding: '0 18px', height: 44,
        background: 'rgba(255,255,255,0.15)',
        border: '1px solid rgba(255,255,255,0.18)',
        borderRadius: 22,
        fontSize: 13, fontWeight: 400,
      }}>
        {Icon.search({ size: 14, color: '#fff' })}
        <span style={{ flex: 1, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis', color: showTyped ? '#fff' : 'rgba(255,255,255,0.75)' }}>
          {showTyped ? typed : placeholder}
          {phase === 'typing' && <span className="cal-blink" style={{ marginLeft: 1 }}>|</span>}
        </span>
        <span style={{ fontSize: 11.5, opacity: 0.8, whiteSpace: 'nowrap' }}>
          Or click <b style={{ fontWeight: 600 }}>CTRL + K</b> to open
        </span>
      </span>

      {/* Time pill */}
      <span style={{
        padding: '8px 14px',
        background: 'rgba(255,255,255,0.15)',
        border: '1px solid rgba(255,255,255,0.18)',
        borderRadius: 22,
        fontSize: 12, fontWeight: 500,
        flexShrink: 0,
      }}>2:00 pm</span>

      {/* Right edit circle */}
      <button style={{
        width: 44, height: 44, borderRadius: 999, border: 'none',
        background: '#fff', color: SCOUT_TOKENS.ink,
        display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
        cursor: 'pointer', flexShrink: 0,
      }}><IconEdit size={16} /></button>
    </div>
  );
}

// ---------- Status bar ----------------------------------------------------

function ScoutStatusBar({ phase }) {
  const done = phase === 'done';
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap',
      background: SCOUT_TOKENS.panel,
      border: `1px solid ${SCOUT_TOKENS.lineSoft}`,
      borderRadius: 24,
      padding: '12px 20px',
      fontSize: 12,
      color: SCOUT_TOKENS.muted,
    }}>
      <CalStar size={14} grad={true} />
      <span style={{ color: SCOUT_TOKENS.ink, fontWeight: 500 }}>Scout is searching</span>
      <span style={{ fontFamily: 'ui-monospace, "SF Mono", Menlo, monospace', color: SCOUT_TOKENS.ink, fontWeight: 600 }}>2.4b+ profiles</span>
      <span>to find you</span>
      <AppliedChip icon={Icon.users({ size: 10 })} label="Senior" />
      <AppliedChip icon={Icon.briefcase({ size: 10 })} label="Software Engineer" />
      <span>candidates in</span>
      <AppliedChip icon={Icon.pin({ size: 10 })} label="Auckland" />
      <span>with</span>
      <AppliedChip icon={Icon.zap({ size: 10 })} label="React" />
      <span>skills, who have</span>
      <AppliedChip icon={<IconMail size={10} />} label="Email" />
      <AppliedChip icon={Icon.phone({ size: 10 })} label="Phone" />
      <div style={{ flex: 1 }} />
      {done ? (
        <span style={{ color: SCOUT_TOKENS.ink, fontWeight: 600, fontSize: 11.5, fontFamily: 'ui-monospace, "SF Mono", Menlo, monospace' }}>1,025 · &lt;15s</span>
      ) : (
        <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, fontSize: 11, color: SCOUT_TOKENS.muted }}>
          <span style={{
            width: 12, height: 12, borderRadius: 999,
            border: '2px solid rgba(1,65,215,0.18)', borderTopColor: '#0141D7',
            animation: 'cal-star-spin 0.8s linear infinite',
          }} />
          Searching…
        </span>
      )}
    </div>
  );
}

// ---------- Filter rail ---------------------------------------------------

function ScoutFilterRail({ compact, onReset }) {
  const filters = [
    { label: 'Job requirements',       count: 4, icon: Icon.briefcase({ size: 14, color: SCOUT_TOKENS.ink }), active: true },
    { label: 'Pay expectations',       sub: 'From $140,000 p.a.', icon: <span style={{ fontSize: 14, color: SCOUT_TOKENS.ink }}>$</span> },
    { label: 'Location & work',        count: 2, icon: Icon.pin({ size: 14, color: SCOUT_TOKENS.ink }) },
    { label: 'Contact preferences',    count: 1, icon: Icon.users({ size: 14, color: SCOUT_TOKENS.ink }) },
    { label: 'Date filters',           sub: 'Last week',          icon: Icon.calendar({ size: 14, color: SCOUT_TOKENS.ink }) },
  ];
  return (
    <div style={{
      width: compact ? 200 : 220, flexShrink: 0,
      background: SCOUT_TOKENS.panel,
      border: `1px solid ${SCOUT_TOKENS.lineSoft}`,
      borderRadius: 24,
      padding: 16,
      display: 'flex', flexDirection: 'column', gap: 14,
    }}>
      {/* nav arrows */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
        <span style={{
          display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          width: 44, height: 36, borderRadius: 18,
          background: SCOUT_TOKENS.lineSoft, color: SCOUT_TOKENS.muted,
        }}>{Icon.search({ size: 13 })}</span>
        <span style={{ flex: 1 }} />
        <span style={{
          display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          width: 36, height: 36, borderRadius: 999,
          background: SCOUT_TOKENS.lineSoft, color: SCOUT_TOKENS.muted,
        }}>{Icon.chevR({ size: 13 })}</span>
      </div>

      {/* stat summary rows */}
      <div style={{ display: 'flex', flexDirection: 'column', gap: 6, fontSize: 12 }}>
        <div style={{ display: 'inline-flex', alignItems: 'center', gap: 6, color: SCOUT_TOKENS.amber, fontWeight: 500 }}>
          <IconAlert size={13} color={SCOUT_TOKENS.warnOrange} />
          <span style={{ color: SCOUT_TOKENS.amber }}>1,025 results</span>
        </div>
        <div style={{ display: 'inline-flex', alignItems: 'center', gap: 6, color: SCOUT_TOKENS.ink, fontWeight: 500 }}>
          <span style={{
            width: 13, height: 13, borderRadius: 999,
            background: SCOUT_TOKENS.liBlue, color: '#fff',
            display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          }}>
            <svg width="7" height="7" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="4" strokeLinecap="round" strokeLinejoin="round"><path d="m4 12 5 5L20 6"/></svg>
          </span>
          2 keywords
        </div>
        <div style={{ display: 'inline-flex', alignItems: 'center', gap: 6, color: SCOUT_TOKENS.ink, fontWeight: 500 }}>
          <span style={{
            width: 13, height: 13, borderRadius: 999,
            background: SCOUT_TOKENS.liBlue, color: '#fff',
            display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          }}>
            <svg width="7" height="7" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="4" strokeLinecap="round" strokeLinejoin="round"><path d="m4 12 5 5L20 6"/></svg>
          </span>
          1 smart
        </div>
      </div>

      {/* progress */}
      <div>
        <div style={{ fontSize: 11, fontWeight: 600, color: SCOUT_TOKENS.ink, marginBottom: 4 }}>78%</div>
        <div style={{
          height: 6, borderRadius: 999,
          background: SCOUT_TOKENS.lineSoft,
          position: 'relative', overflow: 'hidden',
        }}>
          <div style={{
            position: 'absolute', inset: 0, width: '78%',
            background: SCOUT_TOKENS.grad, borderRadius: 999,
          }} />
        </div>
      </div>

      {/* filter items */}
      <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
        {filters.slice(0, compact ? 3 : 5).map((f) => (
          <div key={f.label} style={{
            display: 'flex', alignItems: 'center', gap: 10,
            padding: '8px 4px',
            borderTop: `1px solid ${SCOUT_TOKENS.lineSoft}`,
          }}>
            <span style={{
              width: 28, height: 28, borderRadius: 999,
              background: SCOUT_TOKENS.lineSoft,
              display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
              flexShrink: 0,
            }}>{f.icon}</span>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontSize: 11.5, fontWeight: 500, color: SCOUT_TOKENS.ink, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>
                {f.label}{f.count ? ` (${f.count})` : ''}
              </div>
              {f.sub && <div style={{ fontSize: 10.5, color: SCOUT_TOKENS.liBlue, fontWeight: 500 }}>{f.sub}</div>}
            </div>
          </div>
        ))}
      </div>

      {/* reset */}
      <button
        onClick={onReset}
        style={{
          marginTop: 'auto',
          padding: '9px 14px',
          background: SCOUT_TOKENS.panel,
          color: SCOUT_TOKENS.liBlue,
          border: `1px solid ${SCOUT_TOKENS.lineSoft}`,
          borderRadius: 18,
          fontSize: 12, fontWeight: 500,
          cursor: 'pointer',
        }}
      >Reset</button>
    </div>
  );
}

// ---------- Candidate card ------------------------------------------------

function ScoutCandidateCard({ c, visible, delay, compact, unlocked, onAdvance }) {
  const tone = unlocked ? 'gold' : 'blue';
  const handleClick = (e) => {
    if (unlocked) return;
    const rect = e.currentTarget.getBoundingClientRect();
    onAdvance(c.name, rect.left + rect.width / 2, rect.top + rect.height / 2);
  };
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 14,
      background: SCOUT_TOKENS.panel,
      border: `1px solid ${SCOUT_TOKENS.lineSoft}`,
      borderRadius: 24,
      padding: '14px 18px',
      opacity: visible ? 1 : 0,
      transform: visible ? 'translateY(0)' : 'translateY(10px)',
      transition: `opacity .5s ${delay}ms ease, transform .5s ${delay}ms ease`,
    }}>
      {/* avatar + name + location */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 12, width: compact ? 190 : 220, flexShrink: 0, minWidth: 0 }}>
        <div style={{ position: 'relative', flexShrink: 0 }}>
          <img src={c.img} alt="" width={44} height={44}
            style={{ width: 44, height: 44, borderRadius: '38%', objectFit: 'cover', display: 'block' }}
          />
          <span style={{
            position: 'absolute', bottom: -3, right: -3,
            width: 18, height: 18, borderRadius: 999,
            background: SCOUT_TOKENS.liBlue,
            display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
            border: '2px solid #fff',
            fontSize: 9, fontWeight: 800, letterSpacing: -0.02, color: '#fff',
            fontFamily: 'Arial, sans-serif',
          }}>in</span>
        </div>
        <div style={{ minWidth: 0 }}>
          <div style={{ fontWeight: 600, fontSize: 13, color: SCOUT_TOKENS.ink, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{c.name}</div>
          <div style={{ fontSize: 11, color: SCOUT_TOKENS.muted }}>New Zealand, Auckland</div>
        </div>
      </div>

      {/* company squircle + role + company */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, width: compact ? 190 : 230, flexShrink: 0, minWidth: 0 }}>
        <span style={{
          width: 36, height: 36, borderRadius: '32%',
          background: '#fff',
          overflow: 'hidden', flexShrink: 0,
          border: `1px solid ${SCOUT_TOKENS.lineSoft}`,
        }}>
          <img src={c.logo} alt="" width={36} height={36}
            style={{ width: 36, height: 36, objectFit: 'contain', display: 'block', padding: 4, boxSizing: 'border-box' }}
          />
        </span>
        <div style={{ minWidth: 0 }}>
          <div style={{ fontWeight: 600, fontSize: 12, color: SCOUT_TOKENS.ink, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{c.role}</div>
          <div style={{ fontSize: 11, color: SCOUT_TOKENS.liBlue, display: 'inline-flex', alignItems: 'center', gap: 3 }}>
            {c.company}
            <svg width="9" height="9" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M15 3h6v6M10 14 21 3M21 14v7H3V3h7"/></svg>
          </div>
        </div>
      </div>

      {/* chips */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 6, flexWrap: 'wrap', flex: 1, minWidth: 0 }}>
        <VerifiedChip label="Software engineer" tone={tone} />
        <VerifiedChip label="React" tone={tone} />
        <VerifiedChip label="Senior" tone={tone} />
        <LockChip unlocked={unlocked} />
        <PlainChip label="Education" tone={unlocked ? 'gold' : 'plain'} />
        <PlainChip label="Work history" tone={unlocked ? 'gold' : 'plain'} />
        {!compact && <PlainChip label="+ More" tone={unlocked ? 'gold' : 'plain'} />}
      </div>

      {/* Advance */}
      <button
        onClick={handleClick}
        style={{
          all: 'unset', cursor: unlocked ? 'default' : 'pointer',
          flexShrink: 0,
        }}
      >
        <TintedChip tone={tone} style={{ padding: '7px 14px', fontSize: 12 }}>
          {unlocked ? 'Advanced' : 'Advance'}
          {unlocked ? (
            <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round">
              <path d="m4 12 5 5L20 6"/>
            </svg>
          ) : (
            <CalStar size={11} grad={true} />
          )}
        </TintedChip>
      </button>
    </div>
  );
}

// ---------- Main demo -----------------------------------------------------

// `bare` strips the outer panel chrome (bg / shadow / border / padding / ambient glow)
// and keeps the body collapsed until the search completes, so the pill appears to
// "drop down" the rest of the UI. Used on the /scout hero.
function ScoutDemo({ autoplay = true, compact = false, bare = false }) {
  const [phase, setPhase] = React.useState('idle'); // idle | typing | searching | done
  const [typed, setTyped] = React.useState('');
  const [unlocked, setUnlocked] = React.useState(() => new Set());
  const fullQuery = 'Find me senior software engineers in Auckland with React experience';
  const ref = React.useRef(null);
  const inView = useInView(ref, 0.15);

  const handleAdvance = React.useCallback((name, x, y) => {
    setUnlocked(prev => {
      if (prev.has(name)) return prev;
      const next = new Set(prev);
      next.add(name);
      return next;
    });
    if (typeof window !== 'undefined' && typeof window.fireConfetti === 'function') {
      window.fireConfetti(x, y, {
        count: 90,
        colors: ['#FFC935', '#FFEEC3', '#B28815', '#FE833B', '#FFFFFF', '#0084FF'],
      });
    }
  }, []);

  const handleReset = React.useCallback(() => {
    setUnlocked(new Set());
  }, []);

  React.useEffect(() => {
    if (!autoplay || !inView) return;
    let t, i = 0;
    setTyped(''); setPhase('typing');
    const type = () => {
      if (i <= fullQuery.length) {
        setTyped(fullQuery.slice(0, i));
        i++;
        t = setTimeout(type, 22 + Math.random() * 14);
      } else {
        setPhase('searching');
        t = setTimeout(() => setPhase('done'), 900);
      }
    };
    t = setTimeout(type, 400);
    return () => clearTimeout(t);
  }, [autoplay, inView]);

  const visibleCount = phase === 'done' ? SCOUT_DEMO_CANDIDATES.length : 0;
  const showBody = phase !== 'idle';
  const bodyRevealed = bare ? phase === 'done' : showBody;
  const cards = SCOUT_DEMO_CANDIDATES.slice(0, compact ? 4 : 6);

  return (
    <div ref={ref} style={{
      position: 'relative',
      ...(bare ? {} : {
        background: SCOUT_TOKENS.bg,
        borderRadius: 28,
        padding: 16,
        boxShadow: 'var(--shadow-lg)',
        border: `1px solid ${SCOUT_TOKENS.lineSoft}`,
        overflow: 'hidden',
      }),
    }}>
      {/* ambient blue glow — only with chrome */}
      {!bare && (
        <div aria-hidden style={{
          position: 'absolute', top: -80, left: '50%', transform: 'translateX(-50%)',
          width: '60%', height: 200,
          background: 'radial-gradient(closest-side, rgba(1,65,215,0.18), transparent)',
          pointerEvents: 'none', filter: 'blur(40px)',
        }} />
      )}

      <div style={{
        position: 'relative', display: 'flex', flexDirection: 'column',
        gap: bare && !bodyRevealed ? 0 : 10,
        transition: 'gap .4s ease',
      }}>
        <ScoutHeaderPill typed={typed} phase={phase} />

        <div style={{
          display: 'flex', flexDirection: 'column', gap: 10,
          ...(bare
            ? {
                maxHeight: bodyRevealed ? 1400 : 0,
                opacity: bodyRevealed ? 1 : 0,
                transform: bodyRevealed ? 'translateY(0)' : 'translateY(-8px)',
                overflow: 'hidden',
                transition: 'max-height .6s cubic-bezier(.2,.7,.2,1), opacity .45s ease, transform .45s ease',
              }
            : {
                opacity: bodyRevealed ? 1 : 0.4,
                transition: 'opacity .4s ease',
              }),
        }}>
          <ScoutStatusBar phase={phase} />

          <div style={{ display: 'flex', gap: 10, alignItems: 'stretch' }}>
            <ScoutFilterRail compact={compact} onReset={handleReset} />
            <div style={{ flex: 1, display: 'flex', flexDirection: 'column', gap: 10, minWidth: 0 }}>
              {cards.map((c, i) => (
                <ScoutCandidateCard
                  key={c.name}
                  c={c}
                  visible={visibleCount > i}
                  delay={i * 80}
                  compact={compact}
                  unlocked={unlocked.has(c.name)}
                  onAdvance={handleAdvance}
                />
              ))}
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

/* ============================================================
   CandidatesTable — snapshot of the All Candidates view
   ============================================================ */
const CANDIDATE_ROWS = [
  { name: 'Georgia Bermann', role: 'Enterprise Account Executive', company: 'Salesforce', shortlisted: true, status: 'Open · Actively seeking', statusTone: 'green' },
  { name: 'Marcus Ross',     role: 'Senior Full Stack Developer',  company: 'Atlassian',  shortlisted: true, status: 'Open · Under offer',      statusTone: 'blue' },
  { name: 'Priya Gupta',     role: 'Lead Data Scientist',          company: 'Netflix',    shortlisted: true, status: 'Interviewing',             statusTone: 'purple' },
  { name: 'Allan Haigh',     role: 'Cyber Security Analyst',       company: 'Canva',      shortlisted: true, status: 'Open · Under offer',       statusTone: 'blue' },
  { name: 'Liam O’Connor',  role: 'Product Marketing Manager',    company: 'Spotify',    shortlisted: false, status: 'Not seeking',            statusTone: 'red' },
  { name: 'Rachelle Beaudry',role: 'Digital Marketing Specialist', company: 'Airbnb',     shortlisted: true, status: 'Open · Actively seeking', statusTone: 'green' },
  { name: 'Jessica Lee',     role: 'Design Analyst',               company: 'Xero',       shortlisted: false, status: 'Not seeking',              statusTone: 'red' },
  { name: 'Elias Thorne',    role: 'Senior Frontend Engineer',     company: 'Paypal',     shortlisted: false, status: 'Not seeking',              statusTone: 'red' },
];

const STATUS_TONES = {
  green:  { bg: '#DCFCE7', color: '#166534' },
  blue:   { bg: '#DBEAFE', color: '#1E40AF' },
  purple: { bg: '#F3E8FF', color: '#6B21A8' },
  red:    { bg: '#FEE2E2', color: '#991B1B' },
};

function CandidatesTable({ rows = CANDIDATE_ROWS, showChrome = true, maxRows }) {
  const display = maxRows ? rows.slice(0, maxRows) : rows;
  return (
    <div style={{
      background: '#fff', borderRadius: 18, overflow: 'hidden',
      boxShadow: 'var(--shadow-lg)', border: '1px solid var(--cal-line-2)',
    }}>
      {showChrome && (
        <div style={{
          background: 'var(--cal-blue-ink)', color: '#fff', padding: '18px 22px',
          display: 'flex', alignItems: 'center', gap: 12,
        }}>
          <span style={{ fontSize: 20, fontWeight: 500 }}>Candidates</span>
          <div style={{ flex: 1 }} />
          <button className="cal-btn cal-btn--white" style={{ padding: '8px 14px', fontSize: 13 }}>
            {Icon.plus({ size: 14 })} Add Candidate
          </button>
          <button className="cal-btn" style={{
            padding: '8px 14px', fontSize: 13, background: 'rgba(255,255,255,0.1)', color: '#fff',
            border: '1px solid rgba(255,255,255,0.3)',
          }}>
            {Icon.filter({ size: 14 })} Saved Filters
          </button>
        </div>
      )}
      {/* filter tabs */}
      <div style={{
        display: 'flex', gap: 8, padding: '14px 18px',
        borderBottom: '1px solid var(--cal-line-2)', overflow: 'hidden',
      }}>
        {[
          { label: 'All', count: '10,000', active: true },
          { label: 'Open', count: '5,529' },
          { label: 'Senior React Devs', count: '106', dot: '#22C55E' },
          { label: 'Product Managers', count: '1,922', dot: '#A855F7' },
          { label: 'SaaS Sales', count: '338', dot: '#EF4444' },
        ].map((t) => (
          <span key={t.label} style={{
            display: 'inline-flex', alignItems: 'center', gap: 6,
            padding: '6px 12px', borderRadius: 999,
            background: t.active ? 'var(--cal-blue-ink)' : '#F3F4F6',
            color: t.active ? '#fff' : 'var(--cal-ink)',
            fontSize: 13, fontWeight: 500, whiteSpace: 'nowrap',
          }}>
            {t.dot && <span style={{ width: 6, height: 6, borderRadius: 999, background: t.dot }} />}
            {t.label}
            <span style={{
              padding: '1px 7px', borderRadius: 999, fontSize: 11,
              background: t.active ? 'rgba(255,255,255,0.18)' : '#fff',
              color: t.active ? '#fff' : 'var(--cal-muted)',
              fontWeight: 500,
            }}>{t.count}</span>
          </span>
        ))}
      </div>

      {/* table header */}
      <div style={{
        display: 'grid',
        gridTemplateColumns: '24px 1.6fr 1.5fr 0.8fr 1.3fr 1fr 40px',
        gap: 12, padding: '10px 18px',
        fontSize: 12, color: 'var(--cal-muted)', fontWeight: 500,
        borderBottom: '1px solid var(--cal-line-2)',
      }}>
        <span></span>
        <span>Candidate name ↑↓</span>
        <span>Title ↑↓</span>
        <span>Shortlist</span>
        <span>Status ↑↓</span>
        <span>Phone</span>
        <span></span>
      </div>

      {display.map((r, i) => (
        <div key={r.name} style={{
          display: 'grid',
          gridTemplateColumns: '24px 1.6fr 1.5fr 0.8fr 1.3fr 1fr 40px',
          gap: 12, padding: '12px 18px', alignItems: 'center',
          borderBottom: i === display.length - 1 ? 'none' : '1px solid var(--cal-line-2)',
        }}>
          <input type="checkbox" style={{ accentColor: 'var(--cal-blue-ink)' }} />
          <div style={{ display: 'flex', alignItems: 'center', gap: 10, minWidth: 0 }}>
            <Avatar name={r.name} size={28} />
            <div style={{ fontSize: 14, fontWeight: 500, color: 'var(--cal-ink)', display: 'inline-flex', alignItems: 'center', gap: 6 }}>
              {r.name} <LinkedInBadge size={12} />
              {r.shortlisted && <CalStar size={11} grad={false} style={{ color: '#F59E0B' }} />}
            </div>
          </div>
          <div style={{ display: 'flex', alignItems: 'center', gap: 8, minWidth: 0 }}>
            <CompanyDot name={r.company} size={18} />
            <span style={{ fontSize: 13, color: 'var(--cal-body)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{r.role}</span>
          </div>
          <span style={{
            fontSize: 12, color: r.shortlisted ? 'var(--cal-green)' : 'var(--cal-muted)',
            fontWeight: 500,
          }}>
            {r.shortlisted ? '✓ Yes' : '✗ No'}
          </span>
          <span style={{
            display: 'inline-flex', alignItems: 'center', padding: '3px 10px', borderRadius: 999,
            fontSize: 11, fontWeight: 500, width: 'max-content',
            background: STATUS_TONES[r.statusTone].bg, color: STATUS_TONES[r.statusTone].color,
          }}>{r.status}</span>
          <span style={{ fontSize: 12, color: 'var(--cal-muted)' }}>+64 22 348 3151</span>
          <span style={{ color: r.shortlisted ? '#EF4444' : '#D1D5DB', textAlign: 'center' }}>♥</span>
        </div>
      ))}
    </div>
  );
}

/* ============================================================
   ChatMock — tiny conversation snippet
   ============================================================ */
function ChatMock() {
  return (
    <div style={{
      background: '#fff', borderRadius: 18, border: '1px solid var(--cal-line-2)',
      padding: 18, boxShadow: 'var(--shadow-md)',
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, paddingBottom: 12, borderBottom: '1px solid var(--cal-line-2)' }}>
        <Avatar name="Georgia Bermann" size={32} />
        <div>
          <div style={{ fontSize: 14, fontWeight: 600 }}>Georgia Bermann <span className="cal-chip" style={{ marginLeft: 4, fontSize: 10 }}>Candidate</span></div>
          <div style={{ fontSize: 11, color: 'var(--cal-green)', display: 'inline-flex', alignItems: 'center', gap: 5 }}>
            <span style={{ width: 6, height: 6, borderRadius: 999, background: 'var(--cal-green)' }} /> online
          </div>
        </div>
      </div>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 10, padding: '14px 0 12px' }}>
        <Bubble mine text="Confirmed for Thursday @ 10:00 AM! 📄 Attached are the interview brief and NDA." />
        <Bubble text="Received! Thanks, Jane. I'll sign the NDA and send it back this afternoon. Really looking forward to it." />
        <Bubble mine text="Sounds good. Sarah (VP of Product) will be joining the second half of the call." />
      </div>
      <div style={{
        display: 'flex', alignItems: 'center', gap: 8,
        border: '1px solid var(--cal-line)', borderRadius: 14, padding: '10px 12px',
      }}>
        <span style={{ flex: 1, fontSize: 13, color: 'var(--cal-muted)' }}>Google Meet! I've just sent the calendar invite…</span>
        <button style={{
          border: 'none', width: 30, height: 30, borderRadius: 999,
          background: 'var(--cal-blue-ink)', color: '#fff', display: 'inline-flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer',
        }}>{Icon.send({ size: 14, color: '#fff' })}</button>
      </div>
    </div>
  );
}
function Bubble({ mine, text }) {
  return (
    <div style={{ display: 'flex', justifyContent: mine ? 'flex-end' : 'flex-start' }}>
      <div style={{
        maxWidth: '80%', padding: '10px 14px', borderRadius: 14,
        fontSize: 13, lineHeight: 1.45,
        background: mine ? 'var(--cal-blue-ink)' : '#F3F4F6',
        color: mine ? '#fff' : 'var(--cal-ink)',
        borderBottomRightRadius: mine ? 4 : 14,
        borderBottomLeftRadius: mine ? 14 : 4,
      }}>{text}</div>
    </div>
  );
}

/* ============================================================
   TalentPools — snapshot grid
   ============================================================ */
const POOLS = [
  { name: 'Senior React Developers', count: 134, owner: 'Ryan',   updated: '2h',  color: '#2563EB' },
  { name: 'Enterprise Sales Q1',     count: 89,  owner: 'Ryan',   updated: '5m',  color: '#F97316' },
  { name: 'Executive Leadership',    count: 27,  owner: 'Sandra', updated: '1d',  color: '#A855F7' },
  { name: 'High Potential Finalists',count: 53,  owner: 'Mike',   updated: '4h',  color: '#EF4444' },
  { name: 'Global Tech Talent',      count: 402, owner: 'Sandra', updated: '12m', color: '#0EA5E9' },
  { name: 'Product Design Leads',    count: 61,  owner: 'Cassie', updated: '3d',  color: '#EC4899' },
];
function TalentPools() {
  return (
    <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 12 }}>
      {POOLS.map((p) => (
        <div key={p.name} className="cal-card" style={{ padding: 14, borderRadius: 14 }}>
          <div style={{ display: 'flex', gap: -6, marginBottom: 8 }}>
            {[0, 1, 2].map(i => (
              <Avatar key={i} name={p.name + i} size={26}
                variant="gradient"
                />
            ))}
          </div>
          <div style={{ fontSize: 14, fontWeight: 600 }}>{p.name}</div>
          <div style={{ fontSize: 12, color: 'var(--cal-muted)', marginTop: 2 }}>{p.count} candidates</div>
          <div style={{ marginTop: 10, display: 'flex', alignItems: 'center', gap: 6, fontSize: 11, color: 'var(--cal-muted)' }}>
            <Avatar name={p.owner} size={16} />
            <span>{p.owner}</span>
            <span style={{ marginLeft: 'auto' }}>Updated {p.updated}</span>
          </div>
        </div>
      ))}
    </div>
  );
}

/* ============================================================
   Mobile phone frame with caliber home (used in final section)
   ============================================================ */
function CaliberMobile() {
  return (
    <div style={{
      width: 300, background: '#fff', borderRadius: 36, padding: '16px 14px',
      border: '10px solid #111', boxShadow: 'var(--shadow-lg)',
      position: 'relative',
    }}>
      {/* notch */}
      <div style={{
        position: 'absolute', top: -10, left: '50%', transform: 'translateX(-50%)',
        width: 120, height: 22, background: '#111', borderRadius: '0 0 14px 14px',
      }} />
      <div style={{ paddingTop: 14 }}>
        <div style={{ display: 'flex', justifyContent: 'space-between', fontSize: 10, color: 'var(--cal-muted)', marginBottom: 14 }}>
          <span style={{ fontWeight: 600, color: 'var(--cal-ink)' }}>9:41</span>
          <span>● ● ● ▣</span>
        </div>
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 16 }}>
          <CalLogo size={16} />
          <div style={{ display: 'inline-flex', gap: 8, alignItems: 'center' }}>
            {Icon.search({ size: 16, color: 'var(--cal-ink)' })}
            <span style={{
              width: 24, height: 24, borderRadius: 999, background: 'var(--cal-blue-50)',
              display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
              color: 'var(--cal-blue-ink)',
            }}>{Icon.phone({ size: 13 })}</span>
          </div>
        </div>
        <div style={{ fontSize: 11, color: 'var(--cal-muted)', marginBottom: 2 }}>Friday, 1 Oct</div>
        <div style={{ fontSize: 18, fontWeight: 500, marginBottom: 14 }}>
          You have <span style={{ color: 'var(--cal-blue-ink)' }}>3 meetings</span> today
        </div>
        {/* date strip */}
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(7, 1fr)', gap: 2, marginBottom: 16 }}>
          {['Mon','Tue','Wed','Thu','Fri','Sat','Sun'].map((d, i) => (
            <div key={d} style={{
              textAlign: 'center', padding: '6px 0', borderRadius: 12,
              background: i === 4 ? 'var(--cal-blue-50)' : 'transparent',
              color: i === 4 ? 'var(--cal-blue-ink)' : 'var(--cal-muted)',
            }}>
              <div style={{ fontSize: 9 }}>{d}</div>
              <div style={{ fontSize: 13, fontWeight: i === 4 ? 600 : 400 }}>{27 + i}</div>
            </div>
          ))}
        </div>
        <div style={{ fontSize: 12, fontWeight: 600, marginBottom: 10 }}>Recently viewed</div>
        {[
          { name: 'Maria Oak',      role: 'Manager, Netflix' },
          { name: 'Georgia Bermann',role: 'Account Executive' },
          { name: 'Kyle Sewerter',  role: 'UX Designer' },
        ].map(c => (
          <div key={c.name} style={{
            display: 'flex', alignItems: 'center', gap: 10, padding: '8px 10px',
            marginBottom: 6, borderRadius: 12, border: '1px solid var(--cal-line-2)',
          }}>
            <Avatar name={c.name} size={26} />
            <div style={{ flex: 1 }}>
              <div style={{ fontSize: 12, fontWeight: 600 }}>{c.name}</div>
              <div style={{ fontSize: 10, color: 'var(--cal-muted)' }}>{c.role}</div>
            </div>
            <span style={{ color: 'var(--cal-blue-ink)' }}>{Icon.chat({ size: 13 })}</span>
          </div>
        ))}
        {/* floating Scout */}
        <div style={{
          position: 'absolute', bottom: 80, right: 20,
          width: 44, height: 44, borderRadius: 999,
          background: 'var(--cal-blue-ink)',
          display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          boxShadow: 'var(--shadow-blue)',
        }}>
          <CalStar size={20} grad={false} style={{ color: '#fff' }} />
        </div>
      </div>
    </div>
  );
}

Object.assign(window, {
  ScoutDemo, CandidatesTable, ChatMock, TalentPools, CaliberMobile,
  SCOUT_DEMO_CANDIDATES, CANDIDATE_ROWS,
});
