The presidential election in a country convulsed by crisis was headed toward a potential legal showdown early Wednesday morning, with President Trump prematurely declaring he had won even as Democratic nominee Joe Biden had paths to victory and key states continued to tally votes.

Democratic hopes for a resounding coast-to-coast repudiation of Trump over his management of the coronavirus pandemic, the economy and race relations did notmaterialize.

Instead, Trump was buoyed by projected victories in Florida and Ohio, while also keeping his margins with Biden tight in Georgia, North Carolina and Texas — all of them too tight to declare a winner as of 1 a.m.

Still, Biden showed strength with an early lead in Arizona, a traditionally Republican state where Trump was especially vulnerable. And Biden’s clearest path to clinching a majority of electoral college votes remained in sight: a trio of Rust Belt states that both campaigns had prioritized.

Michigan, Pennsylvania and Wisconsin were slow to report returns Tuesday night, especially from cities and suburban areas populated with Democrats. Both campaigns were prepared for legal challenges over ballots, meaning the result may not be clear until Wednesday or later.

Addressing the nation from the White House about 2:30 a.m., Trump challenged the integrity of the vote to an unprecedented and breathtaking degree. The president said the ongoing vote count in Georgia, Pennsylvania and other key battleground states amounted to “a major fraud on our nation,” and he vowed to file lawsuits to stop it.

Claiming a conspiracy to keep from declaring him the victor, Trump said: “This is a fraud on the American public. This is an embarrassment to our country. We were getting ready to win this election. Frankly, we did win this election.”

The president claimed: “We’ll be going to the U.S. Supreme Court. We want all voting to stop.” He added, “We don’t want them to find any ballots at 4 o’clock in the morning and add them to the list.”

Each state has its own election laws, and challenges to their procedures and practices would be filed in lower-level courts.

Earlier in the morning, Biden addressed supporters at an outdoor drive-in rally in his hometown of Wilmington, Del., where he urged Democrats to “keep the faith” and projected confidence that he ultimately would prevail.

“We knew this was going to go on, but who knew we were going to go into maybe tomorrow morning, maybe even longer?” Biden said. “But look, we feel good about where we are. We really do. I’m here to tell you tonight we believe we’re on track to win this election.

“We’re going to have to be patient until the hard work of tallying votes is finished,” Biden added. “And it ain’t over till every vote is counted.”

With voter turnout on pace to break historic records, Biden wagered that legions of women and minority voters who have recoiled from Trump’s divisive conduct in office would bring an end to his tumultuous presidency. The former vice president offered himself as a healer with the compassion and empathy he said was needed to usher in an era of civility and restore the soul of America.

Biden also sought to make history with Sen. Kamala D. Harris of California as his running mate. A daughter of Jamaican and Indian immigrants, she was trying to become the country’s first female, first Black and first Asian American to hold the No. 2 job.

But as the electoral prize of Florida appeared to slip out of reach, Biden and his aides settled in for a long slog, with aides pointing to silver linings and bracing for an extended wait as results are slowly reported in Pennsylvania and Wisconsin.

Trump, meanwhile, sought to overcome his deficit in the polls all year with an energetic final burst of campaigning in which he demonized Biden, embellished his record and promised to end the pandemic and revitalize the economy.

In the run-up to Tuesday, Trump complained about a potentially drawn-out vote count that would not be completed on election night, repeatedly attacking the Supreme Court in recent days for its rulings allowing some states to continue accepting ballots if they arrive after Election Day. He has exhibited a special focus on Pennsylvania.

Trump told supporters such a scenario could be “physically dangerous” for the country, and he has threatened to challenge late-arriving ballots. “We’re going to go in night of, as soon as that election is over, we’re going in with our lawyers,” Trump told reporters Sunday, bemoaning the potential for the counting of late-arriving ballots to delay the election results.

With the pandemic still raging across the country, more than 100 million Americans voted early in person or by mail — by far a record — and overall turnout was expected to exceed the 136.7 million who voted in the 2016 presidential campaign.

In many key states, Biden led in early vote totals, a recognition of his campaign’s push for Democrats to vote early. But Election Day voters broke for Trump, in some places by wide margins, according to preliminary exit poll data.

