// Direction C — PROJECT LANES. Swimlane / kanban by property. A decision
// banner spans the top; each project is a lane of stacked stage cards
// (pipeline for in-flight work, shipped/commerce cards for live ones).
// Spend + momentum strip anchors the bottom.

function C_Header() {
  const m = WW.meta;
  return (
    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 20 }}>
      <div style={{ display: 'flex', alignItems: 'baseline', gap: 12 }}>
        <span className="ww-h" style={{ fontSize: 24, letterSpacing: '-0.02em' }}>{m.title}</span>
        <span style={{ color: 'var(--text-3)', fontSize: 15 }}>{m.subtitle}</span>
        <span style={{ color: 'var(--text-4)', fontSize: 13 }}>· by property</span>
      </div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 18 }}>
        {WW.kpis.map((k, i) => (
          <div key={i} style={{ display: 'flex', alignItems: 'baseline', gap: 6 }}>
            <span className="ww-h ww-tnum" style={{ fontSize: 18, color: k.tone ? (TONE[k.tone] || TONE.neutral).fg : 'var(--text)' }}>{k.value}</span>
            <span className="ww-mono" style={{ fontSize: 10, color: 'var(--text-4)', textTransform: 'uppercase', letterSpacing: '.04em' }}>{k.label}</span>
          </div>
        ))}
      </div>
    </div>
  );
}

function C_DecisionBanner() {
  return (
    <div className="ww-card" style={{ padding: '18px 22px', marginBottom: 22, borderTop: '3px solid var(--accent)' }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 12, marginBottom: 14 }}>
        <span style={{ color: 'var(--accent)', display: 'flex' }}><Icon.bolt s={19} /></span>
        <span className="ww-h" style={{ fontSize: 18 }}>Needs you today</span>
        <span style={{ flex: 1 }} />
        <span style={{ display: 'inline-flex', alignItems: 'center', gap: 8, fontSize: 13, fontWeight: 600, color: 'var(--accent)' }}>
          <Icon.clock s={15} /> {WW.meta.call}
        </span>
      </div>
      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(5,1fr)', gap: 12 }}>
        {WW.decisions.map(d => (
          <div key={d.n} style={{ background: 'var(--surface-2)', border: '1px solid var(--border)', borderRadius: 'var(--radius-sm)', padding: '12px 13px', display: 'flex', flexDirection: 'column', gap: 8 }}>
            <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
              <span className="ww-mono ww-h" style={{ fontSize: 12, color: 'var(--accent)' }}>{String(d.n).padStart(2, '0')}</span>
              <Tag>{d.area}</Tag>
            </div>
            <div className="ww-h" style={{ fontSize: 13, lineHeight: 1.25, flex: 1 }}>{d.title}</div>
            <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 6 }}>
              <span className="ww-rec" style={{ fontSize: 10.5, padding: '2px 8px' }}><Icon.check s={10} /> {d.rec}</span>
              <span className="ww-mono" style={{ fontSize: 10.5, color: 'var(--text-3)' }}>{d.cost}</span>
            </div>
          </div>
        ))}
      </div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 16, marginTop: 14, paddingTop: 14, borderTop: '1px solid var(--border)' }}>
        <span className="ww-mono" style={{ fontSize: 11, color: 'var(--text-3)', textTransform: 'uppercase', letterSpacing: '.06em', flex: 'none' }}>Ready on your go</span>
        {WW.goSignals.map(g => (
          <div key={g.ref} style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
            <span className="ww-mono" style={{ fontSize: 10.5, color: 'var(--accent)' }}>{g.ref}</span>
            <span style={{ fontSize: 12.5, color: 'var(--text-2)' }}>{g.title}</span>
            <Tag>{g.owner}</Tag>
          </div>
        ))}
      </div>
    </div>
  );
}

// generic stage card in a lane
function C_Card({ tone = 'neutral', children, onLeor }) {
  const t = TONE[tone] || TONE.neutral;
  return (
    <div className="ww-card" style={{ padding: '12px 14px', borderLeft: `3px solid ${t.solid}`,
      boxShadow: onLeor ? `0 0 0 1px ${t.border}` : 'var(--shadow-sm)' }}>
      {children}
    </div>
  );
}

