// Shared UI components
const { useState, useEffect, useRef, useMemo } = React;

const StatusBar = ({ light = false }) => (
  <div className="status-bar" style={{ color: light ? 'white' : 'var(--c-ink)' }}>
    <span>9:41</span>
    <span className="right">
      <svg width="18" height="12" viewBox="0 0 18 12" fill={light ? 'white' : 'currentColor'}>
        <rect x="0" y="6" width="3" height="6" rx="1"/>
        <rect x="5" y="3" width="3" height="9" rx="1"/>
        <rect x="10" y="0" width="3" height="12" rx="1"/>
      </svg>
      <svg width="16" height="12" viewBox="0 0 16 12" fill="none" stroke={light ? 'white' : 'currentColor'} strokeWidth="1.5">
        <path d="M1 5a10 10 0 0 1 14 0M3.5 7.5a6 6 0 0 1 9 0M6 10a2 2 0 0 1 4 0"/>
      </svg>
      <svg width="24" height="12" viewBox="0 0 24 12" fill="none" stroke={light ? 'white' : 'currentColor'} strokeWidth="1.2">
        <rect x="1" y="2" width="19" height="8" rx="2"/>
        <rect x="21" y="4" width="2" height="4" rx="1" fill={light ? 'white' : 'currentColor'}/>
        <rect x="3" y="4" width="13" height="4" rx="1" fill={light ? 'white' : 'currentColor'}/>
      </svg>
    </span>
  </div>
);

const Logo = ({ size = 44 }) => (
  <div style={{ width: size, height: size, borderRadius: size * 0.28, background: 'linear-gradient(135deg, var(--c-primary), var(--c-primary-deep))', display: 'grid', placeItems: 'center', boxShadow: '0 6px 16px rgba(27,154,95,0.3)', position: 'relative', overflow: 'hidden' }}>
    <svg viewBox="0 0 24 24" width={size * 0.55} height={size * 0.55} fill="none" stroke="white" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M5 19c0-7 5-13 16-14-1 9-6 14-13 14"/>
      <path d="M5 19c2-4 4-6 8-8"/>
    </svg>
    <div style={{ position: 'absolute', top: '8%', left: '12%', right: '40%', bottom: '55%', background: 'radial-gradient(circle, rgba(255,255,255,0.4), transparent 70%)', borderRadius: '50%' }}/>
  </div>
);

const Brand = ({ size = 18, color = 'var(--c-ink)' }) => (
  <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
    <Logo size={size * 1.4}/>
    <div style={{ fontSize: size, fontWeight: 900, letterSpacing: '-0.03em', color }}>
      Precio<span style={{ color: 'var(--c-primary)' }}>Vivo</span>
    </div>
  </div>
);

const Confetti = () => {
  const colors = ['#1b9a5f', '#ff6b3d', '#ffc845', '#d4385e', '#6fd6b2', '#142340'];
  const bits = Array.from({ length: 36 }, (_, i) => ({
    left: Math.random() * 100,
    color: colors[i % colors.length],
    delay: Math.random() * 0.6,
    duration: 1.2 + Math.random() * 1.2,
    rotate: Math.random() * 360
  }));
  return (
    <div className="confetti-layer">
      {bits.map((b, i) => (
        <span key={i} className="confetti-bit" style={{
          left: `${b.left}%`,
          background: b.color,
          animationDelay: `${b.delay}s`,
          animationDuration: `${b.duration}s`,
          transform: `rotate(${b.rotate}deg)`,
        }}/>
      ))}
    </div>
  );
};

const ScreenHeader = ({ onBack, title, right }) => (
  <div className="row between" style={{ padding: '6px 16px 14px' }}>
    <div className="row gap-2" style={{ alignItems: 'center' }}>
      {onBack && (
        <button className="btn-ghost" onClick={onBack} style={{ width: 42, height: 42, borderRadius: 21, padding: 0, display: 'grid', placeItems: 'center', border: '1.5px solid var(--c-line)', background: 'white', cursor: 'pointer' }}>
          <Icon name="arrow-left" size={20}/>
        </button>
      )}
      {title && <div style={{ fontSize: 17, fontWeight: 800, marginLeft: 6 }}>{title}</div>}
    </div>
    {right}
  </div>
);

const ProductImg = ({ kind = 'green', label, size = 'md' }) => {
  const dim = size === 'lg' ? { width: '100%', height: 130 } : size === 'sm' ? { width: 54, height: 54 } : { width: 72, height: 72 };
  // Pseudo product shape per kind
  const shape = ({
    green: <path d="M50 18c-12 0-22 12-22 26 0 16 12 28 22 28s22-12 22-28c0-14-10-26-22-26z" />,  // avocado-ish
    navy: <rect x="32" y="22" width="36" height="56" rx="6"/>, // carton
    orange: <path d="M28 32c0-8 8-12 22-12s22 4 22 12v40c0 4-4 8-8 8H36c-4 0-8-4-8-8z"/>, // bottle/can
    yellow: <ellipse cx="50" cy="50" rx="22" ry="28"/>,
    berry: <path d="M28 26h44l-4 50c0 4-4 8-8 8H40c-4 0-8-4-8-8z"/>,
    mint: <circle cx="50" cy="50" r="26"/>,
  })[kind] || <circle cx="50" cy="50" r="26"/>;
  return (
    <div className={`product-img ${kind}`} style={dim}>
      <svg viewBox="0 0 100 100" width="60%" height="60%" style={{ opacity: 0.55 }} fill="currentColor">{shape}</svg>
      {label && <div style={{ position: 'absolute', bottom: 4, left: 6, right: 6, fontSize: 8, textAlign: 'center', opacity: 0.6, textTransform: 'uppercase', letterSpacing: '0.08em' }}>{label}</div>}
    </div>
  );
};

const Money = ({ value, struck = false }) => (
  <span className={struck ? 'struck' : 'money'}>${value.toFixed(2)}</span>
);

const StoreDot = ({ color, short, size = 28 }) => (
  <div style={{ width: size, height: size, borderRadius: size/2, background: color, display: 'grid', placeItems: 'center', color: 'white', fontSize: size * 0.36, fontWeight: 800, letterSpacing: '-0.02em', flexShrink: 0 }}>{short}</div>
);

Object.assign(window, { StatusBar, Logo, Brand, Confetti, ScreenHeader, ProductImg, Money, StoreDot });
