// Pau Fit — shared components
// Icons, tab bar, progress rings, pills

// ─────────────────────────────────────────────────────────────
// Icons (line style, hairline 1.6)
// ─────────────────────────────────────────────────────────────
const Icon = {
  home: (c = '#000', f = false) =>
  <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
      <path d="M3 11.5L12 4l9 7.5V20a1 1 0 0 1-1 1h-5v-6h-6v6H4a1 1 0 0 1-1-1v-8.5z"
    fill={f ? c : 'none'} stroke={c} strokeWidth="1.6" strokeLinejoin="round" />
    </svg>,

  target: (c = '#000', f = false) =>
  <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
      <circle cx="12" cy="12" r="9" stroke={c} strokeWidth="1.6" />
      <circle cx="12" cy="12" r="5.5" stroke={c} strokeWidth="1.6" />
      <circle cx="12" cy="12" r="2" fill={f ? c : 'none'} stroke={c} strokeWidth="1.6" />
    </svg>,

  fork: (c = '#000', f = false) =>
  <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
      <path d="M7 3v8a3 3 0 0 0 6 0V3M10 11v10M17 3c-1.5 1-2.5 3-2.5 6s1 5 2.5 5v7"
    stroke={c} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"
    fill={f ? c : 'none'} />
    </svg>,

  people: (c = '#000', f = false) =>
  <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
      <circle cx="9" cy="8" r="3.3" stroke={c} strokeWidth="1.6" fill={f ? c : 'none'} />
      <circle cx="17" cy="9.5" r="2.6" stroke={c} strokeWidth="1.6" fill={f ? c : 'none'} />
      <path d="M3 19c0-3 2.7-5 6-5s6 2 6 5M15 19c0-2.5 1.7-4 4-4s3 1 3 3"
    stroke={c} strokeWidth="1.6" strokeLinecap="round" fill="none" />
    </svg>,

  user: (c = '#000', f = false) =>
  <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
      <circle cx="12" cy="8" r="4" stroke={c} strokeWidth="1.6" fill={f ? c : 'none'} />
      <path d="M4 21c0-4 3.5-7 8-7s8 3 8 7" stroke={c} strokeWidth="1.6" strokeLinecap="round" fill="none" />
    </svg>,

  play: (c = '#fff') =>
  <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
      <path d="M3 2l9 5-9 5V2z" fill={c} />
    </svg>,

  back: (c = '#000') =>
  <svg width="22" height="22" viewBox="0 0 22 22" fill="none">
      <path d="M14 4l-7 7 7 7" stroke={c} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
    </svg>,

  close: (c = '#000') =>
  <svg width="22" height="22" viewBox="0 0 22 22" fill="none">
      <path d="M5 5l12 12M17 5L5 17" stroke={c} strokeWidth="2" strokeLinecap="round" />
    </svg>,

  heart: (c = '#000', f = false) =>
  <svg width="22" height="22" viewBox="0 0 22 22" fill="none">
      <path d="M11 19s-7-4.5-7-10.5A4 4 0 0 1 11 6a4 4 0 0 1 7 2.5C18 14.5 11 19 11 19z"
    stroke={c} strokeWidth="1.6" fill={f ? c : 'none'} strokeLinejoin="round" />
    </svg>,

  flame: (size = 22, c = '#D14C7A') =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M12 3c.5 1.5-.5 2.6-1 3.4-1 1.6-2 3-2 5.1 0 3.3 2.2 5.5 5 5.5 3.3 0 5-2.5 5-5.5 0-3-1.7-5.2-3.5-7-1-1-2-1.8-3.5-1.5z"
    fill={c} opacity="0.95" />
      <path d="M10 14.5c0-1.5 1-2.5 2-3.5.5 1.5 2 2 2 3.5 0 1.5-1 2.5-2 2.5s-2-1-2-2.5z"
    fill="#fff" opacity="0.6" />
    </svg>,

  check: (c = '#fff', size = 14) =>
  <svg width={size} height={size} viewBox="0 0 14 14" fill="none">
      <path d="M3 7.5l2.5 2.5L11 4.5" stroke={c} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
    </svg>,

  lock: (c = '#fff', size = 12) =>
  <svg width={size} height={size} viewBox="0 0 12 12" fill="none">
      <rect x="2" y="5" width="8" height="6" rx="1.2" fill={c} />
      <path d="M4 5V3.5a2 2 0 0 1 4 0V5" stroke={c} strokeWidth="1.4" fill="none" />
    </svg>,

  diamond: (size = 26, c = '#C9956B') =>
  <svg width={size} height={size} viewBox="0 0 26 26" fill="none">
      <path d="M5 9l4-5h8l4 5-8 13L5 9z" fill={c} />
      <path d="M9 4l4 5 4-5M5 9h16M9 9l4 13M17 9l-4 13" stroke="#fff" strokeWidth="0.8" strokeLinejoin="round" opacity="0.5" />
    </svg>,

  search: (c = '#000') =>
  <svg width="20" height="20" viewBox="0 0 20 20" fill="none">
      <circle cx="9" cy="9" r="6" stroke={c} strokeWidth="1.6" />
      <path d="M14 14l4 4" stroke={c} strokeWidth="1.6" strokeLinecap="round" />
    </svg>,

  sort: (c = '#000') =>
  <svg width="20" height="20" viewBox="0 0 20 20" fill="none">
      <path d="M4 6h12M6 10h8M8 14h4" stroke={c} strokeWidth="1.6" strokeLinecap="round" />
    </svg>,

  chevR: (c = '#000', size = 10) =>
  <svg width={size} height={size * 1.6} viewBox="0 0 10 16" fill="none">
      <path d="M2 2l6 6-6 6" stroke={c} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" fill="none" />
    </svg>,

  bell: (c = '#000') =>
  <svg width="22" height="22" viewBox="0 0 22 22" fill="none">
      <path d="M5 16h12l-1.5-2V10a4.5 4.5 0 0 0-9 0v4L5 16zM9 19a2 2 0 0 0 4 0" stroke={c} strokeWidth="1.6" strokeLinejoin="round" fill="none" />
    </svg>,

  share: (c = '#000') =>
  <svg width="22" height="22" viewBox="0 0 22 22" fill="none">
      <path d="M11 14V3M7 7l4-4 4 4M5 12v6a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2v-6" stroke={c} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" fill="none" />
    </svg>

};