function C_LaneHead({ p }) {
  return (
    <div style={{ marginBottom: 12 }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 8 }}>
        <Tag>{p.key}</Tag>
        <Pill tone={p.status}>{p.statusLabel}</Pill>
      </div>
      <div className="ww-h" style={{ fontSize: 17 }}>{p.name}</div>
      <div style={{ fontSize: 11.5, color: 'var(--text-3)', lineHeight: 1.35 }}>{p.tagline}</div>
      <div style={{ display: 'flex', flexWrap: 'wrap', gap: '5px 10px', marginTop: 10 }}>
        {p.metrics.map(([k, v]) => (
          <span key={k} style={{ fontSize: 10.5, color: 'var(--text-3)' }}>
            {k} <span className="ww-mono" style={{ color: 'var(--text-2)', fontWeight: 600 }}>{v}</span>
          </span>
        ))}
      </div>
    </div>
  );
}

function C_Lane({ p, children }) {
  return (
    <div style={{ display: 'flex', flexDirection: 'column' }}>
      <div style={{ borderBottom: '2px solid var(--border-strong)', paddingBottom: 12, marginBottom: 14 }}>
        <C_LaneHead p={p} />
      </div>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>{children}</div>
    </div>
  );
}

function C_Lanes({ cols = 4 } = {}) {
  const byKey = Object.fromEntries(WW.projects.map(p => [p.key, p]));
  const winsFor = (tag) => WW.wins.filter(w => w.tag === tag);

  return (
    <div style={{ display: 'grid', gridTemplateColumns: `repeat(${cols},1fr)`, gap: 18, marginBottom: 22, alignItems: 'start' }}>

      {/* COV */}
      <C_Lane p={byKey.COV}>
        <Eyebrow>Shipped</Eyebrow>
        {winsFor('COV').map((w, i) => (
          <C_Card key={i} tone="ok">
            <div style={{ display: 'flex', gap: 8, alignItems: 'flex-start' }}>
              <span style={{ color: 'var(--ok-solid)', flex: 'none', marginTop: 1 }}><Icon.check s={13} /></span>
              <div>
                <div style={{ fontSize: 12, color: 'var(--text-2)', lineHeight: 1.4 }}>{w.text}</div>
                <div className="ww-mono" style={{ fontSize: 10, color: 'var(--text-4)', marginTop: 4 }}>{w.t}</div>
              </div>
            </div>
          </C_Card>
        ))}
      </C_Lane>

      {/* BPC */}
      <C_Lane p={byKey.BPC}>
        <Eyebrow>Shipped</Eyebrow>
        {winsFor('BPC').map((w, i) => (
          <C_Card key={i} tone="ok">
            <div style={{ display: 'flex', gap: 8, alignItems: 'flex-start' }}>
              <span style={{ color: 'var(--ok-solid)', flex: 'none', marginTop: 1 }}><Icon.check s={13} /></span>
              <div>
                <div style={{ fontSize: 12, color: 'var(--text-2)', lineHeight: 1.4 }}>{w.text}</div>
                <div className="ww-mono" style={{ fontSize: 10, color: 'var(--text-4)', marginTop: 4 }}>{w.t}</div>
              </div>
            </div>
          </C_Card>
        ))}
        {winsFor('AFF').map((w, i) => (
          <C_Card key={'a' + i} tone="warn">
            <div style={{ fontSize: 12, color: 'var(--text-2)', lineHeight: 1.4 }}>{w.text}</div>
            <div className="ww-mono" style={{ fontSize: 10, color: 'var(--text-4)', marginTop: 4 }}>affiliate · {w.t}</div>
          </C_Card>
        ))}
      </C_Lane>

      {/* GROPLATE — workstream pipeline */}
      <C_Lane p={byKey.GROPLATE}>
        <Eyebrow>Pipeline · 5 workstreams</Eyebrow>
        {WW.groplate.workstreams.map(w => (
          <C_Card key={w.name} tone={w.tone} onLeor={w.onLeor}>
            <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 7 }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 7 }}>
                {w.onLeor && <span style={{ color: 'var(--accent)', display: 'flex' }}><Icon.bolt s={12} /></span>}
                <span className="ww-h" style={{ fontSize: 13 }}>{w.name}</span>
              </div>
              <Pill tone={w.tone} dot={false} style={{ fontSize: 9 }}>{w.state}</Pill>
            </div>
            <Track value={w.progress} tone={w.tone} height={5} />
            <div style={{ fontSize: 11, color: 'var(--text-2)', lineHeight: 1.4, marginTop: 8 }}>{w.next}</div>
            <div style={{ display: 'flex', gap: 6, alignItems: 'flex-start', marginTop: 8, paddingTop: 8, borderTop: '1px solid var(--border)' }}>
              <span style={{ fontSize: 10.5, color: w.onLeor ? 'var(--text)' : 'var(--text-3)', fontWeight: w.onLeor ? 600 : 400, lineHeight: 1.35 }}>{w.blocker}</span>
            </div>
          </C_Card>
        ))}
      </C_Lane>

      {/* PADFOUNDRY — commerce + risks */}
      <C_Lane p={byKey.PADFOUNDRY}>
        <Eyebrow>Commerce</Eyebrow>
        {WW.commerce.map(c => (
          <C_Card key={c.item} tone={c.tone}>
            <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 5 }}>
              <span className="ww-h" style={{ fontSize: 12.5 }}>{c.item}</span>
              <Pill tone={c.tone} dot={false} style={{ fontSize: 9 }}>{c.state}</Pill>
            </div>
            <div style={{ fontSize: 11, color: 'var(--text-3)', lineHeight: 1.4 }}>{c.value}</div>
          </C_Card>
        ))}
        <Eyebrow style={{ marginTop: 6 }}>Critical risks · 4</Eyebrow>
        {WW.risks.map(r => (
          <C_Card key={r.ref} tone={r.tone}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 7, marginBottom: 4 }}>
              <Pill tone={r.tone} dot={false} style={{ fontSize: 9 }}>{r.level}</Pill>
              <span className="ww-h" style={{ fontSize: 12.5, flex: 1 }}>{r.title}</span>
              <span className="ww-mono" style={{ fontSize: 9.5, color: 'var(--text-4)' }}>{r.ref}</span>
            </div>
            <div style={{ fontSize: 10.5, color: 'var(--text-3)', lineHeight: 1.4 }}>{r.detail}</div>
          </C_Card>
        ))}
      </C_Lane>
    </div>
  );
}

