The Treaty of Utrecht says that Gibraltar if it were to leave English-owned status would automatically be ceded Spain iirc. (Just found out) so that rules out the micronation idea.
Well yes but centuries old treaties can probably be changed. If it couldn't then Gibraltar would almost certainly refuse to leave the UK and the UK government could well get them a different deal with the EU than the rest of the UK. Trust me Gibraltan's fear of Spain's attempts to destroy their unique culture by submerging them back into Spain and their visceral dislike of the Spanish government is much stronger than their love of the EU.
It's a treaty that would have to be negotiated with Spain. With a likely PP government. They won't play ball. Basically Gibraltar is at the mercy of the British negotiators - if they are sold out to try and get Spain on the British side there is little legally the Gibraltar people can do about it.