// OrgLens — Shell components (Header, Sidebar, shared icons, primitives)

// ===================== ICON LIBRARY =====================
const Icon = ({ d, size = 16, color = "currentColor", viewBox = "0 0 24 24", style, ...rest }) => (
  <svg width={size} height={size} viewBox={viewBox} fill={color} style={{flexShrink:0, display:'block', ...style}} {...rest}>
    {Array.isArray(d) ? d.map((p, i) => <path key={i} d={p}/>) : <path d={d}/>}
  </svg>
);

const ICONS = {
  grid:       "M3 3h5v5H3V3zm0 8h5v5H3v-5zm8-8h5v5h-5V3zm0 8h5v5h-5v-5zm8-8h5v5h-5V3zm0 8h5v5h-5v-5z",
  home:       "M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z",
  dashboard:  "M3 3h8v10H3V3zm10 0h8v6h-8V3zM3 15h8v6H3v-6zm10-4h8v10h-8V11z",
  list:       "M3 13h2v-2H3v2zm0 4h2v-2H3v2zm0-8h2V7H3v2zm4 4h14v-2H7v2zm0 4h14v-2H7v2zM7 7v2h14V7H7z",
  map:        "M20.5 3l-.16.03L15 5.1 9 3 3.36 4.9c-.21.07-.36.25-.36.48V20.5c0 .28.22.5.5.5l.16-.03L9 18.9l6 2.1 5.64-1.9c.21-.07.36-.25.36-.48V3.5c0-.28-.22-.5-.5-.5zM10 5.47l4 1.4v11.66l-4-1.4V5.47zm-5 .99l3-1.01v11.7l-3 1.16V6.46zm14 11.08l-3 1.01V6.86l3-1.16v11.84z",
  sparkle:    "M12 2l1.5 4.5L18 8l-4.5 1.5L12 14l-1.5-4.5L6 8l4.5-1.5L12 2zm6 12l.75 2.25L21 17l-2.25.75L18 20l-.75-2.25L15 17l2.25-.75L18 14zM5 14l.75 2.25L8 17l-2.25.75L5 20l-.75-2.25L2 17l2.25-.75L5 14z",
  building:   "M12 7V3H2v18h20V7H12zM6 19H4v-2h2v2zm0-4H4v-2h2v2zm0-4H4V9h2v2zm0-4H4V5h2v2zm4 12H8v-2h2v2zm0-4H8v-2h2v2zm0-4H8V9h2v2zm0-4H8V5h2v2zm10 12h-8v-2h2v-2h-2v-2h2v-2h-2V9h8v10zm-2-8h-2v2h2v-2zm0 4h-2v2h2v-2z",
  field:      "M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-5 14H7v-2h7v2zm3-4H7v-2h10v2zm0-4H7V7h10v2z",
  lock:       "M18 8h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zm-6 9c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm3.1-9H8.9V6c0-1.71 1.39-3.1 3.1-3.1 1.71 0 3.1 1.39 3.1 3.1v2z",
  apex:       "M9.4 16.6L4.8 12l4.6-4.6L8 6l-6 6 6 6 1.4-1.4zm5.2 0l4.6-4.6-4.6-4.6L16 6l6 6-6 6-1.4-1.4z",
  release:    "M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z",
  report:     "M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z",
  settings:   "M19.14 12.94c.04-.3.06-.61.06-.94 0-.32-.02-.64-.07-.94l2.03-1.58c.18-.14.23-.41.12-.61l-1.92-3.32c-.12-.22-.37-.29-.59-.22l-2.39.96c-.5-.38-1.03-.7-1.62-.94l-.36-2.54c-.04-.24-.24-.41-.48-.41h-3.84c-.24 0-.43.17-.47.41l-.36 2.54c-.59.24-1.13.57-1.62.94l-2.39-.96c-.22-.08-.47 0-.59.22L2.74 8.87c-.12.21-.08.47.12.61l2.03 1.58c-.05.3-.09.63-.09.94s.02.64.07.94l-2.03 1.58c-.18.14-.23.41-.12.61l1.92 3.32c.12.22.37.29.59.22l2.39-.96c.5.38 1.03.7 1.62.94l.36 2.54c.05.24.24.41.48.41h3.84c.24 0 .44-.17.47-.41l.36-2.54c.59-.24 1.13-.56 1.62-.94l2.39.96c.22.08.47 0 .59-.22l1.92-3.32c.12-.22.07-.47-.12-.61l-2.01-1.58zM12 15.6c-1.98 0-3.6-1.62-3.6-3.6s1.62-3.6 3.6-3.6 3.6 1.62 3.6 3.6-1.62 3.6-3.6 3.6z",
  search:     "M15.5 14h-.79l-.28-.27A6.47 6.47 0 0 0 16 9.5 6.5 6.5 0 1 0 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z",
  bell:       "M12 22c1.1 0 2-.9 2-2h-4c0 1.1.9 2 2 2zm6-6v-5c0-3.07-1.64-5.64-4.5-6.32V4c0-.83-.67-1.5-1.5-1.5s-1.5.67-1.5 1.5v.68C7.63 5.36 6 7.92 6 11v5l-2 2v1h16v-1l-2-2z",
  help:       "M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 17h-2v-2h2v2zm2.07-7.75l-.9.92C13.45 12.9 13 13.5 13 15h-2v-.5c0-1.1.45-2.1 1.17-2.83l1.24-1.26c.37-.36.59-.86.59-1.41 0-1.1-.9-2-2-2s-2 .9-2 2H8c0-2.21 1.79-4 4-4s4 1.79 4 4c0 .88-.36 1.68-.93 2.25z",
  chevR:      "M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z",
  chevD:      "M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6-1.41-1.41z",
  chevU:      "M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z",
  arrowDown:  "M16 13h-3V3h-2v10H8l4 4 4-4zM4 19v2h16v-2H4z",
  arrowUp:    "M8 11h3v10h2V11h3l-4-4-4 4zM4 3v2h16V3H4z",
  arrowFlat:  "M3 11h18v2H3z",
  check:      "M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z",
  warn:       "M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z",
  error:      "M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z",
  info:       "M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z",
  close:      "M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z",
  download:   "M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z",
  refresh:    "M17.65 6.35A7.95 7.95 0 0 0 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08A5.99 5.99 0 0 1 12 18c-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z",
  jira:       "M11.53 2.05A11.55 11.55 0 0 0 5.5 5.5l5.5 5.5h.53V2.05zm6.97 3.45L13 11h.53V18a11.55 11.55 0 0 0 5.5-5.5 11.55 11.55 0 0 0 0-7zM5.5 18.5A11.55 11.55 0 0 0 11.53 22V11l-5.5 5.5z",
  filter:     "M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z",
  more:       "M12 8c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm0 2c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm0 6c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z",
  play:       "M8 5v14l11-7z",
  externalLink: "M19 19H5V5h7V3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2v-7h-2v7zM14 3v2h3.59l-9.83 9.83 1.41 1.41L19 6.41V10h2V3h-7z",
  user:       "M12 12c2.7 0 4.8-2.1 4.8-4.8S14.7 2.4 12 2.4 7.2 4.5 7.2 7.2 9.3 12 12 12zm0 2.4c-3.2 0-9.6 1.6-9.6 4.8v2.4h19.2v-2.4c0-3.2-6.4-4.8-9.6-4.8z",
  flow:       "M2.5 19.5L7 15h3.5l3-3H17l4.5-4.5-1-1L16.5 11H13l-3 3H6L1.5 18.5l1 1zM18 5h4v4h-2V6.41l-3.29 3.3-1.42-1.42L18.59 5H18z",
};

