Correct me if I'm wrong, but I thought that even in his will Philip IV directed that an Austrian Habsburg should inheirate the throne upon Charles' death. It stands to reason therefore that he would likely name an Austiran Habsburg as his heir if Charles preceeds him in death. Perhaps Karl Joseph survives, perhaps he names Mariana knowing that Leopold will ensure her rights. Perhaps he legitimizes John of Austria, the younger.
Even if Philip IV declares one of his Austrian relatives to be his heir, I don't think that will stop Louis XIV from launching a war in favor of his son's claim. "Louis I" (is that the right numbering?) has a better claim than any other royal in Europe, and his father Louis XIV is in a position to really do something about making the claim real.
The War of Spanish Succession was fought near the end of Louis XIV's reign, when he was old and France was tired. In this TL, the *War of Spanish Succession will be Louis XIV's first war, and I think that we will be able to prevail.
I think the Dutch would be involved. Even if Louis's focus is on Spain proper the Spanish Netherlands are a too tempting target for him. After all, even if he doesn't get the crown he could receive it as a compensation.
The Dutch were originally allied to the French when Louis XIV first invaded the Spanish Netherlands- if anything I think that Louis XIV might be willing to recognize Dutch annexation of all the Spanish Netherlands in exchange for support against the Hapsburg.
Would this be a good idea for the Dutch? No, but they might not realize that for a while, since French military strength is focused on the long and bloody campaign for Spain, being fought in Spain proper and Italy.
So we would have the Dutch, Spain and Austria (probably with some other HRE's state) against Louis XIV. Actually, even England could have entered on the Spanish side. The secret Treaty of Dover was only signed in 1670. In 1668 Charles II (the English one) allied with Sweden and the Netherlands in order to oppose Louis XIV in the War of Devolution. Sure, the French won, but there were not able to take the entire Spanish Netherlands as they wanted.
The English didn't want French domination of the Netherlands- something that with Louis XIV's focus on Spain and Italy won't happen. I really think that this POD is the perfect one for a Bourbon-wank, since it comes with a strong France and the best foreign policy situation that France will probably have in the 17th century.
So Louis XIV abdicates his wife's claim to the throne, but gets some important Spanish territory (Milan? Naples? Netherlands?). Margaret is declared Queen of Spain, but she is not allowed to marry Leopold, in order to avoid the reunion of the Habsburg territories. As a compensation, Leopold receives some Spanish territories too (which of them?).
Margaret needs to marry, but as Leopold is the last male Habsburg than she needs to find another royal house. Also, I imagine that she would be pressed to marry fast, in order to ensure a heir. Who would be available by 1666?
I vote for Bourbon-wank. Louis XIV wins Spain and Spain's colonial possessions, and agrees to Austrian ownership of Spain's Italian possessions, and Dutch ownership of Spanish Netherlands. Louis XIV appoints a Frenchman as his son's regent in Spain, which doesn't appreciate that, but is too exhausted to do much about it.
Perhaps send Louis XIV's younger son to Spain for his education? The clear implication is that the younger son will become his older brother's Spanish viceroy, once Louis XIV dies. The new Bourbon regime in Madrid begins to reform the whole structure of the Spanish Empire, with a clear eye towards rendering the thing easier to rule from Paris. The addition of the Spanish Empire prompts Louis XIV to build up naval strength in order to make sure he is able to maintain control of the vast new territory- the cost of this naval expansion perhaps precludes further wars against the Dutch and Austrians? (A naive notion- Louis XIV not making constant war- I know)