// 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
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:      '#fff',
      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:        '#F4EEE6',
    surface:   '#FFFFFF',
    surface2:  '#F8F4EE',
    surface3:  '#EFE8DD',
    text:      '#0F0F12',
    textMuted: 'rgba(60,60,67,0.65)',
    textDim:   'rgba(60,60,67,0.40)',
    border:    'rgba(0,0,0,0.06)',
    primary:        oklchStr(Math.max(0.55, Math.min(0.7, 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:      '#fff',
    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 -------------------------------------------------------
// White-label locked to Inspire (Mayra Villarreal). Single brand only.
const COACH_PRESETS = [
  {
    id: 'inspire',
    name: 'Inspire',
    handle: 'mayravillarreal',
    coach: 'Mayra Villarreal',
    primary: '#810100',     // signature cherry red
    secondary: '#C58A7A',   // warm dusty rose complement
    font: 'Manrope',
    wordmark: 'Inspire',
    avatar: '#810100',
    habitEmoji: '🌸',
  },
];

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