Stanford’s David Shaw mostly handed out B and C grades to his position groups after Saturday’s 17-7, season-opening victory over Northwestern, but the No. 25 Cardinal did earn one A-plus from their head coach.

While nursing a 10-7 lead for most of the final 7½ minutes of a sun-soaked afternoon on The Farm, Shaw loved the resolve his players showed on the sideline and how that translated onto the field.

“One of the things you want as a coach is: When things are difficult, I don’t have to turn into Knute Rockne. I don’t have to do all of the talking. The talking should come from the players,” Shaw said. “… There was no sense of panic. The fourth-quarter chatter was outstanding. Guys were positive. Guys were pushing each other. Guys were trying to go out there and excel.”

Casey Toohill sacked Hunter Johnson with 20 seconds remaining and forced a fumble that Jordan Fox recovered in the end zone to ice a hard-fought game between teams that might have been more entertaining competing in Trivial Pursuit.

It was fitting for Stanford’s defense to make the victory-clinching play, because the unit kept the Cardinal in the game on a day when the offense struggled to find rhythm and lost its starting quarterback at halftime.

K.J. Costello, who led the Pac-12 in passing efficiency last season, was 16-of-20 for 152 yards and a touchdown in the first half. While trying to slide to safety two seconds before the break, Costello was jarred with an Earnest Brown IV forearm to the chin.

Costello’s head snapped back, and his helmet flew off after banging on the ground. After being treated by the medical staff on the field, Costello went to the locker room and didn’t return. In his stead, Davis Mills was 7-for-14 for 81 yards and lost two fumbles.

Mostly using quick-hitting passes and taking advantage of their abilities to make would-be tacklers miss, receivers Connor Wedington and Michael Wilson combined for 13 catches for 116 yards. Running back Cameron Scarlett ran 22 times for 97 yards as Stanford showed signs of returning to its physical running style after a down 2018 when the Cardinal averaged 3.65 yards per rush (122nd of 129 FBS teams).

Already facing one of the nation’s toughest schedules, including a brutal start to the season, Stanford could now face road games at USC and No. 17 Central Florida and a home game against No. 11 Oregon without Costello. But the Cardinal have to feel better about their chances after the performance of their defense.

A group that lost top tacklers Bobby Okereke and Sean Barton for last season allowed only 210 yards and forced four turnovers. Stanford got two forced fumbles from Curtis Robinson and an interception apiece from Andrew Pryts and Paulson Adebo to ensure a 12th straight home-opening victory.

Stanford schedule Date Opponent Result/time Aug. 31 Northwestern W 17-7 Sept. 7 at USC 7:30 p.m. Sept. 14 at Central Florida 12:30 p.m. Sept. 21 Oregon TBD Sept. 28 at Oregon St. TBD Oct. 5 Washington TBD Oct. 17 UCLA 6 p.m. Oct. 26 Arizona TBD Nov. 9 at Colorado TBD Nov. 16 at Washington St. TBD Nov. 23 Cal TBD Nov. 30 Notre Dame TBD

Read More

“As a defense, we have the mentality that it’s on us,” said junior cornerback Adebo, who had five tackles and two pass breakups to go along with his interception. “If the other team doesn’t score any points, we win the game. … No matter what happens, we’re going to be the ones who have the last say on who wins the game.”

Stanford took a 10-0 lead in the closing 3:12 of the first half. Costello found Wilson on a sneak route for a 2-yard touchdown that capped a 15-play, 90-yard drive, and just before the halftime horn, Jet Toner made a career-long 51-yard field goal that was set up by Brown’s late hit on Costello.

Former walk-on quarterback TJ Green played better than Clemson transfer Johnson, but with Green getting X-rays on an injured lower leg, Johnson was the Northwestern quarterback for all but one of the Wildcats’ second-half drives.

After opening the game 4-for-10 for 40 yards and two interceptions, Johnson found some success with his legs. He ran five times for 37 yards on Northwestern’s first scoring drive, including a 2-yard run that converted a 4th-and-1 situation. John Moten IV’s 1-yard touchdown run trimmed Stanford’s lead to 10-7 with 7:49 remaining.

Northwestern still trailed by only a field goal when it got the ball with 30 seconds remaining at its own 22-yard line. That’s when Toohill knocked the ball out of Johnson’s grasp and sent it bounding into the end zone for Fox’s touchdown recovery.

“You have to live for those moments,” Toohill said. “You have to really enjoy when it’s tough and when your back is against the wall.”

Rusty Simmons is a San Francisco Chronicle staff writer. Email: rsimmons@sfchronicle.com Twitter: @Rusty_SFChron