TORONTO — Canada was back in a revamped North American free trade deal with the United States and Mexico late Sunday after weeks of bitter, high-pressure negotiations that brushed up against a midnight deadline.

In a joint statement, U.S. Trade Representative Robert Lighthizer and Canadian Foreign Affairs Minister Chrystia Freeland said the agreement "will strengthen the middle class, and create good, well-paying jobs and new opportunities for the nearly half billion people who call North America home."

The new deal, reached just before a midnight deadline imposed by the U.S., will be called the United States-Mexico-Canada Agreement, or USMCA. It replaces the 24-year-old North American Free Trade Agreement, which President Donald Trump had called a job-killing disaster.

The agreement reached Sunday gives U.S. farmers greater access to the Canadian dairy market. But it keeps a NAFTA dispute-resolution process that the U.S. wanted to jettison and offers Canada protection if Trump goes ahead with plans to impose tariffs on cars, trucks and auto parts imported into the United States.

"It's a good day for Canada," Prime Minister Justin Trudeau said as he left his office. Trudeau said he would have more to say Monday.

"We celebrate a trilateral deal. The door closes on trade fragmentation in the region," Jesus Seade, trade negotiator for Mexico's incoming president, said via Twitter.

Representatives for the government of Mexican president-elect Andres Manuel Lopez Obrador have called a press conference to discuss details of the trade deal on Monday.

Canada, the United States' No. 2 trading partner, was left out when the U.S. and Mexico reached an agreement last month to revamp the North American Free Trade Agreement.

The Trump administration officially notified Congress of the U.S.-Mexico trade agreement on Aug. 31. That started a 90-day clock that would let outgoing Mexican President Enrique Pena Nieto sign the new pact before he leaves office Dec. 1.

Trump threatened to go ahead with a revamped NAFTA — with or without Canada. It was unclear, however, whether Trump had authority from Congress to pursue a revamped NAFTA with only Mexico.

Some lawmakers immediately expressed relief that Canada had been reinstated in the regional trading bloc. "I am pleased that the Trump administration was able to strike a deal to modernize NAFTA with both Mexico and Canada," said Senate Finance Chairman Orrin Hatch, R-Utah. "NAFTA is a proven success."

NAFTA tore down most trade barriers between the United States, Canada and Mexico, leading to a surge in trade between the three countries. But Trump and other critics said it encouraged manufacturers to move south of the border to take advantage of low-wage Mexican wages, costing American jobs.

Trump campaigned on a promise to rewrite NAFTA — or get rid of it. Talks on a rewrite began more than a year ago. To placate Trump, Mexico agreed in August to provisions that would require 40 percent to 45 percent of a car be built in countries where auto workers earn at least $16 an hour to qualify for NAFTA's duty-free benefits.

It was surprising that the United States found it easier to cut a deal with Mexico than with Canada, a longtime ally with a high-wage economy similar to America's. "When this got started, Canada was the teacher's pet and Mexico was the problem child," said Michael Camunez, president of Monarch Global Strategies and former U.S. Commerce Department official.

But relations between Ottawa and Washington soured. In the aftermath of a disastrous G-7 summit in Quebec in June, Trump called Trudeau "weak" and "dishonest."

The two countries need each other economically. Canada is by far the No. 1 destination for U.S. exports, and the U.S. market accounts for 75 percent of what Canada sells abroad.