// Shared scroll-reveal hook — defined once, used across all components
window.useInView = (threshold = 0.15) => {
  const ref = React.useRef(null);
  const [inView, setInView] = React.useState(false);
  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const obs = new IntersectionObserver(([e]) => {
      if (e.isIntersecting) { setInView(true); obs.disconnect(); }
    }, { threshold });
    obs.observe(el);
    return () => obs.disconnect();
  }, []);
  return [ref, inView];
};

// Header.jsx — Sticky Nav + Hero + Lead Form
const Header = ({ onNav }) => {
  const [scrolled, setScrolled] = React.useState(false);
  const [formData, setFormData] = React.useState({ name: '', business: '', email: '', phone: '', setup: '' });
  const [submitted, setSubmitted] = React.useState(false);
  const [focused, setFocused] = React.useState('');

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

  const navLinks = [
    { label: 'Line Cleaning',  href: 'index.html#line-cleaning' },
    { label: 'Why It Matters', href: 'index.html#why-it-matters' },
    { label: 'Service Area',   href: 'index.html#service-area' },
    
    { label: 'SkyTab POS',     href: 'skytab.html' },
    { label: 'All Services',   href: 'services.html' },
  ];

  const handleSubmit = (e) => {
    e.preventDefault();
    setSubmitted(true);
  };

  const inputStyle = (key) => ({
    padding: '11px 14px',
    fontFamily: "'Raleway', sans-serif",
    fontSize: 14,
    background: focused === key ? 'rgba(255,255,255,0.14)' : 'rgba(255,255,255,0.08)',
    border: focused === key ? '1px solid rgba(232,180,34,0.6)' : '1px solid rgba(255,255,255,0.18)',
    borderRadius: 3,
    color: '#fff',
    outline: 'none',
    boxSizing: 'border-box',
    width: '100%',
    transition: 'all 0.2s',
  });

  return (
    <header style={{ position: 'relative', background: '#0E0C0B', overflow: 'hidden' }}>

      {/* Real photography background */}
      <div style={{
        position: 'absolute', inset: 0, zIndex: 0,
        backgroundImage: 'url(uploads/istockphoto-1093593288-612x612.jpg)',
        backgroundSize: 'cover', backgroundPosition: 'center 20%',
      }} />
      {/* Dark overlay — heavy left for text legibility, lighter right to show photo */}
      <div style={{ position: 'absolute', inset: 0, background: 'linear-gradient(105deg, rgba(10,6,4,0.96) 0%, rgba(10,6,4,0.78) 42%, rgba(10,6,4,0.60) 65%, rgba(10,6,4,0.72) 100%)', zIndex: 1 }} />
      {/* Gold glow orb */}
      <div style={{ position: 'absolute', top: '8%', right: '28%', width: 560, height: 560, background: 'radial-gradient(circle, rgba(232,180,34,0.08) 0%, rgba(232,180,34,0.02) 45%, transparent 70%)', zIndex: 2, pointerEvents: 'none' }} />
      {/* Red glow bottom left */}
      <div style={{ position: 'absolute', bottom: '-5%', left: '-5%', width: 400, height: 400, background: 'radial-gradient(circle, rgba(139,16,32,0.18) 0%, transparent 70%)', zIndex: 2, pointerEvents: 'none' }} />

      {/* ── NAV ── */}
      <nav style={{
        position: 'fixed', top: 0, left: 0, right: 0, zIndex: 100,
        background: scrolled ? 'rgba(14,12,11,0.92)' : 'transparent',
        backdropFilter: scrolled ? 'blur(12px)' : 'none',
        borderBottom: scrolled ? '1px solid rgba(255,255,255,0.06)' : '1px solid transparent',
        transition: 'all 0.35s ease',
      }}>
        <div style={{ maxWidth: 1200, margin: '0 auto', padding: '0 32px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', height: 68 }}>
          <img src="assets/tap-estate-logo.png" alt="Tap Estate" style={{ height: 34, filter: 'brightness(0) invert(1)', cursor: 'pointer', transition: 'opacity 0.2s' }}
            onClick={() => onNav && onNav('home')} />
          <div style={{ display: 'flex', alignItems: 'center', gap: 28 }}>
            {navLinks.map(l => (
              <a key={l.label} href={l.href}
                style={{ color: 'rgba(255,255,255,0.70)', fontSize: 12, fontFamily: "'Raleway', sans-serif", fontWeight: 700, textDecoration: 'none', letterSpacing: '0.08em', textTransform: 'uppercase', transition: 'color 0.2s' }}
                onMouseEnter={e => e.target.style.color = '#fff'}
                onMouseLeave={e => e.target.style.color = 'rgba(255,255,255,0.70)'}
              >{l.label}</a>
            ))}
          </div>
          <a href="tel:5192812337" style={{
            display: 'flex', alignItems: 'center', gap: 8,
            background: '#8B1020', color: '#fff',
            padding: '9px 18px', fontSize: 13, fontFamily: "'Raleway', sans-serif",
            fontWeight: 700, textDecoration: 'none', borderRadius: 3,
            letterSpacing: '0.05em', transition: 'background 0.2s',
          }}
            onMouseEnter={e => e.currentTarget.style.background = '#6B0C18'}
            onMouseLeave={e => e.currentTarget.style.background = '#8B1020'}
          >
            <svg width="13" height="13" viewBox="0 0 24 24" fill="currentColor"><path d="M6.6 10.8c1.4 2.8 3.8 5.1 6.6 6.6l2.2-2.2c.3-.3.7-.4 1-.2 1.1.4 2.3.6 3.6.6.6 0 1 .4 1 1V20c0 .6-.4 1-1 1-9.4 0-17-7.6-17-17 0-.6.4-1 1-1h3.5c.6 0 1 .4 1 1 0 1.3.2 2.5.6 3.6.1.3 0 .7-.2 1L6.6 10.8z"/></svg>
            519-281-2337
          </a>
        </div>
      </nav>

      {/* ── HERO ── */}
      <div style={{ position: 'relative', zIndex: 10, maxWidth: 1200, margin: '0 auto', padding: '72px 32px 88px', display: 'grid', gridTemplateColumns: '1fr 400px', gap: 56, alignItems: 'center' }}>

        {/* Left */}
        <div>
          {/* Eyebrow */}
          <div style={{ display: 'inline-flex', alignItems: 'center', gap: 10, marginBottom: 28, animation: 'fadeInUp 0.6s ease both', animationDelay: '0.1s' }}>
            <div style={{ width: 28, height: 2, background: '#E8B422' }}></div>
            <span style={{ fontFamily: "'Raleway', sans-serif", fontSize: 11, fontWeight: 700, letterSpacing: '0.18em', textTransform: 'uppercase', color: '#E8B422' }}>Draught Line Specialists</span>
          </div>

          <h1 style={{ fontFamily: "'Raleway', sans-serif", fontWeight: 800, fontSize: 'clamp(52px, 7vw, 84px)', textTransform: 'uppercase', color: '#fff', lineHeight: 0.90, margin: '0 0 28px', letterSpacing: '-0.01em', animation: 'fadeInUp 0.7s ease both', animationDelay: '0.22s' }}>
            Better Beer<br />
            <span style={{ color: '#E8B422' }}>Starts</span><br />
            at the Line
          </h1>

          <p style={{ fontFamily: "'Raleway', sans-serif", fontSize: 17, fontWeight: 600, color: 'rgba(255,255,255,0.95)', margin: '0 0 6px', animation: 'fadeInUp 0.6s ease both', animationDelay: '0.36s' }}>Cleaner lines. Better taste. Happier customers.</p>
          <p style={{ fontFamily: "'Raleway', sans-serif", fontSize: 15, color: 'rgba(255,255,255,0.75)', margin: '0 0 40px', lineHeight: 1.65, maxWidth: 460, animation: 'fadeInUp 0.6s ease both', animationDelay: '0.44s' }}>
            Premium draught beer services for bars, restaurants and hospitality venues across Southwestern Ontario.
          </p>

          <div style={{ display: 'flex', gap: 12, flexWrap: 'wrap', marginBottom: 44, animation: 'fadeInUp 0.6s ease both', animationDelay: '0.54s' }}>
            <button onClick={() => document.getElementById('assessment-form').scrollIntoView({ behavior: 'smooth' })}
              style={{ background: '#E8B422', color: '#1A1210', border: 'none', padding: '14px 26px', fontFamily: "'Raleway', sans-serif", fontSize: 12, fontWeight: 800, letterSpacing: '0.10em', textTransform: 'uppercase', cursor: 'pointer', borderRadius: 3, display: 'flex', alignItems: 'center', gap: 9, transition: 'transform 0.15s, background 0.2s' }}
              onMouseEnter={e => { e.currentTarget.style.background = '#F5CC6A'; e.currentTarget.style.transform = 'translateY(-1px)'; }}
              onMouseLeave={e => { e.currentTarget.style.background = '#E8B422'; e.currentTarget.style.transform = 'translateY(0)'; }}
            >
              Get a Free Line Assessment
              <svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor"><path d="M13 7l5 5m0 0l-5 5m5-5H6"/></svg>
            </button>
            <a href="tel:5192812337" style={{ background: 'transparent', color: '#fff', border: '1.5px solid rgba(255,255,255,0.30)', padding: '14px 26px', fontFamily: "'Raleway', sans-serif", fontSize: 12, fontWeight: 700, letterSpacing: '0.10em', textTransform: 'uppercase', cursor: 'pointer', borderRadius: 3, textDecoration: 'none', display: 'inline-flex', alignItems: 'center', gap: 9, transition: 'border-color 0.2s, color 0.2s' }}
              onMouseEnter={e => { e.currentTarget.style.borderColor = 'rgba(255,255,255,0.65)'; e.currentTarget.style.color = '#fff'; }}
              onMouseLeave={e => { e.currentTarget.style.borderColor = 'rgba(255,255,255,0.30)'; }}
            >Book a Service Call</a>
          </div>

          {/* Trust strip */}
          <div style={{ display: 'flex', gap: 28, flexWrap: 'wrap', animation: 'fadeInUp 0.6s ease both', animationDelay: '0.64s' }}>
            {[
              { icon: '📍', text: 'Locally Owned' },
              { icon: '✓', text: 'Quality Guaranteed' },
              { icon: '⚡', text: 'Fast Response' },
              { icon: '🤝', text: 'Real Relationships' },
            ].map(t => (
              <div key={t.text} style={{ display: 'flex', alignItems: 'center', gap: 7 }}>
                <span style={{ width: 22, height: 22, borderRadius: '50%', background: 'rgba(232,180,34,0.15)', border: '1px solid rgba(232,180,34,0.30)', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 11, flexShrink: 0 }}>{t.icon}</span>
                <span style={{ fontFamily: "'Raleway', sans-serif", fontSize: 12, color: 'rgba(255,255,255,0.82)', fontWeight: 600, letterSpacing: '0.04em' }}>{t.text}</span>
              </div>
            ))}
          </div>
        </div>

        {/* Right — Lead Form */}
        <div id="assessment-form" style={{ background: 'rgba(139,16,32,0.97)', padding: '32px 28px', borderRadius: 4, boxShadow: '0 24px 64px rgba(0,0,0,0.45)', border: '1px solid rgba(255,255,255,0.06)', backdropFilter: 'blur(4px)', animation: 'fadeInRight 0.8s ease both', animationDelay: '0.3s' }}>
          {submitted ? (
            <div style={{ textAlign: 'center', padding: '28px 0' }}>
              <div style={{ width: 56, height: 56, borderRadius: '50%', background: 'rgba(232,180,34,0.15)', border: '2px solid #E8B422', display: 'flex', alignItems: 'center', justifyContent: 'center', margin: '0 auto 20px', fontSize: 24 }}>✓</div>
              <div style={{ fontFamily: "'Raleway',sans-serif", fontSize: 26, fontWeight: 800, color: '#fff', textTransform: 'uppercase', lineHeight: 1.1, marginBottom: 12 }}>You're All Set!</div>
              <div style={{ fontFamily: "'Raleway', sans-serif", fontSize: 14, color: 'rgba(255,255,255,0.65)', lineHeight: 1.6 }}>We'll be in touch shortly to arrange your free line assessment.</div>
            </div>
          ) : (
            <form onSubmit={handleSubmit}>
              <div style={{ marginBottom: 20 }}>
                <div style={{ fontFamily: "'Raleway',sans-serif", fontSize: 11, fontWeight: 700, letterSpacing: '0.15em', textTransform: 'uppercase', color: 'rgba(255,255,255,0.55)', marginBottom: 8 }}>Free Consultation</div>
                <div style={{ fontFamily: "'Raleway',sans-serif", fontSize: 24, fontWeight: 800, textTransform: 'uppercase', color: '#fff', lineHeight: 1.05, marginBottom: 8 }}>Get Your Free<br />Line Check</div>
                <div style={{ width: 36, height: 2, background: '#E8B422', marginBottom: 12 }}></div>
                <div style={{ fontFamily: "'Raleway', sans-serif", fontSize: 13, color: 'rgba(255,255,255,0.78)', lineHeight: 1.5 }}>We'll assess your system, look for issues, and give you honest recommendations.</div>
              </div>

              <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8, marginBottom: 8 }}>
                <input placeholder="Your Name" value={formData.name} onChange={e => setFormData({...formData, name: e.target.value})}
                  onFocus={() => setFocused('name')} onBlur={() => setFocused('')}
                  style={inputStyle('name')} />
                <input placeholder="Business Name" value={formData.business} onChange={e => setFormData({...formData, business: e.target.value})}
                  onFocus={() => setFocused('business')} onBlur={() => setFocused('')}
                  style={inputStyle('business')} />
              </div>
              <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8, marginBottom: 8 }}>
                <input placeholder="Email Address" type="email" value={formData.email} onChange={e => setFormData({...formData, email: e.target.value})}
                  onFocus={() => setFocused('email')} onBlur={() => setFocused('')}
                  style={inputStyle('email')} />
                <input placeholder="Phone Number" type="tel" value={formData.phone} onChange={e => setFormData({...formData, phone: e.target.value})}
                  onFocus={() => setFocused('phone')} onBlur={() => setFocused('')}
                  style={inputStyle('phone')} />
              </div>
              <textarea placeholder="Tell us about your setup (optional)" value={formData.setup} onChange={e => setFormData({...formData, setup: e.target.value})}
                onFocus={() => setFocused('setup')} onBlur={() => setFocused('')}
                style={{ ...inputStyle('setup'), resize: 'none', height: 72, marginBottom: 12 }} />

              <button type="submit" style={{ width: '100%', background: '#E8B422', color: '#1A1210', border: 'none', padding: '14px', fontFamily: "'Raleway', sans-serif", fontSize: 13, fontWeight: 800, letterSpacing: '0.08em', textTransform: 'uppercase', cursor: 'pointer', borderRadius: 3, transition: 'background 0.2s' }}
                onMouseEnter={e => e.currentTarget.style.background = '#F5CC6A'}
                onMouseLeave={e => e.currentTarget.style.background = '#E8B422'}
              >Get a Free Assessment →</button>

              <div style={{ fontFamily: "'Raleway', sans-serif", fontSize: 11, color: 'rgba(255,255,255,0.30)', textAlign: 'center', marginTop: 10, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 5 }}>
                <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect><path d="M7 11V7a5 5 0 0 1 10 0v4"></path></svg>
                We respect your privacy. No spam. Ever.
              </div>
            </form>
          )}
        </div>
      </div>

      {/* Bottom fade */}
      <div style={{ position: 'absolute', bottom: 0, left: 0, right: 0, height: 80, background: 'linear-gradient(to bottom, transparent, rgba(14,12,11,0.4))', zIndex: 2, pointerEvents: 'none' }} />
    </header>
  );
};

Object.assign(window, { Header });
