Canada didn’t get the result it wanted on Sunday night, losing 2-0 to Brazil—but it was still enough to see the hosts through to the women’s soccer semifinals at Toronto 2015 Pan American Games.

Despite only winning one of its three group-stage games, Canada finished second in Group B ahead of Costa Rica and Ecuador, thanks to superior goal differential. That was made possible by Ecuador’s surprising 2-0 win over Costa Rica earlier on Sunday, which meant that Canada was through to the semis as long as they didn’t lose to Brazil by five goals or more.

Canada walks out against Brazil in round robin women's football on July 19, 2015 at the Pan Am Games.

Canada walks out against Brazil in round robin women’s football on July 19, 2015 at the Pan Am Games.

Goalkeeper Stephanie Labbé returned to the lineup after missing Canada’s previous game, a 2-0 loss to Costa Rica, due to a red-card suspension. Kadeisha Buchanan and Ashley Lawrence were also back in Canada’s starting eleven, after missing the Costa Rica game due to school commitments.

Those three, plus 17-year-old Jessie Fleming, are the only four members of Canada’s Pan Am squad to have been at last month’s Women’s World Cup. Brazil, meanwhile, brought the majority of its World Cup squad to this competition.

The highly experienced Brazilian team applied plenty of early pressure, but Canada’s defence, anchored by 20-year-old superstar Buchanan, held firm during a rough-and-tumble first half. Brazil’s best scoring chance came in the 42nd minute, when a long-range shot was parried by Labbé and fell into the path of Cristiane, who wasted a close-range opportunity by blasting the ball over the net.

Canada's Chelsea Stewart (in red) and Brazil's Andressa Alves battle for a ball at Pan Am Games football on July 19, 2015.

Canada’s Chelsea Stewart (in red) and Brazil’s Andressa Alves battle for a ball at Pan Am Games football on July 19, 2015.

The South Americans nearly got on the board right after the halftime break, when Labbé dropped a corner kick and Brazilian captain Formiga turned the ball into the net with a bicycle kick—but the referee disallowed the goal, determining that Rafaelle had fouled Labbé.

Brazil finally did break the deadlock in the 55th minute, when Fabiana sprinted to beat Chelsea Stewart to a loose ball and crossed it in to Andressa Alves, who converted from eight yards out.

Though the crowd in Hamilton roared its approval every time the Canadian team moved forward, a tightly played second half resulted in few clear-cut scoring opportunities for either side. But in the 86th minute, Cristiane got between a pair of Canadian defenders and cracked home a half volley from 20 yards out.

Shelina Zadowsky goes up for a ball against Brazil during Pan Am Games football on July 19, 2105.

Shelina Zadowsky goes up for a ball against Brazil during Pan Am Games football on July 19, 2105.

The result sets up Canada vs. Colombia and Brazil vs. Mexico in the semifinals on Wednesday, a repeat of the semifinal matchups at the 2011 Pan Ams. That year, a full-strength Canadian side defeated Brazil in a penalty shootout to win the title.

RELATED: Canada men – v. Brazil | v. Panama
RELATED: Canada women – v. Ecuador | v. Costa Rica

With a mostly developmental squad, Canada won’t necessarily be favourites to retain the crown, but the team has now at least guaranteed themselves the opportunity to play for a spot on the Pan Am podium in front of an appreciative home crowd.