Also at stake in Tuesday’s elections was the battle for control of both houses of Congress. Democrats were expected to maintain or even expand the House majority they secured in 2018, but Republican control of the Senate appeared in jeopardy.

Democrats would need three seats to gain control of the upper chamber if Biden wins and four if Trump prevails. In Colorado, Democrat John Hickenlooper, a former governor, was projected to defeat Sen. Cory Gardner (R) in a state that Biden also was projected to win.

In South Carolina, where Democrat Jaime Harrison’s fierce challenge to Sen. Lindsey O. Graham (R) drew national attention and record-breaking fundraising, Graham was projected to hold on to win another term.

And in Kentucky, home to another nationally watched race, Senate Majority Leader Mitch McConnell (R) was projected to win reelection and easily dispatch a challenge from well-financed Democrat Amy McGrath.

Early returns showed Sen. Martha McSally (R-Ariz.) trailing Democrat Mark Kelly, while other Republican incumbents faced stiff challenges in Iowa, Maine, Montana and North Carolina.

Republicans picked up at least one Senate seat and had hopes of winning a second. Sen. Doug Jones (D-Ala.) was projected to lose to Republican challenger Tommy Tuberville in a heavily Republican state. And in Michigan, Democratic incumbent Gary Peters was trying to fend off a spirited challenge from Republican John James.

At Biden’s election night event in Wilmington, where Democrats had prepared to possibly celebrate an early landslide victory, campaign aides played down the national ramifications of his disappointment in Florida, where he was projected to lose the state after significantly ­underperforming in the vote-rich Miami area.

“I always knew this was going to be a tough race,” said Sen. Christopher A. Coons (D-Del.), a Biden supporter. “We can all hope for an immediate early answer. But I think we need to be patient and wait for every vote to be counted.” Coons added that Biden had “campaigned his heart out.”

Though Biden did not have to win Florida to secure the 270 electoral college votes required to win the presidency, he invested heavily in the state and dispatched the Democratic Party’s most popular figure, former president Barack Obama, to campaign in Miami on Monday night.

While Biden built up a large lead in early and absentee voting in Florida in October, Republicans gained ground as Election Day neared, with Trump winning about two-thirds of Election Day voters, according to preliminary exit polls. With almost 11 million votes counted — a significant jump from the 9.4 million total in 2016 — Trump led 51.3 percent to 47.8 percent late Tuesday.

Trump showed surprising strength in Miami-Dade County, the most populous in Florida and one that traditionally leans Democratic and is home to a large community of Cuban Americans and immigrants from Venezuela. Trump’s message to that community and to other Hispanic voters appeared to prove pivotal, as he relentlessly branded Biden as a “socialist.”

In 2016, Democrat Hillary Clinton won Miami-Dade County by a margin of almost 30 percentage points, but on Tuesday, Trump appeared to have narrowed Biden’s lead there to less than 10 points.

Meanwhile, in Central Florida, where Puerto Rican voters are a large and powerful voting bloc, Biden performed better than Clinton, but it was not enough to make up for Trump’s surge in the Miami area. Additionally, strong turnout among the state’s smaller, more rural counties also boosted Trump’s vote total, allowing him to overcome Democrats’ advantage in the state’s large cities.

Biden campaign officials said other signs from Florida could bode well. One official pointed to gains in suburban parts of the state, adding that similar movements were being seen elsewhere nationally. The official, speaking on the condition of anonymity, also said that Cuban Americans tend to vote differently from other Hispanic voters, making the Miami-Dade result less representative of national dynamics.

A quadrennial swing state that awards a whopping 29 electoral college votes, Florida drew hundreds of millions of dollars in campaign advertising this year, more than any other state.

Emotions were running high among Florida voters, according to preliminary exit poll data. About 2 in 3 Biden voters said they would be “scared” if Trump were to be reelected, while more than half of Trump voters said the same about the possibility of a Biden presidency.

The Biden campaign was seeing early signs it liked elsewhere, including outperforming Clinton’s 2016 margins in suburban counties in Ohio and in two Kentucky counties that surround Cincinnati, which they see as an indicator of how he might perform in Pennsylvania’s all-important suburban counties.

Before polls closed Tuesday, both candidates sounded notes of optimism, though they were anticipating a close finish — especially in Pennsylvania, a new bellwether that both campaigns considered key in their paths to 270 electoral votes.

