Want smart analysis of the most important news in your inbox every weekday, along with other global reads, interesting ideas and opinions to know? Sign up for the Today’s WorldView newsletter.

Ahead of local elections throughout his country last weekend, Turkish President Recep Tayyip Erdogan resorted to his usual tactics. He cast some of his ruling party’s opponents as traitors in league with terrorists. He screened the horrific video of the massacres at two mosques in New Zealand and claimed the mantle of Muslim victimhood. And he stirred angry nationalism from city to city in a bid to distract voters frustrated by the country’s faltering economy.

But this time it didn’t work. In what’s being viewed as a rebuke to Erdogan, candidates representing his Justice and Development Party, or AKP, were defeated in the major cities throughout Turkey including the capital, Ankara, as well as Izmir, Adana and Antalya. In Istanbul, the country’s biggest city, a politician from the opposition Republican People’s Party looks to have secured a majority of votes, though the AKP is challenging the result, and there are fears of government foul play afoot. Even as AKP officials point out that they and their coalition partners won the most votes across the country, the next days will test Erdogan’s ability to stomach defeat.

The loss of Istanbul “would be an especially harsh blow to the president,” wrote The Washington Post’s Kareem Fahim. “Erdogan rose to national prominence as the city’s mayor from 1994 to 1998. The city has since served as a source of wealth and prestige for his party and a showcase — with its sprinting construction, megaprojects and multiplying mosques — for his broader ideological vision.”

But amid a grim economic downturn, which has seen the country’s currency, the lira, lose a third of its value against the dollar, Erdogan could no longer hang his hat on the prosperity ushered in during an earlier period of his rule. And his divisive populism failed to secure enough nationalist support among urban voters.

“Rather than Erdogan’s imaginary enemies,” noted Omer Taspinar of the Brookings Institution, “Turkish voters in almost all major cities preferred to focus on something much more concrete and visible: inflation, unemployment, recession and a major drop in standards of living.”

In many countries, municipal elections are low on the totem pole of political relevance. But in Turkey’s system, they select the heads of the country’s 81 provinces and the cities and towns within them. This round of elections was also a crucial litmus test for the years ahead. For the past half-decade, Erdogan has taken Turkish voters on a ceaseless electoral roller coaster, which included multiple parliamentary elections, a referendum on his ambitions to remake the country’s political system and then a presidential election. But Erdogan, who either as prime minister or president has been in power for close to two decades, does not face an election until 2023. That’s just as well, given the chastening verdict from Sunday.

“The loss of Turkey’s major cities has also shattered Erdogan’s image as an invincible politician,” wrote Gonul Tol, a Turkey scholar at the Middle East Institute in Washington. She added that the election delivered “a huge blow to the clientelistic network Erdogan has built over the last 25 years,” which had helped him secure “the loyalty of the business elite and put their resources to work to consolidate his power.”

At the same time, the election was the latest sign of a more-galvanized opposition overcoming years of division and dysfunction. “Erdogan was blessed with an opposition of disparate groups that hated each other,” Soner Cagaptay of the Washington Institute for Near East Policy told Fahim. But now they had demonstrated what could happen when “they came together in a meaningful union for the first time.”

The deflating outcome for Erdogan adds to his mounting woes. Abroad, he is engaged in a new squabble with the United States over Turkey’s purchase of a Russian missile defense system. At home, he faces mounting calls to enact deep economic reforms.

“It’s a terrible result for Erdogan,” Berk Esen, an assistant professor of international relations at Ankara’s Bilkent University, told the Financial Times. “There’s an economic crisis, there’s an international crisis due to an ongoing standoff with the U.S. At the same time, there is an electoral defeat that shows his international partners and domestic opponents that he’s quite vulnerable.”

There’s a broader story to be told, as well. Well before President Trump, Indian Prime Minister Narendra Modi or Hungarian Prime Minister Viktor Orban, Erdogan arrived at the politics of the zeitgeist. His pro-business, center-right agenda eventually gave way to a more hardened and illiberal nationalism, and his majoritarian policies were built around retaining power. Crackdowns and purges flushed out potential opponents within Turkey’s judiciary and state bureaucracy, while squeezing the country’s civil society and news outlets. State media is entirely in Erdogan’s camp, while pro-Erdogan business executives have taken control of many of the country’s leading independent media houses.

But even with the deck stacked against them, the opposition made tactical alliances and managed to score significant wins. “There is a lesson in all of this for democrats around the world struggling under populists or semi-authoritarian regimes,” wrote Post columnist Asli Aydintasbas. “If you are organized, find the right candidate with the right message — there’s your chance.”

Moreover, as much of Europe reckons with the ascent of upstart populist or nationalist parties in their countries, Erdogan offers a cautionary tale for what happens if they ever assume and keep power — that is, as long as voters still get to decide.

“When populist rhetoric comes from a fresh face and aims at a calcified elite, it sounds plausible,” wrote Selim Koru, an Ankara-based analyst. “When it has supplanted that elite, and it has reached its demographic limits, it begins to look ridiculous.”

Want smart analysis of the most important news in your inbox every weekday, along with other global reads, interesting ideas and opinions to know? Sign up for the Today’s WorldView newsletter.