// Simple stroke icons. Each is a React component that accepts `s` (size) prop.
function makeIcon(defaultSize, body) {
  return function Icon(props) {
    const s = (props && typeof props.s === 'number') ? props.s : defaultSize;
    return body(s);
  };
}

const Ic = {
  search: makeIcon(18, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><circle cx="11" cy="11" r="7"/><path d="m21 21-4.3-4.3"/></svg>),
  back: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="m15 18-6-6 6-6"/></svg>),
  fwd: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="m9 18 6-6-6-6"/></svg>),
  reload: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M3 12a9 9 0 0 1 15-6.7L21 8"/><path d="M21 3v5h-5"/><path d="M21 12a9 9 0 0 1-15 6.7L3 16"/><path d="M3 21v-5h5"/></svg>),
  panel: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="4" width="18" height="16" rx="2"/><path d="M9 4v16"/></svg>),
  split: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="4" width="18" height="16" rx="2"/><path d="M12 4v16"/></svg>),
  more: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="5" r="1"/><circle cx="12" cy="12" r="1"/><circle cx="12" cy="19" r="1"/></svg>),
  plus: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M12 5v14M5 12h14"/></svg>),
  x: makeIcon(14, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M18 6 6 18M6 6l12 12"/></svg>),
  star: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="m12 2 3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/></svg>),
  bookmark: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M19 21l-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z"/></svg>),
  history: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M3 12a9 9 0 1 0 3-6.7L3 8"/><path d="M3 3v5h5"/><path d="M12 7v5l3 2"/></svg>),
  tabs: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="6" width="18" height="14" rx="2"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>),
  folder: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/></svg>),
  settings: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><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 9 19.4a1.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 4.6 9a1.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.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>),
  grid: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6"><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>),
  arrowRight: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M5 12h14M13 5l7 7-7 7"/></svg>),
  sun: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/></svg>),
  moon: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8Z"/></svg>),
  cloud: makeIcon(20, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round"><path d="M17.5 19a4.5 4.5 0 1 0-1.3-8.8 6 6 0 1 0-11.7 1.7A4 4 0 0 0 6 19z"/></svg>),
  pin: makeIcon(14, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M12 17v5M9 3h6l-1 6 3 3H7l3-3-1-6z"/></svg>),
  bolt: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M13 2 4 14h7l-1 8 9-12h-7z"/></svg>),
  sparkle: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M12 3v4M12 17v4M3 12h4M17 12h4M5.6 5.6l2.8 2.8M15.6 15.6l2.8 2.8M5.6 18.4l2.8-2.8M15.6 8.4l2.8-2.8"/></svg>),
  chat: makeIcon(16, s => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>),
};

// Brand mark — Ajna logo (concentric overlapping arcs)
function AjnaMark({ size = 36 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 168 168" fill="none" xmlns="http://www.w3.org/2000/svg">
      <path d="M167.383 91.1602C167.383 133.134 133.356 167.16 91.3828 167.16H72.3154C111.035 163.653 141.372 131.106 141.372 91.4736V0L167.383 0V91.1602Z" fill="#1F67ED"/>
      <path d="M141.059 51.2676L76.0938 51.2676C34.4827 51.2676 0.682443 84.7086 0.101563 126.182L0.101563 102.203C0.101585 60.2295 34.1279 26.2031 76.1016 26.2031L141.059 26.2031V51.2676Z" fill="#1FBEA1"/>
      <path d="M76.1016 26.2031C34.1279 26.2031 0.101575 60.2295 0.101563 102.203L0.101563 167.16H0L0 102.203C2.29648e-05 60.2295 34.0264 26.2031 76 26.2031H76.1016ZM141.367 91.1602C141.367 133.134 107.341 167.16 65.3672 167.16H47.2422C85.9658 163.658 116.307 131.109 116.308 91.4736V26.2031L141.367 26.2031V91.1602Z" fill="#0A53DF"/>
      <path d="M116.308 91.4736C116.308 133.447 82.2813 167.474 40.3076 167.474H0.101563L0.101563 127.268C0.101583 85.294 34.1279 51.2676 76.1016 51.2676H116.308V91.4736ZM89.3691 79.458C75.0325 65.1476 51.7878 65.1476 37.4512 79.458C23.1146 93.7685 23.1148 116.971 37.4512 131.281C51.7878 145.592 75.0325 145.592 89.3691 131.281C103.706 116.971 103.706 93.7685 89.3691 79.458Z" fill="#0B9D83"/>
      <path d="M167.383 26.2031L76.1016 26.2031C35.572 26.2031 2.45355 57.9286 0.222656 97.9004L0.222656 76C0.222665 34.0264 34.249 1.48511e-06 76.2227 0L167.383 0V26.2031Z" fill="#48E3C7"/>
      <circle cx="63.3863" cy="105.376" r="25.3397" fill="#013EB3"/>
    </svg>
  );
}

// Favicon stand-ins — circular tiles with a letter or simple shape
function FavTile({ label, bg, size = 44, emoji }) {
  return (
    <div style={{
      width: size, height: size, borderRadius: size / 2,
      background: bg || 'var(--bg-elev-2)',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      color: 'white', fontWeight: 600, fontSize: size * 0.38,
      boxShadow: 'inset 0 0 0 1px rgba(255,255,255,0.06)',
      flexShrink: 0,
    }}>
      {emoji || label}
    </div>
  );
}

// Real favicon loader — uses Google S2 service and falls back to FavTile if it fails.
// `domain` is like "github.com". `label`/`bg` are the fallback tile values.
function Favicon({ domain, size = 18, radius, label, bg, tileSize }) {
  const [failed, setFailed] = React.useState(false);
  const r = radius != null ? radius : size / 2;
  if (failed || !domain) {
    return <FavTile size={tileSize || size} bg={bg} label={label || (domain ? domain[0].toUpperCase() : '?')} />;
  }
  // Google S2 supports sz= parameter (16, 32, 64, 128, 256)
  const px = size <= 16 ? 32 : size <= 32 ? 64 : 128;
  const url = `https://www.google.com/s2/favicons?sz=${px}&domain=${domain}`;
  return (
    <img
      src={url}
      alt=""
      width={size}
      height={size}
      onError={() => setFailed(true)}
      style={{
        width: size, height: size,
        borderRadius: r,
        objectFit: 'cover',
        background: 'var(--bg-elev-2)',
        flexShrink: 0,
        display: 'block',
      }}
    />
  );
}

// Larger favicon variant for NTP shortcuts — puts the favicon inside a circular tile
function ShortcutFavicon({ domain, label, bg, size = 48 }) {
  const [failed, setFailed] = React.useState(false);
  if (failed || !domain) {
    return (
      <div style={{
        width: size, height: size, borderRadius: size/2,
        background: bg || 'var(--bg-elev-2)',
        display: 'grid', placeItems: 'center',
        color: 'white', fontWeight: 700, fontSize: size * 0.38,
        boxShadow: 'inset 0 0 0 1px rgba(255,255,255,0.06)',
      }}>{label}</div>
    );
  }
  const url = `https://www.google.com/s2/favicons?sz=128&domain=${domain}`;
  return (
    <div style={{
      width: size, height: size, borderRadius: size/2,
      background: 'var(--bg-elev)',
      display: 'grid', placeItems: 'center',
      boxShadow: 'inset 0 0 0 1px oklch(from var(--fg) l c h / 0.08), 0 2px 6px rgba(0,0,0,0.1)',
      overflow: 'hidden',
    }}>
      <img
        src={url}
        alt=""
        width={size * 0.55}
        height={size * 0.55}
        onError={() => setFailed(true)}
        style={{ display: 'block' }}
      />
    </div>
  );
}

Object.assign(window, { Ic, AjnaMark, FavTile, Favicon, ShortcutFavicon });
