$pages
refers to the first level pages, hence the open root page will always be a first level page.
It should work if you always use $page
(i.e. the current page) as your basis. Then get the children of this page.
The first link would be $page
, with the other links going to $page->children()
If I understood correctly what you want to achieve, that is…