/* ==========================================================================
   Shared primitives: Nav, Footer, Eyebrow, SectionHeader, CTAButton, Icon
   ========================================================================== */

const PAGES = [
  { id: 'home',       label: 'Home' },
  { id: 'uas',        label: 'UAS Program' },
  { id: 'services',   label: 'Services' },
  { id: 'contact',    label: 'Contact' },
];

const Icon = ({ name, size = 20, stroke = 1.5, color = 'currentColor' }) => {
  const common = {
    width: size, height: size, viewBox: '0 0 24 24', fill: 'none',
    stroke: color, strokeWidth: stroke, strokeLinecap: 'round', strokeLinejoin: 'round',
  };
  const paths = {
    arrow: <path d="M5 12h14M13 5l7 7-7 7"/>,
    check: <polyline points="20 6 9 17 4 12"/>,
    plus:  <path d="M12 5v14M5 12h14"/>,
    minus: <path d="M5 12h14"/>,
    radar: <><circle cx="12" cy="12" r="9"/><path d="M12 12 L20 7"/><circle cx="12" cy="12" r="1" fill={color}/></>,
    target:<><circle cx="12" cy="12" r="9"/><circle cx="12" cy="12" r="5"/><circle cx="12" cy="12" r="1" fill={color}/></>,
    mappin:<><path d="M20 10c0 6-8 12-8 12S4 16 4 10a8 8 0 1 1 16 0z"/><circle cx="12" cy="10" r="3"/></>,
    shield:<path d="M12 2 4 6v6c0 5 3.5 9 8 10 4.5-1 8-5 8-10V6l-8-4z"/>,
    calendar:<><rect x="3" y="5" width="18" height="16" rx="2"/><path d="M8 3v4M16 3v4M3 11h18"/></>,
    chart: <><path d="M3 3v18h18"/><path d="M7 15l4-4 4 3 5-6"/></>,
    mail:  <><rect x="3" y="5" width="18" height="14" rx="2"/><path d="M3 7l9 6 9-6"/></>,
    phone: <path d="M22 16.9v3a2 2 0 0 1-2.2 2 19.8 19.8 0 0 1-8.6-3.1 19.5 19.5 0 0 1-6-6A19.8 19.8 0 0 1 2.1 4.2 2 2 0 0 1 4.1 2h3a2 2 0 0 1 2 1.7c.1.9.3 1.8.6 2.6a2 2 0 0 1-.5 2.1L8 9.6a16 16 0 0 0 6 6l1.2-1.2a2 2 0 0 1 2.1-.5c.8.3 1.7.5 2.6.6A2 2 0 0 1 22 16.9z"/>,
    play:  <polygon points="5 3 19 12 5 21 5 3"/>,
    drone: <><circle cx="12" cy="12" r="2"/><path d="M6 6l3 3M18 6l-3 3M6 18l3-3M18 18l-3-3"/><circle cx="5" cy="5" r="2"/><circle cx="19" cy="5" r="2"/><circle cx="5" cy="19" r="2"/><circle cx="19" cy="19" r="2"/></>,
    drone2: <><path d="M12 9v6M9 12h6"/><ellipse cx="5" cy="5" rx="3" ry="1.5"/><ellipse cx="19" cy="5" rx="3" ry="1.5"/><ellipse cx="5" cy="19" rx="3" ry="1.5"/><ellipse cx="19" cy="19" rx="3" ry="1.5"/><path d="M7 7l3.5 3.5M17 7l-3.5 3.5M7 17l3.5-3.5M17 17l-3.5-3.5"/><circle cx="12" cy="12" r="2.5"/></>,
    clipboard:<><rect x="6" y="4" width="12" height="18" rx="2"/><path d="M9 4V3a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v1"/><path d="M9 12h6M9 16h4"/></>,
    sparkles:<><path d="M12 3l1.5 4.5L18 9l-4.5 1.5L12 15l-1.5-4.5L6 9l4.5-1.5z"/><path d="M19 15l.8 2.2L22 18l-2.2.8L19 21l-.8-2.2L16 18l2.2-.8z"/></>,
    users: <><circle cx="9" cy="8" r="4"/><path d="M1 22v-2a6 6 0 0 1 8-5.7"/><circle cx="17" cy="9" r="3"/><path d="M22 22v-2a5 5 0 0 0-6-4.9"/></>,
    play2: <><circle cx="12" cy="12" r="10"/><polygon points="10 8 16 12 10 16" fill={color} stroke="none"/></>,
    star:  <polygon points="12 2 15 9 22 9.3 16.5 14 18 21 12 17.3 6 21 7.5 14 2 9.3 9 9"/>,
    quote: <><path d="M9 7H5a2 2 0 0 0-2 2v4a2 2 0 0 0 2 2h2v3a3 3 0 0 1-3 3"/><path d="M19 7h-4a2 2 0 0 0-2 2v4a2 2 0 0 0 2 2h2v3a3 3 0 0 1-3 3"/></>,
    film:  <><rect x="3" y="3" width="18" height="18" rx="2"/><path d="M3 8h18M3 16h18M8 3v18M16 3v18"/></>,
    video: <><rect x="3" y="6" width="13" height="12" rx="2"/><path d="M16 10l5-3v10l-5-3z"/></>,
    layers:<><path d="M12 2 2 7l10 5 10-5-10-5z"/><path d="M2 17l10 5 10-5M2 12l10 5 10-5"/></>,
    globe: <><circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3a13 13 0 0 1 0 18M12 3a13 13 0 0 0 0 18"/></>,
    camera:<><rect x="3" y="7" width="18" height="13" rx="2"/><path d="M8 7l1.5-3h5L16 7"/><circle cx="12" cy="13" r="3.5"/></>,
  };
  return <svg {...common}>{paths[name] || null}</svg>;
};

