// Monjez landing — The Operation System (interactive 5-layer flow + live previews)
const { useState: useStateSys } = React;

function accentVars(a) {
  return a === 'teal'
    ? { c: 'var(--teal-signal)', soft: 'var(--teal-soft)', glow: 'rgba(20,184,166,0.45)' }
    : { c: 'var(--blue-400)', soft: 'var(--blue-soft)', glow: 'rgba(37,99,235,0.5)' };
}

function OperationSystem() {
  const t = window.useT();
  const S = t.system;
  const layers = S.layers;
  const [active, setActive] = useStateSys(0);
  const L = layers[active];
  const av = accentVars(L.accent);

  return (
    <section id="system" style={{ position: 'relative', padding: '96px 0', borderTop: '1px solid var(--border-subtle)' }}>
      <window.Container>
        <window.Reveal>
          <window.SectionHead
            eyebrow={S.eyebrow}
            title={<span>{S.title.pre}<window.Accent>{S.title.accent}</window.Accent>{S.title.post}</span>}
            sub={S.sub}
          />
        </window.Reveal>

        {/* stepper */}
        <window.Reveal delay={0.05}>
          <div style={{ marginTop: 52, position: 'relative' }}>
            <div className="mj-stepper" style={{ display: 'grid', gridTemplateColumns: `repeat(${layers.length}, 1fr)`, gap: 10, position: 'relative' }}>
              <div style={{ position: 'absolute', top: 32, left: '10%', right: '10%', height: 2, background: 'var(--border-subtle)', zIndex: 0 }} />
              <div style={{ position: 'absolute', top: 32, insetInlineStart: '10%', width: `${(active / (layers.length - 1)) * 80}%`, height: 2, background: 'linear-gradient(90deg, var(--electric-blue), var(--teal-signal))', zIndex: 0, transition: 'width .4s cubic-bezier(.4,0,.2,1)' }} />
              {layers.map((ly, i) => {
                const on = i === active;
                const lv = accentVars(ly.accent);
                const Ic = window[ly.icon];
                return (
                  <button key={ly.id} onClick={() => setActive(i)}
                    style={{ position: 'relative', zIndex: 1, background: 'none', border: 'none', cursor: 'pointer', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 12, padding: 0 }}>
                    <span style={{
                      width: 64, height: 64, borderRadius: 18, display: 'grid', placeItems: 'center',
                      background: on ? lv.soft : 'var(--graphite)', color: on ? lv.c : 'var(--slate-gray)',
                      border: '1px solid ' + (on ? lv.c : 'var(--border-subtle)'),
                      boxShadow: on ? `0 0 0 4px ${lv.soft}, 0 10px 30px ${lv.glow}` : 'none',
                      transition: 'all .25s ease', transform: on ? 'translateY(-2px)' : 'none',
                    }}>
                      <Ic size={26} stroke="currentColor" />
                    </span>
                    <span style={{ textAlign: 'center' }}>
                      <span style={{ display: 'block', fontFamily: 'var(--font-mono)', fontSize: 10.5, letterSpacing: '0.08em', color: on ? lv.c : 'var(--text-muted)' }}>{ly.tag}</span>
                      <span className="mj-step-name" style={{ display: 'block', fontSize: 13.5, fontWeight: on ? 600 : 500, color: on ? 'var(--cloud-white)' : 'var(--slate-gray)', marginTop: 3, lineHeight: 1.25 }}>{ly.name}</span>
                    </span>
                  </button>
                );
              })}
            </div>
            <div className="mj-flowlabels" style={{ display: 'flex', justifyContent: 'space-between', marginTop: 14, fontSize: 12, color: 'var(--text-muted)', fontFamily: 'var(--font-mono)', letterSpacing: '0.04em' }}>
              <span>{S.flowIn}</span>
              <span>{S.flowOut}</span>
            </div>
          </div>
        </window.Reveal>

        {/* detail */}
        <div className="mj-sys-detail" style={{ marginTop: 40, display: 'grid', gridTemplateColumns: '1fr 1.08fr', gap: 28, alignItems: 'stretch' }}>
          <div key={'info' + active} style={{ animation: 'mjFadeUp .4s ease' }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 12, marginBottom: 18 }}>
              <span style={{ width: 46, height: 46, borderRadius: 12, flexShrink: 0, display: 'grid', placeItems: 'center', background: av.soft, color: av.c, border: `1px solid ${av.c}` }}>
                {React.createElement(window[L.icon], { size: 22 })}
              </span>
              <div>
                <div style={{ fontFamily: 'var(--font-display)', fontWeight: 600, fontSize: 24, letterSpacing: '-0.02em' }}>{L.name}</div>
                <div style={{ fontSize: 14, color: av.c }}>{L.tagline}</div>
              </div>
            </div>

            <div style={{ marginBottom: 18 }}>
              <div style={{ fontFamily: 'var(--font-mono)', fontSize: 11, letterSpacing: '0.08em', color: 'var(--danger)', opacity: 0.85, marginBottom: 7 }}>{S.bottleneck}</div>
              <p style={{ margin: 0, fontSize: 15.5, lineHeight: 1.6, color: 'var(--text-secondary)' }}>{L.problem}</p>
            </div>
            <div style={{ marginBottom: 20 }}>
              <div style={{ fontFamily: 'var(--font-mono)', fontSize: 11, letterSpacing: '0.08em', color: av.c, marginBottom: 7 }}>{S.builds}</div>
              <p style={{ margin: 0, fontSize: 15.5, lineHeight: 1.6, color: 'var(--cloud-white)' }}>{L.build}</p>
            </div>

            <div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginBottom: 22 }}>
              {L.caps.map((c) => (
                <span key={c} style={{ display: 'inline-flex', alignItems: 'center', gap: 7, fontSize: 13, color: 'var(--text-secondary)', background: 'var(--white-04)', border: '1px solid var(--border-subtle)', borderRadius: 'var(--radius-pill)', padding: '6px 12px' }}>
                  <window.IconCheck size={14} stroke={av.c} />{c}
                </span>
              ))}
            </div>

            <div style={{ display: 'flex', alignItems: 'flex-start', gap: 12, padding: '16px 18px', borderRadius: 'var(--radius-md)', background: av.soft, border: `1px solid ${av.c}` }}>
              <window.IconSparkles size={20} stroke={av.c} style={{ flexShrink: 0, marginTop: 2 }} />
              <div style={{ fontFamily: 'var(--font-display)', fontWeight: 600, fontSize: 17, lineHeight: 1.4, color: 'var(--cloud-white)' }}>{L.outcome}</div>
            </div>
          </div>

          <window.Card padding={0} style={{ overflow: 'hidden', display: 'flex', flexDirection: 'column' }}>
            <div key={'prev' + active} style={{ flex: 1, animation: 'mjFade .45s ease', display: 'flex', flexDirection: 'column' }}>
              <LayerPreview id={L.id} av={av} />
            </div>
            <div style={{ display: 'flex', borderTop: '1px solid var(--border-subtle)' }}>
              {L.metrics.map(([k, v], i) => (
                <div key={k} style={{ flex: 1, padding: '14px 18px', borderInlineStart: i ? '1px solid var(--border-subtle)' : 'none' }}>
                  <div style={{ fontSize: 11.5, color: 'var(--text-muted)' }}>{k}</div>
                  <div style={{ fontSize: 15, fontWeight: 600, color: av.c, marginTop: 4 }}>{v}</div>
                </div>
              ))}
            </div>
          </window.Card>
        </div>
      </window.Container>
    </section>
  );
}

