
const { useState, useEffect } = React;

/* ── Shared helpers ── */
const Sec = ({ id, bg = '#1A1A1A', children, py = '100px' }) => (
  <section id={id} style={{ background: bg, padding: `${py} 0`, overflow: 'hidden', position: 'relative' }}>
    <div style={{ maxWidth: '1200px', margin: '0 auto', padding: '0 48px' }}>{children}</div>
  </section>
);

const Badge = ({ text }) => (
  <div style={{
    display: 'inline-flex', alignItems: 'center', gap: '10px',
    fontFamily: "'JetBrains Mono', monospace", fontSize: '10px',
    color: '#E86A00', letterSpacing: '3px', marginBottom: '20px',
  }}>
    <div style={{ width: '24px', height: '1px', background: '#E86A00' }} />
    {text}
    <div style={{ width: '24px', height: '1px', background: '#E86A00' }} />
  </div>
);

const ImgPlaceholder = ({ label, height = 260 }) => (
  <div style={{
    width: '100%', height,
    background: 'repeating-linear-gradient(135deg,#1E1E1E 0,#1E1E1E 10px,#242424 10px,#242424 20px)',
    display: 'flex', alignItems: 'center', justifyContent: 'center',
    border: '1px solid rgba(232,106,0,0.12)',
  }}>
    <span style={{
      fontFamily: "'JetBrains Mono', monospace", fontSize: '10px',
      color: 'rgba(245,240,235,0.22)', letterSpacing: '2px', textTransform: 'uppercase',
    }}>{label}</span>
  </div>
);

const Stars = ({ n = 5 }) => (
  <div style={{ display: 'flex', gap: '3px' }}>
    {Array.from({ length: 5 }).map((_, i) => (
      <svg key={i} width="14" height="14" viewBox="0 0 24 24" fill={i < n ? '#E86A00' : 'rgba(245,240,235,0.15)'}>
        <polygon points="12,2 15.09,8.26 22,9.27 17,14.14 18.18,21.02 12,17.77 5.82,21.02 7,14.14 2,9.27 8.91,8.26" />
      </svg>
    ))}
  </div>
);

/* ── Countdown Timer ── */
const Countdown = ({ ms, large = false }) => {
  const [left, setLeft] = useState(ms);
  useEffect(() => {
    const iv = setInterval(() => setLeft(p => Math.max(0, p - 1000)), 1000);
    return () => clearInterval(iv);
  }, []);
  const h = Math.floor(left / 3.6e6);
  const m = Math.floor((left % 3.6e6) / 6e4);
  const s = Math.floor((left % 6e4) / 1000);
  const pad = n => n.toString().padStart(2, '0');
  const numSize = large ? '56px' : '36px';
  const labelSize = '9px';
  return (
    <div style={{ display: 'flex', gap: large ? '12px' : '6px', alignItems: 'center' }}>
      {[[pad(h), 'HRS'], [pad(m), 'MIN'], [pad(s), 'SEC']].map(([v, l], i, arr) => (
        <React.Fragment key={l}>
          <div style={{ textAlign: 'center' }}>
            <div style={{ fontFamily: "'JetBrains Mono', monospace", fontSize: numSize, fontWeight: '600', color: '#E86A00', lineHeight: 1, minWidth: large ? '78px' : '48px' }}>{v}</div>
            <div style={{ fontFamily: "'JetBrains Mono', monospace", fontSize: labelSize, color: 'rgba(245,240,235,0.3)', letterSpacing: '3px', marginTop: '4px' }}>{l}</div>
          </div>
          {i < 2 && <span style={{ fontFamily: "'JetBrains Mono', monospace", fontSize: large ? '28px' : '18px', color: 'rgba(232,106,0,0.4)', marginBottom: large ? '20px' : '14px' }}>:</span>}
        </React.Fragment>
      ))}
    </div>
  );
};

/* ── Progress Bar ── */
const ProgressBar = ({ current, target, label }) => {
  const [w, setW] = useState(0);
  const pct = Math.min(100, (current / target) * 100);
  useEffect(() => { const t = setTimeout(() => setW(pct), 400); return () => clearTimeout(t); }, [pct]);
  return (
    <div style={{ marginBottom: '40px' }}>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', marginBottom: '14px' }}>
        <span style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: '14px', color: 'rgba(245,240,235,0.5)', fontWeight: '500' }}>{label}</span>
        <span style={{ fontFamily: "'JetBrains Mono', monospace", fontSize: '26px', color: '#E86A00', fontWeight: '600', lineHeight: 1 }}>
          {current}<span style={{ fontSize: '15px', color: 'rgba(245,240,235,0.3)' }}> / {target}</span>
        </span>
      </div>
      <div style={{ height: '5px', background: 'rgba(245,240,235,0.07)', overflow: 'hidden' }}>
        <div style={{
          height: '100%', width: `${w}%`,
          background: 'linear-gradient(90deg, #E86A00 0%, #FF9A3C 100%)',
          transition: 'width 1.5s cubic-bezier(0.4,0,0.2,1)',
          position: 'relative',
        }}>
          <div style={{ position: 'absolute', right: 0, inset: 0, background: 'linear-gradient(90deg, transparent 60%, rgba(255,255,255,0.25))' }} />
        </div>
      </div>
      <div style={{ marginTop: '6px', textAlign: 'right', fontFamily: "'JetBrains Mono', monospace", fontSize: '10px', color: 'rgba(245,240,235,0.22)', letterSpacing: '1px' }}>
        {Math.round(pct)}% COMPLETE
      </div>
    </div>
  );
};