const Eyebrow = ({ children, style }) => (
  <div style={{
    fontFamily: 'Inter', fontSize: 12, fontWeight: 600,
    letterSpacing: '0.14em', textTransform: 'uppercase', color: '#E8500A',
    display: 'inline-flex', alignItems: 'center', gap: 10,
    ...style,
  }}>
    <span style={{ width: 24, height: 1, background: '#E8500A' }}/>
    {children}
  </div>
);

const BrandMark = ({ size = 35 }) => (
  <img src="assets/logo.png" height={size} alt="Bright Media" style={{ flexShrink: 0, display: 'block' }}/>
);

const Button = ({ variant = 'primary', icon, children, onClick, fullWidth, size = 'md' }) => {
  const sizes = {
    sm: { pad: '8px 16px', fs: 13 },
    md: { pad: '12px 24px', fs: 15 },
    lg: { pad: '14px 28px', fs: 16 },
  }[size];
  const styles = {
    primary:   { bg:'#E8500A', fg:'#fff', bd:'#E8500A' },
    secondary: { bg:'transparent', fg:'#E8500A', bd:'#E8500A' },
    ghost:     { bg:'transparent', fg:'#fff', bd:'#2A4A75' },
    onLight:   { bg:'#0A1628', fg:'#fff', bd:'#0A1628' },
  }[variant];
  return (
    <button onClick={onClick} style={{
      fontFamily: 'Inter', fontWeight: 600, letterSpacing: '0.02em',
      fontSize: sizes.fs, padding: sizes.pad, borderRadius: 6,
      border: '1.5px solid ' + styles.bd, background: styles.bg, color: styles.fg,
      cursor: 'pointer', display: 'inline-flex', alignItems: 'center', gap: 8,
      width: fullWidth ? '100%' : 'auto', justifyContent: 'center',
      transition: 'all 200ms cubic-bezier(0.2,0.8,0.2,1)', whiteSpace: 'nowrap',
    }}
      onMouseEnter={e => {
        if (variant === 'primary')   { e.currentTarget.style.background = '#FF6420'; e.currentTarget.style.borderColor = '#FF6420'; }
        if (variant === 'secondary') { e.currentTarget.style.background = 'rgba(232,80,10,0.1)'; }
        if (variant === 'ghost')     { e.currentTarget.style.borderColor = '#A8B8CC'; e.currentTarget.style.background = 'rgba(255,255,255,0.03)'; }
      }}
      onMouseLeave={e => {
        e.currentTarget.style.background = styles.bg; e.currentTarget.style.borderColor = styles.bd;
      }}
    >
      {children}{icon && <Icon name={icon} size={16}/>}
    </button>
  );
};

