// Custom SVG icon set — thin 1.5px line, 24x24 viewBox. No emoji.
// All icons accept { size, stroke, className, style }.

const Icon = ({ children, size = 20, stroke = 1.6, className = "", style = {}, ...rest }) => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    width={size}
    height={size}
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    strokeWidth={stroke}
    strokeLinecap="round"
    strokeLinejoin="round"
    className={className}
    style={{ flexShrink: 0, ...style }}
    {...rest}
  >
    {children}
  </svg>
);

const IconBook = (p) => (
  <Icon {...p}>
    <path d="M4 4.5C4 3.7 4.7 3 5.5 3H11v16H5.5c-.8 0-1.5.7-1.5 1.5V4.5Z" />
    <path d="M20 4.5C20 3.7 19.3 3 18.5 3H13v16h5.5c.8 0 1.5.7 1.5 1.5V4.5Z" />
  </Icon>
);

const IconSearch = (p) => (
  <Icon {...p}>
    <circle cx="11" cy="11" r="6.5" />
    <path d="m20 20-3.5-3.5" />
  </Icon>
);

const IconBag = (p) => (
  <Icon {...p}>
    <path d="M5 8h14l-1 12H6L5 8Z" />
    <path d="M9 8V6a3 3 0 0 1 6 0v2" />
  </Icon>
);

const IconClock = (p) => (
  <Icon {...p}>
    <circle cx="12" cy="12" r="8.5" />
    <path d="M12 7.5V12l3 2" />
  </Icon>
);

const IconFlame = (p) => (
  <Icon {...p}>
    <path d="M12 3c1 3 4 4.5 4 8.5a4 4 0 1 1-8 0C8 9 10 8 10.5 6 11 7.5 12 6 12 3Z" />
  </Icon>
);

const IconUsers = (p) => (
  <Icon {...p}>
    <circle cx="9" cy="9" r="3.5" />
    <path d="M3 20c0-3 2.7-5 6-5s6 2 6 5" />
    <path d="M16 4.5a3.5 3.5 0 0 1 0 7" />
    <path d="M18 20c0-2.5 1.5-4 3-4.5" />
  </Icon>
);

const IconPlus = (p) => (
  <Icon {...p}>
    <path d="M12 5v14M5 12h14" />
  </Icon>
);

const IconLink = (p) => (
  <Icon {...p}>
    <path d="M10 14a4 4 0 0 0 5.6 0l3-3a4 4 0 0 0-5.6-5.6l-1 1" />
    <path d="M14 10a4 4 0 0 0-5.6 0l-3 3a4 4 0 0 0 5.6 5.6l1-1" />
  </Icon>
);

const IconCamera = (p) => (
  <Icon {...p}>
    <path d="M4 8h3l2-2h6l2 2h3v11H4V8Z" />
    <circle cx="12" cy="13" r="3.5" />
  </Icon>
);

const IconScan = (p) => (
  <Icon {...p}>
    <path d="M4 8V6a2 2 0 0 1 2-2h2" />
    <path d="M16 4h2a2 2 0 0 1 2 2v2" />
    <path d="M20 16v2a2 2 0 0 1-2 2h-2" />
    <path d="M8 20H6a2 2 0 0 1-2-2v-2" />
    <path d="M4 12h16" />
  </Icon>
);

const IconX = (p) => (
  <Icon {...p}>
    <path d="M6 6l12 12M18 6 6 18" />
  </Icon>
);

const IconChevLeft = (p) => (
  <Icon {...p}>
    <path d="m14 6-6 6 6 6" />
  </Icon>
);

const IconChevRight = (p) => (
  <Icon {...p}>
    <path d="m10 6 6 6-6 6" />
  </Icon>
);

const IconChevDown = (p) => (
  <Icon {...p}>
    <path d="m6 10 6 6 6-6" />
  </Icon>
);

const IconStar = ({ filled = false, ...p }) => (
  <Icon {...p} fill={filled ? "currentColor" : "none"}>
    <path d="m12 3.5 2.7 5.5 6 .9-4.4 4.2 1.1 6-5.4-2.9-5.4 2.9 1.1-6L3.3 9.9l6-.9L12 3.5Z" />
  </Icon>
);