/* ════════════════════════════════
   SECTION 2 — CHALLENGE
════════════════════════════════ */
const ChallengeSection = () => {
  const [ch, setCh] = useState({ bags: 127, bagsTarget: 200, boxes: 43, boxesTarget: 100 });
  useEffect(() => {
    if (!window.SUPABASE_READY) return;
    (async () => {
      const { data } = await window.db.from('cms_settings').select('key,value')
        .in('key', ['challenge_72hr_current','challenge_72hr_target','challenge_30day_current','challenge_30day_target']);
      if (data) {
        const m = Object.fromEntries(data.map(r => [r.key, +r.value]));
        setCh({ bags: m['challenge_72hr_current']||0, bagsTarget: m['challenge_72hr_target']||200, boxes: m['challenge_30day_current']||0, boxesTarget: m['challenge_30day_target']||100 });
      }
    })();
  }, []);
  return (
  <Sec id="challenge" bg="#111111">
    <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '80px', alignItems: 'center' }} className="mobile-stack">
      <div>
        <Badge text="STREET CHALLENGE" />
        <h2 style={{ fontFamily: "'Bebas Neue', sans-serif", fontSize: 'clamp(48px,6vw,80px)', letterSpacing: '3px', color: '#F5F0EB', lineHeight: 0.92, marginBottom: '20px' }}>
          The Street<br /><span style={{ color: '#E86A00' }}>Challenge</span><br />is LIVE
        </h2>
        <p style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: '16px', color: 'rgba(245,240,235,0.55)', lineHeight: 1.7, marginBottom: '40px' }}>
          We're on a mission to move 200 packs in 72 hours. Join the movement.
        </p>
        <div style={{ marginBottom: '20px' }}>
          <div style={{ fontFamily: "'JetBrains Mono', monospace", fontSize: '10px', color: 'rgba(245,240,235,0.3)', letterSpacing: '3px', marginBottom: '16px' }}>TIME REMAINING</div>
          <Countdown ms={48 * 3600 * 1000} large={true} />
        </div>
      </div>
      <div>
        <ProgressBar current={ch.bags} target={ch.bagsTarget} label="72 Hour Street Challenge — bags" />
        <ProgressBar current={ch.boxes} target={ch.boxesTarget} label="30 Day Master Challenge — boxes" />
        <div style={{
          background: 'rgba(232,106,0,0.07)', border: '1px solid rgba(232,106,0,0.18)',
          padding: '20px 24px', marginTop: '8px',
        }}>
          <div style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: '14px', color: 'rgba(245,240,235,0.6)', lineHeight: 1.6 }}>
            Every bag you order counts toward the challenge. Place your order and be part of the movement.
          </div>
          <a href="order.html" style={{ display: 'inline-block', marginTop: '16px', fontFamily: "'Space Grotesk', sans-serif", fontWeight: '700', fontSize: '13px', color: '#E86A00', letterSpacing: '0.5px' }}>
            Join the challenge →
          </a>
        </div>
      </div>
    </div>
  </Sec>
  );
};

/* ════════════════════════════════
   SECTION 3 — PRODUCTS
════════════════════════════════ */
const ProductSection = () => (
  <Sec id="products" bg="#1A1A1A">
    <div style={{ textAlign: 'center', marginBottom: '64px' }}>
      <Badge text="THE PRODUCT" />
      <h2 style={{ fontFamily: "'Bebas Neue', sans-serif", fontSize: 'clamp(48px,7vw,96px)', letterSpacing: '3px', color: '#F5F0EB', lineHeight: 0.92, marginBottom: '24px' }}>
        You Remember<br /><span style={{ color: '#E86A00' }}>This.</span>
      </h2>
      <p style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: '17px', color: 'rgba(245,240,235,0.55)', lineHeight: 1.75, maxWidth: '580px', margin: '0 auto' }}>
        Xiexiang Crunch Oat Chocolates — the snack that had everyone talking. Real oats. Real crunch. Real energy. Now available again in Accra, delivered straight to you.
      </p>
    </div>

    {/* Image gallery */}
    <div style={{ display: 'grid', gridTemplateColumns: '1.4fr 1fr 1fr', gap: '12px', marginBottom: '64px' }} className="mobile-stack">
      <img src="uploads/pack-of-3-img1.jpeg" alt="3 bags of Xiexiang Crunch Oat Chocolates" style={{ width: '100%', height: '360px', objectFit: 'cover', display: 'block' }} />
      <img src="uploads/1-pack-4.jpeg"        alt="Single pack — Xiexiang Crunch Oat Chocolates" style={{ width: '100%', height: '360px', objectFit: 'cover', display: 'block' }} />
      <img src="uploads/oat-choco.jpeg"        alt="Oat Choco bars close-up" style={{ width: '100%', height: '360px', objectFit: 'cover', display: 'block' }} />
    </div>

    {/* Pricing tiers — Single / 2-pack / 3-pack (with super challenge override) */}
    <PricingTiers />
  </Sec>
);

