/* global React */
const { useState, useEffect, useRef } = React;

const COOKIE_IMG = (window.__resources && window.__resources.cookieHero) || "assets/cookie-hero.jpg";
const LOGO_BROWN = (window.__resources && window.__resources.logoBrown) || "assets/logo-brown.png";
const LOGO_LIGHT = (window.__resources && window.__resources.logoLight) || "assets/logo-light.png";
const COOKIE_VISUALS = [
  { position: "center 35%", scale: 1.08 },
  { position: "center 58%", scale: 1.18 },
];

function useReveal(threshold) {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const io = new IntersectionObserver(
      ([e]) => { if (e.isIntersecting) { el.classList.add("in"); io.disconnect(); } },
      { threshold: threshold || 0.07, rootMargin: "0px 0px -2% 0px" }
    );
    io.observe(el);
    return () => io.disconnect();
  }, []);
  return ref;
}

/* ── HERO ─────────────────────────────────────────────────────── */
/* The morphing Nav logo lives over this section's top center.
   Hero leaves room for it via a flex spacer, then centers the headline below. */
const INSTAGRAM_URL = "https://instagram.com/drommecookies";

function Hero({ t }) {
  return (
    <section style={{
      position: "relative",
      minHeight: "100svh",
      background: "linear-gradient(135deg, var(--cream) 0%, var(--paper) 38%, var(--cream-soft) 100%)",
      display: "flex",
      flexDirection: "column",
      paddingTop: 64,
      paddingBottom: "clamp(28px, 5vw, 56px)",
      overflow: "hidden",
    }}>
      <div style={{
        position: "absolute",
        inset: 0,
        background: "radial-gradient(circle at 72% 44%, rgba(166,123,91,0.22), transparent 34%), linear-gradient(180deg, rgba(246,241,234,0.38), rgba(139,90,60,0.08))",
        pointerEvents: "none",
      }} />
      <div style={{
        position: "absolute",
        right: "clamp(-340px, -18vw, -132px)",
        bottom: "clamp(96px, 8vw, 140px)",
        width: "clamp(340px, 40vw, 640px)",
        aspectRatio: "1 / 1",
        opacity: 0.16,
        filter: "saturate(1.16) contrast(1.04)",
        transform: "rotate(-7deg)",
        pointerEvents: "none",
      }}>
        <img
          src={COOKIE_IMG}
          alt=""
          aria-hidden="true"
          style={{
            width: "100%",
            height: "100%",
            objectFit: "cover",
            objectPosition: "center 36%",
            boxShadow: "0 44px 92px rgba(61,34,17,0.2)",
          }}
        />
      </div>

      {/* Editorial corner marks framing the logo */}
      <div style={{
        position: "absolute",
        top: "clamp(82px, 9vh, 108px)",
        left: "clamp(20px, 4vw, 64px)",
        fontFamily: "var(--mono)", fontSize: 9,
        letterSpacing: "0.26em", textTransform: "uppercase",
        color: "rgba(139,90,60,0.42)",
        zIndex: 10,
      }}>
        Lier · 59°N
      </div>
      <div style={{
        position: "absolute",
        top: "clamp(82px, 9vh, 108px)",
        right: "clamp(20px, 4vw, 64px)",
        fontFamily: "var(--mono)", fontSize: 9,
        letterSpacing: "0.26em", textTransform: "uppercase",
        color: "rgba(139,90,60,0.42)",
        zIndex: 10,
      }}>
        Est. 2026 · N° 001
      </div>

      {/* Spacer reserving vertical real estate for the fixed morphing logo */}
      <div className="hero-logo-spacer" style={{
        flex: "0 0 auto",
        height: "clamp(128px, 24vh, 292px)",
        pointerEvents: "none",
      }} />

      {/* Center stage: eyebrow + headline */}
      <div className="container" style={{
        flex: 1,
        display: "flex",
        flexDirection: "column",
        alignItems: "center",
        justifyContent: "flex-start",
        textAlign: "center",
        paddingTop: 0,
        paddingBottom: "clamp(16px, 3vh, 52px)",
      }}>
        {/* Eyebrow framed by side rules */}
        <div style={{
          display: "flex", alignItems: "center",
          gap: "clamp(12px, 2vw, 18px)",
          marginBottom: "clamp(16px, 2.4vw, 28px)",
          fontFamily: "var(--mono)", fontSize: 9,
          letterSpacing: "0.3em", textTransform: "uppercase",
          color: "rgba(139,90,60,0.52)",
        }}>
          <span style={{ display: "inline-block", width: "clamp(24px, 4vw, 44px)", height: 1, background: "currentColor" }} />
          <span>Bakt på bestilling · Lier</span>
          <span style={{ display: "inline-block", width: "clamp(24px, 4vw, 44px)", height: 1, background: "currentColor" }} />
        </div>

        <h1 style={{
          fontFamily: "var(--serif)",
          fontSize: "clamp(52px, 8.2vw, 132px)",
          lineHeight: 0.92,
          letterSpacing: "-0.034em",
          fontWeight: 400,
          margin: 0,
          color: "var(--chocolate)",
          maxWidth: "15ch",
          textShadow: "0 18px 46px rgba(122,82,51,0.12)",
        }}>
          {t.hero.h1}
          <br />
          <em style={{
            color: "var(--caramel)",
            display: "inline-block",
            fontSize: "0.58em",
            lineHeight: 0.98,
            maxWidth: "18ch",
          }}>{t.hero.h2}</em>
        </h1>
      </div>

      {/* Bottom frame: subtext + CTAs */}
      <div className="container" style={{
        display: "flex",
        justifyContent: "center",
        alignItems: "center",
        flexWrap: "wrap",
        gap: "clamp(20px, 4vw, 40px)",
        paddingTop: "clamp(24px, 3vw, 40px)",
        borderTop: "1px solid rgba(139,90,60,0.18)",
        position: "relative",
        zIndex: 2,
      }}>
        <div style={{
          display: "flex",
          gap: "clamp(18px, 2vw, 26px)",
          alignItems: "center",
          justifyContent: "center",
          flexWrap: "wrap",
        }}>
          <a
            href={INSTAGRAM_URL}
            target="_blank"
            rel="noopener noreferrer"
            style={{
              display: "inline-flex", alignItems: "center",
              padding: "14px 32px",
              background: "var(--chocolate)",
              color: "var(--paper)",
              fontFamily: "var(--sans)", fontSize: 10,
              fontWeight: 600, letterSpacing: "0.12em",
              textTransform: "uppercase",
              textDecoration: "none",
              transition: "background 0.3s ease",
            }}
            onMouseEnter={e => { e.currentTarget.style.background = "var(--chocolate-2)"; }}
            onMouseLeave={e => { e.currentTarget.style.background = "var(--chocolate)"; }}
          >
            {t.hero.cta1}
          </a>
          <a href="#menu" style={{
            display: "inline-flex", alignItems: "center", gap: 14,
            color: "var(--chocolate)",
            fontFamily: "var(--sans)", fontSize: 10,
            fontWeight: 500, letterSpacing: "0.12em",
            textTransform: "uppercase",
            textDecoration: "none",
            opacity: 0.7,
            transition: "opacity 0.3s ease",
          }}
          onMouseEnter={e => { e.currentTarget.style.opacity = "1"; }}
          onMouseLeave={e => { e.currentTarget.style.opacity = "0.7"; }}>
            {t.hero.cta2}
            <span style={{ display: "inline-block", width: 26, height: 1, background: "currentColor" }} />
          </a>
        </div>
      </div>

      <style>{`
        @media (min-width: 768px) {
          .hero-logo-spacer { height: clamp(220px, 34vh, 372px) !important; }
        }
      `}</style>
    </section>
  );
}

