(function(){
/* global React */
const { useState, useEffect, useRef, useMemo, createContext, useContext } = React;

// ── Router (simple hash-based) ────────────────────────────────────
const RouterCtx = createContext({ path: '/', nav: () => {} });

function Router({ children }) {
const [path, setPath] = useState(() => {
    const h = window.location.hash.replace(/^#/, '');
    return h || '/';
  });
  useEffect(() => {
    const onHash = () => {
      const h = window.location.hash.replace(/^#/, '') || '/';
      setPath(h);
      localStorage.setItem('cgp_last_path', h);
      window.scrollTo({ top: 0, behavior: 'instant' });
    };
    window.addEventListener('hashchange', onHash);
if (!window.location.hash) {
      window.location.hash = '#/';
      setPath('/');
    }
    return () => window.removeEventListener('hashchange', onHash);
  }, []);
  const nav = (p) => { window.location.hash = '#' + p; };
  return <RouterCtx.Provider value={{ path, nav }}>{children}</RouterCtx.Provider>;
}

function useRouter() { return useContext(RouterCtx); }

function Link({ to, children, className, style, onClick, ...rest }) {
  const { nav } = useRouter();
  return (
    <a
      href={'#' + to}
      className={className}
      style={style}
      onClick={(e) => { if (onClick) onClick(e); if (!e.defaultPrevented) { e.preventDefault(); nav(to); } }}
      {...rest}
    >{children}</a>
  );
}

// ── Small UI atoms ────────────────────────────────────────────────
function Stars({ n = 5, size = 14 }) {
  return (
    <span className="stars" aria-label={`${n} out of 5 stars`}>
      {Array.from({ length: n }).map((_, i) => (
        <svg key={i} width={size} height={size} viewBox="0 0 24 24" fill="currentColor" aria-hidden>
          <path d="M12 2l2.9 6.9 7.4.6-5.6 4.9 1.7 7.3L12 17.8 5.6 21.7l1.7-7.3L1.7 9.5l7.4-.6L12 2z"/>
        </svg>
      ))}
    </span>
  );
}

function Ph({ label, brief, ratio = '16 / 10', style, small, src, alt, pos }) {
  if (src) {
    return (
      <div className="ph" style={{ aspectRatio: ratio, overflow: 'hidden', background: 'var(--color-accent-soft)', ...(style||{}) }}>
        <img src={src} alt={alt || brief || label || ''}
             style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: pos || 'center', display: 'block' }} />
      </div>
    );
  }
  return (
    <div className="ph" style={{ aspectRatio: ratio, ...(style||{}) }}>
      {!small && (
        <div className="ph-label">
          <b>{label}</b><br/>
          {brief}
        </div>
      )}
    </div>
  );
}

function Icon({ name, size = 18, stroke = 1.6 }) {
  const paths = {
    phone: <path d="M4 5c0-1 1-2 2-2h2l2 5-2 1c1 2 3 4 5 5l1-2 5 2v2c0 1-1 2-2 2C10 18 6 14 4 5z"/>,
    check: <path d="M4 12l5 5L20 6"/>,
    arrow: <path d="M5 12h14M13 6l6 6-6 6"/>,
    arrowd: <path d="M6 9l6 6 6-6"/>,
    shield: <path d="M12 3l8 3v6c0 5-4 8-8 9-4-1-8-4-8-9V6l8-3zM9 12l2 2 4-4"/>,
    star: <path d="M12 2l3 7 7 .6-5.3 4.6 1.7 7L12 17l-6.4 4.2 1.7-7L2 9.6 9 9l3-7z"/>,
    pin: <path d="M12 22s7-7 7-13a7 7 0 10-14 0c0 6 7 13 7 13z"/>,
    clock: <circle cx="12" cy="12" r="9"/>,
    clock2: <path d="M12 7v5l3 2"/>,
    mail: <path d="M3 6h18v12H3zM3 6l9 7 9-7"/>,
    house: <path d="M3 11l9-7 9 7v10h-6v-6h-6v6H3V11z"/>,
    leaf: <path d="M20 4C10 4 4 10 4 20c10 0 16-6 16-16zM4 20c4-4 8-6 13-10"/>,
    ruler: <path d="M3 7h18v6H3zM6 7v3M9 7v5M12 7v3M15 7v5M18 7v3"/>,
    drop: <path d="M12 3s6 7 6 12a6 6 0 11-12 0c0-5 6-12 6-12z"/>,
    wrench: <path d="M14 3a5 5 0 014 8l7 7-3 3-7-7a5 5 0 01-8-4 5 5 0 014-7l2 2-2 2 2 2 2-2 2 2-1-6z"/>,
    broom: <path d="M20 2l-9 9 2 2 9-9zM3 21c3-1 5-3 6-5l2 2c-2 1-4 3-5 6l-3-3z"/>,
    grid: <path d="M3 3h7v7H3zM14 3h7v7h-7zM3 14h7v7H3zM14 14h7v7h-7z"/>,
    building: <path d="M5 21V5l7-3v19M12 8h7v13h-7M8 9h1M8 13h1M8 17h1M15 12h1M15 16h1"/>,
    chevron: <path d="M9 6l6 6-6 6"/>,
    plus: <path d="M12 5v14M5 12h14"/>,
    minus: <path d="M5 12h14"/>,
    close: <path d="M6 6l12 12M6 18L18 6"/>,
    menu: <path d="M4 7h16M4 12h16M4 17h16"/>,
    book: <path d="M4 5a2 2 0 012-2h12v18H6a2 2 0 01-2-2V5zM8 3v16"/>,
    chat: <path d="M4 4h16v12H8l-4 4V4z"/>,
    user: <path d="M12 12a4 4 0 100-8 4 4 0 000 8zM4 21c0-4 4-7 8-7s8 3 8 7"/>,
    search: <path d="M10 4a6 6 0 100 12 6 6 0 000-12zM20 20l-5-5"/>,
    upload: <path d="M12 3v12M7 8l5-5 5 5M4 17v4h16v-4"/>,
    sparkle: <path d="M12 3v6M12 15v6M3 12h6M15 12h6M6 6l4 4M14 14l4 4M6 18l4-4M14 10l4-4"/>,
    calendar: <path d="M3 6h18v15H3zM3 11h18M8 3v4M16 3v4"/>,
    dollar: <path d="M12 3v18M16 7c0-2-2-3-4-3s-4 1-4 3 2 3 4 3 4 1 4 3-2 3-4 3-4-1-4-3"/>,
  };
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor"
         strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round" aria-hidden>
      {paths[name] || null}
    </svg>
  );
}

// ── Logo ──────────────────────────────────────────────────────────
function Logo({ height = 36 }) {
  return <img src="assets/logo.png" alt="Coastline Gutter Pros" style={{ height, width: 'auto' }} />;
}

// ── Exports ───────────────────────────────────────────────────────
Object.assign(window, { Router, useRouter, Link, Stars, Ph, Icon, Logo, RouterCtx });

})();
