// Citely landing page — shared section components.
// Rendered by landing-app.jsx inside desktop/mobile artboards.
// All sections accept { copy, direction, scale } props.

// Wordmark D2 (Mark) — renders "Citely" with tick on the i.
function Wordmark({ size = 22, color, kit = false }) {
  const c = color || 'var(--ink)';
  const tickSize = size * 0.15;
  return (
    <span className="wm" style={{ fontSize: size, color: c }}>
      C
      <span className="wi">
        ı
        <span className="wt" style={{
          width: tickSize, height: tickSize,
          top: -size * 0.04,
        }} />
      </span>
      tely
      {kit && <span style={{ marginLeft: size * 0.22, fontWeight: 400, color: 'var(--muted)' }}>Kit</span>}
    </span>
  );
}

// Section number eyebrow
function Eyebrow({ num, label }) {
  return (
    <div className="eyebrow">
      <span>§ {num}</span>
      <span className="sep">·</span>
      <span>{label}</span>
    </div>
  );
}

// Primary CTA button with arrow
function CTA({ children, sub }) {
  return (
    <div style={{ display: 'inline-flex', alignItems: 'center', gap: 20, flexWrap: 'wrap' }}>
      <button className="btn-primary">
        {children}
        <span className="arr" />
      </button>
      {sub && <span style={{ fontFamily: 'var(--mono)', fontSize: 11, letterSpacing: '0.06em', color: 'var(--muted)', textTransform: 'uppercase' }}>{sub}</span>}
    </div>
  );
}

