DENVER – Colorado lawmakers narrowly passed an ambitious plan to rescue the state pension fund from the fiscal brink just minutes before the 2018 legislative session gaveled to a close at midnight Wednesday.

After daylong negotiations, Gov. John Hickenlooper lobbied fellow Democrats to pass the bill at a moment when their support appeared to be wavering. Opposition from the state’s largest teachers union threatened to unravel the deal reached with Senate Republicans.

“We have to think long term about the 585,000 people who are benefiting from this,” House Majority Leader KC Becker, a Democrat and one of the bill sponsors, told her caucus before the vote. “If we fail to act in a responsible way and we jeopardize anything about this retirement system, it is on our backs, and it is on our conscience.”

The pension fund provides retirement benefits to state workers, teachers and a number of other public employees across the state — around 1 in 10 Coloradans, in total — and has huge ramifications for taxpayer spending and public services across the state. The pension sports some of the largest debts of any pension in the country, owing retirees $32 billion to $50 billion in unfunded benefits.

The bill now heads to the governor’s desk. It calls for cuts to retirement benefits, and requires public employees and taxpayer-funded government agencies to contribute more of each paycheck into the retirement fund. The state will contribute $225 million annually to help pay off the system’s unfunded debt.

The bill increases employee contributions by 2 percentage points. That’s more than House Democrats and public sector unions had wanted but less than the GOP-led state Senate had previously approved.

Retirees will lose cost-of-living raises for two years. After that, annual raises will be cut to 1.5 percent, down from the current 2 percent.

The state government and school districts would pay higher contributions than they do today. And future employees would have to work longer to qualify for full benefits: The bill increases the retirement age to 64 for future employees. Currently, state workers can retire at 60, while teachers can retire at 58.

Sen. Jack Tate, a Republican bill sponsor, said it was critical that the pension measure “have a certain fairness in the retirement age between the public sector and the private sector.” Private sector retirees can’t draw full benefits from Social Security until 67.

State pension recipients in Colorado don’t pay into or receive Social Security benefits.

Late Wednesday, several Democrats called for the bill to be rejected — and suggested instead that the governor call a special session — amid opposition from the Colorado Education Association. House Speaker Crisanta Duran was among more than two dozen Democrats to vote “no.”

The legislation ultimately passed 34-29 in the House and 24-11 in the Senate.

Outside the Capitol, the teachers union ripped lawmakers for rushing a compromise that few had seen until hours before it passed. “This is bad policy done in haste,” said Kerrie Dallman, president of the Colorado Education Association.

The deal represents Colorado’s second pension rescue in the last decade.

In 2010, lawmakers cut retirement benefits and increased contributions to stave off a looming insolvency triggered by the Great Recession. The fix fell short, in part because retirees are living longer and the pension’s investments are no longer expected to grow as much as policymakers had previously projected.

The latest effort includes safeguards to automatically adjust benefits and contributions as needed to keep the pension on track to pay off its unfunded debt within 30 years.

The Democrat-led House and Republican-led Senate have resolved other top priorities of the 120-day session.

In March, lawmakers passed a $28.9 billion budget that boosts funding to transportation and schools. This week, they finalized a related K-12 funding bill that increases spending by $461 million in the fiscal year that begins July 1.

On Tuesday, lawmakers passed a bill seeking voter approval to borrow $2.34 billion for transportation projects. The bill sets aside $645 million for roads over the next two years and would ask voters in 2019 permission to issue $2.34 billion in transportation bonds. The state would owe up to $3.25 billion in borrowing costs over 20 years.

Lawmakers on Wednesday approved a last-minute compromise to reauthorize the seven-member Colorado Civil Rights Commission and the Civil Rights Division until September 2027.