In the 1970s, certain non-scientific branches of academia invented an entirely new concept and attached the label "gender" to it. The concept was that the way one presents themselves in society is "gender". This historically has never been the meaning of gender. The public at large continued to use gender in the original meaning (as you will see with official forms asking for 'gender'. If they asked for 'sex', people would add a box with "yes please" on it and tick it).
In the 2010s this new meaning of gender leaked out of academic circles and into the general vernacular. But it is a concept that is entirely the invention of left-leaning academics, cross citing each other repeatedly in echo chamber journals.
You say there is "great variety in both", but this is not true. There is great variety in "gender as a social construct", that is, the cosmetic way people dress, do their hair etc. But there is not great variety in "sex and I don't mean fucking". Disorders of sex development (DSDs) are extremely rare and do not constitute a new sex and are recognised medically as disorders. For example, XXYY occurs in 1 in 18,000 to 40,000 male births. XXXXY occurs in 1 in 85,000 to 100,000 male births. Compare that to the "normal" male sex characteristics, or the "normal" female sex characteristics, that each occur in about 1 in 2 births.
People erroneously make the claim that other conditions, like triple X syndrome, constitute a DSD (occurs in 1 in 1000 females), but because it causes no health issues or abnormal development it is not considered a DSD by the medical community. The DSD Guideline documents [http://www.dsdguidelines.org] are a trustworthy source of information and definitions.