There is a beautiful formula to count the number of ideals $I$ in the ring of integers $\mathcal{O}_K$ of a number field $K$, given by

$$\sum_{n \leq X} a_n \sim C_K X,$$

where $a_n$ is the number of ideals in $\mathcal{O}_K$ of norm $n$ and $C_K$ is the residue at $s = 1$ of the Dedekind zeta function $\zeta_K$ of $K$. The quantity $C_K$ is used in the class number formula, see: https://en.wikipedia.org/wiki/Class_number_formula

Indeed, one can further refine the result by counting principal ideals. Let $b_n$ denote the number of principal ideals in $\mathcal{O}_K$ of norm $n$. Then

$$\displaystyle \sum_{n \leq X} b_n \sim \frac{C_K}{h_K} X,$$

where $h_K$ is the class number of $K$.

Now let $K = \mathbb{Q}(\sqrt{d})$, where $d$ is a positive fundamental discriminant. Let $\epsilon_d = u_0 + v_0 \sqrt{d}$, where $(u_0, v_0)$ is the smallest positive solution to the Pell equation $x^2 - dy^2 = \pm 4$. Then the regulator of $K$ is given by $\log \epsilon_d$, so the class number formula yields

$$\displaystyle C_K = \frac{2 h(d) \log \epsilon_d}{\sqrt{d}}.$$

Therefore, the number of principal ideals up to $X$ is given by

$$\displaystyle \sum_{n \leq X} b_n \sim \frac{2 \log \epsilon_d}{\sqrt{d}} X.$$

My question is, what about the error term? According to these notes due to Andrew Granville, one gets an error term of $O_d(X^{1/2})$. He did not specify the dependence on $d$. I believe it is not too difficult to obtain $O(\max\{u_0, v_0\} X^{1/2})$ from Granville's arguments, but this seems very large (at least, the dependence on $d$ of the error term is far worse than the dependence on $d$ of the main term). Is there a way to show that the dependence on $d$ is small, say $O(X^{1/2} \log \epsilon_d)$?