// Hero.jsx — MinduBier × Copa 2026 Hero with Countdown

function Hero({ tweaks, onCTAClick }) {
  const { days, hours, minutes, seconds } = useCountdown(COPA_KICKOFF);
  const variant = tweaks.heroVariant || 'celebrativo';
  const isDark = tweaks.theme === 'dark';

  return (
    <section className="hero" style={{ position: 'relative', overflow: 'hidden' }}>
      {/* Background flourishes */}
      <div aria-hidden="true" style={{
        position: 'absolute', inset: 0, pointerEvents: 'none', zIndex: 0
      }}>
        {/* Soft yellow burst */}
        <div style={{
          position: 'absolute', top: '-20%', right: '-15%',
          width: '70%', aspectRatio: '1',
          background: isDark ?
          'radial-gradient(circle, rgba(254,221,0,0.10) 0%, rgba(254,221,0,0) 60%)' :
          'radial-gradient(circle, rgba(254,221,0,0.45) 0%, rgba(254,221,0,0) 60%)',
          filter: 'blur(20px)'
        }} />
        <div style={{
          position: 'absolute', bottom: '-30%', left: '-10%',
          width: '60%', aspectRatio: '1',
          background: isDark ?
          'radial-gradient(circle, rgba(0,151,57,0.18) 0%, rgba(0,151,57,0) 65%)' :
          'radial-gradient(circle, rgba(0,151,57,0.20) 0%, rgba(0,151,57,0) 65%)',
          filter: 'blur(30px)'
        }} />
      </div>

      <div className="container" style={{
        position: 'relative', zIndex: 1,
        padding: 'clamp(80px, 12vw, 130px) 20px clamp(40px, 6vw, 80px)'
      }}>
        <div className="hero-grid" style={{
          display: 'grid',
          gap: 'clamp(32px, 5vw, 64px)',
          alignItems: 'center'
        }}>
          {/* TEXT side */}
          <div className="hero-text fade-up" style={{ order: variant === 'produto' ? 0 : 0 }}>
            <div className="badge badge-copa fade-up" style={{ marginBottom: 20 }}>
              <Icon.Trophy size={14} /> Edição limitada · Copa 2026
            </div>

            <h1 className="fade-up-delay-1" style={{
              fontSize: 'clamp(40px, 7vw, 80px)',
              fontFamily: MB.display,
              fontWeight: 800,
              lineHeight: 0.98,
              letterSpacing: '-0.025em',
              margin: 0,
              color: isDark ? '#fffff4' : MB.bgDarker
            }}>
              COM MINDUBIER,<br />
              <span style={{
                background: 'linear-gradient(135deg, var(--copa-green-deep) 0%, var(--copa-green) 45%, var(--copa-yellow) 100%)',
                WebkitBackgroundClip: 'text',
                WebkitTextFillColor: 'transparent',
                backgroundClip: 'text'
              }}>JÁ SOMOS HEXA!</span>
            </h1>

            <p className="fade-up-delay-2" style={{
              fontFamily: MB.body,
              fontSize: 'clamp(16px, 1.6vw, 19px)',
              lineHeight: 1.6,
              maxWidth: 540,
              marginTop: 20,
              color: isDark ? 'rgba(255,255,244,0.78)' : MB.bgDark
            }}>Da cervejaria mais premiada da Bahia para o peito de toda a torcida. Com a camisa da MinduBier 10 Anos, desenhada para a Copa 2026, com certeza o HEXA vem!



            </p>

            {/* Countdown */}
            <div className="fade-up-delay-2" style={{ marginTop: 32 }}>
              <div className="label" style={{ marginBottom: 10 }}>Contagem regressiva para a Copa</div>
              <div style={{ display: 'flex', gap: 10, flexWrap: 'wrap' }}>
                <CountdownTile value={days} label="dias" big isDark={isDark} />
                <CountdownTile value={hours} label="horas" isDark={isDark} />
                <CountdownTile value={minutes} label="min" isDark={isDark} />
                <CountdownTile value={seconds} label="seg" isDark={isDark} pulse />
              </div>
            </div>

            <div className="fade-up-delay-3 hero-actions" style={{
              marginTop: 36,
              display: 'flex',
              gap: 14,
              flexWrap: 'wrap'
            }}>
              <button className="btn btn-primary" onClick={onCTAClick}>
                {tweaks.ctaText || 'Quero a minha'} <Icon.ArrowRight size={18} />
              </button>
              <a href="#galeria" className="btn btn-ghost">Ver a camisa</a>
            </div>

          </div>

          {/* IMAGE side */}
          <div className="hero-image fade-up-delay-1" style={{
            position: 'relative',
            order: 1
          }}>
            <ProductFrame variant={variant} isDark={isDark} />
          </div>
        </div>
      </div>

      <style>{`
        @media (min-width: 900px) {
          .hero-grid {
            grid-template-columns: 1fr 1.15fr;
          }
          .hero-image { order: 1 !important; }
          .hero-text { order: 0 !important; }
        }
        @media (max-width: 899px) {
          .hero-grid { grid-template-columns: 1fr; }
          .hero-image { order: 0 !important; margin-bottom: 8px; }
          .hero-text  { order: 1 !important; }
          .hero-actions .btn { flex: 1; }
        }
      `}</style>
    </section>);

}

