// Hero — scrollytelling SVG alpine scene with parallax layers + pinned headline

function HeroMountainScene({ progress }) {
  // progress 0..1 across the hero stage
  const p = Math.max(0, Math.min(1, progress));
  // translate layers different amounts for parallax
  const tSky   = `translateY(${-p * 40}px)`;
  const tFar   = `translateY(${-p * 80}px)`;
  const tMid   = `translateY(${-p * 170}px)`;
  const tNear  = `translateY(${-p * 300}px) translateX(${p * -30}px)`;
  const tFore  = `translateY(${-p * 420}px) translateX(${p * 40}px)`;
  const tTrail = `translateY(${-p * 300}px) translateX(${p * -30}px)`;

  // Full-bleed: span 120vw, centered, so parallax translates don't expose gaps.
  const bleed = { bottom: 0, height: '100%', left: '-10vw', right: '-10vw', width: '120vw' };

  return (
    <div className="absolute inset-0" aria-hidden>
      {/* Sky gradient + sun */}
      <div className="hero-sky-grad" style={{ transform: tSky }} />
      <div className="hero-sun" style={{ transform: `${tSky} translateY(${p * 60}px)` }} />

      {/* Tiny stars, faintly */}
      <svg className="hero-stars" viewBox="0 0 1600 900" preserveAspectRatio="xMidYMid slice" style={{ transform: tSky, opacity: 0.4 }}>
        {Array.from({ length: 40 }).map((_, i) => {
          const x = (i * 97) % 1600;
          const y = (i * 53) % 380;
          const r = (i % 3) * 0.4 + 0.4;
          return <circle key={i} cx={x} cy={y} r={r} fill="#F5F0EB" opacity={0.35 + (i % 5) * 0.1} />;
        })}
      </svg>

      {/* Birds */}
      <svg className="hero-stars" viewBox="0 0 1600 900" preserveAspectRatio="xMidYMid slice" style={{ transform: `translateY(${-p * 60}px) translateX(${p * 80}px)` }}>
        <g stroke="#1F5E7A" strokeWidth="1.4" fill="none" strokeLinecap="round" strokeLinejoin="round" opacity="0.5">
          <path d="M980 220 q8 -8 16 0 q8 -8 16 0" />
          <path d="M1040 250 q6 -6 12 0 q6 -6 12 0" />
          <path d="M1100 210 q7 -7 14 0 q7 -7 14 0" />
          <path d="M1180 270 q5 -5 10 0 q5 -5 10 0" />
        </g>
      </svg>

      {/* Far ridge */}
      <svg className="hero-layer" viewBox="0 0 1600 900" preserveAspectRatio="xMidYMax slice"
           style={{ ...bleed, transform: tFar }}>
        <defs>
          <linearGradient id="farGrad" x1="0" x2="0" y1="0" y2="1">
            <stop offset="0%" stopColor="#9CCFE2" />
            <stop offset="100%" stopColor="#72BAD6" />
          </linearGradient>
        </defs>
        <path d="M-300 560 L120 470 L240 520 L360 420 L480 480 L600 400 L740 460 L880 390 L1020 450 L1160 410 L1320 470 L1460 430 L1900 480 L1900 900 L-300 900 Z" fill="url(#farGrad)" opacity="0.85" />
      </svg>

      {/* Middle ridge */}
      <svg className="hero-layer" viewBox="0 0 1600 900" preserveAspectRatio="xMidYMax slice"
           style={{ ...bleed, transform: tMid }}>
        <defs>
          <linearGradient id="midGrad" x1="0" x2="0" y1="0" y2="1">
            <stop offset="0%" stopColor="#55AACB" />
            <stop offset="100%" stopColor="#2E7A96" />
          </linearGradient>
          <linearGradient id="midSnow" x1="0" x2="0" y1="0" y2="1">
            <stop offset="0%" stopColor="#F5F0EB" />
            <stop offset="100%" stopColor="#C5E3EE" />
          </linearGradient>
        </defs>
        <path d="M-300 650 L100 560 L220 600 L340 500 L440 570 L560 440 L700 520 L820 480 L960 540 L1080 490 L1240 560 L1400 510 L1900 580 L1900 900 L-300 900 Z" fill="url(#midGrad)" />
        {/* snow caps */}
        <path d="M340 500 L380 515 L360 530 L420 525 L440 570 L400 560 L370 555 Z" fill="url(#midSnow)" opacity="0.9" />
        <path d="M560 440 L610 470 L590 490 L650 485 L700 520 L660 510 L620 500 Z" fill="url(#midSnow)" opacity="0.9" />
        <path d="M1080 490 L1120 510 L1100 525 L1160 520 L1200 545 L1160 535 L1130 530 Z" fill="url(#midSnow)" opacity="0.85" />
      </svg>

      {/* Near ridge — the hero peak */}
      <svg className="hero-layer" viewBox="0 0 1600 900" preserveAspectRatio="xMidYMax slice"
           style={{ ...bleed, transform: tNear }}>
        <defs>
          <linearGradient id="nearGrad" x1="0" x2="0" y1="0" y2="1">
            <stop offset="0%" stopColor="#1F5E7A" />
            <stop offset="100%" stopColor="#154B63" />
          </linearGradient>
          <linearGradient id="nearSnow" x1="0" x2="0" y1="0" y2="1">
            <stop offset="0%" stopColor="#F5F0EB" />
            <stop offset="100%" stopColor="#9CCFE2" />
          </linearGradient>
          <linearGradient id="nearBlush" x1="0" x2="1" y1="0" y2="1">
            <stop offset="0%" stopColor="#F2BBA0" />
            <stop offset="100%" stopColor="#B8715A" />
          </linearGradient>
        </defs>
        <path d="M-300 780 L180 640 L260 700 L420 520 L520 620 L640 380 L820 600 L960 500 L1100 620 L1280 560 L1440 640 L1900 700 L1900 900 L-300 900 Z" fill="url(#nearGrad)" />

        {/* hero peak snow */}
        <path d="M640 380 L700 440 L680 460 L740 455 L780 500 L820 540 L820 600 L760 580 L720 560 L680 540 L650 510 Z" fill="url(#nearSnow)" />
        {/* sunrise blush on peak */}
        <path d="M640 380 L700 440 L680 445 L720 450 L700 480 L680 470 Z" fill="url(#nearBlush)" opacity="0.6" />

        <path d="M420 520 L470 560 L450 575 L510 570 L520 620 L470 605 L440 590 Z" fill="url(#nearSnow)" opacity="0.9" />
        <path d="M1100 620 L1140 645 L1120 660 L1180 655 L1220 680 L1170 670 L1140 660 Z" fill="url(#nearSnow)" opacity="0.9" />
      </svg>

      {/* A thin wandering trail on the peak */}
      <svg className="hero-layer" viewBox="0 0 1600 900" preserveAspectRatio="xMidYMax slice"
           style={{ ...bleed, transform: tTrail }}>
        <path
          d="M640 382 Q 680 420 700 460 Q 720 500 760 520 Q 800 535 820 560 Q 830 590 860 615 Q 900 650 950 680 Q 1020 720 1100 760"
          fill="none" stroke="#F5F0EB" strokeWidth="2" strokeDasharray="4 6" strokeLinecap="round" opacity="0.7"
        />
      </svg>

      {/* Foreground rocks/foreground fringe */}
      <svg className="hero-layer" viewBox="0 0 1600 900" preserveAspectRatio="xMidYMax slice"
           style={{ ...bleed, transform: tFore }}>
        <defs>
          <linearGradient id="foreGrad" x1="0" x2="0" y1="0" y2="1">
            <stop offset="0%" stopColor="#0D3044" />
            <stop offset="100%" stopColor="#0E1520" />
          </linearGradient>
        </defs>
        <path d="M-300 900 L-300 820 L120 740 L260 780 L420 720 L560 780 L720 740 L900 800 L1080 760 L1240 810 L1400 770 L1900 830 L1900 900 Z" fill="url(#foreGrad)" />
        {/* pine trees */}
        <g fill="#0E1520">
          {Array.from({ length: 22 }).map((_, i) => {
            const x = 40 + i * 73 + (i % 3) * 9;
            const baseY = 820 - Math.sin(i * 1.1) * 28;
            const h = 16 + (i % 4) * 4;
            return (
              <g key={i} transform={`translate(${x}, ${baseY})`}>
                <path d={`M0 0 L${-h * 0.45} ${h} L${h * 0.45} ${h} Z`} />
                <rect x="-1" y={h} width="2" height="3" />
              </g>
            );
          })}
        </g>
      </svg>

      {/* Vignette wash at bottom — grows with scroll so the scene dissolves into parchment */}
      <div className="absolute inset-x-0 bottom-0" style={{
        height: `${40 + p * 55}vh`,
        background: 'linear-gradient(180deg, rgba(245,240,235,0) 0%, rgba(245,240,235,0.6) 45%, #F5F0EB 100%)',
        opacity: Math.min(1, p * 1.8)
      }} />
    </div>
  );
}

