Thanks for your code adjustment. I had also received a syntax error and could not find the error. In order not to get stuck, I had ChatGPT check the code. Now the menu works as it should. Here is the corrected code:
(To keep the code clearer, I removed some CSS classes).
<?php
$items = $pages->listed();
if ($items->isNotEmpty()):
?>
<ul class="menu-list">
<?php foreach ($items as $item): ?>
<?php $children = $item->children()->listed(); ?>
<?php if ($children->isEmpty()): ?>
<li class="menu-list-item <?php e($item->isOpen(), 'active') ?>">
<a href="<?= $item->url() ?>"><?= $item->title()->html() ?></a>
</li>
<?php else: ?>
<li class="menu-list-item">
<a href="javascript:void(0);"><?= $item->title()->html() ?></a><span class="menu-toggle"></span>
<ul class="sub-menu-item">
<?php foreach ($children as $child): ?>
<li class="<?php e($child->isOpen(), 'active') ?>">
<a href="<?= $child->url() ?>"><?= $child->title()->html() ?></a>
</li>
<?php endforeach ?>
</ul>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
<?php endif ?>