// store-pdp.jsx — Product detail, Cart drawer, Mobile menu (i18n-aware)

const { useState: useS2, useEffect: useE2, useMemo: useM2 } = React;

// ───────────────────────── Product page ─────────────────────────
function ProductPage({ id, mobile }) {
  const p = window.ECOPROF_PRODUCTS.find(x => x.id === id);
  const cat = window.ECOPROF_CATEGORIES.find(c => c.id === p?.cat);
  const { addToCart, goto, settings, t, tr, fmt, lang, isWished, toggleWishlist, setCheckoutPrefill } = useStore();
  const [qty, setQty] = useS2(1);
  const [tab, setTab] = useS2('description');
  const [activeImg, setActiveImg] = useS2(0);
  const [bundle, setBundle] = useS2({});
  const [qbOpen, setQbOpen] = useS2(false);
  const [qbSent, setQbSent] = useS2(false);
  const [qbForm, setQbForm] = useS2({ name: '', phone: '', email: '', comment: '' });
  const [vidOpen, setVidOpen] = useS2(false);
  const [vidIdx, setVidIdx] = useS2(0);

  // Video model — products may have several clips (texture, about, how-to…).
  // Falls back to the legacy single `youtube` field. Labels resolve by kind.
  const VKIND = {
    texture: { uk:'Текстура',     en:'Texture' },
    about:   { uk:'Про продукт',  en:'About' },
    howto:   { uk:'Як наносити',  en:'How to use' },
    routine: { uk:'У ритуалі',    en:'In routine' },
    result:  { uk:'Результат',    en:'Result' },
  };
  const videos = (p?.videos && p.videos.length)
    ? p.videos
    : (p?.youtube ? [{ kind:'about', dur:'3:42' }] : []);
  const vLabel = (v) => (VKIND[v.kind] || VKIND.about)[lang];

  const fbtIds = (window.ECOPROF_BUNDLES[id] || [id, 'p11', 'p17', 'p25']).filter(x => x !== id);
  const fbtList = [p, ...fbtIds.map(fid => window.ECOPROF_PRODUCTS.find(x => x.id === fid)).filter(Boolean)];

  useE2(() => {
    const init = {};
    fbtList.forEach(x => { init[x.id] = true; });
    setBundle(init);
  }, [id]);

  if (!p) return null;

  const bundleSelected = fbtList.filter(x => bundle[x.id]);
  const bundleTotal = bundleSelected.reduce((s, x) => s + x.price, 0);
  // No combo discount — buying the full set unlocks a free gift instead.
  const bundleGiftQ = 3; // min items in set to earn the gift
  const bundleGiftEarned = bundleSelected.length >= bundleGiftQ;

  const sameLine = window.ECOPROF_PRODUCTS.filter(x => x.cat === p.cat && x.id !== p.id);
  let related = sameLine.slice(0, 4);
  // If category has < 4 products, supplement with bestsellers from other lines
  if (related.length < 4) {
    const extras = window.ECOPROF_PRODUCTS
      .filter(x => x.id !== p.id && x.cat !== p.cat && x.badges.includes('best'))
      .filter(x => !related.some(r => r.id === x.id))
      .slice(0, 4 - related.length);
    related = [...related, ...extras];
  }
  const relatedTitleKey = sameLine.length > 0 ? 'related.same' : 'related.also';

  const variant = settings?.saleStyle || 'b';
  const promoBadge = window.promoBadge(p.promo, lang);
  const promoLine = window.promoLine(p.promo, lang);
  const onSale = false;

  // Gallery slides: base shots + optional before/after + optional video
  // Gallery: product images (same size, like real admin uploads) + optional before/after.
  const slides = [0, 1, 2, ...(p.beforeAfter ? ['ba'] : [])];
  const curIdx = Math.max(0, slides.indexOf(activeImg));
  const goSlide = (dir) => setActiveImg(slides[(curIdx + dir + slides.length) % slides.length]);

  // Mobile swipe on the gallery image → prev/next slide. Touch-only (desktop uses arrows).
  const touchX = React.useRef(null);
  const onGalleryTouchStart = (e) => { touchX.current = e.touches[0] ? e.touches[0].clientX : null; };
  const onGalleryTouchEnd = (e) => {
    if (touchX.current == null) return;
    const endX = e.changedTouches[0] ? e.changedTouches[0].clientX : touchX.current;
    const dx = endX - touchX.current;
    touchX.current = null;
    if (slides.length > 1 && Math.abs(dx) > 40) goSlide(dx < 0 ? 1 : -1);
  };

  return (
    <div className="ep-pdp-root">
      <div className="ep-container" style={{ paddingTop: 16, paddingBottom: 8 }}>
        <div className="ep-mono" style={{ fontSize: 11 }}>
          <a onClick={() => goto({ name: 'home' })} style={{ cursor:'pointer' }}>{t('nav.shop')}</a>
          <span style={{ margin: '0 8px' }}>/</span>
          <a>{tr(cat.name)}</a>
          <span style={{ margin: '0 8px' }}>/</span>
          <span style={{ color: 'var(--ep-ink)' }}>{p.name}</span>
        </div>
      </div>

      <div className="ep-container">
        <div className="ep-pdp-grid">
          {/* Gallery — full-bleed image, thumbnails overlay on hover */}
          <div className="ep-pdp-gallery">
            <div className="ep-pdp-main" onTouchStart={onGalleryTouchStart} onTouchEnd={onGalleryTouchEnd}>
              {/* why: gallery badges (NEW / promo / award) show only on the product photos.
                  In the before/after view they collide top-right with the «N днів» badge —
                  that view has its own До / N днів labels, so hide gallery badges there. */}
              {activeImg !== 'ba' && (
                <>
                  {p.badges.includes('new') && (
                    <span className="ep-badge new" style={{ position:'absolute', top:24, left:24, zIndex: 6 }}>NEW</span>
                  )}
                  {promoBadge && (
                    <span className="ep-promo-pill" style={{ position:'absolute', top: p.badges.includes('new') ? 66 : 24, left: 24, right: 'auto', zIndex: 6 }}>{promoBadge}</span>
                  )}
                  {p.award && (
                    <div style={{ position:'absolute', top: 24, right:24, zIndex: 6 }}>
                      <span className="ep-award-pill" style={{ '--award-tint': window.ECOPROF_AWARDS[p.award]?.tint }}>
                        {window.ECOPROF_AWARDS[p.award]?.label}
                      </span>
                    </div>
                  )}
                </>
              )}
              {slides.length > 1 && (
                <>
                  <button className="ep-pdp-arrow prev" onClick={() => goSlide(-1)} aria-label="previous image">
                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M15 18l-6-6 6-6"/></svg>
                  </button>
                  <button className="ep-pdp-arrow next" onClick={() => goSlide(1)} aria-label="next image">
                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M9 18l6-6-6-6"/></svg>
                  </button>
                </>
              )}
              {/* Main shows BA composition when BA thumb is selected */}
              {activeImg === 'ba' && p.beforeAfter ? (
                <div style={{ position:'relative', width:'100%', height:'100%' }}>
                  {/* padding:0 overrides .ep-pdp-main img { padding:6% } — that inset is for transparent product PNGs, not the full-bleed before/after photo */}
                  <img src="portraits/ba.jpg" alt=""
                    style={{ width: '100%', height: '100%', objectFit: 'cover', padding: 0, display: 'block' }}/>
                  <div style={{ position:'absolute', top: 0, bottom: 0, left:'50%', width: 1.5, transform:'translateX(-50%)', background:'rgba(255,255,255,0.85)', boxShadow:'0 0 6px rgba(0,0,0,0.15)', pointerEvents:'none', zIndex: 4 }}/>
                  <div style={{ position:'absolute', top: 16, left: 16, zIndex: 5, padding:'4px 10px', borderRadius: 999, background:'rgba(255,255,255,0.92)', backdropFilter:'blur(4px)', fontFamily:'var(--ep-font-mono)', fontSize: 10, fontWeight: 600, letterSpacing:'0.12em', textTransform:'uppercase', color:'var(--ep-ink)' }}>{lang==='uk' ? 'До' : 'Before'}</div>
                  <div style={{ position:'absolute', top: 16, right: 16, zIndex: 5, padding:'4px 10px', borderRadius: 999, background:'var(--ep-accent)', color:'var(--ep-accent-ink)', fontFamily:'var(--ep-font-mono)', fontSize: 10, fontWeight: 600, letterSpacing:'0.12em', textTransform:'uppercase' }}>{lang==='uk' ? `${p.beforeAfter.days} днів` : `${p.beforeAfter.days} days`}</div>
                </div>
              ) : (
                <ProductMedia p={p}/>
              )}

            </div>
            {/* Gallery bar (watch-video + thumbnails) — SIBLING of .ep-pdp-main, not a child.
                why: on desktop it's an absolute overlay (hover-revealed) over the image;
                on mobile it becomes a static row BELOW the image so it no longer overlaps the photo. */}
            {(videos.length > 0 || slides.length > 1) && (
              <div className="ep-pdp-gallery-bar">
                {videos.length > 0 && (
                  <button className="ep-pdp-vidbtn" onClick={() => { setVidIdx(0); setVidOpen(true); }}>
                    <span className="pp"><svg width="13" height="13" viewBox="0 0 24 24" fill="currentColor"><path d="M7 5l13 7-13 7z"/></svg></span>
                    {videos.length > 1
                      ? (lang==='uk' ? 'Відео' : 'Videos')
                      : (lang==='uk' ? 'Дивитись відео' : 'Watch video')}
                    {videos.length > 1 && <span className="ct">{videos.length}</span>}
                  </button>
                )}
                {slides.length > 1 && (
                  <div className="ep-pdp-thumbs-ov">
                    {slides.map((key) => {
                      const active = activeImg === key;
                      if (key === 'ba') return (
                        <button key="ba" className={`ep-pdp-thumb-ov ba ${active ? 'active' : ''}`} onClick={() => setActiveImg('ba')} aria-label={lang==='uk' ? 'До / після' : 'before / after'}>
                          <img src="portraits/ba.jpg" alt=""/>
                          <span className="ba-split"/>
                          <span className="ba-tag">{lang==='uk' ? 'До/Після' : 'B / A'}</span>
                        </button>
                      );
                      return (
                        <button key={key} className={`ep-pdp-thumb-ov ${active ? 'active' : ''}`} onClick={() => setActiveImg(key)} aria-label="product image">
                          <ProductMedia p={p}/>
                        </button>
                      );
                    })}
                  </div>
                )}
              </div>
            )}
          </div>

          {/* Info */}
          <div className="ep-pdp-info">
            <div className="ep-eyebrow" style={{ color: cat.ink }}>{tr(cat.name)} · {p.vol}</div>
            <h1 className="ep-sell-title">{p.name}</h1>
            <p className="ep-sell-tagline">{tr(p.sub)}</p>

            <div style={{ display:'flex', alignItems:'center', gap: 12 }}>
              <span style={{ color:'#d9a04a', display:'inline-flex', gap: 1 }}>
                {Array.from({length:5}).map((_,i) => <I.star key={i}/>)}
              </span>
              <span style={{ fontSize: 13, color: 'var(--ep-ink-2)', whiteSpace: 'nowrap' }}>{p.rating.toFixed(2)} · {p.reviews} {t('pdp.reviews')}</span>
            </div>

            {p.award && (
              <div className="ep-pdp-awards" style={{ marginTop: 20 }}>
                {[p.award, 'editor-choice-2025'].slice(0, 2).map((a, i) => {
                  const aw = window.ECOPROF_AWARDS[a];
                  if (!aw) return null;
                  return (
                    <div key={i} className="ep-pdp-award">
                      <div className="medal" style={{ '--medal': cat.ink }}>{aw.label.split(' ')[0].slice(0,3)}</div>
                      <div className="meta">
                        <div className="a">{aw.label}</div>
                        <div className="b">{tr(aw.sub)}</div>
                      </div>
                    </div>
                  );
                })}
              </div>
            )}

            <div className="ep-sell-pricebox">
              <span className="ep-sell-price">{fmt(p.price)}</span>
            </div>
            {promoLine && (
              <div className="ep-promo-callout">
                <span className="b">{promoBadge}</span>
                <span className="l">{promoLine}</span>
              </div>
            )}

            {/* Before/After & video live in the gallery (thumbnail + main view) */}

            <ul className="ep-sell-checks">
              {[t('pdp.bullets.1'), t('pdp.bullets.2'), t('pdp.bullets.3'), t('pdp.bullets.4')].map((b, i) => (
                <li key={i}>
                  <span className="ck"><svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round"><path d="M4 12l5 5L20 7"/></svg></span>
                  {b}
                </li>
              ))}
            </ul>

            <div className="ep-sell-actions">
              <button className="ep-btn accent xl" onClick={() => addToCart(p.id)}>
                {t('pdp.btn.add')}
              </button>
              <button className="ep-btn xl ep-btn-oneclick" onClick={() => { setQbForm({ name:'', phone:'', email:'', comment:'' }); setQbOpen(true); }}>
                <svg width="15" height="15" viewBox="0 0 24 24" fill="currentColor"><path d="M13 2L4.5 13H11l-1 9 8.5-11H12z"/></svg>
                {lang==='uk' ? 'Buy with Shop Pay' : 'Buy with Shop Pay'}
              </button>
              <button
                onClick={() => toggleWishlist(p.id)}
                className="ep-icon-btn"
                style={{
                  border:'1px solid var(--ep-line)', width: mobile ? 46 : 56, height: mobile ? 46 : 56,
                  color: isWished(p.id) ? 'var(--ep-accent)' : 'var(--ep-ink)',
                  background: isWished(p.id) ? 'var(--ep-accent-soft)' : 'transparent',
                }}>
                <svg width="20" height="20" viewBox="0 0 24 24" fill={isWished(p.id) ? 'currentColor' : 'none'} stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round">
                  <path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 1 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"/>
                </svg>
              </button>
            </div>

            <SellTrust lang={lang}/>
          </div>
        </div>

        {/* ── Selling-landing sections ── */}
        <SellBenefits p={p} lang={lang} mobile={mobile}/>
        <SellDescription p={p} tr={tr} t={t} lang={lang} mobile={mobile}/>
        <SellIngredients lang={lang} mobile={mobile}/>
        <SellSteps p={p} lang={lang} mobile={mobile}/>
        <SellBeforeAfter p={p} lang={lang}/>

        {/* Frequently Bought Together */}
        <div className="ep-fbt">
          <div className="ep-fbt-head">
            <div>
              <div className="ep-eyebrow">{t('fbt.eyebrow')}</div>
              <h3 className="ep-section-title" style={{ fontSize: mobile ? 22 : 26, marginTop: 6 }}>{t('fbt.title')}</h3>
            </div>
          </div>
          <div className="ep-fbt-row">
            {fbtList.map((x, i) => (
              <React.Fragment key={x.id}>
                <div className={`ep-fbt-item ${bundle[x.id] ? 'checked' : ''}`}
                     onClick={() => setBundle({ ...bundle, [x.id]: !bundle[x.id] })}>
                  <div className="thumb">
                    <div className="check"/>
                    <ProductMedia p={x}/>
                  </div>
                  <div className="nm">{x.name}</div>
                  <div className="pr">{fmt(x.price)}</div>
                </div>
                {i < fbtList.length - 1 && <span className="ep-fbt-plus">+</span>}
              </React.Fragment>
            ))}
            <span className="ep-fbt-plus equals">=</span>
            <div className={`ep-fbt-giftnode ${bundleGiftEarned ? 'on' : ''}`}>
              <div className="thumb">
                <img src="images/gift.png" alt=""/>
                <span className="giftico">
                  <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M20 12v9H4v-9M2 7h20v5H2zM12 22V7M12 7S10.5 3 8 3a2 2 0 0 0 0 5zM12 7s1.5-4 4-4a2 2 0 0 1 0 5z"/></svg>
                </span>
              </div>
              <div className="nm">{lang==='uk' ? 'Подарунок' : 'Gift'}</div>
              <div className="gsub">{lang==='uk' ? 'косметичка + 3 семпли' : 'pouch + 3 samples'}</div>
            </div>
          </div>
          <div className="ep-fbt-total">
            <div style={{ display:'flex', alignItems:'center', gap: 14, flexWrap:'wrap' }}>
              <div style={{ display:'flex', alignItems:'baseline', gap: 10 }}>
                <span style={{ color:'var(--ep-ink-3)', fontSize: 13 }}>{t('fbt.total')}</span>
                <span style={{ fontSize: 22, fontWeight: 600, fontFamily: 'var(--ep-font-display)' }}>{fmt(bundleTotal)}</span>
              </div>
              {bundleGiftEarned ? (
                <span className="ep-fbt-gift on">
                  <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"><path d="M4 12l5 5L20 7"/></svg>
                  {lang==='uk' ? 'Подарунок у замовленні' : 'Gift included'}
                </span>
              ) : (
                <span className="ep-fbt-gift">
                  {lang==='uk'
                    ? `Ще ${bundleGiftQ - bundleSelected.length} до подарунка`
                    : `${bundleGiftQ - bundleSelected.length} more to the gift`}
                </span>
              )}
            </div>
            <button className="ep-btn" onClick={() => bundleSelected.forEach(x => addToCart(x.id))}>
              {t('fbt.add')} · {bundleSelected.length} {t('fbt.pcs')}
            </button>
          </div>
        </div>

        {/* Repeat CTA */}
        <SellRepeatCTA p={p} lang={lang} fmt={fmt} addToCart={addToCart} onBuy={() => addToCart(p.id, qty)}/>

        {/* Related */}
        {related.length > 0 && (
        <section className="ep-section" style={{ paddingBottom: mobile ? 32 : 64 }}>
          <div className="ep-section-head">
            <div>
              <div className="ep-eyebrow">{sameLine.length > 0 ? `${tr(cat.name)} · ${t('pdp.relSub')}` : (lang === 'uk' ? 'Також рекомендуємо' : 'Also recommended')}</div>
              <h2 className="ep-section-title" style={{ fontSize: mobile ? 26 : 32 }}>{sameLine.length > 0 ? t('pdp.related') : (lang === 'uk' ? 'Може зацікавити' : 'You might also like')}</h2>
            </div>
          </div>
          <div className="ep-grid">
            {related.map(rp => <ProductCard key={rp.id} p={rp}/>)}
          </div>
        </section>
        )}

        {/* FAQ + reviews — last sections before footer */}
        <SellFaq p={p} lang={lang} mobile={mobile}/>
        <SellReviews p={p} t={t} lang={lang} mobile={mobile}/>
      </div>

      {qbOpen && (
        <div className="ep-qb-backdrop" onClick={() => setQbOpen(false)}>
          <div className="ep-qb" onClick={(e) => e.stopPropagation()}>
            <button className="ep-qb-x" onClick={() => setQbOpen(false)} aria-label="close">
              <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round"><path d="M6 6l12 12M18 6L6 18"/></svg>
            </button>
            {(
              <>
                <div className="ep-eyebrow">{lang==='uk' ? 'Купівля в 1 клік' : '1-click order'}</div>
                <h3 className="ep-qb-h">{lang==='uk' ? 'Швидке замовлення' : 'Quick order'}</h3>
                <div className="ep-qb-sum">
                  <div className="th"><img src={p.image} alt=""/></div>
                  <div style={{ flex:1, minWidth:0 }}>
                    <div style={{ fontSize:14, fontWeight:600, color:'var(--ep-ink)' }}>{p.name}</div>
                    <div style={{ fontSize:12, color:'var(--ep-ink-3)' }}>{qty} × {fmt(p.price)}</div>
                  </div>
                  <div style={{ fontWeight:700, fontFamily:'var(--ep-font-display)', fontSize:18 }}>{fmt(p.price*qty)}</div>
                </div>
                <div className="ep-qb-qty">
                  <span className="lb">{lang==='uk' ? 'Кількість' : 'Quantity'}</span>
                  <div className="ep-qb-stepper">
                    <button type="button" onClick={() => setQty(Math.max(1, qty - 1))} disabled={qty <= 1} aria-label="−">−</button>
                    <span>{qty}</span>
                    <button type="button" onClick={() => setQty(qty + 1)} aria-label="+">+</button>
                  </div>
                </div>
                <label className="ep-qb-field">
                  <span className="lb">{lang==='uk' ? 'Номер телефону' : 'Phone number'}</span>
                  <input type="tel" value={qbForm.phone} onChange={(e)=>setQbForm({...qbForm, phone:e.target.value})} placeholder="+38 (0__) ___ __ __"/>
                </label>
                <label className="ep-qb-field">
                  <span className="lb">Email</span>
                  <input type="email" value={qbForm.email} onChange={(e)=>setQbForm({...qbForm, email:e.target.value})} placeholder="you@email.com"/>
                </label>
                <label className="ep-qb-field">
                  <span className="lb">{lang==='uk' ? 'Імʼя (необовʼязково)' : 'Name (optional)'}</span>
                  <input type="text" value={qbForm.name} onChange={(e)=>setQbForm({...qbForm, name:e.target.value})} placeholder={lang==='uk'?'Як до вас звертатися':'Your name'}/>
                </label>
                <label className="ep-qb-field">
                  <span className="lb">{lang==='uk' ? 'Коментар (необовʼязково)' : 'Comment (optional)'}</span>
                  <textarea rows={2} value={qbForm.comment} onChange={(e)=>setQbForm({...qbForm, comment:e.target.value})} placeholder={lang==='uk'?'Побажання до замовлення':'Notes for your order'}/>
                </label>
                <button className="ep-btn accent" style={{ width:'100%', marginTop: 4 }}
                  disabled={!qbForm.phone.trim() || !qbForm.email.trim()}
                  onClick={() => {
                    addToCart(p.id, qty);
                    setCheckoutPrefill({ name: qbForm.name, phone: qbForm.phone, email: qbForm.email, comment: qbForm.comment });
                    setQbOpen(false);
                    goto({ name: 'checkout' });
                  }}>
                  {lang==='uk' ? 'Перейти до оформлення' : 'Continue to checkout'} <span style={{ fontSize: 15 }}>→</span>
                </button>
                <p className="ep-qb-note">{lang==='uk' ? 'Далі — доставка й оплата. Ваші контакти вже будуть заповнені.' : 'Next — delivery & payment. Your contacts will be pre-filled.'}</p>
              </>
            )}
          </div>
        </div>
      )}

      {vidOpen && videos.length > 0 && (
        <div className="ep-vid-backdrop" onClick={() => setVidOpen(false)}>
          <div className="ep-vid" onClick={(e) => e.stopPropagation()}>
            <button className="ep-qb-x" onClick={() => setVidOpen(false)} aria-label="close" style={{ background:'rgba(255,255,255,0.9)' }}>
              <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round"><path d="M6 6l12 12M18 6L6 18"/></svg>
            </button>
            <div className="ep-vid-stage">
              <span className="ep-vid-kind">{vLabel(videos[vidIdx])}</span>
              <button className="ep-pdp-video-play" aria-label="play">
                <svg width="22" height="22" viewBox="0 0 24 24" fill="currentColor"><path d="M7 5l13 7-13 7z"/></svg>
              </button>
            </div>
            <div className="ep-vid-meta">
              <div className="e">{vLabel(videos[vidIdx])} · «{p.name}»</div>
              <div className="t">{t('pdp.yt.title')}</div>
              <div className="d">{videos[vidIdx].dur || '—'}</div>
            </div>

            {videos.length > 1 && (
              <div className="ep-vid-rail">
                {videos.map((v, i) => (
                  <button key={i} className={`ep-vid-railitem ${i === vidIdx ? 'active' : ''}`} onClick={() => setVidIdx(i)}>
                    <span className="thumb">
                      <ProductMedia p={p}/>
                      <span className="pp"><svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><path d="M7 5l13 7-13 7z"/></svg></span>
                    </span>
                    <span className="meta">
                      <span className="k">{vLabel(v)}</span>
                      <span className="dd">{v.dur || '—'}</span>
                    </span>
                  </button>
                ))}
              </div>
            )}
          </div>
        </div>
      )}
    </div>
  );
}

