FOR Turkey’s Islamist president Recep Tayyip Erdogan, who has won every campaign he has run since being elected mayor of Istanbul in 1994, it was a first taste of defeat. Voters denied his Justice and Development (AK) party a majority in Sunday’s parliamentary polls, thwarting his dreams of rewriting the constitution to grant himself executive powers. AK, led by Ahmet Davutoglu, the prime minister, garnered 41% of the vote and looked set to win 258 seats, 18 too few to govern alone. The result marks a big decline from the record 50% AK won in the 2011 elections. For the first time since AK swept to power in 2002, coalition rule seems all but inevitable. And Mr Erdogan's drive to aggregate power to himself, which led critics to accuse him of authoritarianism, has been halted.

In the Kurdish capital of Diyarbakir, supporters of the pro-Kurdish Peoples’ Democracy Party (pictured), which entered parliament for the first time, were ecstatic. But the prospect of a return to weak multiparty rule sent jitters through the financial markets. Istanbul's main stock index dropped 8% in early trading, while the Turkish lira fell 5% to a record low. “Welcome to the good old Turkey,” tweeted Mehmet Simsek, the finance minister.

Political analysts were less downcast. “The Erdogan era is drawing to a close,” said Ozer Sencar, an Ankara-based pollster. That take may be exaggerated, but the result was certainly a blow to Mr Erdogan, who had brushed aside the constitution's requirement that the president remain non-partisan in order to hit the campaign trail for AK. Tellingly, Mr Erdogan did not make a public appearance after the outcome was announced on Sunday. Mr Davutoglu tried to put a brave face on the results, declaring that AK remains “the backbone of Turkey”. But the faces at the party's after-election rally were dejected.

The main causes of AK’s setback were a faltering economy, Mr Erdogan’s growing authoritarianism, and the stunning success of the HDP. While the party has its roots in the Kurdish autonomy movement, it has created an eclectic alliance that draws in Turkish liberals, leftists, Christians and gays. The HDP sailed past the 10% threshold for seats in parliament for the first time, winning 13% and an estimated 80 seats.

The HDP’s co-chair, Selahattin Demirtas, played a pivotal role. The witty, telegenic 42-year-old former human rights lawyer is widely seen as the most charismatic politician to emerge on the Turkish scene since Mr Erdogan began his climb nearly two decades ago. Mr Demirtas managed to appeal both to Kurds and to Turks worried about Mr Erdogan's ambitions. “Turkey has pulled back from the brink. The debates about an executive presidency, about dictatorship are over,” Mr Demirtas declared in his victory speech.

Pollsters reckon that the HDP siphoned away up to four percentage points of AK’s support. In prior elections many Kurds voted for AK, which has been much more accommodating of Kurdish concerns than previous Turkish governments. But in this election they turned to the HDP, particularly after a twin bomb attack in the Kurds’ regional capital of Diyarbakir on June 5th, which killed four people and wounded hundreds. The attack was perceived as having been orchestrated by rogue elements in order to provoke anti-government violence and scare away voters from the HDP. Instead, “it had the opposite effect,” says Ahmet Sumbul, the editor of Mucadele, a local daily; pro-AK Kurds “voted for the HDP in droves”.

The HDP also ate into the share of the traditional secular opposition group, the Republican People’s Party (CHP). The CHP came second with 25% of the vote, about the same share it drew in 2011. Many secularists are thought to have voted strategically in favour of the pro-Kurdish bloc in order to torpedo Mr Erdogan’s ambitions. A record 96 women were elected; other winners include Turkey's first-ever MPs from the Roma and Yazidi minorities. Three Armenians also won, the first time in decades that Armenians will be represented.