Nancy Pelosi’s first showdown with President Trump began with him publicly questioning her political viability. It ended with the House speaker winning an unmitigated victory and reviving her reputation as a legislative savant.

Trump’s capitulation — agreeing to reopen the federal government after a 35-day standoff without funding for a U.S.-Mexico border wall — generated rave reviews for Pelosi from fellow Democrats and grudging respect from Republicans who watched as she kept an unruly party caucus united in the face of GOP divide-and-conquer tactics.

Pelosi (D-Calif.) emerges from the shutdown as a stronger leader of her party — and more popular with the public, by early measures — as Democrats eye aggressive efforts to counter Trump’s agenda through ambitious legislation and tough oversight. That suggests the shutdown might have been a strategic misstep for Trump, in addition to a tactical error.

“He’s used to hand-to-hand combat,” said former senator Barbara Boxer (D-Calif.), a longtime Pelosi friend and partner in politics. “With Nancy, it’s hand-to-hand combat with a velvet glove, and he’s not used to it.”

Even before the shutdown began, it became a clash between Trump, 72 — the political outsider, a New Yorker born to privilege and accustomed to getting his way — and Pelosi, 78 — the oft-caricatured San Francisco liberal who was actually steeped in the street politics of her Baltimore youth and years of hardball on Capitol Hill.

When the two met in the Oval Office on Dec. 11 Trump suggested she was constrained by the fact she had not yet been formally elected speaker: “Nancy’s in a situation where it’s not easy for her to talk right now.”

Pelosi shot back: “Mr. President, please don’t characterize the strength that I bring to this meeting.”

In retrospect, the remark was more a warning than a retort. Throughout the past seven weeks, according to interviews with dozens of lawmakers and congressional aides from both parties, Trump and White House officials appeared to fundamentally misjudge Pelosi’s support among Democrats and her resolve to hold firm against border wall funding.

As recently as Thursday, Republicans indicated that they thought they might be able to break Democrats apart by painting Pelosi as intransigent and unwilling to negotiate on the wall. “I think it’s time for the Democratic Party to have an intervention with the speaker,” Rep. Liz Cheney (R-Wyo.), the Republican Conference chairwoman, told reporters.

Indeed, not all Democrats share Pelosi’s view that the wall is an “immorality,” but she kept fractious Democrats focused on a simple message: There would be no negotiations on the wall as long as the government remained closed.

“We can’t set a precedent for holding the federal workers hostage, holding anyone hostage, and using them as a bargaining tool for a policy discussion,” said Rep. Jason Crow (D-Colo.), one of many freshmen who beat a suburban Republican by running on a moderate platform. “People have different views on the right way to get [border security] done, and there’s legitimate policy differences there, but let’s have that discussion after we get our federal workers back to work.”

Tweeting late Friday, Trump vowed to keep fighting for his wall, saying the reopening of government “was in no way a concession.”

“It was taking care of millions of people who were getting badly hurt by the Shutdown with the understanding that in 21 days, if no deal is done, it’s off to the races!” he said.

But there appears to be little appetite on Capitol Hill for a reprise of the draining shutdown. Trump’s Plan B — declaring a national emergency and tapping military construction accounts to fund the wall — has unnerved many Republicans and spurred Democrats to prepare for litigation that might not be settled before Trump’s term is up.

“I think he’s finally met his match,” said Assistant Speaker Ben Ray Luján (D-N.M.). “The speaker always presents herself in public and in private with the utmost respect. But she’s firm, and she’s strong, and she understands how to wield that power.”

Throughout the standoff, Pelosi followed her own advice: Don’t get in the gutter with Trump — or, as she put it colorfully last month, don’t engage in a “tinkle contest with a skunk.” The episode was also influenced by her respect for the presidency, if not for the president himself, aides said.

In a central episode in the shutdown ordeal, Pelosi effectively blocked Trump from delivering the State of the Union address that they had mutually scheduled for Jan. 29. But Pelosi’s initial message to Trump did not cancel the invitation outright — instead, she suggested “that we work together to determine another suitable date after government has reopened for this address or for you to consider delivering your State of the Union address in writing.”

Her decision puzzled observers on Capitol Hill and in the White House — including the No. 2 Democrat in the House, Majority Leader Steny H. Hoyer (Md.), who declared in a television interview moments after the announcement that the speech had been canceled outright, a step Pelosi had carefully avoided.

