// Forefront / Shared components for the marketing site.
// Editorial monochrome. Square, ruled, no shadow. Adapted from the DS marketing kit.

const INK = '#0a0a0a';
const PAPER = '#fafaf7';
const SOFT = '#e8e6df';
const MUTE = '#6b6b66';
const HAIR = 'rgba(10,10,10,0.12)';
const HAIR_REV = 'rgba(250,250,247,0.22)';
const EASE = 'cubic-bezier(0.2, 0.6, 0.2, 1)';

/* ---------------- Section shell ---------------- */
// Padding is driven by --sec-pad so the density tweak can flex the whole site.
const Section = ({ id, children, dark = false, soft = false, label }) => (
  <section
    id={id}
    data-screen-label={label}
    style={{
      padding: 'var(--sec-pad, 100px) 0',
      borderBottom: `1px solid ${INK}`,
      background: dark ? INK : soft ? SOFT : PAPER,
      color: dark ? PAPER : INK,
    }}
  >
    <div className="ff-container" style={{ maxWidth: 1280, margin: '0 auto', padding: '0 60px' }}>{children}</div>
  </section>
);

const SectionHead = ({ number, headline, accent, headlineTail = '.', intro, dark = false }) => (
  <div className="ff-sectionhead" style={shStyles.head}>
    <div style={{ ...shStyles.num, color: dark ? HAIR_REV_SOLID(dark) : MUTE }}>§ {number}</div>
    <div>
      <div style={{ ...shStyles.title, color: dark ? PAPER : INK }}>
        {headline}{' '}
        {accent && <em style={{ fontStyle: 'italic', fontWeight: 400 }}>{accent}</em>}{headlineTail}
      </div>
      {intro && <p style={{ ...shStyles.intro, color: dark ? 'rgba(250,250,247,0.6)' : MUTE }}>{intro}</p>}
    </div>
  </div>
);
const HAIR_REV_SOLID = () => 'rgba(250,250,247,0.6)';

const shStyles = {
  head: { display: 'grid', gridTemplateColumns: '200px 1fr', gap: 60, marginBottom: 60, alignItems: 'baseline' },
  num: { fontFamily: 'var(--font-mono)', fontSize: 11, letterSpacing: '0.2em', textTransform: 'uppercase' },
  title: { fontFamily: 'var(--font-display)', fontWeight: 300, fontSize: 'clamp(40px, 5vw, 64px)', letterSpacing: '-0.03em', lineHeight: 1, fontVariationSettings: '"opsz" 144', textWrap: 'balance' },
  intro: { fontFamily: 'var(--font-body)', fontSize: 18, maxWidth: 620, marginTop: 24, fontWeight: 300, lineHeight: 1.6 },
};

/* ---------------- Button ---------------- */
const Button = ({ children, variant = 'primary', size = 'md', reverse = false, onClick, href }) => {
  const [hover, setHover] = React.useState(false);
  const base = {
    fontFamily: 'var(--font-mono)',
    fontSize: size === 'lg' ? 12 : size === 'sm' ? 10 : 11,
    letterSpacing: '0.2em', textTransform: 'uppercase',
    padding: size === 'lg' ? '18px 30px' : size === 'sm' ? '10px 18px' : '14px 24px',
    border: '1px solid currentColor', cursor: 'pointer', textDecoration: 'none',
    display: 'inline-block', whiteSpace: 'nowrap',
    transition: `background ${'240ms'} ${EASE}, color 240ms ${EASE}`,
    background: 'transparent', color: reverse ? PAPER : INK,
  };
  const variants = {
    primary: reverse
      ? { ...base, background: PAPER, color: INK, borderColor: PAPER }
      : { ...base, background: INK, color: PAPER, borderColor: INK },
    secondary: { ...base },
  };
  const hoverStyle = (() => {
    if (!hover) return null;
    if (variant === 'primary') return reverse ? { background: 'transparent', color: PAPER } : { background: 'transparent', color: INK };
    return reverse ? { background: PAPER, color: INK } : { background: INK, color: PAPER };
  })();
  const Tag = href ? 'a' : 'button';
  return (
    <Tag href={href} onClick={onClick}
      onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}
      style={{ ...variants[variant], ...(hoverStyle || {}) }}>
      {children}
    </Tag>
  );
};

