// ─────────────────────────────────────────────────────────────
// tokens.jsx — Icon helper + small UI utilities
// ─────────────────────────────────────────────────────────────

function useViewport() {
  const get = () => {
    const w = typeof window !== 'undefined' ? window.innerWidth : 1024;
    return {
      width: w,
      isMobile: w <= 768,
      isNarrow: w <= 1024,
    };
  };
  const [v, setV] = React.useState(get);
  React.useEffect(() => {
    const onResize = () => setV(get());
    window.addEventListener('resize', onResize);
    return () => window.removeEventListener('resize', onResize);
  }, []);
  return v;
}

window.useViewport = useViewport;

function LedgerIcon({ name, size = 18, color = 'currentColor', strokeWidth = 1.75, style = {} }) {
  const ref = React.useRef(null);
  React.useEffect(() => {
    if (ref.current && window.lucide) {
      ref.current.innerHTML = '';
      const el = document.createElement('i');
      el.setAttribute('data-lucide', name);
      el.style.width = size + 'px';
      el.style.height = size + 'px';
      ref.current.appendChild(el);
      window.lucide.createIcons({ icons: window.lucide.icons, attrs: { width: size, height: size, 'stroke-width': strokeWidth } });
    }
  }, [name, size, strokeWidth]);
  return (
    <span
      ref={ref}
      style={{
        display: 'inline-flex',
        alignItems: 'center',
        justifyContent: 'center',
        width: size,
        height: size,
        color,
        flex: `0 0 ${size}px`,
        ...style,
      }}
      data-lucide-wrap={name}
    />
  );
}

// Resolve a CSS variable from :root (handles dark mode automatically)
function cssVar(name) {
  return getComputedStyle(document.documentElement).getPropertyValue(name).trim() || name;
}

window.LedgerIcon = LedgerIcon;
window.cssVar = cssVar;