const PricingTiers = () => {
  const [superActive, setSuperActive] = useState(window.EGGBOY_CONFIG?.business?.superChallengeActive || false);

  useEffect(() => {
    if (!window.SUPABASE_READY) return;
    (async () => {
      const { data } = await window.db.from('cms_settings').select('value').eq('key', 'super_challenge_active').maybeSingle();
      if (data) setSuperActive(data.value === 'true' || data.value === '1');
    })();
  }, []);

  const c = window.EGGBOY_CONFIG?.business || {};
  const single  = c.pricePerBag || 40;
  const two     = c.bundle2Price || 75;
  const three   = superActive ? (c.bundleChallengePrice || 99) : (c.bundle3Price || 110);
  const threeLabel = superActive ? 'SUPER CHALLENGE' : 'EVERYDAY BUNDLE';

  const tiers = [
    {
      key:'single', img:'uploads/1-pack.jpeg',
      label:'SINGLE BAG', qty:1, price:single, save:0,
      bg:'#242424', border:'1px solid rgba(245,240,235,0.08)',
      desc:'One bag. Perfect to try us out.',
    },
    {
      key:'double', img:'uploads/1-pack-4.jpeg',
      label:'GRAB 2', qty:2, price:two, save:single*2 - two,
      bg:'#1E1E1E', border:'1px solid rgba(232,106,0,0.18)',
      desc:'Pair them up. Snack now, snack later.',
    },
    {
      key:'triple', img:'uploads/pack-of-3-img2.jpeg',
      label: threeLabel, qty:3, price:three, save:single*3 - three,
      bg:'#1F1000', border:'2px solid #E86A00',
      desc: superActive ? 'Limited-time challenge price — 3 bags GHS 99!' : 'Three bags. Stock up for the week.',
      featured:true,
      coupon: superActive ? 'STREET99' : null,
    },
  ];

  return (
    <div style={{ display:'grid', gridTemplateColumns:'repeat(3,1fr)', gap:'16px', maxWidth:'1080px', margin:'0 auto' }} className="mobile-stack">
      {tiers.map(t => (
        <div key={t.key} style={{ background: t.bg, border: t.border, borderRadius:'14px', overflow:'hidden', position:'relative', display:'flex', flexDirection:'column' }}>
          {t.featured && <div style={{ position:'absolute', top:'-12px', right:'20px', background:'#E86A00', color:'#1A1A1A', fontFamily:"'JetBrains Mono',monospace", fontSize:'9px', fontWeight:'700', letterSpacing:'2px', padding:'5px 12px', borderRadius:'6px', zIndex:1 }}>{superActive ? 'CHALLENGE PRICE' : 'BEST VALUE'}</div>}
          <img src={t.img} alt={t.label} style={{ width:'100%', height:'200px', objectFit:'cover', display:'block' }} />
          <div style={{ padding:'24px 22px 26px', flex:1, display:'flex', flexDirection:'column' }}>
            <div style={{ fontFamily:"'JetBrains Mono',monospace", fontSize:'10px', color: t.featured ? '#E86A00' : 'rgba(245,240,235,0.3)', letterSpacing:'2px', marginBottom:'14px' }}>{t.label}</div>
            <div style={{ display:'flex', alignItems:'baseline', gap:'8px', marginBottom:'4px' }}>
              <div style={{ fontFamily:"'Bebas Neue',sans-serif", fontSize:'52px', color:'#F5F0EB', letterSpacing:'2px', lineHeight:1 }}>
                GHS <span style={{ color:'#E86A00' }}>{t.price}</span>
              </div>
              {t.save > 0 && <div style={{ fontFamily:"'JetBrains Mono',monospace", fontSize:'11px', color:'#1E8449', fontWeight:'600' }}>SAVE GHS {t.save}</div>}
            </div>
            {t.qty > 1 && <div style={{ fontFamily:"'JetBrains Mono',monospace", fontSize:'11px', color:'rgba(245,240,235,0.35)', marginBottom:'14px' }}>{t.qty} bags · GHS {(t.price/t.qty).toFixed(0)} per bag</div>}
            <div style={{ fontFamily:"'Space Grotesk',sans-serif", fontSize:'13px', color:'rgba(245,240,235,0.45)', lineHeight:1.6, marginBottom:'20px', flex:1 }}>{t.desc}</div>
            {t.coupon && (
              <div style={{ background:'rgba(232,106,0,0.12)', border:'1px dashed rgba(232,106,0,0.4)', padding:'7px 12px', marginBottom:'14px', fontFamily:"'JetBrains Mono',monospace", fontSize:'13px', color:'#E86A00', letterSpacing:'3px', textAlign:'center', borderRadius:'6px' }}>{t.coupon}</div>
            )}
            <a href={`order.html?qty=${t.qty}${t.coupon?`&coupon=${t.coupon}`:''}`} className="btn-primary" style={{ width:'100%', textAlign:'center', display:'block', boxSizing:'border-box' }}>
              {t.qty === 1 ? 'Order Now' : `Grab ${t.qty} Bags`} →
            </a>
          </div>
        </div>
      ))}
    </div>
  );
};