const IconHeart = ({ filled = false, ...p }) => (
  <Icon {...p} fill={filled ? "currentColor" : "none"}>
    <path d="M12 20s-7-4.5-7-10a4.5 4.5 0 0 1 7-3.5A4.5 4.5 0 0 1 19 10c0 5.5-7 10-7 10Z" />
  </Icon>
);

const IconEdit = (p) => (
  <Icon {...p}>
    <path d="M4 20h4l10-10-4-4L4 16v4Z" />
    <path d="m14 6 4 4" />
  </Icon>
);

const IconTrash = (p) => (
  <Icon {...p}>
    <path d="M4 7h16" />
    <path d="M6 7v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V7" />
    <path d="M9 7V5a2 2 0 0 1 2-2h2a2 2 0 0 1 2 2v2" />
    <path d="M10 11v6M14 11v6" />
  </Icon>
);

const IconPlay = (p) => (
  <Icon {...p}>
    <path d="M7 4.5v15l13-7.5L7 4.5Z" />
  </Icon>
);

const IconCheck = (p) => (
  <Icon {...p}>
    <path d="m5 12 5 5 9-11" />
  </Icon>
);

const IconHome = (p) => (
  <Icon {...p}>
    <path d="M4 11 12 4l8 7" />
    <path d="M6 10v10h12V10" />
  </Icon>
);

const IconGear = (p) => (
  <Icon {...p}>
    <circle cx="12" cy="12" r="3" />
    <path d="M12 2v2M12 20v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M2 12h2M20 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42" />
  </Icon>
);

const IconShield = (p) => (
  <Icon {...p}>
    <path d="M12 2 4 5v6c0 5 4 9 8 11 4-2 8-6 8-11V5L12 2Z" />
  </Icon>
);

const IconArrowRight = (p) => (
  <Icon {...p}>
    <path d="M5 12h14M13 6l6 6-6 6" />
  </Icon>
);

const IconDice = (p) => (
  <Icon {...p}>
    <rect x="3" y="3" width="18" height="18" rx="4" />
    <circle cx="8.5"  cy="8.5"  r="1.5" fill="currentColor" />
    <circle cx="12"   cy="12"   r="1.5" fill="currentColor" />
    <circle cx="15.5" cy="15.5" r="1.5" fill="currentColor" />
  </Icon>
);

const IconCalendar = (p) => (
  <Icon {...p}>
    <rect x="3" y="5" width="18" height="16" rx="2" />
    <path d="M3 10h18M8 3v4M16 3v4" />
  </Icon>
);

// Custom cookbook mark — a stylized "open book + fork" combined mark
const IconLogo = ({ size = 28, ...p }) => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    width={size}
    height={size}
    viewBox="0 0 32 32"
    fill="none"
    {...p}
  >
    <path
      d="M4 7c0-.8.7-1.5 1.5-1.5H14V24H5.5c-.8 0-1.5.7-1.5 1.5V7Z"
      stroke="currentColor"
      strokeWidth="1.8"
      strokeLinejoin="round"
    />
    <path
      d="M28 7c0-.8-.7-1.5-1.5-1.5H18V24h8.5c.8 0 1.5.7 1.5 1.5V7Z"
      stroke="currentColor"
      strokeWidth="1.8"
      strokeLinejoin="round"
    />
    <circle cx="16" cy="15" r="2.5" fill="currentColor" />
  </svg>
);


