// Shared primitives + global styles + nav + footer
const { useEffect, useState, useRef, useMemo, useCallback } = React;

// ---------- global styles injected once ----------
const GLOBAL_CSS = `
  .container { max-width: 1280px; margin: 0 auto; padding: 0 32px; position: relative; z-index: 2; }
  @media (max-width: 720px){ .container { padding: 0 20px; } }

  .section { position: relative; padding: 120px 0; border-top: 1px solid var(--line); }
  .section--tight { padding: 80px 0; }
  @media (max-width: 720px){ .section { padding: 80px 0; } }

  .eyebrow {
    font-family: var(--mono);
    font-size: 11px;
    font-weight: 500;
    letter-spacing: .12em;
    text-transform: uppercase;
    color: var(--ink-3);
    display: inline-flex;
    align-items: center;
    gap: 10px;
  }
  .eyebrow::before {
    content: "";
    width: 6px; height: 6px; border-radius: 50%;
    background: var(--accent);
    box-shadow: 0 0 0 4px color-mix(in oklab, var(--accent) 25%, transparent);
  }

  .display {
    font-family: var(--serif);
    font-weight: 400;
    font-variation-settings: "opsz" 144, "SOFT" 30;
    letter-spacing: -0.035em;
    line-height: 0.92;
    color: var(--ink);
    margin: 0;
  }
  .display--xl { font-size: clamp(56px, 11vw, 168px); }
  .display--lg { font-size: clamp(44px, 7vw, 96px); }
  .display--md { font-size: clamp(32px, 4.4vw, 64px); }
  .display em, .italic-em {
    font-family: var(--serif-italic);
    font-style: italic;
    font-weight: 400;
  }

  .label {
    font-family: var(--mono);
    font-size: 12px;
    color: var(--ink-3);
    letter-spacing: .04em;
  }

  .btn {
    display: inline-flex; align-items: center; gap: 10px;
    padding: 14px 22px;
    border-radius: 999px;
    font-family: var(--mono);
    font-size: 13px;
    font-weight: 500;
    letter-spacing: .02em;
    transition: transform .2s ease, background .2s ease, color .2s ease, border-color .2s ease;
    border: 1px solid var(--ink);
    background: var(--ink);
    color: var(--bg);
    cursor: pointer;
  }
  .btn:hover { transform: translateY(-2px); background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
  .btn--ghost { background: transparent; color: var(--ink); }
  .btn--ghost:hover { background: var(--ink); color: var(--bg); }
  .btn .arrow { transition: transform .2s ease; }
  .btn:hover .arrow { transform: translateX(3px); }

  /* magnetic underline link */
  .link {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    color: var(--ink);
  }
  .link::after {
    content: "";
    position: absolute;
    left: 0; right: 0; bottom: -2px;
    height: 1px;
    background: currentColor;
    transform-origin: right;
    transform: scaleX(1);
    transition: transform .35s cubic-bezier(.7,0,.2,1);
  }
  .link:hover::after { transform-origin: left; transform: scaleX(0); animation: linkSweep .7s .15s cubic-bezier(.7,0,.2,1) forwards; }
  @keyframes linkSweep { from { transform-origin: left; transform: scaleX(0); } to { transform: scaleX(1); } }

  /* reveal on scroll */
  .reveal { opacity: 0; transform: translateY(18px); transition: opacity .9s cubic-bezier(.2,.6,.2,1), transform .9s cubic-bezier(.2,.6,.2,1); }
  .reveal.in { opacity: 1; transform: none; }
  .reveal.delay-1 { transition-delay: .08s; }
  .reveal.delay-2 { transition-delay: .16s; }
  .reveal.delay-3 { transition-delay: .24s; }
  .reveal.delay-4 { transition-delay: .32s; }

  .hairline { height: 1px; background: var(--line); }

  /* terminal */
  .term {
    background: #14130F;
    color: #E9E4D8;
    border-radius: 14px;
    font-family: var(--mono);
    font-size: 13px;
    line-height: 1.55;
    overflow: hidden;
    box-shadow:
      0 1px 0 rgba(255,255,255,0.04) inset,
      0 30px 60px -30px rgba(0,0,0,0.35),
      0 8px 24px -8px rgba(0,0,0,0.25);
  }
  .term__bar {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 14px;
    background: #1B1A15;
    border-bottom: 1px solid #2C2A23;
  }
  .term__dots { display: flex; gap: 6px; }
  .term__dots span { width: 11px; height: 11px; border-radius: 50%; background: #3a382f; }
  .term__title { color: #8a8576; font-size: 11px; letter-spacing: .04em; margin-left: 6px; }
  .term__body { padding: 16px 18px 20px; }
  .term__line { white-space: pre-wrap; }
  .term__prompt { color: #FF5B22; }
  .term__prompt-2 { color: #6FB1FF; }
  .term__muted { color: #8a8576; }
  .term__ok { color: #6CD499; }
  .term__warn { color: #E5B96A; }
  .term__cursor {
    display: inline-block;
    width: 8px; height: 14px;
    background: #E9E4D8;
    vertical-align: -2px;
    animation: blink 1s steps(2) infinite;
    margin-left: 2px;
  }
  @keyframes blink { 50% { opacity: 0; } }

  /* nav */
  .nav {
    position: fixed; top: 18px; left: 0; right: 0;
    z-index: 100;
    display: flex; justify-content: center;
    pointer-events: none;
  }
  .nav__inner {
    pointer-events: auto;
    display: flex; align-items: center; gap: 4px;
    padding: 6px;
    background: color-mix(in oklab, var(--bg) 78%, transparent);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--line-2);
    border-radius: 999px;
    box-shadow: 0 10px 30px -15px rgba(0,0,0,0.18);
  }
  .nav__brand {
    padding: 8px 14px;
    font-family: var(--mono);
    font-weight: 700;
    font-size: 12px;
    letter-spacing: .02em;
    display: flex; align-items: center; gap: 8px;
    border-right: 1px solid var(--line-2);
    margin-right: 4px;
  }
  .nav__brand .blip { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: pulse 2s infinite; }
  @keyframes pulse {
    0%, 100% { box-shadow: 0 0 0 0 color-mix(in oklab, var(--accent) 60%, transparent); }
    50% { box-shadow: 0 0 0 6px transparent; }
  }
  .nav__link {
    padding: 8px 12px;
    font-family: var(--mono);
    font-size: 12px;
    color: var(--ink-2);
    border-radius: 999px;
    transition: background .2s ease, color .2s ease;
  }
  .nav__link:hover { background: var(--ink); color: var(--bg); }
  .nav__cta {
    padding: 8px 14px;
    background: var(--ink);
    color: var(--bg);
    font-family: var(--mono);
    font-size: 12px;
    border-radius: 999px;
    margin-left: 4px;
    transition: background .2s ease;
  }
  .nav__cta:hover { background: var(--accent); color: var(--accent-ink); }
  @media (max-width: 760px){
    .nav__link { display: none; }
  }

  /* footer */
  .footer {
    position: relative;
    z-index: 2;
    border-top: 1px solid var(--line);
    padding: 80px 0 32px;
  }
  .footer__big {
    font-family: var(--serif);
    font-size: clamp(64px, 14vw, 220px);
    line-height: .9;
    letter-spacing: -0.04em;
    margin: 0;
  }
  .footer__row {
    display: flex; justify-content: space-between; align-items: end;
    gap: 24px; flex-wrap: wrap;
    margin-top: 56px;
    padding-top: 24px;
    border-top: 1px solid var(--line);
    font-family: var(--mono);
    font-size: 12px;
    color: var(--ink-3);
  }
  .footer__row a:hover { color: var(--ink); }

  /* marquee */
  .marquee {
    overflow: hidden;
    white-space: nowrap;
    -webkit-mask-image: linear-gradient(90deg, transparent, #000 8%, #000 92%, transparent);
            mask-image: linear-gradient(90deg, transparent, #000 8%, #000 92%, transparent);
  }
  .marquee__track {
    display: inline-flex; gap: 56px;
    animation: marqueeScroll 38s linear infinite;
    will-change: transform;
  }
  @keyframes marqueeScroll { from { transform: translateX(0); } to { transform: translateX(-50%); } }
  .marquee__item {
    display: inline-flex; align-items: center; gap: 16px;
    font-family: var(--serif);
    font-size: clamp(36px, 6vw, 80px);
    letter-spacing: -0.03em;
  }
  .marquee__star { color: var(--accent); }

  /* card */
  .card {
    background: var(--bg);
    border: 1px solid var(--line-2);
    border-radius: var(--radius);
    transition: transform .35s cubic-bezier(.2,.6,.2,1), box-shadow .35s ease, border-color .2s ease;
  }
  .card:hover {
    transform: translateY(-3px);
    border-color: var(--ink-2);
    box-shadow: 0 24px 50px -30px rgba(0,0,0,0.25);
  }

  /* form */
  .input, .textarea {
    width: 100%;
    background: transparent;
    border: 0;
    border-bottom: 1px solid var(--line-2);
    padding: 14px 0 12px;
    font: inherit;
    color: var(--ink);
    outline: none;
    transition: border-color .2s ease;
  }
  .input::placeholder, .textarea::placeholder { color: var(--ink-3); }
  .input:focus, .textarea:focus { border-color: var(--ink); }
  .field-label { display: flex; justify-content: space-between; align-items: baseline; }

  /* status pill */
  .pill {
    display: inline-flex; align-items: center; gap: 6px;
    font-family: var(--mono);
    font-size: 11px;
    padding: 4px 10px;
    border-radius: 999px;
    border: 1px solid var(--line-2);
    color: var(--ink-2);
  }
  .pill .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--good); animation: pulse 2s infinite; }

  /* corner ticks */
  .ticks { position: absolute; pointer-events: none; }
  .ticks span {
    position: absolute; width: 8px; height: 8px;
    border-color: var(--ink-2);
    border-style: solid; border-width: 0;
  }

  /* hover lift utility */
  .lift { transition: transform .25s cubic-bezier(.2,.6,.2,1); }
  .lift:hover { transform: translateY(-2px); }

  /* ═══════════════════════════════════════════════
     RESPONSIVE — TABLET (≤1024px)
     ═══════════════════════════════════════════════ */
  @media (max-width: 1024px) {
    /* hero */
    .hero-grid { grid-template-columns: 1fr !important; gap: 40px !important; }
    .hero-stats { gap: 24px !important; }

    /* section heads */
    .section-head-grid { grid-template-columns: 1fr !important; gap: 24px !important; }

    /* about */
    .about-grid { grid-template-columns: 1fr !important; gap: 40px !important; }

    /* services */
    .services-grid { grid-template-columns: 1fr !important; }

    /* process */
    .step-grid { grid-template-columns: 80px 1fr !important; }
    .step-deliverables { grid-column: 1 / -1 !important; margin-top: 16px !important; flex-direction: row !important; flex-wrap: wrap !important; }

    /* work demos */
    .demo-grid { grid-template-columns: 1fr !important; gap: 32px !important; }
    .demo-grid > div { order: unset !important; }

    /* pricing */
    .pricing-grid { grid-template-columns: 1fr !important; }

    /* writing */
    .post-grid { grid-template-columns: auto auto 1fr auto auto !important; gap: 12px !important; }

    /* contact */
    .contact-grid { grid-template-columns: 1fr !important; gap: 40px !important; }
  }

  /* ═══════════════════════════════════════════════
     RESPONSIVE — MOBILE (≤720px)
     ═══════════════════════════════════════════════ */
  @media (max-width: 720px) {
    /* typography scale down */
    .display--xl { font-size: clamp(36px, 10vw, 56px) !important; }
    .display--lg { font-size: clamp(30px, 8vw, 44px) !important; }
    .display--md { font-size: clamp(26px, 6vw, 32px) !important; }

    /* tighter sections */
    .section { padding: 64px 0 !important; }

    /* hero */
    .hero-grid { gap: 32px !important; margin-top: 32px !important; margin-bottom: 32px !important; }
    .hero-top { flex-direction: column !important; gap: 12px !important; align-items: flex-start !important; }
    .hero-stats { flex-direction: column !important; gap: 20px !important; }
    .hero-bottom { flex-direction: column !important; align-items: flex-start !important; gap: 16px !important; }

    /* about card */
    .about-card { padding: 32px 24px 28px !important; }

    /* process */
    .step-grid { grid-template-columns: 1fr !important; gap: 16px !important; }
    .step-number { font-size: 40px !important; }
    .step-title { font-size: 28px !important; }
    .step-subtitle { font-size: 16px !important; display: block !important; margin-top: 4px !important; }

    /* pricing */
    .pricing-grid { gap: 16px !important; }
    .pricing-note { flex-direction: column !important; text-align: center !important; }

    /* writing posts */
    .post-grid {
      grid-template-columns: 1fr !important;
      gap: 8px !important;
      padding: 20px 0 !important;
    }
    .post-grid .post-date { display: none !important; }
    .post-grid .post-read { display: none !important; }
    .post-grid .post-arrow { display: none !important; }

    /* booking calendar — 5-col to 3-col */
    .booking-cal-grid { grid-template-columns: repeat(3, 1fr) !important; }

    /* footer */
    .footer__big { font-size: clamp(40px, 12vw, 64px) !important; }
    .footer__row { flex-direction: column !important; align-items: flex-start !important; }

    /* hero buttons stack */
    .hero-ctas { flex-direction: column !important; width: 100% !important; }
    .hero-ctas .btn { width: 100% !important; justify-content: center !important; }

    /* nav mobile tweaks */
    .nav__inner { padding: 4px !important; }
    .nav__brand { font-size: 11px !important; padding: 6px 10px !important; }
    .nav__cta { font-size: 11px !important; padding: 6px 10px !important; }
  }

  /* ═══════════════════════════════════════════════
     RESPONSIVE — SMALL MOBILE (≤480px)
     ═══════════════════════════════════════════════ */
  @media (max-width: 480px) {
    .container { padding: 0 16px !important; }
    .display--xl { font-size: clamp(28px, 9vw, 40px) !important; }
    .about-grid { gap: 28px !important; }
    .about-detail-grid { grid-template-columns: 1fr !important; gap: 20px !important; }
    .services-card-inner { grid-template-columns: 1fr !important; }
    .booking-cal-grid { grid-template-columns: repeat(3, 1fr) !important; }
  }
`;

