// ============================================================
// ShieldTech Secret Weapon — atomic components
// All atoms attach to window for cross-file babel access.
// ============================================================

// Token shortcuts (mirrors CSS vars; useful for inline styles)
const C = {
  bg1: '#05070A', bg2: '#0B1118', card: '#101923', cardElev: '#142131',
  border: '#1E293B', borderStrong: '#334155',
  fg1: '#F8FAFC', fg2: '#CBD5E1', fgMuted: '#94A3B8',
  blue: '#00A3FF', blueElectric: '#29C7FF', blueDeep: '#0369A1',
  success: '#22C55E', warning: '#F59E0B', danger: '#EF4444',
  assistant: '#A855F7', assistantSoft: '#D8B4FE',
  hot:      { bg: '#3B0D0D', fg: '#FCA5A5', border: '#7F1D1D' },
  warm:     { bg: '#3A2506', fg: '#FCD34D', border: '#92400E' },
  nurture:  { bg: '#082F49', fg: '#7DD3FC', border: '#075985' },
  dead:     { bg: '#1E293B', fg: '#94A3B8', border: '#334155' },
  done:     { bg: '#052E16', fg: '#86EFAC', border: '#166534' },
  approval: { bg: '#2E1065', fg: '#D8B4FE', border: '#6B21A8' },
};

