// ESI PLATFORM (sub-platform of Discovery) — shared tokens (cyan accent)
const Tesi = window.ArbiterTokens;

const esi = {
  // Accent — cyan / data-flow
  cyan: '#0891B2',
  cyanDeep: '#0E7490',
  cyanLight: '#06B6D4',
  cyanBg: 'rgba(8,145,178,0.06)',
  cyanBorder: 'rgba(8,145,178,0.22)',
  // Palette
  slate: '#475569',
  slateBg: 'rgba(71,85,105,0.06)',
  amber: '#B45309',
  amberBg: 'rgba(180,83,9,0.08)',
  orange: '#EA580C',
  orangeBg: 'rgba(234,88,12,0.08)',
  crimson: '#B91C1C',
  crimsonBg: 'rgba(185,28,28,0.06)',
  violet: '#7C3AED',
  violetBg: 'rgba(124,58,237,0.06)',
  emerald: '#059669',
  emeraldBg: 'rgba(5,150,105,0.08)',
  teal: '#0D9488',
  tealBg: 'rgba(13,148,136,0.06)',
  cobalt: '#1D4ED8',
  cobaltBg: 'rgba(29,78,216,0.06)',

  container: { flex: 1, overflow: 'auto', background: Tesi.color.bg.primary },
  header: {
    padding: '16px 24px', borderBottom: `1px solid ${Tesi.color.border.light}`,
    background: Tesi.color.bg.card, display: 'flex', alignItems: 'center', justifyContent: 'space-between',
  },
  headerTitle: { display: 'flex', alignItems: 'center', gap: '12px' },
  esiIcon: {
    width: '32px', height: '32px', borderRadius: '6px',
    background: 'linear-gradient(135deg, #0891B2 0%, #0E7490 100%)',
    display: 'flex', alignItems: 'center', justifyContent: 'center',
    fontSize: '14px', fontWeight: 700, color: '#fff',
  },
  title: { fontSize: '18px', fontWeight: 700, color: Tesi.color.text.primary, letterSpacing: '-0.02em' },
  subtitle: { fontSize: '12px', color: Tesi.color.text.tertiary, marginTop: '1px' },
  tabs: {
    display: 'flex', gap: '0', borderBottom: `1px solid ${Tesi.color.border.light}`,
    background: Tesi.color.bg.card, padding: '0 24px',
  },
  tab: {
    padding: '10px 16px', fontSize: '12px', fontWeight: 500,
    color: Tesi.color.text.tertiary, cursor: 'pointer', border: 'none', background: 'none',
    borderBottom: '2px solid transparent', fontFamily: Tesi.font.family,
    transition: 'all 0.15s', marginBottom: '-1px', whiteSpace: 'nowrap',
  },
  tabActive: { color: '#0891B2', borderBottom: '2px solid #0891B2', fontWeight: 600 },
  body: { padding: '20px 24px' },

  card: { background: Tesi.color.bg.card, border: `1px solid ${Tesi.color.border.light}`, borderRadius: Tesi.radius.lg, overflow: 'hidden', marginBottom: '16px' },
  cardH: { padding: '10px 16px', borderBottom: `1px solid ${Tesi.color.border.light}`, fontSize: '12px', fontWeight: 600, color: Tesi.color.text.primary, display: 'flex', alignItems: 'center', justifyContent: 'space-between' },

  stat: { display: 'flex', flexDirection: 'column', gap: '2px', padding: '12px 16px', background: Tesi.color.bg.secondary, borderRadius: '6px', border: `1px solid ${Tesi.color.border.light}` },
  statLabel: { fontSize: '10px', fontWeight: 600, color: Tesi.color.text.tertiary, textTransform: 'uppercase', letterSpacing: '0.08em' },
  statValue: { fontSize: '22px', fontWeight: 700, letterSpacing: '-0.02em', lineHeight: 1.1, color: Tesi.color.text.primary },
  statDelta: { fontSize: '10px', fontWeight: 500, marginTop: '2px' },

  tag: { display: 'inline-flex', alignItems: 'center', padding: '2px 8px', borderRadius: '10px', fontSize: '10px', fontWeight: 600 },
  pill: { display: 'inline-flex', alignItems: 'center', padding: '3px 10px', borderRadius: '12px', fontSize: '11px', fontWeight: 500, gap: '4px' },

  th: { fontSize: '10px', fontWeight: 600, color: Tesi.color.text.tertiary, textTransform: 'uppercase', letterSpacing: '0.08em', padding: '8px 12px', textAlign: 'left', background: Tesi.color.bg.secondary, borderBottom: `1px solid ${Tesi.color.border.light}` },
  td: { fontSize: '12px', color: Tesi.color.text.primary, padding: '10px 12px', borderBottom: `1px solid ${Tesi.color.border.light}`, verticalAlign: 'middle' },

  btnPrimary: { padding: '6px 14px', borderRadius: '6px', background: '#0891B2', border: 'none', color: '#fff', fontSize: '12px', fontWeight: 700, cursor: 'pointer', fontFamily: Tesi.font.family },
  btnSecondary: { padding: '5px 12px', borderRadius: '6px', border: `1px solid ${Tesi.color.border.light}`, background: Tesi.color.bg.card, fontSize: '11px', fontWeight: 500, color: Tesi.color.text.secondary, cursor: 'pointer', fontFamily: Tesi.font.family },
  btnGhost: { padding: '4px 10px', borderRadius: '5px', border: 'none', background: 'transparent', fontSize: '11px', fontWeight: 500, color: '#0891B2', cursor: 'pointer', fontFamily: Tesi.font.family },

  // Data-source type icon/color mapper
  sourceStyle: (type) => {
    const map = {
      'Email':         { icon: '▸', bg: 'rgba(29,78,216,0.08)', color: '#1D4ED8' },
      'Chat':          { icon: '▸', bg: 'rgba(124,58,237,0.08)', color: '#7C3AED' },
      'File Share':    { icon: '◇', bg: 'rgba(8,145,178,0.08)', color: '#0891B2' },
      'Cloud Storage': { icon: '◇', bg: 'rgba(8,145,178,0.08)', color: '#0891B2' },
      'Mobile':        { icon: '▪', bg: 'rgba(13,148,136,0.08)', color: '#0D9488' },
      'Laptop':        { icon: '▪', bg: 'rgba(71,85,105,0.08)', color: '#475569' },
      'ERP/Finance':   { icon: '₿', bg: 'rgba(5,150,105,0.08)', color: '#059669' },
      'Messaging':     { icon: '◆', bg: 'rgba(180,83,9,0.08)', color: '#B45309' },
      'DevOps':        { icon: 'cmd', bg: 'rgba(234,88,12,0.08)', color: '#EA580C' },
      'Archive':       { icon: '▣', bg: 'rgba(71,85,105,0.08)', color: '#475569' },
      'SaaS':          { icon: '*', bg: 'rgba(124,58,237,0.08)', color: '#7C3AED' },
    };
    return map[type] || { icon: '●', bg: Tesi.color.bg.secondary, color: Tesi.color.text.secondary };
  },

  // Status color
  statusColor: (s) => {
    const map = {
      'Active':           { bg: 'rgba(5,150,105,0.08)', color: '#059669' },
      'Complete':         { bg: 'rgba(5,150,105,0.08)', color: '#059669' },
      'Complete ok':       { bg: 'rgba(5,150,105,0.08)', color: '#059669' },
      'In Progress':      { bg: 'rgba(8,145,178,0.08)', color: '#0891B2' },
      'Processing':       { bg: 'rgba(8,145,178,0.08)', color: '#0891B2' },
      'Queued':           { bg: 'rgba(71,85,105,0.08)', color: '#475569' },
      'Paused':           { bg: 'rgba(180,83,9,0.08)', color: '#B45309' },
      'Failed':           { bg: 'rgba(185,28,28,0.08)', color: '#B91C1C' },
      'Preserved':        { bg: 'rgba(124,58,237,0.08)', color: '#7C3AED' },
      'Imaged':           { bg: 'rgba(13,148,136,0.08)', color: '#0D9488' },
      'Hosted':           { bg: 'rgba(8,145,178,0.08)', color: '#0891B2' },
      'Produced':         { bg: 'rgba(5,150,105,0.08)', color: '#059669' },
      'Indexed':          { bg: 'rgba(5,150,105,0.08)', color: '#059669' },
    };
    return map[s] || { bg: Tesi.color.bg.secondary, color: Tesi.color.text.secondary };
  },

  // Bytes/volume formatter
  bytes: (gb) => {
    if (gb == null) return '—';
    if (gb >= 1024) return `${(gb / 1024).toFixed(2)} TB`;
    if (gb >= 1) return `${gb.toFixed(1)} GB`;
    return `${(gb * 1024).toFixed(0)} MB`;
  },
  money: (n) => {
    if (n == null) return '—';
    const abs = Math.abs(n);
    if (abs >= 1_000_000) return `$${(n / 1_000_000).toFixed(2)}M`;
    if (abs >= 1_000) return `$${(n / 1_000).toFixed(0)}K`;
    return `$${n.toFixed(0)}`;
  },
  num: (n) => n == null ? '—' : n.toLocaleString(),
};

window.__esi = esi;