function CountdownTile({ value, label, big = false, pulse = false, isDark }) {
  const padded = String(value).padStart(2, '0');
  return (
    <div className="countdown-tile" style={{
      minWidth: big ? 92 : 72,
      padding: '14px 12px',
      borderRadius: 14,
      background: isDark ? 'rgba(0,0,0,0.35)' : 'rgba(50,50,49,0.92)',
      color: isDark ? 'var(--copa-yellow)' : '#fffff4',
      border: '1px solid ' + (isDark ? 'rgba(254,221,0,0.20)' : 'rgba(254,221,0,0.30)'),
      textAlign: 'center',
      fontFamily: MB.display,
      boxShadow: '0 8px 24px rgba(0,0,0,0.18), inset 0 1px 0 rgba(255,255,255,0.08)'
    }}>
      <div style={{
        fontSize: big ? 40 : 28,
        fontWeight: 800,
        lineHeight: 1,
        letterSpacing: '-0.02em',
        fontVariantNumeric: 'tabular-nums',
        animation: pulse ? 'pulseDot 1.6s ease-in-out infinite' : 'none'
      }}>{padded}</div>
      <div style={{
        fontSize: 10,
        fontWeight: 600,
        textTransform: 'uppercase',
        letterSpacing: '0.12em',
        marginTop: 6,
        opacity: 0.7
      }}>{label}</div>
    </div>);

}

function TrustItem({ icon, text, isDark }) {
  return (
    <div style={{
      display: 'flex',
      alignItems: 'center',
      gap: 8,
      fontFamily: MB.body,
      fontSize: 13,
      color: isDark ? 'rgba(255,255,244,0.7)' : MB.bgDark,
      fontWeight: 500
    }}>
      <span style={{ color: MB.copper }}>{icon}</span>
      <span>{text}</span>
    </div>);

}

