Egypt's highest religious authority, Al-Azhar, is emerging from the shadow of ousted President Hosni Mubarak as it tries to salvage a prestige tarnished by decades of submission to strongman leaders.
From within its tall, crenellated walls, Al-Azhar's sheikhs spent more than 1,000 years studying Islam's holy texts and interpreting their meaning for the faithful, building an authority unrivalled in the Muslim world.
Some of its luster dimmed when President Gamal Abdel Nasser brought it under the authority of the state in 1961. Forty years on, Al-Azhar's sheikhs were being dragooned into supporting the harsh security regime that cemented Mubarak three-decade rule.
With its reputation shackled to that of an unpopular leader, Al-Azhar waned just as firebrand preachers with less religious learning began spreading their dogmatic strain of Islam using the Internet and satellite TV.
A nadir was reached in 2007 when Muhammad Sayyid Tantawi, head of Al-Azhar for more than a decade until his death last year, announced that journalists who spread rumours about the state of Mubarak's health should "receive 80 lashes".
To restore its reputation, Al-Azhar's leading lights are reinventing the institution as an advocate of democracy, reform of the state and, perhaps surprisingly, secular rule.
"Many people feel there is a strong need to have such an institution in the future to preserve Islam's moderation against other waves led by stricter and less reliable organizations," said Hassan Nafaa, chairman of the political science department in Cairo University.
Change began breezing through Al-Azhar's cool stone corridors months before the uprising swept Mubarak out of office in February.
In 2010, his Prime Minister Ahmed Nazif oversaw the appointment of the more free-spirited Sheikh Ahmed El-Tayeb to replace Tantawi.
When millions took to the streets to force Mubarak from office in January, the expected Azhar fatwa (decree) demanding that Egyptians rally behind their president never came and the institution instead issued a statement merely urging restraint.
Many Azhar scholars joined hands with the protesters in the uprising, and drew no criticism from their superiors.
With Mubarak gone, it gathered together intellectuals to collaborate on a vision for Egypt's political future.
An eleven-point document published by Al-Azhar in June proposed freedom of opinion, faith and human rights in a state that would be "civilian, protected by constitution and law".
"Al-Azhar wanted to develop … to reflect the changes it saw coming," said Mohamed Rafa'a al-Tahtawi, a former Azhar spokesman who worked on a first draft of the political document.
Tahtawi said he resigned his post in February to join the uprising, but former colleagues welcomed him back to the institution as if he had never been away.
"There was no rejection from an Al-Azhar sheikh or any other official. On the contrary, I was encouraged by everyone. I think everyone knew that change was inevitable and no one could stop it coming."
What Al-Azhar says still has moral traction, even among Christians who make up some 10 percent of Egypt's 81 million people.
In a country undergoing a crisis in state education, its university offers some of the best courses in modern sciences and languages, business studies, engineering and agriculture.
It runs schools across Egypt and sends experts to teach across the Middle East and beyond. Many of the Muslim world's most influential scholars and politicians are graduates.
The gravitas endures, but restoring its authority over religious affairs has become more vital than ever after the weakening of Mubarak's security apparatus and the resurgence of groups such as Salafis who oppose the submission of religious leaders to secular authority.
Al-Azhar's leaders have begun reaching out to politicians competing to form an elected new government, including members of Islamist groups such as the Muslim Brotherhood, which was officially banned under Mubarak and is likely to play a decisive role in politics.
The ruling Supreme Council of the Armed Forces has set a timeline for a parliamentary election in November, followed by a presidential election possibly early next year.
An Azhar sheikh, Ahmed El-Tayeb, met Brotherhood leader Mohamed Badie in May. El-Tayeb said the Brotherhood was "always close to Al-Azhar but past circumstances did not allow for such meetings to take place".
"The meeting discussed the importance of reaching a unified Islamic discourse that is moderate," El-Tayeb said according to a report in Al-Ahram newspaper.
Last month, Al-Azhar mediated between liberal politicians and Islamists who opposed their attempt to lay down the principles of a new constitution — a move the Islamists saw as a ploy to block the creation of an Islamic state.
But secularists are skeptical of Al-Azhar's push into politics.
"What will happen if we disagreed with Al-Azhar on something? It will be hard to argue with a religious institution," said human rights analyst Gamal Eid, head of the Cairo-based Arabic Network for Human Rights Information (ANHRI).
"You cannot ask for a civil state and allow religious organizations to participate in politics," he said.
Al-Azhar is now pushing to be released from government control, saying its head should once again be elected by staff, not chosen by the head of state.
"Al-Azhar has realized there is no way for it to continue functioning as it did under Mubarak. It knows it must clean up internal corruption," said political analyst Nafaa.
"I can see a strong desire to return to being a leader of the Sunni Muslim world."