// ───────────────────────── Cart drawer ─────────────────────────
function CartDrawer({ mobile }) {
  const { cart, cartOpen, setCartOpen, setQty, addToCart, subtotal, goto, t, tr, fmt, lang } = useStore();
  const total = subtotal;
  return (
    <>
      <div className={`ep-drawer-backdrop ${cartOpen ? 'open' : ''}`} onClick={() => setCartOpen(false)}/>
      <aside className={`ep-drawer ${cartOpen ? 'open' : ''}`}>
        <div className="ep-drawer-head">
          <div>
            <div className="ep-eyebrow">{t('cart.eyebrow')}</div>
            <div style={{ fontSize: 18, fontWeight: 500, marginTop: 2 }}>
              {cart.length} {window.itemsLabel(cart.length, lang)}
            </div>
          </div>
          <button className="ep-icon-btn" onClick={() => setCartOpen(false)}><I.x/></button>
        </div>

        {subtotal > 0 && (
          <div style={{ padding: '12px 24px 0' }}>
            <div style={{ display:'flex', gap: 10, alignItems:'flex-start', padding:'10px 12px', background:'var(--ep-surface-3)', borderRadius: 10 }}>
              <span style={{ flex:'none', color:'var(--ep-accent)', transform:'translateY(1px)' }}>
                <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M3 7h11v8H3zM14 10h4l3 3v2h-7z"/><circle cx="7" cy="17" r="2"/><circle cx="17" cy="17" r="2"/></svg>
              </span>
              <div style={{ fontSize: 12, color:'var(--ep-ink-2)', lineHeight: 1.45 }}>
                {lang==='uk'
                  ? <>Доставка — за тарифом Нової Пошти, розрахується на оформленні.</>
                  : <>Delivery at the Nova Poshta tariff, calculated at checkout.</>}
              </div>
            </div>
          </div>
        )}

        <div className="ep-drawer-body">
          {cart.length === 0 && (
            <div className="ep-cart-empty">
              <div className="ic"><I.bag/></div>
              <div style={{ fontSize: 16, color: 'var(--ep-ink)', fontWeight: 500 }}>{t('cart.empty.t')}</div>
              <div style={{ fontSize: 13, maxWidth: 280 }}>{t('cart.empty.s')}</div>
              <button className="ep-btn ghost sm" style={{ marginTop: 12 }} onClick={() => { setCartOpen(false); goto({ name:'home' }); }}>{t('cart.empty.cta')}</button>
            </div>
          )}
          {cart.map(it => {
            const p = window.ECOPROF_PRODUCTS.find(x => x.id === it.id);
            if (!p) return null;
            return (
              <div key={it.id} className="ep-cart-item">
                <div className="thumb">
                  <ProductMedia p={p}/>
                </div>
                <div className="meta">
                  <div className="nm">{p.name}</div>
                  <div className="sb">{tr(p.sub)} · {p.vol}</div>
                  <div className="row">
                    <div className="qty-sm">
                      <button onClick={() => setQty(it.id, it.qty - 1)}>−</button>
                      <span>{it.qty}</span>
                      <button onClick={() => setQty(it.id, it.qty + 1)}>+</button>
                    </div>
                    <div style={{ display:'flex', alignItems:'center', gap: 10 }}>
                      <div className="pr">{fmt(p.price * it.qty)}</div>
                      <button className="x" onClick={() => setQty(it.id, 0)}><I.x/></button>
                    </div>
                  </div>
                </div>
              </div>
            );
          })}
          {cart.length > 0 && (
            <div style={{ padding: '24px 0 8px' }}>
              <div className="ep-mono" style={{ fontSize: 10, marginBottom: 14, color:'var(--ep-ink-2)' }}>{t('cart.add.more')}</div>
              <div style={{ display:'flex', flexDirection:'column', gap: 0 }}>
                {window.ECOPROF_PRODUCTS
                  .filter(x => x.badges.includes('best') && !cart.some(c => c.id === x.id))
                  .slice(0, 3)
                  .map(up => (
                    <div key={up.id} style={{
                      display:'flex', alignItems:'center', gap: 12, padding: '10px 0',
                      borderTop: '1px solid var(--ep-line-2)',
                    }}>
                      <div style={{ width: 54, height: 54, borderRadius: 8, background: '#fff', border: '1px solid var(--ep-line-2)', flex:'none', overflow:'hidden', display:'flex' }}>
                        <ProductMedia p={up}/>
                      </div>
                      <div style={{ flex: 1, minWidth: 0 }}>
                        <div style={{ fontSize: 13, fontWeight: 500, color:'var(--ep-ink)', whiteSpace:'nowrap', overflow:'hidden', textOverflow:'ellipsis' }}>{up.name}</div>
                        <div style={{ fontSize: 11, color:'var(--ep-ink-3)', marginTop: 1 }}>{up.vol}</div>
                      </div>
                      <div style={{ fontSize: 13, fontVariantNumeric:'tabular-nums', color:'var(--ep-ink-2)' }}>{fmt(up.price)}</div>
                      <button
                        onClick={() => addToCart(up.id)}
                        aria-label="add"
                        className="ep-upsell-add">
                        <svg width="11" height="11" viewBox="0 0 11 11" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"><path d="M5.5 1v9M1 5.5h9"/></svg>
                      </button>
                    </div>
                ))}
              </div>
            </div>
          )}
        </div>

        {cart.length > 0 && (
          <div className="ep-drawer-foot">
            <div className="ep-cart-totals">
              <div className="row"><span>{t('cart.sub')}</span><span>{fmt(subtotal)}</span></div>
              <div className="row"><span>{t('cart.ship')}</span><span style={{ color:'var(--ep-ink-3)' }}>{lang==='uk'?'на оформленні':'at checkout'}</span></div>
              <div className="row total"><span>{t('cart.tot')}</span><span>{fmt(total)}</span></div>
            </div>
            <button className="ep-btn accent lg full" onClick={() => { setCartOpen(false); goto({ name: 'checkout' }); }}>{t('cart.checkout')} <I.arrow/></button>
            <div style={{ textAlign:'center', fontSize: 11, color:'var(--ep-ink-3)', marginTop: 12 }}>
              {t('cart.pay')}
            </div>
          </div>
        )}
      </aside>
    </>
  );
}

