/* journify.ai — JourneyCards: the row under the hero call card (content/homepage.json s1.journey). Four cards,
   Post, Book, Call and Review: the journey a buyer travels through the founder's business, and what changes at
   each step (JC, 2026-11-09, after Calendly's Book / Prep / Capture / Follow up row). A card shows its title and
   one line when closed; the open card shows a paragraph, three bullets with the icon of the piece that does the
   work, and one picture drawn in HTML (a post, the DM app inbox, the Next call's last exchange, a Sally review).
   One card is open at a time: hover or focus opens it above 900px, a tap toggles it; under 900px the four stack
   and all are open. The pictures are drawn by components/Pictures.jsx, the glare colours come from REC_GLARE in
   components/Recording.jsx, and the open card widens over --t-card. The How it works wave runs across the lower half of the four cards, faint on a closed card and
   in full colour on the open one (JC, 2026-11-09), and the cards join at the top.
   The row is wider than the text column (1240px). The fourth card is Learn.
   Replaced ProofTiles on 2026-11-09; the 28% and 30% live in the FAQ and on /case-studies/coaching. */

const JC_CSS = `
.jc { margin-top: 56px; }
.jc-h2 { max-width: 760px; }
.jc-wide { position: relative; left: 50%; transform: translateX(-50%); width: min(1240px, calc(100vw - 32px)); }
.jc-row { margin-top: 28px; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; align-items: stretch; transition: grid-template-columns var(--t-card) var(--ease); }
.jc-card { position: relative; display: flex; flex-direction: column; min-height: 0; padding: 24px 24px 40px; background: var(--block); border-radius: var(--r-block); cursor: default; overflow: hidden; }
.jc-card:focus-visible { outline: 2px solid var(--action); outline-offset: 2px; }
.jc-card:not(:last-child)::after { content: ''; position: absolute; top: 64px; right: -12px; width: 12px; height: 28px; background: var(--block); }
.jc-card > * { position: relative; z-index: 1; }
.jc-glare { position: absolute; left: -12%; right: -12%; bottom: -6%; height: 64%; z-index: 0; pointer-events: none; opacity: .44; transition: opacity 600ms; filter: blur(18px);
  background: radial-gradient(58% 62% at 50% 100%, color-mix(in srgb, var(--glare) 64%, transparent) 0%, transparent 70%); }  /* closed .44 x 64% reads as before; open goes to the full 64%, about a third more (JC, 2026-11-09) */
.jc-card.is-open .jc-glare { opacity: 1; }
.jc-rec { position: absolute; bottom: 0; height: 52%; z-index: 0; pointer-events: none; opacity: .3; transition: opacity 600ms; }
.jc-rec i { position: absolute; top: 50%; width: 4px; border-radius: 2px; transform: translateY(-50%); }
.jc-card.is-open .jc-rec { opacity: 1; }
.jc-card h3 { margin: 0; }
.jc-closed { margin: 10px 0 0; font-size: 15px; line-height: 1.5; color: var(--ink-2); transition: opacity var(--t-reveal); }
.jc-body { position: absolute; inset: 0; display: flex; flex-direction: column; opacity: 0; visibility: hidden; transform: translateY(10px); transition: opacity 380ms 160ms, transform 480ms 160ms, visibility 0s var(--t-card); }  /* out of the flow while closed, so a closed card stays short */
.jc-card.is-open .jc-closed { opacity: 0; position: absolute; }
.jc-card.is-open .jc-body { position: static; opacity: 1; visibility: visible; transform: none; transition: opacity 380ms 200ms, transform 480ms 200ms, visibility 0s; }
.jc-open { margin: 10px 0 0; font-size: 15px; line-height: 1.55; color: var(--ink-2); max-width: 440px; }
.jc-bullets { list-style: none; margin: 18px 0 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.jc-bullets li { display: flex; align-items: center; gap: 10px; font-size: 14px; line-height: 1.4; color: var(--ink); }
.jc-ic { flex: none; width: 26px; height: 26px; border-radius: 50%; background: var(--card); border: 1px solid var(--hairline); display: inline-flex; align-items: center; justify-content: center; }
.jc-ic img { width: 14px; height: 14px; display: block; }
.jc-ic svg { width: 14px; height: 14px; display: block; fill: none; stroke: var(--ink); stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.jc-proof { margin: 16px 0 0; font-size: 14px; line-height: 1.5; color: var(--ink-2); }
.jc-proof a { white-space: nowrap; color: var(--link); font-weight: var(--w-medium); text-decoration: underline; text-underline-offset: 4px; text-decoration-thickness: 1px; margin-left: 6px; }
.jc-pic { margin-top: 24px; max-width: 440px; }
.jc-loop { margin: 20px 0 0; font-size: 17px; line-height: 1.5; }
.jc-intro { margin: 16px 0 0; max-width: 680px; color: var(--ink-2); }   /* /tools: the loop's two paragraphs under the heading */
@media (max-width: 899px) {
  .jc { margin-top: 40px; }
  .jc-wide { left: 0; transform: none; width: auto; }
  .jc-row { grid-template-columns: minmax(0, 1fr) !important; gap: 12px; transition: none; }  /* beats the inline columns the open card sets */
  .jc-card { padding: 22px 22px 36px; }
  .jc-card:not(:last-child)::after { display: none; }
  .jc-card .jc-closed { display: none; }
  .jc-card .jc-body { position: static; opacity: 1; visibility: visible; transform: none; transition: none; }  /* transform too, or the three cards that are not open sit 10px lower than the open one */
  .jc-pic { margin-top: 18px; }
  .jc-rec { height: 44%; }
  .jc-loop { font-size: 16px; }
}
@media (prefers-reduced-motion: reduce) {
  .jc-row, .jc-closed, .jc-body, .jc-rec, .jc-glare { transition: none; }
  .jc-body { transform: none; }
}
`;