// ───────────────────────────── Icon ─────────────────────────────
function Icon({ name, size = 20, color = 'currentColor', strokeWidth = 1.75, style = {} }) {
  const p = { width: size, height: size, viewBox: '0 0 24 24', fill: 'none',
    stroke: color, strokeWidth, strokeLinecap: 'round', strokeLinejoin: 'round', style };
  const paths = {
    target: <><circle cx="12" cy="12" r="10"/><circle cx="12" cy="12" r="6"/><circle cx="12" cy="12" r="2" fill={color} stroke="none"/></>,
    layers: <><polygon points="12 2 2 7 12 12 22 7 12 2"/><polyline points="2 17 12 22 22 17"/><polyline points="2 12 12 17 22 12"/></>,
    sparkles: <><path d="M12 3l1.5 4.5L18 9l-4.5 1.5L12 15l-1.5-4.5L6 9l4.5-1.5L12 3z"/><path d="M19 14l.7 2.3L22 17l-2.3.7L19 20l-.7-2.3L16 17l2.3-.7L19 14z"/></>,
    branch: <><circle cx="6" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><circle cx="18" cy="12" r="3"/><path d="M9 6h6a3 3 0 0 1 3 3v3"/><path d="M6 9v6"/></>,
    settings: <><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></>,
    phone: <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.13.96.36 1.9.7 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.91.34 1.85.57 2.81.7A2 2 0 0 1 22 16.92z"/>,
    mail: <><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22 6 12 13 2 6"/></>,
    voicemail: <><circle cx="6" cy="12" r="4"/><circle cx="18" cy="12" r="4"/><line x1="6" y1="16" x2="18" y2="16"/></>,
    mic: <><rect x="9" y="2" width="6" height="12" rx="3"/><path d="M19 10v2a7 7 0 0 1-14 0v-2"/><line x1="12" y1="19" x2="12" y2="23"/><line x1="8" y1="23" x2="16" y2="23"/></>,
    check: <polyline points="20 6 9 17 4 12"/>,
    checkCircle: <><circle cx="12" cy="12" r="10"/><polyline points="9 12 11 14 15 10"/></>,
    x: <><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></>,
    xCircle: <><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></>,
    clock: <><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></>,
    chevron: <polyline points="9 18 15 12 9 6"/>,
    chevronLeft: <polyline points="15 18 9 12 15 6"/>,
    chevronDown: <polyline points="6 9 12 15 18 9"/>,
    chevronUp: <polyline points="18 15 12 9 6 15"/>,
    flame: <path d="M8.5 14.5A2.5 2.5 0 0 0 11 12c0-1.38-.5-2-1-3-1.072-2.143-.224-4.054 2-6 .5 2.5 2 4.9 4 6.5 2 1.6 3 3.5 3 5.5a7 7 0 1 1-14 0c0-1.153.433-2.294 1-3a2.5 2.5 0 0 0 2.5 2.5z"/>,
    calendar: <><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></>,
    inbox: <><polyline points="22 12 16 12 14 15 10 15 8 12 2 12"/><path d="M5.45 5.11 2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z"/></>,
    gavel: <><path d="M14 13l-7 7-3-3 7-7"/><path d="M9 8l6 6"/><path d="M15 5l4 4"/><path d="M17 3l4 4"/><path d="M2 22h7"/></>,
    activity: <polyline points="22 12 18 12 15 21 9 3 6 12 2 12"/>,
    arrowRight: <><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></>,
    arrowUpRight: <><line x1="7" y1="17" x2="17" y2="7"/><polyline points="7 7 17 7 17 17"/></>,
    plus: <><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></>,
    play: <polygon points="6 3 20 12 6 21 6 3"/>,
    refresh: <><polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10"/><path d="M20.49 15a9 9 0 0 1-14.85 3.36L1 14"/></>,
    mapPin: <><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"/><circle cx="12" cy="10" r="3"/></>,
    user: <><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></>,
    users: <><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></>,
    moreHorizontal: <><circle cx="12" cy="12" r="1"/><circle cx="19" cy="12" r="1"/><circle cx="5" cy="12" r="1"/></>,
    google: <><path d="M21.35 11.1H12v3.2h5.35c-.5 2.4-2.6 4.1-5.35 4.1A6.4 6.4 0 0 1 5.6 12 6.4 6.4 0 0 1 12 5.6c1.6 0 3 .6 4.1 1.6l2.3-2.3A9.6 9.6 0 1 0 12 21.6c5.3 0 9.6-3.8 9.6-9.6 0-.3 0-.6-.05-.9z"/></>,
    building: <><rect x="4" y="3" width="16" height="18" rx="2"/><line x1="9" y1="9" x2="9" y2="9.01"/><line x1="15" y1="9" x2="15" y2="9.01"/><line x1="9" y1="13" x2="9" y2="13.01"/><line x1="15" y1="13" x2="15" y2="13.01"/><line x1="9" y1="17" x2="15" y2="17"/></>,
    hardHat: <><path d="M2 18h20"/><path d="M4 18a8 8 0 0 1 16 0"/><path d="M9 11V7a3 3 0 0 1 6 0v4"/></>,
    fileText: <><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></>,
    alert: <><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></>,
    alertTriangle: <><path d="M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></>,
    shield: <path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>,
    lock: <><rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></>,
    search: <><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></>,
    filter: <polygon points="22 3 2 3 10 12.46 10 19 14 21 14 12.46 22 3"/>,
    bell: <><path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.73 21a2 2 0 0 1-3.46 0"/></>,
    signal: <><line x1="2" y1="20" x2="2" y2="20"/><path d="M6.34 17.66a8 8 0 0 1 0-11.31"/><path d="M9.17 14.83a4 4 0 0 1 0-5.66"/><path d="M14.83 9.17a4 4 0 0 1 0 5.66"/><path d="M17.66 6.34a8 8 0 0 1 0 11.31"/><line x1="22" y1="4" x2="22" y2="4"/></>,
    powerOff: <><path d="M18.36 6.64a9 9 0 1 1-12.73 0"/><line x1="12" y1="2" x2="12" y2="12"/></>,
    trash: <><polyline points="3 6 5 6 21 6"/><path d="M19 6l-2 14a2 2 0 0 1-2 2H9a2 2 0 0 1-2-2L5 6"/><path d="M10 11v6"/><path d="M14 11v6"/></>,
    edit: <><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></>,
    sun: <><circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/></>,
    moon: <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>,
    trending: <><polyline points="23 6 13.5 15.5 8.5 10.5 1 18"/><polyline points="17 6 23 6 23 12"/></>,
    snooze: <><path d="M22 12A10 10 0 1 1 12 2"/><path d="M16 4h6v6"/><path d="M16 4l6 6"/></>,
    wifi: <><path d="M5 12.55a11 11 0 0 1 14.08 0"/><path d="M1.42 9a16 16 0 0 1 21.16 0"/><path d="M8.53 16.11a6 6 0 0 1 6.95 0"/><line x1="12" y1="20" x2="12.01" y2="20"/></>,
    wifiOff: <><line x1="1" y1="1" x2="23" y2="23"/><path d="M16.72 11.06A10.94 10.94 0 0 1 19 12.55"/><path d="M5 12.55a10.94 10.94 0 0 1 5.17-2.39"/><path d="M10.71 5.05A16 16 0 0 1 22.58 9"/><path d="M1.42 9a15.91 15.91 0 0 1 4.7-2.88"/><path d="M8.53 16.11a6 6 0 0 1 6.95 0"/><line x1="12" y1="20" x2="12.01" y2="20"/></>,
    cloud: <path d="M18 10h-1.26A8 8 0 1 0 9 20h9a5 5 0 0 0 0-10z"/>,
    database: <><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M21 12c0 1.66-4 3-9 3s-9-1.34-9-3"/><path d="M3 5v14c0 1.66 4 3 9 3s9-1.34 9-3V5"/></>,
    zap: <polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/>,
    download: <><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></>,
    send: <><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></>,
    eye: <><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></>,
    paperclip: <path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"/>,
    history: <><path d="M3 3v5h5"/><path d="M3.05 13A9 9 0 1 0 6 5.3L3 8"/><polyline points="12 7 12 12 16 14"/></>,
  };
  return <svg {...p}>{paths[name] || null}</svg>;
}