/* ---------- Nav ---------- */
const Nav = ({ current, onNav }) => {
  const [scrolled, setScrolled] = React.useState(false);
  const [navVisible, setNavVisible] = React.useState(current !== 'home');

  React.useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 8);
    window.addEventListener('scroll', onScroll); onScroll();
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  // Reset visibility when navigating to/from home
  React.useEffect(() => {
    if (current !== 'home') {
      setNavVisible(true);
    } else {
      setNavVisible(false);
    }
  }, [current]);

  // Fade in when video first loops
  React.useEffect(() => {
    const handler = () => setNavVisible(true);
    window.addEventListener('bm-video-looped', handler);
    return () => window.removeEventListener('bm-video-looped', handler);
  }, []);

  return (
    <nav style={{
      position: 'sticky', top: 0, zIndex: 50,
      background: !navVisible ? 'transparent' : scrolled ? 'rgba(10,22,40,0.88)' : 'rgba(10,22,40,0.6)',
      backdropFilter: navVisible ? 'blur(12px)' : 'none',
      WebkitBackdropFilter: navVisible ? 'blur(12px)' : 'none',
      borderBottom: '1px solid ' + (!navVisible ? 'transparent' : scrolled ? '#2A4A75' : 'transparent'),
      opacity: navVisible ? 1 : 0,
      transition: 'opacity 1.6s ease, background 200ms cubic-bezier(0.2,0.8,0.2,1), border-color 200ms cubic-bezier(0.2,0.8,0.2,1)',
      pointerEvents: navVisible ? 'auto' : 'none',
    }}>
      <div style={{
        maxWidth: 1280, margin: '0 auto', padding: '16px 32px',
        display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 32,
        position: 'relative',
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 12, cursor: 'pointer' }} onClick={() => onNav('home')}>
          <BrandMark/>
        </div>
        <div style={{ display: 'flex', gap: 28, fontFamily: 'Inter', fontSize: 14, fontWeight: 500, position: 'absolute', left: '50%', transform: 'translateX(-50%)' }}>
          {PAGES.map(it => (
            <a key={it.id} onClick={() => onNav(it.id)}
              style={{
                cursor: 'pointer', position: 'relative', padding: '4px 0',
                color: current === it.id ? '#fff' : '#A8B8CC',
                transition: 'color 200ms',
              }}
              onMouseEnter={e => e.currentTarget.style.color = '#fff'}
              onMouseLeave={e => e.currentTarget.style.color = current === it.id ? '#fff' : '#A8B8CC'}
            >
              {it.label}
              {current === it.id && (
                <span style={{ position:'absolute', left:0, right:0, bottom:-4, height:2, background:'#E8500A', borderRadius:2 }}/>
              )}
            </a>
          ))}
        </div>
        <div style={{ display: 'flex', gap: 12, alignItems: 'center' }}>
<Button variant="primary" size="sm" onClick={() => onNav('contact')}>Request a campaign</Button>
        </div>
      </div>
    </nav>
  );
};

/* ---------- Page hero (small banner used on non-home pages) ---------- */
const PageHeader = ({ eyebrow, title, subtitle, children }) => (
  <section style={{
    position:'relative', overflow:'hidden',
    padding:'96px 32px 72px',
    background:'radial-gradient(ellipse at 85% 20%, rgba(232,80,10,0.12), transparent 55%), #0A1628',
    borderBottom:'1px solid #1E3A5F',
  }}>
    {/* subtle grid */}
    <div style={{
      position:'absolute', inset:0, opacity:0.35,
      backgroundImage:'linear-gradient(#2A4A75 1px, transparent 1px), linear-gradient(90deg, #2A4A75 1px, transparent 1px)',
      backgroundSize:'80px 80px',
      maskImage:'radial-gradient(ellipse at 50% 40%, black 30%, transparent 75%)',
      WebkitMaskImage:'radial-gradient(ellipse at 50% 40%, black 30%, transparent 75%)',
      pointerEvents:'none',
    }}/>
    <div style={{ position:'relative', maxWidth:1280, margin:'0 auto' }}>
      {eyebrow && <Eyebrow style={{ marginBottom: 20 }}>{eyebrow}</Eyebrow>}
      <h1 style={{
        fontFamily:'Barlow', fontWeight:700, fontSize:72, lineHeight:1.02,
        letterSpacing:'-0.025em', margin:0, color:'#fff', maxWidth: 1000,
      }}>{title}</h1>
      {subtitle && (
        <p style={{ color:'#A8B8CC', fontSize:18, lineHeight:1.7, margin:'24px 0 0', maxWidth: 640 }}>{subtitle}</p>
      )}
      {children && <div style={{ marginTop: 32 }}>{children}</div>}
    </div>
  </section>
);

