MEXICO CITY — For nearly a quarter-century, this sprawling capital has been the bastion of the Mexican left, a training ground for mayors with bigger political aspirations — including Andrés Manuel López Obrador.

So it was a shock late Sunday when results from Mexico’s midterm elections rolled in. The president’s party had been trounced in the city, losing races for the leadership of more than half the 16 boroughs.

López Obrador’s Morena party still emerged as the biggest winner in the elections. It lost its supermajority in the lower house of congress but captured enough seats to form a majority with allies. The party will probably emerge with about half of Mexico’s 32 governorships, a significant increase. But the president might lose the aura of invincibility he’s enjoyed since coming into power three years ago in a landslide.

“In 2018, Morena swept everything. Now the result is much more spread around,” said Jesús Silva-Herzog Márquez, a political scientist at the Tecnológico de Monterrey.

López Obrador on Monday insisted that his project to transform Mexico — by funneling more resources to the needy, and shoring up state industries such as oil — remains intact. “I am reaffirming our commitment to the people, and especially the poor,” he told journalists.

López Obrador and his allies still have a strong hand. They emerged with enough seats in the lower house to control the budget. The president could lure deputies to switch to Morena during the horse-trading that typically follows an election.

Without supermajorities in congress, however, he’ll have a hard time keeping his pledges to change the constitution to give permanence to his priorities. They include rolling back the opening of the oil sector to foreign investment and shrinking the independent pro-democracy institutions he’s derided as a waste of money.

Morena’s setbacks suggest rising unease with the president’s extensive power — the party also leads a majority in the Senate — and unhappiness over his performance handling the economy, the anticorruption fight and the continued extreme levels of violence.

“We Mexicans decided to put some containment around the presidential wishes,” said Sergio Aguayo, a political scientist. “We want some checks and balances.”

Opposition parties had portrayed the election as a last-ditch opportunity to put the brakes on a president veering toward the authoritarianism that dominated Mexico in the last century. The long-ruling Institutional Revolutionary Party combined forces with conservatives and a small leftist party to field coalition candidates. In total, they’re expected to control about 200 seats in the lower house, compared to about 280 for Morena and its allies. Coalition candidates also took the top jobs in eight boroughs in the capital, where Morena previously ruled 14 boroughs.

Few doubt that López Obrador will remain the dominant figure in Mexican politics through the end of his term in 2024. He enjoys approval ratings of about 60 percent and speaks to the nation through daily news conferences. Poorer Mexicans have applauded his social programs such as grants for students and pensions for the elderly, and his dramatic increases in the minimum wage, instituted without triggering inflation. They find his frugal style a welcome contrast with the extravagance of the country’s corruption-riddled politicians of the past.

López Obrador has called a referendum next year on whether he should stay in office. After Sunday’s vote, said Silva-Herzog Marquez, “he might be looking at that with some concern.”

The president and Mexico City Mayor Claudia Sheinbaum — widely viewed as his protegee — have remained popular even as the pandemic ravaged the capital. Their standing was probably damaged, however, by a deadly accident last month involving a metro train that tumbled from an overpass. There had been frequent allegations of shoddy work and corruption in building the Line 12 route. (Sheinbaum was not up for reelection.)

The president’s polarizing language might have further alienated some voters. He lambastes his critics — in the political opposition, the media and civic groups — as allied with rich conservatives.

The Mexico City election results were particularly striking. The capital has been ruled by the left since Cuauhtémoc Cárdenas was elected mayor in 1997, the first opposition candidate to defeat the PRI in the city. López Obrador served as mayor from 2000 to 2005, and left with an approval rating of more than 80 percent.

“He lost the middle class of Mexico City,” said Aguayo.

On Twitter, Mexicans began posting memes Monday showing the city’s new electoral map — with the opposition-controlled western part in blue (marked “capitalist”) and the Morena-governed boroughs in the east in pink (“communist”).

The memes were intended to be humorous, but they underlined the polarization that has divided the country. López Obrador acknowledged Monday that his party’s support largely came from the poor segment of the population.

“This is the time of the poorest Mexicans,” he said, and then added a politically appealing coda: “Here we are including the middle class, the lower-middle class, the middle-middle class, and everyone.”

Read more: