/* eslint-disable */
/* Eight Icons web app — Vercel-faithful chassis, restructured for the
   search/browse/filter surface. Mirrors icons8's existing IA. */

const { useState, useEffect, useMemo, useRef } = React;
const ICON_VARIANTS = window.ICON_VARIANTS;

/* ---------- tweaks ---------- */
const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "accent": "#ffffff",
  "mode": "light",
  "density": "comfortable",
  "showLabels": true
}/*EDITMODE-END*/;

const ACCENT_OPTIONS = ["#ffffff", "#FF0080", "#0070F3", "#50E3C2"];

/* ---------- shared bits ---------- */
const ArrowR = ({ size = 11, sw = 2 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={sw}>
    <path d="M5 12h14M13 5l7 7-7 7"/>
  </svg>
);
const Chev = ({ dir = "down", size = 10 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
    {dir === "down" && <path d="M6 9l6 6 6-6"/>}
    {dir === "right" && <path d="M9 6l6 6-6 6"/>}
  </svg>
);

/* shared sparkle path — used for the brand mark and the Technique filter chips.
   It's a 4-point star with 8 path vertices: the visual rhyme for “eight” —
   and the most iconic shape in the library itself. */
const SPARKLE_22 = "M11 1 L13.2 8.8 L21 11 L13.2 13.2 L11 21 L8.8 13.2 L1 11 L8.8 8.8 Z";
const SPARKLE_16 = "M8 1 L9.6 6.4 L15 8 L9.6 9.6 L8 15 L6.4 9.6 L1 8 L6.4 6.4 Z";
const SPARKLE_16_SM = "M12.5 1.5 L13.2 4.2 L15.8 5 L13.2 5.8 L12.5 8.5 L11.8 5.8 L9.2 5 L11.8 4.2 Z";

const Wordmark = () => (
  <a className="brand" href="/">
    <svg className="mark-svg" viewBox="0 0 22 22" aria-hidden="true">
      <path d={SPARKLE_22} fill="currentColor"/>
    </svg>
    <span>eight<span className="domain">.design</span></span>
  </a>
);

/* ---------- TOP NAV ---------- */
function TopNav() {
  const [megaOpen, setMegaOpen] = useState(null);
  return (
    <header className="topbar">
      <div className="topbar-inner">
        <div className="topbar-l">
          <Wordmark/>
        </div>

        <nav className="nav-links" aria-label="Primary">
          <button
            className={`nav-link ${megaOpen === 'apps' ? 'is-open' : ''}`}
            onMouseEnter={() => setMegaOpen('apps')}
            onMouseLeave={() => setMegaOpen(null)}>
            Apps <Chev/>
          </button>
          <button
            className={`nav-link ${megaOpen === 'browse' ? 'is-open' : ''}`}
            onMouseEnter={() => setMegaOpen('browse')}
            onMouseLeave={() => setMegaOpen(null)}>
            Browse <Chev/>
          </button>
          <button
            className={`nav-link ${megaOpen === 'ai' ? 'is-open' : ''}`}
            onMouseEnter={() => setMegaOpen('ai')}
            onMouseLeave={() => setMegaOpen(null)}>
            AI generators <Chev/>
          </button>
          <a className="nav-link" href="#">
            Order custom icons
            <span className="nav-pill">New</span>
            <Chev/>
          </a>
          <a className="nav-link" href="#">Collections</a>
          <a className="nav-link" href="#">Forum</a>
          <a className="nav-link" href="#">Pricing</a>
        </nav>

        <div className="topbar-r">
          <button className="products-btn">
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
              <rect x="3" y="3" width="7" height="7" rx="1"/>
              <rect x="14" y="3" width="7" height="7" rx="1"/>
              <rect x="3" y="14" width="7" height="7" rx="1"/>
              <rect x="14" y="14" width="7" height="7" rx="1"/>
            </svg>
            Products
            <Chev/>
          </button>
          <button className="locale-btn">
            <span className="locale-flag" aria-label="English (US)">EN</span>
            <Chev/>
          </button>
          <button className="avatar">I</button>
        </div>
      </div>
    </header>
  );
}

/* ---------- MODE RAIL (Filters / Categories / Collections) ---------- */
function ModeRail({ mode, setMode }) {
  const items = [
    { id: 'filters',     icon: (
      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7">
        <path d="M4 6h16M7 12h10M10 18h4"/>
      </svg>
    ), label: 'Filters' },
    { id: 'categories',  icon: (
      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7">
        <path d="M3 6h2M8 6h13M3 12h2M8 12h13M3 18h2M8 18h13"/>
      </svg>
    ), label: 'Categories' },
    { id: 'collections', icon: (
      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7">
        <path d="M19 21l-7-5-7 5V5a2 2 0 012-2h10a2 2 0 012 2z"/>
      </svg>
    ), label: 'Collections' },
  ];

  return (
    <aside className="rail">
      {items.map(it => (
        <button
          key={it.id}
          className={`rail-btn ${mode === it.id ? 'is-on' : ''}`}
          onClick={() => setMode(it.id)}>
          <span className="rail-ico">{it.icon}</span>
          <span className="rail-lbl">{it.label}</span>
        </button>
      ))}

      <button className="rail-btn rail-bottom">
        <span className="rail-ico">
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7">
            <path d="M3 11l18-8-8 18-2.5-7.5z"/>
          </svg>
        </span>
        <span className="rail-lbl">Request icon</span>
      </button>
      <button className="rail-btn">
        <span className="rail-ico">
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7">
            <circle cx="12" cy="12" r="9"/>
            <path d="M12 17v.01M9.5 9a2.5 2.5 0 015 0c0 1.5-2.5 2-2.5 4"/>
          </svg>
        </span>
        <span className="rail-lbl">Help</span>
      </button>
    </aside>
  );
}

/* ---------- FILTER SIDEBAR ---------- */
function FilterChip({ label, icon, active, onClick }) {
  return (
    <button className={`fchip ${active ? 'is-on' : ''}`} onClick={onClick}>
      {icon && <span className="fchip-ico">{icon}</span>}
      <span>{label}</span>
    </button>
  );
}

function FilterSection({ title, children }) {
  return (
    <div className="fsection">
      <div className="fsection-h">{title}</div>
      <div className="fsection-body">{children}</div>
    </div>
  );
}

function FilterSidebar({ filters, toggle }) {
  const is = (k, v) => (filters[k] || []).includes(v);

  return (
    <aside className="fsidebar">
      <div className="fsidebar-scroll">

        {/* Style header */}
        <div className="fsection style-section">
          <div className="fsection-h fsection-h--row">
            <span>Style</span>
            <a className="link-pill" href="#">Order custom icons</a>
          </div>
          <button className="style-all">
            <span className="style-all-emoji">
              <span className="emoji-tile">
                <svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
                  <path d={SPARKLE_16} fill="currentColor"/>
                </svg>
              </span>
              <span className="emoji-tile alt">
                <svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
                  <defs>
                    <linearGradient id="style-all-grad" x1="0" y1="0" x2="0" y2="1">
                      <stop offset="0" stopColor="#ff5e9c"/>
                      <stop offset="1" stopColor="#5e7eff"/>
                    </linearGradient>
                  </defs>
                  <path d={SPARKLE_16} fill="url(#style-all-grad)"/>
                </svg>
              </span>
            </span>
            <span className="style-all-label">All style packs</span>
            <Chev dir="right" size={11}/>
          </button>
        </div>

        {/* Animation */}
        <FilterSection title="Animation">
          <FilterChip label="Static"   active={is('animation','static')}   onClick={() => toggle('animation','static')} />
          <FilterChip label="Animated" active={is('animation','animated')} onClick={() => toggle('animation','animated')} />
        </FilterSection>

        {/* OS */}
        <FilterSection title="OS">
          <FilterChip
            label="Android"
            active={is('os','android')}
            onClick={() => toggle('os','android')}
            icon={<span dangerouslySetInnerHTML={{__html: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><path fill="#7cb342" d="M12 29c0 1.1-.9 2-2 2s-2-.9-2-2v-9c0-1.1.9-2 2-2s2 .9 2 2V29zM40 29c0 1.1-.9 2-2 2s-2-.9-2-2v-9c0-1.1.9-2 2-2s2 .9 2 2V29zM22 40c0 1.1-.9 2-2 2s-2-.9-2-2v-9c0-1.1.9-2 2-2s2 .9 2 2V40zM30 40c0 1.1-.9 2-2 2s-2-.9-2-2v-9c0-1.1.9-2 2-2s2 .9 2 2V40z"/><path fill="#7cb342" d="M14 18v15c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V18H14zM24 8c-6 0-9.7 3.6-10 8h20C33.7 11.6 30 8 24 8zM20 13.6c-.6 0-1-.4-1-1 0-.6.4-1 1-1s1 .4 1 1C21 13.1 20.6 13.6 20 13.6zM28 13.6c-.6 0-1-.4-1-1 0-.6.4-1 1-1s1 .4 1 1C29 13.1 28.6 13.6 28 13.6z"/><path fill="#7cb342" d="M28.3 10.5c-.2 0-.4-.1-.6-.2-.5-.3-.6-.9-.3-1.4l1.7-2.5c.3-.5.9-.6 1.4-.3.5.3.6.9.3 1.4l-1.7 2.5C29 10.3 28.7 10.5 28.3 10.5zM19.3 10.1c-.3 0-.7-.2-.8-.5l-1.3-2.1c-.3-.5-.2-1.1.3-1.4.5-.3 1.1-.2 1.4.3l1.3 2.1c.3.5.2 1.1-.3 1.4C19.7 10 19.5 10.1 19.3 10.1z"/></svg>`}}/>}
          />
          <FilterChip
            label="iOS"
            active={is('os','ios')}
            onClick={() => toggle('os','ios')}
            icon={<span dangerouslySetInnerHTML={{__html: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><path fill="#42A5F5" d="M40.084,32.613c-0.848,1.835-1.254,2.655-2.342,4.274c-1.521,2.264-3.67,5.089-6.326,5.109c-2.361,0.018-2.971-1.507-6.176-1.482c-3.204,0.016-3.872,1.51-6.237,1.484c-2.654-0.022-4.688-2.568-6.21-4.826c-4.259-6.34-4.707-13.768-2.076-17.721c1.861-2.803,4.807-4.449,7.572-4.449c2.817,0,4.588,1.514,6.916,1.514c2.262,0,3.638-1.517,6.896-1.517c2.464,0,5.07,1.313,6.931,3.575C32.942,21.836,33.931,30.337,40.084,32.613z"/><path fill="#42A5F5" d="M30.046,12.072c1.269-1.577,2.232-3.804,1.882-6.072c-2.069,0.138-4.491,1.418-5.905,3.075c-1.282,1.51-2.345,3.752-1.931,5.922C26.351,15.066,28.689,13.764,30.046,12.072z"/><path fill="#1E88E5" d="M36.736,20.421C28,30.001,20,21.001,9.228,27.842c0.375,3.027,1.53,6.303,3.565,9.331c1.521,2.258,3.556,4.804,6.21,4.826c2.365,0.025,3.033-1.469,6.237-1.484c3.205-0.024,3.814,1.5,6.176,1.482c2.656-0.021,4.805-2.846,6.326-5.109c1.088-1.619,1.494-2.439,2.342-4.274C34.878,30.688,33.389,24.314,36.736,20.421z"/></svg>`}}/>}
          />
          <FilterChip
            label="Windows"
            active={is('os','windows')}
            onClick={() => toggle('os','windows')}
            icon={<span dangerouslySetInnerHTML={{__html: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><path fill="#1976d2" d="M6,6h17v17H6V6z"/><path fill="#1976d2" d="M25.042,22.958V6H42v16.958H25.042z"/><path fill="#1976d2" d="M6,25h17v17H6V25z"/><path fill="#1976d2" d="M25,42V25h17v17H25z"/></svg>`}}/>}
          />
          <FilterChip
            label="macOS"
            active={is('os','macos')}
            onClick={() => toggle('os','macos')}
            icon={<span dangerouslySetInnerHTML={{__html: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><path fill="#42A5F5" d="M40.084,32.613c-0.848,1.835-1.254,2.655-2.342,4.274c-1.521,2.264-3.67,5.089-6.326,5.109c-2.361,0.018-2.971-1.507-6.176-1.482c-3.204,0.016-3.872,1.51-6.237,1.484c-2.654-0.022-4.688-2.568-6.21-4.826c-4.259-6.34-4.707-13.768-2.076-17.721c1.861-2.803,4.807-4.449,7.572-4.449c2.817,0,4.588,1.514,6.916,1.514c2.262,0,3.638-1.517,6.896-1.517c2.464,0,5.07,1.313,6.931,3.575C32.942,21.836,33.931,30.337,40.084,32.613z"/><path fill="#42A5F5" d="M30.046,12.072c1.269-1.577,2.232-3.804,1.882-6.072c-2.069,0.138-4.491,1.418-5.905,3.075c-1.282,1.51-2.345,3.752-1.931,5.922C26.351,15.066,28.689,13.764,30.046,12.072z"/><path fill="#1E88E5" d="M36.736,20.421C28,30.001,20,21.001,9.228,27.842c0.375,3.027,1.53,6.303,3.565,9.331c1.521,2.258,3.556,4.804,6.21,4.826c2.365,0.025,3.033-1.469,6.237-1.484c3.205-0.024,3.814,1.5,6.176,1.482c2.656-0.021,4.805-2.846,6.326-5.109c1.088-1.619,1.494-2.439,2.342-4.274C34.878,30.688,33.389,24.314,36.736,20.421z"/></svg>`}}/>}
          />
        </FilterSection>

        {/* Technique */}
        <FilterSection title="Technique">
          <FilterChip
            label="3D"
            active={is('tech','3d')}
            onClick={() => toggle('tech','3d')}
            icon={<span dangerouslySetInnerHTML={{__html: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><path fill="#f2f2f2" stroke="#40396e" stroke-width="3" d="M62.008 36.769l9.15 3.386c3.132 1.159 3.132 5.588 0 6.747l-9.15 3.386c-5.44 2.013-9.729 6.302-11.742 11.742l-3.386 9.15c-1.159 3.132-5.588 3.132-6.747 0l-3.386-9.15c-2.013-5.44-6.302-9.729-11.742-11.742l-9.15-3.386c-3.132-1.159-3.132-5.588 0-6.747l9.15-3.386c5.44-2.013 9.729-6.302 11.742-11.742l3.386-9.15c1.159-3.132 5.588-3.132 6.747 0l3.386 9.15C52.279 30.466 56.568 34.756 62.008 36.769z"/><path fill="#f2f2f2" stroke="#40396e" stroke-width="3" d="M74.785 65.306l-1.391-3.84c-0.471-1.302-2.312-1.302-2.783 0l-1.391 3.84c-0.826 2.282-2.623 4.079-4.905 4.905l-3.84 1.391c-1.302 0.471-1.302 2.312 0 2.783l3.84 1.391c2.282 0.826 4.079 2.623 4.905 4.905l1.391 3.84c0.471 1.302 2.312 1.302 2.783 0l1.391-3.84c0.826-2.282 2.623-4.079 4.905-4.905l3.84-1.391c1.302-0.471 1.302-2.312 0-2.783l-3.84-1.391C77.408 69.384 75.611 67.588 74.785 65.306z"/></svg>`}}/>}
          />
          <FilterChip
            label="Filled"
            active={is('tech','filled')}
            onClick={() => toggle('tech','filled')}
            icon={<span dangerouslySetInnerHTML={{__html: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="currentColor" d="M10.764 2.002l1.749 4.022c.506.506.958.958 1.464 1.464l4.022 1.749c0 .728 0 .799 0 1.527l-4.022 1.749c-.506.506-.958.958-1.464 1.464l-1.749 4.022c-.728 0-.799 0-1.527 0l-1.749-4.022c-.506-.506-.958-.958-1.464-1.464l-4.022-1.749c0-.728 0-.799 0-1.527l4.022-1.749c.506-.506.958-.958 1.464-1.464l1.749-4.022C9.964 2.002 10.036 2.002 10.764 2.002zM18.38 14.031l.796 1.83c.333.333.63.63.963.963l1.83.796c0 .362 0 .398 0 .76l-1.83.796c-.333.333-.63.63-.963.963l-.796 1.83c-.362 0-.398 0-.76 0l-.796-1.83c-.333-.333-.63-.63-.963-.963l-1.83-.796c0-.362 0-.398 0-.76l1.83-.796c.333-.333.63-.63.963-.963l.796-1.83C17.982 14.031 18.018 14.031 18.38 14.031z"/></svg>`}}/>}
          />
          <FilterChip
            label="Line"
            active={is('tech','line')}
            onClick={() => toggle('tech','line')}
            icon={<span dangerouslySetInnerHTML={{__html: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M17.469 9.286l-2.156-.957c-1.657-.736-2.976-2.096-3.683-3.801L10.81 2.554C10.658 2.187 10.329 2.004 10 2.004S9.342 2.187 9.19 2.554L8.371 4.528C7.663 6.232 6.345 7.593 4.687 8.329L2.531 9.286c-.708.314-.708 1.344 0 1.658l2.226.988c1.616.717 2.911 2.029 3.631 3.678l.809 1.852c.155.356.479.534.803.534s.648-.178.804-.534l.809-1.852c.72-1.648 2.015-2.961 3.631-3.678l2.226-.988C18.177 10.63 18.177 9.6 17.469 9.286zM10 14.34c-.949-1.882-2.497-3.37-4.408-4.225C7.523 9.231 9.07 7.706 10 5.78c.93 1.926 2.477 3.451 4.408 4.334C12.497 10.969 10.949 12.458 10 14.34z"/></svg>`}}/>}
          />
          <FilterChip
            label="Hand-drawn"
            active={is('tech','hand')}
            onClick={() => toggle('tech','hand')}
            icon={<span dangerouslySetInnerHTML={{__html: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="6" d="M10.615 38.486s14.529-5.973 18.244-10.183c3.441-3.898 8.203-16.542 9.759-18.81 0 0 .976-1.308 2.687-1.131 2.115.218 2.687.707 3.536 2.121.849 1.414 4.667 14.567 10.607 18.669s13.719 7.637 16.123 8.769c2.404 1.131 2.798 2.569 2.851 3.568.056 1.065-.669 2.451-3.008 3.51-3.625 1.64-11.922 4.791-16.389 8.904-4.55 4.189-8.769 13.86-10.466 17.537-1.697 3.677-5.177 2.918-5.94 1.273-.48-1.034-5.885-13.309-9.051-17.396-3.333-4.302-12.804-8.87-17.537-10.183C8.622 44.188 7.786 40.324 10.615 38.486z"/><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="6" d="M77.987 61.989s-1.894-3.788-4.012-.026c-.518.921-2.052 5.759-4.456 7.881s-4.234 2.785-7.019 3.788c-2.571.925-2.857 3.902-.39 4.735 1.919.648 6.633 2.689 8.33 4.81 1.697 2.121 2.525 4.557 3.091 5.83.566 1.273 2.592 2.82 4.29.557 1.697-2.263 2.674-5.571 4.5-7.377 2.179-2.156 5.516-3.677 7.213-4.101 1.992-.499 2.326-2.95.141-3.96-1.838-.85-5.667-2.391-7.787-4.783C79.714 66.891 77.987 61.989 77.987 61.989z"/></svg>`}}/>}
          />
        </FilterSection>

        {/* Colors */}
        <FilterSection title="Colors">
          <FilterChip
            label="Multi"
            active={is('color','multi')}
            onClick={() => toggle('color','multi')}
            icon={<span className="circle-dot multi"/>}
          />
          <FilterChip
            label="Duo"
            active={is('color','duo')}
            onClick={() => toggle('color','duo')}
            icon={<span className="circle-dot duo"/>}
          />
          <FilterChip
            label="Mono"
            active={is('color','mono')}
            onClick={() => toggle('color','mono')}
            icon={<span className="circle-dot mono"/>}
          />
          <FilterChip
            label="Gradient"
            active={is('color','gradient')}
            onClick={() => toggle('color','gradient')}
            icon={<span className="circle-dot grad"/>}
          />
        </FilterSection>

        {/* Corners */}
        <FilterSection title="Corners">
          <FilterChip
            label="Round"
            active={is('corners','round')}
            onClick={() => toggle('corners','round')}
            icon={<span className="corner-ico round"/>}
          />
          <FilterChip
            label="Sharp"
            active={is('corners','sharp')}
            onClick={() => toggle('corners','sharp')}
            icon={<span className="corner-ico sharp"/>}
          />
        </FilterSection>

        {/* Filter Size — large tiles */}
        <div className="fsection">
          <div className="fsection-h">Filter size</div>
          <div className="size-row">
            {['small','medium','large'].map(s => (
              <button
                key={s}
                className={`size-tile ${is('size', s) ? 'is-on' : ''}`}
                onClick={() => toggle('size', s)}>
                <span className={`size-ico size-${s}`} dangerouslySetInnerHTML={{__html: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" fill="currentColor"><path d="M 32 3 L 1 28 L 1.4921875 28.654297 C 2.8591875 30.477297 5.4694688 30.791703 7.2304688 29.345703 L 32 9 L 56.769531 29.345703 C 58.530531 30.791703 61.140812 30.477297 62.507812 28.654297 L 63 28 L 54 20.742188 L 54 8 L 45 8 L 45 13.484375 L 32 3 z M 32 13 L 8 32 L 8 56 L 56 56 L 56 35 L 32 13 z M 26 34 L 38 34 L 38 52 L 26 52 L 26 34 z"/></svg>`}}/>
                <span>{s[0].toUpperCase() + s.slice(1)}</span>
              </button>
            ))}
          </div>
        </div>

        {/* Aesthetic */}
        <FilterSection title="Aesthetic">
          <FilterChip label="Professional" active={is('aes','pro')}    onClick={() => toggle('aes','pro')} />
          <FilterChip label="Decorative"   active={is('aes','deco')}   onClick={() => toggle('aes','deco')} />
          <FilterChip label="Games"        active={is('aes','games')}  onClick={() => toggle('aes','games')} />
          <FilterChip label="Classic"      active={is('aes','classic')} onClick={() => toggle('aes','classic')} />
          <FilterChip label="Kids"         active={is('aes','kids')}   onClick={() => toggle('aes','kids')} />
        </FilterSection>

        {/* Style */}
        <FilterSection title="Stroke">
          <FilterChip label="Regular" active={is('stroke','regular')} onClick={() => toggle('stroke','regular')} />
          <FilterChip label="Bold"    active={is('stroke','bold')}    onClick={() => toggle('stroke','bold')} />
          <FilterChip label="Thin"    active={is('stroke','thin')}    onClick={() => toggle('stroke','thin')} />
        </FilterSection>
      </div>
    </aside>
  );
}

/* ---------- TOOL BAR (above results) ---------- */
function ToolBar({ assetType, setAssetType, query, setQuery, aiSearch, setAiSearch, view, setView, themeMode, setThemeMode }) {
  const ASSET_TYPES = ['Icons','Illustrations','Photos','Music','3D'];
  const [open, setOpen] = useState(false);

  return (
    <div className="toolbar">
      <div className="toolbar-l">
        <div className="type-select">
          <button className="type-trigger" onClick={() => setOpen(!open)}>
            <span className="type-ico">
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
                <rect x="3" y="3" width="18" height="18" rx="3"/>
                <circle cx="9" cy="9" r="2"/>
                <path d="M21 16l-5-5L5 21"/>
              </svg>
            </span>
            <span>{assetType}</span>
            <Chev/>
          </button>
          {open && (
            <div className="type-menu" onMouseLeave={() => setOpen(false)}>
              {ASSET_TYPES.map(a => (
                <button
                  key={a}
                  className={`type-item ${a === assetType ? 'is-on' : ''}`}
                  onClick={() => { setAssetType(a); setOpen(false); }}>
                  {a}
                  {a === assetType && <span className="check">✓</span>}
                </button>
              ))}
            </div>
          )}
        </div>

        <div className="search-input">
          <input
            type="text"
            value={query}
            onChange={e => setQuery(e.target.value)}
            placeholder="Search 12.8M assets…"
            autoComplete="off"
          />
          {query && (
            <button className="search-clear" onClick={() => setQuery('')}>×</button>
          )}
        </div>
      </div>

      <div className="toolbar-r">
        <label className="ai-toggle">
          <span
            className={`toggle-swatch ${aiSearch ? 'is-on' : ''}`}
            onClick={() => setAiSearch(!aiSearch)}>
            <span className="toggle-knob"></span>
          </span>
          <span>AI search</span>
          <button className="ai-config">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7">
              <line x1="4" y1="6" x2="20" y2="6"/>
              <line x1="4" y1="12" x2="20" y2="12"/>
              <line x1="4" y1="18" x2="20" y2="18"/>
              <circle cx="9" cy="6" r="2" fill="currentColor"/>
              <circle cx="14" cy="12" r="2" fill="currentColor"/>
              <circle cx="7" cy="18" r="2" fill="currentColor"/>
            </svg>
          </button>
        </label>

        <button className="recolor-btn">
          <span className="recolor-orb"></span>
          <span>Recolor</span>
          <Chev/>
        </button>

        <button
          className="theme-btn"
          onClick={() => setThemeMode(themeMode === 'dark' ? 'light' : 'dark')}
          aria-label={`Switch to ${themeMode === 'dark' ? 'light' : 'dark'} canvas`}
          title={`Switch to ${themeMode === 'dark' ? 'light' : 'dark'} canvas`}>
          {themeMode === 'dark' ? (
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
              <circle cx="12" cy="12" r="4"/>
              <path d="M12 2v2M12 20v2M4 12H2M22 12h-2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"/>
            </svg>
          ) : (
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
              <path d="M21 13A9 9 0 1111 3a7 7 0 0010 10z"/>
            </svg>
          )}
        </button>

        <div className="view-toggle">
          <button
            className={view === 'grid' ? 'is-on' : ''}
            onClick={() => setView('grid')}
            aria-label="Grid view">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7">
              <rect x="3" y="3" width="8" height="8" rx="1"/>
              <rect x="13" y="3" width="8" height="8" rx="1"/>
              <rect x="3" y="13" width="8" height="8" rx="1"/>
              <rect x="13" y="13" width="8" height="8" rx="1"/>
            </svg>
          </button>
          <button
            className={view === 'compact' ? 'is-on' : ''}
            onClick={() => setView('compact')}
            aria-label="Compact view">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7">
              <circle cx="5" cy="5" r="1.5" fill="currentColor"/>
              <circle cx="12" cy="5" r="1.5" fill="currentColor"/>
              <circle cx="19" cy="5" r="1.5" fill="currentColor"/>
              <circle cx="5" cy="12" r="1.5" fill="currentColor"/>
              <circle cx="12" cy="12" r="1.5" fill="currentColor"/>
              <circle cx="19" cy="12" r="1.5" fill="currentColor"/>
              <circle cx="5" cy="19" r="1.5" fill="currentColor"/>
              <circle cx="12" cy="19" r="1.5" fill="currentColor"/>
              <circle cx="19" cy="19" r="1.5" fill="currentColor"/>
            </svg>
          </button>
        </div>
      </div>
    </div>
  );
}

/* ---------- RESULTS HEADER (count + sort) ---------- */
function ResultsHeader({ count, query, sort, setSort }) {
  return (
    <div className="results-head">
      <div className="results-h-l">
        <span className="results-count">{count.toLocaleString()}</span>
        <span className="results-dim">
          matches for <b>"{query}"</b> · across 30+ style packs
        </span>
      </div>
      <div className="results-h-r">
        <button className="sort-btn">
          <span>Sort: <b>{sort}</b></span>
          <Chev/>
        </button>
      </div>
    </div>
  );
}

/* ---------- ICON GRID ---------- */
function IconGrid({ query, selectedId, setSelectedId, view, t }) {
  // duplicate the variant set to fill the grid
  const rows = useMemo(() => {
    const out = [];
    for (let i = 0; i < 3; i++) {
      ICON_VARIANTS.forEach((v, idx) => {
        out.push({ ...v, key: `${i}-${idx}` });
      });
    }
    return out.slice(0, 90);
  }, []);

  // grid view wins when it differs from the comfy default; otherwise fall back to the tweak
  const density = view === 'compact' ? 'dense' : t.density;

  return (
    <div className={`igrid d-${density} v-${view}`}>
      {rows.map((icon, idx) => (
        <div
          key={icon.key}
          role="button"
          tabIndex={0}
          className={`icell ${icon.tile ? `tile-${icon.tile}` : ''} ${selectedId === idx ? 'is-selected' : ''}`}
          onClick={() => setSelectedId(idx)}
          onKeyDown={e => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); setSelectedId(idx); } }}
          aria-label={query.toUpperCase()}>
          <span className="icell-art" dangerouslySetInnerHTML={{__html: icon.raw || ''}}/>
          {t.showLabels && <span className="icell-lbl">{query || 'AI'}</span>}
          <span className="icell-actions">
            <button title="Copy SVG" onClick={e => e.stopPropagation()}>
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><rect x="8" y="8" width="13" height="13" rx="2"/><path d="M16 8V6a2 2 0 00-2-2H6a2 2 0 00-2 2v8a2 2 0 002 2h2"/></svg>
            </button>
            <button title="Download" onClick={e => e.stopPropagation()}>
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M12 3v12m-5-5l5 5 5-5M4 21h16"/></svg>
            </button>
          </span>
        </div>
      ))}
    </div>
  );
}

