BERLIN — While other mainstream political parties in Germany flounder in polls and struggle to answer a far-right challenge, the Greens have gained strength as a magnet for liberal-minded voters.

Offering a compassionate approach to migration, a pro-European Union stance and an emphasis on fighting climate change and protecting biodiversity, the party appears poised for an unprecedented second-place finish in traditionally conservative Bavaria in a state election Sunday. It is polling strongly ahead of an election in neighboring Hesse two weeks later.

Nationally, some recent polls have shown the Greens level with the Social Democrats, traditionally Germany's main center-left party.

The Greens have new, dynamic and relatively young leaders, a pragmatic approach that has made them a partner to parties from the center-right to the hard left in nine of Germany's 16 state governments, and clear stances on central issues.

Unlike its mainstream rivals, the party doesn't have to worry much about losing supporters to the far-right Alternative for Germany, or AfD, which entered the national parliament last year and has been the other main beneficiary as the government bogs down in infighting.

"They have settled pretty well into the big niche of higher-earning big city dwellers with global awareness," daily newspaper Sueddeutsche Zeitung wrote in an editorial.

The newspaper noted "they have an existential issue in climate protection" and said that they appear to be on the way to replacing the struggling Social Democrats — conservative Chancellor Angela Merkel's junior partners in an unhappy "grand coalition" government of Germany's traditional political heavyweights — as "a new major party."

The Greens' election slogan in Bavaria is "Give courage instead of spreading fear." Party co-leader Robert Habeck recently lamented a "brutalization of political discourse," adding: "We think the only way of answering that is to no longer let ourselves be driven by fear of making mistakes or by fear of AfD."

The Greens were keen to enter government under Merkel last year, although that required difficult compromises with her conservatives and the pro-business Free Democrats, both traditional opponents.

The Free Democrats pulled the plug after weeks of talks, but the Greens' willingness to govern appears to have paid off. National polls show their support as high as 18 percent, compared with 8.9 percent in the 2017 election. AfD, which won 12.6 percent last year, is polling at similar levels.

The Greens' current flexibility would have been hard to imagine in 1983 when the Greens, then a protest party with a penchant for beards and sunflowers, first took their seats in the German parliament.

From 1998 to 2005, they governed Germany as the junior coalition partner to Chancellor Gerhard Schroeder, a Social Democrat. Joschka Fischer, a one-time left-wing militant and taxi driver, served as a popular foreign minister.

At present, the Greens are benefiting from the weakness of the Social Democrats, their traditional partners. They also partner with Merkel's Christian Democrats in four state governments. Those include southwestern Baden-Wuerttemberg, a traditional conservative stronghold run since 2011 by Green governor Winfried Kretschmann.

Even a coalition with Bavaria's hardline conservatives appears conceivable after the upcoming election Sunday. And, for now at least, there's little sign of long-standing internal tensions between the centrist "realists" who now dominate the leadership and left-wing "fundamentalists."

Manfred Guellner, the head of the Forsa polling agency in Germany, said about half of the party's current supporters are what he calls "new Greens," centrists who don't identify as left-wing in the same way as its traditional base.

"They can be kept if you keep to a pragmatic, rational style of politics," he said.

The Greens have seen a polling bubble burst before. Their ratings soared after the 2011 Fukushima nuclear disaster in Japan, but they emerged from the 2013 national election as the smallest party in parliament.

That followed a poor campaign remembered largely for a debate about the merits of introducing a "veggie day," allowing opponents to label the Greens as politically correct killjoys who would seek to ban everyday pleasures.

"If, like after Fukushima, discussions within the Greens begin again and the 'fundamentalists' gain the upper hand, this could fade away quickly," Guellner said.