// flow-shared.jsx — shared chrome for all Flow screens
// Sidebar, topbar, icons, badge helpers, status mapping.

const { useState, useEffect, useRef, useMemo, createContext, useContext, Fragment } = React;

// ---------- Icon set (lucide-style; inline SVG to avoid extra deps) ----------
const Icon = ({ d, size = 16, stroke = 2, className = '', style, fill = 'none' }) => (
  <svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} viewBox="0 0 24 24"
       fill={fill} stroke="currentColor" strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round"
       className={className} style={style}>
    {typeof d === 'string' ? <path d={d} /> : d}
  </svg>
);
const I = {
  // navigation
  layers:    (p) => <Icon {...p} d={<><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"/></>}/>,
  truck:     (p) => <Icon {...p} d={<><path d="M14 18V6a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2v11a1 1 0 0 0 1 1h2"/><path d="M15 18H9"/><path d="M19 18h2a1 1 0 0 0 1-1v-3.65a1 1 0 0 0-.22-.624l-3.48-4.35A1 1 0 0 0 17.52 8H14"/><circle cx="17" cy="18" r="2"/><circle cx="7" cy="18" r="2"/></>}/>,
  bookmark:  (p) => <Icon {...p} d="m19 21-7-4-7 4V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z"/>,
  warehouse: (p) => <Icon {...p} d={<><path d="M22 8.35V20a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V8.35A2 2 0 0 1 3.26 6.5l8-3.2a2 2 0 0 1 1.48 0l8 3.2A2 2 0 0 1 22 8.35Z"/><path d="M6 18h12"/><path d="M6 14h12"/><rect x="6" y="10" width="12" height="12"/></>}/>,
  stamp:     (p) => <Icon {...p} d={<><path d="M5 22h14"/><path d="M19.27 13.73A2.5 2.5 0 0 0 17.5 13h-11A2.5 2.5 0 0 0 4 15.5V17a1 1 0 0 0 1 1h14a1 1 0 0 0 1-1v-1.5c0-.66-.26-1.3-.73-1.77Z"/><path d="M14 13V8.5C14 7 15 7 15 5a3 3 0 0 0-3-3 3 3 0 0 0-3 3c0 2 1 2 1 3.5V13"/></>}/>,
  clock:     (p) => <Icon {...p} d={<><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></>}/>,
  shield:    (p) => <Icon {...p} d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/>,
  receipt:   (p) => <Icon {...p} d={<><path d="M4 2v20l2-1 2 1 2-1 2 1 2-1 2 1 2-1 2 1V2l-2 1-2-1-2 1-2-1-2 1-2-1-2 1Z"/><path d="M16 8h-6a2 2 0 1 0 0 4h4a2 2 0 1 1 0 4H8"/><path d="M12 17.5v-11"/></>}/>,
  chart:     (p) => <Icon {...p} d={<><line x1="18" x2="18" y1="20" y2="10"/><line x1="12" x2="12" y1="20" y2="4"/><line x1="6"  x2="6"  y1="20" y2="14"/><line x1="3"  x2="21" y1="20" y2="20"/></>}/>,
  home:      (p) => <Icon {...p} d={<><path d="m3 9 9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><polyline points="9 22 9 12 15 12 15 22"/></>}/>,
  // ui
  search:    (p) => <Icon {...p} d={<><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></>}/>,
  bell:      (p) => <Icon {...p} d={<><path d="M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9"/><path d="M10.3 21a1.94 1.94 0 0 0 3.4 0"/></>}/>,
  plus:      (p) => <Icon {...p} d={<><path d="M5 12h14"/><path d="M12 5v14"/></>}/>,
  chevDown:  (p) => <Icon {...p} d="m6 9 6 6 6-6"/>,
  chevRight: (p) => <Icon {...p} d="m9 18 6-6-6-6"/>,
  chevLeft:  (p) => <Icon {...p} d="m15 18-6-6 6-6"/>,
  chevUp:    (p) => <Icon {...p} d="m18 15-6-6-6 6"/>,
  filter:    (p) => <Icon {...p} d="M3 6h18l-7 9v6l-4-2v-4z"/>,
  download:  (p) => <Icon {...p} d={<><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" x2="12" y1="15" y2="3"/></>}/>,
  upload:    (p) => <Icon {...p} d={<><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" x2="12" y1="3" y2="15"/></>}/>,
  check:     (p) => <Icon {...p} d="M20 6 9 17l-5-5"/>,
  x:         (p) => <Icon {...p} d={<><path d="M18 6 6 18"/><path d="m6 6 12 12"/></>}/>,
  alert:     (p) => <Icon {...p} d={<><path d="m21.73 18-8-14a2 2 0 0 0-3.46 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z"/><path d="M12 9v4"/><path d="M12 17h.01"/></>}/>,
  info:      (p) => <Icon {...p} d={<><circle cx="12" cy="12" r="10"/><path d="M12 16v-4"/><path d="M12 8h.01"/></>}/>,
  pin:       (p) => <Icon {...p} d={<><path d="M20 10c0 4.993-5.539 10.193-7.399 11.799a1 1 0 0 1-1.202 0C9.539 20.193 4 14.993 4 10a8 8 0 0 1 16 0"/><circle cx="12" cy="10" r="3"/></>}/>,
  sliders:   (p) => <Icon {...p} d={<><line x1="4" x2="4" y1="21" y2="14"/><line x1="4" x2="4" y1="10" y2="3"/><line x1="12" x2="12" y1="21" y2="12"/><line x1="12" x2="12" y1="8" y2="3"/><line x1="20" x2="20" y1="21" y2="16"/><line x1="20" x2="20" y1="12" y2="3"/><line x1="2" x2="6" y1="14" y2="14"/><line x1="10" x2="14" y1="8" y2="8"/><line x1="18" x2="22" y1="16" y2="16"/></>}/>,
  arrowRight:(p) => <Icon {...p} d={<><path d="M5 12h14"/><path d="m12 5 7 7-7 7"/></>}/>,
  ext:       (p) => <Icon {...p} d={<><path d="M15 3h6v6"/><path d="M10 14 21 3"/><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/></>}/>,
  file:      (p) => <Icon {...p} d={<><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"/></>}/>,
  fileCheck: (p) => <Icon {...p} d={<><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"/><path d="m9 15 2 2 4-4"/></>}/>,
  fileX:     (p) => <Icon {...p} d={<><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="9" x2="15" y1="13" y2="19"/><line x1="15" x2="9" y1="13" y2="19"/></>}/>,
  fileBox:   (p) => <Icon {...p} d={<><path d="M14.5 22H18a2 2 0 0 0 2-2V7l-5-5H6a2 2 0 0 0-2 2v6"/><polyline points="14 2 14 8 20 8"/><circle cx="8" cy="16" r="6"/></>}/>,
  send:      (p) => <Icon {...p} d="m22 2-11 11M22 2l-7 20-4-9-9-4z"/>,
  more:      (p) => <Icon {...p} d={<><circle cx="12" cy="12" r="1"/><circle cx="19" cy="12" r="1"/><circle cx="5"  cy="12" r="1"/></>}/>,
  refresh:   (p) => <Icon {...p} d={<><path d="M21 12a9 9 0 1 1-3-6.7L21 8"/><path d="M21 3v5h-5"/></>}/>,
  edit:      (p) => <Icon {...p} d={<><path d="M12 20h9"/><path d="M16.5 3.5a2.121 2.121 0 1 1 3 3L7 19l-4 1 1-4z"/></>}/>,
  eye:       (p) => <Icon {...p} d={<><path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></>}/>,
  euro:      (p) => <Icon {...p} d={<><path d="M4 10h12"/><path d="M4 14h9"/><path d="M19 6a7.7 7.7 0 0 0-5.2-2A7.9 7.9 0 0 0 6 12c0 4.4 3.5 8 7.8 8 2 0 3.8-.8 5.2-2"/></>}/>,
  flag:      (p) => <Icon {...p} d={<><path d="M4 22V4"/><path d="M4 4s2-2 5-2 5 2 8 2 5-2 5-2v11s-2 2-5 2-5-2-8-2-5 2-5 2"/></>}/>,
  package:   (p) => <Icon {...p} d={<><path d="M16.5 9.4 7.55 4.24"/><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/><polyline points="3.27 6.96 12 12.01 20.73 6.96"/><line x1="12" x2="12" y1="22.08" y2="12"/></>}/>,
  user:      (p) => <Icon {...p} d={<><circle cx="12" cy="8" r="4"/><path d="M4 21v-1a6 6 0 0 1 6-6h4a6 6 0 0 1 6 6v1"/></>}/>,
  building:  (p) => <Icon {...p} d={<><rect x="4" y="2" width="16" height="20" rx="2"/><path d="M9 22v-4h6v4"/><path d="M8 6h.01"/><path d="M16 6h.01"/><path d="M12 6h.01"/><path d="M12 10h.01"/><path d="M12 14h.01"/><path d="M16 10h.01"/><path d="M16 14h.01"/><path d="M8 10h.01"/><path d="M8 14h.01"/></>}/>,
  hash:      (p) => <Icon {...p} d={<><line x1="4" x2="20" y1="9" y2="9"/><line x1="4" x2="20" y1="15" y2="15"/><line x1="10" x2="8" y1="3" y2="21"/><line x1="16" x2="14" y1="3" y2="21"/></>}/>,
  spark:     (p) => <Icon {...p} d="M9.937 15.5A2 2 0 0 0 8.5 14.063l-6.135-1.582a.5.5 0 0 1 0-.962L8.5 9.936A2 2 0 0 0 9.937 8.5l1.582-6.135a.5.5 0 0 1 .963 0L14.063 8.5A2 2 0 0 0 15.5 9.937l6.135 1.581a.5.5 0 0 1 0 .964L15.5 14.063a2 2 0 0 0-1.437 1.437l-1.582 6.135a.5.5 0 0 1-.963 0z"/>,
  lock:      (p) => <Icon {...p} d={<><rect width="18" height="11" x="3" y="11" rx="2" ry="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></>}/>,
  signature: (p) => <Icon {...p} d={<><path d="m21 17-2.156-1.868A.5.5 0 0 0 18 15.5v.5a.5.5 0 0 1-.5.5H16"/><path d="M19 22v-1c0-.5-.5-1-1-1h-2.764a2 2 0 0 1-1.789-1.106L11 13.5"/><path d="M3.69 8.22A2 2 0 1 1 6 5.5C8 7 8 12 8 12s5-1 7-3"/><path d="M5 22a4 4 0 0 1-1.5-3.6c0-1.5.5-2.4 1.5-3.4 2-2 4.5-3 6-3"/></>}/>,
  ship:      (p) => <Icon {...p} d="M2 21c.6.5 1.2 1 2.5 1 2.5 0 2.5-2 5-2 1.3 0 1.9.5 2.5 1s1.2 1 2.5 1c2.5 0 2.5-2 5-2 1.3 0 1.9.5 2.5 1M19.38 20A11.6 11.6 0 0 0 21 14l-9-4-9 4c0 2.9.94 5.34 2.81 7.76M19 13V7a2 2 0 0 0-2-2H7a2 2 0 0 0-2 2v6M12 10v4M2 5l1-2h18l1 2"/>,
  zap:       (p) => <Icon {...p} d="M4 14a1 1 0 0 1-.78-1.63l9.9-10.2a.5.5 0 0 1 .86.46l-1.92 6.02A1 1 0 0 0 13 10h7a1 1 0 0 1 .78 1.63l-9.9 10.2a.5.5 0 0 1-.86-.46l1.92-6.02A1 1 0 0 0 11 14z"/>,
  link:      (p) => <Icon {...p} d={<><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/></>}/>,
  thermo:    (p) => <Icon {...p} d="M14 4v10.54a4 4 0 1 1-4 0V4a2 2 0 0 1 4 0Z"/>,
  globe:     (p) => <Icon {...p} d={<><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></>}/>,
  trend:     (p) => <Icon {...p} d={<><polyline points="22 7 13.5 15.5 8.5 10.5 2 17"/><polyline points="16 7 22 7 22 13"/></>}/>,
};

// ---------- Status helpers ----------
const STATUS_BADGE = {
  draft:             { variant: 'neutral', label: 'Draft' },
  requested:         { variant: 'neutral', label: 'Requested' },
  planned:           { variant: 'primary', label: 'Planned' },
  ready_to_dispatch: { variant: 'primary', label: 'Ready to dispatch' },
  dispatched:        { variant: 'primary', label: 'Dispatched' },
  in_transit:        { variant: 'primary', label: 'In transit' },
  at_destination:    { variant: 'primary', label: 'At destination' },
  delivered:         { variant: 'success', label: 'Delivered' },
  reconciled:        { variant: 'success', label: 'Reconciled' },
  delayed:           { variant: 'warning', label: 'Delayed' },
  blocked:           { variant: 'danger',  label: 'Blocked' },
  exception:         { variant: 'danger',  label: 'Exception' },
};

const StatusBadge = ({ status, dot = true }) => {
  const cfg = STATUS_BADGE[status] || { variant: 'neutral', label: status };
  return (
    <span className={`badge badge-${cfg.variant}`}>
      {dot && <span className="dot" />}
      {cfg.label}
    </span>
  );
};

// ---------- Sidebar nav ----------
const SidebarNav = ({ activeRoute, onNav }) => {
  const isActive = (r) => activeRoute === r || activeRoute?.startsWith(r + '/');

  const Item = ({ id, icon, label, count }) => (
    <div className={`sidebar-item ${isActive(id) ? 'active' : ''}`} onClick={() => onNav?.(id)}>
      {icon}
      <span className="flex-1 ellipsis">{label}</span>
      {count != null && <span className="text-xs text-muted num">{count}</span>}
    </div>
  );
  const Sub = ({ id, label, count, badge }) => (
    <div className={`sidebar-sub ${isActive(id) ? 'active' : ''}`} onClick={() => onNav?.(id)}>
      <span className="flex-1 ellipsis">{label}</span>
      {count != null && <span className="text-xs text-muted num" style={{ marginRight: badge ? 6 : 0 }}>{count}</span>}
      {badge}
    </div>
  );

  return (
    <aside className="sidebar">
      <div className="sidebar-header">
        <ModuleSwitcher />
      </div>

      <div className="col" style={{ paddingBottom: 12 }}>
        <Item id="dashboard"  icon={<I.home size={16} />}      label="Dashboard" />

        <div className="sidebar-section-title">Operate</div>
        <Item id="shipments"  icon={<I.truck size={16} />}     label="Shipments"  count={184} />
        <Sub  id="shipments/list" label="List" />
        <Sub  id="shipments/map"  label="Map" />
        <Item id="bookings"   icon={<I.bookmark size={16} />}  label="Bookings"   count={47} />
        <Item id="customs"    icon={<I.stamp size={16} />}     label="Customs"
              badge={<span className="badge badge-danger" style={{ height: 18, padding: '0 6px', fontSize: 10 }}>2</span>} />
        <Item id="freetime"   icon={<I.clock size={16} />}     label="Free-time clocks"
              badge={<span className="badge badge-warning" style={{ height: 18, padding: '0 6px', fontSize: 10 }}>5</span>} />
        <Item id="claims"     icon={<I.shield size={16} />}    label="Claims"     count={3} />
        <Item id="recon"      icon={<I.receipt size={16} />}   label="Reconciliation" count={12} />

        <div className="sidebar-section-title">Network</div>
        <Item id="fleet/carriers" icon={<I.building size={16} />} label="Carriers" />
        <Item id="fleet/vehicles" icon={<I.truck size={16} />}    label="Vehicles" />
        <Item id="fleet/drivers"  icon={<I.user size={16} />}     label="Drivers" />

        <div className="sidebar-section-title">Insights</div>
        <Item id="reports" icon={<I.chart size={16} />} label="Reports" />
      </div>

      <div className="flex-1" />
      <div style={{ padding: 12, borderTop: '1px solid var(--border)' }}>
        <div className="row gap-2" style={{ alignItems: 'center', padding: '4px 8px' }}>
          <div style={{ width: 28, height: 28, borderRadius: 999, background: 'linear-gradient(135deg, hsl(220 70% 60%), hsl(280 60% 60%))', color: 'white', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 11, fontWeight: 600 }}>EÖ</div>
          <div className="flex-1 col" style={{ minWidth: 0 }}>
            <div className="text-sm fw-500 ellipsis">Elif Özay</div>
            <div className="text-xs text-muted ellipsis">Cambro Özay BG</div>
          </div>
          <I.chevDown size={14} className="text-muted" />
        </div>
      </div>
    </aside>
  );
};

const ModuleSwitcher = () => (
  <button className="row gap-2" style={{
    width: '100%', padding: '8px 10px', borderRadius: 10, border: '1px solid var(--border)',
    background: 'var(--surface-solid)', cursor: 'pointer', alignItems: 'center', font: 'inherit',
  }}>
    <div style={{
      width: 26, height: 26, borderRadius: 7,
      background: 'linear-gradient(135deg, hsl(220 70% 35%), hsl(200 80% 45%))',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      color: 'white', fontWeight: 700, fontSize: 13,
    }}>F</div>
    <div className="col flex-1" style={{ alignItems: 'flex-start', minWidth: 0 }}>
      <div className="text-xs text-muted" style={{ lineHeight: 1.1 }}>Orcavera</div>
      <div className="text-sm fw-600" style={{ lineHeight: 1.2 }}>Flow</div>
    </div>
    <I.chevDown size={14} className="text-muted" />
  </button>
);

// ---------- Topbar ----------
const Topbar = ({ crumbs = [], actions, onSearchFocus }) => (
  <div className="topbar">
    <div className="row gap-2" style={{ alignItems: 'center' }}>
      {crumbs.map((c, i) => (
        <Fragment key={i}>
          {i > 0 && <I.chevRight size={14} className="text-muted" />}
          <span className="crumb">{i === crumbs.length - 1 ? <b>{c}</b> : c}</span>
        </Fragment>
      ))}
    </div>
    <div className="flex-1" />
    <div className="row gap-3" style={{ alignItems: 'center' }}>
      <div className="row gap-2" style={{
        height: 34, padding: '0 10px', borderRadius: 8, border: '1px solid var(--border)',
        background: 'var(--surface-solid)', alignItems: 'center', minWidth: 280,
        color: 'var(--muted)', cursor: 'text',
      }} onClick={onSearchFocus}>
        <I.search size={15} />
        <span className="text-sm flex-1">Search shipments, MRN, container…</span>
        <kbd>⌘K</kbd>
      </div>
      <button className="btn btn-ghost btn-icon" title="Notifications">
        <div style={{ position: 'relative' }}>
          <I.bell size={16} />
          <span style={{ position: 'absolute', top: -3, right: -3, width: 8, height: 8, borderRadius: 999, background: 'var(--danger)', border: '1.5px solid white' }} />
        </div>
      </button>
      {actions}
    </div>
  </div>
);

// ---------- AppShell ----------
const AppShell = ({ activeRoute, onNav, crumbs, topbarActions, children, screenLabel }) => (
  <div className="ab-shell" data-screen-label={screenLabel}>
    <div className="shell" style={{ flex: 1, minHeight: 0 }}>
      <SidebarNav activeRoute={activeRoute} onNav={onNav} />
      <div className="col" style={{ minWidth: 0, height: '100%' }}>
        <Topbar crumbs={crumbs} actions={topbarActions} />
        <div className="ab-body">{children}</div>
      </div>
    </div>
  </div>
);

// ---------- Reusable bits ----------
const SectionHeader = ({ title, subtitle, right }) => (
  <div className="row gap-4" style={{ alignItems: 'flex-end', justifyContent: 'space-between', marginBottom: 16 }}>
    <div className="col gap-1">
      <div className="text-2xl fw-600" style={{ letterSpacing: '-0.02em' }}>{title}</div>
      {subtitle && <div className="text-sm text-muted">{subtitle}</div>}
    </div>
    {right}
  </div>
);

const Card = ({ children, style, className = '', as: As = 'div' }) => (
  <As className={`glass-card ${className}`} style={style}>{children}</As>
);

const KV = ({ label, children, mono }) => (
  <div className="col gap-1" style={{ minWidth: 0 }}>
    <div className="text-xs text-muted" style={{ textTransform: 'uppercase', letterSpacing: '0.04em', fontWeight: 600, fontSize: 10.5 }}>{label}</div>
    <div className={`text-sm ${mono ? 'mono' : ''}`} style={{ color: 'var(--ink)' }}>{children}</div>
  </div>
);

// Free-time bar widget — shared across Free-time tab + dashboard
const FreeTimeBar = ({ usedHours, freeHours, height = 10 }) => {
  const pct = Math.min(100, Math.max(0, (usedHours / freeHours) * 100));
  const cls = pct >= 90 ? 'crit' : pct >= 70 ? 'warn' : 'ok';
  return (
    <div className="ft-bar" style={{ height }}>
      <div className={`ft-fill ${cls}`} style={{ width: pct + '%' }} />
      <div className="ft-tick" style={{ left: '70%' }} />
      <div className="ft-tick" style={{ left: '90%' }} />
    </div>
  );
};

// MRN stepper
const MRNStepper = ({ steps, currentIdx, failed }) => (
  <div className="stepper" style={{ width: '100%' }}>
    {steps.map((s, i) => (
      <Fragment key={i}>
        {i > 0 && <div className={`stepper-conn ${i <= currentIdx ? 'done' : ''}`} />}
        <div className={`stepper-step ${i < currentIdx ? 'done' : i === currentIdx ? (failed ? 'failed' : 'current') : ''}`}>
          <div className="stepper-bub">
            {i < currentIdx ? <I.check size={16} /> : i === currentIdx && failed ? <I.x size={16} /> : (i + 1)}
          </div>
          <div className="text-xs fw-500" style={{ textAlign: 'center' }}>{s.label}</div>
          <div className="text-xs text-muted mono" style={{ textAlign: 'center', fontSize: 10.5 }}>{s.ts || '—'}</div>
        </div>
      </Fragment>
    ))}
  </div>
);

// Tiny sparkline (SVG) for KPI cards
const Sparkline = ({ data, w = 80, h = 24, color = 'var(--primary)' }) => {
  const max = Math.max(...data, 1), min = Math.min(...data, 0);
  const dx = w / (data.length - 1);
  const scale = (v) => h - ((v - min) / (max - min || 1)) * h;
  const path = data.map((v, i) => `${i === 0 ? 'M' : 'L'} ${i * dx} ${scale(v)}`).join(' ');
  const fill = `${path} L ${w} ${h} L 0 ${h} Z`;
  return (
    <svg width={w} height={h} viewBox={`0 0 ${w} ${h}`} style={{ display: 'block', overflow: 'visible' }}>
      <path d={fill} fill={color} fillOpacity="0.12" />
      <path d={path} stroke={color} strokeWidth="1.5" fill="none" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  );
};

// Country flag pill (3-letter)
const CountryFlag = ({ code }) => (
  <span className="badge badge-outline" style={{ height: 20, fontSize: 10.5, fontWeight: 600, padding: '0 6px', letterSpacing: '0.04em' }}>{code}</span>
);

// expose
Object.assign(window, {
  I, Icon, StatusBadge, AppShell, SidebarNav, Topbar, ModuleSwitcher,
  SectionHeader, Card, KV, FreeTimeBar, MRNStepper, Sparkline, CountryFlag,
  Fragment, useState, useEffect, useRef, useMemo,
});
