// Shared components — Logo, Nav, Footer primitives, Reveal hook

const LOGO_SRC = 'assets/logo-mark.png';

function Logo({ size = 40, dark = false }) {
  return (
    <div style={{ display:'flex', alignItems:'center', gap:12 }}>
      <img src={LOGO_SRC} alt="Above & Beyond Janitorial" style={{ width:size, height:size, objectFit:'contain', filter: dark ? 'none' : 'none' }} />
      <div>
        <div style={{ fontFamily:'var(--serif)', fontSize: size * 0.42, letterSpacing:-0.3, lineHeight:1, color: dark ? '#fff' : 'var(--ink)', fontWeight:500 }}>Above &amp; Beyond</div>
        <div style={{ fontSize: size * 0.18, letterSpacing:'.26em', textTransform:'uppercase', color:'var(--accent)', marginTop:3, fontWeight:600 }}>Janitorial · Est. 2004</div>
      </div>
    </div>
  );
}

function Nav({ dark = false }) {
  const [scrolled, setScrolled] = React.useState(false);
  React.useEffect(() => {
    const on = () => setScrolled(window.scrollY > 40);
    on(); window.addEventListener('scroll', on);
    return () => window.removeEventListener('scroll', on);
  }, []);

  const bg = scrolled
    ? (dark ? 'rgba(11,20,48,.92)' : 'rgba(245,243,236,.92)')
    : 'transparent';
  const color = dark ? '#fff' : 'var(--ink)';
  const border = scrolled ? '1px solid ' + (dark ? 'rgba(255,255,255,.08)' : 'var(--mist)') : '1px solid transparent';

  const items = ['Services', 'Industries', 'About', 'Area', 'Contact'];
  const anchors = ['#services', '#industries', '#about', '#area', '#contact'];

  const [wide, setWide] = React.useState(typeof window !== 'undefined' ? window.innerWidth >= 1080 : true);
  React.useEffect(() => {
    const on = () => setWide(window.innerWidth >= 1080);
    on(); window.addEventListener('resize', on);
    return () => window.removeEventListener('resize', on);
  }, []);

  return (
    <div style={{ position:'fixed', top:0, left:0, right:0, zIndex:50, background:bg, borderBottom:border, backdropFilter: scrolled ? 'blur(14px)' : 'none', transition:'all .3s ease' }}>
      <div className="container" style={{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'18px 48px', gap:24 }}>
        <a href="#top" style={{ color, flexShrink:0 }}>
          <Logo size={wide ? 44 : 38} dark={dark} />
        </a>
        {wide && (
          <div style={{ display:'flex', gap:28, fontSize:12, letterSpacing:'.1em', textTransform:'uppercase', color, fontWeight:500 }}>
            {items.map((it, i) => (
              <a key={it} href={anchors[i]} style={{ color, opacity:.85, transition:'opacity .15s', whiteSpace:'nowrap' }}
                 onMouseEnter={(e)=>e.currentTarget.style.opacity=1}
                 onMouseLeave={(e)=>e.currentTarget.style.opacity=.85}>{it}</a>
            ))}
          </div>
        )}
        <a href="tel:12088183175" style={{ display:'flex', alignItems:'center', gap:12, color, flexShrink:0, whiteSpace:'nowrap' }}>
          {wide && <span style={{ fontFamily:'var(--mono)', fontSize:12, color:'var(--accent)' }}>208·818·3175</span>}
          <button className="btn btn-light" style={{ padding:'10px 16px', fontSize:11 }}>
            Call Todd
          </button>
        </a>
      </div>
    </div>
  );
}

// Reveal on scroll
function useReveal() {
  React.useEffect(() => {
    const els = document.querySelectorAll('.fade-up');
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) {
          e.target.classList.add('in');
          io.unobserve(e.target);
        }
      });
    }, { threshold: 0.12, rootMargin: '0px 0px -60px 0px' });
    els.forEach(el => io.observe(el));
    return () => io.disconnect();
  }, []);
}

// Section header bar
function SectionEyebrow({ n, label }) {
  return (
    <div style={{ display:'flex', alignItems:'center', gap:16, marginBottom:28 }}>
      <span className="mono" style={{ fontSize:12, color:'var(--accent)' }}>{n}</span>
      <span style={{ width:40, height:1, background:'var(--accent)' }} />
      <span className="eyebrow">{label}</span>
    </div>
  );
}

function Chip({ children }) {
  return (
    <span style={{ display:'inline-flex', alignItems:'center', gap:8, padding:'6px 12px', border:'1px solid var(--mist)', background:'#fff', fontSize:11, letterSpacing:'.12em', textTransform:'uppercase', color:'var(--muted)', fontWeight:500, fontFamily:'var(--sans)' }}>
      {children}
    </span>
  );
}

// Duotone image — navy shadows, paper highlights
function DuotoneImage({ src, alt, style, label }) {
  return (
    <div className="img-wrap" style={style}>
      <img src={src} alt={alt} style={{ filter: 'grayscale(0.3) contrast(1.05)' }} />
      <div style={{ position:'absolute', inset:0, background:'linear-gradient(180deg, rgba(11,20,48,.15) 0%, rgba(11,20,48,.35) 100%)', pointerEvents:'none' }} />
      {label && <div className="img-label">{label}</div>}
    </div>
  );
}

Object.assign(window, { Logo, Nav, useReveal, SectionEyebrow, Chip, DuotoneImage, LOGO_SRC });