const CloudMark = ({ size = 28, color = "#fff" }) => (
  <svg width={size} height={size} viewBox="0 0 100 100" fill={color} style={{flexShrink:0,display:'block'}}>
    <path d="M41.5 22c5.2 0 9.9 2.1 13.3 5.5 2.8-1.2 5.9-1.9 9.2-1.9 12.2 0 22 9.8 22 22 0 12.2-9.8 22-22 22-1.6 0-3.2-.2-4.7-.5-2.9 3.8-7.5 6.3-12.7 6.3-2.6 0-5-.6-7.2-1.8C37.4 76 34 77 30.5 77 21.4 77 14 69.6 14 60.5c0-3.5 1-6.7 2.8-9.5C14.6 48.7 13 45.5 13 42c0-8.3 6.7-15 15-15 1 0 2 .1 2.9.3 2.5-3.2 6.4-5.3 10.6-5.3z"/>
  </svg>
);

// OrgLens logo mark — magnifying glass over cloud
const OrgLensMark = ({ size = 28 }) => (
  <svg width={size} height={size} viewBox="0 0 32 32" style={{flexShrink:0, display:'block'}}>
    <defs>
      <linearGradient id="lensGrad" x1="0" y1="0" x2="1" y2="1">
        <stop offset="0" stopColor="#1b96ff"/>
        <stop offset="1" stopColor="#0070d2"/>
      </linearGradient>
    </defs>
    <circle cx="14" cy="14" r="9" fill="none" stroke="url(#lensGrad)" strokeWidth="3"/>
    <circle cx="14" cy="14" r="5" fill="#ffffff" opacity="0.95"/>
    <path d="M14 11 L14 17 M11 14 L17 14" stroke="#0070d2" strokeWidth="1.6" strokeLinecap="round"/>
    <rect x="20" y="20" width="9" height="3.6" rx="1.8" transform="rotate(45 20 20)" fill="#1b96ff"/>
  </svg>
);

