// tokens.jsx — Design tokens for Enginuity (corporate engineering consultancy)
// Two directions: corporate (clean sans, white) and heritage (serif, off-white).

const PALETTES = {
  steel:   { name: 'Steel',   hero: '#566b7d', soft: '#879aaa', deep: '#38454f' },
  navy:    { name: 'Navy',    hero: '#1f3b56', soft: '#3a5d80', deep: '#13243a' },
  slate:   { name: 'Slate',   hero: '#404a55', soft: '#5e6873', deep: '#262d34' },
  rust:    { name: 'Rust',    hero: '#b85420', soft: '#c97a4f', deep: '#7a3614' },
  forest:  { name: 'Forest',  hero: '#2d5a3f', soft: '#4a7d5e', deep: '#1a3a26' },
};

const TYPE_PRESETS = {
  corporate: {
    name: 'Corporate',
    display: '"Source Sans 3", -apple-system, BlinkMacSystemFont, sans-serif',
    displayWeight: 600,
    displayTracking: '-0.02em',
    body: '"Source Sans 3", -apple-system, BlinkMacSystemFont, sans-serif',
    eyebrow: '"Source Sans 3", sans-serif',
  },
  heritage: {
    name: 'Heritage',
    display: '"Source Serif 4", "Newsreader", Georgia, serif',
    displayWeight: 500,
    displayTracking: '-0.015em',
    body: '"Source Sans 3", -apple-system, BlinkMacSystemFont, sans-serif',
    eyebrow: '"Source Sans 3", sans-serif',
  },
  workhorse: {
    name: 'Workhorse',
    display: '"DM Sans", "Source Sans 3", sans-serif',
    displayWeight: 600,
    displayTracking: '-0.02em',
    body: '"DM Sans", "Source Sans 3", sans-serif',
    eyebrow: '"DM Sans", sans-serif',
  },
};

function buildTheme(t) {
  const pal = PALETTES[t.palette] || PALETTES.steel;
  const dark = !!t.darkMode;
  const dens = t.density || 'regular';
  const type = TYPE_PRESETS[t.typePreset] || TYPE_PRESETS.corporate;

  const densityScale = { compact: 0.86, regular: 1, comfy: 1.16 }[dens];

  // Light: clean white-ish corporate
  const light = {
    bg: '#ffffff',
    bg2: '#f7f4ed',  // warm cream surface
    bg3: '#ebe6d8',
    fg: '#15171a',
    fg2: '#3d434c',
    mid: '#6c727b',
    ash: '#9aa0a8',
    hair: 'rgba(21,23,26,0.10)',
    hairStrong: 'rgba(21,23,26,0.22)',
    accent: pal.hero,
    accentSoft: pal.soft,
    onAccent: '#ffffff',
    surface: '#fefcf6',
  };
  const darkVars = {
    bg: '#0f1114',
    bg2: '#181b20',
    bg3: '#22262c',
    fg: '#f4f1ea',
    fg2: '#c8c2b5',
    mid: '#8a8f97',
    ash: '#5b5f66',
    hair: 'rgba(244,241,234,0.10)',
    hairStrong: 'rgba(244,241,234,0.24)',
    accent: pal.soft,
    accentSoft: pal.hero,
    onAccent: '#0f1114',
    surface: '#161a20',
  };
  // Heritage variant tweaks the light bg to warm cream
  const heritageLight = {
    ...light,
    bg: '#f7f4ed',
    bg2: '#ffffff',
    bg3: '#ebe6d8',
    surface: '#ffffff',
  };

  let c;
  if (dark) c = darkVars;
  else if (t.direction === 'heritage') c = heritageLight;
  else c = light;

  return {
    dark,
    direction: t.direction,
    type,
    palette: pal,
    density: dens,
    densityScale,
    c,
    cssVars: {
      '--bg':           c.bg,
      '--bg2':          c.bg2,
      '--bg3':          c.bg3,
      '--fg':           c.fg,
      '--fg2':          c.fg2,
      '--mid':          c.mid,
      '--ash':          c.ash,
      '--hair':         c.hair,
      '--hair-s':       c.hairStrong,
      '--accent':       c.accent,
      '--accent-soft':  c.accentSoft,
      '--on-accent':    c.onAccent,
      '--surface':      c.surface,
      '--ff-display':   t.direction === 'heritage' ? TYPE_PRESETS.heritage.display : type.display,
      '--ff-body':      type.body,
      '--ff-eyebrow':   type.eyebrow,
      '--display-w':    t.direction === 'heritage' ? 500 : type.displayWeight,
      '--display-track': t.direction === 'heritage' ? '-0.015em' : type.displayTracking,
      '--dens':         densityScale,
      '--section-y':    `${dens === 'compact' ? 72 : dens === 'comfy' ? 144 : 104}px`,
    },
  };
}

Object.assign(window, { PALETTES, TYPE_PRESETS, buildTheme });
