/* =====================================================================
   Hero — Animated entrance. Mobile-responsive. Time-of-day aware.
   ===================================================================== */

const { useEffect: useEffectHero, useRef: useRefHero, useState: useStateHero } = React;

function Hero({ tod }) {
  const [time, setTime] = useStateHero(() => new Date());
  const [mounted, setMounted] = useStateHero(false);
  const [stripScrolled, setStripScrolled] = useStateHero(false);
  useEffectHero(() => {
    const t = setInterval(() => setTime(new Date()), 1000);
    const m = setTimeout(() => setMounted(true), 50);
    const onScroll = () => setStripScrolled(window.scrollY > 60);
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => { clearInterval(t); clearTimeout(m); window.removeEventListener('scroll', onScroll); };
  }, []);
  const hhmm = time.toLocaleTimeString('en-US', { hour12: false, timeZone: 'America/Chicago' });

  return (
    <section id="top" style={{ position: 'relative', minHeight: '100svh', borderBottom: '1px solid var(--border)', overflow: 'clip', background: 'var(--bg)' }}>
      <HeroShader tod={tod}/>
      <div aria-hidden style={{ position: 'absolute', inset: 0, pointerEvents: 'none',
        background: 'linear-gradient(180deg, color-mix(in oklab, var(--bg) 55%, transparent) 0%, transparent 25%, transparent 75%, color-mix(in oklab, var(--bg) 75%, transparent) 100%)'
      }}/>
      <div aria-hidden style={{ position: 'absolute', inset: 0, pointerEvents: 'none',
        background: 'linear-gradient(90deg, color-mix(in oklab, var(--bg) 55%, transparent) 0%, transparent 45%, transparent 100%)'
      }}/>
      <div aria-hidden style={{ position: 'absolute', inset: 0, pointerEvents: 'none',
        backgroundImage: 'linear-gradient(to right, color-mix(in oklab, var(--fg) 5%, transparent) 1px, transparent 1px)',
        backgroundSize: 'calc(100% / 12) 100%'
      }}/>
      <CornerMarks/>

      {/* Sticky sub-strip — rides below the nav, thins out on scroll */}
      <div className="mw-hero-strip" style={{
        position: 'sticky', top: 'var(--mw-nav-h, 57px)', zIndex: 40,
        borderBottom: '1px solid var(--border)',
        borderTop: '1px solid color-mix(in oklab, var(--border) 50%, transparent)',
        display: 'grid', gridTemplateColumns: '1fr auto 1fr',
        padding: stripScrolled ? '5px clamp(20px, 4vw, 40px)' : '12px clamp(20px, 4vw, 40px)',
        gap: 16,
        fontFamily: 'var(--font-mono)',
        fontSize: stripScrolled ? 9 : 10,
        letterSpacing: '0.16em', color: 'var(--fg-3)', textTransform: 'uppercase',
        background: 'color-mix(in oklab, var(--bg) 78%, transparent)', backdropFilter: 'blur(14px)', WebkitBackdropFilter: 'blur(14px)',
        transition: 'padding .25s var(--ease-out), font-size .25s var(--ease-out)',
      }}>
        <span>35.47°N · 97.52°W — OKLAHOMA STATE</span>
        <span style={{ color: 'var(--fg-1)', display: 'inline-flex', alignItems: 'center', gap: 8 }}>
          <span style={{ width: stripScrolled ? 5 : 6, height: stripScrolled ? 5 : 6, background: 'var(--accent)', boxShadow: '0 0 10px var(--accent)', animation: 'mwBreathe 2s infinite', transition: 'width .25s, height .25s' }}/>
          LIVE · {hhmm} CT · {tod.toUpperCase()}
        </span>
        <span style={{ textAlign: 'right' }}>SERVING OKC METRO & BEYOND</span>
      </div>

      {/* Main content */}
      <div style={{ position: 'relative', zIndex: 2, padding: 'clamp(80px, 12vh, 140px) clamp(20px, 4vw, 48px) clamp(80px, 10vh, 120px)', maxWidth: 1440, margin: '0 auto' }}>
        <div className="mw-hero-grid" style={{ display: 'grid', gridTemplateColumns: 'minmax(0,1fr) 300px', gap: 40, alignItems: 'start' }}>
          <div>
            <div style={{
              fontFamily: 'var(--font-mono)', fontSize: 11, letterSpacing: '0.14em', color: 'var(--fg-2)',
              marginBottom: 28, display: 'flex', alignItems: 'center', gap: 12, flexWrap: 'wrap',
              opacity: mounted ? 1 : 0, transform: mounted ? 'translateY(0)' : 'translateY(8px)',
              transition: 'opacity .5s, transform .5s var(--ease-out)',
            }}>
              <span style={{ width: 8, height: 8, background: 'var(--accent)', boxShadow: '0 0 16px var(--accent)', animation: 'mwBreathe 2.2s ease-in-out infinite' }}/>
              <span>OKLAHOMA'S WEBSITE STUDIO</span>
              <span style={{ opacity: .4 }}>|</span>
              <span>BOOKING SPRING 2026</span>
            </div>
            <h1 style={{
              margin: 0, fontFamily: 'var(--font-display)',
              fontSize: 'clamp(52px, 10.5vw, 160px)',
              lineHeight: 0.88, letterSpacing: '-0.05em',
              fontWeight: 500, color: 'var(--fg-1)', textWrap: 'balance',
            }}>
              <HeroLine delay={100} mounted={mounted}>A website <span style={{ fontStyle: 'italic', fontFamily: 'var(--font-editorial)', fontWeight: 400, color: 'var(--accent)' }}>your&nbsp;customers</span></HeroLine>
              <br/>
              <HeroLine delay={280} mounted={mounted}>
                actually{' '}
                <TypedVerb mounted={mounted} words={['find', 'enjoy', 'trust', 'remember', 'call']}/>
              </HeroLine>
            </h1>

            <div style={{ display: 'grid', gridTemplateColumns: '1fr auto', gap: 32, marginTop: 48, alignItems: 'end',
              opacity: mounted ? 1 : 0, transform: mounted ? 'translateY(0)' : 'translateY(20px)',
              transition: 'opacity .6s 0.5s, transform .6s var(--ease-out) 0.5s',
            }} className="mw-hero-bottom">
              <div style={{ maxWidth: 560 }}>
                <p style={{ fontSize: 'clamp(15px, 1.7vw, 19px)', lineHeight: 1.5, color: 'var(--fg-2)', margin: '0 0 12px' }}>
                  Lawn care, hydraulic shops, restaurants, real estate — whatever you do, we hand-build the website that gets you calls. Real people, real craft. You run your business.
                </p>
                <p style={{ fontSize: 14, lineHeight: 1.55, color: 'var(--fg-3)', margin: 0 }}>
                  Show up on Google. Look good on phones. Want a smart helper that talks to your customers 24/7? We can wire that in too.
                </p>
              </div>
              <div style={{ display: 'flex', gap: 10, flexWrap: 'wrap' }}>
                <MagButton variant="primary" size="lg" href="#contact">Get a free quote</MagButton>
                <MagButton variant="ghost" size="lg" href="#work" noArrow>See our work ↓</MagButton>
              </div>
            </div>
          </div>

          {/* Right panel — a statement, broken into distinct blocks */}
          <HeroAside mounted={mounted}/>
        </div>
      </div>

      <HeroTicker/>
      <style>{`
        @keyframes mwBarRise { from { transform: scaleY(0); opacity: 0 } to { transform: scaleY(1); opacity: 1 } }
        @keyframes mwBreathe { 0%,100% { opacity: 1 } 50% { opacity: .35 } }
        @keyframes mwCaretBlink { 0%, 49% { opacity: 1 } 50%, 100% { opacity: 0 } }
      `}</style>
    </section>
  );
}

