Given a manifold $M$, consider its tangent bundle $TM$ and $T^*M$. Let's work in one trivialization $U$. The coordinate of $TM$ is $(q^1,..,q^n,\dot q^1,..,\dot q^n)$. Be aware: $\dot q^i$ is not the time derivative of $q^I$. They are just really really really sloppy way to describe some arbitrary point $(q^1,..,q^n,k ^1,..,k^n)\in T^*U$. Sometimes physicists say it's off shell.
Now, define a Lagrangian $L:TU\mapsto \mathbb{R}$, define the Legendre transformation with respect to $L$ as
$$\Gamma_L:TU\mapsto T^*U, \Gamma_L(q^1,..,q^n,\dot q^1,..,\dot q^n)=(p_1,..,p_n,q^1,..,q^n),p_i=\frac{\partial L}{\partial \dot q^i}(q^1,..,q^n,\dot q^1,..,\dot q^n)$$
Note that everything is well defined on arbitrary points of $TU$. Under some condition, $\Gamma_L$ is a local diffeomorphism.
Now, define the Hamiltonian $H:T^*M\mapsto \mathbb{R}$ such that
$$H\circ \Gamma_L=E_L$$, where $$E_L(q^1,..,q^n,\dot q^1,..,\dot q^n)=\sum_{i=1}^n\dot q^i\frac{\partial L}{\partial \dot q^i}(q^1,..,q^n,\dot q^1,..,\dot q^n) -L(q^1,..,q^n,\dot q^1,..,\dot q^n)$$. Note that in the definition of $E_L$, everthing is off shell, i.e. $\dot{q}^i$ is not derivative of $q^i$.
Therefore, everything makes sense now. If we write $H(p,q)=\sum_ip_i\dot q^i-L(q^1,..,q^n,\dot q^1,..,\dot q^n)$, then $\dot q^i$ are determined by $(p,q)$ through $\Gamma_L$, the Legendre transformation (through implicit function theorem, or you can see this by diffeomorphism). Most importantly, $\dot q^i$ is not derivative of $q^I$. Note that we fix $p,q$ first, and then we look for $\dot q^i$
This is very confusing. I'm a math background person and spent long time in figuring out words like "independent variable" or "on shell". Most of the explanation misses explaining this clearly to me because they want to avoid differential geometry, but without DG, everything become much more opaque.
Any way, "Quantum Mechanics for Mathematicians
Leon A. Takhtajan" is the right source. If you are serious in studying physics, you will have to learn differential geometry in basic terms.
https://www.math.stonybrook.edu/~leontak/CM.pdf