On the eve of Istanbul’s mayoral election, opposition candidate Ekrem Imamoglu was drawing small crowds of the party faithful to low-key campaign rallies. Two days after his stunning upset victory, thousands of supporters thronged the presumptive winner as he paid his respects at the mausoleum of the founder of modern secular Turkey.

Visiting Mustafa Kemal Ataturk’s tomb is a tradition for election winners in Turkey. For Imamoglu, it was also an act of defiance. A bitter dispute has broken out between his supporters and the ruling Justice and Development Party (AKP) over his narrow vote lead. The 48-year-old challenger is energizing the once moribund Republican People’s Party (CHP). Banished from power for four decades, the CHP is now tantalizingly close to controlling Turkey’s biggest cities.

Imamoglu and his rival, former Prime Minister Binali Yildirim, have both declared themselves Istanbul’s mayor-elect. Less than 0.2 percent of the more than eight million votes cast separates them. A recount has entered its second week, and anxiety is mounting within the opposition over whether the AKP will relinquish its quarter-century hold on Istanbul should the final tally confirm Imamoglu’s victory.

President Recep Tayyip Erdogan has waded into the fight. He sees evidence of “organized crimes” marring the vote and has said that he backs his party’s demand that the election board cancel the Istanbul results outright. A newspaper close to the ruling party denounced a “ballot box coup d’état”; another saw the machinations of international conspirators behind Imamoglu’s sudden ascent. And a prosecutor in the northern town of Samsun is weighing a criminal complaint against Imamoglu over the visit to Ataturk’s mausoleum.

The backlash is a case of sour grapes, Imamoglu says. The AKP is “looking for a remedy after their loss at the ballot box, because they’re not accustomed to losing,” he told me. “After [the AKP] ran the city for 25 years, people developed a mindset that it would never change. This shows that democracy wasn’t working. [But] on the street, I see a need for hope, and that this hope is associated with me has given me a great responsibility.”

Before the election, Imamoglu was a largely unknown district mayor of a far-flung Istanbul suburb. His campaign addressed everyday grievances, such as traffic congestion and job creation. Since the vote, he has shown a tougher side, holding impromptu press conferences to accuse the AKP of “acting like they’ve had their toys taken away,” although he has also tried to project a sense of normalcy to reassure a nervous public.

For the AKP, last month’s local elections were anything but normal. The party suffered its most serious electoral setback in its 16 years in power, as an unusually united opposition tapped into widespread economic discontent to flip control of Ankara and nine provincial capitals.

The Islamist-rooted AKP and its ultranationalist partners still clinched a majority of the votes cast and will remain at the helm of the national government for another four years. But the decline in the AKP’s support in Turkey’s political and economic centers exposes cracks in its city-dwelling base. That decline also threatens the AKP’s long-standing practice of awarding lucrative municipal infrastructure projects to businesses loyal to Erdogan.

Losing Istanbul would not just be a financial hit to the AKP; it would also be a deeply personal blow for Erdogan. He grew up in a poor waterfront neighborhood in the city and gained national prominence after becoming mayor in 1994, setting the stage for his election as prime minister, in 2003, and as president, in 2014.

Since gaining power, Erdogan has ruled Turkey with an ever-tightening grip. After fending off a military coup in 2016, he launched a sweeping crackdown on those he blamed for instigating it, jailing tens of thousands of his opponents. Last year, he won election to a presidency with vast new powers and little parliamentary oversight. He enjoys unwavering support from a pliant press, almost all of which is run by the government or companies close to it.

During the local elections, Erdogan campaigned relentlessly for his mayoral candidates, turning what would have been mundane contests among municipal managers into a referendum on his authoritarian style of rule. At campaign events, the mild-mannered Yildirim was largely sidelined, just the opening act before Erdogan took the stage to warn that the nation’s survival was at stake and to accuse the opposition of consorting with “terrorists” by reaching out to a pro-Kurdish party.

