// VariationDenseMobile — true mobile composition (≤480px) for the article.
// Adapted from /tmp/gcd-news-article-v2/design_handoff_v3_dense/article-mobile.jsx.
// Hero-bottom-driven docked masthead promo; top hairline reading-progress bar.

const cdMobileH2 = {
  fontFamily: "var(--headline-font)",
  fontWeight: 700,
  fontSize: 26,
  lineHeight: 1.18,
  margin: "1.4em 0 0.5em",
  letterSpacing: -0.4,
};

function CDMobileProgressBar() {
  const [pct, setPct] = cdUseState(0);

  cdUseEffect(() => {
    let raf = null;
    const update = () => {
      raf = null;
      const article = document.getElementById('cd-article-mobile');
      if (!article) return;
      const articleTop = article.getBoundingClientRect().top + window.scrollY;
      const articleHeight = article.offsetHeight;
      const vh = window.innerHeight;
      const denom = Math.max(1, articleHeight - vh);
      const raw = (window.scrollY - articleTop) / denom;
      const clamped = Math.max(0, Math.min(1, raw));
      setPct(clamped);
    };
    const onScroll = () => {
      if (raf == null) raf = window.requestAnimationFrame(update);
    };
    window.addEventListener('scroll', onScroll, { passive: true });
    update();
    return () => {
      window.removeEventListener('scroll', onScroll);
      if (raf != null) window.cancelAnimationFrame(raf);
    };
  }, []);

  return (
    <div
      role="progressbar"
      aria-valuemin="0"
      aria-valuemax="100"
      aria-valuenow={Math.round(pct * 100)}
      aria-label="Article reading progress"
      style={{ position: "sticky", top: 0, height: 2, background: "#eee", zIndex: 30 }}
    >
      <div style={{ width: `${pct * 100}%`, height: "100%", background: "#000", transition: "width 80ms linear" }} />
    </div>
  );
}