/* ════════════════════════════════
   SECTION 4 — FLASH SALE BANNER
════════════════════════════════ */
const BannerSection = () => (
  <section style={{ background: '#E86A00', padding: '60px 48px', overflow: 'hidden', position: 'relative' }}>
    <div style={{
      position: 'absolute', inset: 0,
      backgroundImage: 'repeating-linear-gradient(90deg, rgba(0,0,0,0.04) 0, rgba(0,0,0,0.04) 1px, transparent 1px, transparent 40px)',
    }} />
    <div style={{ maxWidth: '1200px', margin: '0 auto', display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: '32px', flexWrap: 'wrap', position: 'relative', zIndex: 1 }}>
      <div>
        <div style={{ fontFamily: "'JetBrains Mono', monospace", fontSize: '10px', color: 'rgba(26,26,26,0.6)', letterSpacing: '4px', marginBottom: '10px' }}>LIMITED STOCK ALERT</div>
        <div style={{ fontFamily: "'Bebas Neue', sans-serif", fontSize: 'clamp(36px,5vw,64px)', color: '#1A1A1A', lineHeight: 0.95, letterSpacing: '2px' }}>
          3 Bags for GHS 99.<br />Order Before It Runs Out.
        </div>
      </div>
      <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-end', gap: '20px' }}>
        <Countdown ms={3 * 3600 * 1000 + 47 * 60 * 1000} />
        <a href="order.html" style={{
          background: '#1A1A1A', color: '#F5F0EB',
          padding: '16px 40px',
          fontFamily: "'Space Grotesk', sans-serif",
          fontWeight: '700', fontSize: '15px', letterSpacing: '0.8px',
          textDecoration: 'none', display: 'inline-block',
          transition: 'background 0.2s',
        }}
          onMouseEnter={e => e.currentTarget.style.background = '#2d2d2d'}
          onMouseLeave={e => e.currentTarget.style.background = '#1A1A1A'}
        >Grab the Bundle →</a>
      </div>
    </div>
  </section>
);

/* ════════════════════════════════
   SECTION 5 — HOW IT WORKS
════════════════════════════════ */
const HowItWorksSection = () => (
  <Sec id="how-it-works" bg="#111111">
    <div style={{ textAlign: 'center', marginBottom: '64px' }}>
      <Badge text="THE PROCESS" />
      <h2 style={{ fontFamily: "'Bebas Neue', sans-serif", fontSize: 'clamp(44px,6vw,80px)', letterSpacing: '3px', color: '#F5F0EB', lineHeight: 0.92 }}>
        How It <span style={{ color: '#E86A00' }}>Works</span>
      </h2>
    </div>
    <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: '0' }} className="mobile-stack">
      {[
        ['01', 'Place Your Order', 'Order online or reserve with a GHS 10 deposit. Takes less than 2 minutes.'],
        ['02', 'Pay Your Way', 'Pay via MoMo, Paystack, or choose cash on delivery. Flexible for everyone.'],
        ['03', 'We Deliver', 'Same-day or next-day delivery across Accra. Track your order in real time.'],
      ].map(([num, title, desc], i) => (
        <div key={i} style={{
          padding: '48px 40px',
          borderLeft: i > 0 ? '1px solid rgba(245,240,235,0.06)' : 'none',
          position: 'relative',
        }}>
          <div style={{
            fontFamily: "'Bebas Neue', sans-serif",
            fontSize: '100px', color: 'rgba(232,106,0,0.1)',
            lineHeight: 1, position: 'absolute', top: '24px', right: '24px',
            letterSpacing: '2px',
          }}>{num}</div>
          <div style={{ fontFamily: "'JetBrains Mono', monospace", fontSize: '28px', color: '#E86A00', fontWeight: '600', marginBottom: '20px' }}>{num}</div>
          <div style={{ fontFamily: "'Bebas Neue', sans-serif", fontSize: '28px', color: '#F5F0EB', letterSpacing: '1px', marginBottom: '14px' }}>{title}</div>
          <div style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: '15px', color: 'rgba(245,240,235,0.5)', lineHeight: 1.7 }}>{desc}</div>
        </div>
      ))}
    </div>
  </Sec>
);

