Spanish prime minister Pedro Sanchez looks set to regain power after his Socialists overcame a historic challenge by right-wing nationalists in elections on Sunday, a result he portrayed as a morale booster for the European Union.
The Socialists gained seats in one of Spain's most hotly contested elections in decades, which featured the rise of far-right party Vox whose ultra-nationalist campaign echoed political trends across Europe where traditional parties have ceded to anti-immigration and eurosceptic forces.
However, the rise of Vox also splintered the right-wing vote, further fragmenting Spain's political landscape and playing into the hands of the Socialists which won an almost 50 per cent increase in seats.
"The Socialists have won the general election," Sanchez declared, smiling broadly as he addressed jubilant supporters at party headquarters in Madrid. Outside, hundreds celebrated in the street, waving red party flags and chanting 'Long live Spain' and 'Long live Socialism'.
"Social democracy has a great future because it has a great present and Spain is an example of that. We will form a pro-European government to strengthen and not weaken Europe," he added.
The result is likely to come as a relief for supporters of further European Union integration ahead of 26 May elections for European Parliament amid fears that Vox could have gained a share of power in Spain.
Vox secured one in 10 votes and 24 seats, the first far-right party to get a sizeable presence in parliament since the country's return to democracy in the late 1970s, but it did not get the kingmaker role it was looking for.
Far left-wing party Podemos immediately offered to open coalition talks with Sanchez, though the two parties together cannot command a majority.
"I have expressed to him ... our willingness to work towards a coalition government," Podemos leader Pablo Iglesias told supporters in Madrid. Forming such a coalition "will take much time and I would ask for your patience", he added.
The Socialists and Podemos are set to fall around 11 seats short of a parliamentary majority, but bridging the gap would require Sanchez to also rely on Basque nationalists and other small parties.
It was unclear in the early hours of Monday if he would also require the support of separatist lawmakers from Catalonia, the country's richest region where a push for independence flared into violence two years ago.
"Pedro Sanchez is in a good position," said Pablo Simon, a political science professor at Madrid's Carlos III University.
A government deal would be complicated and certainly not be clinched before next month's European Parliament elections, he added.
The Keys to Power
Sanchez appears to have two options to put together a coalition.
If he wants to rule with Podemos, near-final voting data showed he would need the support of smaller parties to at least abstain.
According to Sunday night's count, he would require one Catalan separatist lawmaker to abstain but some seats were too close to call. The picture may still change when the count is finalised in the first two weeks of May.
If Sanchez does not need to rely on Catalan separatists, who brought down his minority government, he may have more political freedom to deal with the Catalan independence movement. The issue was at the heart of the election campaign and has at times unnerved euro zone investors and the rest of the EU.
Alternatively, Sanchez could turn to center-right Ciudadanos. Together, the Socialists and Ciudadanos would have an outright majority.
Though he and Ciudadanos leader Albert Rivera have sharply different views on Catalonia and both ruled out forming an alliance during the campaign, speculation persists that the pair could yet strike a coalition deal. Rivera repeated after the election that he would be in the opposition.
Spaniards cast their votes in numbers close to record highs with campaigning dominated by national identity and cultural values like women's rights rather than the economy.
This is the third national election in four years, after the first two eroded the decades-long dominance of the two biggest parties, the Socialists and the conservative Popular Party (PP).
By party, Sanchez's Socialists led with 123 seats in the 350-seat parliament. The PP stood at 66 seats, centre-right Ciudadanos ('Citizens') at 57 seats, Podemos at 42 and Vox at 24.
The big loser was the PP, which lost more than half its seats in the lower house of parliament and also lost control of the senate to the Socialists.
In Spain, issues like mass immigration or euroscepticism that have dominated political discourse elsewhere have been eclipsed by the question of national unity and the threat posed by the Catalan independence movement.
The economy, one of the euro zone's star performers, took a back seat in campaigning across the political parties.