function VariationDenseMobile({ topic, mostRead, bodySize = 17 }) {
  const PADX = 16;
  const heroRef = cdUseRef(null);
  const [scrolled, setScrolled] = cdUseState(false);

  // Switch utility-row state when hero photo bottom passes the masthead line.
  // Use IntersectionObserver with rootMargin to trigger when the hero leaves
  // the viewport's top region.
  cdUseEffect(() => {
    if (!heroRef.current || typeof IntersectionObserver === 'undefined') return;
    const obs = new IntersectionObserver(
      (entries) => {
        entries.forEach((entry) => {
          // When the hero is no longer intersecting (scrolled past), switch.
          // Trigger when 0% of hero is visible (i.e. scrolled below it).
          setScrolled(!entry.isIntersecting && entry.boundingClientRect.bottom < 0);
        });
      },
      { threshold: [0, 0.1], rootMargin: "0px 0px 0px 0px" }
    );
    obs.observe(heroRef.current);
    return () => obs.disconnect();
  }, []);

  return (
    <div
      id="cd-article-mobile"
      className="cd-page-root"
      style={{ background: "#fff", color: "#000", fontFamily: "var(--body-font)", position: "relative", minHeight: "100%" }}
    >
      {/* (1) Top progress hairline */}
      <CDMobileProgressBar />

      {/* Masthead — sticky */}
      <header
        style={{
          borderTop: "3px solid #000",
          borderBottom: "1px solid #000",
          background: "#fff",
          position: "sticky",
          top: 2,
          zIndex: 20,
        }}
      >
        {/* (4) Utility row morph */}
        {scrolled ? (
          <div
            style={{
              display: "flex",
              alignItems: "stretch",
              borderBottom: "1px solid #d4d4d4",
              background: "#000",
              color: "#fff",
            }}
          >
            <div
              style={{
                flex: 1,
                padding: "9px 14px",
                fontSize: 12,
                fontWeight: 600,
                letterSpacing: 0.2,
                display: "flex",
                alignItems: "center",
                gap: 8,
              }}
            >
              <span
                style={{
                  display: "inline-block",
                  width: 6,
                  height: 6,
                  borderRadius: 99,
                  background: "#fff",
                }}
              />
              Free contract review &middot; book in 90 sec
            </div>
            <button
              type="button"
              className="cd-cta-btn"
              onClick={cdScrollToBook}
              style={{
                background: "#fff",
                color: "#000",
                border: 0,
                padding: "0 16px",
                fontFamily: "inherit",
                fontSize: 11,
                fontWeight: 700,
                letterSpacing: 1,
                textTransform: "uppercase",
              }}
            >
              Schedule &rarr;
            </button>
          </div>
        ) : (
          <div
            style={{
              padding: "8px 16px",
              borderBottom: "1px solid #d4d4d4",
              display: "flex",
              justifyContent: "space-between",
              fontSize: 11,
              letterSpacing: 0.4,
              color: "#3a3a3a",
            }}
          >
            <span>Mon, May 4 &middot; Vol. XII, No. 124</span>
            <span style={{ fontWeight: 700, color: "#000" }}>Subscribe</span>
          </div>
        )}
        {/* Wordmark + hamburger */}
        <div
          style={{
            padding: "10px 16px",
            display: "flex",
            alignItems: "center",
            justifyContent: "space-between",
            borderBottom: "1px solid #d4d4d4",
          }}
        >
          <span
            aria-label="menu"
            style={{ display: "inline-flex", flexDirection: "column", gap: 4, padding: 4 }}
          >
            <span style={{ width: 18, height: 1.5, background: "#000" }} />
            <span style={{ width: 18, height: 1.5, background: "#000" }} />
            <span style={{ width: 18, height: 1.5, background: "#000" }} />
          </span>
          <span
            style={{
              fontFamily: "var(--headline-font)",
              fontWeight: 800,
              fontSize: 24,
              letterSpacing: -0.6,
              lineHeight: 1,
            }}
          >
            ConstructionDaily<span style={{ fontWeight: 400, fontStyle: "italic" }}>.com</span>
          </span>
          <span
            aria-label="search"
            style={{
              width: 22,
              height: 22,
              border: "1.5px solid #000",
              borderRadius: 99,
              position: "relative",
              display: "inline-block",
            }}
          >
            <span
              style={{
                position: "absolute",
                right: -3,
                bottom: -3,
                width: 8,
                height: 1.5,
                background: "#000",
                transform: "rotate(45deg)",
                transformOrigin: "left center",
              }}
            />
          </span>
        </div>
        {/* Horizontal scroll nav */}
        <div className="cd-no-scrollbar">
          <div
            style={{
              display: "flex",
              gap: 18,
              padding: "9px 16px",
              fontSize: 12,
              fontWeight: 600,
              letterSpacing: 0.6,
              textTransform: "uppercase",
              whiteSpace: "nowrap",
            }}
          >
            {["Roofing", "Concrete", "Framing", "Trades", "Permits & Code", "Contracts", "Investigations", "Tools"].map((n) => (
              <span
                key={n}
                style={{
                  color: n === "Investigations" ? "#000" : "#666",
                  borderBottom: n === "Investigations" ? "2px solid #000" : "2px solid transparent",
                  paddingBottom: 2,
                }}
              >
                {n}
              </span>
            ))}
          </div>
        </div>
      </header>

      {/* Article header */}
      <div style={{ padding: `20px ${PADX}px 0` }}>
        <div
          style={{
            display: "flex",
            gap: 10,
            alignItems: "center",
            fontSize: 10,
            letterSpacing: 1.5,
            textTransform: "uppercase",
            fontWeight: 700,
          }}
        >
          <span>{topic.section}</span>
          <span style={{ color: "#999" }}>|</span>
          <span style={{ color: "#666", fontWeight: 500 }}>{topic.eyebrow}</span>
        </div>
        <h1
          style={{
            fontFamily: "var(--headline-font)",
            fontWeight: 800,
            fontSize: 32,
            lineHeight: 1.08,
            letterSpacing: -0.6,
            margin: "12px 0 12px",
            textWrap: "balance",
          }}
        >
          {topic.headline}
        </h1>
        <p
          style={{
            fontSize: 16,
            lineHeight: 1.5,
            color: "#333",
            margin: "0 0 18px",
            textWrap: "pretty",
          }}
        >
          {topic.dek}
        </p>
        <div
          style={{
            borderTop: "1px solid #000",
            borderBottom: "1px solid #ccc",
            padding: "12px 0",
            marginBottom: 16,
          }}
        >
          <div style={{ fontSize: 13, lineHeight: 1.4 }}>
            <span>By <strong>{topic.author}</strong></span>
            <span style={{ color: "#888" }}> &middot; </span>
            <span style={{ color: "#666" }}>{topic.authorTitle}</span>
          </div>
          <div style={{ fontSize: 12, color: "#666", marginTop: 4, display: "flex", gap: 8 }}>
            <span>{topic.date}</span>
            <span style={{ color: "#bbb" }}>&middot;</span>
            <span>{topic.readTime}</span>
            <span style={{ color: "#bbb" }}>&middot;</span>
            <span style={{ textTransform: "uppercase", letterSpacing: 1, fontWeight: 700, color: "#000" }}>Share</span>
          </div>
        </div>
      </div>

      {/* (3) Inline collapsible TOC */}
      <div style={{ margin: `0 ${PADX}px 18px` }}>
        <details style={{ border: "1px solid #000", background: "#fafafa" }}>
          <summary
            style={{
              padding: "12px 14px",
              display: "flex",
              justifyContent: "space-between",
              alignItems: "center",
              cursor: "pointer",
              fontSize: 12,
              letterSpacing: 1.2,
              textTransform: "uppercase",
              fontWeight: 700,
            }}
          >
            <span style={{ display: "flex", gap: 10, alignItems: "center" }}>
              <span
                style={{
                  display: "inline-block",
                  padding: "2px 6px",
                  border: "1px solid #000",
                  fontSize: 10,
                  fontWeight: 700,
                }}
              >
                4
              </span>
              In this article
            </span>
            <span
              style={{
                color: "#666",
                textTransform: "none",
                letterSpacing: 0,
                fontWeight: 500,
                fontSize: 12,
              }}
            >
              {topic.readTime} &nbsp;⌄
            </span>
          </summary>
          <ol style={{ margin: 0, padding: "4px 14px 12px", listStyle: "none" }}>
            {[topic.h2_1, topic.h2_2, topic.h2_3, topic.h2_4].map((s, i) => (
              <li
                key={i}
                style={{
                  padding: "10px 0",
                  borderTop: "1px solid #e3e3e3",
                  display: "flex",
                  gap: 10,
                  fontSize: 14,
                  lineHeight: 1.35,
                }}
              >
                <span
                  style={{
                    color: "#999",
                    minWidth: 18,
                    fontVariantNumeric: "tabular-nums",
                  }}
                >
                  {String(i + 1).padStart(2, "0")}
                </span>
                <a
                  href={`#h2-${i + 1}`}
                  onClick={(e) => {
                    e.preventDefault();
                    const el = document.getElementById(`h2-${i + 1}`);
                    if (el) el.scrollIntoView({ behavior: 'smooth', block: 'start' });
                  }}
                  style={{ fontWeight: 500, color: "#000", textDecoration: "none" }}
                >
                  {s}
                </a>
              </li>
            ))}
          </ol>
        </details>
      </div>

      {/* Hero photo */}
      <div ref={heroRef} style={{ margin: `0 ${PADX}px 24px` }}>
        <img
          src="/images/hero.jpg"
          alt="A residential Australian kitchen mid-renovation, with exposed timber framing and dust sheets visible. Editorial illustration."
          style={{
            width: "100%",
            aspectRatio: "4/3",
            objectFit: "cover",
            display: "block",
            border: "1px solid #d6d6d6",
          }}
        />
        <div
          style={{
            marginTop: 8,
            fontSize: 11,
            lineHeight: 1.4,
            color: "#5a5a5a",
          }}
        >
          <strong style={{ color: "#000" }}>Photograph </strong>
          ConstructionDaily / Investigative photography. Image illustrative — no specific contract or homeowner pictured.
        </div>
      </div>

      {/* Body */}
      <div style={{ padding: `0 ${PADX}px`, fontSize: bodySize, lineHeight: 1.65, color: "#1a1a1a" }}>
        <p
          style={{
            margin: "0 0 0.6em",
            fontSize: 10,
            letterSpacing: 1.5,
            fontWeight: 700,
            lineHeight: 1.4,
          }}
        >
          {topic.location} &mdash;
        </p>
        <p style={{ margin: 0 }}>
          <span
            style={{
              float: "left",
              fontFamily: "var(--headline-font)",
              fontSize: bodySize * 3.4,
              lineHeight: 0.85,
              paddingRight: 8,
              paddingTop: 4,
              fontWeight: 700,
            }}
          >
            {topic.intro.charAt(0)}
          </span>
          {topic.intro.slice(1)}
        </p>
        <p style={{ marginTop: "1em", marginBottom: 0 }}>{topic.intro2}</p>

        <h2 id="h2-1" style={cdMobileH2}>{topic.h2_1}</h2>
        <p style={{ margin: 0 }}>{topic.p1}</p>
        <p style={{ marginTop: "1em", marginBottom: 0 }}>{topic.p2}</p>
      </div>

      {/* (5) Pull quote — left-rail bar + hanging quote mark */}
      <aside
        style={{
          margin: `28px ${PADX}px`,
          padding: "8px 0 8px 18px",
          borderLeft: "4px solid #000",
          position: "relative",
        }}
      >
        <span
          aria-hidden
          style={{
            position: "absolute",
            left: 14,
            top: -10,
            fontFamily: "var(--headline-font)",
            fontSize: 56,
            lineHeight: 1,
            fontWeight: 800,
            color: "#000",
          }}
        >
          &ldquo;
        </span>
        <p
          style={{
            margin: "14px 0 0",
            fontFamily: "var(--headline-font)",
            fontSize: 22,
            lineHeight: 1.25,
            fontWeight: 600,
            letterSpacing: -0.2,
          }}
        >
          {topic.pullquote}
        </p>
        <p
          style={{
            margin: "12px 0 0",
            fontSize: 11,
            letterSpacing: 0.5,
            textTransform: "uppercase",
            color: "#555",
            fontWeight: 600,
          }}
        >
          &mdash; {topic.pullattr}
        </p>
      </aside>

      {/* Body cont. */}
      <div style={{ padding: `0 ${PADX}px`, fontSize: bodySize, lineHeight: 1.65, color: "#1a1a1a" }}>
        <h2 id="h2-2" style={cdMobileH2}>{topic.h2_2}</h2>
        <p style={{ margin: 0 }}>{topic.p3}</p>
        <p style={{ marginTop: "1em", marginBottom: 0 }}>{topic.list_intro}</p>
        <ol style={{ marginTop: "1em", paddingLeft: "1.2em", marginBottom: 0 }}>
          {topic.list.map((item, i) => (
            <li key={i} style={{ marginBottom: "0.7em" }}>
              <strong>{item[0]}</strong>{item[1]}
            </li>
          ))}
        </ol>
      </div>

      {/* (8) Inline promo — full-bleed */}
      <aside
        style={{
          margin: "36px 0",
          borderTop: "3px double #000",
          borderBottom: "3px double #000",
          background: "#fafafa",
          padding: `22px ${PADX}px`,
        }}
      >
        <div
          style={{
            fontSize: 10,
            letterSpacing: 1.5,
            textTransform: "uppercase",
            fontWeight: 700,
            color: "#666",
            marginBottom: 8,
          }}
        >
          A note from our sponsor
        </div>
        <h3
          style={{
            fontFamily: "var(--headline-font)",
            fontWeight: 700,
            fontSize: 22,
            lineHeight: 1.18,
            margin: "0 0 8px",
            letterSpacing: -0.3,
          }}
        >
          Reading this article in the middle of getting bids?
        </h3>
        <p style={{ margin: 0, fontSize: 14, lineHeight: 1.5, color: "#222" }}>
          Schedule a free 30-minute call with a Verified local builder. We'll review your contract and tell you which clauses to redline before you sign.
        </p>
        <button
          type="button"
          className="cd-cta-btn"
          onClick={cdScrollToBook}
          style={{
            marginTop: 14,
            width: "100%",
            padding: "14px 16px",
            background: "#000",
            color: "#fff",
            border: 0,
            fontFamily: "inherit",
            fontSize: 13,
            fontWeight: 700,
            letterSpacing: 0.5,
            textTransform: "uppercase",
          }}
        >
          Schedule a Free Review &rarr;
        </button>
        {/* (6) Compact horizontal trust row */}
        {/* TODO: CEO_CONFIRM exact figures */}
        <div
          style={{
            marginTop: 14,
            display: "flex",
            justifyContent: "space-between",
            gap: 10,
            fontSize: 11,
            color: "#666",
          }}
        >
          <span>
            <strong style={{ color: "#000", fontFamily: "var(--headline-font)", fontSize: 14 }}>QBCC</strong> licensed
          </span>
          <span>
            <strong style={{ color: "#000", fontFamily: "var(--headline-font)", fontSize: 14 }}>150+</strong> renovations
          </span>
          <span>
            <strong style={{ color: "#000", fontFamily: "var(--headline-font)", fontSize: 14 }}>0</strong> surprise variations
          </span>
        </div>
      </aside>

      {/* Body tail */}
      <div style={{ padding: `0 ${PADX}px`, fontSize: bodySize, lineHeight: 1.65, color: "#1a1a1a" }}>
        <h2 id="h2-3" style={cdMobileH2}>{topic.h2_3}</h2>
        <p style={{ margin: 0 }}>{topic.p4}</p>
        <p style={{ marginTop: "1em", marginBottom: 0 }}>{topic.p5}</p>

        <h2 id="h2-4" style={cdMobileH2}>{topic.h2_4}</h2>
        <p style={{ margin: 0 }}>{topic.p6}</p>
        <p style={{ marginTop: "1em", marginBottom: 0 }}>{topic.p7}</p>
        <p style={{ marginTop: "1em", marginBottom: 0 }}>{topic.p8}</p>

        <hr style={{ border: 0, borderTop: "1px solid #ccc", margin: "28px 0 14px" }} />
        <p
          style={{
            fontStyle: "italic",
            fontSize: 13,
            color: "#555",
            margin: 0,
            lineHeight: 1.5,
          }}
        >
          {topic.kicker}
        </p>
      </div>

      {/* (9) Closing CTA card */}
      <div
        style={{
          margin: `36px ${PADX}px 0`,
          padding: 22,
          background: "#000",
          color: "#fff",
        }}
      >
        <div
          style={{
            fontSize: 10,
            letterSpacing: 1.5,
            textTransform: "uppercase",
            fontWeight: 700,
            color: "#999",
            marginBottom: 8,
          }}
        >
          Sponsored &middot; ConstructionDaily Verified
        </div>
        <h3
          style={{
            fontFamily: "var(--headline-font)",
            fontWeight: 700,
            fontSize: 26,
            lineHeight: 1.15,
            margin: "0 0 10px",
            letterSpacing: -0.3,
          }}
        >
          Don't sign anything until a builder you trust has read it.
        </h3>
        <p style={{ margin: "0 0 16px", fontSize: 14, lineHeight: 1.55, color: "#ddd" }}>
          A free 30-minute consultation. Verified local builder. No sales pitch.
        </p>
        <button
          type="button"
          className="cd-cta-btn"
          onClick={cdScrollToBook}
          style={{
            width: "100%",
            padding: "16px 18px",
            background: "#fff",
            color: "#000",
            border: 0,
            fontFamily: "inherit",
            fontSize: 13,
            fontWeight: 700,
            letterSpacing: 0.5,
            textTransform: "uppercase",
          }}
        >
          Schedule Free Consultation &rarr;
        </button>
        {/* TODO: CEO_CONFIRM exact figures */}
        <div
          style={{
            marginTop: 12,
            fontSize: 11,
            color: "#bbb",
            textAlign: "center",
          }}
        >
          QBCC licensed &middot; 150+ renovations &middot; 0 surprise variations
        </div>
      </div>
    </div>
  );
}