// ─────────────────────────────────────────────────────────────
// Progress Ring (conic gradient)
// ─────────────────────────────────────────────────────────────
function ProgressRing({ value = 0.4, size = 64, stroke = 6, color = '#D4847A', track = 'rgba(0,0,0,0.06)', children }) {
  const r = (size - stroke) / 2;
  const c = 2 * Math.PI * r;
  const off = c * (1 - value);
  return (
    <div style={{ width: size, height: size, position: 'relative', display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}>
      <svg width={size} height={size} style={{ position: 'absolute', inset: 0, transform: 'rotate(-90deg)' }}>
        <circle cx={size / 2} cy={size / 2} r={r} fill="none" stroke={track} strokeWidth={stroke} />
        <circle cx={size / 2} cy={size / 2} r={r} fill="none" stroke={color} strokeWidth={stroke}
        strokeDasharray={c} strokeDashoffset={off} strokeLinecap="round"
        style={{ transition: 'stroke-dashoffset 600ms cubic-bezier(.2,.8,.2,1)' }} />
      </svg>
      <div style={{ position: 'relative', zIndex: 1 }}>{children}</div>
    </div>);

}

// ─────────────────────────────────────────────────────────────
// Pill (filter chip)
// ─────────────────────────────────────────────────────────────
function Pill({ active, onClick, children, dark = false, color = '#D4847A' }) {
  return (
    <button onClick={onClick} style={{
      border: 'none', cursor: 'pointer',
      padding: '8px 16px', borderRadius: 999,
      fontFamily: 'Poppins, system-ui', fontSize: 13, fontWeight: 500,
      background: active ? color : dark ? 'rgba(255,255,255,0.08)' : 'rgba(0,0,0,0.04)',
      color: active ? '#fff' : dark ? 'rgba(255,255,255,0.7)' : '#2D2A26',
      whiteSpace: 'nowrap',
      transition: 'all 200ms ease'
    }}>{children}</button>);

}

// ─────────────────────────────────────────────────────────────
// Tab Bar (5 tabs, blur glass)
// ─────────────────────────────────────────────────────────────
function TabBar({ active, onChange, dark = false, accent = '#D4847A' }) {
  const tabs = [
  { id: 'home', label: 'Inicio', icon: Icon.home },
  { id: 'retos', label: 'Retos', icon: Icon.target },
  { id: 'recetas', label: 'Comidas', icon: Icon.fork },
  { id: 'comunidad', label: 'Comunidad', icon: Icon.people },
  { id: 'perfil', label: 'Perfil', icon: Icon.user }];

  const inactive = dark ? 'rgba(255,255,255,0.42)' : '#9C928A';
  return (
    <div style={{
      position: 'absolute', bottom: 0, left: 0, right: 0, zIndex: 40,
      paddingBottom: 26, paddingTop: 8,
      background: dark ? 'rgba(20,18,16,0.78)' : 'rgba(250,243,238,0.82)',
      backdropFilter: 'blur(20px) saturate(180%)',
      WebkitBackdropFilter: 'blur(20px) saturate(180%)',
      borderTop: dark ? '0.5px solid rgba(255,255,255,0.08)' : '0.5px solid rgba(0,0,0,0.06)'
    }}>
      <div style={{ display: 'flex', justifyContent: 'space-around', padding: '0 4px' }}>
        {tabs.map((t) => {
          const isActive = active === t.id;
          const c = isActive ? accent : inactive;
          return (
            <button key={t.id} onClick={() => onChange(t.id)} style={{
              border: 'none', background: 'transparent', cursor: 'pointer',
              display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 3,
              padding: '6px 8px', flex: 1, color: c
            }}>
              {t.icon(c, isActive)}
              <span style={{
                fontFamily: 'Poppins, system-ui', fontSize: 10, fontWeight: isActive ? 600 : 500,
                letterSpacing: 0.2
              }}>{t.label}</span>
            </button>);

        })}
      </div>
    </div>);

}

// ─────────────────────────────────────────────────────────────
// Badge for reto cards (GRATIS / PRO)
// ─────────────────────────────────────────────────────────────
function RetoBadge({ tag, dark = false, isPremium = false }) {
  if (tag === 'GRATIS') {
    return (
      <span style={{
        fontFamily: 'Poppins, system-ui', fontSize: 9, fontWeight: 700,
        letterSpacing: 1.2, color: '#5C7A56',
        padding: '4px 8px', borderRadius: 6,
        background: 'rgba(168,197,160,0.28)'
      }}>GRATIS</span>);

  }
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 4,
      fontFamily: 'Poppins, system-ui', fontSize: 9, fontWeight: 700,
      letterSpacing: 1.2, color: '#F5E6C8',
      padding: '4px 8px', borderRadius: 6,
      background: '#2D2A26'
    }}>
      {!isPremium && <span style={{ fontSize: 7 }}>{Icon.lock('#F5E6C8', 8)}</span>}
      PRO
    </span>);

}