/* ── PRODUCTS ─────────────────────────────────────────────────── */
function Products({ t }) {
  const [active, setActive] = useState(0);
  const ref = useReveal();
  const items = t.products.items;
  const item = items[active];
  const visual = COOKIE_VISUALS[active] || COOKIE_VISUALS[0];

  return (
    <section ref={ref} id="menu" className="reveal" style={{
      background: "linear-gradient(180deg, var(--paper) 0%, var(--cream) 58%, var(--cream-soft) 100%)",
      padding: "clamp(64px, 10vw, 128px) 0 clamp(84px, 12vw, 156px)",
      scrollMarginTop: 72,
    }}>
      <div className="container">

        {/* Header */}
        <div style={{
          display: "flex",
          justifyContent: "space-between",
          alignItems: "baseline",
          flexWrap: "wrap",
          gap: 24,
          marginBottom: "clamp(36px, 5.5vw, 72px)",
          paddingBottom: "clamp(16px, 2vw, 24px)",
        }}>
          <h2 style={{
            fontFamily: "var(--serif)",
            fontSize: "clamp(42px, 6.8vw, 118px)",
            lineHeight: 0.9,
            letterSpacing: "-0.028em",
            fontWeight: 400,
            margin: 0,
            color: "var(--chocolate)",
            whiteSpace: "nowrap",
          }}>
            {t.products.title}
          </h2>
          <span style={{
            fontFamily: "var(--mono)", fontSize: 9,
            letterSpacing: "0.26em", textTransform: "uppercase",
            color: "var(--caramel-dk)",
            opacity: 0.72,
            alignSelf: "flex-end",
            paddingBottom: 4,
          }}>{t.products.eyebrow}</span>
        </div>

        {/* Editorial layout */}
        <div style={{
          display: "grid",
          gridTemplateColumns: "minmax(0, 6.4fr) minmax(320px, 3.6fr)",
          gap: "clamp(34px, 6vw, 92px)",
          alignItems: "center",
        }} className="prod-grid">

          {/* Featured display */}
          <div style={{
            position: "relative",
            aspectRatio: "5 / 4",
            overflow: "hidden",
            background: "var(--cream-soft)",
            boxShadow: "0 42px 86px rgba(61,34,17,0.18)",
          }}>
            <img
              src={COOKIE_IMG}
              alt={item.name}
              style={{
                width: "100%", height: "100%",
                objectFit: "cover",
                objectPosition: visual.position,
                transform: `scale(${visual.scale})`,
                transition: "transform 1.4s cubic-bezier(.16,.8,.2,1), object-position 1.1s ease",
                filter: active === 1 ? "saturate(1.16) contrast(1.08) brightness(0.9)" : "saturate(1.08) contrast(1.05)",
              }}
            />
            <div style={{
              position: "absolute", inset: 0,
              background: "linear-gradient(to top, rgba(61,34,17,0.72) 0%, rgba(122,82,51,0.18) 42%, rgba(246,241,234,0.05) 78%)",
              pointerEvents: "none",
            }} />
            <div style={{
              position: "absolute",
              left: "clamp(22px, 3vw, 44px)",
              right: "clamp(22px, 3vw, 44px)",
              bottom: "clamp(22px, 3vw, 44px)",
              color: "var(--paper)",
            }}>
              <div style={{
                fontFamily: "var(--mono)", fontSize: 9,
                letterSpacing: "0.26em", textTransform: "uppercase",
                opacity: 0.64, marginBottom: 12,
              }}>N° 0{active + 1} · bakt på bestilling</div>
              <div style={{
                fontFamily: "var(--serif)",
                fontSize: "clamp(46px, 7vw, 108px)",
                lineHeight: 0.88,
                letterSpacing: "-0.03em",
                marginBottom: 14,
                textShadow: "0 16px 40px rgba(61,34,17,0.36)",
              }}>{item.name}</div>
              <div style={{
                fontSize: "clamp(14px, 1.25vw, 18px)",
                opacity: 0.78,
                lineHeight: 1.65,
                fontFamily: "var(--sans)",
                fontWeight: 300,
                maxWidth: "34ch",
              }}>{item.desc}</div>
              <div style={{
                marginTop: 18,
                fontFamily: "var(--mono)", fontSize: 12,
                letterSpacing: "0.16em",
                color: "var(--paper)",
                opacity: 0.88,
              }}>{item.price} <span style={{ opacity: 0.52, fontSize: 10 }}>/ stk</span></div>
            </div>
          </div>

          {/* List */}
          <div>
            <ol style={{ listStyle: "none", margin: 0, padding: 0 }}>
              {items.map((it, i) => (
                <li
                  key={i}
                  onMouseEnter={() => setActive(i)}
                  onClick={() => setActive(i)}
                  style={{
                    borderTop: i === 0 ? "1px solid rgba(139,90,60,0.12)" : "none",
                    padding: "clamp(24px, 3vw, 38px) 0",
                    cursor: "pointer",
                    display: "flex",
                    justifyContent: "space-between",
                    alignItems: "flex-start",
                    gap: 22,
                  }}
                >
                  <div style={{ flex: 1, minWidth: 0 }}>
                    <div style={{
                      fontFamily: "var(--serif)",
                      fontSize: "clamp(42px, 5vw, 82px)",
                      lineHeight: 0.92,
                      letterSpacing: "-0.028em",
                      color: active === i ? "var(--chocolate)" : "rgba(139,90,60,0.28)",
                      transition: "color 0.45s ease",
                    }}>
                      {it.name}
                    </div>
                    <div style={{
                      maxHeight: active === i ? 60 : 0,
                      opacity: active === i ? 1 : 0,
                      overflow: "hidden",
                      fontSize: "clamp(14px, 1.1vw, 16px)",
                      fontFamily: "var(--sans)", fontWeight: 300,
                      color: "rgba(107,66,41,0.68)",
                      lineHeight: 1.68,
                      marginTop: active === i ? 14 : 0,
                      transition: "max-height 0.4s ease, opacity 0.4s ease, margin-top 0.35s ease",
                    }}>
                      {it.desc}
                    </div>
                  </div>
                  <div style={{
                    fontFamily: "var(--mono)",
                    letterSpacing: "0.14em",
                    color: active === i ? "rgba(139,90,60,0.72)" : "rgba(139,90,60,0.22)",
                    flexShrink: 0,
                    paddingTop: 10,
                    textAlign: "right",
                    transition: "color 0.45s ease",
                  }}>
                    <div style={{ fontSize: 13 }}>{it.price}</div>
                    <div style={{ fontSize: 9, opacity: 0.7, marginTop: 3 }}>/ stk</div>
                  </div>
                </li>
              ))}
              <li style={{ borderTop: "1px solid rgba(139,90,60,0.12)" }} />
            </ol>

            <a
              href={INSTAGRAM_URL}
              target="_blank"
              rel="noopener noreferrer"
              style={{
                display: "inline-flex", alignItems: "center", gap: 18,
                marginTop: "clamp(32px, 4.5vw, 56px)",
                fontFamily: "var(--sans)", fontSize: 10,
                fontWeight: 600, letterSpacing: "0.12em",
                textTransform: "uppercase", color: "var(--chocolate)",
                textDecoration: "none",
                opacity: 0.78,
                transition: "opacity 0.3s ease",
              }}
              onMouseEnter={e => { e.currentTarget.style.opacity = "1"; }}
              onMouseLeave={e => { e.currentTarget.style.opacity = "0.78"; }}
            >
              {t.products.cta}
              <span style={{ display: "inline-block", width: 30, height: 1, background: "currentColor" }} />
            </a>
          </div>
        </div>
      </div>

      <style>{`
        @media (max-width: 760px) {
          .prod-grid { grid-template-columns: 1fr !important; }
        }
      `}</style>
    </section>
  );
}

