OVER THE past few years, president Recep Tayyip Erdogan and his government have throttled dissent, taken control of the judiciary and defanged the press, confining what remains of Turkey’s democracy to the ballot box. In municipal elections on March 31st, the ballot box struck back. Despite taking a plurality of the national vote, Mr Erdogan’s ruling Justice and Development (AK) party suffered defeats in five of Turkey’s six biggest cities, including Istanbul, the country’s economic engine, and the capital Ankara.

The loss of Istanbul, in particularly, stung Mr Erdogan, given that he began his political career there over two decades ago. The AK’s mayoral candidate, a former prime minister and parliamentary speaker, Binali Yildirim, had the support of a media controlled by pro-government tycoons, his party’s seemingly unstoppable electoral machine and the president, who stumped for his candidates as if his own political future were on the line. The opposition’s pick, Ekrem Imamoglu, had the backing of a hapless, squabbling party, a fraction of his opponent’s resources and only a few years of experience as district mayor under his belt. But in one of the biggest upsets in Turkey’s recent election history, it was Mr Imamoglu, the relative unknown, who seemed to have won, by the thinnest of margins.

Controversy and violence cast a cloud over the vote. As soon as Mr Yildirim’s lead over Mr Imamoglu shrank to a fraction of a percentage point, the state news agency, true to its reputation as a government mouthpiece, suddenly stopped updating results from the race. Both candidates claimed victory. When the tally resumed the following morning, the results showed Mr Imamoglu ahead by less than 30,000 votes, with only a few dozen ballot boxes unopened. Brawls broke out in and around some polling stations.

In other parts of the country, the violence turned deadly. Two people were killed in Malatya and two more in Gaziantep, in the south of the country. Turnout across Turkey reached 85%.

Mr Erdogan was able to claim victory, however, since AK, with the help of its nationalist allies, won in most of the country’s 81 provinces, jointly securing 52% of the popular vote in the process, compared to 38% for the main opposition bloc, led by the Republican People’s Party (CHP). “We have come out of this election as the leading party,” Mr Erdogan said, but conceded that AK had underperformed in big cities. He also promised to take measures to mend the economy, which recently dipped into recession, and to abide by free-market rules. The free markets did not seem reassured. Coming off one of its worst weeks since last summer’s currency crisis, the Turkish lira weakened by 2% in dollar terms on Monday morning, before paring its losses. Anxiety that Mr Erdogan will sacrifice economic orthodoxy for political gains has not abated.

The election is hardly a fatal blow for Mr Erdogan and his party. Turkey’s president continues to be armed with a constitution that gives him sweeping executive powers. AK, backed by the Nationalist Movement Party (MHP), still commands a parliamentary majority. But losses in the big cities hurt in both symbolic and practical terms. Because municipalities in Turkey play a central role in the country’s networks of patronage, opposition mayors may end up unearthing one corruption scandal after another. The results also place Mr Erdogan in an uncomfortable spot, squeezed between Kurdish voters, many of whom sided with the opposition alliance, and the MHP. Any attempt to relaunch peace talks with the Kurdistan Workers’ Party (PKK), an armed and outlawed separatist group, may now end up costing AK its parliamentary alliance with the nationalists. “The MHP can dissolve parliament and force early general elections whenever they want,” says Emre Erdogan (no relation), an academic at Bilgi University. “This makes a Kurdish opening so much more difficult.”

For the country’s stability, the more immediate concern is whether Mr Erdogan will make good on his threats to boot some of his opponents from office, starting with Mansur Yavas (of the CHP), who won the Ankara race, and ending with mayors from the Peoples’ Democratic Party (HDP), who prevailed in most of the Kurdish south-east. In the run-up to the election, Turkey’s interior minister announced that 231 opposition candidates were being investigated on terrorism charges. Prosecutors launched a probe against Mr Yavas on the basis of old claims connecting him to a forged cheque. Such threats are not taken lightly in Turkey. In the previous local elections, voters in the south-east elected 102 HDP mayors. At least 94 of them have since been sacked or arrested on suspicion of links to the PKK, and replaced by government trustees.