It was election night in downtown Erie, Pa., and at the Democratic watch party, the mood was grim.

Joe Biden was losing in the battleground states, and he was losing big in Erie, a small place with an outsize role in American politics: The surrounding county had backed Barack Obama twice before flipping to Donald Trump in 2016. It would probably need to flip back if Democrats wanted to win in Pennsylvania.

Jim Wertz, a college administrator, grabbed a beer and took the floor in the cavernous craft brewery where 50 masked and socially distanced Democrats were growing more anxious with every projection.

“We still have a long way to go,” Wertz, who leads the local party and had slept all of two hours in the previous two days, reminded the crowd.

On Saturday, his patience was rewarded: Biden defeated Trump after the Democrat won Pennsylvania. The news set off a dance party among Biden supporters on the streets of Philadelphia, along with an amplification from the president’s backers of unfounded claims of big-city election fraud.

Yet it wasn’t Pennsylvania’s major urban centers that set the result in 2020 — a narrow Biden win — apart from the outcome in 2016, when the state delivered perhaps the cruelest cut of all to Democratic dreams. It was Erie County and other places like it, where relatively minor shifts across a wide swath of small, industrial cities, growing suburbs and sprawling exurbs added up, and made all the difference.

“When this is over, the map in Pennsylvania is going to look almost identical to the one in 2016,” said Chris Borick, director of the Muhlenberg College Institute of Public Opinion. “So what’s different? Not all that much, except on the margins.”

Those margins were tight in 2016: Trump won Erie — population 270,000, hard on the lake of the same name — by two points, contributing to his statewide victory by 0.7 percent. The margins were tighter still on Saturday: Biden was ahead by 1.1 percentage points in Erie, with 99 percent of votes counted, and half a percentage point statewide, with 95 percent of votes counted.

The 2020 result promises to profoundly alter the nation’s direction, forcing an abrupt end to four years of Trump and Republican rule. Yet unlike other elections that have shifted control in the White House — most recently in 2008 and 2016 — it was not accompanied by any fundamental realignment of the American electorate.

If anything, the result reinforced many of the elements that defined Trump’s victory four years ago, especially the stark divide between rural and urban America.

“The 2016 election changed a lot. This didn’t change a ton,” Borick said. “It was setting things in their new place.”

That new place is one marked by a standoff between two very different blocs within the population that have two very different visions of America.

“You have a fast-diversifying younger population. And you still have a large, older, White population,” said William Frey, a senior fellow at the Brookings Institution. “That’s where we’re going to be for a while.”

Despite Biden’s victory, Republicans were holding out hope they could close the gap. They were also pushing in court to throw out ballots received after Election Day or that were “cured” after initially being submitted incorrectly. Trump attorney Rudolph W. Giuliani said Saturday that the campaign plans to file a lawsuit Monday alleging voter fraud in Pennsylvania. But judges have so far been unsympathetic to lawsuits that have been filed, and the Trump campaign has produced no evidence to substantiate the president’s claim of widespread fraud.

Even as ballots continued to be counted in a number of states, it was clear that both sides were able to make some dents in the other’s coalition this year. Trump, for instance, captured more of the Latino vote in states such as Florida and Texas, helping him keep them firmly red despite Democratic hopes of turning them blue.

In the three “Northern battlegrounds” that Biden flippedWisconsin, Michigan and Pennsylvania — Trump boosted rural turnout, while Biden amped up his performance in the cities. But in many of those areas, each side managed to limit the other’s margins, canceling out any gains.

In Philadelphia, for instance, Biden’s margin appeared to have dropped relative to Hillary Clinton’s, though votes were still being counted Saturday. In Wayne County — home to Detroit — the margin was essentially flat, and in Milwaukee, Biden’s percentage gains relative to 2016 were only modest.

The real difference between 2016 and 2020 was most pronounced in suburbs and small cities, where increasing diversity and a less overwhelming margin for Trump among White voters both appeared to have helped propel Biden to victory.

Among White men without college degrees and White suburban women, for instance, Frey said “Biden has done better, or at least less bad” than Clinton did in 2016.

That was evident in the three traditionally Republican counties surrounding Milwaukee: Waukesha, Ozaukee and Washington. Collectively known as the WOW counties, they are predominantly White, and they reliably come out in force for the GOP nominee. They did this year too — but Biden managed to substantially narrow the gap, especially in Ozaukee, where seven points were sliced off Trump’s advantage.

Those gains for Biden more than offset an improved Trump performance in many of Wisconsin’s rural counties and helped deliver Biden victory by just over half a percentage point statewide.

