IT WAS a sorry end to an historic reign. When King Juan Carlos said he was abdicating after almost 39 years on the throne, he recognised the futility of trying to regain popularity lost by scandal and arrogance. On June 18th Spain’s parliament will give effect to the decision so his son can take the throne as Felipe VI. The news was greeted with joy. “A new era!” heralded one front page. Others spoke of a “second transition”, to match the one from dictatorship to democracy that Juan Carlos helped to steer early in his reign. A country depressed by mass unemployment and corruption saw the abdication as proof that change is possible. Juan Carlos himself talked of “hope” as one of the gifts his 46-year-old son would bring a country emerging from a bruising double-dip recession.

The Spanish monarch was a victim of his own success. His aims when he was put on the throne by Francisco Franco, who appointed him as his successor, were to restore democracy and re-establish a monarchy thrown out 44 years earlier. Remarkably, he achieved both.

He began as a rare thing in the late 20th century: a monarch with considerable executive power. He gave up that power by leading his country into constitutional democracy. In return, Spaniards voted to keep the monarchy. They wanted to avoid renewed bloody confrontation between what the poet Antonio Machado called “the two Spains” (of left and right) that provoked civil war in the 1930s. Juan Carlos’s swift squashing of an attempted coup in 1981 settled his place in Spaniards’ hearts. For many years, he was one of Europe’s most popular monarchs.

The new monarchy had come with special privileges, including minimal public scrutiny of the king’s personal and financial affairs. The press complied, ignoring the king’s amorous adventures and the dodgy business dealings of those around him. The king, it was assumed, continued discreetly to exercise some power by banging politicians’ heads together or helping to negotiate deals with foreign powers. He deserved to be left alone.

Yet as Spanish democracy became more robust, this deal began to crack. A new generation that saw democracy as natural demanded transparency and higher ethical standards. The royal family did not notice. The king’s daughter, Cristina, and son-in-law, Iñaki Urdangarin, became embroiled in sleaze allegations that suggested how, at the very least, they exploited their royal status to rake in public money for consultancy work. Fraud and tax-avoidance claims against them are being investigated by the courts.

Juan Carlos also behaved as if nothing had changed. In 2012, as Spain reeled from recession, he flitted to Botswana for a free elephant-hunting trip, accompanied by a glamorous German woman. Spaniards found out only after he was injured and a special aircraft was sent to bring him home. The local branch of the World Wide Fund for Nature fired him. Juan Carlos apologised in public. But the royal family was seen to be poking its snout in the same corruption trough as some politicians. Abdication means Spaniards may forget the scandals and recall Juan Carlos’s contribution to their country’s history.

They may also benefit from some economic uplift. At a summit organised by The Economist in Madrid this week, Mariano Rajoy, the prime minister, crowed over record job growth in May. Luis de Guindos, the finance minister, announced lower borrowing because domestic demand and credit had risen faster than expected. Yet unemployment is still at 25%; it will take years to fall to pre-crisis levels.

Luis Garicano, of the London School of Economics, says a recent surge in imports risks tipping Spain back into the sort of current-account deficit that left it so exposed before. Among the political risks facing the future king are the rise of an anti-capitalist, anti-monarchist left and of Catalan separatism. Those could hurt the economy too. The winds of change could yet blow in a less happy direction.