// ===================== GLOBAL HEADER =====================
const headerStyles = {
  bar: {
    height: 52,
    background: '#032d60',
    display: 'flex',
    alignItems: 'center',
    paddingLeft: 12,
    paddingRight: 16,
    gap: 8,
    flexShrink: 0,
    position: 'relative',
    zIndex: 100,
  },
  gridBtn: {
    width: 32, height: 32, borderRadius: 4, border: 'none',
    background: 'rgba(255,255,255,0.12)', cursor: 'pointer',
    display: 'flex', alignItems: 'center', justifyContent: 'center',
    color: '#fff', flexShrink: 0,
  },
  logoArea: { display: 'flex', alignItems: 'center', gap: 10, marginRight: 8 },
  appName: { color: '#fff', fontSize: 16, fontWeight: 700, whiteSpace: 'nowrap', letterSpacing: '-0.005em' },
  appSub: { color: 'rgba(255,255,255,0.7)', fontSize: 11, fontWeight: 400, whiteSpace: 'nowrap', marginLeft: 6 },
  divider: { width: 1, height: 24, background: 'rgba(255,255,255,0.2)', marginLeft: 4, marginRight: 4 },
  searchWrap: { flex: 1, maxWidth: 420, position: 'relative' },
  searchInput: {
    width: '100%', height: 32, background: 'rgba(255,255,255,0.15)',
    border: '1px solid rgba(255,255,255,0.3)', borderRadius: 4,
    color: '#fff', fontSize: 13, padding: '0 12px 0 36px',
    outline: 'none', fontFamily: 'inherit',
  },
  searchIcon: { position: 'absolute', left: 10, top: '50%', transform: 'translateY(-50%)', color: 'rgba(255,255,255,0.6)' },
  navRight: { display: 'flex', alignItems: 'center', gap: 4, marginLeft: 'auto' },
  iconBtn: {
    width: 32, height: 32, borderRadius: '50%', border: 'none',
    background: 'transparent', cursor: 'pointer',
    display: 'flex', alignItems: 'center', justifyContent: 'center',
    color: 'rgba(255,255,255,0.85)', position: 'relative',
  },
  badge: {
    position: 'absolute', top: 2, right: 2, width: 14, height: 14,
    background: '#c23934', borderRadius: '50%', fontSize: 9, fontWeight: 700,
    color: '#fff', display: 'flex', alignItems: 'center', justifyContent: 'center',
    border: '1px solid #032d60',
  },
  avatar: {
    width: 32, height: 32, borderRadius: '50%', background: '#7f8de1',
    display: 'flex', alignItems: 'center', justifyContent: 'center',
    fontSize: 12, fontWeight: 700, color: '#fff', cursor: 'pointer',
    border: '1px solid rgba(255,255,255,0.3)',
  },
};

