// Shared SVG pieces and small helpers
const { useEffect, useRef, useState } = React;

function useInView(options = {}) {
  const ref = useRef(null);
  const [inView, setInView] = useState(false);
  useEffect(() => {
    if (!ref.current) return;
    const el = ref.current;
    // Fallback: if element is already in viewport on mount, reveal immediately
    const check = () => {
      const r = el.getBoundingClientRect();
      const vh = window.innerHeight || document.documentElement.clientHeight;
      if (r.top < vh * 0.92 && r.bottom > 0) { setInView(true); return true; }
      return false;
    };
    if (check()) return;
    const obs = new IntersectionObserver(([e]) => {
      if (e.isIntersecting) { setInView(true); obs.disconnect(); }
    }, { threshold: 0.15, ...options });
    obs.observe(el);
    const onScroll = () => { if (check()) { obs.disconnect(); window.removeEventListener('scroll', onScroll); } };
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => { obs.disconnect(); window.removeEventListener('scroll', onScroll); };
  }, []);
  return [ref, inView];
}

function Reveal({ children, delay = 0, as: As = "div", className = "", ...rest }) {
  const [ref, inView] = useInView();
  return (
    <As
      ref={ref}
      className={`reveal ${inView ? 'in' : ''} ${className}`}
      style={{ transitionDelay: `${delay}ms` }}
      {...rest}
    >
      {children}
    </As>
  );
}

// Count-up with easing
function useCountUp(target, inView, duration = 1800) {
  const [n, setN] = useState(0);
  useEffect(() => {
    if (!inView) return;
    let raf, start;
    const step = (t) => {
      if (!start) start = t;
      const p = Math.min(1, (t - start) / duration);
      const eased = 1 - Math.pow(1 - p, 3);
      setN(Math.floor(eased * target));
      if (p < 1) raf = requestAnimationFrame(step);
      else setN(target);
    };
    raf = requestAnimationFrame(step);
    return () => cancelAnimationFrame(raf);
  }, [inView, target]);
  return n;
}

// Brand mark — architectural "DR" monogram (bridge pylon)
function BrandMark({ size = 26 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 26 26" fill="none">
      <rect x="0.5" y="0.5" width="25" height="25" stroke="#C9A961" strokeOpacity="0.4"/>
      <path d="M7 5 L7 21" stroke="#C9A961" strokeWidth="1"/>
      <path d="M19 5 L19 21" stroke="#C9A961" strokeWidth="1"/>
      <path d="M7 5 Q13 13 19 5" stroke="#C9A961" strokeWidth="0.7" fill="none" opacity="0.8"/>
      <path d="M7 21 Q13 13 19 21" stroke="#C9A961" strokeWidth="0.7" fill="none" opacity="0.8"/>
      <circle cx="13" cy="13" r="1.2" fill="#C9A961"/>
    </svg>
  );
}