/* ── BRAND STATEMENT ──────────────────────────────────────────── */
function Brand({ t }) {
  const ref = useReveal(0.04);
  return (
    <section ref={ref} className="reveal" style={{
      background: "var(--espresso)",
      color: "var(--paper)",
      padding: "clamp(108px, 18vw, 256px) 0 clamp(96px, 15vw, 216px)",
    }}>
      <div className="container">

        {/* Mono label + rule */}
        <div style={{
          display: "flex",
          alignItems: "center",
          gap: 24,
          marginBottom: "clamp(44px, 5.5vw, 80px)",
        }}>
          <span style={{
            fontFamily: "var(--mono)", fontSize: 9,
            letterSpacing: "0.28em", textTransform: "uppercase",
            color: "rgba(250,248,244,0.3)",
            flexShrink: 0,
          }}>Drømme Cookies · Lier</span>
          <div style={{ flex: 1, height: 1, background: "rgba(250,248,244,0.1)" }} />
        </div>

        <h2 style={{
          fontFamily: "var(--serif)",
          fontSize: "clamp(52px, 9vw, 156px)",
          lineHeight: 0.88,
          letterSpacing: "-0.034em",
          fontWeight: 400,
          margin: "0 0 clamp(32px, 4vw, 60px)",
          color: "var(--paper)",
          maxWidth: "18ch",
        }}>
          <em>{t.brand.title}</em>
        </h2>

        <p style={{
          fontFamily: "var(--sans)",
          fontSize: "clamp(15px, 1.35vw, 19px)",
          lineHeight: 1.78,
          color: "rgba(250,248,244,0.54)",
          maxWidth: "46ch",
          margin: 0,
          fontWeight: 300,
          letterSpacing: "0.008em",
        }}>
          {t.brand.body}
        </p>
      </div>
    </section>
  );
}

