Portugal didn’t try as hard to get involved in mainland Europe and mostly focused on trade and its overseas empire. What caused its decline? Especially since from what I understand they were also allied with the English, meaning their alliance dominated the waves rather heavily even in the 1600s and 1700s
You may have often heard that, but it is a truly outdated narrative about Spain, based on several tropes that entered the Spanish common perception through bourbonic propaganda. Those famous tropes are the decadence, the minor Habsburgs, and Charles II being a disaster, when all of these are false.
In the year 1700, king Charles II of Spain dies, having in his testament declared Philip of Anjou (his grand-nephew) as his heir. Archduke Charles of Austria would not agree with this, and a very long continental scale war would start that would last until 1714. This was the War of the Spanish Succession, and ended with Philip of Anjou's victory, which he paid very dearly: in order to get the Spanish throne (with the Spanish Empire in the Americas and Asia) he had to renounce any European posessions: Naples, Sicily, Sardinia, and the Spanish Low Countries. Thought with perspective, he did the right thing in accepting those conditions in order to get the formidable Spanish Empire.
Bringing a new dynasty always means some changes. Philip had been rasied in his grandfather's court of Versailles, and learned the French principles of centralism. The Habsburg style of a composite monarchy and personal union of crowns would be right out of the window once Philip's power was consolidated. The ushering of his radical changes would mean a hefty dose of propaganda, and that meant vilifying the preceding dynasty.
In this vilification, two concepts came forward: Major Habsburgs, and Minor Habsburgs. The major ones were Charles V and Philip II, while the minor ones were Philip III, Philip IV, and Charles II. This conceptualisation of his predecessor as a "minor Habsburg" was truly important, as Philip would paint himself as some sort of restorer, someone to make Spain great again. Spain was not a declining and decaying empire in the year 1700, and Charles II was not a bad king, but Philip did not care about the truth, which he of course knew.
Charles II's reign has been under a profound historical revision for the past 25 years, in order to strip it of the bourbonic propaganda. Objectively observed, Charles II's reign was nothing short of a miracle: the economy was stabilised, coinage and taxation were reformed and improved, peace was achieved, the American provinces were very developed and rich, and commerce was booming. That without mentioning the Spanish diplomacy, which was ridiculously competent, to the point that in 1697 the French ambassador to the Court of Saint James bitterly commented in a letter that "London is ruled from Madrid". Under Charles II, the Spanish navy was stronger than it had ever been before too.
With these very strong foundations, king Philip V took over Spain, usehered in his reforms, and consolidated the Spanish empire in the Americas. The 18th century, with its limitless supply of great admirals and engineers, was the highest point of the Spanish navy: Romero Landa and his system, Jorge Juan, José de Mazarredo, Blas de Lezo, Gaztañeta, Luis de Córdova, Antonio de Ulloa, Malaspina, Churruca, Antonio Barceló, the list can go on forever. In the 17th and 18th centuries, it was not Portugal nor England who ruled the waves: it was Spain. Spain's naval supremacy basically ends in the battle of Trafalgar, and then for good with the collapse of the empire to the separatist movements of the Spanish America.
The wars and devaluation of the currency did hurt Spain in the 17th century, but in the last years of Philip IV and the reign of Charles II, things got back on track, with currency reforms, taxation reforms, and the achievement of a general state of peace (the biggest wars that drained a lot of resources ended with Philip IV, the 80 years war, the 30 years war, and the war with France). With the Portuguese war over in 1668, then prosperity happened.
With all of this off my chest, as misconceptions need to be corrected, I hope someone answers appropriately about Portugal and its decline. I hope the mods don't kill this answer, as it does not directly adress the question, but it goes to answer some underlying misunderstandings about the history of Spain that are present in the question.