/* HerBalance Website UI Kit — JSX components
 * Loaded with: <script type="text/babel" src="components.jsx"></script>
 * Components are exported to window.* for use across babel scripts.
 */

const Icon = ({ name, size = 20, color, stroke = 1.75, ...rest }) => {
  React.useEffect(() => { if (window.lucide) window.lucide.createIcons(); });
  return <i data-lucide={name} style={{ width: size, height: size, color, strokeWidth: stroke }} {...rest} />;
};

const Button = ({ variant = "primary", size, children, onClick, as: As = "button", ...rest }) => {
  const cls = `hb-btn hb-btn--${variant}${size === "lg" ? " hb-btn--lg" : ""}`;
  return <As className={cls} onClick={onClick} {...rest}>{children}</As>;
};

const Nav = ({ cartCount = 0, onOpenCart }) => {
  const [stuck, setStuck] = React.useState(false);
  React.useEffect(() => {
    const onScroll = () => setStuck(window.scrollY > 8);
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);
  return (
    <nav className={`hb-nav${stuck ? " is-stuck" : ""}`}>
      <div className="hb-container">
        <div className="hb-nav__inner">
          <a className="hb-nav__brand" href="/">HerBalance</a>
          <div className="hb-nav__links">
            <a className="hb-nav__link" href="/product/">Shop</a>
            <a className="hb-nav__link" href="#science">The Science</a>
            <a className="hb-nav__link" href="#testimonials">Real women</a>
            <a className="hb-nav__link" href="#guarantee">60-Day Promise</a>
            <button className="hb-nav__cart" onClick={onOpenCart}>
              <Icon name="shopping-bag" size={16} />
              Cart {cartCount > 0 && <span style={{background:"var(--hb-pink)",color:"#fff",borderRadius:999,padding:"1px 8px",fontSize:12,marginLeft:4}}>{cartCount}</span>}
            </button>
          </div>
        </div>
      </div>
    </nav>
  );
};

const PhotoPlaceholder = ({ label, description, ratio = "4 / 5", style }) => (
  <div className="hb-hero__visual" style={{ aspectRatio: ratio, ...style }}>
    <div className="hb-photo-placeholder">
      <div className="hb-photo-placeholder__label">{label}</div>
      <div className="hb-photo-placeholder__desc">"{description}"</div>
    </div>
  </div>
);

const Hero = ({ onCta }) => (
  <section className="hb-hero" id="top">
    <div className="hb-container">
      <div className="hb-hero__layout">
        <div>
          <div className="hb-hero__eyebrow">Hormone-free · clinically dosed</div>
          <h1 className="hb-hero__headline">
            Same period. <em>Different day one.</em>
          </h1>
          <p className="hb-hero__lead">
            Glycine-Bound Magnesium — formulated to reduce the prostaglandins that turn your bathroom into a warzone every single month.
          </p>
          <div className="hb-hero__ctas">
            <Button variant="primary" size="lg" as="a" href="/product/">Start Your 60-Days</Button>
            <Button variant="ghost" as="a" href="#science">Read The Science</Button>
          </div>
          <div className="hb-hero__sub">Free shipping · No questions returns · Made in USA</div>
        </div>
        <image-slot
          id="hero-photo"
          class="hb-hero__visual hb-hero__visual--slot"
          shape="rounded"
          radius="24"
          placeholder="Drop the hero photograph"
        ></image-slot>
      </div>
    </div>
  </section>
);

const Pillars = () => {
  const items = [
    { n: "01", title: "Cycle comfort.", body: "Calmer cramps. Reduced prostaglandin spikes. Less of the monthly chaos." },
    { n: "02", title: "Digestive stability.", body: "Gentle on a gut already in disruption. Not a laxative. No surprise frictionless wipes." },
    { n: "03", title: "Mood and mind.", body: "Magnesium's role in nervous system regulation, sleep, and the PMS undertow." },
    { n: "04", title: "Hormone-free.", body: "No synthetic hormones. No prescription. No swapping one problem for another." },
  ];
  return (
    <section className="hb-section hb-section--alt">
      <div className="hb-container">
        <div className="hb-section__eyebrow">What it actually does</div>
        <h2 className="hb-section__title">Four things, done <em>properly.</em></h2>
        <p className="hb-section__lead">
          Magnesium glycinate, fully chelated, at a clinical dose. Formulated for a body that's already been through enough.
        </p>
        <div className="hb-pillars">
          {items.map((p) => (
            <div className="hb-pillar" key={p.n}>
              <div className="hb-pillar__num">{p.n}</div>
              <h3 className="hb-pillar__title">{p.title}</h3>
              <p className="hb-pillar__body">{p.body}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
};

const Science = () => (
  <section className="hb-section" id="science">
    <div className="hb-container">
      <div className="hb-science">
        <div>
          <div className="hb-section__eyebrow">The science, plainly</div>
          <h2 className="hb-section__title">It's not in your head. It's <em>prostaglandins.</em></h2>
          <p className="hb-section__lead">
            On day one, your body floods with a chemical called prostaglandin. It's what triggers uterine contractions — and the bowel contractions that come with them. Magnesium glycinate gets out ahead of the spike.
          </p>
          <Button variant="secondary" as="a" href="/product/">See the formula</Button>
        </div>
        <div>
          {[
            { n: "1", title: "The spike", body: "Prostaglandins surge in the uterine lining as your cycle begins. They don't stay in the uterus — they travel." },
            { n: "2", title: "The cascade", body: "The same contractions hit your gut. That's the day-one bathroom situation nobody warned you about." },
            { n: "3", title: "The intervention", body: "275mg of elemental magnesium from glycinate dampens the prostaglandin response — before the spike takes the wheel." },
          ].map((s) => (
            <div className="hb-science__step" key={s.n}>
              <div className="hb-science__step-num">{s.n}</div>
              <div>
                <h4 className="hb-science__step-title">{s.title}</h4>
                <p className="hb-science__step-body">{s.body}</p>
              </div>
            </div>
          ))}
        </div>
      </div>
    </div>
  </section>
);

const Testimonials = () => {
  const items = [
    { initial: "M", name: "Maya R.", meta: "Verified · 8 weeks in", quote: "I made it to work on day one for the first time in three years." },
    { initial: "J", name: "Jess B.", meta: "Verified · 4 months in", quote: "I cancelled my dentist and a wedding within six months. Now I keep both. That's it. That's the review." },
    { initial: "P", name: "Priya S.", meta: "Verified · 11 weeks in", quote: "I didn't know magnesium could do this. I didn't know anything could do this." },
  ];
  return (
    <section className="hb-section hb-section--cream" id="testimonials">
      <div className="hb-container">
        <div className="hb-section__eyebrow">Real women. Specific outcomes.</div>
        <h2 className="hb-section__title">Not five-star graphics. <em>Day-one reports.</em></h2>
        <p className="hb-section__lead">No incentives. No gifted bottles. Just the actual sentence she sent us.</p>
        <div className="hb-testimonials">
          {items.map((t) => (
            <div className="hb-testimonial" key={t.name}>
              <p className="hb-testimonial__quote">"{t.quote}"</p>
              <div className="hb-testimonial__who">
                <div className="hb-testimonial__avatar">{t.initial}</div>
                <div>
                  <div className="hb-testimonial__name">{t.name}</div>
                  <div className="hb-testimonial__meta">{t.meta}</div>
                </div>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
};

const Product = ({ onAdd }) => (
  <section className="hb-section" id="product">
    <div className="hb-container">
      <div className="hb-product">
        <PhotoPlaceholder
          label="Product photograph"
          description="The bottle on a bedside table. A glass of water. A folded book."
          ratio="1"
          style={{ borderRadius: 16, boxShadow: "none" }}
        />
        <div>
          <div className="hb-section__eyebrow">The flagship</div>
          <h2 className="hb-section__title" style={{ marginBottom: 16 }}>Glycine-Bound Magnesium</h2>
          <p className="hb-section__lead" style={{ marginBottom: 32 }}>
            275mg of elemental magnesium from 2,500mg of magnesium glycinate. Three vegan capsules. GMP certified. Made in the USA. No fillers. No hormones. <span className="hb-pulse-mark">Gentle on a gut already in crisis.</span>
          </p>
          <div style={{ display: "flex", gap: 8, flexWrap: "wrap", marginBottom: 32 }}>
            {["Hormone-free", "Vegan capsules", "GMP certified", "Made in USA"].map((c) => (
              <span key={c} style={{
                fontFamily: "var(--font-sans)", fontSize: 13, fontWeight: 500,
                padding: "7px 14px", borderRadius: 999,
                background: "var(--hb-pink-soft)", color: "var(--hb-purple-deep)"
              }}>{c}</span>
            ))}
          </div>
          <div style={{ display: "flex", alignItems: "baseline", gap: 12, marginBottom: 24 }}>
            <span style={{ fontFamily: "var(--font-serif)", fontSize: 44, fontWeight: 500, color: "var(--hb-purple)", letterSpacing: "-.02em" }}>$38</span>
            <span style={{ fontFamily: "var(--font-sans)", fontSize: 14, color: "var(--hb-mute)" }}>30 servings · ~$1.27/day</span>
          </div>
          <div style={{ display: "flex", gap: 16, flexWrap: "wrap" }}>
            <Button variant="primary" size="lg" onClick={onAdd}>Add to cart — $38</Button>
            <Button variant="secondary" as="a" href="#guarantee">See the promise</Button>
          </div>
        </div>
      </div>
    </div>
  </section>
);

const Guarantee = ({ onCta }) => (
  <section className="hb-section hb-section--deep" id="guarantee">
    <div className="hb-container">
      <div className="hb-guarantee">
        <div className="hb-guarantee__seal">
          <div className="hb-guarantee__seal-num">60</div>
          <div className="hb-guarantee__seal-label">Day Promise</div>
        </div>
        <div className="hb-section__eyebrow" style={{ color: "var(--hb-pink-soft)" }}>The final word</div>
        <h2 className="hb-section__title" style={{ margin: "0 auto 24px", color: "#fff" }}>
          Sixty days. <em>No questions.</em><br/>No return required.
        </h2>
        <p className="hb-section__lead" style={{ margin: "0 auto 40px", color: "rgba(255,255,255,.78)", maxWidth: "52ch" }}>
          Take it for two cycles. If it doesn't change your day one — keep the bottle, get a full refund, and we move on. We're only willing to sell you something we're willing to back.
        </p>
        <Button variant="primary" size="lg" onClick={onCta}>Get yours risk free</Button>
      </div>
    </div>
  </section>
);

const FAQ = () => {
  const items = [
    { q: "How is this different from drugstore magnesium?", a: "Most drugstore magnesium is oxide — cheap, poorly absorbed, and famous for sending people straight back to the bathroom. Ours is fully chelated glycinate. It's clinically dosed, gentle, and actually gets where it needs to go." },
    { q: "When will I feel it?", a: "Most women feel a difference by their second cycle. The 60-day promise covers two full cycles for that exact reason. Keep taking it daily — it works upstream of the spike, not after it lands." },
    { q: "Is there anything in here I should know about?", a: "Three ingredients. Magnesium glycinate, a vegan capsule, rice flour. No fillers, no hormones, no synthetic dyes, no surprises." },
    { q: "What if it doesn't work for me?", a: "You email us within 60 days, we refund you in full. You keep the bottle. We do not require a return because we do not want you spending another minute on this." },
  ];
  const [open, setOpen] = React.useState(0);
  return (
    <section className="hb-section hb-section--tight" id="faq">
      <div className="hb-container">
        <div style={{ textAlign: "center", marginBottom: 48 }}>
          <div className="hb-section__eyebrow">Honest answers</div>
          <h2 className="hb-section__title" style={{ margin: "0 auto" }}>The questions <em>she actually asks.</em></h2>
        </div>
        <div className="hb-faq">
          {items.map((it, i) => (
            <div className={`hb-faq__item${open === i ? " is-open" : ""}`} key={i}>
              <button className="hb-faq__q" onClick={() => setOpen(open === i ? -1 : i)}>
                <span>{it.q}</span>
                <span className="hb-faq__icon"><Icon name="plus" size={22} stroke={1.75} /></span>
              </button>
              <div className="hb-faq__a">{it.a}</div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
};

const EmailCapture = () => {
  const [val, setVal] = React.useState("");
  const [done, setDone] = React.useState(false);
  return (
    <section className="hb-section hb-section--alt hb-section--tight">
      <div className="hb-container hb-container--narrow" style={{ textAlign: "center" }}>
        <div className="hb-section__eyebrow">Letters, not blasts</div>
        <h2 className="hb-section__title" style={{ margin: "0 auto 20px" }}>One letter a month. <em>That's the deal.</em></h2>
        <p className="hb-section__lead" style={{ margin: "0 auto 36px" }}>
          The science we're reading, the questions we're getting, the women we're learning from. Unsubscribe in one click — we will not be offended.
        </p>
        {done ? (
          <p style={{ fontFamily: "var(--font-serif)", fontSize: 22, color: "var(--hb-purple)" }}>You're in. Check your inbox.</p>
        ) : (
          <form className="hb-email" style={{ margin: "0 auto" }} onSubmit={(e) => { e.preventDefault(); if (val) setDone(true); }}>
            <input type="email" placeholder="you@everywhere.com" value={val} onChange={(e) => setVal(e.target.value)} required />
            <Button variant="primary">Send it</Button>
          </form>
        )}
      </div>
    </section>
  );
};

const Footer = () => (
  <footer className="hb-footer">
    <div className="hb-container">
      <div className="hb-footer__top">
        <div>
          <div className="hb-footer__brand">HerBalance</div>
          <p className="hb-footer__tag">For the parts of being a woman that nobody talks about. Hormone-free. Clinically dosed. Backed for sixty days.</p>
        </div>
        <div className="hb-footer__col">
          <h5>Shop</h5>
          <ul>
            <li><a href="/product/">Glycine-Bound Magnesium</a></li>
            <li><a href="/product/">Subscribe & save</a></li>
            <li><a href="/refund/">Refund policy</a></li>
          </ul>
        </div>
        <div className="hb-footer__col">
          <h5>Learn</h5>
          <ul>
            <li><a href="#science">The Science</a></li>
            <li><a href="#testimonials">Real stories</a></li>
            <li><a href="#faq">Questions</a></li>
          </ul>
        </div>
        <div className="hb-footer__col">
          <h5>Company</h5>
          <ul>
            <li><a href="#">Our Story</a></li>
            <li><a href="#">Contact</a></li>
            <li><a href="#">Press</a></li>
          </ul>
        </div>
      </div>
      <div className="hb-footer__bottom">
        <span>© HerBalance 2026 · Made in USA · GMP certified</span>
        <span>Privacy · Terms · Accessibility</span>
      </div>
    </div>
  </footer>
);

const CartDrawer = ({ open, onClose, items, onInc, onDec, onRemove }) => {
  const subtotal = items.reduce((s, it) => s + it.qty * it.price, 0);
  return (
    <div className={`hb-cart-drawer${open ? " is-open" : ""}`}>
      <div className="hb-cart-drawer__scrim" onClick={onClose}></div>
      <aside className="hb-cart-drawer__panel">
        <div className="hb-cart-drawer__header">
          <div className="hb-cart-drawer__title">Your bottle</div>
          <button className="hb-cart-drawer__close" onClick={onClose}><Icon name="x" size={24} /></button>
        </div>
        <div className="hb-cart-drawer__body">
          {items.length === 0 ? (
            <p style={{ fontFamily: "var(--font-serif)", fontSize: 20, color: "var(--hb-purple-soft)", textAlign: "center", padding: "40px 0" }}>
              Empty for now. Day one is waiting.
            </p>
          ) : items.map((it, i) => (
            <div className="hb-cart-line" key={i}>
              <div className="hb-cart-line__img"></div>
              <div style={{ flex: 1 }}>
                <h4 className="hb-cart-line__title">{it.title}</h4>
                <div className="hb-cart-line__meta">{it.meta}</div>
                <div className="hb-cart-line__price">${(it.qty * it.price).toFixed(0)}</div>
                <div className="hb-cart-line__qty">
                  <button onClick={() => onDec(i)}>−</button>
                  <span style={{ fontFamily: "var(--font-sans)", fontWeight: 600, color: "var(--hb-purple)" }}>{it.qty}</span>
                  <button onClick={() => onInc(i)}>+</button>
                  <button onClick={() => onRemove(i)} style={{ marginLeft: 12, border: "none", background: "transparent", color: "var(--hb-mute)", cursor: "pointer", fontSize: 12, textDecoration: "underline" }}>remove</button>
                </div>
              </div>
            </div>
          ))}
        </div>
        <div className="hb-cart-drawer__footer">
          <div className="hb-totals">
            <span className="hb-totals__label">Subtotal</span>
            <span className="hb-totals__val">${subtotal.toFixed(0)}</span>
          </div>
          <Button variant="primary" size="lg" onClick={() => alert("(Mock) Checkout flow would start here.")} style={{ width: "100%" }}>Finally. Try it.</Button>
          <p style={{ fontFamily: "var(--font-sans)", fontSize: 12, color: "var(--hb-mute)", textAlign: "center", marginTop: 12 }}>
            60-day promise applies. No return required.
          </p>
        </div>
      </aside>
    </div>
  );
};

Object.assign(window, { Icon, Button, Nav, Hero, Pillars, Science, Testimonials, Guarantee, FAQ, EmailCapture, Footer, CartDrawer, PhotoPlaceholder });