// ── Avatar marks: flat illustrated cooking icons (UI v2) ─────────────
const AV_ICONS = {
  hat: `<path d="M7 14.5V11a1 1 0 01-2.4-1.8A4.2 4.2 0 019 5.6a4.6 4.6 0 016 0 4.2 4.2 0 014.4 3.6A1 1 0 0117 11v3.5z M7 16.5h10v2.2H7z" fill="currentColor"/>`,
  whisk: `<rect x="10.8" y="1.6" width="2.4" height="5.6" rx="1.2" fill="currentColor"/><path d="M12 7.2C6.8 10.4 6.6 16.4 12 20.6c5.4-4.2 5.2-10.2 0-13.4z" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/><path d="M12 7.2C9.4 10.6 9.3 16.6 12 20.6c2.7-4 2.6-10 0-13.4z" fill="none" stroke="currentColor" stroke-width="1.2" opacity="0.7"/><path d="M12 7.2v13.4" stroke="currentColor" stroke-width="1.1" opacity="0.5"/><circle cx="12" cy="21" r="1.05" fill="currentColor"/>`,
  pin: `<g transform="rotate(-14 12 12)"><rect x="4.6" y="9.2" width="14.8" height="5.6" rx="2.8" fill="currentColor"/><rect x="0.8" y="10.3" width="3.5" height="3.4" rx="1.6" fill="currentColor" opacity="0.62"/><rect x="19.7" y="10.3" width="3.5" height="3.4" rx="1.6" fill="currentColor" opacity="0.62"/></g><circle cx="6" cy="19.4" r="0.8" fill="currentColor" opacity="0.5"/><circle cx="17" cy="19.8" r="0.7" fill="currentColor" opacity="0.45"/>`,
  pan: `<circle cx="10.3" cy="12.6" r="7.6" fill="currentColor"/><circle cx="10.3" cy="12.6" r="5.9" fill="currentColor" opacity="0.45"/><rect x="17.2" y="11.4" width="6" height="2.4" rx="1.2" fill="currentColor"/><path d="M7.6 10.4c-1.6 1.3-2 3.6-.9 5.3 1 1.6 3 2.3 4.8 1.8 1.9-.5 3.1-2.3 2.9-4.2-.1-1.2-.8-2.3-1.9-2.9-.8-.5-1.8-.6-2.7-.4" fill="#fdf8ec"/><circle cx="9.7" cy="13.1" r="2.05" fill="#e8b13c"/><circle cx="9" cy="12.4" r="0.62" fill="#fdf8ec" opacity="0.85"/>`,
  tomato: `<circle cx="12" cy="13.8" r="7.4" fill="currentColor"/><path d="M6.4 11.2a7.4 7.4 0 013-3.6" stroke="currentColor" stroke-width="1.1" opacity="0.5" stroke-linecap="round" fill="none"/><path d="M12 6.4c-2.4.2-3.9 1-4.7 2.1 1.4.4 2.5.3 3.4-.1-.4.8-.3 1.5 0 2.2.7-.7 1.1-1.4 1.3-2.2.2.8.6 1.5 1.3 2.2.3-.7.4-1.4 0-2.2.9.4 2 .5 3.4.1-.8-1.1-2.3-1.9-4.7-2.1z" fill="currentColor"/>`,
  cake: `<path d="M6.6 12.4h10.8l-1.5 7.6a1.7 1.7 0 01-1.7 1.4H9.8a1.7 1.7 0 01-1.7-1.4z" fill="currentColor"/><path d="M6.3 11.8c-.7-1.6.2-3 1.7-3.2 0-1.6 1.4-2.8 3-2.6.5-1.3 2-1.9 3.3-1.4 1.1.4 1.8 1.4 1.7 2.6 1.4.5 2 2 1.4 3.4-.2.5-.6.9-1.1 1.2z" fill="currentColor" opacity="0.66"/><circle cx="12" cy="3.3" r="1.45" fill="#e8b13c"/>`,
  fish: `<path d="M2.8 12.4c2.6-4.2 6.4-6.2 10.2-6.2 3.2 0 6 2 7.8 6.2-1.8 4.2-4.6 6.2-7.8 6.2-3.8 0-7.6-2-10.2-6.2z" fill="currentColor"/><path d="M20.6 12.4l-3.4-3.4v6.8z" fill="currentColor" opacity="0.62"/><circle cx="6.8" cy="11" r="1.15" fill="#241d12" opacity="0.78"/><circle cx="6.45" cy="10.6" r="0.4" fill="#fdf8ec"/>`,
  mug: `<path d="M5 7h11v8a4 4 0 01-4 4H9a4 4 0 01-4-4z" fill="currentColor"/><path d="M16 9h2a2.6 2.6 0 010 5.2h-2" stroke="currentColor" stroke-width="1.8" fill="none"/><path d="M8.7 6.2c-.7-1 .5-1.6-.2-2.7M12.1 6.2c-.7-1 .5-1.6-.2-2.7" stroke="currentColor" stroke-width="1" opacity="0.6" fill="none" stroke-linecap="round"/>`,
  bowl: `<path d="M3.6 10.6h16.8a8.4 8.4 0 01-6.4 8.2v1.6h-4v-1.6a8.4 8.4 0 01-6.4-8.2z" fill="currentColor"/><ellipse cx="12" cy="10.6" rx="8.4" ry="1.5" fill="currentColor" opacity="0.55"/><path d="M15.4 9.2L19 2.6" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/><ellipse cx="19.6" cy="2.4" rx="1.7" ry="1.2" transform="rotate(28 19.6 2.4)" fill="currentColor"/>`,
  chili: `<path d="M18.6 6.2c1.6 5.6-1.2 11.4-6.6 13.6-2.9 1.2-6 1.1-8.4-.1 1.2-.5 2.2-1.3 2.8-2.3C8.6 19 12 18 14.4 15.4c2.2-2.4 3.4-5.6 3.1-8.9z" fill="currentColor"/><path d="M18.4 6.6c-.4-1.6.3-2.9 1.7-3.8.1 1 .7 1.6 1.5 1.9-1 .9-2.1 1.4-3.2 1.9z" fill="currentColor" opacity="0.72"/>`,
};
const AVATAR_COLORS = ['#1f4a38', '#a4502f', '#c9922a', '#3e6b50', '#2c6e99', '#6b3a52'];
const AVATAR_SET = [];
['hat','whisk','pin','pan','tomato','cake','fish','mug','bowl','chili'].forEach((ic, i) => {
  AVATAR_SET.push({ icon: ic, color: AVATAR_COLORS[i % AVATAR_COLORS.length] });
  AVATAR_SET.push({ icon: ic, color: AVATAR_COLORS[(i + 4) % AVATAR_COLORS.length] });
});