function injectGlobalCSS() {
  if (document.getElementById('hadi-global-css')) return;
  const s = document.createElement('style');
  s.id = 'hadi-global-css';
  s.textContent = GLOBAL_CSS;
  document.head.appendChild(s);
}

// IntersectionObserver hook for reveal-on-scroll
function useReveal(threshold = 0.15) {
  const ref = useRef(null);
  const [shown, setShown] = useState(false);
  useEffect(() => {
    if (!ref.current) return;
    if (shown) return;
    const obs = new IntersectionObserver((entries) => {
      entries.forEach(e => { if (e.isIntersecting) { setShown(true); obs.disconnect(); } });
    }, { threshold });
    obs.observe(ref.current);
    return () => obs.disconnect();
  }, [shown, threshold]);
  return [ref, shown];
}

// Eyebrow + title block
function SectionHead({ kicker, title, lede, alignRight = false, id }) {
  const [ref, shown] = useReveal();
  return (
    <div ref={ref} className="container section-head-grid" style={{
      display: 'grid',
      gridTemplateColumns: 'minmax(0, 1fr) minmax(0, 1.1fr)',
      gap: 48,
      alignItems: 'end',
      marginBottom: 56,
    }}>
      <div className={`reveal ${shown ? 'in' : ''}`}>
        {id && <div className="label" style={{ marginBottom: 24 }}>// {id}</div>}
        <span className="eyebrow">{kicker}</span>
      </div>
      <div className={`reveal delay-1 ${shown ? 'in' : ''}`}>
        <h2 className="display display--md" style={{ marginBottom: lede ? 24 : 0 }}
            dangerouslySetInnerHTML={{ __html: title }} />
        {lede && <p style={{
          maxWidth: 560,
          color: 'var(--ink-2)',
          fontSize: 18,
          lineHeight: 1.5,
          textWrap: 'pretty',
        }}>{lede}</p>}
      </div>
    </div>
  );
}

function Reveal({ children, delay = 0, as: As = 'div', ...rest }) {
  const [ref, shown] = useReveal();
  return (
    <As ref={ref} className={`reveal ${delay ? `delay-${delay}` : ''} ${shown ? 'in' : ''} ${rest.className || ''}`} {...rest}>
      {children}
    </As>
  );
}

// Nav
function Nav() {
  const links = [
    ['what i do', '#services'],
    ['how it works', '#process'],
    ['examples', '#work'],
    ['pricing', '#pricing'],
    ['notes', '#writing'],
    ['about', '#about'],
  ];
  return (
    <nav className="nav">
      <div className="nav__inner">
        <a href="#top" className="nav__brand"><span className="blip"></span>hadi.systems</a>
        {links.map(([t, h]) => (
          <a key={h} href={h} className="nav__link">{t}</a>
        ))}
        <a href="#booking" className="nav__cta">free 20-min call →</a>
      </div>
    </nav>
  );
}

// Footer
function Footer() {
  return (
    <footer className="footer">
      <div className="container">
        <Reveal>
          <div className="eyebrow" style={{ marginBottom: 24 }}>// last call</div>
          <h2 className="footer__big">
            still<br/>
            <span className="italic">reading?</span>
          </h2>
          <p style={{ marginTop: 32, maxWidth: 480, fontSize: 18, lineHeight: 1.5, color: 'var(--ink-2)', textWrap: 'pretty' }}>
            book the call. twenty minutes. free. you walk away with a real plan whether we work together or not.
          </p>
          <a href="#booking" className="btn" style={{ marginTop: 24 }}>book it now →</a>
        </Reveal>
        <div className="footer__row">
          <div>© {new Date().getFullYear()} hadi · a one-person studio · made with care</div>
          <div style={{ display: 'flex', gap: 24 }}>
            <a href="mailto:hello@hadi.systems" className="link">email</a>
            <a href="https://x.com/hadi" target="_blank" className="link">x / twitter</a>
            <a href="https://linkedin.com/in/hadi" target="_blank" className="link">linkedin</a>
            <a href="#booking" className="link">book a call</a>
          </div>
        </div>
      </div>
    </footer>
  );
}

// Marquee
function Marquee({ items }) {
  const doubled = [...items, ...items];
  return (
    <div className="marquee" style={{ padding: '32px 0', borderTop: '1px solid var(--line)', borderBottom: '1px solid var(--line)' }}>
      <div className="marquee__track">
        {doubled.map((t, i) => (
          <span key={i} className="marquee__item">
            {t}
            <svg width="36" height="36" viewBox="0 0 24 24" className="marquee__star">
              <path fill="currentColor" d="M12 2 L13.6 9.4 L21 11 L13.6 12.6 L12 20 L10.4 12.6 L3 11 L10.4 9.4 Z"/>
            </svg>
          </span>
        ))}
      </div>
    </div>
  );
}

Object.assign(window, {
  injectGlobalCSS, useReveal, SectionHead, Reveal, Nav, Footer, Marquee,
});
