Image caption All of Scotland's 129 MSPs were present for the final votes on the Brexit legislation

The Scottish government's alternative to the EU Withdrawal Bill has been passed by MSPs.

Ministers put forward their "continuity bill" as part of an ongoing row over UK frameworks of post-Brexit powers.

It has undergone more than 20 hours of debate and scrutiny across three weeks, and the finalised bill was backed by a margin of 95 votes to 32.

Brexit minister Mike Russell said he still hoped to strike a deal with the UK government rather than use the bill.

There is now a period of four weeks in which a Supreme Court legal challenge could be lodged by law officers. The UK government said the competence of the bill - previously questioned by Holyrood Presiding Officer Ken Macintosh - would be "considered".

The UK Withdrawal from the European Union (Legal Continuity) (Scotland) Bill was put forward as an emergency stop-gap measure after Scottish ministers warned they may not put forward the Westminster EU Withdrawal Bill for a consent vote at Holyrood.

Both UK and Scottish ministers still say they would rather come to an agreement over post-Brexit powers than have to rely on continuity bills being passed in Edinburgh and Cardiff, but no deal has been yet been finalised despite a series of high-level meetings.

The Welsh National Assembly passed its own continuity legislation just hours before the Holyrood bill.

Image caption Mike Russell told MSPs that his preference was still for a deal with the UK government over post-Brexit powers

All 129 of Holyrood's MSPs were present for the final debate and votes.

There was fierce debate once again over section 13 of the bill, despite opposition members stripping back the powers involved in the committee stage.

This part of the bill, which is not mirrored in the Westminster legislation, would allow ministers to propose "keeping pace" with EU laws post-Brexit, in order to maintain regulatory equivalence with the EU in key fields.

Tory MSP Jamie Greene claimed this was "a back door excuse for the SNP to hand powers back to Brussels", while other members said they were "very extensive" powers to be held by ministers rather than by the parliament.

However, Mr Russell argued that the provisions were "necessary", and could indeed prove "vitally important" for fields like environmental protection.

Ultimately, 40 amendments were accepted, including proposals from members of all parties.

'Respect devolution'

Mr Russell said he was still hopeful that an agreement could be struck with UK ministers over the EU Withdrawal Bill, which would render the Holyrood bill unneeded - but warned that the Westminster bill in its current form is "incompatible with the devolution settlement".

He said the continuity bill would ensure that Scottish law could "continue to operate effectively after withdrawal" should members "be unable to consent" to the UK bill, and said passing it proved MSPs were "resolute in defending devolution".

He said parliament had "risen to the occasion" of dealing with the bill in emergency procedure - which included 11 hours of scrutiny by the finance committee, sifting through 231 proposed amendments.

After the final vote, Mr Russell said: "We are still committed to discussions with the UK government. We now expect them to acknowledge the overwhelming view expressed by the Scottish Parliament - and the Welsh National Assembly - and come forward with proposals that respect the devolution settlement and allow us to come to an agreement."

Image copyright PA

The Scottish Conservatives were the only party to wholly oppose the bill, with Labour and the Greens backing it and the Lib Dems split.

Tory MSP Adam Tomkins said there were "grave doubts" about the competence of the bill, urging law officers to refer it to the Supreme Court - something Mr Russell rejected, saying ministers had "no doubt" the legislation was above board.

He contended that the bill was "bad law which we have been asked to make badly", with a "rushed, reckless" passage through parliament. He also claimed it "trespasses on reserved powers" - something he said did not "respect the devolution settlement" or the UK's constitution.

Mr Tomkins said the "real purpose" of the bill was to "sow the seeds of division within the United Kingdom", to "create legal chaos and confusion".

Labour's Neil Findlay also said he would prefer a deal to be done with UK ministers, saying: "We should not easily forget that it is the Tories that have gotten us into this mess and time is running out for them to get us out of it."

Green MSP Patrick Harvie urged ministers to "defend this bill robustly" if it is challenged, praising MSPs for maximising the amount of cross-party scrutiny MSPs managed to carry out despite the emergency timetable.

And Lib Dem Tavish Scott noted that "most of us would rather not be here", saying that the day the UK leaves the EU would be a "bad day" - adding that parliament would have a "huge workload" in the aftermath.

The Lib Dems were divided in the final vote, with Mike Rumbles saying he wanted "no part of voting for what I believe to be an illegal bill".

What happens next?

The bill has been passed by MSPs, but it is not yet on the statute books.

There is now a 28-day period in which law officers - such as Scotland's Lord Advocate James Wolffe, the UK's top law officer in Scotland, Advocate General Lord Keen, or Attorney General Jeremy Wright - can trigger a legal challenge.

If any of them think the bill might be acting beyond Holyrood's powers - as Presiding Officer Ken Macintosh thinks it might be - they could refer the case to the Supreme Court in London.

If there were a legal challenge during this period, the bill would have to wait until the case was settled before it could receive Royal Assent and actually enter into law.

This would be unprecedented at Holyrood - law officers have never sent a Scottish bill to the Supreme Court, although there have been a few examples concerning the National Assembly for Wales. As such, its hard to know what the timeline would be for any court case - but given the significance of the bill, it might be reasonable to expect the court to treat it as a high priority.

After the Queen has signed off the bill, there could still be a legal challenge from any party with a relevant interest - like in the case of alcohol minimum pricing, which was held up for several years by challenges from the drinks industry.

Of course, at any point through this process, a deal over devolved powers could be struck between Scottish and UK ministers - which would see the Scottish government move to withdraw the bill wholesale.

To this end, a "self-destruct" clause has been written into the bill, which allows MSPs to repeal the bill via a vote of parliament.