/* -----------------------------------------------------------------
   Typed verb — types a word, pauses, backspaces, types the next.
   Uses the editorial italic treatment from the original second line.
   ----------------------------------------------------------------- */
function TypedVerb({ words, mounted }) {
  const [wordIdx, setWordIdx] = useStateHero(0);
  const [typed, setTyped] = useStateHero('');
  const [phase, setPhase] = useStateHero('typing'); // typing | holding | deleting

  useEffectHero(() => {
    if (!mounted) return;
    const word = words[wordIdx % words.length];
    let timer;
    if (phase === 'typing') {
      if (typed.length < word.length) {
        timer = setTimeout(() => setTyped(word.slice(0, typed.length + 1)), 95 + Math.random() * 55);
      } else {
        timer = setTimeout(() => setPhase('holding'), 1700);
      }
    } else if (phase === 'holding') {
      timer = setTimeout(() => setPhase('deleting'), 900);
    } else if (phase === 'deleting') {
      if (typed.length > 0) {
        timer = setTimeout(() => setTyped(word.slice(0, typed.length - 1)), 45);
      } else {
        timer = setTimeout(() => {
          setWordIdx((i) => (i + 1) % words.length);
          setPhase('typing');
        }, 260);
      }
    }
    return () => clearTimeout(timer);
  }, [typed, phase, wordIdx, mounted, words]);

  return (
    <span style={{
      display: 'inline-flex', alignItems: 'baseline',
      fontStyle: 'italic', fontFamily: 'var(--font-editorial)', fontWeight: 400,
      color: 'var(--fg-1)',
    }}>
      <span style={{ whiteSpace: 'pre' }}>{typed}</span>
      <span style={{
        display: 'inline-block',
        width: '0.08em', height: '0.78em',
        background: 'var(--accent)',
        marginLeft: '0.08em',
        alignSelf: 'center',
        animation: 'mwCaretBlink 1.05s steps(1) infinite',
        boxShadow: '0 0 16px color-mix(in oklab, var(--accent) 60%, transparent)',
      }}/>
    </span>
  );
}