// ───────────────────────────── StatusPill / Badges ─────────────────────────────
function StatusPill({ kind = 'warm', children, dot = true, size = 'md', style = {} }) {
  const t = C[kind] || C.warm;
  const sm = size === 'sm';
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 6,
      padding: sm ? '4px 9px' : '5px 11px',
      borderRadius: 999,
      background: t.bg, color: t.fg, border: `1px solid ${t.border}`,
      font: `700 ${sm ? 11 : 12}px/1 Inter, system-ui, sans-serif`,
      letterSpacing: '-0.005em', whiteSpace: 'nowrap',
      ...style,
    }}>
      {dot && <span style={{ width: 5, height: 5, borderRadius: 999, background: t.fg }} />}
      {children}
    </span>
  );
}

function PriorityPill({ level = 'urgent', size = 'md' }) {
  const map = {
    urgent: { bg: '#3B0D0D', fg: '#FCA5A5', border: '#7F1D1D', label: 'Urgent' },
    high:   { bg: '#3A2506', fg: '#FCD34D', border: '#92400E', label: 'High' },
    medium: { bg: '#142131', fg: '#CBD5E1', border: '#334155', label: 'Medium' },
    low:    { bg: '#0B1118', fg: '#94A3B8', border: '#1E293B', label: 'Low' },
  };
  const t = map[level];
  const sm = size === 'sm';
  return (
    <span style={{
      padding: sm ? '3px 8px' : '4px 10px', borderRadius: 999,
      background: t.bg, color: t.fg, border: `1px solid ${t.border}`,
      font: `700 ${sm ? 10 : 11}px/1 Inter, system-ui, sans-serif`,
      letterSpacing: '0.02em', textTransform: 'uppercase',
    }}>{t.label}</span>
  );
}

function SyncChip({ state = 'connected', label, size = 'md' }) {
  const map = {
    connected:    { bg: '#052E16', fg: '#86EFAC', border: '#166534', pulse: false },
    disconnected: { bg: '#1E293B', fg: '#94A3B8', border: '#334155', pulse: false },
    syncing:      { bg: '#082F49', fg: '#7DD3FC', border: '#075985', pulse: true },
    error:        { bg: '#3B0D0D', fg: '#FCA5A5', border: '#7F1D1D', pulse: false },
    created:      { bg: '#052E16', fg: '#86EFAC', border: '#166534', pulse: false },
    pending:      { bg: '#3A2506', fg: '#FCD34D', border: '#92400E', pulse: false },
  };
  const t = map[state] || map.connected;
  const sm = size === 'sm';
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 6,
      padding: sm ? '3px 8px' : '4px 9px', borderRadius: 999,
      background: t.bg, color: t.fg, border: `1px solid ${t.border}`,
      font: `600 ${sm ? 10 : 11}px/1 Inter, system-ui, sans-serif`,
    }}>
      <span style={{
        width: 6, height: 6, borderRadius: 999, background: t.fg,
        position: 'relative',
        animation: t.pulse ? 'pulse 1.8s ease-in-out infinite' : 'none',
      }} />
      {label}
    </span>
  );
}

