/* components.jsx — primitivos no estilo shadcn/ui, customizados para o tema AchaMais */
const { useState, useRef, useEffect } = React;
const cn = (...a) => a.filter(Boolean).join(' ');

/* ---------------- Ícones (line icons simples) ---------------- */
const Svg = (p) => (
  <svg xmlns="http://www.w3.org/2000/svg" width={p.size || 20} height={p.size || 20}
    viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={p.sw || 2}
    strokeLinecap="round" strokeLinejoin="round" className={p.className}>{p.children}</svg>
);
const IconSearch = (p) => <Svg {...p}><circle cx="11" cy="11" r="7" /><line x1="21" y1="21" x2="16.65" y2="16.65" /></Svg>;
const IconExternal = (p) => <Svg {...p}><path d="M15 3h6v6" /><path d="M10 14 21 3" /><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" /></Svg>;
const IconStar = (p) => <Svg {...p} fill={p.fill || 'none'}><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 12 2" /></Svg>;
const IconX = (p) => <Svg {...p}><line x1="18" y1="6" x2="6" y2="18" /><line x1="6" y1="6" x2="18" y2="18" /></Svg>;
const IconChevron = (p) => <Svg {...p}><polyline points="6 9 12 15 18 9" /></Svg>;
const IconCheck = (p) => <Svg {...p}><polyline points="20 6 9 17 4 12" /></Svg>;
const IconAlert = (p) => <Svg {...p}><path d="M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z" /><line x1="12" y1="9" x2="12" y2="13" /><line x1="12" y1="17" x2="12.01" y2="17" /></Svg>;
const IconBox = (p) => <Svg {...p}><path d="M21 16V8a2 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" /><polyline points="3.27 6.96 12 12.01 20.73 6.96" /><line x1="12" y1="22.08" x2="12" y2="12" /></Svg>;
const IconGrid = (p) => <Svg {...p}><rect x="3" y="3" width="7" height="7" rx="1.5" /><rect x="14" y="3" width="7" height="7" rx="1.5" /><rect x="3" y="14" width="7" height="7" rx="1.5" /><rect x="14" y="14" width="7" height="7" rx="1.5" /></Svg>;
const IconList = (p) => <Svg {...p}><line x1="8" y1="6" x2="21" y2="6" /><line x1="8" y1="12" x2="21" y2="12" /><line x1="8" y1="18" x2="21" y2="18" /><circle cx="3.5" cy="6" r="1" fill="currentColor" stroke="none" /><circle cx="3.5" cy="12" r="1" fill="currentColor" stroke="none" /><circle cx="3.5" cy="18" r="1" fill="currentColor" stroke="none" /></Svg>;
const IconLogout = (p) => <Svg {...p}><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4" /><polyline points="16 17 21 12 16 7" /><line x1="21" y1="12" x2="9" y2="12" /></Svg>;
const IconMail = (p) => <Svg {...p}><rect x="2" y="4" width="20" height="16" rx="2" /><path d="m22 7-10 6L2 7" /></Svg>;
const IconLock = (p) => <Svg {...p}><rect x="3" y="11" width="18" height="11" rx="2" /><path d="M7 11V7a5 5 0 0 1 10 0v4" /></Svg>;
const IconUser = (p) => <Svg {...p}><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2" /><circle cx="12" cy="7" r="4" /></Svg>;
const IconChart = (p) => <Svg {...p}><line x1="3" y1="21" x2="21" y2="21" /><rect x="5" y="11" width="3.4" height="7" rx="1" /><rect x="10.3" y="6" width="3.4" height="12" rx="1" /><rect x="15.6" y="9" width="3.4" height="9" rx="1" /></Svg>;
const IconTrendUp = (p) => <Svg {...p}><polyline points="3 17 9 11 13 15 21 7" /><polyline points="15 7 21 7 21 13" /></Svg>;
const IconLink = (p) => <Svg {...p}><path d="M10 13a5 5 0 0 0 7.07 0l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71" /><path d="M14 11a5 5 0 0 0-7.07 0l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71" /></Svg>;
const IconWallet = (p) => <Svg {...p}><path d="M21 12V7H5a2 2 0 0 1 0-4h14v4" /><path d="M3 5v14a2 2 0 0 0 2 2h16v-5" /><path d="M18 12a2 2 0 0 0 0 4h3v-4z" /></Svg>;
const IconBolt = (p) => <Svg {...p}><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2" /></Svg>;
const IconCart = (p) => <Svg {...p}><circle cx="9" cy="21" r="1" /><circle cx="20" cy="21" r="1" /><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6" /></Svg>;
const IconClock = (p) => <Svg {...p}><circle cx="12" cy="12" r="9" /><polyline points="12 7 12 12 15 14" /></Svg>;
const IconShield = (p) => <Svg {...p}><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z" /></Svg>;
const IconArrowRight = (p) => <Svg {...p}><line x1="5" y1="12" x2="19" y2="12" /><polyline points="12 5 19 12 12 19" /></Svg>;
const IconLifebuoy = (p) => <Svg {...p}><circle cx="12" cy="12" r="9.5" /><circle cx="12" cy="12" r="3.8" /><line x1="5.6" y1="5.6" x2="9.3" y2="9.3" /><line x1="14.7" y1="14.7" x2="18.4" y2="18.4" /><line x1="14.7" y1="9.3" x2="18.4" y2="5.6" /><line x1="9.3" y1="14.7" x2="5.6" y2="18.4" /></Svg>;
const IconBell = (p) => <Svg {...p}><path d="M18 8a6 6 0 0 0-12 0c0 7-3 9-3 9h18s-3-2-3-9" /><path d="M13.73 21a2 2 0 0 1-3.46 0" /></Svg>;
const IconCreditCard = (p) => <Svg {...p}><rect x="2" y="5" width="20" height="14" rx="2" /><line x1="2" y1="10" x2="22" y2="10" /></Svg>;
const IconTrash = (p) => <Svg {...p}><polyline points="3 6 5 6 21 6" /><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" /></Svg>;
const IconSliders = (p) => <Svg {...p}><line x1="4" y1="21" x2="4" y2="14" /><line x1="4" y1="10" x2="4" y2="3" /><line x1="12" y1="21" x2="12" y2="12" /><line x1="12" y1="8" x2="12" y2="3" /><line x1="20" y1="21" x2="20" y2="16" /><line x1="20" y1="12" x2="20" y2="3" /><line x1="1" y1="14" x2="7" y2="14" /><line x1="9" y1="8" x2="15" y2="8" /><line x1="17" y1="16" x2="23" y2="16" /></Svg>;
const IconCamera = (p) => <Svg {...p}><path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z" /><circle cx="12" cy="13" r="4" /></Svg>;
const IconDownload = (p) => <Svg {...p}><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" /><polyline points="7 10 12 15 17 10" /><line x1="12" y1="15" x2="12" y2="3" /></Svg>;
const IconBookmark = (p) => <Svg {...p} fill={p.fill || 'none'}><path d="M19 21l-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z" /></Svg>;
const IconTrendDown = (p) => <Svg {...p}><polyline points="23 18 13.5 8.5 8.5 13.5 1 6" /><polyline points="17 18 23 18 23 12" /></Svg>;
const IconFlame = (p) => <Svg {...p} fill={p.fill || 'none'}><path d="M12 2c1 4 5 5 5 9a5 5 0 0 1-10 0c0-1.5.5-2.5 1-3 .2 1.2 1 2 2 2 .5-2.5-.5-5 2-9z" /></Svg>;
const IconBellPlus = (p) => <Svg {...p}><path d="M18.5 8a6.5 6.5 0 0 0-12.9-1" /><path d="M4 8c0 7-3 9-3 9h18s-1.2-.8-2-3" /><path d="M13.73 21a2 2 0 0 1-3.46 0" /><line x1="18" y1="2" x2="18" y2="8" /><line x1="15" y1="5" x2="21" y2="5" /></Svg>;
const IconTarget = (p) => <Svg {...p}><circle cx="12" cy="12" r="9" /><circle cx="12" cy="12" r="5" /><circle cx="12" cy="12" r="1" fill="currentColor" /></Svg>;
const IconCopy = (p) => <Svg {...p}><rect x="9" y="9" width="13" height="13" rx="2" /><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" /></Svg>;
const IconCheckCircle = (p) => <Svg {...p}><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14" /><polyline points="22 4 12 14.01 9 11.01" /></Svg>;
const IconPhone = (p) => <Svg {...p}><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.72c.13.96.36 1.9.7 2.81a2 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-.45c.91.34 1.85.57 2.81.7A2 2 0 0 1 22 16.92z" /></Svg>;
const IconMapPin = (p) => <Svg {...p}><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z" /><circle cx="12" cy="10" r="3" /></Svg>;
const IconFire = (p) => <Svg {...p} fill={p.fill || 'none'}><path d="M12 2c1 4 5 5 5 9a5 5 0 0 1-10 0c0-1.5.5-2.5 1-3 .2 1.2 1 2 2 2 .5-2.5-.5-5 2-9z" /></Svg>;
const IconFireGradient = (p) => {
  const s = p.size || 20;
  const gid = React.useMemo(() => 'fireg-' + Math.random().toString(36).slice(2, 9), []);
  return (
    <svg xmlns="http://www.w3.org/2000/svg" width={s} height={s} viewBox="0 0 24 24" className={p.className}>
      <defs>
        <linearGradient id={gid} x1="3" y1="2" x2="21" y2="22" gradientUnits="userSpaceOnUse">
          <stop offset="0" stopColor="#16BFA3" />
          <stop offset="1" stopColor="#F95E2E" />
        </linearGradient>
      </defs>
      <path d="M12 2c1 4 5 5 5 9a5 5 0 0 1-10 0c0-1.5.5-2.5 1-3 .2 1.2 1 2 2 2 .5-2.5-.5-5 2-9z" fill={`url(#${gid})`} stroke={`url(#${gid})`} strokeWidth="1.5" strokeLinejoin="round" />
    </svg>
  );
};
function IconGoogle(p) {
  const s = p.size || 18;
  return (
    <svg xmlns="http://www.w3.org/2000/svg" width={s} height={s} viewBox="0 0 48 48" className={p.className}>
      <path fill="#FFC107" d="M43.611 20.083H42V20H24v8h11.303c-1.649 4.657-6.08 8-11.303 8-6.627 0-12-5.373-12-12s5.373-12 12-12c3.059 0 5.842 1.154 7.961 3.039l5.657-5.657C34.046 6.053 29.268 4 24 4 12.955 4 4 12.955 4 24s8.955 20 20 20 20-8.955 20-20c0-1.341-.138-2.65-.389-3.917z" />
      <path fill="#FF3D00" d="m6.306 14.691 6.571 4.819C14.655 15.108 18.961 12 24 12c3.059 0 5.842 1.154 7.961 3.039l5.657-5.657C34.046 6.053 29.268 4 24 4 16.318 4 9.656 8.337 6.306 14.691z" />
      <path fill="#4CAF50" d="M24 44c5.166 0 9.86-1.977 13.409-5.192l-6.19-5.238C29.211 35.091 26.715 36 24 36c-5.202 0-9.619-3.317-11.283-7.946l-6.522 5.025C9.505 39.556 16.227 44 24 44z" />
      <path fill="#1976D2" d="M43.611 20.083H42V20H24v8h11.303a12.04 12.04 0 0 1-4.087 5.571l.003-.002 6.19 5.238C36.971 39.205 44 34 44 24c0-1.341-.138-2.65-.389-3.917z" />
    </svg>
  );
}
function IconGear(p) {
  const teeth = Array.from({ length: 8 }, (_, i) => (
    <rect key={i} x="11" y="1.6" width="2" height="3.6" rx="1" transform={`rotate(${i * 45} 12 12)`} />
  ));
  return (
    <svg xmlns="http://www.w3.org/2000/svg" width={p.size || 20} height={p.size || 20} viewBox="0 0 24 24" className={p.className}>
      <g fill="currentColor">{teeth}</g>
      <circle cx="12" cy="12" r="6.2" fill="none" stroke="currentColor" strokeWidth="2" />
      <circle cx="12" cy="12" r="2.4" fill="none" stroke="currentColor" strokeWidth="2" />
    </svg>
  );
}

