Algebraic Chern-Simons theory