// ---------- per-layer previews ----------
function PreviewShell({ url, children }) {
  return (
    <div style={{ background: 'var(--midnight)', flex: 1 }}>
      <div style={{ height: 38, borderBottom: '1px solid var(--border-subtle)', display: 'flex', alignItems: 'center', gap: 7, padding: '0 14px', background: 'var(--graphite-600)' }}>
        {['#EF4444', '#F59E0B', '#A78BFA'].map((c) => <span key={c} style={{ width: 9, height: 9, borderRadius: '50%', background: c, opacity: 0.8 }} />)}
        <span style={{ marginInlineStart: 10, fontSize: 11.5, color: 'var(--text-muted)', fontFamily: 'var(--font-mono)', direction: 'ltr' }}>{url}</span>
      </div>
      <div style={{ padding: 18 }}>{children}</div>
    </div>
  );
}

function Bubble({ who, app, time, children, av }) {
  return (
    <div style={{ display: 'flex', gap: 10, marginBottom: 13 }}>
      <div style={{ width: 30, height: 30, borderRadius: 8, flexShrink: 0, display: 'grid', placeItems: 'center', fontSize: 12, fontWeight: 700, fontFamily: 'var(--font-display)', color: '#fff', background: app ? 'var(--accent)' : 'var(--graphite-400)' }}>{app ? 'AI' : (who ? who[0] : '•')}</div>
      <div style={{ minWidth: 0 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 7, marginBottom: 4 }}>
          <span style={{ fontSize: 12.5, fontWeight: 600, color: 'var(--cloud-white)' }}>{who}</span>
          {app && <span style={{ fontSize: 9, fontWeight: 700, letterSpacing: '0.06em', color: 'var(--blue-400)', background: 'var(--blue-soft)', padding: '1px 5px', borderRadius: 4 }}>APP</span>}
          <span style={{ fontSize: 11, color: 'var(--text-muted)', fontFamily: 'var(--font-mono)' }}>{time}</span>
        </div>
        <div style={{ fontSize: 13, lineHeight: 1.5, color: 'var(--text-secondary)' }}>{children}</div>
      </div>
    </div>
  );
}