Several Pelosi allies said the nuance in her letter to Trump was a sign of respect, not weakness.

“There was no way on earth that he was ever going to get in that chamber if the government was shut down,” Boxer said. “But she did it in the right way. . . . Another guy might have said in a macho battle with Trump, ‘Forget it. It’s not happening. We’re canceling it.’ I think it took him off his track for a little while. It threw him back.”

Trump did not get the hint. A day later, Trump retaliated by canceling a military flight that was set to ferry Pelosi and other Democratic lawmakers on a trip that would include a visit to U.S. troops in Afghanistan. Then this past week, after Trump indicated that he had no interest in rescheduling the speech, Pelosi informed Trump that she had no intention of calling the traditional joint session as long as the government remained closed.

Finally Trump, in late-night tweets, acknowledged that the speech would have to wait.

Speaking to a group of opinion journalists Friday, Pelosi explained the strategy: “You only start with a feather until you get to the sledgehammer.”

Though Trump’s legislative director, Shahira Knight, kept Pelosi’s chief of staff, Danny Weiss, abreast of developments, Pelosi and Trump had no direct interactions after Trump walked out of a Jan. 9 meeting in the White House Situation Room.

There, Pelosi had insisted that any short-term funding extension would not compel Democrats to agree to wall funding. Pelosi stuck to that position throughout the fight.

“Have I not been clear on the wall?” she said Friday when asked if her position had changed after the agreement to reopen the government was reached. “No, I have been very clear on the wall. I have been very clear.”

As the confrontation played out, the House moved bill after bill to reopen government agencies. Meanwhile, in the Republican-controlled Senate, Majority Leader Mitch McConnell (R-Ky.) refused to move on them without Trump’s assent — creating an imbalance of action that helped cement a perception that it was Trump and Republicans, not Pelosi and Democrats, who were keeping the government closed.

On Friday, after Trump agreed to sign the bill reopening the government, Democrats showered Pelosi with praise.

In one tweet, Rep. Karen Bass (D-Calif.) said Pelosi “should give the State of the Union since she’s obviously the one running the country.” Rep. Jackie Speier (D-Calif.) referred to the reported physical problem that disqualified Trump from the Vietnam-era draft: “@POTUS has bone spurs. @SpeakerPelosi has a backbone.” And the rapper Cardi B suggested that Pelosi had treated Trump like a pet dog.

One tweet also underscored Pelosi’s ability to unify her diverse caucus, from moderates in Trump districts to the party’s far left.

“I will tell you something most of the country probably already knows: @SpeakerPelosi does not mess around,” wrote freshman Rep. Alexandria Ocasio-Cortez (D-N.Y.), a dominant voice in the party’s liberal wing.

Said Pelosi on Friday: “Our unity is our power, and that is what, maybe, the president underestimated.”

A CBS News poll released this past week pegged Pelosi’s approval number at 39 percent, a figure higher than Trump’s and McConnell’s — and appreciably higher than seen during last year’s midterm campaign, when Republicans spent tens of millions of dollars on ads attacking Pelosi as a symbol of dysfunctional governance. Fourteen percent of Republicans surveyed said Pelosi had outnegotiated Trump during the shutdown, vs. 6 percent of Democrats who saw Trump outmaneuvering Pelosi.

Among Pelosi’s recent fans are some of the Democrats who wanted to oust her as speaker, arguing that the party needed a fresher face at the helm.

Rep. Filemon Vela (D-Tex.) said he was “more than pleased” that Pelosi had held the line against the wall. He represents a border district centered on Brownsville, where a coast-to-coast wall is widely viewed as folly.

“Those of us who represent these border districts who just think that the wall is just a total waste of money are grateful to Speaker Pelosi and Senator Schumer for the battle that they waged,” he said.

Rep. Tim Ryan (D-Ohio), who ran against Pelosi for House Democratic leader in 2016 and tried to recruit an alternative speaker after the 2018 midterms, said: “I don’t think anyone’s ever denied her ability to negotiate, to be very tough and smart in these scenarios. The irony of the whole thing is, Trump was able to run over all of the Republicans and get them to cower with every demand he had . . . and he ran into a buzz saw.”

“People are seeing her as responsible in the face of gross irresponsibility and chaos,” Ryan added. “You don’t know who else would have been better. But she’s definitely up to the task.”