Variational study of mass generation and deconfinement in Yang-Mills theory