/* ---------------- Button ---------------- */
function Button({ variant = 'default', size = 'md', className, children, ...rest }) {
  const base = 'inline-flex items-center justify-center gap-2 font-medium rounded-xl transition-all duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-offset-ink disabled:opacity-50 disabled:pointer-events-none whitespace-nowrap';
  const variants = {
    default: 'bg-brand text-ink hover:bg-brand/90 focus-visible:ring-brand/60 shadow-[0_6px_24px_-8px_rgba(22,191,163,0.5)]',
    ml: 'bg-gold text-ink hover:bg-gold/90 focus-visible:ring-gold/60 shadow-[0_6px_24px_-8px_rgba(255,208,0,0.55)]',
    coral: 'bg-coral text-white hover:bg-coral/90 focus-visible:ring-coral/60 shadow-[0_6px_24px_-8px_rgba(249,94,46,0.55)]',
    secondary: 'bg-white/[0.06] text-zinc-100 hover:bg-white/[0.1] border border-edge focus-visible:ring-white/20',
    outline: 'border border-edge text-zinc-300 hover:bg-white/[0.05] hover:text-white focus-visible:ring-white/20',
    ghost: 'text-zinc-300 hover:bg-white/[0.06] hover:text-white focus-visible:ring-white/20',
  };
  const sizes = { sm: 'h-9 px-3 text-sm', md: 'h-11 px-5 text-sm', lg: 'h-14 px-7 text-base', icon: 'h-11 w-11' };
  return <button className={cn(base, variants[variant], sizes[size], className)} {...rest}>{children}</button>;
}