// ─────────────────────────────────────────────────────────────
// Streak Flame — wobble animation
// ─────────────────────────────────────────────────────────────
function StreakFlame({ count, color = '#D14C7A' }) {
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 6
    }}>
      <span style={{
        width: 28, height: 28, borderRadius: 999,
        background: `linear-gradient(135deg, ${color}25, ${color}10)`,
        display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
        fontSize: 14
      }}>🔥</span>
      <span style={{
        fontFamily: 'Poppins, system-ui', fontSize: 18, fontWeight: 700, color
      }}>{count}</span>
    </div>);

}

// ─────────────────────────────────────────────────────────────
// Confetti — emoji burst for completion
// ─────────────────────────────────────────────────────────────
function Confetti({ show }) {
  if (!show) return null;
  const items = ['✨', '💗', '🎉', '⭐️', '🌸', '💪🏻', '💖', '✨', '🌟', '💗'];
  return (
    <div style={{
      position: 'absolute', inset: 0, pointerEvents: 'none', zIndex: 100, overflow: 'hidden'
    }}>
      {items.map((e, i) =>
      <span key={i} style={{
        position: 'absolute', top: '40%', left: `${10 + i * 8}%`,
        fontSize: 22 + i % 3 * 6,
        animation: `confetti-fall ${1400 + i * 120}ms cubic-bezier(.2,.5,.4,1) forwards`,
        animationDelay: `${i * 40}ms`
      }}>{e}</span>
      )}
    </div>);

}