// ───────────────────────────── Cards ─────────────────────────────
function Card({ children, elevated = false, glow = null, hover = false, style = {}, onClick, className = '' }) {
  const glows = {
    blue: '0 0 24px rgba(0,163,255,0.22)',
    purple: '0 0 24px rgba(168,85,247,0.20)',
    danger: '0 0 24px rgba(239,68,68,0.18)',
  };
  return (
    <div onClick={onClick} className={`${hover ? 'card-hover' : ''} ${className}`} style={{
      background: elevated ? C.cardElev : C.card,
      border: `1px solid ${C.border}`,
      borderRadius: 24,
      boxShadow: `0 12px 40px rgba(0,0,0,0.28)${glow ? `, ${glows[glow]}` : ''}`,
      cursor: onClick ? 'pointer' : 'default',
      position: 'relative',
      ...style,
    }}>{children}</div>
  );
}

function GlassCard({ children, style = {}, className = '' }) {
  return (
    <div className={className} style={{
      background: 'rgba(16,25,35,0.82)',
      border: `1px solid ${C.border}`,
      borderRadius: 24,
      boxShadow: '0 12px 40px rgba(0,0,0,0.28)',
      backdropFilter: 'blur(18px)',
      WebkitBackdropFilter: 'blur(18px)',
      position: 'relative',
      ...style,
    }}>{children}</div>
  );
}

// ───────────────────────────── Buttons ─────────────────────────────
function PrimaryButton({ children, onClick, full, leadingIcon, trailingIcon, href, disabled, glow, style = {} }) {
  const Tag = href ? 'a' : 'button';
  const tagProps = href ? { href } : { type: 'button', disabled };
  return (
    <Tag {...tagProps} onClick={onClick} className={glow ? 'glow-blue' : ''} style={{
      height: 52, width: full ? '100%' : 'auto', padding: '0 22px',
      border: 0, borderRadius: 16,
      background: disabled ? C.cardElev : 'linear-gradient(135deg, #00A3FF, #29C7FF)',
      color: disabled ? C.fgMuted : '#03182A',
      textDecoration: 'none',
      font: '700 16px/1 Inter, system-ui, sans-serif', letterSpacing: '-0.005em',
      boxShadow: disabled ? 'none' : '0 0 24px rgba(0,163,255,0.28)',
      cursor: disabled ? 'not-allowed' : 'pointer',
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 10,
      transition: 'transform 120ms ease, filter 120ms ease',
      ...style,
    }}>
      {leadingIcon && <Icon name={leadingIcon} size={18} color={disabled ? C.fgMuted : '#03182A'} strokeWidth={2.25} />}
      {children}
      {trailingIcon && <Icon name={trailingIcon} size={18} color={disabled ? C.fgMuted : '#03182A'} strokeWidth={2.25} />}
    </Tag>
  );
}

function SecondaryButton({ children, onClick, full, leadingIcon, trailingIcon, href, style = {} }) {
  const Tag = href ? 'a' : 'button';
  const tagProps = href ? { href } : { type: 'button' };
  return (
    <Tag {...tagProps} onClick={onClick} style={{
      height: 48, width: full ? '100%' : 'auto', padding: '0 18px',
      borderRadius: 16, background: C.cardElev, color: C.fg1,
      textDecoration: 'none', border: `1px solid ${C.border}`,
      font: '600 15px/1 Inter, system-ui, sans-serif', cursor: 'pointer',
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 8,
      ...style,
    }}>
      {leadingIcon && <Icon name={leadingIcon} size={18} />}
      {children}
      {trailingIcon && <Icon name={trailingIcon} size={18} />}
    </Tag>
  );
}

function DangerButton({ children, onClick, full, leadingIcon, style = {} }) {
  return (
    <button onClick={onClick} type="button" style={{
      height: 48, width: full ? '100%' : 'auto', padding: '0 18px',
      borderRadius: 16, background: '#3B0D0D', color: '#FCA5A5',
      border: '1px solid #7F1D1D',
      font: '600 15px/1 Inter, system-ui, sans-serif', cursor: 'pointer',
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 8,
      ...style,
    }}>
      {leadingIcon && <Icon name={leadingIcon} size={18}/>}
      {children}
    </button>
  );
}