function CDMobileMostRead({ mostRead, padX = 16 }) {
  return (
    <section style={{ marginTop: 32 }}>
      <div
        style={{
          padding: `0 ${padX}px`,
          marginBottom: 12,
          display: "flex",
          justifyContent: "space-between",
          alignItems: "baseline",
          borderTop: "3px solid #000",
          paddingTop: 14,
        }}
      >
        <span
          style={{
            fontSize: 10,
            letterSpacing: 1.5,
            textTransform: "uppercase",
            fontWeight: 700,
          }}
        >
          Most read this week
        </span>
        <span style={{ fontSize: 11, color: "#666" }}>swipe &rarr;</span>
      </div>
      <div className="cd-no-scrollbar">
        <div style={{ display: "flex", gap: 12, padding: `0 ${padX}px 8px` }}>
          {mostRead.map((t, i) => (
            <a
              key={t}
              style={{
                flex: "0 0 220px",
                borderTop: "1px solid #000",
                paddingTop: 10,
                color: "#000",
                textDecoration: "none",
              }}
            >
              <div
                style={{
                  fontFamily: "var(--headline-font)",
                  fontWeight: 800,
                  fontSize: 28,
                  color: "#bbb",
                  lineHeight: 1,
                  marginBottom: 6,
                }}
              >
                {i + 1}
              </div>
              <div
                style={{
                  fontFamily: "var(--headline-font)",
                  fontSize: 15,
                  lineHeight: 1.25,
                  fontWeight: 600,
                }}
              >
                {t}
              </div>
              <div
                style={{
                  marginTop: 8,
                  fontSize: 11,
                  color: "#666",
                  letterSpacing: 0.4,
                  textTransform: "uppercase",
                }}
              >
                5 min read
              </div>
            </a>
          ))}
        </div>
      </div>
    </section>
  );
}

