In every proof of this theorem I see, I get stuck by the same thing.
Once we choose the final node $v_n$ at depth $l_n$ to be the code word of length $l_n$, the subtree rooted at that $v_n$ has no children!
Therefore the number of leaves it has is not equal to the formula $$r^{l_n - l_i}$$ when $i=n$ but it is meant to hold for $1\leq i \leq n$.
Am I missing something or are the arguments in the proofs flawed?
See proof: https://en.wikipedia.org/wiki/Kraft%E2%80%93McMillan_inequality#Proof
In this case, "leaves" refers to nodes at depth $\ell_n$: leaves of the original tree, not leaves of the subtree. The subtree rooted at $v_n$, a node at depth $\ell_n$, contains one node at depth $\ell_n$: the node $v_n$ itself. This agrees with the formula $|A_i| = r^{\ell_n -\ell_i}$, since $r^{\ell_n - \ell_n} = r^0 = 1$.