// ───────────────────────── Mobile nav ─────────────────────────
function MobileMenu() {
  const { menuOpen, setMenuOpen, goto, t } = useStore();
  return (
    <div className={`ep-mob-menu ${menuOpen ? 'open' : ''}`}>
      <div style={{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom: 24 }}>
        <div className="ep-logo">ecoprof<span className="dot"/></div>
        <button className="ep-icon-btn" onClick={() => setMenuOpen(false)}><I.x/></button>
      </div>
      <a onClick={() => goto({ name:'catalog' })}>{t('nav.shop')}</a>
      <a onClick={() => goto({ name:'lines' })}>{t('nav.lines')}</a>
      <a>{t('nav.journal')}</a>
      <a>{t('nav.about')}</a>
      <div style={{ marginTop:'auto', borderTop:'1px solid var(--ep-line-2)', paddingTop: 16 }}>
        <div style={{ marginBottom: 12 }}><LangToggle/></div>
        <div className="ep-mono">{t('mob.bottom')}</div>
      </div>
    </div>
  );
}

// ───────────────────────── Store (root) ─────────────────────────
function Store({ mobile = false, settings }) {
  const rootRef = React.useRef(null);
  useApplySettings(rootRef, settings);
  return (
    <StoreProvider settings={settings}>
      <div ref={rootRef} className={`ep-root ${mobile ? 'is-mobile' : ''}`}>
        <StoreInner mobile={mobile}/>
      </div>
    </StoreProvider>
  );
}