/* ── CONTENT STRIP ────────────────────────────────────────────── */
function ContentStrip({ t }) {
  const ref = useReveal(0.04);

  const tiles = [
    { ratio: "9 / 16", label: "Reel · 01" },
    { ratio: "4 / 5",  label: "Foto · 02" },
    { ratio: "1 / 1",  label: "Foto · 03" },
    { ratio: "4 / 5",  label: "Reel · 04" },
    { ratio: "9 / 16", label: "Foto · 05" },
  ];

  return (
    <section ref={ref} className="reveal" style={{
      background: "var(--espresso)",
      padding: "clamp(80px, 12vw, 160px) 0 clamp(72px, 10vw, 140px)",
    }}>
      <div className="container" style={{ marginBottom: "clamp(40px, 5.5vw, 68px)" }}>
        <h2 style={{
          fontFamily: "var(--serif)",
          fontSize: "clamp(40px, 6.5vw, 108px)",
          lineHeight: 0.9,
          letterSpacing: "-0.028em",
          fontWeight: 400,
          margin: 0,
          color: "var(--paper)",
        }}>
          {t.content.title}
        </h2>
      </div>

      <div
        className="hide-scroll"
        style={{
          display: "flex",
          gap: "clamp(8px, 1.2vw, 16px)",
          paddingLeft: "clamp(20px, 4vw, 64px)",
          paddingRight: "clamp(20px, 4vw, 64px)",
          overflowX: "auto",
          WebkitOverflowScrolling: "touch",
        }}
      >
        {tiles.map((tile, i) => {
          const tones = ["rgba(232,220,198,0.14)", "rgba(221,208,184,0.11)", "rgba(245,238,226,0.13)"];
          return (
            <div key={i} style={{
              position: "relative",
              flexShrink: 0,
              height: "clamp(260px, 44vw, 520px)",
              aspectRatio: tile.ratio,
              background: tones[i % 3],
              border: "1px solid rgba(250,248,244,0.08)",
              overflow: "hidden",
            }}>
              <div style={{
                position: "absolute", inset: 0,
                backgroundImage: "repeating-linear-gradient(135deg, rgba(250,248,244,0.024) 0 16px, transparent 16px 32px)",
              }} />
              <div style={{
                position: "absolute", bottom: 14, left: 14,
                fontFamily: "var(--mono)", fontSize: 9,
                letterSpacing: "0.22em", textTransform: "uppercase",
                color: "rgba(250,248,244,0.36)",
                padding: "4px 8px",
                border: "1px solid rgba(250,248,244,0.1)",
              }}>
                {tile.label.startsWith("Reel") ? "▶ " : "○ "}{tile.label}
              </div>
            </div>
          );
        })}
      </div>
    </section>
  );
}

/* ── LOGO ─────────────────────────────────────────────────────── */
function Logo({ size, variant }) {
  const s = size || 200;
  const src = variant === "light" ? LOGO_LIGHT : LOGO_BROWN;

  return (
    <img
      src={src}
      width={s}
      height={s}
      alt="Drømme Cookies"
      style={{
        display: "block",
        width: s,
        height: s,
        objectFit: "contain",
        flexShrink: 0,
      }}
    />
  );
}

Object.assign(window, { useReveal, Hero, Products, Brand, ContentStrip, Logo });
