// ============================================================================
// MobileNav — top header + bottom tab bar. Shown only on <=768px via CSS.
// Slot at the top & bottom of every page. Desktop SiteNav continues to work
// (hidden by mobile.css on narrow viewports).
// ============================================================================

(function () {

  // -------------------- SVG icons (stroke-based, 24) --------------------
  const iconProps = { width: 22, height: 22, viewBox: '0 0 24 24', fill: 'none',
    stroke: 'currentColor', strokeWidth: 1.8, strokeLinecap: 'round', strokeLinejoin: 'round' };

  const IconHome = () => (
    <svg {...iconProps}>
      <path d="M3 11.5 12 4l9 7.5"/>
      <path d="M5 10v10h14V10"/>
    </svg>
  );
  const IconGrid = () => (
    <svg {...iconProps}>
      <rect x="3" y="3"  width="7" height="7" rx="1.5"/>
      <rect x="14" y="3" width="7" height="7" rx="1.5"/>
      <rect x="3" y="14" width="7" height="7" rx="1.5"/>
      <rect x="14" y="14" width="7" height="7" rx="1.5"/>
    </svg>
  );
  const IconRadar = () => (
    <svg {...iconProps}>
      <circle cx="12" cy="12" r="9"/>
      <circle cx="12" cy="12" r="5"/>
      <circle cx="12" cy="12" r="1.6" fill="currentColor"/>
      <path d="M12 12 19.5 7.5"/>
    </svg>
  );
  const IconUser = () => (
    <svg {...iconProps}>
      <circle cx="12" cy="8.5" r="3.8"/>
      <path d="M4.5 20c1.3-4 4.2-6 7.5-6s6.2 2 7.5 6"/>
    </svg>
  );
  const IconPlus = () => (
    <svg width="10" height="10" viewBox="0 0 10 10" fill="none"
      stroke="currentColor" strokeWidth="1.8" strokeLinecap="round">
      <path d="M5 1v8M1 5h8"/>
    </svg>
  );
  const IconMenu = () => (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none"
      stroke="currentColor" strokeWidth="1.8" strokeLinecap="round">
      <path d="M4 7h16M4 12h16M4 17h16"/>
    </svg>
  );

  // -------------------- Top mobile header --------------------
  // Compact: logo left, hamburger right.
  function MobileTopBar({ homeHref = 'Homepage.html' }) {
    return (
      <header className="mobile-top-bar">
        <a href={homeHref} className="mobile-top-brand" aria-label="ABGS — home">
          <span className="mobile-top-brand__halo" aria-hidden="true"/>
          {window.BrandLogo
            ? <window.BrandLogo size={34}/>
            : <span style={{ width: 34, height: 34, borderRadius: 8,
                background: 'linear-gradient(135deg, #1E57D6, #1846B0)' }}/>
          }
          <span className="mobile-top-brand__wordmark">
            <span className="mobile-top-brand__primary">ABGS</span>
            <span className="mobile-top-brand__sub">AB Group Shipping</span>
          </span>
        </a>
      </header>
    );
  }

  // -------------------- Bottom tab bar --------------------
  // current: 'home' | 'services' | 'track' | 'account' (the CTA is separate)
  function MobileTabBar({ current = 'home', quoteHref = '#quote' }) {
    const home     = 'Homepage.html';
    const services = 'services/index.html';
    // Detect subpage so relative hrefs resolve from services/*
    const isSubpage = /services\//.test(window.location.pathname);
    const prefix = isSubpage ? '../' : '';

    const Tab = ({ id, label, href, Icon }) => {
      const active = current === id;
      return (
        <a href={href} className={'mobile-tab-item' + (active ? ' active' : '')}>
          <Icon/>
          <span>{label}</span>
        </a>
      );
    };

    return (
      <nav className="mobile-tab-bar" aria-label="Primary mobile">
        <Tab id="home"     label="Home"     href={prefix + home}     Icon={IconHome}/>
        <Tab id="services" label="Services" href={prefix + services} Icon={IconGrid}/>

        {/* Center CTA — Instagram-style protruding pill */}
        <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'flex-end' }}>
          <a href={quoteHref} className="mobile-tab-cta" aria-label="Get a quote"
            onClick={(e) => {
              if (window.__abgsCTA && window.__abgsCTA.openQuote) {
                e.preventDefault();
                window.__abgsCTA.openQuote('sitenav-mobile-quote');
              }
            }}>
            <span className="plus"><IconPlus/></span>
            <span>QUOTE</span>
          </a>
        </div>

        <Tab id="track"   label="Track"   href={prefix + 'Homepage.html#track'} Icon={IconRadar}/>
        <Tab id="account" label="Account" href={prefix + 'Homepage.html#account'} Icon={IconUser}/>
      </nav>
    );
  }

  window.MobileTopBar = MobileTopBar;
  window.MobileTabBar = MobileTabBar;
})();