function SuccessButton({ children, onClick, full, leadingIcon, style = {} }) {
  return (
    <button onClick={onClick} type="button" style={{
      height: 48, width: full ? '100%' : 'auto', padding: '0 18px',
      borderRadius: 16, background: '#052E16', color: '#86EFAC',
      border: '1px solid #166534',
      font: '600 15px/1 Inter, system-ui, sans-serif', cursor: 'pointer',
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 8,
      ...style,
    }}>
      {leadingIcon && <Icon name={leadingIcon} size={18}/>}
      {children}
    </button>
  );
}

function IconButton({ icon, onClick, size = 40, color = C.fg1, bg = C.cardElev, border = C.border, style = {} }) {
  return (
    <button onClick={onClick} type="button" style={{
      width: size, height: size, borderRadius: Math.round(size * 0.3),
      background: bg, color, border: `1px solid ${border}`,
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer',
      ...style,
    }}>
      <Icon name={icon} size={Math.round(size * 0.5)}/>
    </button>
  );
}

// ───────────────────────────── Caption / labels ─────────────────────────────
function Caption({ children, color = C.fgMuted, style = {} }) {
  return <span style={{
    font: '500 11px/1.4 Inter, system-ui, sans-serif',
    letterSpacing: '0.08em', textTransform: 'uppercase',
    color, ...style,
  }}>{children}</span>;
}

// ───────────────────────────── Bottom nav ─────────────────────────────
function BottomNav({ active, onChange }) {
  const tabs = [
    { id: 'today', label: 'Today', icon: 'target' },
    { id: 'tasks', label: 'Tasks', icon: 'layers' },
    { id: 'bids', label: 'Bids', icon: 'fileText' },
    { id: 'assistant', label: 'Assistant', icon: 'sparkles' },
    { id: 'pipeline', label: 'Pipeline', icon: 'branch' },
    { id: 'settings', label: 'Settings', icon: 'settings' },
  ];
  return (
    <div style={{
      position: 'absolute', left: 12, right: 12, bottom: 'calc(env(safe-area-inset-bottom) + 12px)', height: 64, borderRadius: 22,
      background: 'rgba(11,17,24,0.78)', border: `1px solid ${C.border}`,
      backdropFilter: 'blur(24px)', WebkitBackdropFilter: 'blur(24px)',
      display: 'grid', gridTemplateColumns: 'repeat(6, 1fr)', alignItems: 'center',
      zIndex: 70, boxShadow: '0 16px 50px rgba(0,0,0,0.5)',
    }}>
      {tabs.map(t => {
        const on = active === t.id;
        return (
          <button key={t.id} onClick={() => onChange?.(t.id)} style={{
            background: 'transparent', border: 0, cursor: 'pointer',
            display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 3,
            color: on ? C.blueElectric : C.fgMuted,
            font: '600 10px/1 Inter, system-ui, sans-serif', padding: '8px 2px',
            position: 'relative', minWidth: 0, overflow: 'hidden',
          }}>
            {on && <span style={{
              position: 'absolute', top: 4, width: 24, height: 3, borderRadius: 2,
              background: C.blueElectric, boxShadow: '0 0 8px rgba(41,199,255,0.6)',
            }}/>}
            <Icon name={t.icon} size={21} />
            {t.label}
          </button>
        );
      })}
    </div>
  );
}

// ───────────────────────────── Page header (mobile) ─────────────────────────────
function MobileHeader({ title, subtitle, leading, trailing, large = true }) {
  return (
    <div style={{ padding: '14px 20px 8px', display: 'flex', alignItems: 'center', gap: 12 }}>
      {leading}
      <div style={{ flex: 1, minWidth: 0 }}>
        {subtitle && <Caption>{subtitle}</Caption>}
        <div style={{
          font: `700 ${large ? 26 : 22}px/1.1 Rajdhani, "Inter", system-ui, sans-serif`,
          letterSpacing: '0.01em', color: C.fg1, marginTop: subtitle ? 4 : 0,
        }}>{title}</div>
      </div>
      {trailing}
    </div>
  );
}