// Hero bridge — wide architectural horizon, restrained and iconic
function HeroBridge() {
  // Wide aspect — meant to sit full-width behind the headline
  // Two pylons, a thin illuminated deck, fine stay cables
  return (
    <svg viewBox="0 0 1600 600" fill="none" preserveAspectRatio="xMidYMid slice" style={{ width: '100%', height: '100%' }}>
      <defs>
        <linearGradient id="deckGradH" x1="0" x2="1" y1="0" y2="0">
          <stop offset="0" stopColor="#C9A961" stopOpacity="0"/>
          <stop offset="0.15" stopColor="#C9A961" stopOpacity="0.25"/>
          <stop offset="0.5" stopColor="#E5C88F" stopOpacity="0.85"/>
          <stop offset="0.85" stopColor="#C9A961" stopOpacity="0.25"/>
          <stop offset="1" stopColor="#C9A961" stopOpacity="0"/>
        </linearGradient>
        <linearGradient id="pylonGradH" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor="#E5C88F" stopOpacity="0.0"/>
          <stop offset="0.15" stopColor="#E5C88F" stopOpacity="0.75"/>
          <stop offset="1" stopColor="#8A7340" stopOpacity="0.35"/>
        </linearGradient>
        <radialGradient id="glowH" cx="0.5" cy="0.85" r="0.6">
          <stop offset="0" stopColor="#C9A961" stopOpacity="0.12"/>
          <stop offset="1" stopColor="#C9A961" stopOpacity="0"/>
        </radialGradient>
      </defs>

      {/* Soft underglow */}
      <rect x="0" y="0" width="1600" height="600" fill="url(#glowH)"/>

      {/* Horizon */}
      <line x1="0" y1="430" x2="1600" y2="430" stroke="#C9A961" strokeOpacity="0.06" strokeWidth="0.5" strokeDasharray="1 8"/>

      {/* Deck — the luminous line */}
      <line x1="0" y1="420" x2="1600" y2="420" stroke="url(#deckGradH)" strokeWidth="1"/>
      <line x1="0" y1="420" x2="1600" y2="420" stroke="url(#deckGradH)" strokeWidth="3" opacity="0.35"/>

      {/* Two pylons */}
      {[560, 1040].map(px => (
        <g key={px}>
          <line x1={px} y1="110" x2={px} y2="420" stroke="url(#pylonGradH)" strokeWidth="1.25"/>
          <line x1={px-9} y1="110" x2={px+9} y2="110" stroke="#E5C88F" strokeOpacity="0.55" strokeWidth="0.8"/>
          <line x1={px-5} y1="120" x2={px+5} y2="120" stroke="#E5C88F" strokeOpacity="0.35" strokeWidth="0.6"/>
        </g>
      ))}

      {/* Stay cables — symmetrical harp pattern from each pylon */}
      {(() => {
        const pylons = [{ x: 560, leftEnd: 200, rightEnd: 800 }, { x: 1040, leftEnd: 800, rightEnd: 1400 }];
        const cables = [];
        pylons.forEach((p, pi) => {
          for (let i = 1; i <= 9; i++) {
            const t = i/10;
            // left cables
            const lx = p.x - (p.x - p.leftEnd) * t;
            cables.push(
              <line key={`${pi}L${i}`} x1={p.x} y1="110" x2={lx} y2="420"
                stroke="#C9A961" strokeOpacity={0.12 + (1-t)*0.22} strokeWidth="0.5"
                className="cable" style={{ '--d': `${pi*400 + i*60}ms` }}/>
            );
            // right cables
            const rx = p.x + (p.rightEnd - p.x) * t;
            cables.push(
              <line key={`${pi}R${i}`} x1={p.x} y1="110" x2={rx} y2="420"
                stroke="#C9A961" strokeOpacity={0.12 + (1-t)*0.22} strokeWidth="0.5"
                className="cable" style={{ '--d': `${pi*400 + i*60 + 30}ms` }}/>
            );
          }
        });
        return cables;
      })()}

      {/* Catenary arcs — single continuous curve across span */}
      <path d="M 0 260 Q 560 90 800 220 Q 1040 90 1600 260"
            stroke="#C9A961" strokeWidth="0.7" fill="none" opacity="0.55" className="catenary"/>

      {/* Node markers on deck */}
      {[200, 560, 800, 1040, 1400].map((x) => (
        <circle key={x} cx={x} cy="420" r="2.5" fill="#E5C88F" opacity="0.9"/>
      ))}

      {/* Slow travelling light along the deck */}
      <circle r="2" fill="#E5C88F" opacity="0.9" className="spark">
        <animateMotion dur="14s" repeatCount="indefinite" path="M 0 420 L 1600 420"/>
      </circle>

      <style>{`
        .cable { stroke-dasharray: 400; stroke-dashoffset: 400; }
        .draw .cable { animation: drawCable 2.6s cubic-bezier(.2,.8,.2,1) forwards; animation-delay: var(--d); }
        .catenary { stroke-dasharray: 2000; stroke-dashoffset: 2000; }
        .draw .catenary { animation: drawCable 3.6s cubic-bezier(.2,.8,.2,1) 0.2s forwards; }
        .spark { filter: drop-shadow(0 0 4px #E5C88F); }
        @keyframes drawCable { to { stroke-dashoffset: 0; } }
      `}</style>
    </svg>
  );
}

Object.assign(window, { useInView, Reveal, useCountUp, BrandMark, HeroBridge });