I think the answer for this would be: no, you cannot do it exclusively with CSS ... Or maybe just "not at this moment".
HTML and CSS are declarative languages that describe how the page should be organized and how it should be displayed by the browser. But they are "static": they do not change, or at least do not change on their own (since they can be changed using script languages such as JavaScript).
Because of this, and in your particular case, every time you click the button, you need a different language to track these changes and apply the appropriate action (as you are doing with JavaScript now).
Why did I say "not at this moment"? Since CSS gets some features that make it slightly dynamic, and if it is extended (and notice that I only dream about it), they can help you achieve things that you describe without the need for JavaScript.
For example: counter-reset and counter-increment allow you to track the number of occurrences of an element, and they can be used in content from ::before and ::after . If they were extended, so you can use them in other rules (for example: as a numeric value), and if they track not only elements, but also selectors (for example :active ), you can achieve what you want without using JavaScript ... but then again, what I dream.
source share