Former White House strategist Stephen K. Bannon speaks before Senate candidate Roy Moore at a “Drain the Swamp” campaign rally in Midland City, Ala. (Jabin Botsford)

President Trump lost in Alabama on Tuesday. Again.

This time, Trump threw his support behind Roy Moore, a polarizing, wounded Republican accused of dating and making sexual advances on teenagers — and found himself presiding over a stunning repudiation of Republicans in the Deep South that could have wide reverberations for his agenda and the party he leads.

With the Tuesday night victory of Doug Jones, Democrats captured an elusive seat in Alabama, slashed the already razor-thin margin for Republicans in the United States Senate — now 51 to 49 — and embarrassed a president who bucked many in his party to back Moore.

"If you take the five best consultants in politics and tell them to lose the Alabama Senate race, I'm not sure they could do it," said Josh Holmes, a longtime adviser and former chief of staff to Senate Majority Leader Mitch McConnell (R-Ky.).

The president watched the race unfold Tuesday night after a quick holiday party at the White House, devouring the results on television and receiving updates from his political team. He was joined by Chief of Staff John F. Kelly, who stayed with the president at the White House well into the evening and took updates from the staff to pass along, a White House official said.

Trump had obsessed over the race in recent days, including asking associates last weekend at his Mar-a-Lago resort about Moore and his chances.

By late Tuesday night, aides had already begun guessing where the mercurial Trump would hurl the blame — and how he would distance himself from Moore, a man he had privately criticized for weeks even as he backed him.

Noting that Trump's attorney general, Jeff Sessions, previously held the seat, one senior White House official said, "Now Trump hates him and a Democrat is in the Senate."

In a tweet Tuesday night, the president congratulated Jones on his win, and the White House official said that, so far, Trump had not erupted at anyone. "He knew this was not a sure thing, we all told him that," this person said.

The latest internal White House polls showed the race nearly tied — with Moore losing by less than two points.

Senior White House officials — who had crowed about the president's endorsement and political instincts earlier in the day — began to criticize Moore and his campaign Tuesday night even before the results were in. One White House aide said simply trying to ride a populist, anti-establishment wave, as Trump did in 2016, is not enough for victory; the candidates also need to be quality candidates and Moore fell far short, this person said.

The election was also a devastating blow for former White House chief strategist Stephen K. Bannon, who saw Moore winning in the Republican primary, jumped to lead the parade and continued to back him even amid the allegations while urging Trump to do the same. Bannon was at Moore's victory party Tuesday night and had been crowing to associates the Republican was going to win.

"Not only did Steve Bannon cost us a critical Senate seat in one of the most Republican states in the country, but he also dragged the president of the United States into this fiasco," said Steven Law, a top McConnell ally who leads the Senate Leadership Fund.

Trump's decision to jump into the race was a divisive one in Washington, and it is unclear if the polarizing president — popular in his own party but deeply unpopular outside — helped. According to exit polls, Trump's stock has fallen in the Yellowhammer State, with almost half of the state now disapproving of his job performance.

The president put his own reputation and instincts on the line, recording a robo-call for Moore, heaping praise on him at a Friday night rally and disregarding concerns from party stalwarts and some of his own advisers that Moore could do the unthinkable: lose as a Republican in Alabama.

To many, Trump's decision looked like a self-inflicted mistake: The Republican Party had already pulled support from Moore after The Washington Post reported accusations in early November that Moore had made sexual advances toward teenagers, including one 14-year-old girl, while in his 30s.

Other Republicans had already extricated themselves from the race. For example, McConnell tried to orchestrate a political Kabuki show to remove Moore. Ivanka Trump, the president's daughter, said there was "a special place in hell" for those who prey on children. And Sen. Richard C. Shelby (R-Ala.) said he wrote in the name of a "distinguished Republican."

The backstory for Trump was complicated. He had already picked one loser in Alabama, backing Luther Strange over Moore in the GOP primary, and had expressed regret over it. And after initially agreeing for the Republican National Committee to pull its support of Moore after the allegations surfaced, Trump returned from a lengthy trip to Asia and began rethinking his decision.

He began questioning the accusations — and reminding advisers about his own experience after the explosive "Access Hollywood" tape emerged showing him boasting about groping women's genitals.

"The election was almost a carbon copy of what happened in 2016 with President Trump," Andrew Surabian, a top Bannon aide, said before the polls closed Tuesday. "All of President's Trump supporters felt they were under siege. If anyone understands what a pile-on looks like, it's President Trump."

Counselor Kellyanne Conway told Trump those who wanted Moore out were the same people who wanted him to leave the presidential race, and his political team showed him polls with Moore on the verge of winning.

Trump grew convinced that McConnell had overplayed his hand by so fervently pushing Moore to withdraw. The two men came to an agreement last week to no longer discuss the race, according to a McConnell adviser. And Trump told aides that if he didn't have enough votes in the Senate, he wouldn't get his own agenda passed.

Trump was also concerned that his base was eroding and listened to Bannon and others, who said he should eschew governing toward the middle and remember who elected him.

At first, he seemed content to torch Jones but keep a distance from Moore, arguing against his liberal record. Then last Monday, he made a surprise call to Moore and gave a full-throated endorsement. The White House told Ronna McDaniel, the RNC chair, that she would also have to reenter the race against her wishes.

Joe Trippi, a senior adviser to the Jones campaign, said it was a worrisome sign to the Democrats when Trump began to back Moore's bid.

"There wasn't a whole lot else going for him," Trippi said. "And there's probably no one who is better at it than Trump."

Paul Reynolds, a state committeeman in Alabama, said Trump jumping back in the race had been "positive, very positive." Moore's campaign was elated, according to several people.

But it wasn't enough.

For Republicans in Washington, Wednesday was expected to be a day of serious recriminations — and worries about the future. Several people close to the White House said Trump needed to reshape his political shop and get better advisers.

Surabian, the Bannon aide, said voters and Bannon would continue to blame McConnell. "These guys don't understand how hated they are outside the Beltway," he said.

Holmes, the top McConnell adviser, said in a tweet he wanted to thank Bannon for "showing us how to lose the reddest State of the Union."

"This is a lose-lose situation. This is one of the most regrettable episodes in modern political history," Holmes said. "Every terrible facet of American politics has been on full display over the past five months. The sooner this is behind us, the better it is for everyone."

One White House official also said the consensus among some in the West Wing was that McConnell deserved more blame than Bannon — or Trump. McConnell, this person said, cut off money to Moore, told voters that their candidate would face an ethics investigation, encouraged others to criticize him and tried to change the whole election.

As the results rolled in, some aides worked to ready talking points. One White House aide suggested that the West Wing would try to immediately pivot to tax reform, arguing that with an even thinner majority in the Senate, every vote was even more critical.

But many of Trump's advisers who were skeptical of Moore were at Il Canale in Georgetown, attending a holiday party thrown by Gary Cohn, Trump's chief economic adviser and a registered Democrat.