The Scottish and UK governments have clashed after Supreme Court judges said parts of Holyrood's Brexit legislation would not be allowed to stand.
The judges said the bill "as a whole" was within Holyrood's competence, but that MSPs had acted outwith their powers in relation to one section.
MSPs passed their own Brexit bill in March after a row with UK ministers over Westminster's EU Withdrawal Bill.
But the case was then referred to the court by UK government law officers.
The judges also said changes which were later made to the UK legislation - adding it to a special schedule of protected legislation which MSPs cannot modify - meant a further 21 provisions now could not stand.
Scottish Secretary David Mundell said the court had "provided much-needed legal clarity" that the bill "goes beyond the powers of the Scottish Parliament".
But Scottish Brexit Secretary Mike Russell claimed the UK government had "changed the rules of the game midway through the match" in an "act of constitutional vandalism".
The UK Withdrawal from the European Union (Legal Continuity) (Scotland) Bill - known as the "continuity bill" - was passed under emergency procedures with only the Conservatives and a single Lib Dem MSP voting against it.
It was drafted as an alternative to Westminster's EU Withdrawal Bill, which MSPs refused to give their consent to following a row over how powers currently exercised from Brussels will be used after Brexit.
However, Holyrood Presiding Officer Ken Macintosh wrote an official memo saying the bill was "not within the legislative competence of the parliament".
This was denied by the Scottish government's legal advisers, but paved the way for UK law officers to apply to the Supreme Court to provide "legal certainty" about whether it was valid.
A two-day hearing was held in London in July, with the UK government arguing that the bill should be struck down. However, the Scottish government's case was backed by lawyers from the Welsh and Northern Irish governments.
Lady Hale said the judges had unanimously rejected many of the UK government's arguments.
She said "the whole of the Scottish bill would not be outside the legislative competence of the Scottish Parliament", but that one section - relating to MSPs having to give consent for UK Brexit laws - was not within Holyrood's remit.
And she pointed out that the UK government had subsequently made changes to its legislation which added it to a protected schedule of the Scotland Act as soon as it became law, meaning it cannot be altered by MSPs.
This means the bill was largely competent when it was passed by MSPs in March - but that a number of sections now could not stand.
Mr Russell said parts of the bill had been "thwarted as a result of steps taken by the UK government", claiming they had "changed the rules of the game midway through the match".
He said: "This is an act of constitutional vandalism but that does not take away from the fact this judgment makes clear MSPs were perfectly entitled to prepare Scotland's laws for Brexit at the time this bill was passed.
"The UK government's arguments have been clearly rejected."
Scottish Secretary David Mundell said this was "quite wrong", insisting that it had "always been clear" what the UK legislation would do.
He said the plans to protect the UK legislation from modification by MSPs were drafted in 2017, and that amendments made after the Holyrood bill had passed were just part of this process.
He told BBC Scotland: "It's not true to say that the Scottish government did not know at the point they introduced their bill that the EU Withdrawal Bill was going to be a protected enactment.
"Seven months before they brought their bill forward, they knew, the amendment just facilitated what we said we would do at the point the bill was introduced."
What were the arguments in court?
The UK government's law officers argued in court that the Scottish legislation would "undermine" Westminster's EU Withdrawal Act and should be struck down.
Advocate General for Scotland Lord Keen told the judges that the continuity bill was "fundamentally inconsistent" with UK law, with the Withdrawal Act having been added to a special list of protected legislation which cannot be altered by the devolved administration.
He also argued that the continuity bill cut into the reserved field of international relations, saying that "withdrawal from the EU is a matter for the UK parliament, and the devolved administrations do not have a parallel legislative competence" in this area.
However Lord Advocate James Wolffe, speaking for the Scottish government, told the judges that the Holyrood bill "plainly" did not cut across UK or EU laws.
He said the bill "had effect only in the domestic order", and as such "cannot affect the UK's negotiations with the EU".
Mr Wolffe argued that it was "perfectly practical" for MSPs to prepare the statute book "in anticipation" of leaving the EU, and the bill is "carefully framed so that it does not do or anything or enable anything to be done while the UK remains a member of the EU".
He was backed by lawyers for the Welsh and Northern Irish governments, who said the bill was "elegantly and adroitly drafted".
What will happen now?
The ruling means that the EU Withdrawal Act remains the basis on which Scotland's statute book will be prepared for Brexit.
MSPs could potentially bring the continuity bill back for reconsideration at Holyrood, provided they take action on the areas highlighted by the Supreme Court.
On Thursday afternoon, Mr Wolffe said ministers would seek talks with all opposition parties about "whether that's the right way forward".
Members including Green co-convener Patrick Harvie and Labour's Neil Findlay pressed ministers to do so, with Mr Harvie saying the UK government had "clearly malign intentions" in the case.
However, Scottish Conservative MSP Adam Tomkins said what was left after the court had "eviscerated" the bill would just replicate the provisions of the UK legislation, saying that "there is no need for parliament to reconsider any of this - what parliament should do is bin it".
Mr Wolffe also defended Presiding Officer Ken Macintosh for raising concerns about the bill in the first place, saying that the point he had raised was "one that reasonable legal minds could disagree on". He said that the court's ruling "should not be taken as a criticism of the presiding officer".