JUST last year he looked unassailable. Turkey’s Recep Tayyip Erdogan had seen off a coup attempt, cracked down on opposition and secured new and unparalleled powers in a referendum.

“We never bow down in front of anyone except God,” the Islamist said after being swept back into office in last June’s presidential elections.

Mr Erdogan – international onlookers decided – was now one of a new breed of nationalist authoritarian leaders, such as Egypt’s Sisi, Russia’s Putin and China’s Xi. He – and his way – was here to stay.

Scroll forward 12 months and the 65-year-old “strongman” has lost some of his grip.

He may still only bow before God: but Erdogan has been forced to concede the loss of his home city, Europe’s biggest, to a secularist opponent.

The new mayor of Istanbul and its 16 million people was named on Sunday as 49-year social democrat Ekrem Imamoglu.

This, even on the scale of the giant metropolis straddling the boundary of Europe and Asia, is no mere local poll. It was a significant swing away from political Islamism back to the secular roots of the Turkish Republuic founded by Mustafa Kemal, the “Ataturk”, from the ruins of the old Ottoman Empire after the First World War.

The vote, Mr Imamoglu said as his victory was confirmed, “renewed our faith in democracy and justice”. Why? Because it was an election he had already won, back in March, before electoral authorities overturned the result. Indeed, Mr Imamoglu even served a few weeks in the mayoral post, which Erdogan first used to build his conservative base.

Erdogan’s conservative Justice and Development Party, AKP, had put up a former prime minister as candidate, Binali Yildirim.

READ MORE: Avoid demos, UK tourists told

He conceded moments after returns showed him trailing well behind Mr Imamoglu, 54 per cent to 45%. The secularist had increased his lead from a wafer-thin 13,000 to 800,000.

And so, effectively, ended Mr Erdogan’s party’s 25-year hold on the city. Mr Erdogan himself was mayor of Istanbul from 1994 to 1998. His tenure ended with a conviction for challenging constitutional secularism – and a short spell in jail that saw him banned from politics.

His AKP, founded in 2001, has ruled Turkey since 2002. A year later Mr Erdogan was prime minister, a post he held until he was elevated to the presidency in 2014. His rule is not under immediate threat: he will not face re-election until 2023.

“You have protected the reputation of democracy in Turkey with the whole world watching,” Mr Imamoglu told his supporters on Sunday. “Turkey won this election, not a single party. You will see it with time. I will not marginalize anyone. For me, a duty order awaits. This is the most precious, honourable and proud duty in my whole life.”

Following his second victory, tens of thousands of people erupted in mass celebration across Istanbul, including outside the offices of the Republican People’s Party or CHP which backed Mr Imamoglu. Other secularist parties had stood aside.

The Herald:

The new mayor still faces significant opposition. Mr Erdogan’s party still controls 25 of Istanbul’s 39 districts and a majority in the municipal assembly. AKP also lost control of the capital city of Ankara in Turkey’s March local elections, held as the country faced an economic downturn, battled high inflation and two credit rating downgrades in the past year.

Mr Erdogan used his mayoralty as a stepping stone to rule Turkey, after taking credit for improvements to living standards. “The AKP no doubt improved the lives of Istanbul residents during its rule,” Fariba Nawa wrote for America’s Foreign Policy magazine. “Those gains made Erdogan incredibly popular.”

But it is now concerns about living standards that are driving voters away from his party. Inflation is 19% a year. The Turkish lira has lost about 40% of its value over the last two years. Construction companies had borrowed in dollars to power growth in the earlier years of Mr Erdogan’s rule. They are now struggling to pay that debt.

The Herald:

Mr Erdogan may have a sense of deja vu as he watches Mr Imamoglu. Both men come from the Black Sea coast and rose in Istanbul after careers in football. Mr Erdogan played for Kasimpasa, one of the city’s teams. Its stadium now bears his name. Mr Imamoglu was an executive at Trabzonspor.

Pundits see CHP on the rise, especially as it casts off a reputation for secularism and nationalism and forms a focal point for opposition. “It’s the worst setback he’s had, Sinan Ulgen, chairman of Edam, an Istanbul-based think-tank, said of Mr Erdogan’s defeat. Speaking to the Financial Times, he added: “It will embolden the CHP and Turkey’s parliamentary opposition because now they are firmly in control of not only Istanbul but basically Turkey’s major metropolitan cities.”