// ── Nav + Hero ────────────────────────────────────────────────────────────

const PHONE_DISPLAY = '0511 / 38 47 20';
const PHONE_HREF    = 'tel:+4951138 4720';

function Nav() {
  const t     = useT();
  const [solid, setSolid] = React.useState(false);
  const [open,  setOpen]  = React.useState(false);

  React.useEffect(() => {
    const onScroll = () => setSolid(window.scrollY > 40);
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  const linkColor = solid ? '#1A1A1A' : 'rgba(255,255,255,0.92)';

  return (
    <header
      className="fixed top-0 inset-x-0 z-50 transition-all duration-500"
      style={{
        background: solid ? '#ffffff' : 'transparent',
        boxShadow:  solid ? '0 1px 24px rgba(0,0,0,0.08)' : 'none',
      }}
    >
      <div className="mx-auto max-w-[1240px] px-6 md:px-10">
        <div
          className="flex items-center justify-between"
          style={{ height: solid ? '70px' : '84px', transition: 'height 400ms ease' }}
        >
          <Logo light={!solid} />

          {/* desktop nav */}
          <nav className="hidden md:flex items-center gap-[28px]">
            {t.nav.links.map((l) => (
              <a
                key={l.href}
                href={l.href}
                className="font-sans font-medium text-[15px] transition-colors duration-200"
                style={{ color: linkColor }}
                onMouseEnter={(e) => (e.currentTarget.style.color = '#0D9488')}
                onMouseLeave={(e) => (e.currentTarget.style.color = linkColor)}
              >
                {l.label}
              </a>
            ))}
            <a
              href={PHONE_HREF}
              className="flex items-center gap-2 font-sans font-bold text-[15px] text-accent transition-transform duration-200 hover:scale-[1.03]"
            >
              <PhoneGlyph size={16} />
              {PHONE_DISPLAY}
            </a>
          </nav>

          {/* mobile hamburger */}
          <button
            className="md:hidden flex flex-col gap-[5px] p-2 -mr-2"
            aria-label={t.nav.menuOpen}
            onClick={() => setOpen((v) => !v)}
          >
            {[0, 1, 2].map((i) => (
              <span
                key={i}
                className="block h-[2px] w-[24px] rounded-full transition-all duration-300"
                style={{
                  background: solid || open ? '#1A1A1A' : '#ffffff',
                  transform:
                    open
                      ? i === 0 ? 'translateY(7px) rotate(45deg)'
                      : i === 2 ? 'translateY(-7px) rotate(-45deg)'
                      : 'scaleX(0)'
                      : 'none',
                  opacity: open && i === 1 ? 0 : 1,
                }}
              />
            ))}
          </button>
        </div>
      </div>

      {/* mobile drawer */}
      <div
        className="md:hidden overflow-hidden bg-white"
        style={{
          maxHeight:  open ? '420px' : '0px',
          boxShadow:  open ? '0 16px 30px rgba(0,0,0,0.10)' : 'none',
          transition: 'max-height 420ms cubic-bezier(0.22,1,0.36,1)',
        }}
      >
        <nav className="flex flex-col px-6 py-3">
          {t.nav.links.map((l) => (
            <a
              key={l.href}
              href={l.href}
              onClick={() => setOpen(false)}
              className="font-sans font-medium text-[17px] text-ink py-[14px] border-b border-slate-100"
            >
              {l.label}
            </a>
          ))}
          <a
            href={PHONE_HREF}
            onClick={() => setOpen(false)}
            className="flex items-center gap-2 font-sans font-bold text-[17px] text-accent py-[18px] border-b border-slate-100"
          >
            <PhoneGlyph size={18} />
            {PHONE_DISPLAY}
          </a>
        </nav>
      </div>
    </header>
  );
}

function Hero() {
  const t = useT();
  const [offset, setOffset] = React.useState(0);

  React.useEffect(() => {
    let raf = null;
    const onScroll = () => {
      if (raf) return;
      raf = requestAnimationFrame(() => { setOffset(window.scrollY); raf = null; });
    };
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  return (
    <section id="hero" className="relative min-h-[100svh] w-full overflow-hidden flex items-center">
      {/* parallax background */}
      <div
        className="absolute inset-0 z-0"
        style={{ transform: `translateY(${offset * 0.28}px) scale(1.08)`, willChange: 'transform' }}
      >
        <img
          src="hero.png"
          alt="Klempner beim Löten von Kupferrohren"
          style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'center' }}
        />
      </div>

      <div className="absolute inset-0 z-[1]" style={{ background: 'rgba(10,20,40,0.55)' }} />
      <div
        className="absolute inset-0 z-[1]"
        style={{ background: 'linear-gradient(90deg, rgba(10,20,40,0.55) 0%, rgba(10,20,40,0.15) 60%, transparent 100%)' }}
      />

      <div className="relative z-10 mx-auto max-w-[1240px] w-full px-6 md:px-10" style={{ paddingTop: '96px' }}>
        <div className="max-w-[720px]">
          <Reveal>
            <Eyebrow tone="light">{t.hero.eyebrow}</Eyebrow>
          </Reveal>
          <Reveal delay={90}>
            <h1
              className="font-display font-bold text-white mt-[22px]"
              style={{ fontSize: 'clamp(46px, 7.4vw, 92px)', lineHeight: 1.02, letterSpacing: '-0.025em' }}
            >
              {t.hero.headingLine1}<br className="hidden sm:block" />{t.hero.headingLine2}
            </h1>
          </Reveal>
          <Reveal delay={170}>
            <p className="font-sans text-white/85 mt-[26px] max-w-[520px]" style={{ fontSize: '19px', lineHeight: 1.6 }}>
              {t.hero.subline}
            </p>
          </Reveal>
          <Reveal delay={250}>
            <div className="flex flex-col sm:flex-row gap-[14px] mt-[38px]">
              <a
                href={PHONE_HREF}
                className="inline-flex items-center justify-center gap-[10px] font-sans font-bold text-white rounded-md transition-transform duration-200 hover:scale-[1.02]"
                style={{ background: '#0D9488', padding: '17px 30px', fontSize: '17px', boxShadow: '0 10px 30px rgba(13,148,136,0.35)' }}
              >
                <PhoneGlyph size={18} />
                {t.hero.ctaPrimary} — {PHONE_DISPLAY}
              </a>
              <a
                href="#leistungen"
                className="inline-flex items-center justify-center font-sans font-semibold text-white rounded-sm transition-colors duration-200"
                style={{ border: '1.5px solid rgba(255,255,255,0.55)', padding: '17px 28px', fontSize: '17px' }}
                onMouseEnter={(e) => { e.currentTarget.style.background = 'rgba(255,255,255,0.12)'; }}
                onMouseLeave={(e) => { e.currentTarget.style.background = 'transparent'; }}
              >
                {t.hero.ctaSecondary}
              </a>
            </div>
          </Reveal>
        </div>
      </div>

      <a
        href="#leistungen"
        className="absolute left-1/2 -translate-x-1/2 text-white/70 hover:text-white transition-colors"
        style={{ bottom: '30px', animation: 'wbob 2s ease-in-out infinite' }}
        aria-label={t.hero.scrollLabel}
      >
        <ArrowDown size={30} />
      </a>
    </section>
  );
}

Object.assign(window, { Nav, Hero, PHONE_DISPLAY, PHONE_HREF });
