// components.jsx — shared UI for "hire a human"
const { useState } = React;

/* ---------- icons (inline SVG, 1.6 stroke) ---------- */
const IconPaths = {
  box:    <><path d="M3 7l9-4 9 4-9 4-9-4z"/><path d="M3 7v10l9 4 9-4V7"/><path d="M12 11v10"/></>,
  search: <><circle cx="11" cy="11" r="7"/><path d="M21 21l-4-4"/></>,
  mega:   <><path d="M3 11v2a1 1 0 0 0 1 1h2l4 4V6L6 10H4a1 1 0 0 0-1 1z"/><path d="M14 8a4 4 0 0 1 0 8"/></>,
  camera: <><path d="M4 8h3l2-2h6l2 2h3v11H4z"/><circle cx="12" cy="13" r="3.5"/></>,
  ticket: <><path d="M4 8a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2 2 2 0 0 0 0 4 2 2 0 0 1-2 2H6a2 2 0 0 1-2-2 2 2 0 0 0 0-4z"/></>,
  home:   <><path d="M4 11l8-6 8 6"/><path d="M6 10v9h12v-9"/></>,
  star:   <><path d="M12 3l2.6 5.6L20 9.3l-4 4 1 6-5-2.8L7 19.3l1-6-4-4 5.4-.7z"/></>,
  pin:    <><path d="M12 21s7-6.4 7-11a7 7 0 1 0-14 0c0 4.6 7 11 7 11z"/><circle cx="12" cy="10" r="2.5"/></>,
  clock:  <><circle cx="12" cy="12" r="8.5"/><path d="M12 8v4.5l3 2"/></>,
  users:  <><circle cx="9" cy="9" r="3"/><path d="M3.5 19a5.5 5.5 0 0 1 11 0"/><path d="M16 7a3 3 0 0 1 0 6"/><path d="M20.5 19a5.5 5.5 0 0 0-3-4.9"/></>,
  check:  <><path d="M4 12.5l5 5 11-11"/></>,
  bolt:   <><path d="M13 3L5 13h6l-1 8 8-10h-6z"/></>,
  arrow:  <><path d="M5 12h14"/><path d="M13 6l6 6-6 6"/></>,
  shield: <><path d="M12 3l8 3v6c0 5-3.5 8-8 9-4.5-1-8-4-8-9V6z"/><path d="M9 12l2 2 4-4"/></>,
  chat:   <><path d="M4 5h16v11H9l-5 4z"/></>,
  bell:   <><path d="M18 8a6 6 0 1 0-12 0c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.7 21a2 2 0 0 1-3.4 0"/></>,
  plus:   <><path d="M12 5v14"/><path d="M5 12h14"/></>,
  heart:  <><path d="M12 20s-7-4.6-7-10a4 4 0 0 1 7-2.6A4 4 0 0 1 19 10c0 5.4-7 10-7 10z"/></>,
  back:   <><path d="M19 12H5"/><path d="M11 6l-6 6 6 6"/></>,
  doc:    <><path d="M7 3h7l4 4v14H7z"/><path d="M14 3v4h4"/><path d="M10 12h5M10 16h5"/></>,
  wallet: <><path d="M3 7h15a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H4a1 1 0 0 1-1-1z"/><path d="M3 7V6a1 1 0 0 1 1-1h12"/><circle cx="16" cy="13" r="1.3"/></>,
  settings:<><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09a1.65 1.65 0 0 0-1.08-1.51 1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09a1.65 1.65 0 0 0 1.51-1.08 1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9c.26.604.852.997 1.51 1H21a2 2 0 0 1 0 4h-.09c-.658.003-1.25.396-1.51 1z"/></>,
  logout:  <><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"/></>,
  upload:  <><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></>,
  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-6A19.79 19.79 0 0 1 2.12 4.18 2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.127.96.362 1.903.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.907.338 1.85.573 2.81.7A2 2 0 0 1 22 16.92z"/></>,
  mail:    <><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></>,
  edit:    <><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></>,
  eye:     <><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></>,
  image:   <><rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></>,
  ban:     <><circle cx="12" cy="12" r="10"/><line x1="4.93" y1="4.93" x2="19.07" y2="19.07"/></>,
  grid:    <><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></>,
  dollar:  <><line x1="12" y1="1" x2="12" y2="23"/><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"/></>,
  link:    <><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/></>,
};
function Icon({ name, size = 20, stroke = 1.7, style, fill = false }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill={fill ? "currentColor" : "none"}
      stroke="currentColor" strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round"
      style={{ flexShrink: 0, ...style }}>
      {IconPaths[name]}
    </svg>
  );
}

