// Act XI — THE MARKET (sizing)
// God-mode redesign 2026-04-28: 5-panel beat-swap matching Ask cadence.
// Old version dense — TAM/SAM/SOM bars + methodology + 5yr trajectory all
// stacked on one viewport. New shape phases each level in dramatically.
//
//   b0  Overview — concentric TAM > SAM > SOM circles + headline
//   b1  TAM punctum — $51.7M + bottoms-up breakdown
//   b2  SAM punctum — $25.3M + bottoms-up breakdown
//   b3  SOM punctum — $3.93M Y3 target + relative position
//   b4  5-year ARR trajectory + methodology + bridge

const MKT_LEVELS = [
  {
    tag: 'TAM',
    value: '$51.7M',
    sub: 'Total addressable',
    breakdown: [
      ['347,500', 'reachable users × $144 / yr'],
      ['+ 21',    'archive deals × $74.7K avg'],
      ['10',      'countries · 650K+ objects'],
      ['80 / 20', 'museum revenue share'],
    ],
    note: 'The full universe — every accredited fashion-history program plus every museum partner who could license content.',
    color: 'var(--gold)',
    rgb: '201, 168, 76',
    sizePct: 1.0,
  },
  {
    tag: 'SAM',
    value: '$25.3M',
    sub: 'Serviceable',
    breakdown: [
      ['168,500', 'reachable users in our motion'],
      ['60',      'English-language accredited programs'],
      ['Y1–Y3',   'direct institutional sales'],
      ['~49%',    'of TAM penetration ceiling'],
    ],
    note: 'Where we can ship in the first three years, given a direct institutional sales motion and English-language program coverage.',
    color: 'var(--sage)',
    rgb: '143, 166, 143',
    sizePct: 0.49,
  },
  {
    tag: 'SOM',
    value: '$3.93M',
    sub: 'Year-3 ARR target',
    breakdown: [
      ['23,600',  'activated paid users'],
      ['5',       'signed archive partnerships'],
      ['FIT',     'pilot anchor + 4 closed schools'],
      ['~16%',    'SAM penetration by end of Y3'],
    ],
    note: 'The beachhead. FIT pilot anchors the cohort; four additional institutions close by end of Year 3.',
    color: 'var(--rust)',
    rgb: '166, 82, 59',
    sizePct: 0.076,
  },
];

const MKT_TRAJECTORY = [
  { y: 'Yr 1', arr: '$0.42M', note: 'FIT pilot · 2 additional schools' },
  { y: 'Yr 2', arr: '$1.78M', note: '5 institutions · student tier live' },
  { y: 'Yr 3', arr: '$3.93M', note: 'SOM target · 5 archive deals', highlight: true },
  { y: 'Yr 4', arr: '$7.60M', note: 'EU expansion begins' },
  { y: 'Yr 5', arr: '$13.2M', note: 'Series B trajectory' },
];

