Not posting the solution, sorry since it's still a w.i.p. But suggesting that you should be aware also that this can be done with zero template edits. So advice here would be using the navtab IDs if you want to limit on the template edits. Below is an example so it's worthwhile looking at doing this via css eliminating the template editing as a possible alternative.
View attachment 61789