function GlobalHeader() {
  return (
    <header style={headerStyles.bar}>
      <button style={headerStyles.gridBtn} title="App Launcher">
        <Icon d={ICONS.grid} size={16}/>
      </button>
      <div style={headerStyles.logoArea}>
        <OrgLensMark size={26}/>
        <div style={{display:'flex', alignItems:'baseline'}}>
          <span style={headerStyles.appName}>OrgLens</span>
          <span style={headerStyles.appSub}>Org Health Intelligence</span>
        </div>
      </div>
      <div style={headerStyles.divider}/>
      <div style={headerStyles.searchWrap}>
        <span style={headerStyles.searchIcon}><Icon d={ICONS.search} size={14}/></span>
        <input style={headerStyles.searchInput} placeholder="Search findings, components, or objects…"/>
      </div>
      <div style={headerStyles.navRight}>
        <button style={headerStyles.iconBtn} title="Notifications">
          <Icon d={ICONS.bell} size={18}/>
          <span style={headerStyles.badge}>5</span>
        </button>
        <button style={headerStyles.iconBtn} title="Help"><Icon d={ICONS.help} size={18}/></button>
        <button style={headerStyles.iconBtn} title="Setup"><Icon d={ICONS.settings} size={18}/></button>
        <div style={headerStyles.avatar}>JD</div>
      </div>
    </header>
  );
}

// ===================== LEFT NAV (Lightning rail) =====================
const sidebarStyles = {
  rail: {
    width: 56, background: '#16325c',
    display: 'flex', flexDirection: 'column',
    alignItems: 'center', paddingTop: 8, paddingBottom: 8,
    gap: 2, flexShrink: 0, height: '100%',
  },
  item: (active) => ({
    width: 48, height: 48, borderRadius: 4,
    display: 'flex', flexDirection: 'column',
    alignItems: 'center', justifyContent: 'center', gap: 2,
    cursor: 'pointer',
    background: active ? 'rgba(255,255,255,0.18)' : 'transparent',
    border: active ? '1px solid rgba(255,255,255,0.28)' : '1px solid transparent',
    color: active ? '#fff' : 'rgba(255,255,255,0.78)',
    position: 'relative', transition: 'background 150ms',
  }),
  label: { fontSize: 9, fontWeight: 700, textTransform: 'uppercase', letterSpacing: '0.04em', lineHeight: 1 },
  divider: { width: 32, height: 1, background: 'rgba(255,255,255,0.15)', margin: '6px 0' },
};

const RAIL_ITEMS = [
  { id: 'dashboard', label: 'Score',     icon: ICONS.dashboard },
  { id: 'backlog',   label: 'Backlog',   icon: ICONS.list },
  { id: 'automap',   label: 'Auto Map',  icon: ICONS.map },
  { id: 'ai',        label: 'AI Ready',  icon: ICONS.sparkle },
  { id: 'portfolio', label: 'Orgs',      icon: ICONS.building },
];

function LeftRail({ activeScreen, onNavigate }) {
  return (
    <nav style={sidebarStyles.rail}>
      {RAIL_ITEMS.map((item, i) => (
        <React.Fragment key={item.id}>
          {i === 5 && <div style={sidebarStyles.divider}/>}
          <div
            style={sidebarStyles.item(activeScreen === item.id)}
            onClick={() => onNavigate(item.id)}
            onMouseEnter={e => { if (activeScreen !== item.id) e.currentTarget.style.background = 'rgba(255,255,255,0.08)'; }}
            onMouseLeave={e => { if (activeScreen !== item.id) e.currentTarget.style.background = 'transparent'; }}
            title={item.label}
          >
            <Icon d={item.icon} size={18}/>
            <span style={sidebarStyles.label}>{item.label}</span>
          </div>
        </React.Fragment>
      ))}
    </nav>
  );
}

