/* global React */
const { useState, useEffect, useRef, useMemo } = React;

// ============ Icons (Lucide-style strokes) ============
const Icon = ({ name, size = 20, stroke = 2, ...p }) => {
  const s = { width: size, height: size, fill: 'none', stroke: 'currentColor', strokeWidth: stroke, strokeLinecap: 'round', strokeLinejoin: 'round' };
  const d = {
    arrowRight: <><path d="M5 12h14"/><path d="m12 5 7 7-7 7"/></>,
    arrowLeft: <><path d="M19 12H5"/><path d="m12 19-7-7 7-7"/></>,
    truck: <><path d="M14 18V6a2 2 0 0 0-2-2H3a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h1"/><path d="M15 18h5a1 1 0 0 0 1-1v-3.34a1 1 0 0 0-.3-.7L17.7 10H14"/><circle cx="7" cy="18" r="2"/><circle cx="17" cy="18" r="2"/></>,
    shield: <><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/><path d="m9 12 2 2 4-4"/></>,
    lock: <><rect width="18" height="11" x="3" y="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></>,
    gift: <><rect x="3" y="8" width="18" height="4" rx="1"/><path d="M12 8v13"/><path d="M19 12v7a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2v-7"/><path d="M7.5 8a2.5 2.5 0 0 1 0-5C11 3 12 8 12 8"/><path d="M16.5 8a2.5 2.5 0 0 0 0-5C13 3 12 8 12 8"/></>,
    heart: <><path d="M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.29 1.51 4.04 3 5.5l7 7z"/></>,
    trash: <><path d="M3 6h18"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><path d="m19 6-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/></>,
    plus: <><path d="M5 12h14"/><path d="M12 5v14"/></>,
    minus: <><path d="M5 12h14"/></>,
    check: <><path d="M20 6 9 17l-5-5"/></>,
    checkCircle: <><circle cx="12" cy="12" r="10"/><path d="m9 12 2 2 4-4"/></>,
    x: <><path d="M18 6 6 18"/><path d="m6 6 12 12"/></>,
    tag: <><path d="M12.586 2.586A2 2 0 0 0 11.172 2H4a2 2 0 0 0-2 2v7.172a2 2 0 0 0 .586 1.414l8.704 8.704a2.426 2.426 0 0 0 3.42 0l6.58-6.58a2.426 2.426 0 0 0 0-3.42z"/><circle cx="7.5" cy="7.5" r="1.5"/></>,
    note: <><path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><path d="M9 3a1 1 0 0 0-1 1v2a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4a1 1 0 0 0-1-1z"/><path d="M9 14h6"/><path d="M9 10h2"/></>,
    clock: <><circle cx="12" cy="12" r="10"/><path d="M12 6v6l4 2"/></>,
    package: <><path d="m7.5 4.27 9 5.15"/><path d="M21 8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/><path d="M3.3 7 12 12l8.7-5"/><path d="M12 22V12"/></>,
    rotate: <><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/></>,
    cart: <><circle cx="8" cy="21" r="1"/><circle cx="19" cy="21" r="1"/><path d="M2.05 2.05h2l2.66 12.42a2 2 0 0 0 2 1.58h9.78a2 2 0 0 0 1.95-1.57l1.65-7.43H5.12"/></>,
    chev: <><path d="m9 18 6-6-6-6"/></>,
    flask: <><path d="M9 2v7.31"/><path d="M15 2v7.31"/><path d="M8 14h8"/><path d="M9 2h6"/><path d="M6.25 18.5A2.5 2.5 0 0 0 8.63 22h6.74a2.5 2.5 0 0 0 2.38-3.5L15 9.31V2H9v7.31z"/></>,
    sparkles: <><path d="m12 3-1.9 5.8a2 2 0 0 1-1.3 1.3L3 12l5.8 1.9a2 2 0 0 1 1.3 1.3L12 21l1.9-5.8a2 2 0 0 1 1.3-1.3L21 12l-5.8-1.9a2 2 0 0 1-1.3-1.3z"/></>,
    user: <><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></>,
    search: <><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></>,
    phone: <><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"/></>,
    // v0.7.5 A4: brand glyphs for footer social. Real namaximum.sk
    // wires only Facebook, Instagram, YouTube via socialsLinks - no
    // TikTok. We render them as filled brand marks (fill='currentColor')
    // so the existing .p-foot-soc / .pdp-d-ft-soc treatments (white pill
    // on dark footer) keep working.
    facebook: <><path fill="currentColor" stroke="none" d="M22 12a10 10 0 1 0-11.56 9.88v-6.99H7.9V12h2.54V9.79c0-2.5 1.49-3.89 3.77-3.89 1.09 0 2.24.2 2.24.2v2.46h-1.26c-1.24 0-1.63.77-1.63 1.56V12h2.78l-.44 2.89h-2.34v6.99A10 10 0 0 0 22 12z"/></>,
    instagram: <><rect x="2.5" y="2.5" width="19" height="19" rx="5"/><circle cx="12" cy="12" r="4"/><circle cx="17.5" cy="6.5" r="1" fill="currentColor" stroke="none"/></>,
    youtube: <><path fill="currentColor" stroke="none" d="M21.58 7.19a2.51 2.51 0 0 0-1.77-1.77C18.25 5 12 5 12 5s-6.25 0-7.81.42a2.51 2.51 0 0 0-1.77 1.77A26.4 26.4 0 0 0 2 12a26.4 26.4 0 0 0 .42 4.81 2.51 2.51 0 0 0 1.77 1.77C5.75 19 12 19 12 19s6.25 0 7.81-.42a2.51 2.51 0 0 0 1.77-1.77A26.4 26.4 0 0 0 22 12a26.4 26.4 0 0 0-.42-4.81zM10 15.02V8.98l5.2 3.02z"/></>
  };
  return <svg viewBox="0 0 24 24" style={s} {...p}>{d[name]}</svg>;
};

