Even when dealing with Biological Sex a legal system that recognizes it as relevant to more than between patient/doctor has to deal with how it doesn't always fit neatly into the biological binary. People are sometimes born
intersex and the law has to leave room for that, where the body may develop in a way that doesn't match the chromosomes or there is a mutation in the chromosomes. Turns out human invented categories always have examples that break them, just ask the Platypus.
----
And from a purely biological explanation for transexuality, a thought I've had that there's probably been research into I've missed and I fully accept I'm talking out my arse:
With my limited understanding of biology I know that when a fetus forms, it's always female first. Then other hormones come in and cause male traits to develop, but the template it's using as a starting point is female biology. And biology is not an exact process, so I can see plenty of room for things to go wrong. A male body forms but the brain develops in such a way that it 'expects' to interface with a female body. A female body remains but enough hormones happen that the brain develops in a way that it expects a male body. And all sorts of combinations of percentages in between.
Now, if that's the biological basis then there's two potential ways it can be 'addressed'. Either 'fix' the body so the brain can accept interfacing with it, or 'fix' the brain so it correctly accepts the body. But we aren't dealing with a simple chemical imbalance like can be addressed by SSRIs, so 'fixing' the brain is basically a no go. So you're left with doing what you can for the body to make the person's quality of life livable.
And again, the legal/social system has to cover the entire population of that society so has to accept this which means Lived Gender inherently becomes separate from Biological Sex as legal/social concepts.
Yes I'm aware this is probably the most coldly clinical way to talk about it, sorry about that.