The U.S. trade deficit widened last year to the biggest since 2012 as exports fell more than imports, though a narrowing gap in December suggests demand is stabilizing overseas for American goods. For all of 2016, the deficit increased 0.4 percent to $502.3 billion, including a wider annual gap with Mexico and a smaller one with China, Commerce Department figures showed Tuesday. The monthly shortfall shrank 3.2 percent to $44.3 billion. The median forecast in a Bloomberg survey of economists called for a deficit of $45 billion in December. While a bigger annual deficit reflects President Donald Trump’s concern that other nations are benefiting from jobs and manufacturing at America’s expense, an improving economy makes it tough to shrink the gap as steady U.S. consumer spending drives imports. December had the strongest monthly gain in exports in four years, while imports rose the most since June. Bloomberg survey estimates for the December goods-and-services trade deficit ranged from $42 billion to $46 billion. The November gap was revised to $45.7 billion from an initially reported $45.2 billion. For all of last year, exports declined 2.3 percent, while imports fell 1.8 percent. Exports in December increased 2.7 percent, the most since September 2012, to $190.7 billion, on higher sales of commercial aircraft and industrial supplies, the Commerce Department data showed. Imports rose 1.5 percent to $235 billion on purchases of motor vehicles and materials used in manufacturing. Excluding petroleum, the trade shortfall narrowed to $38.2 billion from $39.7 billion. After eliminating the influence of prices, which renders the numbers used to calculate gross domestic product, the trade deficit shrank to $62.3 billion in December from $63.9 billion in the prior month. Net exports subtracted 1.7 percentage points from fourth-quarter GDP, the most since the second quarter of 2010, figures showed last month. The wider trade deficit followed a spike in soybean shipments that boosted exports in the July-to-September period. The economy grew at a 1.9 percent annualized rate last quarter. The trade data also showed the deficit with China, the world’s second-biggest economy, shrank to $347 billion from $367.2 billion. The shortfall with Mexico expanded to a five-year high of $63.2 billion, from $60.7 billion in 2015.