function StoreInner({ mobile }) {
  const { route, lang } = useStore();
  const innerRef = React.useRef(null);

  // On route change, scroll the closest scrollable ancestor to the top.
  // The Store renders inside an .ep-board iframe-like container that scrolls
  // (not the window), so window.scrollTo would do nothing here.
  React.useEffect(() => {
    let el = innerRef.current;
    while (el) {
      if (el.scrollHeight > el.clientHeight + 1) {
        try { el.scrollTo({ top: 0, behavior: 'instant' }); } catch { el.scrollTop = 0; }
        return;
      }
      el = el.parentElement;
    }
    try { window.scrollTo({ top: 0, behavior: 'instant' }); } catch {}
  }, [route]);

  return (
    <div ref={innerRef}>
      <PromoStrip/>
      <Header mobile={mobile}/>
      <main>
        {route.name === 'home' && (
          <>
            <Hero mobile={mobile}/>
            <CategoryCircles mobile={mobile}/>
            <AwardsStrip/>
            <Bestsellers mobile={mobile}/>
            <QuizCallout mobile={mobile}/>
            <StoryBlock mobile={mobile}/>
          </>
        )}
        {route.name === 'product' && <ProductPage id={route.id} mobile={mobile}/>}
        {route.name === 'catalog' && <CatalogPage preset={route.cat} tag={route.tag} mobile={mobile}/>}
        {route.name === 'lines' && <LinesPage mobile={mobile}/>}
        {route.name === 'about' && <AboutPage mobile={mobile}/>}
        {route.name === 'journal' && <JournalPage mobile={mobile}/>}
        {route.name === 'article' && <ArticlePage id={route.id} mobile={mobile}/>}
        {route.name === 'contact' && <ContactPage mobile={mobile}/>}
        {route.name === 'shipping' && <ShippingPage mobile={mobile}/>}
        {route.name === 'returns' && <ReturnsPage mobile={mobile}/>}
        {route.name === 'faq' && <FAQPage mobile={mobile}/>}
        {route.name === 'gift' && <GiftCardsPage mobile={mobile}/>}
        {route.name === 'careers' && <CareersPage mobile={mobile}/>}
        {route.name === 'role' && <RolePage id={route.id} mobile={mobile}/>}
        {route.name === 'wishlist' && <WishlistPage mobile={mobile}/>}
        {route.name === 'auth' && <AuthPage mobile={mobile}/>}
        {route.name === 'account' && <AccountPage mobile={mobile}/>}
        {route.name === 'privacy' && <PrivacyPage mobile={mobile}/>}
        {route.name === 'terms' && <TermsPage mobile={mobile}/>}
        {route.name === 'cookies' && <CookiesPage mobile={mobile}/>}
        {route.name === 'checkout' && <CheckoutPage mobile={mobile}/>}
        {route.name === 'quiz' && <QuizPage mobile={mobile}/>}
        {route.name === 'consult' && <ConsultPage mobile={mobile}/>}
      </main>
      <Footer mobile={mobile}/>
      <CartDrawer mobile={mobile}/>
      <CartToast/>
      <SearchOverlay/>
      <CookieBanner/>
      <AuthModal/>
      <a href="https://t.me/ecoprof_support" target="_blank" rel="noopener noreferrer" className="ep-tg-fab" aria-label={lang==='uk'?'Чат у Telegram':'Telegram chat'}>
        <svg width="26" height="26" viewBox="0 0 24 24" fill="currentColor"><path d="M21.9 4.3 18.5 20c-.25 1.1-.9 1.37-1.83.85l-5.05-3.72-2.44 2.35c-.27.27-.5.5-1.02.5l.36-5.16 9.4-8.49c.4-.36-.09-.56-.63-.2L5.06 13.0 .08 11.44c-1.08-.34-1.1-1.08.23-1.6L20.5 2.78c.9-.33 1.69.2 1.4 1.52z"/></svg>
        <span className="ep-tg-fab-label">{lang==='uk'?'Допомога':'Help'}</span>
      </a>
      {mobile && <MobileMenu/>}
    </div>
  );
}

Object.assign(window, { ProductPage, CartDrawer, MobileMenu, Store, StoreInner });
