By Mitra Taj

LIMA (Reuters) - Failure to ratify the U.S.-led sweeping trade pact TPP would hand China "the keys to the castle" on globalization and do nothing to solve the real problems underlying American anxiety over jobs, the top U.S. trade official said Thursday.

The tariff-slashing Trans-Pacific Partnership (TPP) has turned into a hot-button topic in the run-up to the Nov. 8 U.S. election, threatening to dampen support from lawmakers needed to pass a deal critics condemn as a job-killer.

U.S. Trade Representative Michael Froman said he was still optimistic Congress would pass the 12-member TPP, in part because China has been moving ahead with a trade deal of its own, the Regional Comprehensive Economic Partnership (RCEP), that would boost its exports and let it set labor and environmental standards in the fast-growing Asia Pacific region.

"We're one vote away from either cementing our leadership in this region and in the global trading system or ceding it to China," Froman told reporters in Lima after attending the inauguration of Peruvian President Pedro Pablo Kuczynski.

"At the end of the day I don't think Congress wants to be responsible for handing the keys to the castle to China."

Froman's defense of the TPP follows weeks of heated attacks on the deal as anti-TPP chants and signs have peppered the Republican and Democratic conventions.

Republican nominee Donald Trump has called the TPP a "death blow" for manufacturing jobs and his Democratic rival Hillary Clinton has firmed up her opposition to it in recent days.

Froman declined to comment on the candidates but said the TPP had become a "scapegoat" for legitimate concerns over income inequality, stagnant wages and jobs lost to automation. "You don't get to vote on the next generation of robots," he said, "you get to vote on trade agreements".

But the debate over TPP has also helped broaden support for domestic policies, such as infrastructure development and educational programs, to help Americans adapt to rapid economic change, Froman said.

U.S. President Barack Obama wants the TPP passed this year.

Froman ruled out renegotiating the "carefully balanced" text, but said issues were being solved by working on country implementation plans. He cited pork producers, dairy farmers and financial services as once-reluctant stakeholders that now back the TPP.

"The last major issue outstanding has to do with biologics and intellectual property rights," Froman said. "We're having good constructive conversations" with members of Congress.

(Reporting by Mitra Taj; Editing by Alison Williams)