// ───────────────────────────── Back row ─────────────────────────────
function BackRow({ onBack, label = 'Back', center, trailing }) {
  return (
    <div style={{ padding: '12px 20px 4px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8 }}>
      <button onClick={onBack} style={{
        background: 'transparent', color: C.fgMuted, border: 0,
        display: 'inline-flex', alignItems: 'center', gap: 6, cursor: 'pointer',
        font: '600 14px/1 Inter, system-ui, sans-serif', padding: 0,
      }}>
        <Icon name="chevronLeft" size={18} />{label}
      </button>
      <div style={{ font: '500 11px/1.4 Inter, system-ui, sans-serif', letterSpacing: '0.08em', textTransform: 'uppercase', color: C.fgMuted }}>
        {center}
      </div>
      <div style={{ minWidth: 60, display: 'flex', justifyContent: 'flex-end' }}>{trailing}</div>
    </div>
  );
}

// ───────────────────────────── Mission progress ring ─────────────────────────────
function ProgressRing({ value = 0, total = 12, size = 92, label = 'CALLS' }) {
  const pct = Math.min(1, value / total) * 100;
  return (
    <div style={{
      width: size, height: size, flex: 'none', borderRadius: 999, position: 'relative',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
    }}>
      {/* outer pulse */}
      <div style={{
        position: 'absolute', inset: -3, borderRadius: 999,
        border: '1.5px solid rgba(0,163,255,0.25)',
        animation: 'ringPulse 2.4s ease-out infinite',
      }} />
      <div className="mission-ring-track" style={{
        '--pct': `${pct}%`, width: '100%', height: '100%', borderRadius: 999,
        boxShadow: '0 0 28px rgba(0,163,255,0.28)',
      }}/>
      <div style={{ position: 'absolute', inset: 5, borderRadius: 999, background: 'rgba(11,17,24,0.96)' }}/>
      <div style={{ position: 'relative', zIndex: 1, textAlign: 'center' }}>
        <div style={{ font: '700 22px/1 Rajdhani, "Inter", system-ui, sans-serif', fontVariantNumeric: 'tabular-nums', color: C.fg1 }}>{value}/{total}</div>
        <div style={{ font: '500 9px/1 Inter, system-ui, sans-serif', letterSpacing: '0.12em', color: C.fgMuted, marginTop: 4 }}>{label}</div>
      </div>
    </div>
  );
}

// ───────────────────────────── Bar / linear progress ─────────────────────────────
function LinearProgress({ value, total, color = C.blue, height = 6, label, sublabel }) {
  const pct = Math.min(100, (value / total) * 100);
  return (
    <div style={{ width: '100%' }}>
      {(label || sublabel) && (
        <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: 6, gap: 8 }}>
          {label && <span style={{ font: '600 12px/1 Inter, system-ui, sans-serif', color: C.fg2, whiteSpace: 'nowrap' }}>{label}</span>}
          {sublabel && <span style={{ font: '500 12px/1 Inter, system-ui, sans-serif', color: C.fgMuted, fontVariantNumeric: 'tabular-nums' }}>{sublabel}</span>}
        </div>
      )}
      <div style={{ width: '100%', height, background: C.cardElev, borderRadius: 999, overflow: 'hidden' }}>
        <div style={{
          width: `${pct}%`, height: '100%',
          background: `linear-gradient(90deg, ${C.blue}, ${C.blueElectric})`,
          borderRadius: 999, boxShadow: `0 0 10px ${color}66`,
          transition: 'width 600ms cubic-bezier(0.22,1,0.36,1)',
        }} />
      </div>
    </div>
  );
}

// ───────────────────────────── Toggle ─────────────────────────────
function Toggle({ on, onChange }) {
  return (
    <button onClick={() => onChange?.(!on)} type="button" style={{
      width: 42, height: 26, borderRadius: 999,
      background: on ? 'linear-gradient(135deg,#00A3FF,#29C7FF)' : C.cardElev,
      border: `1px solid ${on ? '#0369A1' : C.border}`, position: 'relative',
      boxShadow: on ? '0 0 14px rgba(0,163,255,0.32)' : 'none',
      cursor: 'pointer', padding: 0,
    }}>
      <div style={{
        position: 'absolute', top: 1, left: on ? 18 : 2,
        width: 22, height: 22, borderRadius: 999, background: '#fff',
        boxShadow: '0 2px 6px rgba(0,0,0,0.4)', transition: 'left 160ms ease',
      }}/>
    </button>
  );
}