/* ════════════════════════════════
   SECTION 6 — DELIVERY ZONES
════════════════════════════════ */
const DeliverySection = () => (
  <Sec id="delivery" bg="#1A1A1A">
    <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '80px', alignItems: 'center' }} className="mobile-stack">
      <div>
        <Badge text="DELIVERY ZONES" />
        <h2 style={{ fontFamily: "'Bebas Neue', sans-serif", fontSize: 'clamp(44px,5.5vw,72px)', letterSpacing: '3px', color: '#F5F0EB', lineHeight: 0.92, marginBottom: '24px' }}>
          We Come<br /><span style={{ color: '#E86A00' }}>To You.</span>
        </h2>
        <p style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: '16px', color: 'rgba(245,240,235,0.5)', lineHeight: 1.7 }}>
          Covering all major zones in Accra. Same-day delivery available. WhatsApp us to confirm your area.
        </p>
        <a href="https://wa.me/233553608947" target="_blank" rel="noreferrer" style={{
          display: 'inline-flex', alignItems: 'center', gap: '8px',
          marginTop: '28px', background: '#1E8449', color: '#F5F0EB',
          padding: '12px 24px',
          fontFamily: "'Space Grotesk', sans-serif", fontWeight: '600', fontSize: '14px',
          textDecoration: 'none', transition: 'background 0.2s',
        }}
          onMouseEnter={e => e.currentTarget.style.background = '#26a35a'}
          onMouseLeave={e => e.currentTarget.style.background = '#1E8449'}
        >
          <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347z"/><path d="M12 0C5.373 0 0 5.373 0 12c0 2.127.558 4.122 1.534 5.857L0 24l6.336-1.511A11.933 11.933 0 0012 24c6.627 0 12-5.373 12-12S18.627 0 12 0zm0 21.818a9.819 9.819 0 01-5.001-1.368l-.36-.214-3.73.889.906-3.629-.235-.374A9.818 9.818 0 012.182 12C2.182 6.577 6.577 2.182 12 2.182S21.818 6.577 21.818 12 17.423 21.818 12 21.818z"/></svg>
          WhatsApp to Order
        </a>
      </div>
      <div>
        {[
          ['Kasoa', 'GHS 10'],
          ['Accra Central / Lapaz / Achimota', 'GHS 15'],
          ['East Legon / Spintex / Airport', 'GHS 20'],
          ['Outside Accra', 'GHS 40'],
        ].map(([zone, fee], i) => (
          <div key={i} style={{
            display: 'flex', justifyContent: 'space-between', alignItems: 'center',
            padding: '20px 0',
            borderBottom: '1px solid rgba(245,240,235,0.06)',
          }}>
            <span style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: '15px', color: 'rgba(245,240,235,0.7)', fontWeight: '500' }}>{zone}</span>
            <span style={{ fontFamily: "'JetBrains Mono', monospace", fontSize: '18px', color: '#E86A00', fontWeight: '600' }}>{fee}</span>
          </div>
        ))}
        <div style={{
          marginTop: '20px', padding: '14px 18px',
          background: 'rgba(30,132,73,0.1)', border: '1px solid rgba(30,132,73,0.25)',
          fontFamily: "'Space Grotesk', sans-serif", fontSize: '13px',
          color: 'rgba(245,240,235,0.6)', lineHeight: 1.6,
        }}>
          Free delivery available on orders above GHS 120. WhatsApp for bulk orders.
        </div>
      </div>
    </div>
  </Sec>
);

/* ════════════════════════════════
   SECTION 7 — TESTIMONIALS
════════════════════════════════ */
const TestimonialsSection = () => {
  const [reviews, setReviews] = useState([
    { name: 'Kofi Asante', loc: 'Accra Central', stars: 5, text: 'Brother this snack is no joke. I bought 3 bags and they were gone in 2 days. The crunch is real and it\'s actually filling.' },
    { name: 'Abena Mensah', loc: 'East Legon', stars: 5, text: 'Finally! I\'ve been searching for these everywhere. Delivered same day and came well-wrapped. EggBoyGH is different.' },
    { name: 'Kwame Darko', loc: 'Lapaz', stars: 5, text: 'The bundle deal at GHS 99 is unbeatable. Real oats, real taste. EggBoyGH is doing big things for Accra.' },
  ]);

  useEffect(() => {
    if (!window.SUPABASE_READY) return;
    (async () => {
      const { data } = await window.db.from('testimonials')
        .select('name,location,rating,text').eq('live', true);
      if (data && data.length)
        setReviews(data.map(r => ({ name: r.name, loc: r.location, stars: r.rating, text: r.text })));
    })();
  }, []);
  return (
    <Sec id="testimonials" bg="#111111">
      <div style={{ textAlign: 'center', marginBottom: '60px' }}>
        <Badge text="REVIEWS" />
        <h2 style={{ fontFamily: "'Bebas Neue', sans-serif", fontSize: 'clamp(44px,6vw,80px)', letterSpacing: '3px', color: '#F5F0EB', lineHeight: 0.92 }}>
          What People Are Saying<br /><span style={{ color: '#E86A00' }}>About the Return</span>
        </h2>
      </div>
      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: '20px' }} className="mobile-stack">
        {reviews.map((r, i) => (
          <div key={i} style={{ background: '#1A1A1A', border: '1px solid rgba(245,240,235,0.07)', padding: '36px 32px' }}>
            <Stars n={r.stars} />
            <p style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: '16px', color: 'rgba(245,240,235,0.75)', lineHeight: 1.75, margin: '20px 0 28px', fontStyle: 'italic' }}>
              "{r.text}"
            </p>
            <div>
              <div style={{ fontFamily: "'Space Grotesk', sans-serif", fontWeight: '600', fontSize: '14px', color: '#F5F0EB' }}>{r.name}</div>
              <div style={{ fontFamily: "'JetBrains Mono', monospace", fontSize: '11px', color: 'rgba(245,240,235,0.3)', letterSpacing: '1px', marginTop: '3px' }}>{r.loc}</div>
            </div>
          </div>
        ))}
      </div>
    </Sec>
  );
};

