Unified theory of nonlinear electrodynamics and gravity