// Navigation
function Nav({ copy, mobile }) {
  if (mobile) {
    return (
      <div style={{ padding: '20px 24px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', borderBottom: '1px solid var(--hairline)' }}>
        <Wordmark size={20} />
        <div style={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
          <span style={{ width: 20, height: 1, background: 'var(--ink)' }} />
          <span style={{ width: 20, height: 1, background: 'var(--ink)' }} />
        </div>
      </div>
    );
  }
  return (
    <div style={{ padding: '26px 72px', display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
      <Wordmark size={22} />
      <nav style={{ display: 'flex', alignItems: 'center', gap: 40 }}>
        {copy.nav.items.map((it, i) => (
          <a key={i} href="#" style={{ color: 'var(--ink)', textDecoration: 'none', fontSize: 14, letterSpacing: '-0.005em' }}>{it}</a>
        ))}
        <button className="btn-primary" style={{ padding: '10px 16px', fontSize: 13 }}>
          {copy.nav.cta}
          <span className="arr" />
        </button>
      </nav>
    </div>
  );
}

// Hero
function Hero({ copy, direction, mobile }) {
  const structured = direction === 'structured';
  const c = copy.hero;
  return (
    <section style={{ padding: mobile ? '72px 24px 80px' : '140px 72px 120px', position: 'relative' }}>
      {structured && !mobile && (
        <div style={{ position: 'absolute', top: 32, left: 72, right: 72, display: 'flex', justifyContent: 'space-between', fontFamily: 'var(--mono)', fontSize: 10, letterSpacing: '0.1em', color: 'var(--g400)', textTransform: 'uppercase' }}>
          <span>§ 00 · Index</span>
          <span>Citely · 2026</span>
        </div>
      )}
      <div style={{ marginBottom: mobile ? 28 : 48 }}>
        <div className="eyebrow" style={{ marginBottom: mobile ? 24 : 40 }}>
          <span>{c.eyebrow}</span>
        </div>
        <h1 className="kr" style={{
          fontSize: mobile ? 44 : 92,
          fontWeight: 500,
          letterSpacing: '-0.038em',
          lineHeight: mobile ? 1.05 : 0.98,
          marginBottom: mobile ? 24 : 36,
          maxWidth: mobile ? '100%' : 900,
        }}>{c.h1}</h1>
        <p style={{
          fontSize: mobile ? 16 : 21,
          lineHeight: 1.5,
          letterSpacing: '-0.012em',
          color: 'var(--g600)',
          maxWidth: mobile ? '100%' : 620,
          marginBottom: mobile ? 36 : 48,
        }}>{c.sub}</p>
        <CTA sub={c.ctaSub}>{c.cta}</CTA>
      </div>
      {!mobile && (
        <div style={{ marginTop: 120, display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 0, borderTop: '1px solid var(--hairline)' }}>
          <div style={{ padding: '20px 0', fontFamily: 'var(--mono)', fontSize: 10, letterSpacing: '0.12em', textTransform: 'uppercase', color: 'var(--muted)' }}>{c.trust}</div>
          {['NORTHWIND', 'KUMO', 'SANTO.INC'].map((t, i) => (
            <div key={i} style={{ padding: '20px 0', fontFamily: 'var(--mono)', fontSize: 12, letterSpacing: '0.18em', color: 'var(--g400)', borderLeft: '1px solid var(--hairline)', textAlign: 'center' }}>{t}</div>
          ))}
        </div>
      )}
      {mobile && (
        <div style={{ marginTop: 48, display: 'flex', gap: 18, flexWrap: 'wrap', paddingTop: 18, borderTop: '1px solid var(--hairline)', fontFamily: 'var(--mono)', fontSize: 10.5, letterSpacing: '0.16em', color: 'var(--g400)' }}>
          <span>NORTHWIND</span><span>·</span><span>KUMO</span><span>·</span><span>SANTO.INC</span>
        </div>
      )}
    </section>
  );
}

// Line chart for The Shift section — pure SVG, monochrome.
function ShiftChart() {
  // 2020 → 2026, share routed to answer engines.
  const data = [
    { x: 2020, y: 1 }, { x: 2021, y: 2 }, { x: 2022, y: 4 },
    { x: 2023, y: 9 }, { x: 2024, y: 18 }, { x: 2025, y: 28 }, { x: 2026, y: 38 },
  ];
  const w = 800, h = 300, pt = 20, pb = 30, pl = 40, pr = 20;
  const xs = data.map(d => d.x), ys = data.map(d => d.y);
  const minX = Math.min(...xs), maxX = Math.max(...xs);
  const maxY = 40;
  const px = (v) => pl + (v - minX) / (maxX - minX) * (w - pl - pr);
  const py = (v) => pt + (1 - v / maxY) * (h - pt - pb);
  const path = data.map((d, i) => (i === 0 ? 'M' : 'L') + px(d.x) + ',' + py(d.y)).join(' ');

  return (
    <div style={{ width: '100%', marginTop: 36 }}>
      <svg viewBox={`0 0 ${w} ${h}`} preserveAspectRatio="xMidYMid meet" style={{ width: '100%', height: 'auto', display: 'block' }}>
        {/* y grid */}
        {[0, 10, 20, 30, 40].map((v, i) => (
          <g key={i}>
            <line x1={pl} x2={w - pr} y1={py(v)} y2={py(v)} stroke="var(--g200)" strokeWidth="1" strokeDasharray={v === 0 ? '' : '2 4'} />
            <text x={pl - 8} y={py(v) + 3} textAnchor="end" fontFamily="var(--mono)" fontSize="10" letterSpacing="0.04em" fill="var(--muted)">{v}%</text>
          </g>
        ))}
        {/* x ticks */}
        {data.map((d, i) => (
          <text key={i} x={px(d.x)} y={h - 10} textAnchor="middle" fontFamily="var(--mono)" fontSize="10" letterSpacing="0.04em" fill="var(--muted)">{d.x}</text>
        ))}
        {/* line */}
        <path d={path} fill="none" stroke="var(--ink)" strokeWidth="1.5" strokeLinejoin="miter" />
        {/* points */}
        {data.map((d, i) => (
          <rect key={i} x={px(d.x) - 3} y={py(d.y) - 3} width="6" height="6" fill="var(--paper)" stroke="var(--ink)" strokeWidth="1.5" />
        ))}
        {/* endpoint label */}
        <g>
          <rect x={px(2026) - 3} y={py(38) - 3} width="6" height="6" fill="var(--ink)" stroke="var(--ink)" strokeWidth="1.5" />
          <line x1={px(2026) + 6} x2={px(2026) + 20} y1={py(38)} y2={py(38) - 14} stroke="var(--ink)" strokeWidth="1" />
          <text x={px(2026) + 24} y={py(38) - 18} fontFamily="var(--mono)" fontSize="11" fill="var(--ink)" fontWeight="500">38%</text>
          <text x={px(2026) + 24} y={py(38) - 5} fontFamily="var(--mono)" fontSize="9.5" letterSpacing="0.08em" fill="var(--muted)">Q1 · 2026</text>
        </g>
      </svg>
    </div>
  );
}

// Answer-engine mock — the signature motion lives here.
function AnswerEngineMock({ engine = 'PERPLEXITY', revealed = false, compact = false }) {
  return (
    <div className={'ae-mock' + (revealed ? ' revealed' : '')} style={{ padding: compact ? '20px 22px' : '28px 32px' }}>
      <div className="ae-head">
        <span className="engine">{engine}</span>
        <div className="dots"><span /><span /><span /></div>
      </div>
      <div className="ae-query">How does Acme handle enterprise SSO?</div>
      <div className="ae-answer">
        Acme provides enterprise SSO through SAML 2.0 and OIDC, with SCIM provisioning for user lifecycle management<span className="cite">¹</span>. The implementation supports Okta, Azure AD, and Google Workspace, and can be enabled per-workspace by an administrator<span className="cite hl">²<span className="tmark" /></span>. Session policies and just-in-time user creation are configurable<span className="cite">³</span>.
      </div>
      <div className="ae-sources">
        Sources
        <div className="ae-source-row"><span className="idx">01</span><span>support.northwind.com</span></div>
        <div className="ae-source-row hl"><span className="idx">02</span><span>acme.com / security</span><span className="mark" /></div>
        <div className="ae-source-row"><span className="idx">03</span><span>community.saas-auth.dev</span></div>
      </div>
    </div>
  );
}

// The Shift section
function SectionShift({ copy, direction, mobile, revealed }) {
  return (
    <section style={{ padding: mobile ? '64px 24px' : '120px 72px', borderTop: '1px solid var(--hairline)' }}>
      <Eyebrow num={copy.shift.num} label={copy.shift.label} />
      <h2 style={{
        fontSize: mobile ? 32 : 56,
        fontWeight: 500,
        letterSpacing: '-0.032em',
        lineHeight: 1.05,
        marginTop: 32,
        marginBottom: 24,
        maxWidth: 780,
      }}>{copy.shift.h2}</h2>
      <p style={{ fontSize: mobile ? 15 : 18, color: 'var(--g600)', maxWidth: 620, lineHeight: 1.6, marginBottom: mobile ? 36 : 60 }}>
        {copy.shift.sub}
      </p>

      <div style={{ display: 'grid', gridTemplateColumns: mobile ? '1fr' : '1.1fr 1fr', gap: mobile ? 40 : 64, alignItems: 'flex-start' }}>
        <div>
          <div className="eyebrow" style={{ marginBottom: 16 }}>
            <span>Fig. 01</span>
            <span className="sep">·</span>
            <span>Share of queries → answer engines</span>
          </div>
          <ShiftChart />
          <div style={{ marginTop: 14, fontFamily: 'var(--mono)', fontSize: 10, color: 'var(--g400)', letterSpacing: '0.04em' }}>{copy.shift.note}</div>
        </div>
        <div>
          <div className="eyebrow" style={{ marginBottom: 16 }}>
            <span>Fig. 02</span>
            <span className="sep">·</span>
            <span>A cited answer</span>
          </div>
          <AnswerEngineMock revealed={revealed} compact={mobile} />
          <div style={{ marginTop: 14, fontFamily: 'var(--mono)', fontSize: 10, color: 'var(--g400)', letterSpacing: '0.04em' }}>
            Citation marked with <span style={{ display: 'inline-block', width: 6, height: 6, background: 'var(--accent)', transform: 'rotate(45deg)', verticalAlign: 'middle', margin: '0 4px' }} /> is Citely-assisted.
          </div>
        </div>
      </div>
    </section>
  );
}

// Pillars — 진단 · 전략 · 구현
function SectionPillars({ copy, mobile, direction }) {
  return (
    <section style={{ padding: mobile ? '64px 24px' : '120px 72px', borderTop: '1px solid var(--hairline)' }}>
      <Eyebrow num={copy.pillars.num} label={copy.pillars.label} />
      <h2 style={{
        fontSize: mobile ? 32 : 56,
        fontWeight: 500,
        letterSpacing: '-0.032em',
        lineHeight: 1.05,
        marginTop: 32,
        marginBottom: mobile ? 40 : 80,
        maxWidth: 820,
      }}>{copy.pillars.h2}</h2>
      <div style={{
        display: 'grid',
        gridTemplateColumns: mobile ? '1fr' : 'repeat(3, 1fr)',
        gap: 0,
        borderTop: '1px solid var(--ink)',
      }}>
        {copy.pillars.items.map((it, i) => (
          <div key={i} style={{
            padding: mobile ? '32px 0' : '40px 32px 48px 0',
            borderRight: !mobile && i < 2 ? '1px solid var(--hairline)' : 'none',
            borderBottom: mobile ? '1px solid var(--hairline)' : 'none',
          }}>
            <div className="pillar-num" style={{ marginBottom: 24 }}>— {it.num}</div>
            <div className="pillar-kr">{it.kr}</div>
            <div className="pillar-en">{it.en}</div>
            <p style={{ color: 'var(--g600)', fontSize: 15, lineHeight: 1.6, marginBottom: 28, maxWidth: 360 }}>{it.body}</p>
            <div style={{ fontFamily: 'var(--mono)', fontSize: 10.5, letterSpacing: '0.08em', color: 'var(--ink)', textTransform: 'uppercase', paddingTop: 16, borderTop: '1px solid var(--hairline)' }}>
              {it.out}
            </div>
          </div>
        ))}
      </div>
    </section>
  );
}

// Modes — two cards
function SectionModes({ copy, mobile }) {
  return (
    <section style={{ padding: mobile ? '64px 24px' : '120px 72px', borderTop: '1px solid var(--hairline)' }}>
      <Eyebrow num={copy.modes.num} label={copy.modes.label} />
      <h2 style={{ fontSize: mobile ? 32 : 56, fontWeight: 500, letterSpacing: '-0.032em', lineHeight: 1.05, marginTop: 32, marginBottom: mobile ? 40 : 60 }}>
        {copy.modes.h2}
      </h2>
      <div style={{ display: 'grid', gridTemplateColumns: mobile ? '1fr' : '1fr 1fr', gap: 24 }}>
        {copy.modes.items.map((m, i) => (
          <div key={i} className="mode-card">
            <div className="mode-kind">{m.kind} · Mode</div>
            <div>
              <div className="mode-title">{i === 1 ? <><Wordmark size={26} /> <span style={{ color: 'var(--muted)', fontWeight: 400, marginLeft: 4 }}>Kit</span></> : m.title}</div>
              <div className="mode-sub" style={{ marginTop: 8 }}>{m.sub}</div>
            </div>
            <p style={{ color: 'var(--g600)', fontSize: 15, lineHeight: 1.6, maxWidth: 420, margin: 0 }}>{m.body}</p>
            <div className="mode-meta">
              {m.meta.map((t, j) => <span key={j}>{t}</span>)}
            </div>
          </div>
        ))}
      </div>
    </section>
  );
}

// Method
function SectionMethod({ copy, mobile }) {
  return (
    <section style={{ padding: mobile ? '64px 24px' : '120px 72px', borderTop: '1px solid var(--hairline)' }}>
      <Eyebrow num={copy.method.num} label={copy.method.label} />
      <h2 style={{ fontSize: mobile ? 32 : 56, fontWeight: 500, letterSpacing: '-0.032em', lineHeight: 1.05, marginTop: 32, marginBottom: mobile ? 32 : 48 }}>
        {copy.method.h2}
      </h2>
      <div style={{ maxWidth: 960 }}>
        {copy.method.items.map((it, i) => (
          <div key={i} className="method-row" style={mobile ? { gridTemplateColumns: '40px 1fr', rowGap: 6 } : {}}>
            <div className="method-n">{it.n}</div>
            <div className="method-t">{it.t}</div>
            {!mobile && <div className="method-b">{it.b}</div>}
            {mobile && <div className="method-b" style={{ gridColumn: '2 / -1' }}>{it.b}</div>}
          </div>
        ))}
      </div>
    </section>
  );
}

// Cases
function SectionCases({ copy, mobile }) {
  return (
    <section style={{ padding: mobile ? '64px 24px' : '120px 72px', borderTop: '1px solid var(--hairline)' }}>
      <Eyebrow num={copy.cases.num} label={copy.cases.label} />
      <h2 style={{ fontSize: mobile ? 32 : 56, fontWeight: 500, letterSpacing: '-0.032em', lineHeight: 1.05, marginTop: 32, marginBottom: mobile ? 28 : 48 }}>
        {copy.cases.h2}
      </h2>
      <div>
        {copy.cases.items.map((c, i) => (
          <div key={i} className="case" style={{ gridTemplateColumns: mobile ? '1fr' : '1fr 1fr', gap: mobile ? 24 : 64, display: 'grid' }}>
            <div>
              <div className="case-metric" style={{ fontSize: mobile ? 64 : 112 }}>{c.metric}</div>
              <div className="case-unit">{c.unit}</div>
            </div>
            <div>
              <div className="case-who">{c.who}</div>
              <div className="case-body">{c.body}</div>
            </div>
          </div>
        ))}
      </div>
    </section>
  );
}

// FAQ
function SectionFAQ({ copy, mobile }) {
  const [open, setOpen] = React.useState(0);
  return (
    <section style={{ padding: mobile ? '64px 24px' : '120px 72px', borderTop: '1px solid var(--hairline)' }}>
      <Eyebrow num={copy.faq.num} label={copy.faq.label} />
      <h2 style={{ fontSize: mobile ? 32 : 56, fontWeight: 500, letterSpacing: '-0.032em', lineHeight: 1.05, marginTop: 32, marginBottom: mobile ? 28 : 48 }}>
        {copy.faq.h2}
      </h2>
      <div style={{ maxWidth: 840 }}>
        {copy.faq.items.slice(0, 5).map((f, i) => {
          const isOpen = open === i;
          return (
            <div key={i} className={'faq-item' + (isOpen ? ' open' : '')}>
              <button className="faq-q" onClick={() => setOpen(isOpen ? -1 : i)}>
                <span>{f.q}</span>
                <span className="plus" />
              </button>
              <div className="faq-a" style={{ maxHeight: isOpen ? 400 : 0 }}>
                <div className="faq-a-inner">{f.a}</div>
              </div>
            </div>
          );
        })}
      </div>
    </section>
  );
}

// CTA + Footer
function SectionCTAFooter({ copy, mobile }) {
  return (
    <>
      <section style={{ padding: mobile ? '80px 24px' : '160px 72px', borderTop: '1px solid var(--hairline)' }}>
        <h2 style={{
          fontSize: mobile ? 36 : 76,
          fontWeight: 500,
          letterSpacing: '-0.035em',
          lineHeight: mobile ? 1.1 : 1,
          marginBottom: mobile ? 32 : 48,
          maxWidth: 900,
        }}>{copy.ctaFooter.h2}</h2>
        <div style={{ display: 'flex', gap: 32, alignItems: 'center', flexWrap: 'wrap' }}>
          <CTA sub={copy.ctaFooter.sub}>{copy.ctaFooter.cta}</CTA>
          <span style={{ color: 'var(--muted)' }}>·</span>
          <a href="#" style={{ color: 'var(--ink)', textDecoration: 'none', borderBottom: '1px solid var(--ink)', paddingBottom: 2, fontSize: 14 }}>{copy.ctaFooter.contact}</a>
        </div>
      </section>
      <footer style={{ padding: mobile ? '40px 24px 32px' : '48px 72px 40px', borderTop: '1px solid var(--hairline)' }}>
        <div style={{ display: 'grid', gridTemplateColumns: mobile ? '1fr' : '1fr auto auto', gap: mobile ? 24 : 40, alignItems: 'start' }}>
          <div>
            <Wordmark size={18} />
            <div style={{ marginTop: 14, fontFamily: 'var(--mono)', fontSize: 11, color: 'var(--muted)', letterSpacing: '0.02em', lineHeight: 1.65 }}>
              {copy.footer.col1}<br/>
              {copy.footer.op}<br/>
              {copy.footer.biz}<br/>
              {copy.footer.domain}
            </div>
          </div>
          {!mobile && <div />}
          <div style={{ fontFamily: 'var(--mono)', fontSize: 10.5, letterSpacing: '0.12em', textTransform: 'uppercase', color: 'var(--muted)', textAlign: mobile ? 'left' : 'right', lineHeight: 1.6 }}>
            {copy.footer.powered}<br/>
            <span style={{ color: 'var(--ink)', fontWeight: 500, letterSpacing: '0.12em' }}>{copy.footer.poweredBrand}</span>
          </div>
        </div>
      </footer>
    </>
  );
}

Object.assign(window, {
  Wordmark, Eyebrow, CTA, Nav, Hero, ShiftChart, AnswerEngineMock,
  SectionShift, SectionPillars, SectionModes, SectionMethod,
  SectionCases, SectionFAQ, SectionCTAFooter
});
