Egypt's Islamist-dominated constituent assembly has approved a draft constitution, as the judiciary threatens to dissolve it amid a power struggle with President Mohammed Morsi.
The draft will now be sent to Mr Morsi, who is expected to call a referendum.
The Supreme Constitutional Court is due to rule on Sunday on whether the assembly should be dissolved.
Senior judges have been in a stand-off with the president since he granted himself sweeping new powers.
An emergency decree issued last week said Mr Morsi's decisions could not be revoked by any authority, including the judiciary, until the new constitution had been ratified and a fresh parliamentary election is held.
It also stated that the courts could not dissolve the constituent assembly.
The president insists the powers he has taken are meant to be temporary and will protect the transition to a constitutional democracy, but their breadth has raised fears that he might become a new strongman and triggered mass opposition protests across the country.
'A difficult birth'
The process of drafting a new constitution - to replace the one suspended after Hosni Mubarak was overthrown - began in March, but was slowed by a court ruling in April dissolving the first constituent assembly, amid accusations that it was dominated by Islamists.
In June, political parties agreed on the make-up of a new panel, which included a range of politicians, members of the armed forces, police, judiciary and trade unions, as well as Muslim and Christian leaders.
However, liberals, secularists and Coptic Christians continued to complain about the distribution of seats. Most of their representatives on the assembly boycotted its sessions over the past few months - leaving the majority Islamists with a relatively free hand - and 43 separate legal challenges to its constitutional legitimacy were filed.
Mr Morsi's decree of 22 November gave the 100-member panel until January to complete the draft constitution. But after the Supreme Constitutional Court said it would soon rule on the lawsuits, supporters of the president on the assembly decided to pass a rushed draft to head off the threat of dissolution.
During a marathon session that began on Thursday and continued through the night, the assembly voted on and passed all 234 articles.
Among the historic changes to Egypt's system of government, the draft limits the amount of time a president can serve to two four-year terms.
It also introduces some civilian oversight of the military establishment.
However, the defence minister will still be chosen from the armed forces, they will remain insulated from parliamentary oversight - although a special council that includes officers will oversee military affairs and the defence budget - and military courts will also still be able to try civilians.
The draft keeps in place an article defining "principles of Sharia", or Islamic law, as the main source of legislation. However, a new article says al-Azhar University, one of the leading authorities on Sunni Islam, must be consulted on any matters related to Sharia.
Freedom of worship is also limited to followers of the three Abrahamic religions - Islam, Christianity and Judaism - which would exclude minorities such as Bahais, who have complained of persecution in Egypt.
The draft calls for freedom from discrimination, but does not specify whether women or religious minorities are protected.
There are also contradictions in the articles protecting freedom of expression and those protecting people or religions from "insults". The draft also provides for "society" to play a role in promoting family values.
President Morsi, who is a member of the Islamist Muslim Brotherhood, said on Thursday that he expected to call a referendum very soon on the draft, the creation of which he described as "a difficult birth from the womb of an ancient nation".
"The most important thing of this period is that we finish the constitution, so that we have a parliament under the constitution, elected properly, an independent judiciary, and a president who executes the law," he added.
However, the leading opposition figure, Mohamed ElBaradei, said the draft constitution would be "consigned to the garbage bin of history", and would only exacerbate the current divisions in Egypt.
The Coptic Church's chief negotiator, Kamal Saleh, said it was "not the constitution of Egypt". The Church, whose members make up about 10% of Egypt's population, boycotted the vote.