// Helpers
const fmt = {
  num: (n) => {
    if (n == null || isNaN(n)) return '—';
    if (Math.abs(n) >= 1e12) return (n/1e12).toFixed(1)+'T';
    if (Math.abs(n) >= 1e9) return (n/1e9).toFixed(1)+'B';
    if (Math.abs(n) >= 1e6) return (n/1e6).toFixed(1)+'M';
    if (Math.abs(n) >= 1e3) return (n/1e3).toFixed(1)+'K';
    return Math.round(n).toLocaleString();
  },
  // Always renders in K units (1 decimal) for uniform display in visit tables
  k: (n) => {
    if (n == null || isNaN(n)) return '—';
    if (Math.abs(n) >= 1e9) return (n/1e9).toFixed(1)+'B';
    if (Math.abs(n) >= 1e6) return (n/1e6).toFixed(1)+'M';
    return (n/1e3).toFixed(1)+'K';
  },
  int: (n) => n == null ? '—' : Math.round(n).toLocaleString('en-US'),
  pct: (n, d=1) => n == null ? '—' : (n*100).toFixed(d)+'%',
  pctRaw: (n, d=1) => n == null ? '—' : n.toFixed(d)+'%',
  idr: (n) => {
    if (n == null || isNaN(n)) return '—';
    const sign = n < 0 ? '-' : '';
    const absN = Math.abs(n);
    if (absN >= 1e12) return sign + (absN/1e12).toFixed(1)+'T';
    if (absN >= 1e9)  return sign + (absN/1e9).toFixed(1)+'B';
    if (absN >= 1e6)  return sign + (absN/1e6).toFixed(1)+'M';
    if (absN >= 1e3)  return sign + (absN/1e3).toFixed(1)+'K';
    return sign + Math.round(absN).toLocaleString();
  },
};

// Color per segment (used for bars / swatches)
const SEG_COLORS = {
  // Green gradient
  "Power Players":   "#064e3b",
  "Dedicated Squad": "#047857",
  "Social Squad":    "#10b981",
  "Holiday Heroes":  "#34d399",
  // Yellow gradient
  "Casual":          "#b45309",
  "Drifters":        "#d97706",
  "Budget":          "#f59e0b",
  "Dormant":         "#fbbf24",
  // Red gradient
  "Heavy Hitters":   "#991b1b",
  "Prize Chasers":   "#ef4444",
  // Gray
  "Outlier":         "#9ca3af",
};

const SEG_PASTEL_COLORS = {
  // Green group (subtle emerald/green-50)
  "Power Players":   "#ecfdf5",
  "Dedicated Squad": "#ecfdf5",
  "Social Squad":    "#ecfdf5",
  "Holiday Heroes":  "#ecfdf5",
  // Yellow group (subtle amber/yellow-50)
  "Casual":          "#fffbeb",
  "Drifters":        "#fffbeb",
  "Budget":          "#fffbeb",
  "Dormant":         "#fffbeb",
  // Red group (subtle red-50)
  "Heavy Hitters":   "#fef2f2",
  "Prize Chasers":   "#fef2f2",
  // Gray
  "Outlier":         "#f9fafb",
};
const TIER_COLORS = {
  vip: "#e63329",
  loyal: "#1a2a5e",
  casual: "#5a76c7",
  light: "#9aa3bf",
  edge: "#f5d616",
};

window.fmt = fmt;
window.SEG_COLORS = SEG_COLORS;
window.SEG_PASTEL_COLORS = SEG_PASTEL_COLORS;
window.TIER_COLORS = TIER_COLORS;
