// Generic saint deep-dive renderer. Takes a slug, looks up content from
// window.SAINTS_DATA, and renders the same editorial layout used for
// Carlo Acutis. Self-contained styles.

function SaintHero({ saint }) {
  if (saint.image) {
    return (
      <div className="feat-dd-hero" style={{ background: "var(--bg)" }}>
        <img
          src={saint.image}
          alt={saint.fullName}
          className="feat-dd-img"
          style={{ objectPosition: saint.imagePosition || "center 28%" }} />
      </div>);
  }
  // No portrait — render a tonal field using the saint's hue so the page
  // still has a banded hero, not a sudden flat title.
  const hue = saint.hue || "#1d2330";
  return (
    <div className="feat-dd-hero" style={{ background: hue }}>
      <div className="feat-dd-hero-art">
        <div className="feat-dd-hero-glow" style={{
          background: `radial-gradient(120% 80% at 30% 25%, ${tint(hue, 0.5)} 0%, ${hue} 55%, ${shade(hue, 0.45)} 100%)`
        }}></div>
        <div className="feat-dd-hero-mono">{saint.name.replace(/^(St\.|Bl\.)\s*/, "").charAt(0)}</div>
        <div className="feat-dd-hero-label">{saint.fullName}</div>
      </div>
    </div>);
}

// Hue helpers — lighten and darken a hex color.
function _hexToRgb(h) {
  const x = h.replace("#", "");
  return [
    parseInt(x.substr(0, 2), 16),
    parseInt(x.substr(2, 2), 16),
    parseInt(x.substr(4, 2), 16)
  ];
}
function _rgbToHex(r, g, b) {
  const c = (v) => Math.max(0, Math.min(255, Math.round(v))).toString(16).padStart(2, "0");
  return "#" + c(r) + c(g) + c(b);
}
function tint(hex, amt) {
  const [r, g, b] = _hexToRgb(hex);
  return _rgbToHex(r + (255 - r) * amt, g + (255 - g) * amt, b + (255 - b) * amt);
}
function shade(hex, amt) {
  const [r, g, b] = _hexToRgb(hex);
  return _rgbToHex(r * (1 - amt), g * (1 - amt), b * (1 - amt));
}

