WASHINGTON (Reuters) - President Donald Trump suffered a stunning political setback on Friday in a Congress controlled by his own party when Republican leaders pulled legislation to overhaul the U.S. healthcare system, a major 2016 election campaign promise of the president and his allies. House of Representatives leaders yanked the bill after a rebellion by Republican moderates and the party’s most conservative lawmakers left them short of votes, ensuring that Trump’s first major legislative initiative since taking office on Jan. 20 ended in failure. Democrats were unified against it. House Republicans had planned a vote on the measure after Trump late on Thursday cut off negotiations with Republicans who had balked at the plan and issued an ultimatum to vote on Friday, win or lose. But desperate lobbying by the White House and Republican House Speaker Paul Ryan was unable to round up the 216 votes needed for passage. “We learned a lot about loyalty. We learned a lot about the vote-getting process,” Trump told reporters at the White House, although he sought to shift the blame to the Democrats even though his party controls the White House, the House and the Senate. With Friday’s legislative collapse, Democratic former President Barack Obama’s signature domestic policy achievement, the 2010 Affordable Care Act - known as Obamacare - remains in place despite seven years of Republican promises to dismantle it. The healthcare failure called into question not only Trump’s ability to get other key parts of his agenda, including tax cuts and a boost in infrastructure spending, through Congress, but the Republican Party’s capacity to govern effectively. Neither Trump nor Ryan indicated any plans to try to tackle healthcare legislation again anytime soon. Trump said he would turn his attention to getting “big tax cuts” through Congress, another tricky proposition. Republican supporters said the legislation would achieve their goal of rolling back the government’s “nanny state” role in healthcare. The White House made undoing Obamacare its top priority when Trump took office two months ago. But the White House and House leaders were unable to come up with a plan that satisfied the clashing interests of moderates and conservatives, despite Trump’s vaunted image as a deal maker. Amid a chaotic scramble for votes, Ryan, who championed the bill, met with Trump at the White House. Ryan said he recommended that it be withdrawn from the House floor because he did not have the votes to pass it, and Trump agreed. “We were just probably anywhere from 10 to 15 votes short,” Trump said. “With no Democrat support we couldn’t quite get there.”  Senate Democratic leader Chuck Schumer said the bill failed “because of two traits that have plagued the Trump presidency since he took office: incompetence and broken promises.” Democrats said the bill would take away medical insurance from millions of Americans and leave the more-than-$3 trillion U.S. healthcare system in disarray. And some moderate Republicans opposed the bill because of worries that millions of America would be hurt. “There were things in this bill that I didn’t particularly like,” Trump added, without specifying what those were, but expressed confidence in Ryan’s leadership. “Perhaps the best thing that could happen is exactly what happened today, because we’ll end up with a truly great healthcare bill in the future after this mess known as Obamacare explodes,” said Trump, who had posted multiple tweets throughout March proclaiming that “Obamacare is imploding” and repeatedly saying that Republicans were coming together to pass the bill. Friday’s events cast doubt on whether Ryan can get major legislation approved by fractious Republican lawmakers. “I will not sugarcoat this. This is a disappointing day for us. Doing big things is hard,” Ryan said at a news conference, adding that his fellow Republicans are experiencing what he called “growing pains” transitioning from an opposition party to a governing party. “Obamacare’s the law of the land,” Ryan added. “We’re going to be living with Obamacare for the foreseeable future.” Members of the Freedom Caucus, the House’s most conservative members, were instrumental in the bill’s failure, opposing it among other reasons because they considered parts too similar to Obamacare. Trump said he was disappointed and “a little surprised” with the Freedom Caucus opposition. The nonpartisan Congressional Budget Office said under the Republican legislation 14 million people would lose medical coverage by next year and more than 24 million would be uninsured in 2026. News that the bill had been pulled before a final vote was greeted initially with a small sigh of relief by U.S. equity investors, who earlier in the week had been fretful that an outright defeat would damage Trump’s other priorities, such as tax cuts and infrastructure spending.     Benchmark U.S. stock market indexes ended the session mixed after rallying back from session lows following the news. The S&P 500 Index ended fractionally lower, the blue chip Dow Jones Industrial Average slipped about 0.3 percent and the Nasdaq Composite Index rose about 0.2 percent.  Shares of hospital operators finished sharply higher, with the S&P healthcare facilities index up 2.7 percent, while the S&P 500 healthcare sector edged down 0.03 percent. The dollar strengthened modestly on the news, and U.S. Treasury bond yields edged up from session lows. Trump said he would be “totally open” to working with Democrats on healthcare “when they all become civilized.” House Democratic leader Nancy Pelosi said working to lower prescription drug prices was one area of possible cooperation with Republicans. Republican Representative Dana Rohrabacher said before the bill was pulled that voting it down would be “neutering Trump” while empowering his opponents. “You don’t cut the balls off a bull and then expect that he can go out and get the job done,” Rohrabacher told Reuters. “This will emasculate Trump and we can’t do that. ... If we bring this down now, Trump will have lost all of his leverage to pass whatever bill it is, whether it’s the tax bill or whatever reforms that he wants.” Representative Joe Barton of Texas, when asked why his fellow Republicans were so united over the past seven years to dump Obamacare only to fall apart when they actually do something about it, said, “Sometimes you’re playing fantasy football and sometimes you’re in the real game.” Obamacare boosted the number of Americans with health insurance through mandates on individuals and employers, and income-based subsidies. About 20 million Americans gained insurance coverage through the law. The House plan would have rescinded a range of taxes created by Obamacare, ended a penalty on people who refuse to obtain health insurance, and ended Obamacare’s income-based subsidies to help people buy insurance while creating less-generous age-based tax credits It also would have ended Obamacare’s expansion of the Medicaid state-federal insurance program for the poor, cut future federal Medicaid funding and let states impose work requirements on some Medicaid recipients.  House leaders agreed to a series of last-minute changes to try to win over disgruntled conservatives, including ending the Obamacare requirement that insurers cover certain “essential benefits” such as maternity care, mental health services and prescription drug coverage. Click on the links below for related graphics: Graphic on Obamacare and Republican healthcare bill (tmsnrt.rs/2n0ZMKf) Graphic on poll on Americans' views of the Republican healthcare bill (tmsnrt.rs/2n7f3e4) 