// ─── Concentric circles geometry ─────────────────────────────────────────
// Three nested circles sized by sizePct relative to TAM. Center-aligned.
// The "active" circle (when zoomed into a level) gets a glow + thicker stroke.
function MarketConcentric({ activeLevel = -1, scale = 1, fadeNonActive = false }) {
  const TAM_PX = 460;
  return (
    <div style={{
      position: 'relative',
      width: TAM_PX, height: TAM_PX,
      transform: `scale(${scale})`,
      transformOrigin: 'center',
      transition: 'transform 0.8s cubic-bezier(0.22, 1, 0.36, 1)',
    }}>
      {MKT_LEVELS.map((lvl, i) => {
        const size = TAM_PX * lvl.sizePct;
        const isActive = activeLevel === i;
        const fade = fadeNonActive && activeLevel >= 0 && !isActive;
        return (
          <div key={lvl.tag} style={{
            position: 'absolute',
            left: '50%', top: '50%',
            transform: 'translate(-50%, -50%)',
            width: size, height: size,
            borderRadius: '50%',
            border: `${isActive ? 2 : 1}px solid ${lvl.color}`,
            background: i === 2
              ? `radial-gradient(circle, rgba(${lvl.rgb}, 0.18), rgba(${lvl.rgb}, 0.06))`
              : `rgba(${lvl.rgb}, ${isActive ? 0.10 : 0.04})`,
            boxShadow: isActive ? `0 0 32px rgba(${lvl.rgb}, 0.45)` : 'none',
            opacity: fade ? 0.32 : 1,
            transition: 'all 0.7s cubic-bezier(0.22, 1, 0.36, 1)',
          }} />
        );
      })}
      {/* Inline labels — each label sits in the annulus just inside the top
          arc of its ring so they never overlap each other. TAM label inside
          the TAM ring's top edge; SAM in the TAM-minus-SAM annulus at top;
          SOM in the SAM-minus-SOM annulus at top. SOM ring itself is tiny so
          the label is the protagonist there. */}
      {activeLevel < 0 && (
        <>
          {/* TAM — top of outer ring */}
          <div style={{
            position: 'absolute', top: 18, left: '50%',
            transform: 'translateX(-50%)',
          }}>
            <span className="label-text" style={{
              color: 'var(--gold)', letterSpacing: '0.36em',
            }}>TAM · $51.7M</span>
          </div>
          {/* SAM — at top arc of SAM ring (~25% down from container top) */}
          <div style={{
            position: 'absolute', top: '21%', left: '50%',
            transform: 'translateX(-50%)',
          }}>
            <span className="label-text" style={{
              color: 'var(--sage)', letterSpacing: '0.36em',
            }}>SAM · $25.3M</span>
          </div>
          {/* SOM — at top arc of SOM ring (~46% down) */}
          <div style={{
            position: 'absolute', top: '42%', left: '50%',
            transform: 'translateX(-50%)',
            textAlign: 'center',
          }}>
            <div className="label-text" style={{
              color: 'var(--rust)', letterSpacing: '0.32em', fontSize: 10,
            }}>SOM</div>
            <div className="font-display" style={{
              fontSize: 14, color: 'var(--parchment)', marginTop: 2,
              fontStyle: 'italic',
            }}>$3.93M</div>
          </div>
        </>
      )}
    </div>
  );
}

