A federal judge dealt the Trump administration a blow Monday, ruling that captured migrant kids can’t be held in long-term detention.

US District Judge Dolly Gee, in Los Angeles, turned down the feds’ bid to nix the 20-day maximum stay in detention that’s been in place since 1997.

Gee blasted the Department of Justice for its “cynical attempt” to adjust the 20-day standard known as the “Flores Agreement.”

“Defendants seek to light a match to the Flores Agreement and ask this Court to upend the parties’ agreement by judicial fiat,” Gee ruled.

“It is apparent that Defendants’ Application is a cynical attempt … to shift responsibility to the Judiciary for over 20 years of Congressional inaction and ill-considered Executive action that have led to the current stalemate.”

President Trump hoped to end the Obama-era “catch and release” policy with an executive order that forced the separation of more than 2,000 kids from their parents.

But Trump had to reverse course in the face of national, bipartisan outrage.

And now the White House is facing a tight, court-ordered deadline to reunite all families separated under its former “zero tolerance” family separation policy.

All kids under 5 are supposed to be returned by Tuesday and everyone one else by July 26, under a federal court order.

The administration did not immediately say whether it would appeal Gee’s ruling.

“We disagree with the court’s ruling declining to amend the Flores Agreement to recognize the current crisis of families making the dangerous and unlawful journey across our southern border,” DOJ spokesman Devin O’Malley said in a statement.