function ProductFrame({ variant, isDark }) {
  // Local toggle: front / back
  const [side, setSide] = React.useState('frente'); // 'frente' | 'costas'
  const isFront = side === 'frente';

  // "produto" = clean studio panel with bigger frame, more breathing room
  // "celebrativo" = floating shirt with playful tags + giant green halo
  if (variant === 'produto') {
    return (
      <div style={{
        position: 'relative',
        margin: '0 auto',
        width: '100%',
        maxWidth: 620
      }}>
        <div style={{
          position: 'relative',
          aspectRatio: '3/4',
          borderRadius: 36,
          overflow: 'hidden',
          background: isDark ?
          'radial-gradient(120% 80% at 50% 20%, rgba(254,221,0,0.10) 0%, rgba(13,27,20,1) 70%)' :
          'radial-gradient(120% 80% at 50% 20%, rgba(254,221,0,0.55) 0%, rgba(255,248,198,1) 65%, rgba(255,255,244,1) 100%)',
          boxShadow: '0 40px 80px rgba(0,0,0,0.18), inset 0 1px 0 rgba(255,255,255,0.5)',
          padding: 'clamp(20px, 4vw, 40px)'
        }}>
          {/* Diagonal stripe pattern */}
          <div aria-hidden="true" style={{
            position: 'absolute', inset: 0, opacity: 0.08, pointerEvents: 'none',
            backgroundImage: 'repeating-linear-gradient(135deg, rgba(0,151,57,0.6) 0 2px, transparent 2px 24px)'
          }} />

          <ShirtFlipper
            isFront={isFront}
            frontSrc={R('frente_mock', 'assets/mockup-mindu-copa-frente.webp')}
            backSrc={R('costas_mock', 'assets/mockup-mindu-copa-costas.webp')} />
          

          {/* Side toggle */}
          <SideToggle side={side} onChange={setSide} isDark={isDark} />
        </div>

        <div style={{
          marginTop: 14,
          textAlign: 'center',
          fontFamily: MB.display,
          fontSize: 12, fontWeight: 600,
          letterSpacing: '0.12em',
          textTransform: 'uppercase',
          color: isDark ? 'rgba(255,255,244,0.55)' : MB.bgDark
        }}>
          Camisa oficial · {isFront ? 'Frente' : 'Costas'}
        </div>
      </div>);

  }

  // CELEBRATIVO — giant shirt with playful overlays
  return (
    <div style={{
      position: 'relative',
      margin: '0 auto',
      width: '100%',
      maxWidth: 640
    }}>
      <div style={{
        position: 'relative',
        aspectRatio: '3/4'
      }}>
        {/* Big green/yellow halo */}
        <div aria-hidden="true" style={{
          position: 'absolute',
          inset: '-8% -10% -4% -10%',
          background: isDark ?
          'radial-gradient(circle at 50% 45%, rgba(0,151,57,0.30) 0%, rgba(0,151,57,0) 60%), radial-gradient(circle at 50% 55%, rgba(254,221,0,0.18) 0%, rgba(254,221,0,0) 70%)' :
          'radial-gradient(circle at 50% 45%, rgba(254,221,0,0.60) 0%, rgba(254,221,0,0) 60%), radial-gradient(circle at 50% 80%, rgba(0,151,57,0.22) 0%, rgba(0,151,57,0) 70%)',
          filter: 'blur(30px)',
          zIndex: 0
        }} />

        {/* Confetti dots */}
        <ConfettiSparkles />

        {/* Shirt flipper */}
        <ShirtFlipper
          isFront={isFront}
          frontSrc={R('frente_mock', 'assets/mockup-mindu-copa-frente.webp')}
          backSrc={R('costas_mock', 'assets/mockup-mindu-copa-costas.webp')}
          tilt />
        

        {/* Floating "R$ 99" tag */}
        <div style={{
          position: 'absolute',
          top: '4%',
          right: '-2%',
          background: 'linear-gradient(135deg, var(--copa-green) 0%, var(--copa-green-deep) 100%)',
          color: '#fff',
          borderRadius: 20,
          padding: '16px 22px',
          boxShadow: '0 16px 40px rgba(0,151,57,0.45), inset 0 1px 0 rgba(255,255,255,0.25)',
          transform: 'rotate(8deg)',
          zIndex: 3,
          fontFamily: MB.display,
          textAlign: 'center',
          border: '2px solid rgba(255,255,255,0.15)'
        }}>
          <div style={{ fontSize: 10, fontWeight: 600, letterSpacing: '0.14em', opacity: 0.85 }}>POR APENAS</div>
          <div style={{ fontSize: 38, fontWeight: 900, lineHeight: 1, marginTop: 4, letterSpacing: '-0.02em' }}>R$ 99</div>
        </div>

        {/* Floating edition badge */}
        <div style={{
          position: 'absolute',
          bottom: '8%',
          left: '-4%',
          background: '#fffff4',
          color: MB.bgDarker,
          borderRadius: 16,
          padding: '12px 16px',
          boxShadow: '0 16px 40px rgba(50,50,49,0.30)',
          transform: 'rotate(-5deg)',
          zIndex: 3,
          fontFamily: MB.display,
          fontWeight: 700,
          fontSize: 14,
          display: 'flex',
          alignItems: 'center',
          gap: 8,
          border: '2px solid var(--copa-yellow)'
        }}>
          <Icon.Sparkles size={16} color={MB.copper} />
          Edição limitada!
        </div>

        {/* Side toggle */}
        <SideToggle side={side} onChange={setSide} isDark={isDark} celebrativo />
      </div>
    </div>);

}

