A brown-skinned woman wearing a tshirt and jeans holds a small boy in her arms. It's a nighttime scene, and dozens of people are behind her.

Officials in Costa Rica and Panama are confiscating migrants' passports and cellphones, denying them access to legal services and moving them between remote outposts as they wrestle with the logistics of a suddenly reversed migration flow.