/* ════════════════════════════════
   SECTION 8 — ABOUT
════════════════════════════════ */
const AboutSection = () => (
  <section style={{ background: '#E86A00', padding: '100px 0', position: 'relative', overflow: 'hidden' }}>
    <div style={{
      position: 'absolute', inset: 0,
      backgroundImage: 'radial-gradient(circle, rgba(0,0,0,0.06) 1px, transparent 1px)',
      backgroundSize: '28px 28px',
    }} />
    <div style={{ maxWidth: '800px', margin: '0 auto', padding: '0 48px', position: 'relative', zIndex: 1, textAlign: 'center' }}>
      <div style={{ fontFamily: "'JetBrains Mono', monospace", fontSize: '10px', color: 'rgba(26,26,26,0.55)', letterSpacing: '4px', marginBottom: '24px' }}>WHY EGGBOYGH</div>
      <h2 style={{ fontFamily: "'Bebas Neue', sans-serif", fontSize: 'clamp(52px,8vw,108px)', color: '#1A1A1A', lineHeight: 0.9, letterSpacing: '3px', marginBottom: '28px' }}>
        Built for<br />the Hustle.
      </h2>
      <p style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: '18px', color: 'rgba(26,26,26,0.7)', lineHeight: 1.8, marginBottom: '44px' }}>
        We're not just delivering snacks. We're bringing back what Ghana loved and building something bigger. EggBoyGH is Ghana's street fuel brand — affordable, energetic, and built for the hustle. Oat Chocolates today. More coming soon.
      </p>
      <div style={{ display: 'flex', gap: '16px', justifyContent: 'center', flexWrap: 'wrap' }}>
        <a href="order.html" style={{
          background: '#1A1A1A', color: '#F5F0EB',
          padding: '18px 48px',
          fontFamily: "'Space Grotesk', sans-serif", fontWeight: '700', fontSize: '16px',
          letterSpacing: '0.8px', textDecoration: 'none', display: 'inline-block',
          transition: 'background 0.2s',
        }}
          onMouseEnter={e => e.currentTarget.style.background = '#2d2d2d'}
          onMouseLeave={e => e.currentTarget.style.background = '#1A1A1A'}
        >Order Now →</a>
        <a href="reservation.html" style={{
          background: 'transparent', color: '#1A1A1A',
          padding: '18px 48px', border: '2px solid rgba(26,26,26,0.35)',
          fontFamily: "'Space Grotesk', sans-serif", fontWeight: '600', fontSize: '16px',
          letterSpacing: '0.8px', textDecoration: 'none', display: 'inline-block',
          transition: 'border-color 0.2s',
        }}>Reserve Yours</a>
      </div>
    </div>
  </section>
);

/* ════════════════════════════════
   VIDEO REVIEWS SLIDER
════════════════════════════════ */
const DEMO_VIDEOS = [
  { id:1, customer_name:'Kofi Asante',  location:'Lapaz',      url:null, thumbnail:null },
  { id:2, customer_name:'Abena Mensah', location:'East Legon', url:null, thumbnail:null },
  { id:3, customer_name:'Kwame Darko',  location:'Kasoa',      url:null, thumbnail:null },
  { id:4, customer_name:'Ama Owusu',    location:'Spintex',    url:null, thumbnail:null },
];

