/* Size picker — pick the LE200 size. Three layout variants exposed via tweaks. */

function SizePicker({ config, setConfig, onNext, openRecommender, layout = 'ladder', personaStyle = 'script' }) {
  const models = window.LE200.MODELS;
  const [compareIds, setCompareIds] = useState([]);

  const toggleCompare = (id) => {
    setCompareIds((prev) => {
      if (prev.includes(id)) return prev.filter((x) => x !== id);
      if (prev.length >= 2) return [prev[1], id];
      return [...prev, id];
    });
  };

  const PersonaQuote = ({ m, big = false }) => {
    if (personaStyle === 'bubble') {
      return (
        <div style={{ position: 'relative', display: 'inline-block', padding: '12px 16px', background: 'color-mix(in oklab, var(--accent), transparent 88%)', border: '1px solid color-mix(in oklab, var(--accent-deep), transparent 60%)', maxWidth: 220, fontFamily: 'var(--font-body)', fontSize: 13, fontStyle: 'italic', color: 'var(--ink)' }}>
          "{m.persona}"
          <span style={{ position: 'absolute', left: 14, bottom: -8, width: 0, height: 0, borderLeft: '8px solid transparent', borderRight: '8px solid transparent', borderTop: '8px solid color-mix(in oklab, var(--accent), transparent 88%)' }} />
        </div>);

    }
    if (personaStyle === 'clean') {
      return (
        <div style={{ borderLeft: '2px solid var(--accent)', paddingLeft: 12, fontSize: 14, fontStyle: 'italic', color: 'var(--ink-2)', maxWidth: 260, lineHeight: 1.4 }}>
          "{m.persona}"
        </div>);

    }
    return (
      <div className="script" style={{ fontSize: big ? 32 : 22, color: 'var(--accent-deep)', lineHeight: 1.05, maxWidth: 260 }}>
        "{m.persona}"
      </div>);

  };

  return (
    <div className="page">
      <main>
        <section className="container" style={{ paddingTop: 40, paddingBottom: 24 }}>
          <SectionHeader
            tag="[01] · Step 1 of 4 · Select size"
            title="Which LE200 fits the way you fire?"
            sub="Capacity, footprint, and BTU all scale together. The persona quote next to each is real positioning — from potters who own that size."
            action={
            <div style={{ display: 'flex', gap: 12 }}>
                <button className="btn btn-ghost" onClick={openRecommender}>Help me pick →</button>
                {compareIds.length === 2 &&
              <button className="btn" onClick={() => document.getElementById('compare-row')?.scrollIntoView({ behavior: 'smooth', block: 'start' })}>
                    Compare {compareIds.join(' ↔ ')}
                  </button>
              }
              </div>
            } />
          
        </section>

        {/* ───── LAYOUT VARIANT: LADDER (default) ───── */}
        {layout === 'ladder' &&
        <section className="container" style={{ paddingBottom: 40 }}>
            <div style={{
            border: '1px solid var(--rule)',
            background: 'var(--field-2)',
            padding: '40px 24px 0',
            display: 'grid', gridTemplateColumns: 'repeat(5, 1fr)', gap: 0,
            position: 'relative'
          }}>
              {/* Scale label */}
              <div style={{
              position: 'absolute', top: 12, left: 16,
              fontFamily: 'var(--font-mono)', fontSize: 10, color: 'var(--ink-4)', letterSpacing: '0.12em', textTransform: 'uppercase'
            }}>
                Fig. 02 · Lineup elevation · drawn to relative scale
              </div>

              {models.map((m, i) => {
              const heightPct = 30 + m.capacity / 40 * 70;
              const selected = config.size === m.id;
              return (
                <div key={m.id} style={{
                  position: 'relative',
                  borderRight: i < 4 ? '1px dashed color-mix(in oklab, var(--ink), transparent 70%)' : 'none'
                }}>
                    <button
                    onClick={() => setConfig({ size: m.id })}
                    style={{
                      background: selected ? 'color-mix(in oklab, var(--accent), transparent 90%)' : 'transparent',
                      border: 'none', cursor: 'pointer', width: '100%',
                      display: 'flex', flexDirection: 'column', alignItems: 'center',
                      padding: '20px 8px', color: 'inherit', fontFamily: 'inherit',
                      position: 'relative'
                    }}>
                    
                      {selected && <span style={{ position: 'absolute', left: 0, top: 0, bottom: 0, width: 3, background: 'var(--accent)' }} />}
                      <div className="mono upper" style={{ fontSize: 10, color: 'var(--ink-3)', letterSpacing: '0.12em' }}>{m.sku}</div>
                      <div className="display" style={{ fontSize: 32, margin: '10px 0 2px' }}>
                        {m.capacity}<span style={{ fontSize: 14, color: 'var(--ink-3)', marginLeft: 4 }}>cu ft</span>
                      </div>
                      <div style={{ height: 300, display: 'flex', alignItems: 'flex-end', marginTop: 10 }}>
                        <img src={m.front} alt={m.sku} style={{
                        height: heightPct + '%', width: 'auto', maxHeight: 300,
                        filter: `drop-shadow(0 14px 18px rgba(14, 53, 61, ${selected ? 0.28 : 0.16}))`,
                        transition: 'filter 0.25s, transform 0.25s',
                        transform: selected ? 'translateY(-4px)' : 'none'
                      }} />
                      </div>
                      <div style={{ marginTop: 16, minHeight: 80, display: 'flex', alignItems: 'center' }}>
                        <PersonaQuote m={m} />
                      </div>
                      <div style={{ fontFamily: 'var(--font-mono)', fontSize: 10, color: 'var(--ink-3)', textTransform: 'uppercase', letterSpacing: '0.08em', marginTop: 8, textAlign: 'center', padding: '0 8px' }}>
                        {m.buyer}
                      </div>
                    </button>
                    {/* Compare checkbox */}
                    <label style={{
                    position: 'absolute', top: 8, right: 8,
                    display: 'inline-flex', alignItems: 'center', gap: 6,
                    fontFamily: 'var(--font-mono)', fontSize: 10, color: 'var(--ink-3)',
                    cursor: 'pointer', textTransform: 'uppercase', letterSpacing: '0.08em',
                    userSelect: 'none'
                  }}>
                      <input
                      type="checkbox"
                      checked={compareIds.includes(m.id)}
                      onChange={() => toggleCompare(m.id)}
                      style={{ accentColor: 'var(--accent-deep)' }} />
                    
                      Compare
                    </label>
                    <div style={{
                    borderTop: '1px solid var(--rule)',
                    padding: '12px 12px 16px',
                    display: 'flex', justifyContent: 'space-between',
                    fontFamily: 'var(--font-mono)', fontSize: 11
                  }}>
                      <span style={{ color: 'var(--ink-3)' }}>{m.btu.toLocaleString()} BTU</span>
                      <span style={{ color: 'var(--ink)' }}>{fmtUSD(m.price)}</span>
                    </div>
                  </div>);

            })}
            </div>
          </section>
        }

        {/* ───── LAYOUT VARIANT: CARDS ───── */}
        {layout === 'cards' &&
        <section className="container" style={{ paddingBottom: 40 }}>
            <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(320px, 1fr))', gap: 20 }}>
              {models.map((m) => {
              const selected = config.size === m.id;
              return (
                <div key={m.id} style={{
                  border: selected ? '1px solid var(--ink)' : '1px solid color-mix(in oklab, var(--ink), transparent 80%)',
                  background: 'var(--field)',
                  padding: 24,
                  position: 'relative',
                  cursor: 'pointer'
                }} onClick={() => setConfig({ size: m.id })}>
                    {selected && <span style={{ position: 'absolute', left: 0, top: 0, bottom: 0, width: 3, background: 'var(--accent)' }} />}
                    <Crosshairs />
                    <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline' }}>
                      <div>
                        <div className="mono upper" style={{ fontSize: 10, color: 'var(--ink-3)', letterSpacing: '0.12em' }}>{m.sku}</div>
                        <div className="display" style={{ fontSize: 56, lineHeight: 0.9, marginTop: 6 }}>
                          {m.capacity}<span style={{ fontSize: 16, color: 'var(--ink-3)' }}> cu ft</span>
                        </div>
                      </div>
                      <div className="mono" style={{ fontSize: 14, fontVariantNumeric: 'tabular-nums' }}>{fmtUSD(m.price)}</div>
                    </div>
                    <div style={{ display: 'flex', justifyContent: 'center', margin: '20px 0' }}>
                      <img src={m.front} alt={m.sku} style={{ maxWidth: 200, maxHeight: 240, filter: 'drop-shadow(0 12px 16px rgba(14, 53, 61, 0.18))' }} />
                    </div>
                    <PersonaQuote m={m} />
                    <div style={{ marginTop: 16, fontFamily: 'var(--font-mono)', fontSize: 10, color: 'var(--ink-3)', textTransform: 'uppercase', letterSpacing: '0.08em' }}>
                      {m.buyer}
                    </div>
                    <div style={{
                    marginTop: 16, paddingTop: 16,
                    borderTop: '1px dashed color-mix(in oklab, var(--ink), transparent 70%)',
                    display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 8,
                    fontFamily: 'var(--font-mono)', fontSize: 11
                  }}>
                      <div><div className="muted" style={{ fontSize: 9, letterSpacing: '0.1em' }}>BTU</div>{(m.btu / 1000).toFixed(0)}k</div>
                      <div><div className="muted" style={{ fontSize: 9, letterSpacing: '0.1em' }}>BURNERS</div>{m.burners}</div>
                      <div><div className="muted" style={{ fontSize: 9, letterSpacing: '0.1em' }}>STACK</div>{m.stacking.join('×')}"</div>
                    </div>
                  </div>);

            })}
            </div>
          </section>
        }

        {/* ───── LAYOUT VARIANT: COMPARISON ROW ───── */}
        {layout === 'compare' &&
        <section className="container" style={{ paddingBottom: 40 }}>
            <div style={{ border: '1px solid var(--rule)', overflow: 'auto' }}>
              <table style={{ width: '100%', borderCollapse: 'collapse', fontFamily: 'var(--font-mono)', fontSize: 13, fontVariantNumeric: 'tabular-nums' }}>
                <thead>
                  <tr style={{ borderBottom: '1px solid var(--rule)' }}>
                    <th style={{ textAlign: 'left', padding: 16, color: 'var(--ink-3)', textTransform: 'uppercase', fontSize: 10, letterSpacing: '0.12em', fontWeight: 600 }}>Spec</th>
                    {models.map((m) =>
                  <th key={m.id} style={{ padding: 16, borderLeft: '1px solid var(--rule)', textAlign: 'center' }}>
                        <button onClick={() => setConfig({ size: m.id })} style={{
                      background: config.size === m.id ? 'color-mix(in oklab, var(--accent), transparent 88%)' : 'transparent',
                      border: 'none', padding: 8, cursor: 'pointer', width: '100%',
                      color: 'inherit', fontFamily: 'inherit'
                    }}>
                          <div style={{ height: 120, display: 'flex', alignItems: 'flex-end', justifyContent: 'center' }}>
                            <img src={m.front} alt={m.sku} style={{ maxHeight: '100%', maxWidth: '100%' }} />
                          </div>
                          <div className="display" style={{ fontSize: 22, marginTop: 8 }}>{m.sku}</div>
                          <div style={{ fontSize: 11, color: 'var(--ink-3)', textTransform: 'uppercase', marginTop: 4 }}>{m.buyer}</div>
                        </button>
                      </th>
                  )}
                  </tr>
                </thead>
                <tbody>
                  {[
                ['Capacity', (m) => m.capacity + ' cu ft'],
                ['Setting area', (m) => m.settingArea + ' cu ft'],
                ['Stacking (in)', (m) => m.stacking.join(' × ')],
                ['Footprint (in)', (m) => m.footprint.join(' × ')],
                ['Max BTU/hr', (m) => m.btu.toLocaleString()],
                ['Burners', (m) => m.burners],
                ['Ship weight', (m) => m.shipWeight.toLocaleString() + ' lb'],
                ['Price', (m) => fmtUSD(m.price)]].
                map(([k, fn], i) =>
                <tr key={i} style={{ borderBottom: '1px dashed color-mix(in oklab, var(--ink), transparent 80%)' }}>
                      <td style={{ padding: '12px 16px', color: 'var(--ink-3)', textTransform: 'uppercase', fontSize: 10, letterSpacing: '0.1em' }}>{k}</td>
                      {models.map((m) =>
                  <td key={m.id} style={{ padding: '12px 16px', borderLeft: '1px solid var(--rule)', textAlign: 'center', color: config.size === m.id ? 'var(--accent-deep)' : 'var(--ink)' }}>
                          {fn(m)}
                        </td>
                  )}
                    </tr>
                )}
                  <tr>
                    <td style={{ padding: '12px 16px', color: 'var(--ink-3)', textTransform: 'uppercase', fontSize: 10, letterSpacing: '0.1em' }}>Persona</td>
                    {models.map((m) =>
                  <td key={m.id} style={{ padding: '12px 16px', borderLeft: '1px solid var(--rule)', textAlign: 'center', fontFamily: 'var(--font-script)', color: 'var(--accent-deep)', fontSize: 18, lineHeight: 1.1 }}>
                        "{m.persona}"
                      </td>
                  )}
                  </tr>
                </tbody>
              </table>
            </div>
          </section>
        }

        {/* SIDE-BY-SIDE COMPARE (when 2 selected) ----------------- */}
        {compareIds.length === 2 &&
        <section id="compare-row" className="container" style={{ paddingTop: 60, paddingBottom: 40 }}>
            <SectionHeader tag="[01.b] · Side-by-side" title={`${compareIds[0]} ↔ ${compareIds[1]}`} sub="See them at the same scale, with the specs that actually differ between sizes." />
            <CompareRow ids={compareIds} onPick={(id) => {setConfig({ size: id });window.scrollTo({ top: 0, behavior: 'smooth' });}} />
          </section>
        }

        {/* NEXT bar ----------------------------------------------- */}
        <section className="container" style={{ paddingTop: 40, paddingBottom: 80 }}>
          <div className="rule" style={{ paddingTop: 24, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
            <div>
              <div className="mono upper" style={{ fontSize: 10, color: 'var(--ink-3)', letterSpacing: '0.12em' }}>
                Selected
              </div>
              <div className="display" style={{ fontSize: 32, marginTop: 6 }}>
                {config.size ? `LE200-${config.size} · ${window.LE200.MODELS.find((m) => m.id === config.size).capacity} cu ft` : 'Pick a size to continue'}
              </div>
            </div>
            <button
              className="btn btn-accent btn-arrow"
              disabled={!config.size}
              style={{ opacity: config.size ? 1 : 0.3, cursor: config.size ? 'pointer' : 'not-allowed' }}
              onClick={onNext}>
              
              Configure this kiln
            </button>
          </div>
        </section>
      </main>
      <Footer />
    </div>);

}

function CompareRow({ ids, onPick }) {
  const [a, b] = ids.map((id) => window.LE200.MODELS.find((m) => m.id === id));
  if (!a || !b) return null;
  const maxCap = Math.max(a.capacity, b.capacity);
  return (
    <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 24 }}>
      {[a, b].map((m) =>
      <div key={m.id} style={{ border: '1px solid var(--rule)', padding: 24, background: 'var(--field)', position: 'relative' }}>
          <Crosshairs />
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline' }}>
            <div>
              <div className="mono upper" style={{ fontSize: 10, color: 'var(--ink-3)', letterSpacing: '0.12em' }}>{m.sku}</div>
              <div className="display" style={{ fontSize: 80, lineHeight: 0.9, marginTop: 6 }}>
                {m.capacity}<span style={{ fontSize: 18, color: 'var(--ink-3)' }}> cu ft</span>
              </div>
            </div>
            <button className="btn btn-ghost" onClick={() => onPick(m.id)}>Pick this</button>
          </div>
          <div style={{ height: 320, display: 'flex', alignItems: 'flex-end', justifyContent: 'center', marginTop: 20 }}>
            <img src={m.front} alt={m.sku} style={{
            maxHeight: 40 + m.capacity / maxCap * 280 + 'px',
            filter: 'drop-shadow(0 14px 18px rgba(14, 53, 61, 0.18))'
          }} />
          </div>
          <table className="spec-table" style={{ marginTop: 16 }}>
            <tbody>
              <tr><td className="k">Footprint W×D×H</td><td className="v">{m.footprint.join(' × ')}"</td></tr>
              <tr><td className="k">Stacking interior</td><td className="v">{m.stacking.join(' × ')}"</td></tr>
              <tr><td className="k">Burners · BTU/hr</td><td className="v">{m.burners} · {m.btu.toLocaleString()}</td></tr>
              <tr><td className="k">Ship weight</td><td className="v">{m.shipWeight.toLocaleString()} lb</td></tr>
              <tr><td className="k">Base price</td><td className="v">{fmtUSD(m.price)}</td></tr>
            </tbody>
          </table>
        </div>
      )}
    </div>);

}

window.SizePicker = SizePicker;