function SaintDeepDive({ slug, previousRoute, onNavigate }) {
  const saint = (window.SAINTS_DATA || {})[slug];

  if (!saint) {
    return (
      <section className="feat-dd">
        <div className="feat-dd-body">
          <div className="saint-copy">
            <button
              type="button"
              className="feat-dd-backlink"
              onClick={() => onNavigate && onNavigate(previousRoute || "saints")}>
              ← Back
            </button>
            <h1 className="feat-dd-name">Saint not found</h1>
            <p className="passage-text">No entry exists for <code>{slug}</code>.</p>
          </div>
        </div>
      </section>);
  }

  return (
    <section className="feat-dd">
      <SaintHero saint={saint} />

      <div className="feat-dd-body">
        <div className="saint-copy">
          <button
            type="button"
            className="feat-dd-backlink"
            onClick={() => onNavigate && onNavigate(previousRoute || "saints")}>
            ← Back
          </button>
          <h1 className="feat-dd-name">{saint.fullName}</h1>

          <div className="saint-meta">
            <div className="meta-cell">
              <div className="meta-cap">Born</div>
              <div className="meta-val">{saint.born}</div>
            </div>
            <div className="meta-cell">
              <div className="meta-cap">Died</div>
              <div className="meta-val">{saint.died}</div>
            </div>
            <div className="meta-cell">
              <div className="meta-cap">Feast Day</div>
              <div className="meta-val">{saint.feast}</div>
            </div>
            <div className="meta-cell">
              <div className="meta-cap">Patron of</div>
              <div className="meta-val">{saint.patron}</div>
            </div>
          </div>

          {saint.quote && (
            <figure className="quote">
              <span className="quote-mark">“</span>
              <blockquote className="quote-text">
                {saint.quote.text}
              </blockquote>
              {saint.quote.translation && (
                <figcaption className="quote-trans">
                  {saint.quote.translation}
                </figcaption>
              )}
            </figure>
          )}

          {(saint.passages || []).map((p, i) => (
            <section key={i} className="passage">
              <div className="passage-cap">{p.cap}</div>
              <p
                className="passage-text"
                dangerouslySetInnerHTML={{ __html: p.text }} />
            </section>
          ))}

          {saint.timeline && saint.timeline.length > 0 && (
            <section className="timeline-section">
              <div className="passage-cap">A life in dates</div>
              <ol className="timeline">
                {saint.timeline.map((t, i) => (
                  <li key={i} className="tl-row">
                    <span className="tl-year">{t.year}</span>
                    <span
                      className="tl-event"
                      dangerouslySetInnerHTML={{ __html: t.event }} />
                  </li>
                ))}
              </ol>
            </section>
          )}

          {saint.prayer && (
            <section className="prayer-block">
              <div className="passage-cap">Prayer to {saint.fullName}</div>
              <p className="prayer-intro">{saint.prayer.intro}</p>
              <blockquote className="prayer-body">
                <p className="prayer-sub">A prayer for his/her intercession</p>
                {saint.prayer.body.map((line, i) => (
                  <p key={i} dangerouslySetInnerHTML={{ __html: line }} />
                ))}
                {saint.prayer.closing && (
                  <p className="prayer-closing">{saint.prayer.closing}</p>
                )}
              </blockquote>
              <div className="prayer-coda">
                <span>{saint.prayer.coda}</span>
                <span className="prayer-versicle">{saint.prayer.versicle}</span>
              </div>
              {saint.prayer.source && (
                <div
                  className="prayer-source"
                  dangerouslySetInnerHTML={{
                    __html:
                      (saint.prayer.source.prefix || "") +
                      (saint.prayer.source.url
                        ? ` · <a href="${saint.prayer.source.url}" target="_blank" rel="noopener noreferrer">${saint.prayer.source.label}</a>`
                        : saint.prayer.source.label
                          ? ` · ${saint.prayer.source.label}`
                          : "")
                  }} />
              )}
            </section>
          )}

          {saint.further && saint.further.length > 0 && (
            <aside className="further">
              <div className="passage-cap">Further reading</div>
              <ul className="further-list">
                {saint.further.map((item, i) => (
                  <li key={i} dangerouslySetInnerHTML={{ __html: item }} />
                ))}
              </ul>
            </aside>
          )}
        </div>
      </div>

      <style>{`
        /* ─── Hero ───────────────────────────────────────────── */
        .feat-dd {
          display: flex; flex-direction: column;
          padding-bottom: clamp(80px, 10vw, 140px);
        }
        .feat-dd-hero {
          position: relative; width: 100%;
          height: clamp(280px, 40vh, 420px);
          overflow: hidden;
        }
        .feat-dd-img {
          position: absolute; inset: 0;
          width: 100%; height: 100%;
          object-fit: cover;
          display: block;
          -webkit-mask-image: linear-gradient(to bottom, #000 0%, #000 55%, rgba(0,0,0,0.5) 80%, transparent 100%);
                  mask-image: linear-gradient(to bottom, #000 0%, #000 55%, rgba(0,0,0,0.5) 80%, transparent 100%);
        }
        .feat-dd-hero-art {
          position: absolute; inset: 0;
          overflow: hidden;
          -webkit-mask-image: linear-gradient(to bottom, #000 0%, #000 55%, rgba(0,0,0,0.6) 82%, transparent 100%);
                  mask-image: linear-gradient(to bottom, #000 0%, #000 55%, rgba(0,0,0,0.6) 82%, transparent 100%);
        }
        .feat-dd-hero-glow {
          position: absolute; inset: 0;
        }
        .feat-dd-hero-mono {
          position: absolute; left: clamp(24px, 5vw, 72px); bottom: 18px;
          font-family: var(--font-display);
          font-weight: 300;
          font-size: clamp(120px, 18vw, 240px);
          line-height: 0.85;
          letter-spacing: -0.05em;
          color: rgba(255,255,255,0.92);
          mix-blend-mode: overlay;
        }
        .feat-dd-hero-label {
          position: absolute; top: clamp(20px, 4vh, 32px); right: clamp(24px, 5vw, 72px);
          font-family: var(--font-body);
          font-size: 11px; letter-spacing: 0.32em; text-transform: uppercase;
          color: rgba(255,255,255,0.72);
          max-width: 280px; text-align: right;
        }

        .feat-dd-body {
          max-width: 1240px; width: 100%; margin: 0 auto;
          padding: clamp(32px, 5vw, 64px) clamp(28px, 5vw, 72px) 0;
        }
        .feat-dd-body .saint-copy { max-width: 720px; margin: 0 auto; }
        .feat-dd-name {
          font-family: var(--font-display);
          font-weight: 500;
          line-height: 1.05;
          letter-spacing: -0.02em;
          text-wrap: balance;
          color: var(--ink);
          font-size: clamp(36px, 5vw, 56px);
          text-align: left;
          margin: 0 0 clamp(8px, 1vw, 12px);
        }
        .feat-dd-backlink {
          appearance: none;
          background: transparent;
          border: 0;
          padding: 0;
          margin: 0 0 clamp(16px, 2vw, 24px);
          cursor: pointer;
          color: var(--ink-mute);
          font-family: var(--font-body);
          font-size: 12px;
          letter-spacing: 0.04em;
          line-height: 1;
          white-space: nowrap;
          align-self: flex-start;
          display: inline-block;
          transition: color 180ms ease;
        }
        .feat-dd-backlink:hover { color: var(--accent); }

        /* ─── Saint copy ─────────────────────────────────────── */
        .saint-copy {
          font-family: var(--font-body);
          color: var(--ink);
          max-width: 560px;
        }
        .saint-meta {
          display: grid;
          grid-template-columns: 1fr 1fr;
          gap: 18px 28px;
          padding: 22px 0;
          margin: 12px 0 32px;
        }
        .meta-cap {
          font-size: 10px;
          letter-spacing: 0.28em;
          text-transform: uppercase;
          color: var(--ink-mute);
          margin-bottom: 4px;
        }
        .meta-val {
          font-family: var(--font-display);
          font-size: 15.5px;
          color: var(--ink);
          line-height: 1.4;
        }

        .quote {
          margin: 0 0 36px;
          padding: 4px 0 4px 28px;
          position: relative;
        }
        .quote-mark {
          position: absolute;
          top: -18px; left: 12px;
          font-family: var(--font-display);
          font-size: 64px;
          color: var(--accent);
          opacity: 0.32;
          line-height: 1;
        }
        .quote-text {
          margin: 0 0 8px;
          font-family: var(--font-quote);
          font-style: italic;
          font-weight: 400;
          font-size: clamp(19px, 2vw, 24px);
          line-height: 1.4;
          color: var(--ink);
        }
        .quote-trans {
          font-family: var(--font-body);
          font-size: 12px;
          letter-spacing: 0.16em;
          text-transform: uppercase;
          color: var(--ink-mute);
        }

        .passage { margin-bottom: 28px; }
        .passage-cap {
          font-size: 10.5px;
          letter-spacing: 0.32em;
          text-transform: uppercase;
          color: var(--accent);
          margin-bottom: 10px;
          font-weight: 500;
        }
        .passage-text {
          font-family: var(--font-quote);
          font-size: clamp(15px, 1.15vw, 17px);
          line-height: 1.65;
          color: var(--ink-soft);
          margin: 0;
          text-wrap: pretty;
        }
        .passage-text em { font-style: italic; color: var(--ink); }

        .timeline-section { margin: 40px 0 32px; }
        .timeline {
          list-style: none; padding: 0; margin: 16px 0 0;
          border-top: 1px solid var(--bg-deep);
        }
        .tl-row {
          display: grid; grid-template-columns: 110px 1fr; gap: 24px;
          padding: 14px 0;
          border-bottom: 1px solid var(--bg-deep);
        }
        .tl-year {
          font-family: var(--font-display);
          font-size: 13px; letter-spacing: 0.18em;
          color: var(--accent); font-weight: 500;
          padding-top: 2px;
        }
        .tl-event {
          font-family: var(--font-quote);
          font-size: 15px; line-height: 1.55;
          color: var(--ink-soft);
        }
        .tl-event em { font-style: italic; color: var(--ink); }

        .prayer-block {
          margin: 48px 0 8px;
          padding: 32px clamp(20px, 4vw, 44px);
          background: var(--bg-warm);
          position: relative;
        }
        .prayer-block::before {
          content: "✻";
          position: absolute;
          top: 18px; right: 24px;
          color: var(--accent);
          font-size: 14px;
          opacity: 0.6;
        }
        .prayer-intro {
          font-family: var(--font-body);
          font-size: 13px; line-height: 1.55;
          color: var(--ink-mute);
          margin: 12px 0 22px;
          max-width: 520px;
        }
        .prayer-intro em { font-style: italic; color: var(--ink-soft); }
        .prayer-body {
          margin: 0;
          font-family: var(--font-quote);
          font-size: clamp(15px, 1.2vw, 17px);
          line-height: 1.7;
          color: var(--ink-soft);
          text-wrap: pretty;
        }
        .prayer-body p { margin: 0 0 14px; }
        .prayer-body p:last-child { margin-bottom: 0; }
        .prayer-body em { font-style: italic; color: var(--ink); }
        .prayer-sub {
          font-family: var(--font-body);
          font-size: 11px;
          letter-spacing: 0.28em;
          text-transform: uppercase;
          color: var(--accent);
          font-weight: 600;
          margin: 0 0 4px !important;
        }
        .prayer-closing { font-style: italic; color: var(--ink); }
        .prayer-coda {
          margin-top: 22px;
          padding-top: 16px;
          border-top: 1px solid var(--bg-deep);
          font-family: var(--font-body);
          font-size: 10.5px;
          letter-spacing: 0.32em;
          text-transform: uppercase;
          color: var(--accent);
          text-align: center;
          display: flex;
          flex-direction: column;
          gap: 8px;
        }
        .prayer-versicle {
          color: var(--ink);
          letter-spacing: 0.18em;
          font-weight: 500;
          font-style: italic;
          text-transform: none;
          font-size: 12px;
        }
        .prayer-source {
          margin-top: 14px;
          font-family: var(--font-body);
          font-size: 11.5px;
          color: var(--ink-mute);
          line-height: 1.5;
          text-align: center;
        }
        .prayer-source em { font-style: italic; }
        .prayer-source a {
          color: var(--accent);
          text-decoration: none;
          border-bottom: 1px solid currentColor;
        }

        .further {
          margin-top: 40px; padding-top: 22px;
          border-top: 1px solid var(--bg-deep);
        }
        .further-list {
          list-style: none; padding: 0; margin: 14px 0 0;
          font-family: var(--font-quote);
          font-size: 14px; line-height: 1.7;
          color: var(--ink-mute);
        }
        .further-list li { margin-bottom: 6px; }
        .further-list em { font-style: italic; color: var(--ink-soft); }
      `}</style>
    </section>);
}

window.SaintDeepDive = SaintDeepDive;