const VideoSlider = () => {
  const [videos, setVideos] = useState(DEMO_VIDEOS);

  useEffect(() => {
    if (!window.SUPABASE_READY) return;
    (async () => {
      const { data } = await window.db.from('videos').select('*').eq('active', true).order('sort_order');
      if (data && data.length) setVideos(data);
    })();
  }, []);

  return (
    <Sec id="video-reviews" bg="#0D0D0D" py="80px">
      <div style={{ textAlign:'center', marginBottom:'48px' }}>
        <Badge text="REAL CUSTOMERS" />
        <h2 style={{ fontFamily:"'Bebas Neue',sans-serif", fontSize:'clamp(44px,6vw,80px)', letterSpacing:'3px', color:'#F5F0EB', lineHeight:0.92 }}>
          They Tried It.<br /><span style={{ color:'#E86A00' }}>Now They Can't Stop.</span>
        </h2>
      </div>

      <div style={{ display:'flex', gap:'16px', overflowX:'auto', paddingBottom:'16px', scrollSnapType:'x mandatory', WebkitOverflowScrolling:'touch', scrollbarWidth:'none', msOverflowStyle:'none' }}>
        {videos.map(v => (
          <div key={v.id} style={{ minWidth:'260px', maxWidth:'280px', scrollSnapAlign:'start', flexShrink:0, background:'#1A1A1A', border:'1px solid rgba(245,240,235,0.07)', borderRadius:'16px', overflow:'hidden' }}>
            {v.url ? (
              <video src={v.url} controls playsInline poster={v.thumbnail || undefined} style={{ width:'100%', height:'420px', objectFit:'cover', display:'block', background:'#111' }} />
            ) : (
              <div style={{ width:'100%', height:'420px', background:'repeating-linear-gradient(135deg,#1a1a1a 0,#1a1a1a 10px,#242424 10px,#242424 20px)', display:'flex', flexDirection:'column', alignItems:'center', justifyContent:'center', gap:'14px' }}>
                <div style={{ width:'60px', height:'60px', borderRadius:'50%', background:'rgba(232,106,0,0.12)', border:'2px solid rgba(232,106,0,0.3)', display:'flex', alignItems:'center', justifyContent:'center' }}>
                  <svg width="22" height="22" viewBox="0 0 24 24" fill="#E86A00"><polygon points="5,3 19,12 5,21" /></svg>
                </div>
                <div style={{ fontFamily:"'JetBrains Mono',monospace", fontSize:'9px', color:'rgba(245,240,235,0.2)', letterSpacing:'2px' }}>CUSTOMER VIDEO</div>
              </div>
            )}
            <div style={{ padding:'16px 18px' }}>
              <div style={{ fontFamily:"'Space Grotesk',sans-serif", fontWeight:'600', fontSize:'14px', color:'#F5F0EB' }}>{v.customer_name}</div>
              <div style={{ fontFamily:"'JetBrains Mono',monospace", fontSize:'10px', color:'rgba(245,240,235,0.35)', letterSpacing:'1px', marginTop:'3px' }}>{v.location}</div>
            </div>
          </div>
        ))}

        {/* CTA card */}
        <a href="https://wa.me/233553608947" target="_blank" rel="noreferrer" style={{ minWidth:'220px', scrollSnapAlign:'start', flexShrink:0, background:'rgba(232,106,0,0.06)', border:'2px dashed rgba(232,106,0,0.25)', borderRadius:'16px', display:'flex', flexDirection:'column', alignItems:'center', justifyContent:'center', gap:'14px', padding:'36px 24px', textDecoration:'none' }}>
          <div style={{ width:'52px', height:'52px', borderRadius:'50%', background:'rgba(232,106,0,0.12)', border:'2px solid rgba(232,106,0,0.3)', display:'flex', alignItems:'center', justifyContent:'center' }}>
            <svg width="20" height="20" viewBox="0 0 24 24" fill="#E86A00"><path d="M12 5v14M5 12l7 7 7-7"/></svg>
          </div>
          <div style={{ fontFamily:"'Bebas Neue',sans-serif", fontSize:'20px', color:'#E86A00', letterSpacing:'2px', textAlign:'center' }}>Share Your Review</div>
          <div style={{ fontFamily:"'Space Grotesk',sans-serif", fontSize:'13px', color:'rgba(245,240,235,0.4)', textAlign:'center', lineHeight:1.6 }}>Tried EggBoyGH? Send us your video on WhatsApp!</div>
          <div style={{ background:'#1E8449', color:'#F5F0EB', padding:'10px 22px', fontFamily:"'Space Grotesk',sans-serif", fontWeight:'700', fontSize:'13px', borderRadius:'8px' }}>WhatsApp Us</div>
        </a>
      </div>
    </Sec>
  );
};

Object.assign(window, {
  ChallengeSection,
  ProductSection,
  PricingTiers,
  BannerSection,
  HowItWorksSection,
  DeliverySection,
  TestimonialsSection,
  AboutSection,
  VideoSlider,
  CutoffBanner,
  Leaderboard,
});

/* ════════════════════════════════
   SAME-DAY CUTOFF BANNER
════════════════════════════════ */
function CutoffBanner() {
  const [ms, setMs] = useState(window.timeUntilCutoff ? window.timeUntilCutoff() : 0);
  useEffect(() => {
    const iv = setInterval(() => setMs(window.timeUntilCutoff ? window.timeUntilCutoff() : 0), 1000);
    return () => clearInterval(iv);
  }, []);
  if (!ms || ms <= 0) return null;
  const cutoffPast = new Date().getUTCHours() >= (window.EGGBOY_CONFIG?.business?.sameDayCutoffHour || 14);
  if (cutoffPast) return null;
  const h = Math.floor(ms / 3.6e6);
  const m = Math.floor((ms % 3.6e6) / 6e4);
  const s = Math.floor((ms % 6e4) / 1000);
  const pad = n => n.toString().padStart(2, '0');

  return (
    <div style={{ background:'#1E8449', padding:'10px 20px', display:'flex', alignItems:'center', justifyContent:'center', gap:'10px', flexWrap:'wrap', position:'sticky', top:0, zIndex:1001, fontFamily:"'Space Grotesk',sans-serif" }}>
      <span style={{ fontSize:'14px', color:'#F5F0EB', fontWeight:'600' }}>🛵 Order in</span>
      <span style={{ fontFamily:"'JetBrains Mono',monospace", fontSize:'14px', color:'#fff', background:'rgba(0,0,0,0.22)', padding:'4px 10px', borderRadius:'6px', fontWeight:'700' }}>
        {pad(h)}:{pad(m)}:{pad(s)}
      </span>
      <span style={{ fontSize:'14px', color:'#F5F0EB', fontWeight:'600' }}>for same-day Accra delivery!</span>
    </div>
  );
}