// ─── Panel 0 · Overview ──────────────────────────────────────────────────
function MktOverviewPanel({ vis }) {
  const eased = 1 - Math.pow(1 - vis, 3);
  return (
    <div style={{
      position: 'absolute', inset: 0,
      opacity: eased, transform: `translateY(${(1 - eased) * 24}px)`,
      transition: 'opacity 0.7s ease, transform 0.7s ease',
      pointerEvents: vis > 0.05 ? 'auto' : 'none',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      padding: '0 80px',
    }}>
      <div style={{
        display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 80,
        alignItems: 'center', maxWidth: 1480, width: '100%',
      }}>
        {/* Left — typography */}
        <div>
          <span className="label-text" style={{
            color: 'var(--sage)', letterSpacing: '0.36em',
          }}>The Market · bottoms-up</span>
          <h2 className="font-display" style={{
            fontSize: 'clamp(56px, 6.4vw, 100px)',
            lineHeight: 0.94, letterSpacing: '-0.02em',
            color: 'var(--parchment)',
            margin: '20px 0 0', textTransform: 'uppercase',
          }}>
            A market<br />
            <span style={{ color: 'var(--gold)', fontStyle: 'italic' }}>we can count.</span>
          </h2>
          <div style={{
            margin: '32px 0 0', height: 1, width: 240,
            background: 'linear-gradient(to right, var(--gold), transparent)',
          }} />
          <p className="font-editorial" style={{
            margin: '28px 0 0', fontSize: 18, lineHeight: 1.55,
            color: 'var(--parchment-dim)', maxWidth: 540,
            fontStyle: 'italic', textWrap: 'pretty',
          }}>
            Built per-user and per-institution from three real inputs —
            the NASAD roster, the ARTstor benchmark, and our Pro-tier survey.
          </p>
          <p className="font-editorial" style={{
            margin: '18px 0 0', fontSize: 14, lineHeight: 1.5,
            color: 'var(--taupe)', maxWidth: 540,
            textWrap: 'pretty',
          }}>
            The old $4.2B / $12.4B top-down figures got retired the moment we
            realized we couldn&rsquo;t attribute a single dollar of them to a
            subscription that costs less than lunch.
          </p>
        </div>
        {/* Right — concentric circles */}
        <div style={{
          display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}>
          <MarketConcentric activeLevel={-1} />
        </div>
      </div>
    </div>
  );
}

// ─── Level zoom panel (used for TAM, SAM, SOM) ───────────────────────────
function MktLevelPanel({ vis, level, levelIdx }) {
  const eased = 1 - Math.pow(1 - vis, 3);
  return (
    <div style={{
      position: 'absolute', inset: 0,
      opacity: eased, transform: `translateY(${(1 - eased) * 24}px)`,
      transition: 'opacity 0.7s ease, transform 0.7s ease',
      pointerEvents: vis > 0.05 ? 'auto' : 'none',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      padding: '0 80px',
    }}>
      <div style={{
        display: 'grid', gridTemplateColumns: '1.4fr 1fr', gap: 80,
        alignItems: 'center', maxWidth: 1480, width: '100%',
      }}>
        {/* Left — punctum + breakdown */}
        <div>
          <div style={{ display: 'flex', alignItems: 'baseline', gap: 16, marginBottom: 14 }}>
            <span className="label-text" style={{
              color: level.color, letterSpacing: '0.36em',
            }}>{level.tag} · {level.sub}</span>
            <div style={{ flex: 1, height: 1, background: `rgba(${level.rgb}, 0.3)` }} />
          </div>
          <div className="font-display" style={{
            fontSize: 'clamp(96px, 11vw, 180px)',
            lineHeight: 0.92, letterSpacing: '-0.04em',
            color: level.color, fontStyle: 'italic',
            margin: '0 0 18px',
          }}>{level.value}</div>
          <p className="font-editorial" style={{
            margin: '0 0 32px', fontSize: 18, lineHeight: 1.55,
            color: 'var(--parchment-dim)', maxWidth: 540,
            fontStyle: 'italic', textWrap: 'pretty',
          }}>
            {level.note}
          </p>
          {/* Breakdown — 4 rows of figure + descriptor */}
          <div style={{
            display: 'grid', gridTemplateColumns: '1fr 1fr',
            gap: '18px 32px', maxWidth: 620,
          }}>
            {level.breakdown.map(([fig, desc], i) => (
              <div key={i}>
                <div className="font-display" style={{
                  fontSize: 28, color: 'var(--parchment)',
                  lineHeight: 1, letterSpacing: '-0.01em',
                  fontStyle: 'italic',
                }}>{fig}</div>
                <div className="font-editorial" style={{
                  marginTop: 6, fontSize: 13.5, lineHeight: 1.45,
                  color: 'var(--taupe)', textWrap: 'pretty',
                }}>{desc}</div>
              </div>
            ))}
          </div>
        </div>
        {/* Right — concentric circles, this level highlighted */}
        <div style={{
          display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}>
          <MarketConcentric activeLevel={levelIdx} fadeNonActive />
        </div>
      </div>
    </div>
  );
}

// ─── Panel 4 · Trajectory + bridge ───────────────────────────────────────
function MktTrajectoryPanel({ vis }) {
  const eased = 1 - Math.pow(1 - vis, 3);
  return (
    <div style={{
      position: 'absolute', inset: 0,
      opacity: eased, transform: `translateY(${(1 - eased) * 24}px)`,
      transition: 'opacity 0.7s ease, transform 0.7s ease',
      pointerEvents: vis > 0.05 ? 'auto' : 'none',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      padding: '0 80px',
    }}>
      <div style={{ width: '100%', maxWidth: 1480 }}>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 16, marginBottom: 18 }}>
          <span className="label-text" style={{ color: 'var(--sage)', letterSpacing: '0.32em' }}>Five-year ARR ramp</span>
          <div style={{ flex: 1, height: 1, background: 'rgba(143,166,143,0.22)' }} />
          <span className="label-text" style={{ color: 'var(--rust)' }}>
            ◆ SOM target · Y3
          </span>
        </div>
        <h3 className="font-display" style={{
          fontSize: 'clamp(40px, 4.4vw, 64px)',
          lineHeight: 0.98, letterSpacing: '-0.02em',
          color: 'var(--parchment)', margin: '0 0 40px', textTransform: 'uppercase',
        }}>
          From beachhead, <span style={{ color: 'var(--gold)', fontStyle: 'italic' }}>compounding.</span>
        </h3>
        <div style={{
          display: 'grid', gridTemplateColumns: 'repeat(5, 1fr)',
          alignItems: 'flex-start',
        }}>
          {MKT_TRAJECTORY.map((r, i) => (
            <div key={r.y} style={{
              padding: '0 22px',
              borderRight: i < 4 ? '1px solid rgba(143,166,143,0.18)' : 'none',
            }}>
              <div className="label-text" style={{ color: 'var(--taupe)' }}>{r.y} · ARR</div>
              <div className="font-display" style={{
                fontSize: 'clamp(36px, 3.6vw, 52px)',
                color: r.highlight ? 'var(--rust)' : 'var(--parchment)',
                lineHeight: 1.05, marginTop: 14, letterSpacing: '-0.02em',
                fontStyle: 'italic',
              }}>
                {r.arr}
              </div>
              <div className="font-editorial" style={{
                fontSize: 12.5, color: 'var(--parchment-dim)',
                marginTop: 12, lineHeight: 1.5, fontStyle: 'italic',
                textWrap: 'pretty',
              }}>
                {r.note}
              </div>
            </div>
          ))}
        </div>
        {/* Methodology footer + bridge */}
        <div style={{
          marginTop: 56, padding: '22px 28px',
          background: 'rgba(143,166,143,0.05)',
          borderLeft: '2px solid var(--sage)',
          display: 'flex', alignItems: 'center', justifyContent: 'space-between',
          gap: 32,
        }}>
          <div>
            <span className="label-text" style={{ color: 'var(--gold)' }}>Four inputs</span>
            <div className="font-editorial" style={{
              marginTop: 8, fontSize: 14, lineHeight: 1.6,
              color: 'var(--parchment-dim)', fontStyle: 'italic',
            }}>
              NASAD roster · ARTstor benchmark · Pro-tier WTP survey · 21-archive network
            </div>
          </div>
          <div style={{ textAlign: 'right' }}>
            <span className="label-text" style={{ color: 'var(--gold)' }}>Next · the tiers</span>
            <div className="font-display" style={{
              fontSize: 22, color: 'var(--parchment)',
              marginTop: 6, letterSpacing: '-0.01em', fontStyle: 'italic',
            }}>
              $9.99 · $14.99 · $24K →
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

function ActMarketSizingInner({ index, total, act }) {
  const p0 = useBeatProgress(0);
  const p1 = useBeatProgress(1);
  const p2 = useBeatProgress(2);
  const p3 = useBeatProgress(3);
  const p4 = useBeatProgress(4);

  const visOverview   = panelVis(p0, p1);
  const visTAM        = panelVis(p1, p2);
  const visSAM        = panelVis(p2, p3);
  const visSOM        = panelVis(p3, p4);
  const visTrajectory = p4;

  return (
    <ActFrame act={act} index={index} total={total} bg="var(--petrol)" bleed>
      <div style={{ position: 'absolute', inset: 0, zIndex: 4 }}>
        <MktOverviewPanel vis={visOverview} />
        <MktLevelPanel vis={visTAM} level={MKT_LEVELS[0]} levelIdx={0} />
        <MktLevelPanel vis={visSAM} level={MKT_LEVELS[1]} levelIdx={1} />
        <MktLevelPanel vis={visSOM} level={MKT_LEVELS[2]} levelIdx={2} />
        <MktTrajectoryPanel vis={visTrajectory} />
      </div>
    </ActFrame>
  );
}

function ActMarketSizing({ index, total, act }) {
  return (
    <ScrollBeats beats={5} bg="var(--petrol)">
      <ActMarketSizingInner index={index} total={total} act={act} />
    </ScrollBeats>
  );
}

window.ActMarketSizing = ActMarketSizing;
