After years of trying, Democrats expanded their influence in state capitols on Tuesday, flipping more than 300 state legislative seats while also claiming a majority of the nation’s attorney general offices.

The Democratic gains mark a significant turnaround for a party that had been losing clout in state legislatures for nearly a decade, allowing Republicans in many states to loosen restrictions on firearms, push through new voter-identification laws and weaken environmental regulations. Democrats had also ceded enormous power to Republicans to redraw congressional boundaries.

The victories — buoyed by an apparent net Democratic pickup of seven governorships — will also help fortify the party’s efforts to use states as a firewall against President Trump, including through coordinated lawsuits against the administration.

Although some returns are preliminary, Democrats appear to have won new attorney general offices in Colorado, Michigan, Nevada and Wisconsin. If confirmed, Democrats will occupy 27 of the nation’s 51 attorney general offices next year.

“We now have four more AGs in the room . . . who will be ready to hold this administration in check,” said Oregon Attorney General Ellen F. Rosenblum, the chairwoman of the Democratic Attorneys General Association.

All 50 states and the District of Columbia have an attorney general, although the functions and responsibilities of each office can vary greatly.

Over the past two years, the Democratic attorneys general have stepped up their efforts to coordinate, including weekly conference calls to discuss legal challenges against Trump. Over the past 18 months, Democratic attorneys general have filed dozens of lawsuits against Trump, including several aimed at preventing him from modifying the Affordable Care Act.

House Minority Leader Nancy Pelosi (D-Calif.) on Nov. 7 celebrated Democratic victories in the midterms and the diversity of the incoming House Democrats. (Reuters)

Josh Shapiro, the Democratic attorney general of Pennsylvania, said Wednesday that the election results reaffirm Democrats’ strategy of pushing their agenda through the courts.

“There is going to be gridlock in Washington that is going to rule the day,” Shapiro said, referring to expected partisan fights next year between the White House and a divided Congress.

“But what is clear is that the attorneys general will actually be working to get things done. . . protecting people, individual rights and being the only effective check on the federal government.”

In all, 30 states and the District held races for attorney general on Tuesday. Zack Roday, communicators director for the Republican Attorneys General Association, said in an interview that Tuesday’s election amounted to “a natural” leveling of what had been years of GOP dominance in attorney general races.

“This is a reflection of the environment and to the fact these states had been under Republican control for a long time, and these races . . . ebb and flow,” Roday said, noting that Republicans still won fiercely contested attorney general races in Florida, Ohio, Georgia, South Dakota and South Carolina.

For Democrats, however, the wins represented the party’s broader effort to rebound after it was pummeled in local and state races when President Barack Obama was in office.

In 2010, during Obama’s first midterm elections, Republicans won control of 21 additional state legislative chambers after more than 700 new GOP legislators were elected.

Before Tuesday’s election, Republicans held the majority in two-thirds of state legislative chambers. Republicans also held 33 of the nation’s 50 governorships, just one below their all-time high of 34.

Now, after new state leaders are sworn in next month or in January, Republicans will have a 27-23 advantage over Democrats in governorships, if results hold.

Democrats also will have made up ground in state legislatures.

The Democratic Legislative Campaign Committee (DLCC) said the party won at least 323 GOP-held legislative seats on Tuesday. Republicans counter that they won nearly 100 seats held by Democrats.

Still, the DLCC is confident that Democrats have won new majorities in the Colorado Senate, the New York Senate, the Maine Senate, the Minnesota House, and both the Senate and the House in New Hampshire.

Democrats also significantly eroded Republican legislative majorities in Pennsylvania, North Carolina and Arizona while also making more-modest inroads in Florida and Michigan.

In New York, Democrats won eight state Senate seats, ending about a decade of GOP control while giving their party complete control over both the legislative and executive branches of government.

State Sen. Andrea Stewart-Cousins, who will probably become the new Senate leader, said even she was surprised by the extent of her party’s wins on Long Island and in the Hudson Valley, which she credited to unusually high turnout.

“I think people realized after the 2016 elections that four years between presidential elections is a long time, but state legislatures matter,” Stewart-Cousins said.

She expects many Democratic priorities will now swiftly move through the legislative process.

“Whether it’s gun laws, or criminal justice reform, or reproductive rights issues, to acknowledging climate change, there are many things that have not been able to move because of my Republican colleagues,” Stewart-Cousins said. “We are expecting we will be able to restore trust in government for New Yorkers as well as being a progressive beacon.”

Former Florida attorney general Bill McCollum, the chairman of the Republican State Leadership Committee, said GOP losses in state races are still relatively limited considering the number of seats the party was defending.

McCollum noted that high-profile Democrats, including Obama and former U.S. attorney general Eric H. Holder Jr., have made it a priority to focus on legislative and statewide offices. Holder’s group, the National Democratic Redistricting Committee, raised more than $18 million to try to influence state elections.

“We kept both the Wisconsin House and Senate, the Michigan House and Senate, and the Senate in Pennsylvania, and those were three very big priorities for them,” McCollum said.

Even so, McCollum acknowledges that Democrats will probably be even more aggressive in targeting local and statewide GOP-held offices in 2020.

“They have finally figured out they have to pay attention,” said McCollum. “Are we concerned? Of course we are concerned — the sheer amount of money concerns us — but last night’s showing shows money is not everything.”