WASHINGTON (Reuters) - President Barack Obama’s bid to save his plan to spare millions of immigrants in the country illegally from deportation and give them work permits ran into trouble on Monday at the U.S. Supreme Court in a case testing the limits of presidential power.

The court, with four conservative justices and four liberals, seemed divided along ideological lines during 90 minutes of arguments in the case brought by 26 states led by Texas that sued to block Obama’s unilateral 2014 executive action that bypassed Congress.

Liberal justices voiced support for Obama’s action. The conservatives sounded skeptical. A 4-4 decision would be a grim defeat for Obama because it would uphold lower court rulings that threw out his action last year and doom his quest to revamp a U.S. immigration policy he calls broken.

More than a thousand people in favor of Obama’s action staged a raucous demonstration outside the white marble courthouse on a sunny spring day, with cheery mariachi music from a red-and-black clad band filling the air. A smaller group of Obama critics staged their own rally.

In order to win, Obama would need the support of one of the court’s conservatives, most likely Chief Justice John Roberts or Anthony Kennedy. But they both at times hit the Obama administration’s lawyer, U.S. Solicitor General Donald Verrilli, with tough questions.

Kennedy expressed concern that Obama had exceeded its authority by having the executive branch set immigration policy rather than carry out laws passed by Congress.

“It’s as if the president is setting the policy and the Congress is executing it. That’s just upside down,” Kennedy said.

A ruling is due by the end of June.

Obama’s plan was tailored to let roughly 4 million people - those who have lived illegally in the United States at least since 2010, have no criminal record and have children who are U.S. citizens or lawful permanent residents - get into a program that shields them from deportation and supplies work permits.

Obama said the program, called Deferred Action for Parents of Americans and Lawful Permanent Residents (DAPA), was aimed at preventing families from being torn apart.

The case comes during a heated presidential campaign in which the status of the roughly 11 million immigrants in the United States illegally, most of them from Mexico and other Latin American nations, has been a central theme. Immigration is also a global concern, with Europe now struggling with a flood of immigrants fleeing violence in Syria, Iraq and elsewhere.

The Republican-governed states that filed suit asserted that the Democratic president overstepped his authority provided in the Constitution while his administration said he merely provided guidance on how to enforce deportation laws.

A 4-4 ruling is possible because there are only eight justices following February’s death of conservative Antonin Scalia.

Slideshow ( 13 images )

POSSIBLE COMPROMISE

One possible compromise outcome would be that the court could uphold Obama’s plan in part while leaving some legal questions unresolved, including whether the government can provide work permits to eligible applicants.

Obama would also win if the justices decide the states had no legitimate grounds to sue. Texas said it had “standing” to sue because it would be hurt by the additional costs it would incur by providing driver’s licenses to those given legal status.

Slideshow ( 13 images )

Liberal Justice Ruth Bader Ginsburg noted the “basic problem” that the government lacks the resources to deport everyone in the country illegally, meaning it must set priorities.

“There are these people who are here to stay, no matter what,” Ginsburg said.

Liberal Justice Sonia Sotomayor criticized Texas’ argument about the economic harm caused by Obama’s action, saying millions of immigrants “are here in the shadows” and will affect the economy “whether we want (them) to or not.”

Verrilli said the federal government has regularly launched programs aimed at giving large groups of immigrants temporary legal status as part of its role establishing enforcement priorities due to limited resources.

Asked by Roberts if the government has the power to allow all immigrants who are in the country illegally to stay, Verrilli said: “Definitely not.”

Shortly before the plan was to take effect, a federal judge in Texas blocked it after the states filed suit. The New Orleans-based 5th U.S. Circuit Court of Appeals upheld that decision in November.

Obama’s executive action arose from frustration within the White House and the immigrant community about a lack of action in politically polarized Washington to address the status of people living in the United States illegally.

He took the action after House of Representatives Republicans killed bipartisan legislation, called the biggest overhaul of U.S. immigration laws in decades and providing a path to citizenship for illegal immigrants, that was passed by the Senate in 2013.

Obama, stifled by Republican lawmakers on many of his major legislative initiatives, has drawn Republican ire with his use of executive action to get around Congress on immigration policy and other matters including gun control and healthcare.