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

// ── Icons ──────────────────────────────────────────────────────
const Icon = {
  Logo: () => (
    <svg viewBox="0 0 16 16" fill="none">
      <path
        d="M1 8h3l2-5 3 10 2-7 2 4h2"
        stroke="currentColor"
        strokeWidth="1.5"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  ),
  Clock: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <circle cx="8" cy="8" r="6" stroke="currentColor" strokeWidth="1.3" />
      <path
        d="M8 5v3l2 1.5"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
      />
    </svg>
  ),
  Users: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <circle cx="6" cy="6" r="2.2" stroke="currentColor" strokeWidth="1.3" />
      <circle
        cx="11.5"
        cy="5.5"
        r="1.8"
        stroke="currentColor"
        strokeWidth="1.3"
      />
      <path
        d="M2 13c0-2 1.8-3.5 4-3.5s4 1.5 4 3.5M11 9.5c1.7 0 3 1.3 3 3.5"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
      />
    </svg>
  ),
  Team: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <circle cx="5" cy="5.5" r="1.8" stroke="currentColor" strokeWidth="1.3" />
      <circle
        cx="11"
        cy="5.5"
        r="1.8"
        stroke="currentColor"
        strokeWidth="1.3"
      />
      <path
        d="M1.5 12.5c0-1.6 1.3-3 3.5-3s3.5 1.4 3.5 3M7.5 12.5c0-1.6 1.3-3 3.5-3s3.5 1.4 3.5 3"
        stroke="currentColor"
        strokeWidth="1.3"
      />
    </svg>
  ),
  Building: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <rect
        x="2.5"
        y="2.5"
        width="11"
        height="11"
        rx="1"
        stroke="currentColor"
        strokeWidth="1.3"
      />
      <path
        d="M5 5h1M5 7.5h1M5 10h1M8 5h1M8 7.5h1M8 10h1M11 5h.5M11 7.5h.5"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
      />
    </svg>
  ),
  Globe: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <circle cx="8" cy="8" r="6" stroke="currentColor" strokeWidth="1.3" />
      <path
        d="M2 8h12M8 2c2 2 2 10 0 12M8 2c-2 2-2 10 0 12"
        stroke="currentColor"
        strokeWidth="1.3"
      />
    </svg>
  ),
  Layers: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M8 2 2 5.5l6 3.5 6-3.5L8 2zM2 8l6 3.5L14 8M2 10.5l6 3.5 6-3.5"
        stroke="currentColor"
        strokeWidth="1.2"
        strokeLinejoin="round"
      />
    </svg>
  ),
  Search: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <circle cx="7" cy="7" r="4.5" stroke="currentColor" strokeWidth="1.3" />
      <path
        d="m10.5 10.5 3 3"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
      />
    </svg>
  ),
  Spark: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M8 2v4M8 10v4M2 8h4M10 8h4M4 4l2 2M10 10l2 2M12 4l-2 2M6 10l-2 2"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
      />
    </svg>
  ),
  Check: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <circle cx="8" cy="8" r="6" fill="currentColor" opacity="0.15" />
      <path
        d="m5 8 2 2 4-4"
        stroke="currentColor"
        strokeWidth="1.5"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  ),
  Circle: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <circle cx="8" cy="8" r="6" stroke="currentColor" strokeWidth="1.2" />
    </svg>
  ),
  Play: (p) => (
    <svg viewBox="0 0 16 16" fill="currentColor" {...p}>
      <path d="M5 3.5v9l7-4.5z" />
    </svg>
  ),
  Pause: (p) => (
    <svg viewBox="0 0 16 16" fill="currentColor" {...p}>
      <rect x="4.5" y="3" width="2.5" height="10" rx="0.5" />
      <rect x="9" y="3" width="2.5" height="10" rx="0.5" />
    </svg>
  ),
  Mic: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <rect
        x="6"
        y="2"
        width="4"
        height="7"
        rx="2"
        stroke="currentColor"
        strokeWidth="1.3"
      />
      <path
        d="M3.5 8c0 2.5 2 4.5 4.5 4.5S12.5 10.5 12.5 8M8 12.5V14"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
      />
    </svg>
  ),
  Close: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="m4 4 8 8M12 4l-8 8"
        stroke="currentColor"
        strokeWidth="1.5"
        strokeLinecap="round"
      />
    </svg>
  ),
  Wiki: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M3 3h6a3 3 0 0 1 3 3v7.5l-3-2-6 0V3z"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinejoin="round"
      />
    </svg>
  ),
  List: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M3 4h10M3 8h10M3 12h10"
        stroke="currentColor"
        strokeWidth="1.4"
        strokeLinecap="round"
      />
    </svg>
  ),
  Target: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <circle cx="8" cy="8" r="6" stroke="currentColor" strokeWidth="1.2" />
      <circle cx="8" cy="8" r="3" stroke="currentColor" strokeWidth="1.2" />
      <circle cx="8" cy="8" r="1" fill="currentColor" />
    </svg>
  ),
  Wave: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M1 8h2l1-4 2 8 2-10 2 10 2-6 1 2h2"
        stroke="currentColor"
        strokeWidth="1.2"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  ),
  Decision: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M8 1.5 14.5 8 8 14.5 1.5 8 8 1.5z"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinejoin="round"
      />
    </svg>
  ),
  Blocker: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M8 2 14 13H2L8 2z"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinejoin="round"
      />
      <path
        d="M8 7v3M8 11.5v0.5"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
      />
    </svg>
  ),
  Idea: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M8 1.5a4.5 4.5 0 0 0-3 7.9V12h6V9.4A4.5 4.5 0 0 0 8 1.5z"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinejoin="round"
      />
      <path
        d="M6 13.5h4M7 15h2"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
      />
    </svg>
  ),
  File: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M9 1.5H4a1 1 0 0 0-1 1v11a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V5.5L9 1.5z"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinejoin="round"
      />
      <path d="M9 1.5V5.5h4" stroke="currentColor" strokeWidth="1.3" />
    </svg>
  ),
  Send: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M2 8 14 2l-4 12-2-5-6-1z"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinejoin="round"
      />
    </svg>
  ),
  Mail: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <rect
        x="1.5"
        y="3.5"
        width="13"
        height="9"
        rx="1.5"
        stroke="currentColor"
        strokeWidth="1.3"
      />
      <path d="m2 5 6 4 6-4" stroke="currentColor" strokeWidth="1.3" />
    </svg>
  ),
  Calendar: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <rect
        x="2"
        y="3"
        width="12"
        height="11"
        rx="1.5"
        stroke="currentColor"
        strokeWidth="1.3"
      />
      <path
        d="M2 6h12M5 2v2M11 2v2"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
      />
    </svg>
  ),
  ArrowLeft: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M13 8H3M7 4 3 8l4 4"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  ),
  ArrowRight: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M3 8h10M9 4l4 4-4 4"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  ),
  Plus: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M8 3v10M3 8h10"
        stroke="currentColor"
        strokeWidth="1.4"
        strokeLinecap="round"
      />
    </svg>
  ),
  Eye: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M1 8c2-3.5 4.5-5 7-5s5 1.5 7 5c-2 3.5-4.5 5-7 5s-5-1.5-7-5z"
        stroke="currentColor"
        strokeWidth="1.2"
      />
      <circle cx="8" cy="8" r="2" stroke="currentColor" strokeWidth="1.2" />
    </svg>
  ),
  Heart: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M8 13.5s-5-3-5-7a2.5 2.5 0 0 1 5-.5 2.5 2.5 0 0 1 5 .5c0 4-5 7-5 7z"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinejoin="round"
      />
    </svg>
  ),
  Hash: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M6 2 4 14M12 2l-2 12M2 6h12M2 10h12"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
      />
    </svg>
  ),
  Chevron: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="m4 6 4 4 4-4"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  ),
  SignOut: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M6 2.5H3a1 1 0 0 0-1 1v9a1 1 0 0 0 1 1h3M11 11l3-3-3-3M14 8H6"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  ),
  AlertCircle: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <circle cx="8" cy="8" r="6" stroke="currentColor" strokeWidth="1.3" />
      <path
        d="M8 5v3.5M8 10.5v.5"
        stroke="currentColor"
        strokeWidth="1.4"
        strokeLinecap="round"
      />
    </svg>
  ),
  Edit: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <path
        d="M11.5 2.5a1.4 1.4 0 0 1 2 2L5.5 12.5l-3 .5.5-3 8-8z"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinejoin="round"
      />
      <path d="M10 4l2 2" stroke="currentColor" strokeWidth="1.3" />
    </svg>
  ),
  Settings: (p) => (
    <svg viewBox="0 0 16 16" fill="none" {...p}>
      <circle cx="8" cy="8" r="2" stroke="currentColor" strokeWidth="1.3" />
      <path
        d="M8 1.5v2M8 12.5v2M1.5 8h2M12.5 8h2M3.4 3.4l1.4 1.4M11.2 11.2l1.4 1.4M12.6 3.4l-1.4 1.4M4.8 11.2l-1.4 1.4"
        stroke="currentColor"
        strokeWidth="1.3"
        strokeLinecap="round"
      />
    </svg>
  ),
};

// ── Avatar helper ──────────────────────────────────────────────
function avatarBg(name) {
  let hash = 0;
  for (let i = 0; i < name.length; i++) hash = (hash + name.charCodeAt(i)) * 7;
  const offset = (hash % 5) * 25;
  const h1 = `calc(var(--avatar-hue) + ${offset})`;
  const h2 = `calc(var(--avatar-hue) + ${offset + 15})`;
  return `linear-gradient(135deg, hsl(${h1} 38% 48%), hsl(${h2} 42% 54%))`;
}
function Avatar({ name, size, you }) {
  const init = name
    .split(" ")
    .map((p) => p[0])
    .slice(0, 2)
    .join("")
    .toUpperCase();
  const cls =
    size === "lg" ? "avatar lg" : size === "sm" ? "avatar sm" : "avatar";
  const bg = you ? "var(--avatar-you)" : avatarBg(name);
  return (
    <div className={cls} style={{ background: bg }}>
      {init}
    </div>
  );
}

window.Icon = Icon;
window.Avatar = Avatar;
