* Jubilant crowds celebrate win in Iran's major cities
* Rohani says his win is triumph over "extremism"
* Calls for new tone in Iran's foreign relations
* Rohani may have advantages over predecessor Khatami
* Patience needed, given Iran's "minefield" politics
Thousands of Iranians celebrated on the streets into Sunday's early hours, counting on moderate president-elect Hassan Rohani to follow through on promises of better relations abroad and more freedom at home after routing hardliners at the polls.
A mid-ranking Shi'ite cleric, Rohani is an Islamic Republic insider who has held senior political and military posts since the 1979 revolution and maintained a good rapport throughout with theocratic Supreme Leader Ayatollah Ali Khamenei, Iran's most powerful man who has the last word on all the big issues.
While no reformer himself, Rohani gained the backing of politically sidelined but still popular reformist leaders. His call for an end to the "era of extremism" won over many voters disgruntled over economic crises and crackdowns on free speech and dissidents that marked Mahmoud Ahmadinejad's presidency.
Rohani's surprise win however is not expected to quickly resolve the stand-off with the West over Iran's disputed nuclear ambitions or break its commitment to backing President Bashar al-Assad in Syria's civil war.
But the new president will run the economy of the sprawling OPEC member state of 75 million people and exert influence when Khamenei decides on national security matters.
His victory goes some way to repairing the legitimacy of the Islamic Republic, punctured four years ago when dozens were killed in protests after an election reformists said was rigged, and may help pragmatic voices muzzled since then to re-emerge.
Thousands of young Iranians took to the streets of the capital Tehran and other big cities as soon as the poll results were announced on Saturday, making sure their voices and expectations of the new president were clearly heard.
The president-elect, known in the West as Iran's main nuclear negotiator in 2003-05, immediately sought to build bridges on Sunday, expressing approval of the street parties but also having talks with the conservative speaker of parliament.
"With their celebrations last night, the Iranian people showed they are hopeful about the future and God willing, morals and moderation will govern the country," Rohani told state TV.
Hardliners whose power comes from their unquestioning loyalty to Khamenei both badly miscalculated the public mood and failed to set aside their own factional differences and field a single candidate, analysts said.
Both Khamenei and the powerful hardline Islamic Revolutionary Guard Corps that controls large swathes of the oil-dependent economy said the election was a victory for all.
Whether Rohani succeeds in ushering in change to Iran, or whether the next four years yield the same stalemate that marked the 1997-2005 presidency of reformist Mohammad Khatami, will hinge on his ability to balance the demands and expectations of the people with the interests and constraints of those who hold the pivotal instruments of power in the Islamic Republic.
ROHANI MAY HAVE ADVANTAGES OVER KHATAMI
Rohani's reputation as a mediator and someone who has worked within the corridors of power should be an advantage that Khatami, who was director of the national library before he became president, never enjoyed.
"Rohani is the ultimate regime insider. In contrast to Khatami, who held no governmental position when he was catapulted into the presidency, Rohani has never been out of power or Khamenei's good graces," said Ali Vaez, Iran analyst at the International Crisis Group.
"Also, Rohani is a centrist politician, with a unique bridge-building ability. He is unlikely to alienate competing power centres, who can stymie his reforms," he said.
A big test will be whether Rohani pushes for the release from house arrest of Mirhossein Mousavi and Mehdi Karoubi, two reformist leaders held under house arrest since 2011. That demand was a constant chant of Rohani supporters at his campaign rallies and on the streets of Tehran and elsewhere overnight.
"This will in my view be the first real test of how sincere this election has been. Then we will know the calibre of Mr Rohani," said Ali Ansari, professor at St Andrew's University in Scotland.
"Much depends on the political will of the fractured elite and the willingness of Khamenei to pull back. There is some anxiety that the powers that be, having got their 'popular election', will now settle back into their comfort zones."
Despite similarities between Khatami and Rohani's upset election victories, political realities "are fundamentally different", said Yasmin Alem, a U.S.-based Iran expert.
"The supreme leader is more powerful, the Revolutionary Guards are more influential, and the conservatives are more in control. However, Rohani is a crafty statesman and stands a better chance ... of navigating Iran's political minefield."
Rohani has a tough task ahead of him dealing with Iran's myriad domestic and foreign policy problems, she said.
"Iranian voters should demonstrate the same maturity and patience they did at the polls, if they want to avoid the disillusionment that followed Khatami's presidency."
Rohani himself called for patience soon after his win was announced on Saturday. "The country's problems won't be solved overnight and this needs to happen gradually and with consultation with experts," he told the state news agency IRNA.
But Rohani, whose conciliatory style contrasts with the confrontational populism of Ahmadinejad, said there was a new chance "in the international arena for ... those who truly respect democracy and cooperation and free negotiation".
Post-election revellers were optimistic. "I am hopeful about the future, hopeful that we will have more social freedoms, more stability in Iran, better relations with other countries and hopefully a much better economy," said Hoda, 26, from Tehran.
As well as chanting "Long live Rohani!" and wishing good riddance to the current president with "Ahmadi, bye bye!", jubilant crowds did not shy from feting Mousavi, the reformist leader defeated in the election four years ago.
"Mousavi, Mousavi, congratulations on your victory!" the crowds shouted.
Pictures and videos of the celebrations showed more people wearing the green colours of Mousavi's 2009 campaign than Rohani's purple. Police stood by and even shared jokes with some people in the throng.
Others had an ironic take on the "death to dictator" chants of the huge 2009 protests at which security forces opened fire, shouting "thank you dictator" for allowing a fair vote now.