A Variational Principle For Modeling Electronic Excitations In Gas And Condensed Phase