/* -----------------------------------------------------------------
   Hero aside — reworked as a statement. Denser, layered, contrasty.
   Header band, three clearly broken-up blocks, stronger typography.
   ----------------------------------------------------------------- */
function HeroAside({ mounted }) {
  return (
    <aside className="mw-hero-aside" style={{
      position: 'relative',
      border: '1px solid var(--border-strong)',
      background: 'color-mix(in oklab, var(--bg) 82%, transparent)',
      backdropFilter: 'blur(8px)',
      WebkitBackdropFilter: 'blur(8px)',
      fontFamily: 'var(--font-mono)', color: 'var(--fg-2)',
      opacity: mounted ? 1 : 0, transform: mounted ? 'translateX(0)' : 'translateX(20px)',
      transition: 'opacity .6s 0.7s, transform .6s var(--ease-out) 0.7s',
    }}>
      {/* Header band */}
      <div style={{
        display: 'flex', justifyContent: 'space-between', alignItems: 'center',
        padding: '10px 14px',
        background: 'var(--accent)', color: 'var(--accent-ink)',
        fontSize: 10, letterSpacing: '0.18em', textTransform: 'uppercase', fontWeight: 600,
      }}>
        <span>◆ Studio Dossier</span>
        <span style={{ opacity: .75 }}>No. 04 · 26</span>
      </div>

      {/* Section 1 — Serving */}
      <AsideRow label="Serving">
        <div style={{ color: 'var(--fg-1)', fontSize: 14, lineHeight: 1.4, fontFamily: 'var(--font-display)', letterSpacing: '-0.01em', fontWeight: 500 }}>
          Oklahoma City · Edmond<br/>
          Norman · Moore · Tulsa
        </div>
        <div style={{ marginTop: 8, fontSize: 10, letterSpacing: '0.1em', color: 'var(--fg-3)', textTransform: 'uppercase' }}>
          + Nationwide, Remote
        </div>
      </AsideRow>

      {/* Section 2 — What we do (list with tick marks) */}
      <AsideRow label="What we do">
        <ul style={{ listStyle: 'none', padding: 0, margin: 0, display: 'grid', gap: 5 }}>
          {[
            'Hand-built websites',
            'Show up on Google',
            'Google Business setup',
            'Local SEO — the works',
            'Smart AI helpers',
          ].map((s, i) => (
            <li key={s} style={{ display: 'flex', alignItems: 'center', gap: 8, fontSize: 12, color: 'var(--fg-1)', letterSpacing: '0.01em' }}>
              <span style={{
                width: 14, textAlign: 'center', color: 'var(--accent)',
                fontSize: 11, fontWeight: 700,
              }}>{String(i + 1).padStart(2, '0')}</span>
              <span style={{ width: 8, height: 1, background: 'var(--border-strong)' }}/>
              {s}
            </li>
          ))}
        </ul>
      </AsideRow>

      {/* Section 3 — Capacity */}
      <AsideRow label="2026 Capacity" accent>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 10, marginBottom: 10 }}>
          <span style={{
            fontFamily: 'var(--font-display)', fontSize: 38, lineHeight: 1, fontWeight: 500,
            letterSpacing: '-0.04em', color: 'var(--fg-1)',
          }}>7</span>
          <span style={{
            fontSize: 10, letterSpacing: '0.16em', color: 'var(--fg-3)', textTransform: 'uppercase',
          }}>spots open · 17 booked</span>
        </div>
        <div style={{ display: 'flex', gap: 3 }}>
          {[...Array(24)].map((_, i) => (
            <div key={i} style={{
              flex: 1, height: 22,
              background: i < 17 ? 'var(--accent)' : 'color-mix(in oklab, var(--fg-1) 14%, transparent)',
              opacity: i < 17 ? (0.45 + i * 0.03) : 1,
              animation: mounted ? `mwBarRise .5s var(--ease-out) ${0.9 + i * 0.02}s both` : 'none',
            }}/>
          ))}
        </div>
      </AsideRow>

      {/* Footer stamp */}
      <div style={{
        borderTop: '1px solid var(--border-strong)',
        padding: '10px 14px',
        display: 'flex', justifyContent: 'space-between',
        fontSize: 9, letterSpacing: '0.2em', color: 'var(--fg-4)', textTransform: 'uppercase',
      }}>
        <span>Est. OKC 2019</span>
        <span style={{ color: 'var(--accent)' }}>● Accepting briefs</span>
      </div>
    </aside>
  );
}

