// Brand system: derive a full color scale from a primary color in OKLCH
// so it works in light + dark mode.

// --- color utils ---------------------------------------------------------
function hexToRgb(hex) {
  const h = hex.replace('#', '');
  const v = h.length === 3
    ? h.split('').map(c => parseInt(c + c, 16))
    : [0, 2, 4].map(i => parseInt(h.slice(i, i + 2), 16));
  return { r: v[0] / 255, g: v[1] / 255, b: v[2] / 255 };
}
function rgbToOklch({ r, g, b }) {
  const lin = (x) => x <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
  const R = lin(r), G = lin(g), B = lin(b);
  const l = 0.4122214708*R + 0.5363325363*G + 0.0514459929*B;
  const m = 0.2119034982*R + 0.6806995451*G + 0.1073969566*B;
  const s = 0.0883024619*R + 0.2817188376*G + 0.6299787005*B;
  const l_ = Math.cbrt(l), m_ = Math.cbrt(m), s_ = Math.cbrt(s);
  const L = 0.2104542553*l_ + 0.7936177850*m_ - 0.0040720468*s_;
  const a = 1.9779984951*l_ - 2.4285922050*m_ + 0.4505937099*s_;
  const bb = 0.0259040371*l_ + 0.7827717662*m_ - 0.8086757660*s_;
  const C = Math.sqrt(a*a + bb*bb);
  let H = Math.atan2(bb, a) * 180 / Math.PI;
  if (H < 0) H += 360;
  return { L, C, H };
}
function hexToOklch(hex) { return rgbToOklch(hexToRgb(hex)); }

function oklchStr(L, C, H, alpha) {
  const a = alpha == null ? '' : ` / ${alpha}`;
  return `oklch(${L.toFixed(3)} ${C.toFixed(3)} ${H.toFixed(2)}${a})`;
}

// Build a full token set for one mode given a primary + secondary
// Pick legible text for a given primary. White is only safe on darker brand
// colours — on lime, peach or cyan it drops to ~1.5:1 and disappears.
function onColorFor(hex) {
  const lin = [1, 3, 5].map(i => parseInt(hex.slice(i, i + 2), 16) / 255)
    .map(c => (c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4)));
  const L = 0.2126 * lin[0] + 0.7152 * lin[1] + 0.0722 * lin[2];
  const whiteRatio = 1.05 / (L + 0.05);
  return whiteRatio >= 3.0 ? '#fff' : '#101010';
}

function buildTokens({ primary, secondary, dark }) {
  const p = hexToOklch(primary);
  const s = hexToOklch(secondary);

  if (dark) {
    return {
      // surfaces
      bg:        '#0E0E10',
      surface:   '#1A1A1D',
      surface2:  '#232327',
      surface3:  '#2C2C32',
      // text
      text:      '#F5F5F7',
      textMuted: 'rgba(235,235,245,0.6)',
      textDim:   'rgba(235,235,245,0.35)',
      // borders
      border:    'rgba(255,255,255,0.08)',
      // brand
      primary:        oklchStr(Math.min(0.78, Math.max(0.6, p.L + 0.05)), p.C, p.H),
      primarySoft:    oklchStr(0.30, Math.min(0.10, p.C * 0.6), p.H),
      primaryTint:    oklchStr(0.32, Math.min(0.08, p.C * 0.5), p.H),
      onPrimary:      onColorFor(primary),
      primaryRaw:     oklchStr(p.L, p.C, p.H),
      secondary:      oklchStr(Math.min(0.78, Math.max(0.6, s.L + 0.05)), s.C, s.H),
      secondarySoft:  oklchStr(0.30, Math.min(0.10, s.C * 0.6), s.H),
      // semantic (fixed across brands)
      protein:   '#F58A3C',
      carbs:     '#F5C13C',
      fat:       '#3C8BF5',
      success:   '#34C759',
    };
  }
  // light
  return {
    bg:        '#FFFFFF',
    surface:   '#F6F6F8',
    surface2:  '#FFFFFF',
    surface3:  '#EAEAEE',
    text:      '#0F0F12',
    textMuted: 'rgba(60,60,67,0.65)',
    textDim:   'rgba(60,60,67,0.40)',
    border:    'rgba(0,0,0,0.08)',
    primary:        oklchStr(Math.max(0.28, Math.min(0.88, p.L)), p.C, p.H),
    primarySoft:    oklchStr(0.93, Math.min(0.06, p.C * 0.4), p.H),
    primaryTint:    oklchStr(0.95, Math.min(0.04, p.C * 0.3), p.H),
    onPrimary:      onColorFor(primary),
    primaryRaw:     oklchStr(p.L, p.C, p.H),
    secondary:      oklchStr(Math.max(0.55, Math.min(0.7, s.L)), s.C, s.H),
    secondarySoft:  oklchStr(0.93, Math.min(0.06, s.C * 0.4), s.H),
    protein:   '#F58A3C',
    carbs:     '#F5C13C',
    fat:       '#3C8BF5',
    success:   '#34C759',
  };
}

// --- Coach presets -------------------------------------------------------
const COACH_PRESETS = [
  {
    id: 'nowfit',
    name: 'NOWFit',
    handle: 'nowfit.coach',
    coach: 'Camila Rivas',
    primary: '#F26B1F',     // signature orange
    secondary: '#1F4FF2',
    font: 'Inter',
    wordmark: 'NOWFit',
    avatar: '#F26B1F',
    habitEmoji: '🔥',
  },
  {
    id: 'kinetic',
    name: 'Kinetic',
    handle: 'kineticlab',
    coach: 'Sasha Wren',
    primary: '#7B5BFF',     // electric violet
    secondary: '#22D3A0',
    font: 'Manrope',
    wordmark: 'KINETIC',
    avatar: '#7B5BFF',
    habitEmoji: '🔥',
  },
  {
    id: 'verde',
    name: 'Verde Wellness',
    handle: 'verde.studio',
    coach: 'Lía Marín',
    primary: '#2E7D5B',     // forest green
    secondary: '#C58A3A',
    font: 'DM Sans',
    wordmark: 'verde',
    avatar: '#2E7D5B',
    habitEmoji: '🔥',
  },
  {
    id: 'iron',
    name: 'IronCo',
    handle: 'ironco.fit',
    coach: 'Marcus Reid',
    primary: '#D11A2A',     // bold red
    secondary: '#222222',
    font: 'Space Grotesk',
    wordmark: 'IRON / CO',
    avatar: '#D11A2A',
    habitEmoji: '🔥',
  },
];

window.onColorFor = onColorFor;
window.buildTokens = buildTokens;
window.COACH_PRESETS = COACH_PRESETS;
window.hexToOklch = hexToOklch;