window.Icon = Icon;

// ============ Product visuals (brandless supplement containers) ============
// kind: 'tub-lg' | 'tub' | 'bottle' | 'sachet' | 'shaker' | 'bar'
const ProductVisual = ({ kind = 'tub', color = '#0077BC', label = '', sub = '' }) => {
  const W = 90, H = 110;
  const common = { width: '100%', height: '100%', viewBox: `0 0 ${W} ${H}`, preserveAspectRatio: 'xMidYMid meet' };

  if (kind === 'bottle' || kind === 'bottle-sm') {
    // Pill bottle — shorter, narrower
    return (
      <svg {...common}>
        <defs>
          <linearGradient id={`b-body-${color}`} x1="0" x2="1">
            <stop offset="0" stopColor="#FAFBFC"/>
            <stop offset=".5" stopColor="#fff"/>
            <stop offset="1" stopColor="#E8ECF2"/>
          </linearGradient>
        </defs>
        {/* cap */}
        <rect x="32" y="20" width="26" height="10" rx="1.5" fill="#2A2E35"/>
        <rect x="32" y="20" width="26" height="3" fill="#3C4149"/>
        {/* neck */}
        <rect x="36" y="30" width="18" height="4" fill="#D7DBE2"/>
        {/* body */}
        <rect x="26" y="34" width="38" height="62" rx="4" fill={`url(#b-body-${color})`} stroke="#D7DBE2" strokeWidth=".5"/>
        {/* label */}
        <rect x="26" y="50" width="38" height="32" fill="#fff"/>
        <rect x="26" y="50" width="38" height="4" fill={color}/>
        <rect x="26" y="78" width="38" height="2" fill={color} opacity=".4"/>
        <text x="45" y="67" textAnchor="middle" fontFamily="system-ui, sans-serif" fontWeight="800" fontSize="7" fill={color} letterSpacing=".3">{label}</text>
        <text x="45" y="75" textAnchor="middle" fontFamily="system-ui, sans-serif" fontWeight="600" fontSize="4.5" fill="#6B7280" letterSpacing=".3">{sub}</text>
      </svg>
    );
  }

  if (kind === 'sachet') {
    return (
      <svg {...common}>
        <defs>
          <linearGradient id={`s-body-${color}`} x1="0" x2="1">
            <stop offset="0" stopColor="#F5F7FA"/>
            <stop offset=".5" stopColor="#fff"/>
            <stop offset="1" stopColor="#E6EAF0"/>
          </linearGradient>
        </defs>
        {/* notch top */}
        <path d="M24 28 L66 28 L64 24 L62 26 L60 24 L58 26 L56 24 L54 26 L52 24 L50 26 L48 24 L46 26 L44 24 L42 26 L40 24 L38 26 L36 24 L34 26 L32 24 L30 26 L28 24 L26 26 Z" fill="#D7DBE2"/>
        <rect x="24" y="28" width="42" height="60" rx="2" fill={`url(#s-body-${color})`} stroke="#D7DBE2" strokeWidth=".5"/>
        <rect x="24" y="38" width="42" height="14" fill={color}/>
        <text x="45" y="48" textAnchor="middle" fontFamily="system-ui, sans-serif" fontWeight="800" fontSize="7" fill="#fff" letterSpacing=".4">{label}</text>
        <rect x="28" y="58" width="34" height="1" fill={color} opacity=".3"/>
        <rect x="28" y="64" width="22" height="1" fill={color} opacity=".3"/>
        <text x="45" y="80" textAnchor="middle" fontFamily="system-ui, sans-serif" fontWeight="600" fontSize="4" fill="#6B7280" letterSpacing=".3">{sub}</text>
      </svg>
    );
  }

  if (kind === 'shaker') {
    return (
      <svg {...common}>
        <defs>
          <linearGradient id={`sh-body-${color}`} x1="0" x2="1">
            <stop offset="0" stopColor="#E8ECF2"/>
            <stop offset=".4" stopColor="#F7F9FC"/>
            <stop offset="1" stopColor="#D7DBE2"/>
          </linearGradient>
        </defs>
        <path d="M30 16 Q30 12 34 12 L56 12 Q60 12 60 16 L60 22 L30 22 Z" fill="#2A2E35"/>
        <rect x="28" y="22" width="34" height="4" fill="#3C4149"/>
        <rect x="26" y="26" width="38" height="70" rx="5" fill={`url(#sh-body-${color})`} stroke="#C9CFD8" strokeWidth=".5"/>
        <rect x="26" y="42" width="38" height="22" fill="#fff" opacity=".7"/>
        <text x="45" y="55" textAnchor="middle" fontFamily="system-ui, sans-serif" fontWeight="800" fontSize="7" fill={color} letterSpacing=".4">{label}</text>
        {/* measure marks */}
        <g stroke="#6B7280" strokeWidth=".5" opacity=".5">
          <line x1="58" y1="72" x2="62" y2="72"/>
          <line x1="58" y1="80" x2="62" y2="80"/>
          <line x1="58" y1="88" x2="62" y2="88"/>
        </g>
      </svg>
    );
  }

  if (kind === 'bar') {
    return (
      <svg {...common}>
        <rect x="14" y="42" width="62" height="26" rx="3" fill="#2A2E35"/>
        <rect x="14" y="48" width="62" height="10" fill={color}/>
        <text x="45" y="56" textAnchor="middle" fontFamily="system-ui, sans-serif" fontWeight="800" fontSize="8" fill="#fff" letterSpacing=".5">{label}</text>
        <text x="45" y="64" textAnchor="middle" fontFamily="system-ui, sans-serif" fontWeight="600" fontSize="4" fill="#D7DBE2" letterSpacing=".3">{sub}</text>
        {/* wrapper crimps */}
        <path d="M14 42 L10 38 L10 72 L14 68 Z" fill="#1B1E23"/>
        <path d="M76 42 L80 38 L80 72 L76 68 Z" fill="#1B1E23"/>
      </svg>
    );
  }

  // default — tub (powder container). 'tub-lg' is slightly taller
  const tall = kind === 'tub-lg';
  const bodyTop = tall ? 22 : 28;
  return (
    <svg {...common}>
      <defs>
        <linearGradient id={`t-body-${color}`} x1="0" x2="1">
          <stop offset="0" stopColor="#F5F7FA"/>
          <stop offset=".5" stopColor="#fff"/>
          <stop offset="1" stopColor="#E1E5EB"/>
        </linearGradient>
      </defs>
      {/* lid */}
      <rect x="18" y={bodyTop - 10} width="54" height="10" rx="1.5" fill="#2A2E35"/>
      <rect x="18" y={bodyTop - 10} width="54" height="3" fill="#3C4149"/>
      {/* body */}
      <rect x="20" y={bodyTop} width="50" height={tall ? 80 : 70} rx="3" fill={`url(#t-body-${color})`} stroke="#D7DBE2" strokeWidth=".5"/>
      {/* colored label band */}
      <rect x="20" y={bodyTop + 14} width="50" height="26" fill="#fff"/>
      <rect x="20" y={bodyTop + 14} width="50" height="4" fill={color}/>
      <rect x="20" y={bodyTop + 36} width="50" height="2" fill={color} opacity=".4"/>
      <text x="45" y={bodyTop + 28} textAnchor="middle" fontFamily="system-ui, sans-serif" fontWeight="800" fontSize="8" fill={color} letterSpacing=".3">{label}</text>
      <text x="45" y={bodyTop + 34} textAnchor="middle" fontFamily="system-ui, sans-serif" fontWeight="600" fontSize="4.5" fill="#6B7280" letterSpacing=".3">{sub}</text>
    </svg>
  );
};

window.ProductVisual = ProductVisual;