Object.assign(window, { Icon, ProgressRing, Pill, TabBar, RetoBadge, StreakFlame, Confetti, InsigniaCard });

// ─────────────────────────────────────────────────────────────
// Insignia — badge ganada al completar un reto
// Diseño: medalla circular con ring gradient, emoji centro,
// multiplicador (x2, x3...) en esquina si times > 1
// ─────────────────────────────────────────────────────────────
function InsigniaCard({ b, T, large = false }) {
  const size = large ? 120 : 88;
  return (
    <div style={{
      flexShrink: 0, width: size + 24, scrollSnapAlign: 'start',
      display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 8,
      padding: '10px 4px'
    }}>
      <div style={{ position: 'relative', width: size, height: size }}>
        {/* outer ring (gradient) */}
        <div style={{
          position: 'absolute', inset: 0, borderRadius: '50%',
          background: `conic-gradient(from -90deg, ${b.color}, ${b.bg}, ${b.color}, ${b.bg}, ${b.color})`,
          padding: 4, boxShadow: `0 8px 24px ${b.color}40`
        }}>
          <div style={{
            width: '100%', height: '100%', borderRadius: '50%',
            background: `radial-gradient(circle at 50% 35%, #fff 0%, ${b.bg} 55%, ${b.color}40 100%)`,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            position: 'relative'
          }}>
            {/* Inner emoji */}
            <span style={{ fontSize: size * 0.5, filter: 'drop-shadow(0 2px 6px rgba(45,42,38,0.18))' }}>{b.emoji}</span>
            {/* Sparkles */}
            <span style={{ position: 'absolute', top: '15%', right: '18%', fontSize: 10, opacity: 0.7 }}>✨</span>
            <span style={{ position: 'absolute', bottom: '20%', left: '18%', fontSize: 8, opacity: 0.6 }}>✨</span>
          </div>
        </div>
        {/* Multiplier */}
        {b.times > 1 &&
        <div style={{
          position: 'absolute', bottom: -2, right: -2,
          minWidth: 28, height: 28, borderRadius: 999, padding: '0 8px',
          background: '#2D2A26', color: '#F5E6C8',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          fontFamily: 'Poppins', fontSize: 11, fontWeight: 800, letterSpacing: -0.2,
          border: `2px solid ${T.bg || '#fff'}`,
          boxShadow: '0 2px 8px rgba(0,0,0,0.2)'
        }}>×{b.times}</div>
        }
      </div>
      <div style={{ textAlign: 'center' }}>
        <div style={{ fontFamily: 'Poppins', fontSize: 12, fontWeight: 600, color: T.text, letterSpacing: -0.1, lineHeight: 1.2 }}>{b.title}</div>
        <div style={{ fontFamily: 'Poppins', fontSize: 9, color: T.muted, fontWeight: 500, marginTop: 2 }}>{b.lastDate}</div>
      </div>
    </div>);

}