// Chapter II — Product placement.
// A flat product mock placed into a real photograph.
// Layout: ingredients (scene + product) → big hero output with model thumbnails → variation grid.

function ExProductPlacement() {
  const [activeModel, setActiveModel] = React.useState(0);
  const models = Array.from({ length: 9 }, (_, i) => `assets/marlboro-model-${String(i + 1).padStart(2, '0')}.jpg`);

  // Preload all model images so switching is instant
  React.useEffect(() => {
    models.forEach(src => { const img = new Image(); img.src = src; });
  }, []);

  return (
    <ExSection
      id="product-placement"
      chapter="Chapter I · Product placement"
      title={<>Place a product. Into a real scene.</>}
      kicker={<>The original photograph is authentic from the Vault Stock Library.<br />The product is a flat e-commerce image. Product Studio puts one inside the other and matches the light, the angle, the grain.</>}
      tone="deep">

      {/* Hero + side rail: large output on the left, ingredients stacked
          above the 3×3 model picker on the right. */}
      <div className="ex-pp-band" style={{
        display: 'grid',
        gridTemplateColumns: 'minmax(0, 1.55fr) minmax(280px, 0.92fr)',
        gap: 40,
        alignItems: 'start',
      }}>
        {/* LEFT: Hero output */}
        <div className="ex-pp-hero">
          <div style={{
            fontFamily: '"La Villa", sans-serif',
            fontSize: 10.5, letterSpacing: '0.28em', textTransform: 'uppercase',
            color: 'var(--cocoa)', fontWeight: 600,
            paddingBottom: 14, borderBottom: '1px solid var(--cocoa)',
            marginBottom: 18,
          }}>
            Output
          </div>
          <ExFrame
            src={models[activeModel]}
            alt="Product placed into scene with selected model"
            ratio="3/4"
            shadow="lg" />
        </div>

        {/* RIGHT: Ingredients stacked above Models */}
        <div className="ex-pp-rail" style={{
          display: 'flex', flexDirection: 'column', gap: 32,
        }}>
          {/* Ingredients */}
          <div>
            <div style={{
              fontFamily: '"La Villa", sans-serif',
              fontSize: 10.5, letterSpacing: '0.28em', textTransform: 'uppercase',
              color: 'var(--fg-2)', fontWeight: 500,
              paddingBottom: 12, borderBottom: '1px solid var(--border-1)',
              marginBottom: 16,
            }}>
              Ingredients
            </div>
            <div className="ex-pp-ing-grid" style={{
              display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12,
            }}>
              {[
                { src: 'assets/marlboro-original.jpg', label: 'Scene', tone: 'photo' },
                { src: 'assets/marlboro-product.jpg', label: 'Product', tone: 'neutral' },
              ].map((it, i) => (
                <div key={i}>
                  <div style={{
                    width: '100%', aspectRatio: '3/4',
                    background: it.tone === 'neutral' ? '#FFFFFF' : '#E8DFD2',
                    overflow: 'hidden',
                    display: 'flex', alignItems: 'center', justifyContent: 'center',
                  }}>
                    <img src={it.src} alt={it.label} loading="lazy" style={{
                      width: '100%', height: '100%',
                      objectFit: it.tone === 'neutral' ? 'contain' : 'cover',
                      display: 'block',
                    }} />
                  </div>
                  <div style={{
                    marginTop: 6,
                    fontFamily: '"La Villa", sans-serif',
                    fontSize: 10, letterSpacing: '0.22em', textTransform: 'uppercase',
                    color: 'var(--cocoa)', fontWeight: 500,
                  }}>
                    <span style={{ color: 'var(--fg-2)', marginRight: 6 }}>{String(i + 1).padStart(2, '0')}</span>
                    {it.label}
                  </div>
                </div>
              ))}
            </div>
          </div>

          {/* Models picker — 3×3 grid */}
          <div>
            <div style={{
              fontFamily: '"La Villa", sans-serif',
              fontSize: 10.5, letterSpacing: '0.28em', textTransform: 'uppercase',
              color: 'var(--fg-2)', fontWeight: 500,
              paddingBottom: 12, borderBottom: '1px solid var(--border-1)',
              marginBottom: 16,
            }}>
              Models · 9 picks
            </div>
            <div className="ex-pp-models" style={{
              display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 8,
            }}>
              {models.map((src, i) => (
                <button
                  key={i}
                  onClick={() => setActiveModel(i)}
                  style={{
                    width: '100%', aspectRatio: '3/4',
                    borderRadius: 0, overflow: 'hidden',
                    border: i === activeModel ? '2px solid var(--cocoa)' : '1px solid rgba(43,31,25,0.18)',
                    padding: 0, background: 'transparent',
                    cursor: 'pointer',
                    opacity: i === activeModel ? 1 : 0.72,
                    transition: 'opacity 200ms, border-color 200ms',
                  }}>
                  <img src={src} alt={`Model ${i + 1}`}
                    style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'center top', display: 'block' }} />
                </button>
              ))}
            </div>
            <div style={{
              marginTop: 10,
              fontFamily: '"La Villa", sans-serif',
              fontSize: 10, letterSpacing: '0.22em', textTransform: 'uppercase',
              color: 'var(--fg-2)',
            }}>Tap a face to swap</div>
          </div>
        </div>
      </div>

      {/* Variation grid */}
      <div style={{ marginTop: 96 }}>
        <ExSpreadHead
          eyebrow="Eight variations · same feel."
          title="Then turn it into whole campaign in a couple of clicks with Product Studio image presets."
          tone="deep" />

        <div className="ex-mb-grid" style={{
          display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 12
        }}>
          {[1, 2, 3, 4, 5, 6, 7, 8].map((n) =>
          <ExFrame
            key={n}
            src={`assets/marlboro-var-${n}.jpg`}
            alt={`Variation ${n}`}
            ratio="3/4"
            shadow="md" />

          )}
        </div>
        <div style={{
          marginTop: 16,
          fontFamily: '"La Villa", sans-serif',
          fontSize: 11, letterSpacing: '0.28em', textTransform: 'uppercase',
          color: 'var(--fg-2)', fontWeight: 500
        }}>OUTPUT

        </div>
      </div>

      <style>{`
        @media (max-width: 1000px) {
          .ex-mb-grid { grid-template-columns: repeat(3, 1fr) !important; }
          /* Collapse the hero + rail to vertical: ingredients on top, hero,
             then the model picker. */
          .ex-pp-band {
            grid-template-columns: 1fr !important;
            gap: 28px !important;
          }
          .ex-pp-band .ex-pp-rail { order: 0 !important; }
          .ex-pp-band .ex-pp-hero { order: 1 !important; }
          .ex-pp-rail { flex-direction: row !important; gap: 28px !important; align-items: flex-start !important; }
          .ex-pp-rail > div { flex: 1 1 0 !important; min-width: 0 !important; }
        }
        @media (max-width: 700px) {
          /* ─── Section chrome ─── */
          #product-placement { padding-top: 44px !important; padding-bottom: 56px !important; }
          #product-placement .ex-section-head { margin-bottom: 28px !important; gap: 14px !important; }
          #product-placement h2.celestial { font-size: 34px !important; line-height: 1.02 !important; }
          #product-placement .ex-section-head p { font-size: 13.5px !important; line-height: 1.55 !important; }
          #product-placement .ex-section-head > div > div:first-child { font-size: 10px !important; margin-bottom: 12px !important; }

          /* Rail back to a single column on phones, Models below Ingredients */
          .ex-pp-rail { flex-direction: column !important; gap: 24px !important; }
          .ex-pp-band { gap: 22px !important; }

          /* Tighter model picker thumbs on phone */
          .ex-pp-models { gap: 6px !important; }

          /* ─── Variation grid ─── */
          .ex-mb-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 6px !important; }
          /* Pull the variation block closer */
          #product-placement > div > div:last-child { margin-top: 48px !important; }
          /* Spread head before the variation grid */
          #product-placement h3 { font-size: 20px !important; line-height: 1.25 !important; }
          #product-placement > div > div:last-child > div:first-child { margin-bottom: 18px !important; }
        }
      `}</style>
    </ExSection>);

}

Object.assign(window, { ExProductPlacement });