/* ---------- avatar ---------- */
function Avatar({ name, size = 40, ring, src }) {
  const a = avatarFor(name);
  const ringShadow = ring ? "0 0 0 2px var(--paper), 0 0 0 4px " + a.bg : "none";
  if (src) {
    return (
      <img src={src} alt={name} style={{
        width: size, height: size, borderRadius: "50%", objectFit: "cover", flexShrink: 0,
        boxShadow: ring ? "0 0 0 2px var(--paper), 0 0 0 4px var(--line-dk)" : "none", background: a.bg,
      }} />
    );
  }
  return (
    <div style={{
      width: size, height: size, borderRadius: "50%", background: a.bg, color: a.fg,
      display: "grid", placeItems: "center", fontWeight: 700, flexShrink: 0,
      fontSize: size * 0.4, letterSpacing: "-0.02em", fontFamily: "var(--sans)",
      boxShadow: ringShadow,
    }}>{a.initials}</div>
  );
}

/* ---------- button ---------- */
function Button({ children, variant = "primary", size = "md", icon, iconRight, onClick, type, disabled, full, style }) {
  const [h, setH] = useState(false);
  const sizes = {
    sm: { padding: "8px 14px", fontSize: 14, gap: 7 },
    md: { padding: "12px 20px", fontSize: 15.5, gap: 9 },
    lg: { padding: "16px 26px", fontSize: 17, gap: 10 },
  }[size];
  const variants = {
    primary: { background: h ? "var(--clay-dk)" : "var(--clay)", color: "#fff", border: "1px solid transparent" },
    dark:    { background: h ? "#000" : "var(--ink)", color: "var(--paper)", border: "1px solid transparent" },
    ghost:   { background: h ? "var(--ink-3)" : "transparent", color: "var(--ink)", border: "1px solid var(--line)" },
    soft:    { background: h ? "#efe7d8" : "var(--ink-3)", color: "var(--ink)", border: "1px solid transparent" },
    green:   { background: h ? "#19684799" : "transparent", color: "var(--green)", border: "1px solid var(--green)" },
  }[variant];
  return (
    <button type={type || "button"} onClick={disabled ? undefined : onClick} disabled={disabled}
      onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)}
      style={{
        display: "inline-flex", alignItems: "center", justifyContent: "center", ...sizes,
        borderRadius: "var(--r-btn)", fontWeight: 600, cursor: disabled ? "not-allowed" : "pointer",
        fontFamily: "var(--sans)", whiteSpace: "nowrap", lineHeight: 1, width: full ? "100%" : "auto",
        opacity: disabled ? 0.45 : 1, transition: "background .15s, transform .1s",
        transform: h && !disabled ? "translateY(-1px)" : "none", ...variants, ...style,
      }}>
      {icon && <Icon name={icon} size={sizes.fontSize + 2} />}
      {children}
      {iconRight && <Icon name={iconRight} size={sizes.fontSize + 2} />}
    </button>
  );
}

/* ---------- small bits ---------- */
function Stars({ value, size = 13 }) {
  return (
    <span style={{ display: "inline-flex", alignItems: "center", gap: 3, color: "var(--amber)" }}>
      <Icon name="star" size={size} fill stroke={0} />
      <span style={{ color: "var(--ink)", fontWeight: 600, fontSize: size }}>{value.toFixed(1)}</span>
    </span>
  );
}

