import { useEffect, useState } from 'react';

/** Matches CSS breakpoint used for mobile layout (iPhone / narrow viewports). */
export function useMediaQuery(query: string): boolean {
  const [matches, setMatches] = useState(() => {
    if (typeof window === 'undefined') return false;
    return window.matchMedia(query).matches;
  });

  useEffect(() => {
    const mq = window.matchMedia(query);
    const onChange = () => setMatches(mq.matches);
    onChange();
    mq.addEventListener('change', onChange);
    return () => mq.removeEventListener('change', onChange);
  }, [query]);

  return matches;
}

export function useIsMobileLayout(): boolean {
  return useMediaQuery('(max-width: 768px)');
}
