/* =====================================================================
   Tweaks app — drives the 3 comparable design directions + refinements.
   Renders only the Tweaks panel; it writes CSS variables / data-attributes
   onto <html>, so the otherwise-vanilla landing page reacts live.
   ===================================================================== */

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "direction": "marfil",
  "headFont": "auto",
  "accent": ["#B5934E", "#927539", "#D8C28A"],
  "heroShade": 8,
  "ctaText": "Take the Quiz"
}/*EDITMODE-END*/;

const FONTS = {
  cormorant: '"Cormorant Garamond", Georgia, serif',
  fraunces:  '"Fraunces", Georgia, serif',
  playfair:  '"Playfair Display", Georgia, serif',
  ebgaramond:'"EB Garamond", Georgia, serif'
};

function applyTweaks(t) {
  const el = document.documentElement;
  el.dataset.direction = t.direction || 'marfil';

  if (t.headFont && t.headFont !== 'auto') el.style.setProperty('--font-head', FONTS[t.headFont]);
  else el.style.removeProperty('--font-head');

  if (Array.isArray(t.accent)) {
    el.style.setProperty('--gold',   t.accent[0]);
    el.style.setProperty('--gold-d', t.accent[1]);
    el.style.setProperty('--gold-l', t.accent[2]);
  }

  el.style.setProperty('--hero-shade', ((t.heroShade || 0) / 100).toFixed(3));

  const label = (t.ctaText || '').trim();
  document.querySelectorAll('.js-cta-label').forEach(n => { if (label) n.textContent = label; });
}

function App() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);

  React.useEffect(() => { applyTweaks(t); }, [t]);

  return (
    <TweaksPanel title="Tweaks">
      <TweakSection label="Dirección de diseño" />
      <TweakRadio
        label="Estilo"
        value={t.direction}
        options={[
          { value: 'marfil',   label: 'Marfil' },
          { value: 'bosque',   label: 'Bosque' },
          { value: 'hacienda', label: 'Hacienda' }
        ]}
        onChange={(v) => setTweak('direction', v)}
      />

      <TweakSection label="Refinamientos" />
      <TweakSelect
        label="Tipografía de titulares"
        value={t.headFont}
        options={[
          { value: 'auto',       label: 'Auto (según dirección)' },
          { value: 'cormorant',  label: 'Cormorant Garamond' },
          { value: 'fraunces',   label: 'Fraunces' },
          { value: 'playfair',   label: 'Playfair Display' },
          { value: 'ebgaramond', label: 'EB Garamond' }
        ]}
        onChange={(v) => setTweak('headFont', v)}
      />
      <TweakColor
        label="Acento dorado"
        value={t.accent}
        options={[
          ['#B5934E', '#927539', '#D8C28A'],
          ['#C6A961', '#9C8038', '#E3CF95'],
          ['#BE8B40', '#9C6E2C', '#DFB877'],
          ['#94864A', '#6F6534', '#C5BA84']
        ]}
        onChange={(v) => setTweak('accent', v)}
      />
      <TweakSlider
        label="Sombra del hero"
        value={t.heroShade}
        min={0} max={50} step={1} unit="%"
        onChange={(v) => setTweak('heroShade', v)}
      />
      <TweakText
        label="Texto del botón principal"
        value={t.ctaText}
        onChange={(v) => setTweak('ctaText', v)}
      />
    </TweaksPanel>
  );
}

// Apply persisted/default tweaks immediately (before React mounts) so there's no flash.
applyTweaks(TWEAK_DEFAULTS);

ReactDOM.createRoot(document.getElementById('tweaks-root')).render(<App />);
