MADRID -- Prime Minister Pedro Sanchez's Socialists won Spain's national election on Sunday but large gains by the upstart far-right Vox party appear certain to widen the political deadlock in the European Union's fifth-largest economy.

After a fourth national ballot in as many years and the second in less than seven months, the left-wing Socialists held on as the leading power in the national parliament. With 99.9% of the votes counted, the Socialists captured 120 seats, down three seats from the last election in April and still far from the absolute majority of 176 needed to form a government alone.

The big political shift came as right-wing voters flocked to Vox, which only had broken into Parliament in the spring for the first time. Sunday's outcome means there will be no immediate end to the stalemate between forces on the right and the left in Spain, suggesting the country could go many more weeks or even months without a new government.

The far-right party led by 43-year-old Santiago Abascal, who speaks of "reconquering" Spain in terms that echo the medieval wars between Christian and Moorish forces, rocketed from 24 to 52 seats. That will make Vox the third leading party in the Congress of Deputies, giving it much more leverage in forming a government and crafting legislation.

The party has vowed to be much tougher on both Catalan separatists and migrants.

Abascal called his party's success "the greatest political feat seen in Spain."

"Just 11 months ago, we weren't even in any regional legislature in Spain. Today we are the third-largest party in Spain and the party that has grown the most in votes and seats," said Abascal, who promised to battle the "progressive dictatorship."

Right-wing populist and anti-migrant leaders across Europe celebrated Vox's strong showing.

Marine Le Pen, who heads France's National Rally party, congratulated Abascal, saying his impressive work "is already bearing fruit after only a few years."

In Italy, Matteo Salvini of the right-wing League party tweeted a picture of himself next to Abascal with the words "Congratulations to Vox!" above Spanish and Italian flags. And in the Netherlands, anti-Islam Dutch lawmaker Geert Wilders posted a photograph of himself with Abascal and wrote "FELICIDADES" -- Spanish for congratulations -- with three thumbs-up emojis.

With Sunday's outcome, the mainstream conservative Popular Party rebounded from its previous debacle in the April vote to 88 seats from 66, a historic low. The far-left United We Can, which had rejected an offer to help the Socialists form a left-wing government over the summer, lost some ground to get 35 seats.

The night's undisputed loser was the centre-right Citizens party, which collapsed to 10 seats from 57 in April after its leader Albert Rivera refused to help the Socialists form a government and tried to copy some of Vox's hard-line positions.

Sanchez's chances of staying in power still hinges on ultimately winning over the United We Can party and several regional parties, a complicated manoeuvr that he has failed to pull off in recent months.

Sanchez called on opponents to be "responsible" and "generous" by allowing a Socialist-led government to remain in charge.

"We extend this call to all the political parties except for those who self-exclude themselves ... and plant the seeds of hate in our democracy," he added, an apparent allusion to far-right and also possibly to separatist Catalan parties.

United We Can leader Pablo Iglesias extended an offer of support to Sanchez.

"These elections have only served for the right to grow stronger and for Spain to have one of the strongest far-right parties in Europe," Iglesias said. "The only way to stop the far-right in Spain is to have a stable government."

Pablo Casado, the leader of the Popular Party, also pledged to work to end months of political instability. He said "the ball was in the court" of Sanchez, though. In recent months his party and Citizens have struck deals with Vox to take over some cities and regional governments.

Bonnie Field, a professor on Global Studies at Bentley University in California, called the political situation a "mess government-wise."

"Spanish politics are now increasingly complicated and any governing formula is going to require lots of negotiations, and people being open to criticism," she said.

The Socialists took a hit in the country's Senate, losing their absolute majority of 133 seats in the upper parliamentary chamber amid the significant conservative inroads.

Julia Giobelina, a 34-year-old web designer from Madrid, was angry at having to vote for the second time this year. But she said she cast her ballot in hopes of stopping Vox.

"They are the new fascism," Giobelina said. "We citizens need to stand against privatization of health care and other public services."

Spain returned to democracy in the late 1970s after a near four-decade right-wing dictatorship under the late Gen. Francisco Franco. The country used to take pride in claiming that no far-right group had seats in the national Parliament, unlike the rest of Europe. That changed in the spring, but the Socialists' April victory was still seen by many as a respite for Europe, where right-wing parties had gained much ground.

Vox relied on its anti-migrant message and attacks on laws that protect women from domestic abuse as well as what it considers leftist ideology disguised as political correctness. Still, it does not advocate a break from the EU in the very pro-EU Spain.

It has nevertheless flourished after recent riots in Catalonia by separatists, capitalizing on Spanish nationalist sentiment stirred up by the country's worst political conflict in decades. Many right-wingers were also not pleased by the Socialist government's exhumation of Franco's remains last month from his gargantuan mausoleum so he could no longer be exalted in a public place.

The debate over Catalonia, meanwhile, promises to fester.

The three Catalan separatist parties won a combined 23 seats on Sunday.

Many Catalans have been angered by the decision last month by Spain's Supreme Court, which sentenced to prison nine Catalan politicians and activists who led a 2017 drive for the region's independence. The ruling has triggered massive daily protests in Catalonia that left more than 500 people injured, roughly half of them police officers, and dozens arrested.

----

Wilson wrote from Barcelona, Spain; AP journalists Ciaran Giles in Madrid and Renata Brito in Barcelona contributed to this report.