/* ---------- TWEAKS ---------- */
function MyTweaks({ t, setTweak }) {
  return (
    <TweaksPanel title="Tweaks">
      <TweakSection title="Look">
        <TweakColor
          label="Accent"
          value={t.accent}
          options={ACCENT_OPTIONS}
          onChange={v => setTweak('accent', v)}
        />
        <TweakRadio
          label="Mode"
          value={t.mode}
          options={[{value:'dark',label:'Dark'},{value:'light',label:'Light'}]}
          onChange={v => setTweak('mode', v)}
        />
      </TweakSection>
      <TweakSection title="Grid">
        <TweakRadio
          label="Density"
          value={t.density}
          options={[
            { value: 'comfortable', label: 'Comfy' },
            { value: 'dense',       label: 'Dense' },
          ]}
          onChange={v => setTweak('density', v)}
        />
        <TweakToggle
          label="Show labels"
          value={t.showLabels}
          onChange={v => setTweak('showLabels', v)}
        />
      </TweakSection>
    </TweaksPanel>
  );
}

/* ---------- ROOT ---------- */
function App() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);

  const [mode, setMode] = useState('filters');
  const [filters, setFilters] = useState({
    animation: [],
    os: [],
    tech: ['filled'],
    color: [],
    corners: [],
    size: ['medium'],
    aes: [],
    stroke: [],
  });
  const [assetType, setAssetType] = useState('Icons');
  const [query, setQuery] = useState('ai');
  const [aiSearch, setAiSearch] = useState(false);
  const [view, setView] = useState('grid');
  const [sort, setSort] = useState('Popular');
  const [selectedId, setSelectedId] = useState(0);

  // toggle helper
  const toggle = (key, value) => {
    setFilters(prev => {
      const cur = prev[key] || [];
      const next = cur.includes(value)
        ? cur.filter(v => v !== value)
        : [...cur, value];
      return { ...prev, [key]: next };
    });
  };

  // wire tweaks → document
  useEffect(() => {
    const root = document.documentElement;
    root.style.setProperty('--accent', t.accent);
    root.setAttribute('data-mode', t.mode);
  }, [t.accent, t.mode]);

  return (
    <>
      <TopNav/>
      <div className="app-body">
        <ModeRail mode={mode} setMode={setMode}/>
        {mode === 'filters' && <FilterSidebar filters={filters} toggle={toggle}/>}
        {mode === 'categories' && (
          <aside className="fsidebar">
            <div className="fsidebar-scroll">
              <div className="fsection">
                <div className="fsection-h">Categories</div>
                <div className="fsection-body cat-list">
                  {['Web', 'Mobile', 'Brands', 'Business', 'Education', 'Healthcare', 'Photo &amp; video', 'Travel', 'Sports', 'Food', 'Weather', 'Animals'].map(c => (
                    <a key={c} href="#" className="cat-link" dangerouslySetInnerHTML={{__html:c}}/>
                  ))}
                </div>
              </div>
            </div>
          </aside>
        )}
        {mode === 'collections' && (
          <aside className="fsidebar">
            <div className="fsidebar-scroll">
              <div className="fsection">
                <div className="fsection-h">Your collections</div>
                <div className="fsection-body cat-list">
                  <a className="cat-link" href="#">Onboarding ✦ <span className="dim">42</span></a>
                  <a className="cat-link" href="#">Marketing site <span className="dim">128</span></a>
                  <a className="cat-link" href="#">Settings ⚙ <span className="dim">17</span></a>
                  <a className="cat-link" href="#">+ New collection</a>
                </div>
              </div>
            </div>
          </aside>
        )}

        <main className="app-main">
          <ToolBar
            assetType={assetType} setAssetType={setAssetType}
            query={query} setQuery={setQuery}
            aiSearch={aiSearch} setAiSearch={setAiSearch}
            view={view} setView={setView}
            themeMode={t.mode} setThemeMode={v => setTweak('mode', v)}
          />
          <ResultsHeader
            count={12847}
            query={query.toUpperCase() || 'AI'}
            sort={sort} setSort={setSort}
          />
          <IconGrid
            query={query.toUpperCase() || 'AI'}
            selectedId={selectedId} setSelectedId={setSelectedId}
            view={view}
            t={t}
          />
          <div className="grid-foot">
            <span>Showing 90 of 12,847</span>
            <button className="load-more">
              Load more
              <ArrowR/>
            </button>
          </div>
        </main>
      </div>
      <MyTweaks t={t} setTweak={setTweak}/>
    </>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<App/>);