/* ---------------- Nav ---------------- */
const Nav = ({ route, onNavigate }) => {
  const links = [
    { id: 'home', label: 'Index' },
    { id: 'services', label: 'Services' },
    { id: 'how', label: 'How we work' },
    { id: 'contact', label: 'Contact' },
  ];
  return (
    <nav style={navStyles.nav}>
      <a href="#" onClick={(e) => { e.preventDefault(); onNavigate('home'); }} style={navStyles.brand}>
        Fore<em style={{ fontStyle: 'italic', fontWeight: 400 }}>front</em>
      </a>
      <div style={navStyles.right}>
        <ul style={navStyles.ul}>
          {links.map(l => (
            <li key={l.id}>
              <a href={`#${l.id}`}
                onClick={(e) => { e.preventDefault(); onNavigate(l.id); }}
                style={{ ...navStyles.link, color: route === l.id ? INK : MUTE }}
                onMouseEnter={(e) => e.currentTarget.style.color = INK}
                onMouseLeave={(e) => e.currentTarget.style.color = route === l.id ? INK : MUTE}>
                {l.label}
              </a>
            </li>
          ))}
        </ul>
        <Button size="sm" variant="primary" onClick={() => onNavigate('contact')}>Get in touch →</Button>
      </div>
    </nav>
  );
};
const navStyles = {
  nav: { position: 'sticky', top: 0, background: PAPER, borderBottom: `1px solid ${INK}`, padding: '16px 60px', display: 'flex', justifyContent: 'space-between', alignItems: 'center', zIndex: 100, fontFamily: 'var(--font-mono)', fontSize: 11, letterSpacing: '0.15em', textTransform: 'uppercase' },
  brand: { fontFamily: 'var(--font-display)', fontWeight: 300, fontSize: 24, letterSpacing: '-0.02em', textTransform: 'none', color: INK, textDecoration: 'none', fontVariationSettings: '"opsz" 144' },
  right: { display: 'flex', alignItems: 'center', gap: 36 },
  ul: { listStyle: 'none', display: 'flex', gap: 30, margin: 0, padding: 0 },
  link: { textDecoration: 'none', transition: `color 240ms ${EASE}` },
};

/* ---------------- Cover hero ---------------- */
const Cover = ({ eyebrow, number, headline, accent, headlineTail = '.', caption, meta = [], showStats = true, children }) => (
  <header className="ff-cover" style={coverStyles.cover}>
    <div style={coverStyles.top}>
      <div>{eyebrow}</div>
      <div>{number}</div>
    </div>
    <div style={coverStyles.middle}>
      <h1 style={coverStyles.headline}>
        {headline}{' '}
        {accent && <em style={{ fontStyle: 'italic', fontWeight: 400 }}>{accent}</em>}{headlineTail}
      </h1>
      {children}
    </div>
    <div className="ff-cover-bottom" style={coverStyles.bottom}>
      <div style={coverStyles.caption}>{caption}</div>
      {showStats && meta.map((m, i) => (
        <div key={i} style={coverStyles.col}>
          <span style={coverStyles.label}>{m.k}</span>
          <span style={coverStyles.val}>{m.v}</span>
        </div>
      ))}
    </div>
  </header>
);
const coverStyles = {
  cover: { minHeight: '88vh', display: 'flex', flexDirection: 'column', justifyContent: 'space-between', padding: '72px 60px 60px', borderBottom: `1px solid ${INK}`, background: PAPER },
  top: { display: 'flex', justifyContent: 'space-between', fontFamily: 'var(--font-mono)', fontSize: 11, letterSpacing: '0.2em', textTransform: 'uppercase', color: MUTE, gap: 24 },
  middle: { margin: 'auto 0', textAlign: 'left' },
  headline: { fontFamily: 'var(--font-display)', fontWeight: 300, fontSize: 'clamp(52px, 8.5vw, 132px)', lineHeight: 0.95, letterSpacing: '-0.04em', fontVariationSettings: '"opsz" 144', color: INK, margin: 0, maxWidth: '15ch', textWrap: 'balance' },
  bottom: { display: 'grid', gridTemplateColumns: '2fr 1fr 1fr 1fr', gap: 48, alignItems: 'end', fontFamily: 'var(--font-mono)', fontSize: 11, letterSpacing: '0.15em', textTransform: 'uppercase' },
  caption: { fontFamily: 'var(--font-display)', fontStyle: 'italic', fontWeight: 300, fontSize: 'clamp(24px, 2.6vw, 36px)', letterSpacing: '-0.02em', textTransform: 'none', color: INK, lineHeight: 1.15, fontVariationSettings: '"opsz" 144', maxWidth: '30ch' },
  col: { display: 'flex', flexDirection: 'column' },
  label: { color: MUTE, marginBottom: 6 },
  val: { color: INK },
};

/* ---------------- Footer ---------------- */
const Footer = ({ onNavigate }) => (
  <footer style={fStyles.footer}>
    <div style={{ marginBottom: 28 }}>
      <span className="ff-logo" style={{ fontSize: 'clamp(56px, 9vw, 92px)' }}>Fore<em>front</em></span>
    </div>
    <div style={fStyles.end}>At the leading edge of what's next · MMXXVI</div>
    <div style={{ marginTop: 8, marginBottom: 34 }}>
      <Button size="sm" variant="secondary" onClick={() => onNavigate('contact')}>Get in touch →</Button>
    </div>
    <div style={fStyles.line}>
      <span>hello@forefront.co</span><span>·</span><span>forefront.co</span><span>·</span><span>Worldwide</span>
    </div>
  </footer>
);
const fStyles = {
  footer: { padding: '100px 60px 64px', textAlign: 'center', background: PAPER },
  end: { fontFamily: 'var(--font-mono)', fontSize: 10, letterSpacing: '0.3em', textTransform: 'uppercase', color: MUTE },
  line: { fontFamily: 'var(--font-mono)', fontSize: 10, letterSpacing: '0.2em', textTransform: 'uppercase', color: MUTE, display: 'flex', gap: 14, justifyContent: 'center', flexWrap: 'wrap' },
};

Object.assign(window, { Section, SectionHead, Button, Nav, Cover, Footer, INK, PAPER, SOFT, MUTE, HAIR, HAIR_REV, EASE });