// Colored circle bearing a cook's mark; falls back to an initial.
function AvatarMark({ avatar, name = '', size = 38, className = '' }) {
  const bg = avatar ? avatar.color : 'var(--accent-2)';
  return (
    <span
      className={`av-mark ${className}`}
      style={{ background: bg, width: size, height: size, fontSize: size * 0.42 }}
      title={name}
    >
      {avatar
        ? <svg viewBox="0 0 24 24" style={{ color: '#fdf8ec', width: '58%', height: '58%' }}
               dangerouslySetInnerHTML={{ __html: AV_ICONS[avatar.icon] || '' }} />
        : (name || '?').trim()[0]?.toUpperCase()}
    </span>
  );
}

// Find a family member's avatar by display name (exact, then first-name match).
function memberAvatarFor(members, name) {
  if (!name || !Array.isArray(members)) return null;
  const n = name.trim().toLowerCase();
  const hit = members.find(m => (m.name || '').trim().toLowerCase() === n)
    || members.find(m => (m.name || '').trim().toLowerCase().split(/\s+/)[0] === n.split(/\s+/)[0]);
  return hit ? hit.avatar : null;
}

Object.assign(window, {
  Icon,
  IconBook, IconSearch, IconBag, IconClock, IconFlame, IconUsers,
  IconPlus, IconLink, IconCamera, IconScan, IconX,
  IconChevLeft, IconChevRight, IconChevDown,
  IconStar, IconHeart, IconEdit, IconTrash, IconPlay, IconCheck,
  IconHome, IconArrowRight, IconLogo, IconGear, IconShield, IconDice,
  IconCalendar, AV_ICONS, AVATAR_SET, AvatarMark, memberAvatarFor,
});