const JC_SVG = '<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">';
const JC_ICONS = {
  you: JC_SVG + '<circle cx="10" cy="6.5" r="3.2"/><path d="M3.5 17.5c.6-3.4 3.1-5.3 6.5-5.3s5.9 1.9 6.5 5.3"/></svg>',
  calendar: JC_SVG + '<rect x="2.5" y="4" width="15" height="13.5" rx="2"/><path d="M2.5 9h15M6.5 2.5v3M13.5 2.5v3"/></svg>',
  chat: JC_SVG + '<path d="M3 4h14a1.5 1.5 0 0 1 1.5 1.5v7A1.5 1.5 0 0 1 17 14h-6l-4 3.5V14H3a1.5 1.5 0 0 1-1.5-1.5v-7A1.5 1.5 0 0 1 3 4z"/></svg>',
  mic: JC_SVG + '<rect x="7" y="2.5" width="6" height="10" rx="3"/><path d="M4.5 9.5a5.5 5.5 0 0 0 11 0M10 15v2.5M7.5 17.5h5"/></svg>'
};
const JC_IMG = { linkedin: '/images/logos/linkedin-logo.svg', sally: '/images/logos/sally.svg', mark: '/images/logos/journify-mark.svg' };

function JcIcon({ name }) {
  if (JC_IMG[name]) return <span className="jc-ic"><img src={JC_IMG[name]} alt="" /></span>;
  return <span className="jc-ic" dangerouslySetInnerHTML={{ __html: JC_ICONS[name] || JC_ICONS.you }} />;
}

/* the recording across the lower half of the four cards: the How it works wave (components/HowItWorks.jsx, "calm"),
   one smooth rise and fall three times across the row, bars 4px wide and 10px apart, oxblood at the nodes and ember
   at the peaks, faint on a closed card and in full colour on the open one (JC, 2026-11-09: "the vibrations need to
   look more like this"). The wave is drawn once at the row's width and fixed to the row: each card holds a copy
   shifted left by the card's own offset and clips it, and while the columns move the offsets are updated every
   frame, so the wave stays still and the cards slide over it. Nothing else moves.
   The glare: a soft colour at the bottom of each card, its own per card (JC, 2026-11-09: yellow, red, orange,
   darker red, very subtle), a little stronger on the open card. The colours are REC_GLARE (components/Recording.jsx). */
