Montana Gov. Steve Bullock (D) is trying to remove a Trump administration appointee from office. In doing so, he may just oust a Republican from the U.S. Senate and win the seat for himself, too.

Personnel decisions at federal agencies rarely play a role in congressional contests. But the Montana Senate race is proving to be different.

Bullock is trying to leverage a recent court victory against President Trump’s top public lands manager in his effort to beat freshman Sen. Steve Daines (R).

Both candidates are clawing over each other in the closer-than-expected race to prove how much they will protect the Big Sky state’s national parks and other natural wonders — an important draw for a state with an economy that relies on hikers and campers to soak in Montana’s beauty and spend money while doing so.

Democrats’ path to retaking the Senate, where Republicans currently have a 53-to-47 majority, may run through this rural, rugged state where changing a few minds can flip an election. Daines led Bullock by three points among likely voters surveyed by the New York Times and Siena College in mid-October, well within the margin of error.

Daines, a staunch Trump ally, has spent the second half of his Senate term bolstering his environmental record. He had taken to the airwaves with ads featuring sweeping vistas of Montana’s mountains and valleys to tout himself as a “conservative conservationist.”

His biggest legislative triumph was persuading Trump to sign the Great American Outdoors Act, one of the most important environmental laws passed in decades.

But Bullock is hoping to counter Daines with his own public lands victory in the courts.

In September, a federal judge ruled that William Perry Pendley, Trump’s pick to run the Bureau of Land Management, was holding his position illegally since he was never confirmed by the Senate.

Now Bullock is bashing Daines for initially supporting Pendley, an ultraconservative activist who once called for the federal government to sell off its land to private interests. Pendley’s assignment to lead the agency provoked an outcry among environmentalists that was loud even for the Trump era.

“Montanans know that Steve Daines supported somebody to run our public land agency that said that the Founding Fathers never intended to hold public lands,” Bullock said in a recent interview.

Brian Morris, the chief judge of the U.S. District Court in Montana, found that Pendley had been effectively acting director of the agency for well over the one year allowed under the federal law.

Daines had initially supported Pendley’s nomination to run the bureau before signaling his apprehension. The White House ultimately pulled the nomination from the Senate. Even after the court ruling, Pendley has stayed with the agency, anyway.

The BLM plays an outsize role in Western states such as Montana, where the agency oversees more than 8 million acres — an area the size of Maryland. Bullock cut his teeth as an attorney two decades ago defending Montana from a lawsuit by Pendley over access to rivers and streams — something the governor likes to point out in ads.

“People don’t realize quite how much land BLM manages, not for the public but for ranchers and resource extractors," said Daniel Gigone, who owns the Sweetwater Fly Shop about 15 miles east of Bozeman. “But then the appointment of Pendley just shows such a disdain for the thousands of folks who recreate, hunt and fish and who don’t want to see those lands sold off to developers or mining companies.”

Not only did the judge’s order oust Pendley, but it also may invalidate a wide range of decisions the conservative activist took to open up vast parts of the American West to oil and gas drilling.

While the Federal Vacancies Reform Act limits a person to serve in an acting capacity for 210 days, Pendley had been serving for 424 days when Morris ruled. And Interior Secretary David Bernhardt had extended Pendley’s tenure four times, the court noted.

The judge was clearly irked.

“The President cannot shelter unconstitutional ‘temporary’ appointments for the duration of his presidency through a matryoshka doll of delegated authorities,” Morris said, referring to the popular Russian nesting dolls.

Morris has ruled on several cases involving the oil and gas industry. In 2018, Morris invalidated 440 oil and gas lease sales because the BLM would violate protections for the greater sage grouse.

“Judge Morris is an activist judge who had to be overturned by the Supreme Court in an emergency ruling a few months ago because his decision was so poorly thought out,” Kathleen Sgamma, president of the Western Energy Alliance, said in an email. “This ruling extends well beyond his powers as a federal judge."

But Morris, who had been elected chief judge of the state court before President Barack Obama appointed him to the federal bench, makes a poor political target. A star athlete when he attended Butte Central Catholic High School, he played football at Stanford University and later clerked for then-U.S. Supreme Court Chief Justice William Rehnquist, a conservative.

“It is not hard to get the basic functions of government right, but BLM managed to step on its own feet badly," Jan Hasselman, a lawyer with the nonprofit law firm Earthjustice, said in an email. "The Governor was absolutely right to press the issue.”

Well before Bullock’s lawsuit, Daines was laying the groundwork for his own conservation victory.

In March, he went with Sen. Cory Gardner (Colo.), another Republican up for reelection, to the White House to lobby Trump to back their bill injecting $900 million a year into the Land and Water Conservation Fund. The program funnels offshore oil revenue into protecting vast swaths of wildlife habitat, as well as improving small city parks.

The White House had previously proposed slashing money for the perennially underfunded program. But Daines and Gardner told him he would be remembered like Teddy Roosevelt, the GOP’s great conservationist, if he approved it.

The flattery worked. "After about an hour, he leaned back in his chair, he crossed his arms, and he said, ‘If you can get this passed, I look forward to signing it,’ " Daines told reporters in June.

“To change the president’s tune, I would say Sen. Daines and Sen. Gardner definitely played a role in that and deserve credit for that," said Land Tawney, head of Backcountry Hunters and Anglers, a conservation group based in Missoula that is not backing either candidate.

But Daines wasn’t always the program’s biggest backer. Daines expressed opposition to full funding for it when the freshly elected senator met with hunting and conservation groups. “All of us in that room," Tawney said, “our mouths dropped to the floor.” He voted against reauthorizing the LWCF in 2015, arguing it needed revisions.

“Montanans deserve a senator who is going to fight for these values and our public lands all six years,” Bullock said.

Others are frustrated with how long it has taken for Daines to help with other matters. Nathan Varley, who runs a wildlife tourism business out of Gardiner, Mont., at the northern entrance to Yellowstone National Park, said it took years of cajoling for the senator to back a bill blocking mining near the park.

“He’s now running a campaign that claims this as a victory,” Varley said. “But he really wasn’t a driving force.”

Julia Doyle, a Daines campaign spokeswoman, said the senator has one of the strongest records in Congress on public lands. “To suggest anything else is nothing more than political desperation by Steve Bullock and his dark money allies who are grasping for straws,” she said.

Looming over any debate on public lands is climate change. Rising temperatures are fueling Montana forest fires and melting the namesake ice packs of Glacier National Park, which drew over 3 million visitors last year.

“When fishing guides can’t get clients and when ski resorts can’t stay open as long, that’s making climate change more and more real, and that’s going to trickle up to the politicians,” said Sally Mauk, a veteran analyst with Montana Public Radio in Missoula.

In September, Bullock released a wide-ranging climate solutions plan calling for more renewable power and energy-efficient homes.

But Daines, while acknowledging that humans are contributing to rising temperatures, dinged Bullock for a draft of that plan recommending a state tax on carbon emissions, calling it a “gut punch" to oil and coal workers. Bullock tweeted that the attack was “dishonest” and went on to disavow a carbon tax.

While crude oil production peaked in Montana more than a decade ago, the oil sector is a still a political force.

Bullock, who mounted a brief White House run last year, distanced himself from Democratic nominee Joe Biden’s suggestion of a “transition from the oil industry” during the last presidential debate. “Oil and gas will be part of our energy future for quite some time to come, for sure,” Bullock said.

Alexandra Ellerbeck contributed to this report.