ISTANBUL   -   The last partygoers went home as the sun came up. Across Istanbul on Sunday night, hundreds of thousands of opposition supporters danced in the streets waving Turkish flags and brandishing glasses of beer and raki after their candidate for mayor delivered the most serious blow to the president, Recep Tayyip Erdoğan, in his 16-year political career.

As municipal workers cleaned up on Monday morning, however, the front pages of Turkey’s pro-government newspapers downplayed the unprecedented success of the Republican People’s party (CHP) mayor-elect, Ekrem İmamoğlu. “Istanbul has voted,” read the subdued headline of the usually rabidly pro-Erdoğan tabloid Yeni Şafak. There were no pictures of the fireworks and scenes of jubilation hours before.

While the opposition nurses a collective hangover, attention is turning to what the president’s next move will be. İmamoğlu ended 25 years of Islamist party dominance in the rerun for control of Turkey’s biggest city and economic centre, which accounted for 31% of GDP in 2017. The result has serious financial implications for the ruling Justice and Development party (AKP) and its patronage networks, and will amplify the sense among the opposition and within Erdoğan’s party that the president’s power is starting to wane.

The loss of Istanbul also has repercussions for policymaking in Ankara. The defeat has ossified divisions in the AKP coalition with the rightwing Nationalist Movement party (MHP) that now appear to be insurmountable, making a cabinet reshuffle likely. There is also speculation that Erdoğan may call a snap election to rid his government of fractious elements as he struggles to deal with issues such as Turkey’s struggling economy, Ankara’s next steps in Syria and the prospect of US sanctions over the planned purchase of a Russian S-400 missile system.

The former president Abdullah Gül and the former prime minister Ahmet Davutoğlu both openly criticised the AKP for seeking a rerun, fuelling rumours in Turkish media that the senior AKP politicians were preparing to form breakaway parties.

Nicholas Danforth, a senior visiting fellow at the Washington-based German Marshall Fund thinktank, said: “Erdoğan is adept at being conciliatory when necessary and cracking down on dissent when necessary.

“To date, he has maintained power by using both of those methods effectively. Last night’s result is something new, making it hard to tell what the president will do next to reverse the momentum that is building against him.” Many İmamoğlu supporters were anxious in the hours between voting closing and the first results on Sunday night that the government was planning to challenge what polls showed was likely to be a decisive second victory after his narrow win in March was annulled by Turkey’s election board.

However, the AKP candidate, Binali Yıldırım, conceded minutes after the initial results began trickling through, striking a conciliatory tone as he wished İmamoğlu luck. The president issued his congratulations to İmamoğlu on Twitter shortly afterwards and Turkey’s electoral board ratified the victory by a margin of 777,000 ballots – 54% of the vote – on Monday.

The decision to rerun the contest, defying the will of voters who had chosen to punish the government for its mishandling of the economic crisis, was an unusual strategic error by the AKP. The mistake was compounded by an erratic and sloppy second campaign in which Yıldırım was forced to play catch up to İmamoğlu. The opposition candidate, already popular for his inclusive and anti-populist stance, was able to canvass on a new platform of saving Turkish democracy.

As polls in the lead-up to the repeat election showed İmamoğlu pulling ahead by 9%, the government resorted to desperate tactics, insinuating the opposition candidate was working with the outlawed Kurdistan Workers’ party (PKK) and releasing a bizarre statement from the jailed PKK leader Abdullah Öcalan calling on Istanbul’s 4 million Kurdish voters not to support the secular CHP’s İmamoğlu.

On Sunday night, the swift and gracious acknowledgement of defeat struck yet another tone.

Lisel Hintz, a professor at Johns Hopkins University’s European and Eurasian studies department, said: “We still need to see how Erdoğan reacts to İmamoğlu’s victory. Istanbul serves not only as a symbol of where he launched his political career, but also as a massive source of rents that can be used to garner electoral support.

“We’ve seen already through the rerun that he was not willing to let it go easily. We now have to wait and see whether İmamoğlu’s tenure as mayor will be interfered with in any way, whether by cutting off funding and hampering his office’s ability to provide services or by removing him under some legal pretext.”

Also on Monday, the trial began of 16 prominent figures from the 2013 Gezi Park protests in Istanbul that challenged Erdoğan’s authority – a reminder that while Istanbul has signalled its overwhelming appetite for change, the president has over the years consolidated his grip on Turkey’s democratic institutions, and remains very much in control.