const JC_STEP = 10;
function jcBars(rowWidth) {
  const out = [];
  for (let x = JC_STEP / 2; x < rowWidth; x += JC_STEP) {
    const u = x / rowWidth, h2 = 0.5 + 0.42 * Math.sin(u * Math.PI * 6);
    const col = h2 < 0.62
      ? recMixRgb(recRgb('--hairline'), recRgb('--action'), Math.min(1, h2 * 1.8))
      : recMixRgb(recRgb('--action'), recRgb('--ember'), Math.min(1, (h2 - 0.62) * 2.4));
    out.push({ x, h: Math.round(h2 * 92), col });
  }
  return out;
}
function recMixRgb(a, b, u) { return 'rgb(' + a.map((v, i) => Math.round(v + (b[i] - v) * u)).join(',') + ')'; }

function JcRec({ bars, rowWidth }) {
  return (
    <div className="jc-rec" aria-hidden="true" style={{ width: rowWidth, left: 0 }}>
      {bars.map((b, k) => <i key={k} style={{ left: b.x - 2, height: b.h + '%', background: b.col }} />)}
    </div>
  );
}

function JourneyCards({ journey }) {
  if (!journey || !journey.cards) return null;
  const [open, setOpen] = React.useState(0);
  const [wave, setWave] = React.useState({ bars: [], width: 0 });
  const rowRef = React.useRef(null);
  const cols = journey.cards.map((c, i) => (i === open ? '1.9fr' : '1fr')).join(' ');
  /* the wave at the row's width, redrawn on resize */
  React.useEffect(() => {
    const row = rowRef.current; if (!row) return;
    const measure = () => setWave({ bars: jcBars(row.offsetWidth), width: row.offsetWidth });
    measure();
    let rt = 0; const onResize = () => { clearTimeout(rt); rt = setTimeout(measure, 150); };
    window.addEventListener('resize', onResize);
    return () => { clearTimeout(rt); window.removeEventListener('resize', onResize); };
  }, [journey]);
  /* each card's copy of the wave is shifted left by the card's offset; while the columns move (560ms) the offsets
     follow every frame, so the wave stays fixed to the row */
  React.useEffect(() => {
    const row = rowRef.current; if (!row) return;
    const place = () => Array.from(row.children).forEach(c => { const r = c.querySelector('.jc-rec'); if (r) r.style.left = -c.offsetLeft + 'px'; });
    let raf = 0; const t0 = performance.now();
    const tick = now => { place(); if (now - t0 < 700) raf = requestAnimationFrame(tick); };   /* 700ms covers --t-card, 560ms */
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [open, wave]);
  return (
    <div className="jc">
      <style>{JC_CSS}</style>
      <h2 className="j-h2 jc-h2">{parseInline(journey.headline)}</h2>
      {journey.intro && journey.intro.map((t, i) => <p key={i} className="j-body jc-intro">{t}</p>)}
      <div className="jc-wide">
        <div className="jc-row" ref={rowRef} style={{ gridTemplateColumns: cols }} role="list" aria-label={journey.labels && journey.labels.row}>
          {journey.cards.map((c, i) => (
            <div key={c.title} role="listitem" tabIndex={0}
                 className={'jc-card' + (i === open ? ' is-open' : '')}
                 onMouseEnter={() => setOpen(i)} onFocus={() => setOpen(i)} onClick={() => setOpen(i)}>
              <div className="jc-glare" aria-hidden="true" style={{ '--glare': REC_GLARE[c.glare] || REC_GLARE.deep }} />
              <JcRec bars={wave.bars} rowWidth={wave.width} />
              <h3 className="j-h3">{c.title}</h3>
              <p className="jc-closed">{c.closed}</p>
              <div className="jc-body" aria-hidden={i !== open}>
                <p className="jc-open">{c.open}</p>
                {c.bullets && <ul className="jc-bullets">
                  {c.bullets.map((b, k) => <li key={k}><JcIcon name={b.icon} />{b.text}</li>)}
                </ul>}
                {c.proof && <p className="jc-proof">{c.proof.text}{c.proof.link && <a href={c.proof.link.href}>{c.proof.link.label}</a>}</p>}
                <div className="jc-pic"><Pic p={c.picture} /></div>
              </div>
            </div>
          ))}
        </div>
      </div>
      {journey.loop && <p className="jc-loop">{journey.loop}</p>}
    </div>
  );
}

Object.assign(window, { JourneyCards });
