I understand that the US is different in this regard.
In the UK, at least, official documents specify "sex", not "gender". We may be slowly moving towards a world in which sex is officially recorded only in medical records, while gender is an optional field in social media accounts and the like.
All the forms in my country, especially the government ones, use 'gender' and not 'sex'. For me, when I see 'sex' on a form, it's jarring to say the least.
I'm Australian.
But your passport says "Sex / Sexe" just below your date of birth, right?
The design of passports is rather precisely defined by an international standard. Interestingly, and perhaps relevantly, Australia is one of a dozen countries that routinely allows its citizens to have an 'X' instead of an 'F' or 'M' in that field. There is an ongoing legal case to obtain that right in the UK (Christie Elan-Cane).
Does your driving licence indicate "sex" or "gender"? A UK driving licence shows neither, and it doesn't give a "title", either: just full name, address, date of birth, and photo. In most cases that would allow one to guess the sex and gender of the holder, of course, but not always.
But in other settings they are usually conflated.
That aside, yes, “sex” broadly refers to biology in some way, though often oversimplified, while “gender” refers to identity and expression (which also don’t necessarily go hand in hand).