KANSAS CITY, Mo. — No pitcher has confounded the Yankees this season like Michael Pineda.

“We scratch our heads sometimes,” Joe Girardi said Monday afternoon. “He’ll go 5 2/3 (innings), strike out 11 guys and give up five runs. And we scratch our heads.”

Things weren’t that drastic Monday night but in many ways it was close enough.

The righthander allowed three first-inning runs before dominating the Royals, one of the sport’s hottest teams, over the next five innings, retiring 15 straight.

But two quick hits to start the seventh and a bullpen failure opened the door for a five-run inning and the Yankees, though they made it somewhat interesting late with a four-run eighth, were headed for defeat, an 8-5 setback, in front of 22,859 at Kauffman Stadium.

“It’s unusual,” Girardi, ejected in the eighth inning arguing balls and strikes, said of Pineda’s season afterward. “Because at times he’s really, really good.”

advertisement | advertise on newsday

For the Yankees (67-63), 3 1⁄2 games behind the Orioles at the start of the day for the second AL wild-card spot, it was a rough start to a six-game trip against two of the teams ahead of them in that race. Among them are the scorching Royals (69-62), who entered the night having won seven straight series and 17 of their last 21 games.

“They’re a really good team,” said Starlin Castro, who went 1-for-4 with a double and two RBIs. “They do everything.”

Royals starter Dillon Gee is 6-7 with a 4.37 ERA, faring far better as a reliever this season (2-0 with a 3.05 ERA) than as a starter (4-7).

But the 30-year-old righthander, a Met from 2010-2015, allowed just one run and four hits over six innings Monday.

Pineda, 6-11 with a 5.12 ERA and having allowed 22 homers in his previous 25 starts this season, did keep the ball in the park against the Royals, who had 11 hits compared to nine for the Yankees.

Yankees videos

Pineda had set down 15 straight before Kendrys Morales and Salvador Perez opened the seventh with singles. Girardi called on lefthander Tommy Layne, who got Alex Gordon to ground into a 3-6 force, putting runners at the corners.

In came righthander Blake Parker to face Alcides Escobar.

The shortstop fell behind 1-and-2 before crushing one to left for his fourth homer of the season to make it 6-1.

Pineda was charged with five runs and seven hits over six-plus innings. He struck out eight and walked none.

“After that (the first), I made (better) pitches and was just fighting on the mound,” Pineda said.

advertisement | advertise on newsday

Parker allowed a two-out RBI single to Cheslor Cuthbert later in the seventh to make it 7-1 and Hosmer’s RBI single off Kirby Yates made it 8-1 as the Royals sent 10 to the plate.

Jacoby Ellsbury led off the eighth against Chris Young by reaching on a catcher’s interference for the 11th time this season, extending his major-league record. The Yankees sent nine to the plate in the inning, one that saw Girardi dumped by plate umpire Brian O’Nora for disputing a called strike two on Didi Gregorius. Gregorius responded by lashing a two-run double and the Yankees would draw to 8-5 and end up bringing the tying run to the plate in pinch hitter Mark Teixeira. But Kelvin Herrera induced an inning-ending groundout and, after allowing back-to-back two-out singles by Gary Sanchez and Gregorius in the ninth, struck out Castro for his 11th save.

“Two different strike zones tonight,” Girardi said of a situation with the strike zone that had been brewing for much of the game. “It’s unfortunate. I don’t know . . . it’s frustrating because these games mean a lot.”