“Philly’s the key! Philly is the key!” Biden said as he thanked volunteers in Philadelphia, the state’s largest city, where he was hoping to drive up turnout among Black voters and other Democrats to offset Trump’s rural strongholds.

Biden and his campaign advisers have said carrying Michigan, Pennsylvania and Wisconsin was their clearest path to victory. But with Trump on the defensive in the closing days of the campaign, the Biden campaign made a late push for a possible landslide that would include four states that traditionally lean Republican: Arizona, Georgia, North Carolina and Texas.

“Look, you can’t think of an election in the recent past where so many states are up for grabs,” Biden said. “The idea I’m in play in Texas, Georgia, North Carolina, Florida — I mean, come on.”

Trump, his voice raspy from long days of addressing back-to-back outdoor rallies, sounded confident of victory in a morning interview but later was wistful and even downbeat as he visited with campaign staffers at their headquarters in Arlington, Va.

“Winning is easy. Losing is never easy. Not for me, it’s not,” Trump said, making what for him is a rare public admission that he might not come out on top.

The election took place amid a once-in-a-century pandemic that has killed more than 232,000 people in the United States and upended the economy. The number of cases has surged across the country, including in many of the political battleground states, and public health experts have been warning that the spread could worsen heading into winter.

But Trump — whose handling of the crisis is a top reason he had trailed Biden in the polls for so many months — has been insisting on the campaign trail that the country is “rounding the curve” on the virus. The president also has been promising that a vaccine and various therapeutics will soon be widely available, which is more optimistic than the timelines provided by health officials.

Preliminary exit polls showed about a third of voters said the economy was the most important issue in their vote, while roughly 2 in 10 listed the coronavirus or racial inequality. Smaller shares named crime or health-care policy, according to the polls, conducted by Edison Research.

Among Trump supporters, the most important issue was the economy, which about 6 in 10 named. Among Biden supporters, meanwhile, roughly a third said racial inequality was the most important issue to their vote, while slightly fewer named the pandemic.

The preliminary data showed voters nationally are divided about the state of the economy. Roughly half rated it negatively, with about 2 in 10 voters calling the economy “poor” — the lowest rating available to survey takers. About half of voters rated the economy positively, with about 1 in 10 calling it “excellent.”

In 2016, by contrast, exit polling found 62 percent of voters rated the economy negatively, with 21 percent rating it “poor.”

The early exit poll data also showed that voters are divided over whether U.S. efforts to contain the coronavirus were going “well” or “badly.”

In the three “blue wall” Rust Belt states that Barack Obama carried in his 2008 and 2012 campaigns but Trump won in 2016 — Michigan, Pennsylvania and Wisconsin — the economy rated as the top issue, with roughly a third of voters citing it as the issue that mattered most, according to preliminary exit polls.

In all three states, Trump voters were far more likely to cite the economy than Biden voters, roughly a third of whom pointed instead to the pandemic as their top issue.

The outcome of the race carried especially high stakes for Trump, whose presidency has polarized the country on a wide range of fronts, including assaults on immigration and the rule of law.

With a string of unfulfilled promises and a number of national crises that have occurred on his watch, the president made a feverish effort to secure a second term and prevent his time in public office from ending in failure.

The president closed the campaign with a mad dash of boisterous rallies that defied public health guidelines and showcased the personal grievances he had amassed over the past four years.

While Trump sought to drive home a message that despite being the incumbent president, he was a political outsider with the tenacity to take on the establishment, he regularly drifted from his core pitch to court controversy. He accused doctors of profiteering from the coronavirus crisis by inflating the death count, declined multiple opportunities to disavow fringe right-wing groups, praised supporters for using pickup trucks to ambush a Biden campaign bus on a Texas highway, made baseless allegations of electoral fraud and threatened to fire the nation’s top infectious-disease expert, Anthony S. Fauci.

It was a distillation of a presidency that catered to a political base that represents a minority of the public and stoked battles with an ever-growing list of foes.

Throughout the campaign, Biden pitched himself to voters as a uniter who would usher in an era of civility, restore norms, repair foreign alliances and respect democratic institutions. His slogan, emblazoned on campaign buses, signs and T-shirts, was “restore the soul of the nation.”

Linskey reported from Wilmington, Del. Scott Clement contributed to this report.