/* ---------------- Input ---------------- */
function Input({ className, ...rest }) {
  return (
    <input
      className={cn(
        'h-11 w-full rounded-xl bg-white/[0.04] border border-edge px-4 text-sm text-zinc-100 placeholder:text-zinc-500',
        'transition-all duration-200 focus:outline-none focus:border-brand/60 focus:ring-2 focus:ring-brand/25',
        className
      )}
      {...rest}
    />
  );
}

/* ---------------- Comissão (fonte limpa, um único R$) ---------------- */
function Commission({ value, className }) {
  const num = (window.formatBRL(value) || '').replace(/R\$\s*/u, '').trim();
  return (
    <span className={cn('font-num font-extrabold', className)}>
      <span className="mr-0.5 text-[0.6em] font-semibold opacity-55">R$</span>{num}
    </span>
  );
}

/* ---------------- Card ---------------- */
const Card = ({ className, children, ...r }) => (
  <div className={cn('rounded-2xl bg-panel border border-edge overflow-hidden', className)} {...r}>{children}</div>
);
const CardContent = ({ className, children }) => <div className={cn('p-4', className)}>{children}</div>;
const CardFooter = ({ className, children }) => <div className={cn('p-4 pt-0', className)}>{children}</div>;

/* ---------------- Badge ---------------- */
function Badge({ variant = 'neutral', className, children }) {
  const v = {
    ml: 'bg-gold/15 text-gold border border-gold/30',
    shopee: 'bg-coral/15 text-coral border border-coral/30',
    brand: 'bg-brand/15 text-brand border border-brand/30',
    neutral: 'bg-white/[0.06] text-zinc-300 border border-edge',
    solidGold: 'bg-gold text-ink border border-gold',
  };
  return <span className={cn('inline-flex items-center gap-1 rounded-full px-2.5 py-0.5 text-[11px] font-semibold tracking-wide', v[variant], className)}>{children}</span>;
}