function Hero({ progress }) {
  const p = progress;
  // Hold the hero text steady through most of the scroll, then ease it out
  // close to the stage end so the transition into section 1 doesn't feel empty.
  const headlineOpacity = 1 - Math.max(0, (p - 0.70) / 0.28);
  const subOpacity = 1 - Math.max(0, (p - 0.62) / 0.30);
  const headlineY = -p * 30;
  // second headline fades in as first fades out
  const secondOp = Math.max(0, Math.min(1, (p - 0.45) / 0.25));
  const secondY = (1 - secondOp) * 30;

  return (
    <section id="hero" className="hero-stage">
      <div className="hero-sticky">
        <HeroMountainScene progress={p} />

        {/* Atmospheric light wash — a soft parchment-tinted glow that sits behind the text like morning mist */}
        <div className="absolute inset-0 z-10 pointer-events-none hero-mist" style={{
          opacity: Math.max(0, 1 - p * 1.4)
        }} />

        {/* Primary headline */}
        <div className="absolute inset-0 z-20 flex items-start pt-[18vh] md:pt-[20vh]">
          <div className="max-w-[1680px] mx-auto w-full px-6 md:px-10">
            <div className="hero-text-col" style={{ opacity: headlineOpacity, transform: `translateY(${headlineY}px)` }}>
              <div className="flex items-center gap-3 mb-5">
                <span className="h-px w-8" style={{ background: 'var(--summit)' }} />
                <span className="mono-label" style={{ color: 'rgba(26,36,54,0.7)' }}>Alpine base · 47.5°N</span>
              </div>
              <h1 className="font-display hero-headline" style={{
                fontSize: 'clamp(2.75rem, 6.8vw, 6.5rem)',
                fontWeight: 700,
                lineHeight: 1.02,
                letterSpacing: '-0.02em',
                color: 'var(--abyss)',
                textWrap: 'balance'
              }}>
                Websites That Help You Climb <span className="italic font-normal" style={{ color: 'var(--summit)' }}>Higher</span>.
              </h1>
              <p className="mt-7 font-body hero-sub" style={{ fontSize: '1.1rem', lineHeight: 1.6, color: 'var(--abyss)', opacity: subOpacity, maxWidth: '40ch' }}>
                A small studio making websites for local, established businesses. One page or many. Hand-built. Quietly maintained. Never a template.
              </p>
              <div className="mt-7 flex items-center gap-6 flex-wrap" style={{ opacity: subOpacity }}>
                <PrimaryBtn>Begin a project</PrimaryBtn>
                <a href="#products" className="font-display italic hero-sub" style={{
                  fontSize: '1.05rem',
                  color: 'var(--abyss)',
                  textDecoration: 'underline',
                  textUnderlineOffset: '5px',
                  textDecorationThickness: '1.5px',
                  textDecorationColor: 'rgba(26,36,54,0.55)'
                }}>
                  See what we make
                </a>
              </div>
            </div>
          </div>
        </div>

        {/* Bottom indicator */}
        <div className="absolute bottom-8 left-0 right-0 z-20 px-6 md:px-10" style={{ opacity: Math.max(0, 1 - p * 2.2) }}>
          <div className="max-w-[1680px] mx-auto flex items-center justify-between">
            <span className="mono-label" style={{ color: 'rgba(26,36,54,0.55)' }}>01 · The climb begins</span>
            <div className="flex items-center gap-3">
              <span className="mono-label" style={{ color: 'rgba(26,36,54,0.55)' }}>Scroll</span>
              <svg width="14" height="22" viewBox="0 0 14 22" fill="none">
                <rect x="0.5" y="0.5" width="13" height="21" rx="6.5" stroke="#1A2436" strokeOpacity="0.4"/>
                <rect x="6" y="5" width="2" height="5" rx="1" fill="#1A2436">
                  <animate attributeName="y" values="5;10;5" dur="1.8s" repeatCount="indefinite"/>
                  <animate attributeName="opacity" values="1;0.3;1" dur="1.8s" repeatCount="indefinite"/>
                </rect>
              </svg>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}
window.Hero = Hero;