It was a similar story in next-door Michigan, where Biden managed to pull off an approximately 2½-point win, largely by improving on Clinton’s performance in the suburbs outside Detroit.

Macomb County — famed home of the Reagan Democrats — had voted twice for Obama before crossing to Trump in 2016. It didn’t cross back this time, but Biden cut into Trump’s lead. The Democratic nominee made up even more ground in the exurbs of Livingston County, long a Republican stronghold, and in Ottawa County, just outside Grand Rapids.

Trump’s difficulty with key demographic groups was apparent to Erie’s Republican chair, Verel Salmon. He said Trump had no trouble this year in motivating his core Republican base, which credited him with boosting the economy — at least until the coronavirus took hold.

But he noticed that many women in Erie had trouble accepting Trump.

“The idea was the way he talked is not a good example for our kids, or something like that,” said Sal­mon, who is a retired schools superintendent. “It’s a personality-based thing.”

Salmon said some voters in Erie — a largely White industrial and shipping hub about equidistant between Cleveland and Buffalo — appeared to be also turned off by the president’s handling of the pandemic.

“The Democrats very effectively seized on covid and blamed it on the president, which is ridiculous,” he said. “I don’t think any human being on earth could have done any better.”

Wertz, the local Democratic leader, said he sensed that moderate Republicans in Erie were coming over to Biden’s side because “they saw Trump as a detriment to the party and voted against him to restore order.”

Fueled by adrenaline and Red Bull after a vigorous campaign, Wertz was confident heading into Election Day. But the initial results were disheartening, with votes counted on Election Day showing a wide Trump lead. It wasn’t until Thursday evening that enough mail-in votes were counted for Biden to take a slim lead in Erie.

It undoubtedly helped Biden in Pennsylvania’s smaller cities that he was from a small Pennsylvania city himself, and rarely passed up a chance to mention his Scranton roots. From the start, Biden had premised his candidacy on his ability to rebuild the “blue wall” of industrial Democratic strongholds by winning back disillusioned blue-collar voters.

“He is from Pennsylvania,” said Mitch Kates, senior adviser to the state’s Democratic Party. “He has been a figure who identified always with Pennsylvania and he always kept his roots in Pennsylvania. He always talks about Pennsylvania.”

Biden won Lackawanna County, which includes Scranton, by about eight points — a five-point improvement on Clinton’s margin.

He also appeared to have narrowly flipped Northampton County, one of the other three counties, besides Erie, that swung from Obama in 2008 and 2012 to Trump in 2016. A four-point Trump victory four years ago had transformed into a 0.8-point Biden lead, with 99 percent of votes counted.

“We’ve been fairly close and we continue to be pretty close,” said Matthew Munsey, Democratic chair in Northampton, which includes Bethlehem and Easton. “But we’re happy to be back in the Democratic column.”

The third Obama-Trump county — Luzerne, home of the city of Wilkes-Barre — didn’t flip to Biden. It remained with Trump, and by a significant margin, just over 14 points. But that was down from the nearly 20-point thumping that Trump had delivered in 2016, and that local Republicans had vowed he would widen this time around, especially after multiple presidential visits.

The Democrats’ renewed strength reflected the fact that some voters had come to regret their support for Trump four years ago, said Kathy Bozinski, chair of the Luzerne County Democrats. But it also stemmed, she said, from Democrats’ determination to reach out to African American and Latino voters who had often been overlooked in a county that was once overwhelmingly White but is growing more diverse.

“We talked to community leaders of all ages, and asked, ‘How can we make you more a part of the Democratic Party?’ That outreach really made a difference,” she said.

Yolanda Mariano Caliz was among those newly motivated. While she voted for Clinton four years ago, she did so with little enthusiasm. This time was different for the 58-year-old, whose parents immigrated to the United States from the Caribbean nation of St. Vincent. Her daughter works in health care, and she couldn’t understand why Trump never seemed to take the coronavirus seriously.

“We know a couple people who lost family members due to coronavirus,” she said. “To go around and act like it wasn’t happening? We weren’t satisfied.”

In her yard in the Wilkes-Barre suburbs, she put up a Biden sign. When someone destroyed it, she put up another. She reminded people in the neighborhood to register to vote and cast their ballots. On Election Day, she took two friends to vote with her — neither of whom had voted four years ago.

She didn’t consider voting by mail. She needed to see that her ballot was counted.

“I wanted,” she said, “to make sure.”

Christine Spolar contributed to this report from Pittsburgh.