WASHINGTON (AP) -- On a day of high drama, the Republican-controlled Congress struggled into the night Friday to pass emergency legislation to keep the Homeland Security Department in full funding for one week and avert a partial shutdown threatened for midnight.
Acting without fanfare, the Senate cleared the measure less than four hours before the deadline that would have triggered a partial shutdown at the federal agency with anti-terrorism responsibilities.
That sent the bill to the House, where only a few hours earlier, 52 rebellious Republicans unexpectedly joined with Democrats to vote down a three-week funding bill. The vote was 224-203.
Conservatives were furious that the leadership had dropped provisions repealing Obama administration directives that shield immigrants from deportation. Democrats demanded longer-term funding as their price for passage.
"You have made a mess," House Democratic Leader Nancy Pelosi said accusingly to Republicans as the vote neared.
In the aftermath, even some Republicans agreed.
"There are terrorist attacks all over world and we're talking about closing down Homeland Security. This is like living in world of crazy people," tweeted Rep. Peter King of New York, a former chairman of the Homeland Security Committee.
The debacle in the House set a chain of events in motion.
First, Homeland Security officials circulated a lengthy contingency plan indicating that about 30,000 employees could expect to be furloughed without passage of funding legislation.
Then the White House announced President Barack Obama had spoken with Pelosi and Senate Democratic Leader Harry Reid. Moments later, Majority Leader Mitch McConnell of Kentucky strode onto the Senate floor and swiftly gained approval for the seven-day measure.
Taken together, the day's roller-coaster events at the Capitol underscored the difficulty Republicans have had so far this year in translating last fall's election gains into legislative accomplishment - a step its own leaders say is necessary to establish the party's credentials as a responsible, governing party.
Republicans gained control of the Senate in November's balloting, and emerged with their largest House majority in more than 70 years.
A combination of conservative, tea party-backed Republicans on one side of the political aisle and Democrats on the other brought down the funding measure.
The first group was upset because the legislation had been stripped of changes to Obama administration directives policy that shielded millions of immigrants from the threat of deportation. Democrats opposed it in overwhelming numbers because it lacked full-year funding for the sprawling department.
Pelosi and other Democrats urged Republicans both before and after the vote to allow debate on legislation to keep the department in funds through the Sept. 30 end of the budget year - a step the GOP high command has so far refused to take.
That might get enough Democratic votes to pass the bill, but at the same time had the potential to drive away Republicans.
"It does not make any difference whether the funding is for three weeks, three months or a full fiscal year. If it's illegal, it's illegal," said Rep. Mo Brooks, R-Ala.
Further demonstrating GOP woes, House GOP leaders abruptly called off a vote on a major education bill that had attracted significant opposition from conservatives as well as Democrats and the White House.
Aides attributed that decision to the need to work separately on rounding up enough votes to pass the measure that would prevent a partial shutdown at Homeland Security.
Across the Capitol, the Senate waited all day to add its assent after playing out a series of acts in the Republicans' effort to use the measure to wring concessions on immigration from the White House.
A largely symbolic attempt to advance legislation that would repeal Obama's immigration directive of last fall failed on a vote of 57-42, three short of the 60 required.
That separate proposal was "commonsense legislation that would protect our democracy from the egregious example of executive overreach we saw in November," said Senate Majority Leader Mitch McConnell of Kentucky, who successfully led his rank and file in recent days to a decision to pass Homeland Security legislation without immigration-related provisions.
The day's developments occurred against a midnight deadline for funding the department, an agency with significant responsibilities in the nation's fight against terrorism.
An early, 240-183 test vote in the House indicated ample support for the spending bill, but a short while later the House was gaveled into recess while the search went on for support to pass the legislation itself.
Some House Republicans said the entire strategy of passing a short-term measure and seeking negotiations on a longer-term bill that included changes in Obama's immigration policy was flawed. They noting that Senate Democrats had demonstrated their ability to block any challenges to Obama's immigration policies, and that the president had vowed to veto them in any event.
"Some folks just have a harder time facing political reality than others," said Rep. Charlie Dent, R-Pa., speaking of other Republicans.
Obama's first immigration directive, in 2012, lifted the threat of deportation from many immigrants brought to the country illegally as youngsters. Another order last fall applied to millions more who are in the United States unlawfully.
Associated Press writers Charles Babington, Andrew Taylor, Matthew Daly and Laurie Kellman contributed to this report.