Lagrangian dynamics on higher-dimensional spaces with applications to Kaluza–Klein theories