// ───────────────────────────── Avatar (initials) ─────────────────────────────
function Avatar({ initials, size = 40, color = C.blueElectric }) {
  return (
    <div style={{
      width: size, height: size, flex: 'none', borderRadius: Math.round(size * 0.3),
      background: 'linear-gradient(135deg, #142131, #1E293B)',
      border: `1px solid ${C.border}`,
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      color, font: `700 ${Math.round(size * 0.32)}px/1 Inter, system-ui, sans-serif`,
      letterSpacing: '0.02em',
    }}>{initials}</div>
  );
}

// ───────────────────────────── Typing indicator ─────────────────────────────
function TypingDots({ color = C.assistant }) {
  return (
    <span style={{ display: 'inline-flex', gap: 4, padding: '6px 2px' }}>
      <span className="typing-dot" style={{ background: color, animationDelay: '0ms' }}/>
      <span className="typing-dot" style={{ background: color, animationDelay: '160ms' }}/>
      <span className="typing-dot" style={{ background: color, animationDelay: '320ms' }}/>
    </span>
  );
}

// ───────────────────────────── Section header (in-page) ─────────────────────────────
function SectionHeader({ title, right }) {
  return (
    <div style={{ padding: '14px 20px 8px', display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
      <Caption>{title}</Caption>
      {right}
    </div>
  );
}

// ───────────────────────────── Empty / Loading / Error states ─────────────────────────────
function EmptyState({ icon = 'inbox', title, body, action }) {
  return (
    <div style={{ padding: '36px 24px', textAlign: 'center', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 12 }}>
      <div style={{ width: 56, height: 56, borderRadius: 18, background: C.cardElev, border: `1px solid ${C.border}`, display: 'flex', alignItems: 'center', justifyContent: 'center', color: C.fgMuted }}>
        <Icon name={icon} size={26}/>
      </div>
      <div style={{ font: '700 18px/1.2 Rajdhani, "Inter", system-ui, sans-serif', color: C.fg1 }}>{title}</div>
      {body && <div style={{ font: '400 14px/20px Inter, system-ui, sans-serif', color: C.fg2, maxWidth: 260 }}>{body}</div>}
      {action}
    </div>
  );
}

function LoadingShimmer({ height = 64, style = {} }) {
  return (
    <div style={{
      height, borderRadius: 16, background: `linear-gradient(90deg, ${C.card} 0%, ${C.cardElev} 50%, ${C.card} 100%)`,
      backgroundSize: '200% 100%', animation: 'shimmer 1.8s linear infinite',
      border: `1px solid ${C.border}`, ...style,
    }}/>
  );
}

function ErrorState({ title = 'Something went wrong', body, onRetry }) {
  return (
    <Card glow="danger" style={{ padding: 18, borderColor: '#7F1D1D', background: '#1B0908' }}>
      <div style={{ display: 'flex', alignItems: 'flex-start', gap: 12 }}>
        <div style={{ width: 36, height: 36, borderRadius: 12, background: '#3B0D0D', border: '1px solid #7F1D1D', display: 'flex', alignItems: 'center', justifyContent: 'center', flex: 'none' }}>
          <Icon name="alert" size={18} color="#FCA5A5"/>
        </div>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ font: '700 15px/1.2 Inter, system-ui, sans-serif', color: '#FCA5A5' }}>{title}</div>
          {body && <div style={{ font: '400 13px/18px Inter, system-ui, sans-serif', color: C.fg2, marginTop: 4 }}>{body}</div>}
          {onRetry && (
            <button onClick={onRetry} style={{ marginTop: 10, padding: '8px 14px', borderRadius: 12, background: '#7F1D1D', border: 0, color: '#fff', font: '600 13px/1 Inter, system-ui, sans-serif', cursor: 'pointer' }}>
              Retry
            </button>
          )}
        </div>
      </div>
    </Card>
  );
}

Object.assign(window, {
  C, Icon, StatusPill, PriorityPill, SyncChip, Card, GlassCard,
  PrimaryButton, SecondaryButton, DangerButton, SuccessButton, IconButton,
  Caption, BottomNav, MobileHeader, BackRow, ProgressRing, LinearProgress,
  Toggle, Avatar, TypingDots, SectionHeader, EmptyState, LoadingShimmer, ErrorState,
});
