Theory of nonlinear electromagnetic modes