/* ───────────────────────────────────────────────────────────
   icons.jsx — garment silhouettes, category marks, UI glyphs
   Exports to window: CatIcon, GarmentSwatch, Icon (named UI glyphs)
   ─────────────────────────────────────────────────────────── */

/* category silhouettes — single-path, currentColor stroke */
function CatIcon({ id='top', size=28, color='currentColor', sw=1.6 }) {
  const P = { fill:'none', stroke:color, strokeWidth:sw, strokeLinejoin:'round', strokeLinecap:'round' };
  const paths = {
    top:(<g {...P}><path d="M8 5l4 2 4-2 4 2.5-2.2 3.2-1.3-.7V20H7.5V9.3l-1.3.7L4 6.8 8 5z"/></g>),
    bottom:(<g {...P}><path d="M8 4h8l.7 16h-3.2L12 9l-1.5 11H7.3L8 4z"/></g>),
    capi:(<g {...P}><path d="M8 4l4 2 4-2 4 3-2.2 3-1.3-.8V21H7.5V9.2l-1.3.8L4 7l4-3z"/><path d="M12 6v13"/></g>),
    abiti:(<g {...P}><path d="M9 4h6l1 4-2 1.5L15 21H9l1-11.5L8 8l1-4z"/></g>),
    scarpe:(<g {...P}><path d="M4 13c2 .4 3.4.2 4.4-1.2C9.2 10.6 9.6 9 11 9c1.2 0 1.4 1.6 2.6 2.4 1.6 1 4.4 1.1 6.4 1.6 1 .25 1.2 1.2 1 2H4.3C4 14.4 4 13.7 4 13z"/></g>),
    access:(<g {...P}><path d="M7 9h10l1.4 10.5a1 1 0 01-1 1.1H6.6a1 1 0 01-1-1.1L7 9z"/><path d="M9.3 9V7.4a2.7 2.7 0 015.4 0V9"/></g>),
  };
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" style={{display:'block'}}>
      {paths[id]||paths.top}
    </svg>
  );
}

/* a clothing item rendered as an editorial fabric swatch + silhouette,
   or an uploaded photo if item.photo is set */
function GarmentSwatch({ item, size=56, radius=14, ring=false }) {
  const color = item?.color || '#56657a';
  const cat = item?.category || 'top';
  return (
    <div style={{
      width:size, height:size, borderRadius:radius, position:'relative',
      overflow:'hidden', flexShrink:0,
      background: item?.photo ? '#ddd' :
        `linear-gradient(150deg, ${color} 0%, ${shade(color,-18)} 100%)`,
      boxShadow: ring ? '0 0 0 3px rgba(255,255,255,.9), 0 4px 14px rgba(0,34,85,.18)' : 'inset 0 0 0 1px rgba(255,255,255,.10)',
    }}>
      {item?.photo
        ? <img src={item.photo} alt="" style={{width:'100%',height:'100%',objectFit:'cover'}}/>
        : (<>
            <div style={{position:'absolute', inset:0, display:'flex', alignItems:'center', justifyContent:'center'}}>
              <CatIcon id={cat} size={size*0.5} color="rgba(255,255,255,.92)" sw={1.5}/>
            </div>
            {/* soft fabric sheen */}
            <div style={{position:'absolute', inset:0, background:'linear-gradient(125deg, rgba(255,255,255,.18) 0%, transparent 42%)'}}/>
          </>)
      }
    </div>
  );
}

/* lighten/darken a hex color */
function shade(hex, amt){
  const h=hex.replace('#',''); if(h.length!==6) return hex;
  const n=parseInt(h,16);
  let r=(n>>16)+amt, g=((n>>8)&255)+amt, b=(n&255)+amt;
  r=Math.max(0,Math.min(255,r)); g=Math.max(0,Math.min(255,g)); b=Math.max(0,Math.min(255,b));
  return '#'+((r<<16)|(g<<8)|b).toString(16).padStart(6,'0');
}