// Shirt with 3D card-flip between front and back
function ShirtFlipper({ isFront, frontSrc, backSrc, tilt = false }) {
  return (
    <div style={{
      position: 'relative',
      width: '100%', height: '100%',
      zIndex: 2,
      perspective: '1600px',
      transform: tilt ? 'rotate(-2deg)' : 'none'
    }}>
      <div style={{
        position: 'relative',
        width: '100%', height: '100%',
        transformStyle: 'preserve-3d',
        transform: `rotateY(${isFront ? 0 : 180}deg)`,
        transition: `transform 800ms ${MB.ease}`
      }}>
        <img
          src={frontSrc}
          alt="Camisa MinduBier Copa 2026 — Frente"
          style={{
            position: 'absolute', inset: 0,
            width: '100%', height: '100%', objectFit: 'contain',
            backfaceVisibility: 'hidden',
            WebkitBackfaceVisibility: 'hidden',
            transform: 'rotateY(0deg)',
            filter: 'drop-shadow(0 30px 40px rgba(50,50,49,0.28))'
          }} />
        <img
          src={backSrc}
          alt="Camisa MinduBier Copa 2026 — Costas"
          style={{
            position: 'absolute', inset: 0,
            width: '100%', height: '100%', objectFit: 'contain',
            backfaceVisibility: 'hidden',
            WebkitBackfaceVisibility: 'hidden',
            transform: 'rotateY(180deg)',
            filter: 'drop-shadow(0 30px 40px rgba(50,50,49,0.28))'
          }} />
      </div>
    </div>);

}

function SideToggle({ side, onChange, isDark, celebrativo = false }) {
  const items = [
  { key: 'frente', label: 'Frente' },
  { key: 'costas', label: 'Costas' }];

  return (
    <div style={{
      position: 'absolute',
      bottom: celebrativo ? 8 : 16,
      right: celebrativo ? '50%' : 16,
      transform: celebrativo ? 'translateX(50%)' : 'none',
      display: 'inline-flex',
      padding: 4,
      borderRadius: 999,
      background: isDark ? 'rgba(0,0,0,0.65)' : 'rgba(50,50,49,0.92)',
      backdropFilter: 'blur(12px)',
      WebkitBackdropFilter: 'blur(12px)',
      border: '1px solid rgba(254,221,0,0.30)',
      boxShadow: '0 12px 28px rgba(0,0,0,0.30)',
      zIndex: 5
    }}>
      {items.map((it) =>
      <button
        key={it.key}
        type="button"
        onClick={() => onChange(it.key)}
        aria-pressed={side === it.key}
        style={{
          padding: '7px 14px',
          borderRadius: 999,
          border: 'none',
          cursor: 'pointer',
          fontFamily: MB.display,
          fontWeight: 700,
          fontSize: 11,
          letterSpacing: '0.10em',
          textTransform: 'uppercase',
          background: side === it.key ?
          'linear-gradient(135deg, var(--copa-yellow), #ffc800)' :
          'transparent',
          color: side === it.key ? MB.bgDarker : 'rgba(255,255,244,0.75)',
          transition: `all 250ms ${MB.ease}`
        }}>
        
          {it.label}
        </button>
      )}
    </div>);

}

function ConfettiSparkles() {
  const dots = React.useMemo(() => {
    const c = ['var(--copa-green)', 'var(--copa-yellow)', 'var(--brand-copper)', '#ffffff'];
    return Array.from({ length: 18 }).map((_, i) => ({
      id: i,
      top: Math.random() * 100,
      left: Math.random() * 100,
      size: 4 + Math.random() * 7,
      color: c[i % c.length],
      delay: Math.random() * 3
    }));
  }, []);
  return (
    <div aria-hidden="true" style={{
      position: 'absolute', inset: 0, pointerEvents: 'none', zIndex: 1
    }}>
      {dots.map((d) =>
      <span key={d.id} style={{
        position: 'absolute',
        top: d.top + '%', left: d.left + '%',
        width: d.size, height: d.size,
        borderRadius: '50%',
        background: d.color,
        opacity: 0.75,
        animation: `floatDot 4s ${d.delay}s ease-in-out infinite`
      }} />
      )}
      <style>{`
        @keyframes floatDot {
          0%, 100% { transform: translateY(0) scale(1); opacity: 0.7; }
          50%      { transform: translateY(-12px) scale(1.15); opacity: 1; }
        }
      `}</style>
    </div>);

}

Object.assign(window, { Hero, CountdownTile, ProductFrame, ShirtFlipper, SideToggle, ConfettiSparkles });