function CDMobileFooter({ padX = 16 }) {
  return (
    <footer
      style={{
        marginTop: 32,
        borderTop: "3px solid #000",
        padding: `22px ${padX}px 32px`,
        fontSize: 12,
        color: "#444",
      }}
    >
      <div
        style={{
          fontFamily: "var(--headline-font)",
          fontWeight: 800,
          fontSize: 20,
          color: "#000",
          marginBottom: 14,
        }}
      >
        ConstructionDaily<span style={{ fontWeight: 400, fontStyle: "italic" }}>.com</span>
      </div>
      <div
        style={{
          display: "grid",
          gridTemplateColumns: "1fr 1fr",
          gap: "16px 20px",
          marginBottom: 18,
        }}
      >
        {[
          ["News", ["Roofing", "Concrete", "Framing", "Permits & Code"]],
          ["Tools", ["Find a Verified Builder", "Contract Review", "Bid Calculator"]],
          ["Company", ["About", "Editorial Standards", "Contact"]],
          ["Follow", ["Newsletter", "Apple News", "RSS"]],
        ].map(([h, items]) => (
          <div key={h}>
            <div
              style={{
                fontWeight: 700,
                color: "#000",
                marginBottom: 6,
                textTransform: "uppercase",
                fontSize: 10,
                letterSpacing: 1,
              }}
            >
              {h}
            </div>
            {items.map((x) => (
              <div key={x} style={{ marginBottom: 2 }}>
                {x}
              </div>
            ))}
          </div>
        ))}
      </div>
      <div
        style={{
          paddingTop: 12,
          borderTop: "1px solid #ddd",
          color: "#888",
          fontSize: 11,
          lineHeight: 1.5,
        }}
      >
        &copy; 2026 ConstructionDaily, Inc. Editorial independent of sponsored placements. Sponsored content is clearly labeled.
      </div>
    </footer>
  );
}

window.CDVariationDenseMobile = VariationDenseMobile;
window.CDMobileMostRead = CDMobileMostRead;
window.CDMobileFooter = CDMobileFooter;
