Media captionKing Juan Carlos signed the bill formally to remove himself from power

The reign of King Juan Carlos of Spain is in its final hours after he signed the bill formally abdicating in favour of his son, Prince Felipe.

Juan Carlos, 76, signed the bill at a ceremony in the Royal Palace in Madrid, which was attended by only 160 guests.

At midnight local time (22:00 GMT), Felipe, 46, will become king although the event will not be marked in public until Thursday morning.

The succession was endorsed by both of Spain's main political parties.

Before the signing, Juan Carlos sat with Queen Sofia to his right and Prince Felipe and Princess Letizia to his left as the content of the law was read out.

After Juan Carlos had signed the document that will end his rule, Prime Minister Mariano Rajoy also signed the law. Moments later, the assembled guests applauded, the prince's two daughters joined the royal group and the national anthem was played.

Image caption
Juan Carlos was applauded after he signed the law on his abdication

Felipe will head to the lower house of the Spanish parliament on Thursday for the first royal transition the country has seen since democracy was restored after the death of Gen Francisco Franco in 1975.

Image copyrightReutersImage caption
Juan Carlos and his son hugged after the king had signed the law on his abdication

Image copyrightAFPImage caption
Princess Leonor kissed her grandfather after he signed the law handing power to her father

The ceremony at 10:30 local time will take the form of a proclamation rather than a coronation, in part because of the economic hardship that many Spaniards have experienced in recent years.

Juan Carlos, who has been king for 39 years, formally brought his reign to an end in the Hall of Columns at the 18th Century royal palace, the same room in which Gen Franco's body lay in state in November 1975.

Father and son both wore suits which bore the insignia of the order of the golden fleece, Spanish media reported.

Felipe will face the dual challenge of trying to rehabilitate a monarchy that has been damaged by recent scandals and trying to unify a country in which a vocal minority favour republicanism, BBC correspondent Chris Morris reports from Madrid.

Media playback is unsupported on your device

Media captionChris Morris spoke to two young Spaniards about the future of the monarchy

Juan Carlos announced his decision to abdicate on 2 June, saying that a "new generation must be at the forefront... younger people with new energies".

Although he was for many years a popular monarch, his reputation has taken a knock from a corruption investigation into the business dealings his daughter's husband and an lavish elephant hunting trip he took to Botswana in April 2012 in the midst of Spain's financial crisis,

As Juan Carlos was Spain's first ruling monarch for 44 years, a new law of abdication had to be passed by both houses of parliament under the country's 1978 constitution.

When Felipe is proclaimed king on Thursday, he will swear "faithfully to fulfil his duties, to safeguard the constitution and the laws, to respect the rights of citizens and {Spain's] autonomous regions".

The future king lacks a political mandate for change, of course, and cannot undermine or second-guess Spain's democratically elected government, but he will want to exercise his right to be consulted, to encourage, and to warn.

The most serious political challenge facing the new king will be that posed by the Catalan government's decision to hold a referendum on independence on 9 November 2014, which both the central government and the constitutional court deem illegal.

The future king - who speaks Catalan and knows the region well - may be able to play a discreet role in helping political leaders find a constructive way out of a crisis that has been allowed to fester for too long.