/* ---------- Big orange stats strip (from reference) ---------- */
const StatsStrip = ({ items }) => (
  <div style={{
    background: 'linear-gradient(90deg, #E8500A 0%, #C83C00 100%)',
    padding: '22px 32px',
    boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.12), 0 12px 32px rgba(232,80,10,0.15)',
  }}>
    <div style={{
      maxWidth: 1280, margin: '0 auto',
      display: 'grid', gridTemplateColumns: `repeat(${items.length}, 1fr)`, gap: 24,
    }}>
      {items.map((s, i) => (
        <div key={i} style={{
          display:'flex', alignItems:'center', gap:14,
          borderLeft: i === 0 ? 'none' : '1px solid rgba(255,255,255,0.22)',
          paddingLeft: i === 0 ? 0 : 24,
        }}>
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#fff" strokeWidth="2" strokeLinecap="round" style={{flexShrink:0}}>
            <path d="M12 2v20M2 12h20M5 5l14 14M19 5L5 19"/>
          </svg>
          <div style={{ display:'flex', flexDirection:'column', gap:2, minWidth:0 }}>
            <div style={{ fontFamily:'Barlow', fontWeight:700, fontSize:26, color:'#fff', letterSpacing:'-0.02em', lineHeight:1 }}>{s.k}</div>
            <div style={{ fontFamily:'Inter', fontSize:12, color:'rgba(255,255,255,0.85)', lineHeight:1.4 }}>{s.v}</div>
          </div>
        </div>
      ))}
    </div>
  </div>
);

/* ---------- Final CTA band ---------- */
const CTABand = ({ onNav }) => (
  <section style={{ padding: '80px 32px 120px', background:'#0A1628' }}>
    <div style={{ maxWidth: 1280, margin: '0 auto' }}>
      <div style={{
        position:'relative', overflow:'hidden',
        borderRadius: 16, padding: '56px 56px',
        background: 'linear-gradient(135deg, #E8500A 0%, #C83C00 100%)',
        display:'grid', gridTemplateColumns:'1.4fr 1fr', gap: 48, alignItems:'center',
      }}>
        {/* decorative drone silhouette */}
        <svg width="280" height="280" viewBox="0 0 280 280" style={{ position:'absolute', right:-40, top:-20, opacity:0.18 }}>
          <g stroke="#fff" strokeWidth="1.5" fill="none">
            <circle cx="140" cy="140" r="120" strokeDasharray="2 6"/>
            <circle cx="140" cy="140" r="80" strokeDasharray="2 6"/>
            <circle cx="140" cy="140" r="40"/>
          </g>
        </svg>
        <div style={{ position:'relative', zIndex:1 }}>
          <h2 style={{
            fontFamily:'Barlow', fontWeight:700, fontSize:44, lineHeight:1.08,
            letterSpacing:'-0.02em', margin:0, color:'#fff',
          }}>
            Let's craft an aerial strategy that drives <em style={{ fontStyle:'italic', color:'#0A1628' }}>real engagement</em> for your brand.
          </h2>
          <p style={{ color:'rgba(255,255,255,0.88)', fontSize:16, lineHeight:1.7, marginTop: 16, marginBottom: 0, maxWidth: 560 }}>
            One dedicated crew. One project at a time. Full compliance, every flight.
          </p>
        </div>
        <div style={{ position:'relative', zIndex:1, display:'flex', flexDirection:'column', gap:12, alignItems:'flex-start' }}>
          <Button variant="onLight" icon="arrow" onClick={() => onNav('contact')} size="lg">Get your free consultation</Button>
          <a onClick={() => onNav('how')} style={{
            color:'#fff', fontSize:14, fontWeight:500, cursor:'pointer',
            fontFamily:'Inter', textDecoration:'underline', textUnderlineOffset:4, opacity:0.9,
          }}>See how it works →</a>
        </div>
      </div>
    </div>
  </section>
);