function AsideRow({ label, children, accent }) {
  return (
    <div style={{
      padding: '14px 14px 16px',
      borderTop: '1px solid var(--border-strong)',
      background: accent ? 'color-mix(in oklab, var(--accent) 6%, transparent)' : 'transparent',
    }}>
      <div style={{
        display: 'flex', alignItems: 'center', gap: 8, marginBottom: 10,
      }}>
        <span style={{ width: 10, height: 1, background: 'var(--accent)' }}/>
        <span style={{
          fontSize: 9, letterSpacing: '0.22em', color: 'var(--fg-3)', textTransform: 'uppercase', fontWeight: 600,
        }}>{label}</span>
      </div>
      {children}
    </div>
  );
}

function HeroLine({ children, delay, mounted }) {
  return (
    <span style={{
      display: 'inline-block',
      opacity: mounted ? 1 : 0,
      transform: mounted ? 'translateY(0)' : 'translateY(40px)',
      filter: mounted ? 'blur(0)' : 'blur(8px)',
      transition: `opacity .75s cubic-bezier(0.2, 0.8, 0.2, 1) ${delay}ms, transform .85s cubic-bezier(0.2, 0.8, 0.2, 1) ${delay}ms, filter .7s ${delay}ms`,
    }}>{children}</span>
  );
}

function CornerMarks() {
  const Mark = ({ style, t, l, r, b }) => (
    <svg width="20" height="20" viewBox="0 0 20 20" style={{ position: 'absolute', top: t, left: l, right: r, bottom: b, zIndex: 4, ...style }}>
      <path d="M0 0 L20 0 M0 0 L0 20" stroke="var(--fg-3)" strokeWidth="1"/>
    </svg>
  );
  return (
    <>
      <Mark t={20} l={20}/>
      <Mark t={20} r={20} style={{ transform: 'scaleX(-1)' }}/>
      <Mark b={20} l={20} style={{ transform: 'scaleY(-1)' }}/>
      <Mark b={20} r={20} style={{ transform: 'scale(-1,-1)' }}/>
    </>
  );
}

function HeroTicker() {
  const items = ['LAWN CARE & LANDSCAPING', 'RESTAURANTS & CAFÉS', 'REAL ESTATE', 'CONTRACTORS & TRADES', 'AUTO & HYDRAULICS', 'HVAC & PLUMBING', 'SALONS & STUDIOS', 'RETAIL & BOUTIQUES', 'GOOGLE BUSINESS SETUP', 'LOCAL SEO', 'SMART CHATBOTS', 'FOUND ON GOOGLE', 'MOBILE FIRST', 'OKC METRO & BEYOND'];
  const row = [...items, ...items];
  return (
    <div style={{
      position: 'absolute', bottom: 0, left: 0, right: 0, zIndex: 3,
      borderTop: '1px solid var(--border)', padding: '14px 0',
      overflow: 'hidden', whiteSpace: 'nowrap',
      fontFamily: 'var(--font-mono)', fontSize: 11, letterSpacing: '0.14em', color: 'var(--fg-2)', textTransform: 'uppercase',
      background: 'color-mix(in oklab, var(--bg) 80%, transparent)', backdropFilter: 'blur(10px)',
    }}>
      <div style={{ display: 'inline-flex', gap: 40, animation: 'mwTicker 50s linear infinite' }}>
        {row.map((s, i) => (
          <span key={i} style={{ color: i % 3 === 0 ? 'var(--accent)' : 'var(--fg-2)' }}>◆ {s}</span>
        ))}
      </div>
      <style>{`@keyframes mwTicker { from{transform:translateX(0)} to{transform:translateX(-50%)} }`}</style>
    </div>
  );
}

Object.assign(window, { Hero });
