From the outside, this year’s elections looked like a virtual Republican lock. Since Lyndon B. Johnson, new Democratic presidents have lost an average of 45 House and five Senate seats in the midterms. Republicans went to the polls Tuesday needing to gain just five House seats and a single Senate seat to take control, amid soaring inflation and broad dissatisfaction with the nation’s direction.

But behind the scenes, nothing came easy to Republicans this cycle, as their historic tail winds collided with the fractious reality of a political party in the midst of a generational molting. GOP leaders spent much of the last year fighting against each other or plotting against their own primary voters. They were hobbled by unprepared first-time candidates, fundraising shortfalls and Trump, whose self-concern required constant attention — right up to the eve of the election, when he forced party bosses to beg him once again to delay a presidential campaign announcement.