Despite Erdogan’s theatrics, many voters went ahead and punished the AKP for policies that have unleashed soaring inflation and unemployment. Although Imamoglu refrained from attacking Erdogan, who remains Turkey’s most popular politician, he hammered a ruling elite ensconced in “their palaces” while “Istanbul is in a spiral of hunger, poverty, and unemployment.” Largely ignored by the media, Imamoglu took to Facebook to livestream visits to street markets, where he hugged fans and politely engaged AKP supporters who refused to shake his hand.

Pious voters have long shunned the centrist CHP, repelled by its rigidly secularist ideology and stodgy, elitist image. But nominating Imamoglu, a practicing Muslim who scheduled time on the campaign trail to attend Friday prayers, allowed the CHP, humbled by so many years in the political wilderness, to broaden its appeal. It last ran Ankara and Istanbul in the 1970s, and has been in the opposition in parliament since then as well.

“Imamoglu doesn’t carry the baggage of a classic CHP politician,” said Akif Beki, a former press adviser to Erdogan who now writes a column in Karar, a conservative newspaper. “Efforts to discredit him by saying he’s an enemy of the azan [the call to prayer] or will knock down mosques just don’t stick.”

Imamoglu grew up in a religious family in the conservative Black Sea region, and he attended Koran courses in primary school. At university, his politics shifted left. He joined the CHP and a few years later, in 2014, unseated the AKP incumbent to become mayor of Istanbul’s charmless middle-class Beylikduzu district. As mayor, he won plaudits for building parks and a facility with a modern library and a concert hall. On religious holidays, he handed out traditional sweets to residents.

The CHP says it wants to win votes from across the political spectrum, and it formed an alliance with a small right-wing party for last month’s elections. Its candidate in Ankara—who also faced a recount but was declared the winner this week after a far more decisive victory—hails from a far-right movement.

The CHP also welcomed support from millions of Kurdish voters, who likely swung the election in Istanbul, after the left-wing Peoples’ Democratic Party, whose base is overwhelmingly Kurdish, tacitly backed the CHP by not fielding candidates in western cities. “I believe this election brings to an end the period when elections were won with polarization … and shows that a unity candidate is a worthy pursuit,” Imamoglu said.

The CHP may be coming around to the old adage that all politics are local. It is moving away from its top-down approach of handing out coveted offices to party stalwarts and former bureaucrats, Emre Erdogan, a political scientist at Istanbul’s Bilgi University, told me. “In Imamoglu, it found a politician who got his start in local politics, with natural reflexes and who genuinely knows how to communicate with the man on the street,” he said.

That common touch is something Imamoglu shares with Erdogan. Some observers think the challenger has national ambitions. “Like Erdogan, Imamoglu believes he possesses the secret formula for continued political success,” said Emre Erdogan.

Canan Kaftancioglu, the chief strategist of the CHP’s Istanbul campaign, said the campaign and the recount fight have provided the CHP with a new road map. Whether the party will deploy that battle plan in a do-over vote in Istanbul in the coming months or the general election in 2023 remains to be seen.

“There used to be a perception that the CHP didn’t work hard enough, that it easily gave up,” Kaftancioglu said. “This time, we’ve made it clear we are in this till the end, until Imamoglu receives his mandate. This strategy has not only consolidated and motivated our base but has earned the respect of AKP voters.”

That’s the trap in front of Erdogan. Should he succeed in forcing a rematch in Istanbul, he risks compromising his own supporters’ faith in the ballot box. For all the criticism Erdogan faces over the erosion of Turkey’s democracy, his own mandate derives from what he calls the “national will”: his 15 successive electoral victories.

“There’s nothing to be done but to accept that we lost the kingdom of Istanbul,” said Mehmet Kaya, a 59-year-old shopkeeper and lifelong supporter of the AKP and its predecessors. “If they cancel this election to hold another one, we will vote for Imamoglu. Turks always stand by the downtrodden.”