function PayTag({ pay, size = "md" }) {
  const big = size === "lg";
  if (pay.kind === "favor") {
    return (
      <span style={{
        display: "inline-flex", alignItems: "center", gap: 6, color: "var(--green)",
        background: "var(--green-3)", borderRadius: 999, padding: big ? "8px 14px" : "5px 11px",
        fontWeight: 700, fontSize: big ? 16 : 13.5, fontFamily: "var(--sans)",
      }}>
        <Icon name="heart" size={big ? 17 : 14} fill stroke={0} /> Favor
      </span>
    );
  }
  const boosted = pay.originalAmount && pay.originalAmount < pay.amount;
  return (
    <span style={{ display: "inline-flex", alignItems: "center", gap: big ? 10 : 7, flexWrap: "wrap" }}>
      {boosted && (
        <span style={{
          fontFamily: "var(--display)", fontWeight: 600, letterSpacing: "-0.02em",
          fontSize: big ? 20 : 14, color: "var(--ink-2)", textDecoration: "line-through",
        }}>{pay.cur}{pay.originalAmount.toLocaleString()}</span>
      )}
      <span style={{
        fontFamily: "var(--display)", fontWeight: 700, letterSpacing: "-0.02em",
        fontSize: big ? 30 : 19, color: "var(--ink)",
      }}>{pay.cur}{pay.amount.toLocaleString()}<span style={{
        fontFamily: "var(--sans)", fontSize: big ? 14 : 12, fontWeight: 600, color: "var(--ink-2)", marginLeft: 5,
      }}>fixed</span></span>
      {boosted && (
        <span style={{
          display: "inline-flex", alignItems: "center", gap: 4, fontSize: big ? 13 : 11.5, fontWeight: 700,
          color: "var(--green)", background: "var(--green-3)", borderRadius: 999, padding: big ? "4px 10px" : "3px 8px",
          fontFamily: "var(--sans)",
        }}><Icon name="bolt" size={big ? 13 : 11} fill stroke={0} /> Boosted</span>
      )}
    </span>
  );
}

function CatChip({ cat, active, onClick, small }) {
  const c = CAT[cat];
  return (
    <button onClick={onClick} style={{
      display: "inline-flex", alignItems: "center", gap: 7, cursor: "pointer",
      padding: small ? "7px 13px" : "9px 15px", borderRadius: 999, fontFamily: "var(--sans)",
      fontSize: small ? 13.5 : 14.5, fontWeight: 600, whiteSpace: "nowrap",
      transition: "all .15s",
      background: active ? "var(--ink)" : "var(--surface)",
      color: active ? "var(--paper)" : "var(--ink)",
      border: "1px solid " + (active ? "var(--ink)" : "var(--line)"),
    }}>
      <Icon name={c.icon} size={small ? 14 : 16} stroke={1.8} /> {c.label}
    </button>
  );
}

function VerifiedTick({ size = 15 }) {
  return (
    <span title="Verified human" style={{
      display: "inline-grid", placeItems: "center", width: size, height: size,
      borderRadius: "50%", background: "var(--blue)", color: "#fff", flexShrink: 0,
    }}>
      <Icon name="check" size={size * 0.62} stroke={3} />
    </span>
  );
}
/* ---------- confetti ---------- */
function Confetti({ active }) {
  const [particles, setParticles] = React.useState([]);

  React.useEffect(() => {
    if (active) {
      const colors = ["#DC4B2E", "#1F7A55", "#E8A33D", "#2E7CF6", "#F4EEE3"];
      const p = Array.from({ length: 100 }).map((_, i) => ({
        id: i,
        x: Math.random() * 100,
        y: -10 - Math.random() * 20,
        r: Math.random() * 360,
        vx: (Math.random() - 0.5) * 2,
        vy: 2 + Math.random() * 3,
        vr: (Math.random() - 0.5) * 10,
        color: colors[Math.floor(Math.random() * colors.length)],
        size: 5 + Math.random() * 8
      }));
      setParticles(p);

      const timer = setInterval(() => {
        setParticles(prev => {
          let allFallen = true;
          const next = prev.map(pt => {
            const ny = pt.y + pt.vy;
            if (ny < 120) allFallen = false;
            return {
              ...pt,
              x: pt.x + pt.vx,
              y: ny,
              r: pt.r + pt.vr,
              vy: pt.vy + 0.05 // gravity
            };
          });
          if (allFallen) clearInterval(timer);
          return next;
        });
      }, 16);
      return () => clearInterval(timer);
    } else {
      setParticles([]);
    }
  }, [active]);

  if (!active || particles.length === 0) return null;

  return (
    <div style={{ position: "fixed", top: 0, left: 0, right: 0, bottom: 0, pointerEvents: "none", zIndex: 9999, overflow: "hidden" }}>
      {particles.map(p => (
        <div key={p.id} style={{
          position: "absolute",
          left: `${p.x}%`,
          top: `${p.y}%`,
          width: p.size,
          height: p.size,
          backgroundColor: p.color,
          transform: `rotate(${p.r}deg)`,
          borderRadius: p.size % 3 === 0 ? "50%" : "2px"
        }} />
      ))}
    </div>
  );
}

Object.assign(window, { Icon, Avatar, Button, Stars, PayTag, CatChip, VerifiedTick, Confetti });