/* ---------------- Separator ---------------- */
const Separator = ({ className }) => <div className={cn('h-px w-full bg-edge', className)} />;

/* ---------------- Skeleton ---------------- */
const Skeleton = ({ className }) => <div className={cn('animate-pulse rounded-md bg-white/[0.06]', className)} />;

/* ---------------- Alert ---------------- */
function Alert({ tone = 'neutral', className, children }) {
  const tones = {
    neutral: 'border-edge bg-white/[0.03] text-zinc-300',
    gold: 'border-gold/30 bg-gold/[0.06] text-gold',
    coral: 'border-coral/30 bg-coral/[0.06] text-coral',
  };
  return <div className={cn('flex items-start gap-3 rounded-xl border px-4 py-3 text-sm', tones[tone], className)}>{children}</div>;
}
const AlertDescription = ({ className, children }) => <div className={cn('leading-relaxed', className)}>{children}</div>;

/* ---------------- Tabs (segmented control) ---------------- */
function Tabs({ value, onValueChange, items }) {
  return (
    <div className="inline-flex items-center gap-1 rounded-xl bg-white/[0.04] border border-edge p-1">
      {items.map((it) => (
        <button
          key={it.value}
          onClick={() => onValueChange(it.value)}
          className={cn(
            'relative inline-flex items-center gap-1.5 rounded-lg px-3.5 py-1.5 text-sm font-medium transition-all duration-200',
            value === it.value ? 'bg-white/[0.08] text-white shadow-sm' : 'text-zinc-400 hover:text-zinc-200'
          )}
        >
          {it.dot && <span className={cn('h-1.5 w-1.5 rounded-full', it.dot)} />}
          {it.label}
        </button>
      ))}
    </div>
  );
}

