German Chancellor Angela Merkel is shown in Beirut on Thursday. (Hussein Malla/AP)

Josef Joffe is a member of the editorial council for the German weekly newspaper Die Zeit and a fellow at Stanford University’s Hoover Institution.

She used to be the queen, nay, empress of Europe, but now Angela Merkel, 63, is “a chancellor on the way out,” as the German magazine Der Spiegel just put it.

The twilight of her reign is clearly under way, but why this Merkeldämmerung? Her problem is neither the German economy, which is booming, nor a weak and divided opposition. It is the enemy in her own bed: the Christian Social Union, the Bavarian wing of the Christian Democratic Union she leads.

Beset by the surge of the anti-immigrant Alternative for Germany, or AfD, the Christian Social Union is staring at polls giving it only 36 percent of the vote in the state elections on Oct. 14 — after almost four decades of holding absolute majorities in mighty Bavaria. Trying to claw back voters from the AfD, the CSU is echoing the nationalist upstart, which has vaulted from nowhere to become the Bundestag’s third-largest party.

But the CSU’s heaviest guns are trained on Mutti (Mom) Merkel. To put it in the simplest terms: Desperately straining to keep the European Union from fragmenting, the chancellor is seeking a “European solution” to the migrant influx — the distribution of refugees across the E.U. and open borders from Portugal to Poland. Merkel’s nemesis, Horst Seehofer, the CSU chairman and federal interior minister, insists on “Deutschland first.” He wants to turn refugees away at retightened German borders.

Merkel announced a deal with Seehofer on June 18 giving her two weeks to try to reach an agreement with other E.U. leaders that would placate German hard-liners on immigration. The two sister parties likely will make up — for a while. But even if Merkel makes it through this precarious summer and endures to the end of her fourth term, in 2021, she is damaged goods. Her existential problem goes far beyond the intramural political warfare. The European stage she has dominated for so long is collapsing beneath her.

Nor is it just the migrants who are battering the European construction. Merkel, this benign monarch, is being shouldered aside by a new cast of leaders: Donald Trump, Russia’s Vladi­mir Putin, Turkey’s Recep Tayyip Erdogan and, farther afield, China’s Xi Jinping. These characters prefer national advantage to world-order politics.

Closer to home, Britain is on the way out. In Italy, the anti-European populists of the left and right have grabbed power in a free election. Strongmen are running Poland and Hungary. In Austria, the far-right Freedom Party has muscled its way into the government. In much of the E.U., populist parties are scoring double-digit percentages of the vote.

Russia’s imperial ambitions are pressing on the E.U. from the east. In the west, the Atlantic skies are falling. Not a day goes by without President Trump attacking another girder of the liberal order the United States has protected for 70 years. Behold his “what do I care” stare as Merkel towered over him with barely controlled fury in a photograph taken during the recent Group of Seven summit in Quebec. Unalloyed national interest is dethroning the rules-based resolution of disputes. Europe’s cozy life under America’s strategic umbrella is turning nasty as Trump keeps growling: Pay up, or we ship out.

Still, the deadliest threat to Merkel’s tenure preceded the advent of Trump. It began to close in on her in the early fall of 2015 when she opened her heart and her country to almost 1 million Muslim migrants. Determined to show Germany’s friendly face to the world, she refused to set limits. Her fans sported “Refugees Welcome” buttons.

The welcome waned as the flow continued. Merkel’s fabled “Wir schaffen das” — “we can do it” — turned into the greatest miscalculation of her career. She barely squeezed by in the 2017 general election. Now the influx has shattered her domestic power base. After Merkel’s June 18 announcement of a temporary deal with Seehofer, Trump tweeted with apparent glee that the “people of Germany are turning against their leadership as migration is rocking the already tenuous Berlin coalition.”

Never has so much goodness spawned so much misfortune. Normally, embattled leaders seek refuge in foreign policy. But Merkel is running out of partners. Paris and Rome, Warsaw, Vienna and Budapest have opposed her “European solution” for three years. Sweden and Denmark reinstated border controls in 2016. In a new tripolar world, Trump would rather play with the big boys in Moscow and Beijing.

As the United States, Russia and China are recasting global politics, Europe, with an economy as large as America’s, has been demoted to a two-bit player. “Defensive nationalism” — keep them out — is sweeping the E.U. and the rest of the West. Merkel is frantically trying to stem the tide. Europe should hope that she can do better than King Canute.