Egypt's army will continue to be the guardian and ultimate arbiter of the state after moving to secure real power before the victory of Mohammed Mursi, the Islamist president who triumphed in last week's election run-off.
While the generals saw no alternative to respecting the popular vote for Mursi and the Muslim Brotherhood, they see it as their obligation to forestall any attempt by Egypt's new leadership to create an Islamic state under sharia law, senior officers, intelligence officials and diplomats told Reuters.
But a reconstruction of the last two, dramatic weeks, in which Egypt looked primed to explode as the military-backed establishment gripped power more tightly and delayed the result of the presidential contest, also shows the army recognizes that democracy should now be a permanent feature of Egyptian life.
Yet such is the military's mistrust of the Brotherhood, an organization it vigorously repressed and forced underground for six decades, that a flurry of decrees, before, during and immediately after voting, aided by a dramatic court ruling, leave it unclear what powers President Mursi will really have.
The deep-seated antipathy between two historic rivals will be hard to smooth over and has already caused strains over the ceremonial formalities of the handover from military rule, under the Supreme Council of the Armed Forces (SCAF) led by Field Marshal Hussein Tantawi, to an Islamist civilian head of state.
One sticking point for the generals, officers say, is that as head of the armed forces, Tantawi may during the ceremony to transfer power have to salute a leader from a movement the army has always portrayed as a fundamental threat to Egypt. Though purely symbolic, this matters to the military. At any rate, they said, Tantawi would not let himself be filmed saluting Mursi.
The legal framework that Mursi - and more senior Brotherhood figures around him - must work within is the product of a few hasty days of activity by SCAF, which pushed Hosni Mubarak aside to appease last year's street protests, and by judges who, like the military, were appointed under the old regime.
The "deep state" that lingers from the Mubarak dictatorship - the military, civil service, judiciary and big business - had reasserted itself, said one Western diplomat.
"This is about whether the 'deep state' will ever leave Egypt to govern itself," said the diplomat, after the powers of the president were curbed to let him appoint a cabinet while being hemmed in by the army-backed establishment.
For the Brotherhood, however, the diplomat added, "An imperfect presidency is better than none at all ... It's part of a new and delicate art of political compromise."
The new set-up dates back to June 14, a day after the SCAF-appointed government had shocked rights activists by effectively renewing martial law powers for the army to arrest civilians, and two days before the two-day presidential run-off election between Mursi and former air force commander Ahmed Shafik.
The constitutional court ruled that January's parliamentary election, won by the Islamists, violated rules. SCAF ordered the parliament dissolved two days later as the presidential vote was under way.
The following evening, on June 17 as polls closed, SCAF issued a "constitutional declaration", a super-decree of many parts, in which, notably, it took for itself the legislative powers of the parliament and raised the possibility that it might replace the parliamentary body drafting a constitution.
Subsequent details on Mursi's powers to form the government and presidential administration show a division of power that leaves full control of the powerful interior and defense ministries with SCAF. The army also retains sole oversight of the military budget, as well as its extensive commercial empire.
The Brotherhood, through Mursi, will have control of finance and foreign policy, worrying liberal officials appointed during the Mubarak era who now fear for their jobs. Tantawi will remain the head of the military council as well as defense minister.
This struggle for control had been months in the making.
Although it was the radical youth of Tahrir Square who toppled Mubarak, it was the army and the Brotherhood, as by far Egypt's best-organized opposition movement, which became rivals for his inheritance.
The unexpectedly large Islamist victory in the parliamentary election held over the winter - with the Brotherhood taking 46 percent of seats and its hardline Salafist allies a further 24 percent - spurred the generals into action.
One Arab intelligence official said SCAF had moved to clip the wings of the incoming president after listening in to meetings Brotherhood leaders held in Cairo, Istanbul and Doha, at which they had discussed plans to push aside the army after taking power.
"In one of the reports that military intelligence received," this source said, "the Brotherhood said ... the president will push the army out and will carry out a popular referendum to do that after the election."
Mursi and other Brotherhood officials insist they are committed to a pluralist democracy, reject comparisons with the clerical rulers of Iran and say they respect the rights of women and minorities, as well as Egypt's peace treaty with Israel. But an 84-year-old program of establishing Islamic law - promoted energetically by Brotherhood members in parliament this year - leaves many Egyptians, and Western allies, suspicious.
Such suspicions mean it is not just the army in Egypt which has acted to curb the political power of the Islamists.
Some diplomats believe the judges who ruled against the parliamentary election were acting not under military pressure but in their own self-interest; many Mubarak-era appointees are fearful of a full-scale takeover of Egypt's institutions by the Brotherhood, and the judges had reason to believe their days on the bench would be numbered because the Islamist-led assembly had started drawing up legislation to replace them.
A Western diplomat said some members of the judiciary had indicated their opposition to the Brotherhood, and needed no direct instructions from the military on how to rein the group in. "They don't need SCAF to tell them," he said.
The Brotherhood may now reflect on the breadth of the backlash against it in Egyptian society.
Many outsiders were startled at how little public resistance there was to Mubarak-era holdovers trampling on a democratically elected parliament. Yet that reflected how far liberals and the left had come to fear the Brotherhood's social agenda and apparent ambitions to spread its control across all the country's institutions - much as the military had done before.
At a time of national crisis and economic emergency, with a constitution yet to be written, Islamists in parliament had devoted much of their few months in office to drafting measures aimed at curbing social freedoms and the rights of women.
"Some of them stood up saying really awful things" about women's issues, said one Western diplomat, adding that it sent the wrong message about priorities when politicians needed to focus on rebuilding a shattered economy.
There were divisions between pragmatists in the Brotherhood and those hardliners bent on imposing sharia, said the diplomat. But the result was that it became army policy to prevent the transfer of any power to such a parliament, because its moderation could not be relied on.
"People felt parliament has not done anything," the diplomat said, noting a common complaint leveled against the Brotherhood during Mursi's election campaign. "But we all know that it did not have power to do anything."
Parliament's poor performance in securing popular enthusiasm following its election may have emboldened the generals to act against it, and encouraged the deep state to throw its weight behind Shafik against Mursi in the presidential ballot.
Shafik, who was Mubarak's last prime minister, looked as though he might defeat Mursi right up to the moment officials announced the result a full week after the election. In a country with a history of vote-rigging, no one could be sure.
During that long week, SCAF debated intensively how to react to the result, once it became clear that Mursi was in the lead. Its ultimate reaction reveals a recognition of limits Egypt's new taste for democracy has set on its still sweeping powers.
Major-General Mohamed Assar, a member of SCAF, told Reuters two days after voting ended that the council had had nothing to do with the court decision to dissolve parliament, whose election he described as "one of the most important achievements of the military council".
"We were not happy with this court decision," he insisted. But, he added, the council's hands were tied: "We are working under the principle of the sovereignty of law and respect for the authority of the judiciary."
He went on to stress SCAF's commitment to respecting "the first president in the history of Egypt who was chosen by the people" and said: "The elected president will receive full powers with all due respect and he will lead."
The military council's legislative powers, an effective veto on any laws Mursi might want to pass, would last only until a new legislature could be elected, Assar added.
Sources close to the military council also said Mursi would be free to choose many of the ministers he wanted and to propose new laws for implementation.
"The president will have the power to form the cabinet and choose the prime minister," one SCAF member said. "It is the cabinet which will submit the draft laws to the military council which then hands it over to the president who will make a decision on whether to pass the law or reject it."
The same member acknowledged that after a vote that was the most transparent in Egypt's history, the military would have risked conflict had it tried to oppose the will of the people.
"We have to accept that there is a reality that we cannot change," the military council member said. "The first election results showed Mursi winning, and where would Shafik get an extra 600,000 votes from?" In the event, the final tally gave Mursi a more decisive margin of 880,000 votes.
"There was no way we could annul the election," the senior officer told Reuters last week, as all Egypt was on tenterhooks waiting to see which way the army would jump.
"Democratic rules are the only guarantees for the Egyptian state that we don't become like Iran," he said - in other words, a state where Islamists used initial popularity, or electoral success, to perpetuate their rule by curbing voters' choice.
"This is the beginning of real and true democracy," he said. "This election will be followed by other elections; the people will have another chance."
The council member also gave a stark outline of the military's intention to go on steering Egyptian politics.
Not hesitating to offer counsel to the new president, he said Mursi ought to pursue reconciliation: "The president should extend his hand to those who have not elected him before he extends his hand to those who elected him," he said, adding that he expected Mursi to follow a moderate, pragmatic course now he is confronted by the enormous economic problems Egypt faces.
But there was also a military warning: should Mursi and the Brotherhood try to take Egypt in the direction of Iran, where clerical rulers decree who may stand for election and which laws parliament may pass, they would come up against an army that takes its lessons from the Turkey of Ataturk.
Recalling how the founder of the republic which followed the collapse of the Ottoman empire "banned the veil and imposed restrictions on religion" as he built a secular state to replace the religiously endowed emperor, the SCAF member told Reuters that Egypt's army stood ready to act, as Turkey's had done for nearly a century, in rejecting outright Islamic rule.
"They should know that the armed forces are the pillar of stability, and the constitution should say so," he said of the Muslim Brotherhood. "We don't want privileges; we don't want a political role for the military establishment," he said of criticisms the generals simply want to entrench their power and the extensive economic privileges built up under military rule.
But, the council member concluded: "If they want to impose Islamic sharia, the army will save the country. The army won't allow the country to be taken in the wrong direction."
(Additional reporting by Edmund Blair and Marwa Awad; Editing by Alastair Macdonald)