function C_Bottom() {
  const s = WW.spend;
  const max = Math.max(...s.items.map(i => i.hi));
  const r = WW.radar;
  return (
    <div style={{ display: 'grid', gridTemplateColumns: '1.7fr 1fr', gap: 18 }}>
      <div className="ww-card" style={{ padding: '20px 22px' }}>
        <SectionHead eyebrow="Imminent spend" title="On the horizon"
          right={<div style={{ textAlign: 'right' }}><div className="ww-h ww-tnum" style={{ fontSize: 20, color: 'var(--accent)' }}>{s.committedLabel}</div><div style={{ fontSize: 10.5, color: 'var(--text-3)' }}>{s.caption}</div></div>} />
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '12px 28px' }}>
          {s.items.map(it => (
            <div key={it.item}>
              <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', marginBottom: 5 }}>
                <span className="ww-h" style={{ fontSize: 12 }}>{it.item}</span>
                <span className="ww-mono ww-tnum" style={{ fontSize: 11.5, color: 'var(--text-2)' }}>{it.range}</span>
              </div>
              <RangeBar lo={it.lo} hi={it.hi} scaleMax={max} tone="info" height={7} />
              <div style={{ fontSize: 10, color: 'var(--text-4)', marginTop: 4 }}>{it.status}</div>
            </div>
          ))}
        </div>
      </div>
      <div className="ww-card" style={{ padding: '20px 22px', display: 'flex', flexDirection: 'column' }}>
        <SectionHead eyebrow="Padfoundry radar" title="Signal trend"
          right={<span style={{ color: 'var(--text-4)', display: 'flex' }}><Icon.radar s={18} /></span>} />
        <Columns data={r.trend} width={290} height={104} tone="info" />
        <div className="ww-mono" style={{ fontSize: 9.5, color: 'var(--text-4)', marginTop: 'auto', paddingTop: 14 }}>7-day count · as of {r.asOf}</div>
      </div>
    </div>
  );
}

function DirectionC() {
  return (
    <div className="ww" style={{ background: 'var(--surface-2)', padding: '32px 36px', minHeight: '100%', color: 'var(--text-2)' }}>
      <C_Header />
      <C_DecisionBanner />
      <C_Lanes />
      <C_Bottom />
    </div>
  );
}

window.DirectionC = DirectionC;