// ===================== APP CONTEXT BAR (object tab bar style) =====================
const contextStyles = {
  bar: {
    background: '#fff', borderBottom: '1px solid #dddbda',
    display: 'flex', alignItems: 'stretch',
    paddingLeft: 12, paddingRight: 12,
    flexShrink: 0, height: 44,
  },
  appBadge: {
    display: 'flex', alignItems: 'center', gap: 8,
    paddingRight: 16, marginRight: 8, borderRight: '1px solid #dddbda',
  },
  appBadgeIcon: {
    width: 28, height: 28, borderRadius: 4,
    background: 'linear-gradient(135deg, #0070d2 0%, #1b96ff 100%)',
    display: 'flex', alignItems: 'center', justifyContent: 'center',
    color: '#fff',
  },
  appBadgeText: { fontSize: 13, fontWeight: 700, color: '#080707' },
  tabs: { display: 'flex', alignItems: 'stretch', gap: 0, flex: 1 },
  tab: (active) => ({
    display: 'flex', alignItems: 'center', gap: 6,
    padding: '0 14px', fontSize: 13,
    fontWeight: active ? 700 : 400,
    color: active ? '#0070d2' : '#3e3e3c',
    cursor: 'pointer',
    borderBottom: active ? '3px solid #0070d2' : '3px solid transparent',
    marginBottom: -1,
    userSelect: 'none', transition: 'color 150ms',
    whiteSpace: 'nowrap',
  }),
  right: {
    display: 'flex', alignItems: 'center', gap: 8, marginLeft: 'auto',
    paddingLeft: 16, borderLeft: '1px solid #dddbda',
  },
};

function ContextBar({ activeScreen, onNavigate, activeOrgName, onOrgPick, openCount }) {
  const tabs = [
    { id: 'dashboard', label: 'OrgScore' },
    { id: 'backlog',   label: 'Fix Backlog' },
    { id: 'automap',   label: 'Automation Map' },
    { id: 'ai',        label: 'Agentforce Readiness' },
    { id: 'portfolio', label: 'Org Portfolio' },
  ];
  return (
    <div style={contextStyles.bar}>
      <div style={contextStyles.appBadge}>
        <div style={contextStyles.appBadgeIcon}>
          <Icon d={ICONS.dashboard} size={14}/>
        </div>
        <span style={contextStyles.appBadgeText}>OrgLens</span>
      </div>
      <div style={contextStyles.tabs}>
        {tabs.map(t => (
          <div key={t.id} style={contextStyles.tab(activeScreen === t.id)} onClick={() => onNavigate(t.id)}>
            {t.label}
            {t.id === 'backlog' && openCount > 0 && (
              <span style={{
                fontSize: 10, fontWeight: 700, color: '#fff', background: '#c23934',
                padding: '1px 6px', borderRadius: 999, lineHeight: 1.4,
              }}>{openCount}</span>
            )}
          </div>
        ))}
      </div>
      <div style={contextStyles.right}>
        <button onClick={onOrgPick} style={{
          height: 28, padding: '0 10px', border: '1px solid #dddbda',
          borderRadius: 4, background: '#fff', cursor: 'pointer',
          fontSize: 12, fontWeight: 600, color: '#3e3e3c',
          display: 'flex', alignItems: 'center', gap: 6, fontFamily: 'inherit',
        }}>
          <Icon d={ICONS.building} size={12} color="#706e6b"/>
          {activeOrgName}
          <Icon d={ICONS.chevD} size={12} color="#706e6b"/>
        </button>
      </div>
    </div>
  );
}

// ===================== PRIMITIVES (cards, buttons, badges) =====================
const Card = ({ children, style, ...rest }) => (
  <div style={{
    background: '#fff', border: '1px solid #dddbda', borderRadius: 4,
    boxShadow: '0 2px 2px 0 rgba(0,0,0,0.05)',
    ...style,
  }} {...rest}>{children}</div>
);

