Key lawmakers announced a tentative deal late Monday that would avert another government shutdown at the end of the week while denying President Trump much of the money he’s sought to build new walls along the U.S.-Mexico border.

The agreement came together during intense hours of closed-door negotiations at the Capitol, as lawmakers resurrected talks that had fallen apart over the weekend in a dispute over new Democratic demands to limit immigrant detention. Democrats ultimately dropped some of those demands, which had come under fire from Republicans, clearing the way for a deal.

Hurdles remained, and Trump’s ultimate backing was in doubt after quick opposition emerged from conservatives. But lawmakers on both sides said they were motivated to find agreement by the looming specter of another government shutdown Friday night, three weeks after the last one ended.

“What brought us back together I thought, tonight, was we didn’t want that to happen,” said Senate Appropriations Committee Chairman Richard C. Shelby (R-Ala.), the lead Republican in the talks.

House Appropriations Committee Chairman Nita M. Lowey (D-N.Y.), who was in Monday’s meetings, said she hoped the negotiators would have a finished product by Wednesday. She said she ran the proposal by House Speaker Nancy Pelosi (D-Calif.) and she signed off on it.

“Some may be happy, some may not be happy,” said Lowey, assessing how Democrats would receive the deal and saying she hoped the agreement would have the votes needed to pass the House. “We did the best we could.”

The deal includes $1.375 billion for 55 miles of fences along the border, compared with $5.7 billion Trump had sought for more than 200 miles of walls. The deal omits a strict new cap Democrats had sought on immigrants detained within the United States — as opposed to at the border. At the same time, it limits overall levels of detention beds maintained by the U.S. Immigration and Customs Enforcement agency, although GOP aides said ICE would have enough money and flexibility to maintain its current detention levels and add more when needed.

To avert a shutdown, the deal needs to be written into final legislation, passed by both the House and Senate, and signed into law by the president.

White House officials were reviewing the terms of the deal, and Shelby said he was hopeful Trump would be supportive. But details of the compromise disclosed late Monday quickly came under fire from conservatives, raising the prospect of a backlash from the right that could ultimately render it unacceptable to Trump.

Fox News host Sean Hannity, a Trump confidant, immediately called the shutdown deal a “garbage compromise.”

Beyond the wall: Dogs, blimps and other things used to secure the border

Rep. Mark Meadows (R-N.C.), a leader of the conservative House Freedom Caucus who talks regularly with Trump, said that it fails to address serious threats.

“This does not represent a fraction of what the president has promised the American people,” Meadows said in a text message. “I don’t speak for the president but I can’t imagine he will be applauding something so lacking.”

During a Tuesday morning television appearance, Lowey dismissed the criticism from conservative commentators and lawmakers.

“That probably confirms for me that it’s a good deal,” she said on CNN.

Asked if she had received any signals from the White House that Trump would accept the deal, Lowey said: “Look, I don’t listen to signals from above. I listen to the words of my colleagues who are negotiating with me. . . . I am cautiously optimistic that those who don’t want to shut the government down will endorse this bill.”

At a rally in El Paso on Monday night, Trump told a crowd of supporters that he was briefed on the conference committee’s progress as he was walking onstage. “Just so you know — we’re building the wall anyway,” Trump declared to the audience.

The president has readied a plan to declare a national emergency on the southern border, which he believes will allow him to redirect taxpayer money from other projects to build parts of a wall — without approval from Congress. Democrats are all but certain to mount a legal challenge to this approach, and many Republicans also oppose it.

The president cast the Democratic proposal on detention beds as a dangerous idea.

“I will never sign a bill that forces the mass release of violent criminals into our country,” said Trump. “And I will never abolish or in any way mistreat our great heroes from ICE and Border Patrol and law enforcement.”

He added: “We need the wall and it has to be built, and we want to build it fast.”

And Trump defended the ­record-long 35-day government shutdown that ended late last month — even though polling suggests voters largely blamed him for the impasse.

“If we didn’t do that shutdown, we would not have been able to show this country, these politicians, the world, what the hell is happening with the border. That was a very important thing we did,” Trump said.

The reaction from his conservative allies left the ultimate outcome in doubt, but negotiators said that with the president’s assent, there would be time for the legislation to pass the House and Senate and be signed ahead of the Friday midnight deadline when large portions of the government, including the Department of Homeland Security, will run out of funding and begin to shut down.

Negotiators said the deal would fund all government operations through the end of September, potentially removing any more shutdown threats for the remainder of the fiscal year.

Shelby, Lowey, Sen. Patrick J. Leahy (D-Vt.) and Rep. Kay Granger (R-Tex.) participated in the Monday meetings. They are the top lawmakers on a bi­partisan conference committee charged with striking a deal and staving off another shutdown.

Asked why she thought the dispute over the detention beds flared up over the weekend, Lowey paused before saying, “You’ll have to ask those who were debating it and arguing it. It’s one part of the bill. And the issue, in some communities across the country, has really become very volatile and personal to many of the members.”

The White House and congressional leaders have struggled for months to reach an agreement on a government funding bill because of major differences between Democrats and Republicans over immigration policy. Trump called for using $5.7 billion in taxpayer money to construct more than 200 miles of wall along the U.S.-Mexico border. Democrats objected to this, and Trump forced a partial government shutdown that began Dec. 22 to try to exert pressure on Congress to act. The shutdown dragged on for 35 days.

The White House and Republicans eventually backed down and agreed last month to a short-term spending bill for a number of agencies that was meant to give congressional negotiators more time to reach a longer-term deal.

Those negotiators had made steady progress but ran into trouble over the weekend. The White House had largely signaled to Republicans that it would soften its demand for wall money, convinced it could use other legal maneuvers to redirect existing funds. Instead, discussions bogged down over disagreements about how many undocumented immigrants could be detained at once. Republicans wanted flexibility in detention rules, arguing they needed to be able to adjust to account for violent criminals and others. Democrats countered that the changes Republicans sought would give the White House almost limitless powers to detain as many people as it wanted.

The unexpected dispute imperiled talks, spooking negotiators as they worried they were running out of time. Democrats signaled earlier Monday that they were more interested in cutting a deal than digging in as the Friday deadline neared, and they largely backed down by late Monday.

The discussions are the first major political test for Democrats and Republicans after the last government shutdown froze the paychecks of 800,000 federal workers.

A partial shutdown could have a broad impact on the country. Funding lapses would go beyond DHS to hit a number of other federal departments, including the Housing and Urban Development, Treasury, Agriculture and Interior departments, the Environmental Protection Agency, and the Internal Revenue Service.

Seung Min Kim, Josh Dawsey and Robert Costa contributed to this report.