/* ════════════════════════════════
   LEADERBOARD — TOP CONTRIBUTORS
════════════════════════════════ */
function Leaderboard() {
  const demo = [
    { name:'Akua Barimah',  bags:24, location:'East Legon' },
    { name:'Kwame Asante',  bags:18, location:'Accra Central' },
    { name:'Kofi Mensah',   bags:15, location:'Lapaz' },
    { name:'Abena Owusu',   bags:12, location:'Spintex' },
    { name:'Yaw Boateng',   bags:9,  location:'Kasoa' },
  ];
  const [list, setList] = useState(demo);

  useEffect(() => {
    if (!window.SUPABASE_READY) return;
    (async () => {
      const since = new Date(); since.setDate(since.getDate() - 30);
      const { data } = await window.db.from('orders').select('name,zone,qty')
        .gte('created_at', since.toISOString()).neq('status', 'Cancelled');
      if (data && data.length) {
        const agg = {};
        data.forEach(o => {
          if (!agg[o.name]) agg[o.name] = { name:o.name, bags:0, location:o.zone };
          agg[o.name].bags += o.qty;
        });
        const sorted = Object.values(agg).sort((a,b) => b.bags - a.bags).slice(0,5);
        if (sorted.length) setList(sorted);
      }
    })();
  }, []);

  return (
    <Sec id="leaderboard" bg="#0D0D0D" py="80px">
      <div style={{ display:'grid', gridTemplateColumns:'1fr 1.4fr', gap:'48px', alignItems:'center' }} className="mobile-stack">
        <div>
          <Badge text="STREET CHAMPIONS" />
          <h2 style={{ fontFamily:"'Bebas Neue',sans-serif", fontSize:'clamp(40px,5.5vw,68px)', letterSpacing:'3px', color:'#F5F0EB', lineHeight:0.92, marginBottom:'20px' }}>
            Top Buyers<br /><span style={{ color:'#E86A00' }}>This Month</span>
          </h2>
          <p style={{ fontFamily:"'Space Grotesk',sans-serif", fontSize:'15px', color:'rgba(245,240,235,0.55)', lineHeight:1.7, marginBottom:'20px' }}>
            Real customers driving the movement. The top 3 each month win a free bag and bragging rights.
          </p>
          <a href="order.html" style={{ display:'inline-block', fontFamily:"'Space Grotesk',sans-serif", fontWeight:'700', fontSize:'13px', color:'#E86A00' }}>Climb the board →</a>
        </div>
        <div>
          {list.map((p, i) => {
            const rank = i + 1;
            const medal = ['🥇','🥈','🥉'][i] || '';
            return (
              <div key={i} style={{ display:'flex', alignItems:'center', gap:'16px', padding:'16px 20px', background: rank <= 3 ? 'rgba(232,106,0,0.05)' : '#1A1A1A', border:'1px solid rgba(245,240,235,0.05)', borderRadius:'10px', marginBottom:'8px' }}>
                <div style={{ width:'36px', textAlign:'center', fontFamily:"'JetBrains Mono',monospace", fontSize: medal ? '22px' : '14px', color: rank <= 3 ? '#E86A00' : 'rgba(245,240,235,0.3)', fontWeight:'700' }}>{medal || `#${rank}`}</div>
                <div style={{ flex:1 }}>
                  <div style={{ fontFamily:"'Space Grotesk',sans-serif", fontWeight:'600', fontSize:'15px', color:'#F5F0EB' }}>{p.name}</div>
                  <div style={{ fontFamily:"'JetBrains Mono',monospace", fontSize:'10px', color:'rgba(245,240,235,0.35)', letterSpacing:'1px', marginTop:'2px' }}>{p.location.toUpperCase()}</div>
                </div>
                <div style={{ textAlign:'right' }}>
                  <div style={{ fontFamily:"'JetBrains Mono',monospace", fontSize:'22px', color:'#E86A00', fontWeight:'600' }}>{p.bags}</div>
                  <div style={{ fontFamily:"'JetBrains Mono',monospace", fontSize:'9px', color:'rgba(245,240,235,0.3)', letterSpacing:'1px' }}>BAGS</div>
                </div>
              </div>
            );
          })}
        </div>
      </div>
    </Sec>
  );
}