const CardHeader = ({ title, action, eyebrow, icon }) => (
  <div style={{
    padding: '12px 16px', borderBottom: '1px solid #dddbda',
    display: 'flex', alignItems: 'center', gap: 12,
  }}>
    {icon && (
      <div style={{
        width: 28, height: 28, borderRadius: 4, background: '#f3f2f2',
        display: 'flex', alignItems: 'center', justifyContent: 'center', color: '#3e3e3c',
      }}><Icon d={icon} size={16}/></div>
    )}
    <div style={{flex:1, minWidth: 0}}>
      {eyebrow && (
        <div style={{
          fontSize: 11, fontWeight: 700, color: '#706e6b',
          textTransform: 'uppercase', letterSpacing: '0.06em', lineHeight: 1.2,
        }}>{eyebrow}</div>
      )}
      <div style={{fontSize: 15, fontWeight: 700, color: '#080707', lineHeight: 1.3}}>{title}</div>
    </div>
    {action}
  </div>
);

const Btn = ({ variant = 'neutral', size = 'md', children, icon, style, ...rest }) => {
  const variants = {
    brand:   { bg: '#0070d2', color: '#fff', border: '1px solid #0070d2' },
    neutral: { bg: '#fff', color: '#0070d2', border: '1px solid #dddbda' },
    destructive: { bg: '#fff', color: '#c23934', border: '1px solid #dddbda' },
    text:    { bg: 'transparent', color: '#0070d2', border: '1px solid transparent' },
  };
  const v = variants[variant];
  const h = size === 'sm' ? 28 : 32;
  return (
    <button style={{
      height: h, padding: size === 'sm' ? '0 12px' : '0 16px',
      borderRadius: 4, fontSize: 13, fontWeight: 700, cursor: 'pointer', fontFamily: 'inherit',
      background: v.bg, color: v.color, border: v.border,
      display: 'inline-flex', alignItems: 'center', gap: 6,
      whiteSpace: 'nowrap', transition: 'background 150ms',
      ...style,
    }} {...rest}>
      {icon && <Icon d={icon} size={14}/>}
      {children}
    </button>
  );
};

// Priority pill (P1/P2/P3)
const PriorityPill = ({ priority }) => {
  const meta = window.PRIORITY_META[priority] || window.PRIORITY_META.P3;
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', height: 22,
      padding: '0 8px', borderRadius: 4,
      background: meta.bg, color: meta.color,
      fontSize: 11, fontWeight: 700, letterSpacing: '0.02em',
      border: `1px solid ${meta.color}`, lineHeight: 1,
    }}>{meta.label}</span>
  );
};

// Generic SLDS-style badge
const Badge = ({ children, variant = 'neutral', icon }) => {
  const map = {
    neutral: { bg: '#ecebea', color: '#3e3e3c' },
    success: { bg: '#f3fbf2', color: '#2e844a' },
    warning: { bg: '#fffaef', color: '#a96404' },
    error:   { bg: '#fce7e7', color: '#ba0517' },
    info:    { bg: '#eaf4ff', color: '#005fb2' },
  };
  const m = map[variant] || map.neutral;
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 4,
      height: 20, padding: '0 8px', borderRadius: 999,
      background: m.bg, color: m.color,
      fontSize: 11, fontWeight: 700, lineHeight: 1,
    }}>
      {icon && <Icon d={icon} size={10}/>}
      {children}
    </span>
  );
};

// Trend chip (up/down/flat)
const TrendChip = ({ trend, delta }) => {
  const cfg = trend === 'up' ? { color: '#2e844a', bg: '#f3fbf2', icon: ICONS.arrowUp }
            : trend === 'down' ? { color: '#ba0517', bg: '#fce7e7', icon: ICONS.arrowDown }
            : { color: '#706e6b', bg: '#f3f2f2', icon: ICONS.arrowFlat };
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 3,
      padding: '2px 6px', borderRadius: 4, background: cfg.bg, color: cfg.color,
      fontSize: 11, fontWeight: 700,
    }}>
      <Icon d={cfg.icon} size={10}/>
      {delta > 0 ? `+${delta}` : delta === 0 ? '0' : delta}
    </span>
  );
};