/* ---------------- Select (dropdown customizado) ---------------- */
function Select({ value, onChange, options, className }) {
  const [open, setOpen] = useState(false);
  const ref = useRef(null);
  useEffect(() => {
    const h = (e) => { if (ref.current && !ref.current.contains(e.target)) setOpen(false); };
    document.addEventListener('mousedown', h);
    return () => document.removeEventListener('mousedown', h);
  }, []);
  const current = options.find((o) => o.value === value);
  return (
    <div className={cn('relative', className)} ref={ref}>
      <button
        onClick={() => setOpen((o) => !o)}
        className="inline-flex h-10 w-full items-center justify-between gap-3 rounded-xl border border-edge bg-white/[0.04] px-3.5 text-sm text-zinc-200 transition-colors hover:bg-white/[0.07] focus:outline-none focus:ring-2 focus:ring-brand/25"
      >
        <span className="flex items-center gap-2"><span className="text-zinc-500">Ordenar:</span><span className="font-medium text-white">{current ? current.label : '—'}</span></span>
        <IconChevron size={16} className={cn('text-zinc-500 transition-transform', open && 'rotate-180')} />
      </button>
      {open && (
        <div className="absolute right-0 z-30 mt-2 w-60 overflow-hidden rounded-xl border border-edge bg-[#161616] p-1 shadow-2xl shadow-black/60">
          {options.map((o) => (
            <button
              key={o.value}
              onClick={() => { onChange(o.value); setOpen(false); }}
              className={cn('flex w-full items-center justify-between rounded-lg px-3 py-2 text-left text-sm transition-colors', o.value === value ? 'bg-white/[0.07] text-white' : 'text-zinc-300 hover:bg-white/[0.05]')}
            >
              {o.label}
              {o.value === value && <IconCheck size={15} className="text-brand" />}
            </button>
          ))}
        </div>
      )}
    </div>
  );
}

/* ---------------- Dialog ---------------- */
function Dialog({ open, onClose, children }) {
  useEffect(() => {
    const onKey = (e) => { if (e.key === 'Escape') onClose(); };
    if (open) document.addEventListener('keydown', onKey);
    return () => document.removeEventListener('keydown', onKey);
  }, [open, onClose]);
  if (!open) return null;
  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center p-4">
      <div className="absolute inset-0 bg-black/70 backdrop-blur-sm animate-[fadeIn_.15s_ease]" onClick={onClose} />
      <div className="relative w-full max-w-md rounded-2xl border border-edge bg-[#141414] shadow-2xl shadow-black/70 animate-[popIn_.18s_cubic-bezier(.16,1,.3,1)]">
        {children}
      </div>
    </div>
  );
}

/* ---------------- Tooltip (leve) ---------------- */
function Tooltip({ label, children }) {
  const [show, setShow] = useState(false);
  return (
    <span className="relative inline-flex" onMouseEnter={() => setShow(true)} onMouseLeave={() => setShow(false)}>
      {children}
      {show && (
        <span className="pointer-events-none absolute -top-9 left-1/2 z-40 -translate-x-1/2 whitespace-nowrap rounded-md border border-edge bg-[#1b1b1b] px-2.5 py-1 text-xs text-zinc-200 shadow-lg">
          {label}
        </span>
      )}
    </span>
  );
}

Object.assign(window, {
  cn, Button, Input, Commission, Card, CardContent, CardFooter, Badge, Separator, Skeleton,
  Alert, AlertDescription, Tabs, Select, Dialog, Tooltip,
  IconSearch, IconExternal, IconStar, IconX, IconChevron, IconCheck, IconAlert, IconBox, IconGear,
  IconGrid, IconList, IconLogout, IconMail, IconLock, IconUser, IconGoogle,
  IconChart, IconTrendUp, IconLink, IconWallet, IconBolt, IconCart, IconClock, IconShield, IconArrowRight,
  IconLifebuoy, IconBell, IconCreditCard, IconTrash, IconSliders, IconCamera, IconDownload,
  IconBookmark, IconTrendDown, IconFlame, IconBellPlus, IconTarget, IconCopy, IconCheckCircle,
  IconPhone, IconMapPin, IconFire, IconFireGradient,
});
