We calculate the electric dipole moment of the nucleon induced by the QCD theta term. We use the gradient flow to define the topological charge and use $N_f = 2+1$ flavors of dynamical quarks corresponding to pion masses of $700$, $570$, and $410$ MeV, and perform an extrapolation to the physical point based on chiral perturbation theory. We perform calculations at $3$ different lattice spacings in the range of $0.07~{rm fm} < a < 0.11$ fm at a single value of the pion mass, to enable control on discretization effects. We also investigate finite size effects using $2$ different volumes. A novel technique is applied to improve the signal-to-noise ratio in the form factor calculations. The very mild discretization effects observed suggest a continuum-like behavior of the nucleon EDM towards the chiral limit. Under this assumption our results read $d_{n}=-0.00152(71) bartheta e~text{fm}$ and $d_{p}=0.0011(10) bartheta e~text{fm}$. Assuming the theta term is the only source of CP violation, the experimental bound on the neutron electric dipole moment limits $left|barthetaright| < 1.98times 10^{-10}$ ($90%$ CL). A first attempt at calculating the nucleon Schiff moment in the continuum resulted in $S_{p} = 0.50(59)times 10^{-4} bartheta e~text{fm}^3$ and $S_{n} = -0.10(43)times 10^{-4} bartheta e~text{fm}^3$.