// Score ring — SVG donut
const ScoreRing = ({ score, size = 96, stroke = 9, label }) => {
  const r = (size - stroke) / 2;
  const c = 2 * Math.PI * r;
  const pct = Math.max(0, Math.min(100, score));
  const offset = c * (1 - pct / 100);
  const color = scoreColor(score);
  return (
    <div style={{position: 'relative', width: size, height: size, flexShrink: 0}}>
      <svg width={size} height={size} style={{transform: 'rotate(-90deg)'}}>
        <circle cx={size/2} cy={size/2} r={r} fill="none" stroke="#ecebea" strokeWidth={stroke}/>
        <circle cx={size/2} cy={size/2} r={r} fill="none" stroke={color} strokeWidth={stroke}
                strokeDasharray={c} strokeDashoffset={offset} strokeLinecap="round"
                style={{transition: 'stroke-dashoffset 600ms cubic-bezier(0.4,0,0.2,1)'}}/>
      </svg>
      <div style={{
        position: 'absolute', inset: 0,
        display: 'flex', flexDirection: 'column',
        alignItems: 'center', justifyContent: 'center',
      }}>
        <div style={{fontSize: size * 0.32, fontWeight: 700, color: '#080707', lineHeight: 1, letterSpacing: '-0.02em'}}>{Math.round(score)}</div>
        {label && <div style={{fontSize: 10, fontWeight: 700, color: '#706e6b', textTransform: 'uppercase', letterSpacing: '0.06em', marginTop: 2}}>{label}</div>}
      </div>
    </div>
  );
};

function scoreColor(score) {
  if (score >= 75) return '#2e844a';
  if (score >= 55) return '#dd7a01';
  return '#c23934';
}
function scoreBand(score) {
  if (score >= 75) return { label: 'Healthy', color: '#2e844a', bg: '#f3fbf2' };
  if (score >= 55) return { label: 'Needs attention', color: '#a96404', bg: '#fffaef' };
  if (score >= 40) return { label: 'At risk', color: '#ba0517', bg: '#fce7e7' };
  return { label: 'Critical', color: '#8e0c0c', bg: '#fce7e7' };
}

// Horizontal bar
const HBar = ({ value, max = 100, color, height = 6 }) => (
  <div style={{
    width: '100%', height, background: '#ecebea', borderRadius: 999, overflow: 'hidden',
  }}>
    <div style={{
      width: `${(value/max)*100}%`, height: '100%',
      background: color || scoreColor(value),
      borderRadius: 999, transition: 'width 400ms cubic-bezier(0.4,0,0.2,1)',
    }}/>
  </div>
);

// Toast
function Toast({ message, variant = 'success', onDismiss }) {
  const map = {
    success: { bg: '#f3fbf2', border: '#91db8b', color: '#2e844a', icon: ICONS.check },
    info:    { bg: '#eaf4ff', border: '#1b96ff', color: '#005fb2', icon: ICONS.info  },
    warning: { bg: '#fffaef', border: '#fcb95b', color: '#a96404', icon: ICONS.warn  },
  };
  const m = map[variant];
  return (
    <div style={{
      position: 'fixed', bottom: 24, right: 24, background: m.bg,
      border: `1px solid ${m.border}`, borderRadius: 4, padding: '12px 16px',
      boxShadow: '0 4px 12px rgba(0,0,0,0.15)', display: 'flex',
      alignItems: 'center', gap: 10, zIndex: 9000, fontSize: 13,
      fontWeight: 600, color: m.color, maxWidth: 420,
    }}>
      <Icon d={m.icon} size={16} color={m.color}/>
      <span style={{flex: 1}}>{message}</span>
      {onDismiss && (
        <button onClick={onDismiss} style={{
          background: 'none', border: 'none', cursor: 'pointer',
          color: m.color, padding: 2, display: 'flex',
        }}><Icon d={ICONS.close} size={14}/></button>
      )}
    </div>
  );
}

Object.assign(window, {
  Icon, ICONS, CloudMark, OrgLensMark,
  GlobalHeader, LeftRail, ContextBar,
  Card, CardHeader, Btn, PriorityPill, Badge, TrendChip,
  ScoreRing, scoreColor, scoreBand, HBar, Toast,
});