/* ---- UI glyph set ---- */
const Icon = {
  plus:(p)=>g(p,<path d="M12 5v14M5 12h14"/>),
  close:(p)=>g(p,<path d="M6 6l12 12M18 6L6 18"/>),
  check:(p)=>g(p,<path d="M4 12.5l5 5 11-12"/>),
  chevR:(p)=>g(p,<path d="M9 5l7 7-7 7"/>),
  chevL:(p)=>g(p,<path d="M15 5l-7 7 7 7"/>),
  chevD:(p)=>g(p,<path d="M5 9l7 7 7-7"/>),
  arrowR:(p)=>g(p,<><path d="M4 12h15"/><path d="M13 6l6 6-6 6"/></>),
  clock:(p)=>g(p,<><circle cx="12" cy="12" r="8.2"/><path d="M12 7.5V12l3 2"/></>),
  bell:(p)=>g(p,<><path d="M6 16.5V11a6 6 0 1112 0v5.5l1.5 2H4.5L6 16.5z"/><path d="M10 20a2 2 0 004 0"/></>),
  home:(p)=>g(p,<><path d="M4 11l8-6.5L20 11"/><path d="M6 9.6V20h12V9.6"/></>),
  chart:(p)=>g(p,<><path d="M5 20V5"/><path d="M5 20h15"/><path d="M9 20v-6"/><path d="M13 20V9"/><path d="M17 20v-9"/></>),
  layers:(p)=>g(p,<><path d="M12 4l8 4-8 4-8-4 8-4z"/><path d="M4 12l8 4 8-4"/></>),
  camera:(p)=>g(p,<><path d="M4 8h3l1.5-2h7L17 8h3v11H4V8z"/><circle cx="12" cy="13" r="3.4"/></>),
  spark:(p)=>g(p,<path d="M12 3c.6 4.5 1.5 5.4 6 6-4.5.6-5.4 1.5-6 6-.6-4.5-1.5-5.4-6-6 4.5-.6 5.4-1.5 6-6z"/>),
  gift:(p)=>g(p,<><path d="M4 11h16v9H4z"/><path d="M4 8h16v3H4z"/><path d="M12 8v12"/><path d="M12 8c-1.2-3.4-5.3-2.6-4.5.3M12 8c1.2-3.4 5.3-2.6 4.5.3"/></>),
  flame:(p)=>g(p,<path d="M12 3c1 3 4 4 4 8a4 4 0 01-8 0c0-1.4.6-2.2 1.2-3 .3 1 .9 1.4 1.4 1.4C12 9 11 6 12 3z"/>),
  cal:(p)=>g(p,<><rect x="4" y="5.5" width="16" height="14.5" rx="2.5"/><path d="M4 9.5h16M8.5 3.5v4M15.5 3.5v4"/></>),
  undo:(p)=>g(p,<><path d="M5 9h9a5 5 0 010 10h-3"/><path d="M5 9l3.5-3.5M5 9l3.5 3.5"/></>),
  flag:(p)=>g(p,<><path d="M6 21V4"/><path d="M6 5h11l-2 3 2 3H6"/></>),
  google:(p)=>(<svg width={p?.size||20} height={p?.size||20} viewBox="0 0 24 24"><path fill="#4285F4" d="M21.6 12.2c0-.7-.06-1.4-.18-2.05H12v3.9h5.4a4.6 4.6 0 01-2 3v2.5h3.24c1.9-1.75 3-4.32 3-7.35z"/><path fill="#34A853" d="M12 22c2.7 0 4.96-.9 6.62-2.42l-3.24-2.5c-.9.6-2.05.95-3.38.95-2.6 0-4.8-1.75-5.6-4.12H3.06v2.58A10 10 0 0012 22z"/><path fill="#FBBC05" d="M6.4 13.9a6 6 0 010-3.8V7.5H3.06a10 10 0 000 9l3.34-2.6z"/><path fill="#EA4335" d="M12 6.07c1.47 0 2.78.5 3.82 1.5l2.85-2.85A10 10 0 003.06 7.5L6.4 10.1C7.2 7.8 9.4 6.07 12 6.07z"/></svg>),
  apple:(p)=>(<svg width={p?.size||20} height={p?.size||20} viewBox="0 0 24 24"><path fill={p?.color||'#000'} d="M16.4 12.7c0-2.3 1.9-3.4 2-3.5-1.1-1.6-2.8-1.8-3.4-1.8-1.4-.15-2.8.85-3.5.85-.7 0-1.85-.83-3-.8-1.55.02-3 .9-3.8 2.3-1.62 2.8-.42 7 1.16 9.3.77 1.12 1.7 2.38 2.9 2.34 1.16-.05 1.6-.75 3-.75s1.8.75 3 .73c1.24-.02 2.02-1.14 2.78-2.27.87-1.3 1.23-2.56 1.25-2.62-.03-.01-2.4-.92-2.42-3.66zM14.1 5.8c.64-.78 1.07-1.86.95-2.94-.92.04-2.03.61-2.69 1.39-.59.69-1.1 1.79-.96 2.85 1.02.08 2.06-.52 2.7-1.3z"/></svg>),
  ms:(p)=>(<svg width={p?.size||20} height={p?.size||20} viewBox="0 0 24 24"><rect x="3" y="3" width="8.4" height="8.4" fill="#F25022"/><rect x="12.6" y="3" width="8.4" height="8.4" fill="#7FBA00"/><rect x="3" y="12.6" width="8.4" height="8.4" fill="#00A4EF"/><rect x="12.6" y="12.6" width="8.4" height="8.4" fill="#FFB900"/></svg>),
};
function g(p, kids){
  const size=p?.size||22, color=p?.color||'currentColor', sw=p?.sw||1.8;
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
         stroke={color} strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round"
         style={{display:'block', ...(p?.style||{})}}>
      {kids}
    </svg>
  );
}

Object.assign(window, { CatIcon, GarmentSwatch, shade, Icon });
