Three-dimensional toroidal equilibria and stability by a variational spectral method