// BILLING & ANALYTICS PLATFORM — shared tokens (emerald accent)
const Tb = window.ArbiterTokens;

const bl = {
  // Accent palette — emerald (finance/money)
  emerald: '#059669',
  emeraldDeep: '#047857',
  emeraldLight: '#10B981',
  emeraldBg: 'rgba(5,150,105,0.06)',
  emeraldBorder: 'rgba(5,150,105,0.22)',
  teal: '#0D9488',
  tealBg: 'rgba(13,148,136,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)',
  slate: '#475569',
  slateBg: 'rgba(71,85,105,0.06)',

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

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

  stat: { display: 'flex', flexDirection: 'column', gap: '2px', padding: '12px 16px', background: Tb.color.bg.secondary, borderRadius: '6px', border: `1px solid ${Tb.color.border.light}` },
  statLabel: { fontSize: '10px', fontWeight: 600, color: Tb.color.text.tertiary, textTransform: 'uppercase', letterSpacing: '0.08em' },
  statValue: { fontSize: '22px', fontWeight: 700, letterSpacing: '-0.02em', lineHeight: 1.1, color: Tb.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: Tb.color.text.tertiary, textTransform: 'uppercase', letterSpacing: '0.08em', padding: '8px 12px', textAlign: 'left', background: Tb.color.bg.secondary, borderBottom: `1px solid ${Tb.color.border.light}` },
  td: { fontSize: '12px', color: Tb.color.text.primary, padding: '10px 12px', borderBottom: `1px solid ${Tb.color.border.light}`, verticalAlign: 'middle' },

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

  // Aging color helper — returns { bg, color, label }
  agingColor: (days) => {
    if (days <= 30) return { bg: 'rgba(5,150,105,0.08)', color: '#059669', label: '0-30' };
    if (days <= 60) return { bg: 'rgba(180,83,9,0.08)', color: '#B45309', label: '31-60' };
    if (days <= 90) return { bg: 'rgba(234,88,12,0.08)', color: '#EA580C', label: '61-90' };
    return { bg: 'rgba(185,28,28,0.08)', color: '#B91C1C', label: '91+' };
  },
  // Realization signal color
  realColor: (pct) => {
    if (pct >= 92) return '#059669';
    if (pct >= 85) return '#B45309';
    return '#B91C1C';
  },
  // Money formatter — compact "$1.2M / $482K" style
  money: (n, compact = true) => {
    if (n == null) return '—';
    const abs = Math.abs(n);
    const sign = n < 0 ? '-' : '';
    if (compact) {
      if (abs >= 1_000_000) return `${sign}$${(abs / 1_000_000).toFixed(2)}M`;
      if (abs >= 1_000) return `${sign}$${(abs / 1_000).toFixed(0)}K`;
      return `${sign}$${abs.toFixed(0)}`;
    }
    return `${sign}$${abs.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
  },
};

window.__bl = bl;
