/* Wired CIO marketing site — Service pages (slug-driven article + index) */
(function () {
  const DS = window.WiredCIODesignSystem_afa0a4;
  const I = window.WCIcons;
  const { Button, OutcomeBadge, OUTCOMES } = DS;
  const SERVICES = (window.WCData && window.WCData.SERVICES) || [];
  const CASES = (window.WCData && window.WCData.CASE_STUDIES) || [];

  const OUTCOME_ICONS = { grow: I.Rocket, protect: I.ShieldCheck, automate: I.Burst, align: I.BadgeCheck };
  const PILLAR_DEEP = { grow: 'var(--pillar-grow-deep)', protect: 'var(--pillar-protect-deep)', automate: 'var(--pillar-automate-deep)', align: 'var(--pillar-align-deep)' };
  const PILLAR_SOFT = { grow: 'var(--pillar-grow-soft)', protect: 'var(--pillar-protect-soft)', automate: 'var(--pillar-automate-soft)', align: 'var(--pillar-align-soft)' };
  // Each pillar gets a distinct brand background texture in its hero.
  const PILLAR_BG = { grow: 'bg-04', protect: 'bg-05', automate: 'bg-03', align: 'bg-01' };

  function svcHref(slug) { return 'service.html?slug=' + slug; }

  // Eyebrows inherit the pillar accent set on the <article data-pillar> root.
  function Eyebrow({ children }) {
    return <span className="wc-eyebrow" style={{ color: 'var(--svc-accent, var(--wc-emerald))' }}>{children}</span>;
  }

  function ServicePage() {
    const params = new URLSearchParams(window.location.search);
    const slug = params.get('slug') || (SERVICES[0] && SERVICES[0].slug);
    const svc = SERVICES.find((s) => s.slug === slug) || SERVICES[0];
    const o = OUTCOMES[svc.outcome] || {};
    const Ico = OUTCOME_ICONS[svc.outcome];
    const accent = o.accent || 'var(--wc-emerald)';
    const accentSoft = PILLAR_SOFT[svc.outcome] || 'var(--accent-2-soft)';
    const accentDeep = PILLAR_DEEP[svc.outcome] || 'var(--wc-ink-900)';
    const heroBg = PILLAR_BG[svc.outcome] || 'bg-04';
    const siblings = SERVICES.filter((s) => s.outcome === svc.outcome && s.slug !== svc.slug);
    const relCase = svc.relatedCaseSlug ? CASES.find((c) => c.slug === svc.relatedCaseSlug && c.status === 'published') : null;

    React.useEffect(() => { document.title = 'Wired CIO · ' + svc.name; }, [svc.name]);

    return (
      <article data-pillar={svc.outcome} style={{ ['--svc-accent']: accent, ['--svc-soft']: accentSoft, ['--svc-deep']: accentDeep }}>
        {/* Hero — pillar-tinted wash + brand texture + complementary glow */}
        <section style={{ position: 'relative', overflow: 'hidden', background: 'radial-gradient(130% 110% at 88% -20%, color-mix(in srgb, var(--svc-accent) 42%, transparent) 0%, transparent 58%), linear-gradient(140deg, var(--svc-deep) 0%, var(--wc-ink-900) 78%)' }}>
          <div style={{ position: 'absolute', inset: 0, backgroundImage: "url('../../assets/backgrounds/" + heroBg + ".svg')", backgroundSize: 'cover', backgroundPosition: 'center', opacity: 0.62, mixBlendMode: 'soft-light' }} />
          <div style={{ position: 'relative', maxWidth: 'var(--container-max)', margin: '0 auto', padding: '60px var(--container-pad) 68px' }}>
            <a href={'index.html#explore'} style={{ display: 'inline-flex', alignItems: 'center', gap: 7, fontFamily: 'var(--font-body)', fontWeight: 600, fontSize: '0.875rem', color: 'rgba(255,255,255,0.82)', textDecoration: 'none' }}>
              <span style={{ transform: 'rotate(180deg)', display: 'inline-flex' }}><I.ArrowRight size={15} /></span> {o.label}
            </a>
            <div style={{ display: 'flex', alignItems: 'center', gap: 12, marginTop: 20 }}>
              <span style={{ width: 44, height: 44, borderRadius: 'var(--radius-md)', background: accent, color: 'var(--wc-white)', display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}>{Ico ? <Ico size={22} /> : null}</span>
              <OutcomeBadge outcome={svc.outcome} />
            </div>
            <h1 style={{ fontFamily: 'var(--font-display)', fontWeight: 900, fontSize: 'clamp(2.4rem, 5vw, 3.75rem)', lineHeight: 1.05, letterSpacing: '-0.025em', color: 'var(--wc-white)', margin: '18px 0 0', maxWidth: 820, textWrap: 'balance' }}>{svc.name}</h1>
            <p style={{ fontFamily: 'var(--font-body)', fontSize: '1.1875rem', lineHeight: 1.6, color: 'var(--wc-ink-100)', margin: '18px 0 0', maxWidth: 640 }}>{svc.summary}</p>
            <div style={{ display: 'flex', flexWrap: 'wrap', gap: 14, marginTop: 32 }}>
              <Button size="lg" variant="primary" onClick={() => { window.location.href = 'index.html#assessment'; }}>Book a Strategy Call</Button>
              <Button size="lg" variant="on-dark" onClick={() => { window.location.href = 'index.html#explore'; }}>Explore {o.label}</Button>
            </div>
          </div>
        </section>

        {/* Overview + at a glance */}
        <section style={{ background: 'var(--surface-page)', padding: '80px 0' }}>
          <div className="wc-svc-overview" style={{ maxWidth: 'var(--container-max)', margin: '0 auto', padding: '0 var(--container-pad)', display: 'grid', gridTemplateColumns: '1.5fr 1fr', gap: 40, alignItems: 'start' }}>
            <div>
              <Eyebrow>Overview</Eyebrow>
              <h2 style={{ fontFamily: 'var(--font-display)', fontWeight: 800, fontSize: 'clamp(1.75rem, 3vw, 2.25rem)', lineHeight: 1.15, letterSpacing: '-0.02em', color: 'var(--text-strong)', margin: '12px 0 0', textWrap: 'balance' }}>{svc.overviewHead}</h2>
              {svc.overviewBody.map((p, i) => (
                <p key={i} style={{ fontFamily: 'var(--font-body)', fontSize: '1.0625rem', lineHeight: 1.65, color: 'var(--text-body)', margin: i === 0 ? '18px 0 0' : '16px 0 0' }}>{p}</p>
              ))}
            </div>
            <aside style={{ background: 'var(--surface-card)', border: '1px solid var(--border-subtle)', borderRadius: 'var(--radius-lg)', boxShadow: 'var(--shadow-sm)', padding: '26px 28px' }}>
              <div style={{ fontFamily: 'var(--font-body)', fontWeight: 700, fontSize: '0.75rem', letterSpacing: '0.12em', textTransform: 'uppercase', color: 'var(--svc-accent)', marginBottom: 16 }}>At a glance</div>
              <dl style={{ margin: 0, display: 'flex', flexDirection: 'column', gap: 14 }}>
                {svc.glance.map((g, i) => (
                  <div key={i} style={{ display: 'flex', justifyContent: 'space-between', gap: 12, borderBottom: i < svc.glance.length - 1 ? '1px solid var(--border-subtle)' : 'none', paddingBottom: i < svc.glance.length - 1 ? 14 : 0 }}>
                    <dt style={{ color: 'var(--text-muted)', fontSize: '0.9375rem' }}>{g.label}</dt>
                    <dd style={{ margin: 0, fontWeight: 600, color: 'var(--text-strong)', fontSize: '0.9375rem', textAlign: 'right' }}>{g.value}</dd>
                  </div>
                ))}
              </dl>
            </aside>
          </div>
        </section>

        {/* What's included */}
        <section style={{ background: 'var(--surface-card)', padding: '80px 0', borderTop: '1px solid var(--border-subtle)' }}>
          <div style={{ maxWidth: 'var(--container-max)', margin: '0 auto', padding: '0 var(--container-pad)' }}>
            <Eyebrow>What's included</Eyebrow>
            <h2 style={{ fontFamily: 'var(--font-display)', fontWeight: 800, fontSize: 'clamp(1.75rem, 3vw, 2.25rem)', lineHeight: 1.15, letterSpacing: '-0.02em', color: 'var(--text-strong)', margin: '12px 0 0' }}>Everything this service brings to the table.</h2>
            <div className="wc-svc-incl" style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(280px, 1fr))', gap: 16, marginTop: 40 }}>
              {svc.included.map((it, i) => (
                <div key={i} style={{ background: 'var(--surface-page)', border: '1px solid var(--border-subtle)', borderRadius: 'var(--radius-md)', padding: '22px 24px' }}>
                  <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 8 }}>
                    <span style={{ width: 26, height: 26, flexShrink: 0, borderRadius: '50%', background: 'var(--svc-soft)', color: 'var(--svc-accent)', display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}><I.Check size={15} stroke={3} /></span>
                    <h3 style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: '1.125rem', color: 'var(--text-strong)', margin: 0 }}>{it.title}</h3>
                  </div>
                  <p style={{ fontFamily: 'var(--font-body)', fontSize: '0.9375rem', lineHeight: 1.55, color: 'var(--text-body)', margin: 0 }}>{it.body}</p>
                </div>
              ))}
            </div>
          </div>
        </section>

        {/* Related case study */}
        {relCase ? (
          <section style={{ background: 'var(--surface-page)', padding: '72px 0', borderTop: '1px solid var(--border-subtle)' }}>
            <div style={{ maxWidth: 'var(--container-max)', margin: '0 auto', padding: '0 var(--container-pad)' }}>
              <Eyebrow>Case study</Eyebrow>
              <div className="wc-svc-overview" style={{ display: 'grid', gridTemplateColumns: '1.4fr 1fr', gap: 28, marginTop: 18, alignItems: 'stretch' }}>
                <div style={{ background: 'var(--surface-card)', border: '1px solid var(--border-subtle)', borderLeft: '4px solid ' + accent, borderRadius: 'var(--radius-lg)', padding: '30px 32px' }}>
                  <div style={{ fontFamily: 'var(--font-display)', fontWeight: 800, fontSize: '1.5rem', color: 'var(--text-strong)' }}>{relCase.client}</div>
                  <div style={{ fontFamily: 'var(--font-body)', fontSize: '0.875rem', color: 'var(--text-muted)', marginTop: 4 }}>{relCase.descriptor}</div>
                  <p style={{ fontFamily: 'var(--font-body)', fontSize: '1.0625rem', lineHeight: 1.6, color: 'var(--text-body)', margin: '16px 0 0', maxWidth: 520 }}>{relCase.summary}</p>
                  <a href={'case-study.html?slug=' + relCase.slug} style={{ display: 'inline-flex', alignItems: 'center', gap: 7, marginTop: 20, fontFamily: 'var(--font-body)', fontWeight: 700, fontSize: '0.9375rem', color: 'var(--svc-accent)', textDecoration: 'none' }}>Read the case study <I.ArrowRight size={16} /></a>
                </div>
                <div style={{ borderRadius: 'var(--radius-lg)', background: 'linear-gradient(150deg, var(--svc-deep) 0%, var(--wc-ink-900) 80%)', position: 'relative', overflow: 'hidden', minHeight: 200 }}>
                  <div style={{ position: 'absolute', inset: 0, backgroundImage: "url('../../assets/backgrounds/bg-03.svg')", backgroundSize: 'cover', backgroundPosition: 'center', opacity: 0.7, mixBlendMode: 'soft-light' }} />
                </div>
              </div>
            </div>
          </section>
        ) : null}

        {/* More in this pillar */}
        <section style={{ background: relCase ? 'var(--wc-white)' : 'var(--surface-page)', padding: '72px 0', borderTop: '1px solid var(--border-subtle)' }}>
          <div style={{ maxWidth: 'var(--container-max)', margin: '0 auto', padding: '0 var(--container-pad)' }}>
            <Eyebrow>More in {o.label}</Eyebrow>
            <h2 style={{ fontFamily: 'var(--font-display)', fontWeight: 800, fontSize: '1.75rem', lineHeight: 1.15, color: 'var(--text-strong)', margin: '12px 0 0' }}>Explore the rest of the pillar.</h2>
            <div style={{ display: 'flex', flexWrap: 'wrap', gap: 12, marginTop: 24 }}>
              {siblings.map((s) => (
                <a key={s.slug} href={svcHref(s.slug)} style={{ display: 'inline-flex', alignItems: 'center', gap: 8, padding: '12px 18px', background: relCase ? 'var(--surface-page)' : 'var(--wc-white)', border: '1px solid var(--border-subtle)', borderRadius: 'var(--radius-pill)', textDecoration: 'none', fontFamily: 'var(--font-body)', fontWeight: 600, fontSize: '0.9375rem', color: 'var(--text-strong)' }}
                  onMouseEnter={(e) => { e.currentTarget.style.borderColor = accent; }}
                  onMouseLeave={(e) => { e.currentTarget.style.borderColor = 'var(--border-subtle)'; }}>
                  <span style={{ width: 7, height: 7, borderRadius: '50%', background: accent }} />{s.name}
                </a>
              ))}
            </div>
          </div>
        </section>

        {/* CTA */}
        <section style={{ position: 'relative', overflow: 'hidden', background: 'var(--grad-emerald)' }}>
          <div style={{ position: 'absolute', inset: 0, backgroundImage: "url('../../assets/backgrounds/bg-05.svg')", backgroundSize: 'cover', opacity: 0.5, mixBlendMode: 'soft-light' }} />
          <div style={{ position: 'relative', maxWidth: 'var(--container-max)', margin: '0 auto', padding: '72px var(--container-pad)', textAlign: 'center' }}>
            <h2 style={{ fontFamily: 'var(--font-display)', fontWeight: 900, fontSize: 'clamp(1.875rem, 3.5vw, 2.75rem)', lineHeight: 1.1, letterSpacing: '-0.02em', color: 'var(--text-strong)', margin: '0 auto', maxWidth: 640, textWrap: 'balance' }}>{svc.ctaHead}</h2>
            <p style={{ fontFamily: 'var(--font-body)', fontSize: '1.125rem', lineHeight: 1.6, color: 'var(--text-strong)', margin: '16px auto 0', maxWidth: 540 }}>{svc.ctaSub}</p>
            <div style={{ display: 'flex', justifyContent: 'center', gap: 14, marginTop: 30 }}>
              <Button size="lg" variant="secondary" onClick={() => { window.location.href = 'index.html#assessment'; }}>Book a Strategy Call</Button>
            </div>
          </div>
        </section>
      </article>
    );
  }

  window.WCKit = Object.assign(window.WCKit || {}, { ServicePage });
})();
