// Barra de navegação inferior fixa — só no mobile, estilo app.
// Dá acesso imediato às seções principais, ao CTA de cadastro e a um
// menu "Mais" com os demais links do rodapé.
// Auto-monta no fim do body: basta incluir este script (após icons.jsx).
function BottomNav() {
  const [moreOpen, setMoreOpen] = React.useState(false);
  const path = (window.location.pathname || '/').toLowerCase();
  const isQuemSomos = path.endsWith('quem-somos.html');

  const items = [
    { label: 'Recursos', href: '/#telas', icon: 'Sparkle' },
    { label: 'Planos', href: '/#planos', icon: 'Tag' },
    { label: 'Começar', href: 'https://app.econome.com.br/register', icon: 'Bolt', cta: true },
    { label: 'Quem somos', href: '/quem-somos.html', icon: 'User', active: isQuemSomos },
    { label: 'Mais', glyph: 'more_horiz', more: true },
  ];

  // Links do rodapé que não aparecem na barra
  const moreLinks = [
    { label: 'Como funciona', href: '/#como-funciona', glyph: 'route' },
    { label: 'Ajuda', href: '/ajuda.html', glyph: 'help' },
    { label: 'Entrar no app', href: 'https://app.econome.com.br', glyph: 'login' },
    { label: 'Termos de uso', href: 'https://www.econome.com.br/termos.html', glyph: 'description' },
    { label: 'Privacidade', href: 'https://www.econome.com.br/privacidade.html', glyph: 'shield' },
    { label: 'Contato', glyph: 'mail', onClick: () => { if (window.openContactModal) window.openContactModal(); } },
  ];

  const renderIcon = (it) => {
    if (it.glyph) {
      return <span className="material-symbols-outlined" style={{ fontSize: 22, lineHeight: 1 }}>{it.glyph}</span>;
    }
    const I = Icon[it.icon];
    return <I size={22} />;
  };

  return (
    <React.Fragment>
      <nav className="bnav" aria-label="Navegação principal">
        {items.map((it) => {
          const cls = 'bnav-item'
            + (it.active ? ' is-active' : '')
            + (it.cta ? ' bnav-cta' : '')
            + (it.more && moreOpen ? ' is-active' : '');
          if (it.more) {
            return (
              <button key={it.label} type="button" className={cls} onClick={() => setMoreOpen((v) => !v)} aria-expanded={moreOpen} aria-haspopup="true">
                <span className="bnav-ic">{renderIcon(it)}</span>
                <span className="bnav-lb">{it.label}</span>
              </button>
            );
          }
          return (
            <a key={it.label} href={it.href} className={cls}>
              <span className="bnav-ic">{renderIcon(it)}</span>
              <span className="bnav-lb">{it.label}</span>
            </a>
          );
        })}
      </nav>

      {moreOpen && (
        <div className="bnav-sheet-ov" onClick={() => setMoreOpen(false)}>
          <div className="bnav-sheet" role="dialog" aria-label="Mais opções" onClick={(e) => e.stopPropagation()}>
            <div className="bnav-sheet-grab" />
            <div className="bnav-sheet-list">
              {moreLinks.map((l) => {
                const inner = (
                  <React.Fragment>
                    <span className="bnav-sheet-ic"><span className="material-symbols-outlined" style={{ fontSize: 22, lineHeight: 1 }}>{l.glyph}</span></span>
                    <span className="bnav-sheet-lb">{l.label}</span>
                    <span className="bnav-sheet-arrow"><span className="material-symbols-outlined" style={{ fontSize: 18, lineHeight: 1 }}>chevron_right</span></span>
                  </React.Fragment>
                );
                if (l.onClick) {
                  return (
                    <button key={l.label} type="button" className="bnav-sheet-item" onClick={() => { setMoreOpen(false); l.onClick(); }}>
                      {inner}
                    </button>
                  );
                }
                return (
                  <a key={l.label} href={l.href} className="bnav-sheet-item" onClick={() => setMoreOpen(false)}>
                    {inner}
                  </a>
                );
              })}
            </div>
          </div>
        </div>
      )}

      <style>{`
        .bnav { display: none; }

        .bnav-sheet-ov {
          position: fixed; inset: 0; z-index: 70;
          background: rgba(26, 22, 20, 0.42);
          display: flex; align-items: flex-end;
          animation: bnavFade 0.18s ease-out;
        }
        .bnav-sheet {
          width: 100%;
          background: var(--bg-elevated);
          border-radius: 22px 22px 0 0;
          padding: 10px 16px calc(14px + env(safe-area-inset-bottom, 0px));
          box-shadow: 0 -14px 34px -14px rgba(26, 22, 20, 0.32);
          animation: bnavUp 0.24s cubic-bezier(0.22, 1, 0.36, 1);
        }
        .bnav-sheet-grab { width: 40px; height: 4px; border-radius: 999px; background: var(--ink-5); margin: 4px auto 10px; }
        .bnav-sheet-list { display: flex; flex-direction: column; }
        .bnav-sheet-item {
          display: flex; align-items: center; gap: 14px;
          width: 100%;
          padding: 14px 4px;
          color: var(--ink);
          font-size: 15.5px; font-weight: 500;
          text-align: left;
          border-bottom: 1px solid var(--line-soft);
        }
        .bnav-sheet-item:last-child { border-bottom: none; }
        .bnav-sheet-ic {
          width: 36px; height: 36px; border-radius: 10px;
          background: var(--bg-soft); color: var(--ink-3);
          display: flex; align-items: center; justify-content: center; flex: 0 0 auto;
        }
        .bnav-sheet-lb { flex: 1; }
        .bnav-sheet-arrow { color: var(--ink-5); display: flex; }

        @keyframes bnavUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
        @keyframes bnavFade { from { opacity: 0; } to { opacity: 1; } }

        @media (max-width: 880px) {
          .bnav {
            display: grid;
            grid-template-columns: repeat(5, 1fr);
            align-items: center;
            position: fixed;
            left: 0; right: 0; bottom: 0;
            z-index: 60;
            background: rgba(255, 255, 255, 0.94);
            backdrop-filter: saturate(150%) blur(14px);
            -webkit-backdrop-filter: saturate(150%) blur(14px);
            border-top: 1px solid var(--line);
            padding: 9px 4px calc(9px + env(safe-area-inset-bottom, 0px));
            box-shadow: 0 -8px 24px -14px rgba(26, 22, 20, 0.22);
          }
          .bnav-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 4px;
            padding: 4px 2px;
            color: var(--ink-4);
            text-align: center;
          }
          .bnav-ic { display: flex; align-items: center; justify-content: center; height: 24px; }
          .bnav-lb {
            font-size: 10.5px;
            font-weight: 600;
            letter-spacing: -0.01em;
            line-height: 1;
            white-space: nowrap;
          }
          .bnav-item.is-active { color: var(--purple); }
          .bnav-cta { color: var(--purple); }
          .bnav-cta .bnav-ic {
            width: 34px;
            height: 34px;
            border-radius: 11px;
            background: var(--purple);
            color: #fff;
            box-shadow: 0 8px 16px -8px rgba(112, 16, 176, 0.65);
          }
          /* No mobile a barra inferior substitui o menu-hambúrguer */
          body .header-menu { display: none !important; }
          body { padding-bottom: 74px; }
        }
      `}</style>
    </React.Fragment>
  );
}

window.BottomNav = BottomNav;

// Auto-montagem: injeta a barra ao fim do body sem precisar editar o render de cada página.
(function mountBottomNav() {
  function mount() {
    if (document.getElementById('bottom-nav-root')) return;
    const el = document.createElement('div');
    el.id = 'bottom-nav-root';
    document.body.appendChild(el);
    ReactDOM.createRoot(el).render(<BottomNav />);
  }
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', mount);
  } else {
    mount();
  }
})();