function LayerPreview({ id, av }) {
  const t = window.useT();
  const pv = t.pv;
  if (id === 'sales') {
    const p = pv.sales;
    return (
      <PreviewShell url={p.url}>
        <Bubble who={p.who} time="09:41">{p.lead1}</Bubble>
        <Bubble who={p.ai} app time="09:41" av={av}>{p.ai1}</Bubble>
        <Bubble who={p.who} time="09:42">{p.lead2}</Bubble>
        <div style={{ marginTop: 6, borderRadius: 12, border: `1px solid ${av.c}`, background: av.soft, padding: 14 }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 10 }}>
            <window.IconCheckCircle size={16} stroke={av.c} />
            <span style={{ fontSize: 12.5, fontWeight: 600, color: 'var(--cloud-white)' }}>{p.qTitle}</span>
            <span style={{ marginInlineStart: 'auto', fontSize: 11, fontFamily: 'var(--font-mono)', color: av.c }}>{p.score}</span>
          </div>
          {p.rows.map(([k, v]) => (
            <div key={k} style={{ display: 'flex', justifyContent: 'space-between', fontSize: 12.5, padding: '4px 0' }}>
              <span style={{ color: 'var(--text-muted)' }}>{k}</span><span style={{ color: 'var(--cloud-white)', fontWeight: 500 }}>{v}</span>
            </div>
          ))}
        </div>
      </PreviewShell>
    );
  }
  if (id === 'delivery') {
    const p = pv.delivery;
    return (
      <PreviewShell url={p.url}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 14 }}>
          <window.IconWorkflow size={16} stroke={av.c} />
          <span style={{ fontSize: 13, fontWeight: 600, color: 'var(--cloud-white)' }}>{p.header}</span>
          <span style={{ marginInlineStart: 'auto', fontSize: 11, fontFamily: 'var(--font-mono)', color: av.c }}>{p.tag}</span>
        </div>
        {p.steps.map((s, i) => {
          const done = i < p.steps.length - 1;
          return (
            <div key={s} style={{ display: 'flex', alignItems: 'center', gap: 11, padding: '9px 0', borderTop: i ? '1px solid var(--border-subtle)' : 'none' }}>
              <span style={{ width: 20, height: 20, flexShrink: 0, borderRadius: '50%', display: 'grid', placeItems: 'center', background: done ? av.soft : 'var(--graphite-400)', border: '1px solid ' + (done ? av.c : 'var(--border-subtle)') }}>
                {done ? <window.IconCheck size={12} stroke={av.c} /> : <span style={{ width: 6, height: 6, borderRadius: '50%', background: 'var(--text-muted)' }} />}
              </span>
              <span style={{ fontSize: 13, color: done ? 'var(--cloud-white)' : 'var(--text-secondary)' }}>{s}</span>
              {done && <span style={{ marginInlineStart: 'auto', fontSize: 10.5, fontFamily: 'var(--font-mono)', color: 'var(--text-muted)' }}>{p.done}</span>}
            </div>
          );
        })}
      </PreviewShell>
    );
  }
  if (id === 'portal') {
    const p = pv.portal;
    return (
      <PreviewShell url={p.url}>
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 14 }}>
          <span style={{ fontSize: 13, fontWeight: 600, color: 'var(--cloud-white)' }}>{p.title}</span>
          <span style={{ fontSize: 11, color: 'var(--teal-signal)', display: 'inline-flex', alignItems: 'center', gap: 5 }}><span style={{ width: 6, height: 6, borderRadius: '50%', background: 'var(--teal-signal)' }} />{p.status}</span>
        </div>
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 10, marginBottom: 12 }}>
          {p.metrics.map(([k, v, a]) => (
            <div key={k} style={{ borderRadius: 10, border: '1px solid var(--border-subtle)', background: 'var(--graphite)', padding: 12 }}>
              <div style={{ fontSize: 11, color: 'var(--text-muted)' }}>{k}</div>
              <div style={{ fontFamily: 'var(--font-display)', fontWeight: 600, fontSize: 20, color: a === 'teal' ? 'var(--teal-signal)' : 'var(--cloud-white)', marginTop: 4 }}>{v}</div>
            </div>
          ))}
        </div>
        <div style={{ borderRadius: 10, border: '1px solid var(--border-subtle)', padding: 12 }}>
          <div style={{ fontSize: 11.5, color: 'var(--text-muted)', marginBottom: 8 }}>{p.progress}</div>
          <div style={{ height: 8, borderRadius: 99, background: 'var(--graphite-400)', overflow: 'hidden' }}>
            <div style={{ width: '72%', height: '100%', background: 'linear-gradient(90deg, var(--electric-blue), var(--teal-signal))' }} />
          </div>
        </div>
      </PreviewShell>
    );
  }
  if (id === 'team') {
    const p = pv.team;
    return (
      <PreviewShell url={p.url}>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3,1fr)', gap: 10 }}>
          {p.cols.map(([title, items]) => (
            <div key={title}>
              <div style={{ fontSize: 11, fontWeight: 600, color: 'var(--text-muted)', marginBottom: 9 }}>{title}</div>
              <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
                {items.map((it) => (
                  <div key={it} style={{ borderRadius: 9, border: '1px solid var(--border-subtle)', background: 'var(--graphite)', padding: '10px 11px', fontSize: 12, color: 'var(--cloud-white)', lineHeight: 1.35 }}>
                    {it}
                    <div style={{ display: 'flex', alignItems: 'center', gap: 6, marginTop: 8 }}>
                      <span style={{ width: 16, height: 16, borderRadius: '50%', background: 'var(--graphite-400)' }} />
                      <span style={{ width: 16, height: 16, borderRadius: '50%', background: av.soft, border: `1px solid ${av.c}` }} />
                    </div>
                  </div>
                ))}
              </div>
            </div>
          ))}
        </div>
        <div style={{ marginTop: 12, fontSize: 11.5, color: 'var(--text-muted)', fontFamily: 'var(--font-mono)' }}>{p.note}</div>
      </PreviewShell>
    );
  }
  const p = pv.founder;
  const { MetricCard } = window.MonjezDesignSystem_019de9;
  return (
    <PreviewShell url={p.url}>
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 10 }}>
        {p.cards.map(([label, value, delta, dir, accent]) => (
          <MetricCard key={label} label={label} value={value} delta={delta} deltaDirection={dir} accent={accent} style={{ padding: 16 }} />
        ))}
      </div>
    </PreviewShell>
  );
}

Object.assign(window, { OperationSystem });
