BERLIN — Angela Merkel’s Germany has emerged as a beacon of stability in a Western world rocked by Brexit and the election of Donald Trump. But despite Merkel’s successful campaign for a fourth term, Europe’s largest economy is now facing months of uncertainty as she embarks on a task that will be harder than winning an election: forming a governing coalition.

Internationally, most interest in the election has focused on the surge in support for the far-right Alliance for Germany, which began as an anti-euro party but scored 13 percent of the vote by pivoting to an anti-immigrant, anti-Islam, anti-multiculturalism message. But in practical terms, the biggest issue is that the dismal performance of Germany’s second-largest party, the center-left Social Democrats, seems to have left them determined to leave Merkel’s government and go into opposition.

The German chancellorship is not directly elected but is, instead, bestowed on whichever leader can secure backing from a majority in parliament. Merkel is, post-election, the only contender for that office. But with the Social Democrats insisting they won’t support her, her path to actually securing the required majority has become very difficult.

Before the election, there was considerable speculation in Berlin that the SPD might take this course of action — and near-universal support for doing so among party operatives and midlevel activists I spoke to — but considerable doubt that it would actually happen. The cynical view that party leaders would want to maintain the perks and powers of office by accepting cabinet jobs from Merkel in exchange for supporting her in parliament generally prevailed.

“It’s the right choice, but it will be hard to do,” one veteran SPD campaign manager told me before the vote. “The only way to do it is to declare defeat quickly.”

But the cynics were wrong, and SPD leader Martin Schulz chose instead to opt out of government long before the final vote count was available. Appearing onstage at SPD headquarters shortly after the first wave of exit polls became available, Schulz — flanked by prominent party leaders — announced that the SPD’s poor result was due to having lost touch with its base and that the course going forward would be to leave Merkel’s governing coalition and lead the opposition. The ensuing cheers from the crowd were, tellingly, louder than those for almost anything he’d said at his closing campaign rally in Nuremberg the previous day.

The decision tosses the hot potato of government formation into the lap of Merkel, who needs to forge an awkward — and, at the federal level, unprecedented — coalition with the smallish Green Party.

The tough math of coalition formation

Pre-election, there were four parties in the 630-seat German parliament: Merkel’s center-right Christian Democrats, the center-left Social Democrats, the left-wing Die Linke, and the Greens.

The CDU governed in partnership with the Social Democrats, whose leaders in Berlin — Frank-Walter Steinmeier and Sigmar Gabriel — served as Germany’s (largely ceremonial) president and foreign minister, respectively. This so-called “grand coalition” made it difficult for the SPD to distinguish itself from Merkel, which is one reason they reached beyond the Berlin leadership to nominate Schulz, who led the Social Democratic bloc in the European Parliament in Brussels, as their election leader.

The election saw both the CDU and SPD lose seats, while the Greens and Die Linke stayed about flat. The pro-business Free Democrats are making a return to parliament, and the far-right Alliance for Germany is entering parliament and gaining seats for the first time.

The resulting math is fairly simple:

The far-right Alliance for Germany party will hold 94 seats, and all other parties have agreed not to form a coalition with them.

The two mainstream conservative parties, Merkel’s CDU and the smaller Free Democratic Party, have a combined 326 seats, 29 seats short of what they need to form a majority.

The three left-of-center parties — the Social Democrats, the Greens, and the far-left Die Linke — have a combined 287 seats, far short of a majority.

Neither a right-wing nor a left-wing coalition is possible, in other words, meaning Merkel needs the cooperation of either the Social Democrats or the Greens to govern. And with the Social Democrats taking themselves out of the running, that means she needs the Greens. But the Greens are so small that she needs to get their support without losing the backing of the Free Democrats.

And that means that while the math is simple, the politics are extremely complicated.

The SPD is fed up with grand coalitions

The SPD and CDU formed a grand coalition after the very close 2005 election, where Merkel’s party narrowly beat then-Chancellor Gerhard Schröder’s Social Democrats. Schröder stepped down as chancellor, Merkel took over, and Schröder’s successors in the SPD took up seats in her cabinet.

For their trouble, the SPD was greeted with a political catastrophe as the party’s basic identity eroded.

“It’s called the Social Democrats,” one SPD supporter told me at the party’s election night soirée at the Willy Brandt Haus in Berlin, “but these days there’s nothing much social about them.”

He, like many professional campaign operatives in Berlin and state-level SPD politicians, hopes that an opposition role will help lead to an ideological and policy revival.

On a more high-minded level, Social Democrats say they want to lead the parliamentary opposition to Merkel. If they don’t, they say, then the role of official opposition would devolve to the far-right party, giving it further resources and prominence and possibly powering an additional electoral surge. But that leaves the other parties with the difficult task of forging a coalition agreement.

A “Jamaica” coalition is untested and difficult

The alternative to a grand coalition is what Germans call a “Jamaica” coalition between the CDU (whose color is black), the FDP (whose color is yellow), and the Greens (whose color, obviously, is green).

One such coalition currently exists on the state level, another has existed in the past, and there are currently two black-green coalitions running other German states. But “Jamaica” is an untested political formula on the federal level, and the state-level precedents haven’t been incredibly successful.

A big issue here is that the FDP, which in the past had a sort of libertarian orientation, has repositioned itself recently as a more straightforwardly right-wing party even as Merkel has moved the CDU to the center.

Today’s Free Democrats are to Merkel’s right on questions of immigration, European integration, and security versus civil liberties, while the Greens are the most pro-immigrant and pro-European of the German parties. Bringing both of those parties into her tent might appeal to Merkel, personally, since she’s most comfortable occupying the middle ground and could play coalition allies against each other. But it’s not clear what the Greens and FDP would gain from such a partnership.

Bringing them in would require Merkel to move closer to some of their positions and to be generous in doling out cabinet slots. That, in turn, would squeeze out senior members of Merkel’s own party — who might feel that defeat had thus been snatched from the jaws of victory.

The hard part starts now

The upshot is that while on one level Merkel now stands as the absolute, unquestioned master of German politics, on another level she now enters some difficult negotiations with a weak hand.

She has no rival for the chancellorship, so all the pressure is on her to make a deal. SPD leaders can say, legitimately, that they will have a hard time selling their party members on a new coalition arrangement unless they get substantial concessions. And while the FDP and Greens are individually easier to bargain with than the SPD, working out a formula that makes them both happy will be intrinsically difficult.

The growth of the far right under Germany’s proportional electoral system, combined with the refusal of other parties to work with them, means that the coalition math facing Merkel and other political leaders is increasingly difficult. The AfD’s 13 percent of the vote won’t put them in government or anywhere close to it.

But as Anne Applebaum writes, “as in the Netherlands, Austria, France, Poland — and, let’s face it, the United States, Britain, Hungary, Sweden, Finland, Italy and just about everywhere else — the nationalist far-right will now have a loud voice in mainstream politics in Germany.” That starts with the fact that their mere presence in parliament will make it difficult for Merkel to get her fourth term off to a start at all.