A CSS script could do that... if the :has() selector is supported.
With that said, I've recently run into issues with that selector not being supported (due to it being recent CSS spec) in two different (but not recently updated) user style extensions that I've tried it with (in one, it is entirely unrecognized; in the other, you get errors when trying to do nontrivial things in the :has() ).
I could copy/paste some example user CSS for you if you'd like to see if you can get it working.
I can confirm both that my original version has the bug you described and that your fix does not produce that bug.
I don't actually have a native Lemmy account, so I didn't realize that upvoting would cause the upvote count to gain a CSS class that I used in my selector.
(Thankfully, the DOM changed to reflect given/taken upvotes in spite of me not being logged into the Lemmy server at all; I only got a "Not logged in." error each time.)