/* ============================================================
   VIZ — kept deliberately small.

   The landing page gets ONE picture (the four links, in the demo's own
   icon language: the pill is a measure someone chose) and ONE light
   line (where the field stands). Everything that used to live here is
   parked in _notes/unused/_attic.jsx, which is not served — the deep pages
   keep only the three components they actually use.
   ============================================================ */
const { useState: vState } = React;

/* ---------------------------------------------------------------- */
/* Icons — drawn rather than emoji so they hold up at display size,
   but the meaning is the demo's: a pill is a measure someone chose.  */

const MedIcon = {
  pulse: ({ s = 28 }) => (
    <svg viewBox="0 0 24 24" width={s} height={s} fill="none" aria-hidden="true">
      <path d="M2 12h4l2.5-6 3.5 12 2.5-6H22" stroke="currentColor" strokeWidth="1.7"
        strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  break: ({ s = 28 }) => (
    <svg viewBox="0 0 24 24" width={s} height={s} fill="none" aria-hidden="true">
      <path d="M12 20.5S3.5 15 3.5 8.9A4.4 4.4 0 0 1 12 7a4.4 4.4 0 0 1 8.5 1.9c0 6.1-8.5 11.6-8.5 11.6z"
        stroke="currentColor" strokeWidth="1.6" strokeLinejoin="round"/>
      <path d="M12 6.6l-2 3.9 3.2 1.8-2.4 3.9" stroke="currentColor" strokeWidth="1.6"
        strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  euro: ({ s = 28 }) => (
    <svg viewBox="0 0 24 24" width={s} height={s} fill="none" aria-hidden="true">
      <circle cx="12" cy="12" r="9" stroke="currentColor" strokeWidth="1.6"/>
      <path d="M15.4 8.6a4.2 4.2 0 0 0-6.7 1.2 5.4 5.4 0 0 0 0 4.4 4.2 4.2 0 0 0 6.7 1.2M7.4 10.9h6M7.4 13.5h6"
        stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
    </svg>
  ),
  pill: ({ s = 28 }) => (
    <svg viewBox="0 0 24 24" width={s} height={s} fill="none" aria-hidden="true">
      <rect x="2.6" y="8.4" width="18.8" height="7.2" rx="3.6"
        transform="rotate(-45 12 12)" stroke="currentColor" strokeWidth="1.6"/>
      <path d="M9.5 6.9l7.6 7.6" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
    </svg>
  ),
};

/* ---------------------------------------------------------------- */
/* The one picture: the chain in the direction it runs — measure, cause,
   outcome. The right-hand pair is what we bring; the measure on the left
   is the open end, so it is the only card carrying colour.            */

function PictureRow() {
  const { PICTURE } = window.GD;
  return (
    <div>
      <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
        {PICTURE.map((p, i) => {
          const I = MedIcon[p.icon];
          return (
            <div key={p.us} className="relative rounded-2xl border p-6"
              style={p.ours
                ? { borderColor: 'rgba(34,211,238,0.6)', borderStyle: 'dashed', background: 'rgba(34,211,238,0.06)' }
                : { borderColor: 'rgb(var(--c-line))' }}>
              <div style={{ color: p.ours ? '#0891B2' : 'rgb(var(--c-mute))' }}><I s={30}/></div>
              <div className="mt-4 mono text-[9px] text-mute">{p.med.toUpperCase()}</div>
              <div className="mt-1.5 text-[18px] font-semibold tracking-tight leading-snug">{p.us}</div>
              <p className="mt-2 text-[12.5px] text-mute leading-relaxed">{p.b}</p>
              {i < PICTURE.length - 1 && (
                <span aria-hidden className="hidden md:block absolute"
                  style={{ right: -13, top: '50%', transform: 'translateY(-50%)' }}>
                  <svg viewBox="0 0 24 24" width="14" height="14" fill="none">
                    <path d="M5 12h14M13 6l6 6-6 6" stroke="var(--mute)" strokeWidth="1.7"
                      strokeLinecap="round" strokeLinejoin="round"/>
                  </svg>
                </span>
              )}
            </div>
          );
        })}
      </div>

      {/* who owns which end of the chain — one thin row, no decoration */}
      <div className="hidden md:grid grid-cols-3 gap-4 mt-3">
        <div className="rounded-lg px-4 py-2.5 text-center"
          style={{ background: 'rgba(34,211,238,0.08)', border: '1px dashed rgba(34,211,238,0.55)' }}>
          <span className="mono text-[9px]" style={{ color: '#0891B2' }}>WE FIND THESE TOGETHER</span>
        </div>
        <div className="col-span-2 rounded-lg bg-off border border-line px-4 py-2.5 text-center">
          <span className="mono text-[9px] text-mute">THIS THE PRODUCT MEASURES</span>
        </div>
      </div>
    </div>
  );
}

/* ---------------------------------------------------------------- */
/* The light line: five dots, one marker, nothing else.              */

function PathLine() {
  const { PATH, PATH_HERE } = window.GD;
  return (
    <div>
      <div className="hidden md:flex items-center">
        {PATH.map((label, i) => (
          <React.Fragment key={label}>
            <span className={`rounded-full shrink-0 ${i === PATH_HERE ? 'pulse' : ''}`}
              style={{
                height: i === PATH_HERE ? 12 : 8,
                width: i === PATH_HERE ? 12 : 8,
                background: i === PATH_HERE ? '#22D3EE' : 'rgb(var(--c-line))',
                border: i === PATH_HERE ? 'none' : '1px solid rgb(var(--c-subtle))',
              }}/>
            {i < PATH.length - 1 && <span className="flex-1 h-px" style={{ background: 'rgb(var(--c-line))' }}/>}
          </React.Fragment>
        ))}
      </div>
      <div className="hidden md:flex mt-3">
        {PATH.map((label, i) => (
          <div key={label} className="flex-1 pr-4">
            <div className="text-[12.5px] leading-snug"
              style={{ color: i === PATH_HERE ? 'rgb(var(--c-ink))' : 'rgb(var(--c-mute))',
                       fontWeight: i === PATH_HERE ? 600 : 400 }}>
              {label}
            </div>
            {i === PATH_HERE && <div className="mt-1 mono text-[9px]" style={{ color: '#0891B2' }}>WE ARE HERE</div>}
          </div>
        ))}
      </div>

      <ol className="md:hidden space-y-2">
        {PATH.map((label, i) => (
          <li key={label} className="flex items-center gap-3">
            <span className="rounded-full shrink-0"
              style={{ height: 8, width: 8, background: i === PATH_HERE ? '#22D3EE' : 'rgb(var(--c-line))' }}/>
            <span className="text-[13px]"
              style={{ color: i === PATH_HERE ? 'rgb(var(--c-ink))' : 'rgb(var(--c-mute))',
                       fontWeight: i === PATH_HERE ? 600 : 400 }}>
              {label}{i === PATH_HERE ? ' — we are here' : ''}
            </span>
          </li>
        ))}
      </ol>
    </div>
  );
}

/* ---------------------------------------------------------------- */
/* The questions. One open at a time, so the section stays short.    */

function QAList() {
  const { QA } = window.GD;
  const [open, setOpen] = vState(0);
  return (
    <div className="rounded-2xl border border-line overflow-hidden">
      {QA.map((item, i) => {
        const isOpen = open === i;
        return (
          <div key={item.q} className={i > 0 ? 'border-t border-line' : ''}>
            <button type="button" onClick={() => setOpen(isOpen ? -1 : i)}
              className="w-full text-left px-5 sm:px-6 py-4 flex items-start gap-4 hover:bg-off transition-colors"
              aria-expanded={isOpen}>
              <span className="mono text-[8.5px] text-mute mt-1.5 w-9 shrink-0">{item.who}</span>
              <span className="text-[15px] font-medium leading-snug flex-1">{item.q}</span>
              <span className="text-mute mt-0.5 shrink-0"
                style={{ transform: isOpen ? 'rotate(180deg)' : 'none' }}>
                <svg viewBox="0 0 24 24" width="16" height="16" fill="none">
                  <path d="M6 9l6 6 6-6" stroke="currentColor" strokeWidth="1.7"
                    strokeLinecap="round" strokeLinejoin="round"/>
                </svg>
              </span>
            </button>
            {isOpen && (
              <div className="px-5 sm:px-6 pb-5" style={{ paddingLeft: 76 }}>
                <p className="text-[13.5px] text-mute leading-relaxed max-w-2xl">{item.a}</p>
              </div>
            )}
          </div>
        );
      })}
    </div>
  );
}

/* ---------------------------------------------------------------- */
/* Still used by the evidence and method pages.                      */

function RandomDrawStrip({ k = 0, label = 'the observed effect' }) {
  const cells = [];
  for (let i = 0; i < 400; i++) {
    const hit = i < k;
    cells.push(
      <span key={i} className="rounded-[1px]"
        style={{ display: 'block', width: '100%', aspectRatio: '1 / 1',
                 background: hit ? '#B45309' : 'var(--line)', opacity: hit ? 1 : 0.85 }}/>
    );
  }
  return (
    <div>
      <div className="grid gap-[2px]" style={{ gridTemplateColumns: 'repeat(40, minmax(0, 1fr))' }}>
        {cells}
      </div>
      <p className="mt-3 text-[12.5px] text-mute leading-relaxed">
        400 random cohorts of sites where nothing happened.{' '}
        <span className="text-ink font-medium">
          {k === 0 ? `None of them reached ${label}.` : `${k} of them reached ${label}.`}
        </span>
      </p>
    </div>
  );
}

function FiveLights({ checks }) {
  const { FIVE } = window.GD;
  const [open, setOpen] = vState(FIVE[0].id);
  const cur = checks[open];
  return (
    <div>
      <div className="flex flex-wrap gap-2">
        {FIVE.map(f => {
          const [tone] = checks[f.id];
          const active = open === f.id;
          const col = tone === 'green' ? '#16A34A' : tone === 'amber' ? '#B45309' : 'var(--mute)';
          return (
            <button key={f.id} type="button" onClick={() => setOpen(f.id)}
              className={`inline-flex items-center gap-2 rounded-full border px-3 py-1.5 transition-colors ${
                active ? 'border-ink/40 bg-off' : 'border-line hover:border-ink/25'}`}>
              <span className="h-2 w-2 rounded-full" style={{ background: col }}/>
              <span className="mono text-[9.5px]"
                style={{ color: active ? 'rgb(var(--c-ink))' : 'rgb(var(--c-mute))' }}>{f.q}</span>
            </button>
          );
        })}
      </div>
      <div className="mt-4 rounded-xl bg-off border border-line p-4">
        <div className="mono text-[9.5px] text-mute">{FIVE.find(f => f.id === open).ask}</div>
        <p className="mt-2 text-[13px] leading-relaxed"
          style={{ color: cur[0] === 'amber' ? '#B45309' : 'rgb(var(--c-ink))' }}>{cur[1]}</p>
      </div>
    </div>
  );
}

function LadderViz() {
  const { LADDER } = window.GD;
  return (
    <div className="rounded-2xl border border-line overflow-hidden">
      {LADDER.map((l, i) => (
        <div key={l.rung}
          className={`grid grid-cols-1 sm:grid-cols-12 gap-3 px-5 py-4 ${i > 0 ? 'border-t border-line' : ''}`}>
          <div className="sm:col-span-3 flex items-center"><Chip tone={l.tone}>{l.rung.toUpperCase()}</Chip></div>
          <div className="sm:col-span-5 text-[13px] text-mute leading-relaxed flex items-center">{l.when}</div>
          <div className="sm:col-span-4 text-[13px] leading-relaxed flex items-center">{l.say}</div>
        </div>
      ))}
    </div>
  );
}

window.MedIcon = MedIcon;
window.PictureRow = PictureRow;
window.PathLine = PathLine;
window.QAList = QAList;
window.RandomDrawStrip = RandomDrawStrip;
window.FiveLights = FiveLights;
window.LadderViz = LadderViz;
