Minerva Turpin, a 99-year-old resident at Hermitage Richmond, puts an “I voted” sticker on Democratic vice-presidential candidate Tim Kaine after he voted in Richmond. (Shelby Lum /Richmond Times-Dispatch via AP)

Virginians narrowly chose Hillary Clinton over Donald Trump for president, marking the third straight win for Democrats in a state that had been reliably Republican for decades.

But on a night when Clinton fell short of expectations nationwide, the margin was far closer than polling had suggested. With 98 percent of precincts reporting, Clinton had 48.5 percent of the statewide vote, compared with 46.2 percent for Trump — a margin of less than 83,000 votes out of more than 3.4 million total.

Clinton was aided in winning the state’s 13 electoral votes by her running mate, Sen. Tim Kaine, a well-liked former Virginia governor who got his start 20 years ago on the Richmond City Council.

Trump and his running mate, Indiana Gov. Mike Pence, drew heavy support among rural and white voters, but the Democrats were stronger in the more diverse urban and suburban areas around Northern Virginia, parts of Hampton Roads and Richmond.

On its surface, the win seems to confirm the evolution of Virginia from red state to blue. It was the only Southern state that went for Clinton. But the reality is more complicated: Trump was seen by many voters as a unique candidate, and other Republicans in congressional races fared better than he did.

In one of the nation’s most closely watched contests, Republican Rep. Barbara Comstock managed a comfortable victory over Democratic challenger LuAnn Bennett in the 10th District. With 94 percent of precincts reporting, Comstock outdueled Bennett 54 percent to 46 percent in what had been a hard-fought race.

The Trump candidacy highlighted a growing division among voters in Virginia, as large numbers of white residents, particularly those without a college education, saw Trump as the one political figure who captured their anger and frustration with a system that seemed indifferent to them.

What happens next, with Virginia facing a gubernatorial race next year and the Republican Party divided between Trump supporters and mainstream candidates who had distanced themselves from him, will be a high-profile test of how the party regroups.

The question is especially fraught because of how close Trump came to winning in the Old Dominion. As results trickled in and Trump pulled out to an early and persistent lead, Clinton supporters began to fear the worst. At the victory party held by state Democrats in Falls Church, the crowd at the State Theatre grew quieter, and hugs turned to reassuring pats on the back.

Relief over the statewide victory was tempered by Clinton’s weaker-than-expected showing around the country. “We’ve done a great job with new Americans, with Latinos, with women,” said Rep. Don Beyer, who easily won reelection to his Northern Virginia seat. “But the white working-class voter without a high school education that has been the core of our party have gone over to Donald Trump. What are we going to do to bring them back?”

Polling places reported heavy turnout and few glitches. Prince William County lagged behind most other jurisdictions in reporting results, though, and wound up providing the last-minute boost that pushed Clinton to a win in the state.

Gov. Terry McAuliffe, one of Clinton’s closest friends and biggest boosters, tried to reassure the crowd at the State Theatre.

“We are a blue state tonight,” McAuliffe shouted to the assembled Democrats. “Virginia went for Clinton!”

Before dawn on Tuesday, Kaine had been optimistic as he cast his ballot at his family’s longtime polling place in Richmond.

“It’s kind of like we’ve done all we can do, and now it’s in the hands of voters,” the senator said. “But we feel really comfortable about it. And it’s so nice to be back here in the neighborhood.”

Virginia has become politically sexy in recent years after shedding its longtime Republican status to support President Obama in 2008 and 2012. The state that had gone for every GOP nominee since Richard Nixon won in 1968 was suddenly in play, and in 2016 it ranked as one of the hottest swing states in the country.

Clinton’s choice of Kaine as her running mate was a sign of how crucial the state was to Democrats.

But once Trump became the Republican nominee, polls suggested that the Old Dominion looked less like a battleground and more like a win for Clinton. By late summer, the Democrats even held off on TV spending.

Early exit polls on Tuesday suggested that more Democrats than Republicans came out to vote in Virginia — a trend that began in 2008 with Obama’s first election after five straight elections of bigger GOP turnout.

Trump, who owns a golf course and a winery in Virginia, was especially strong in the central and southwest parts of the state. But in recent weeks his campaign seemed in disarray.

In October, Trump axed his Virginia campaign chairman, Corey Stewart, after Stewart complained about a lack of resources. Later, when Clinton began to fade in the polls, the Trump campaign said it would ramp up spending in Virginia. And Trump himself made a dramatic midnight appearance in Loudoun County the Saturday before Election Day.

That prompted the Clinton camp to send Kaine and Vice President Biden to George Mason University on Monday night. McAuliffe also took a more-visible role, spending the final weekend firing up campaign workers at stops around Richmond, Hampton Roads and Northern Virginia.

Trump’s campaign always had problems with women and nonwhite voters in Virginia. Exit polls Tuesday suggested that women favored Clinton by a wide margin, and nonwhite voters went for the Democrat by even more.

“As a woman and as a Muslim, he is very offensive,” Nezha Selloum, 38, said after casting her vote for Clinton at Francis Scott Key Elementary School in Arlington. Northern Virginia precincts reported particularly heavy turnout among immigrant and nonwhite voters.

A much smaller group that made its presence felt in the election: convicted felons who had their voting rights restored by McAuliffe. That move was challenged all the way to the Supreme Court of Virginia by state Republicans and lambasted by Trump on the campaign trail.

One of those new voters was Sylvester Hall, who spent a year in jail in 1979 for buying $25 worth of cigarettes with another man’s bank check. Hall was chauffeured by friends to the polling place at Bailey’s Community Center.

Grinning from ear to ear, he cast his ballot for Clinton, his aunt cheering him in the background, fists in the air. The feeling was “hard to describe,” Hall said. “It’s been beautiful.”

Hispanic voters also are emerging as an important voting bloc in Virginia. In Woodbridge, Marie Vargas, 72, said she had recently become a citizen so she could vote for Clinton. Her granddaughter, Jesenia Gomez, 19, also chose Clinton with her first vote after growing fed up with Trump’s criticism of Latinos.

“It’s time for a change and I don’t think separating people is what we need right now, “ Gomez said.

The Republican candidate’s problems with such groups had raised the question of whether the “Trump effect” would hinder Republicans running for Congress in Virginia. It did not, as Comstock sailed to a win and other races played out as expected.

In the newly redrawn 4th District, A. Donald McEachin, a Democrat who represents Henrico County in the state Senate, became only the second African American to represent Virginia in Congress since Reconstruction. Rep. Robert C. “Bobby” Scott, the other African American in the state’s delegation, was never in much danger in the 3rd District.

Whatever drama surrounding the federal races in Virginia, nothing quite matched the sideshow quality of the mayoral race in the state capital, Richmond. In a crowded field, former state delegate Joseph Morrissey continually led in the polls — despite having pleaded guilty to contributing to the delinquency of a minor for his relationship with his office assistant, who was 17 at the time.

But Morrissey, 59, fell far short of projections Tuesday and conceded the race as polls closed.

Moriah Balingit, Emma Brown, Taylor Hartz, Steve Hendrix, Jenna Portnoy and Paul Schwartzman contributed to this report.

This story has been updated; an earlier version incorrectly said Virginia Republicans challenged McAuliffe’s restoration of voting rights to the U.S. Supreme Court. It was the Supreme Court of Virginia.