/* ---------- Footer ---------- */
const Footer = ({ onNav }) => (
  <footer style={{
    background: '#0A1628', borderTop:'1px solid #1E3A5F',
    padding: '72px 32px 32px',
  }}>
    <div style={{ maxWidth: 1280, margin: '0 auto' }}>
      <div style={{ display: 'grid', gridTemplateColumns: '2fr 1fr 1fr 1fr 1fr', gap: 48, paddingBottom: 48 }}>
        <div>
          <div style={{ display: 'flex', alignItems: 'center', gap: 12, marginBottom: 16 }}>
            <BrandMark/>
          </div>
          <p style={{ color: '#A8B8CC', fontSize: 14, lineHeight: 1.7, maxWidth: 320, margin: 0 }}>
            Innovative aerial media, engineered around compliance. Santa Monica, California.
          </p>
          <div style={{ marginTop: 20, display:'flex', alignItems:'center', gap:10, color:'#4CC38A', fontSize:12 }}>
            <span style={{ width: 8, height: 8, borderRadius: 999, background: '#4CC38A' }}/>
            FAA Part 107 Certified · Operator #BM-107-2024
          </div>
        </div>
        {[
          { t: 'Company', links: [['About','home'],['UAS Program','uas'],['How it works','how'],['Careers','home']] },
          { t: 'Services', links: [['Contract','services'],['Location','services'],['Outsource','services'],['Pricing','pricing']] },
          { t: 'Resources', links: [['FAQs','faq'],['Case studies','home'],['Press kit','home'],['Compliance','home']] },
          { t: 'Contact', links: [['Book a call','contact'],['info@brightmediausa.com\n','contact'],['Santa Monica, CA','contact']] },
        ].map(col => (
          <div key={col.t}>
            <div style={{ fontFamily: 'Inter', fontSize: 12, fontWeight: 600, letterSpacing: '0.14em', textTransform: 'uppercase', color: '#E8500A', marginBottom: 16 }}>{col.t}</div>
            <ul style={{ listStyle: 'none', padding: 0, margin: 0, display: 'flex', flexDirection: 'column', gap: 12 }}>
              {col.links.map(([label, id]) => (
                <li key={label}>
                  <a onClick={() => onNav(id)} style={{
                    color: '#A8B8CC', fontSize: 14, cursor: 'pointer', fontFamily:'Inter',
                    transition:'color 200ms',
                  }}
                    onMouseEnter={e => e.currentTarget.style.color='#fff'}
                    onMouseLeave={e => e.currentTarget.style.color='#A8B8CC'}
                  >{label}</a>
                </li>
              ))}
            </ul>
          </div>
        ))}
      </div>
      <div style={{ borderTop: '1px solid #1E3A5F', paddingTop: 24, display: 'flex', justifyContent: 'space-between', alignItems:'center', color: '#6B7D94', fontSize: 13, fontFamily:'Inter' }}>
        <div>© 2026 Bright Media. All rights reserved.</div>
        <div style={{ display:'flex', gap:24 }}>
          <a style={{ color:'#6B7D94', cursor:'pointer' }}>Privacy</a>
          <a style={{ color:'#6B7D94', cursor:'pointer' }}>Terms</a>
          <a style={{ color:'#6B7D94', cursor:'pointer' }}>FAA Compliance</a>
        </div>
      </div>
    </div>
  </footer>
);

Object.assign(window, { PAGES, Icon, Eyebrow, BrandMark, Button, Nav, PageHeader, StatsStrip, CTABand, Footer });
