<?php
/** @var array<string, mixed> $pageData */
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <?php $themeCss = app_tenant_theme_root_css($tenantSettings ?? app_tenant_settings_defaults()); ?>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Die Kaffeeliste</title>
    <style>
        :root{
            <?= $themeCss ?>
            --line:rgba(143,158,191,.16);
            --line-strong:rgba(174,189,223,.26);
            --shadow:0 24px 64px rgba(1,6,18,.30);
            --radius:18px;
            --surface-0:#0f1720;
            --surface-1:rgba(18,26,36,.84);
            --surface-2:rgba(23,32,45,.92);
            --surface-3:rgba(29,40,55,.96);
            --text-strong:#f4f7ff;
            --text-soft:rgba(219,228,248,.74);
        }
        *{box-sizing:border-box}
        body{
            margin:0;
            min-height:100vh;
            font-family:"Aptos","Segoe UI",sans-serif;
            color:var(--text-strong);
            background:
                radial-gradient(circle at top center, rgba(88,122,255,.18), transparent 26%),
                radial-gradient(circle at 18% 12%, rgba(var(--brand-rgb),.16), transparent 22%),
                linear-gradient(180deg,var(--surface-0) 0%,color-mix(in srgb,var(--surface-0) 82%, black 18%) 100%);
        }
        a{color:inherit;text-decoration:none}
        h1,h2,h3{font-family:"Inter Tight","Aptos","Segoe UI",sans-serif;letter-spacing:-.04em}
        .button,button{
            display:inline-flex;
            align-items:center;
            justify-content:center;
            padding:10px 14px;
            border-radius:10px;
            border:1px solid transparent;
            background:var(--brand);
            color:#fff;
            font:inherit;
            font-weight:700;
            cursor:pointer;
        }
        .button--ghost{
            background:transparent;
            color:var(--text-strong);
            border-color:var(--line-strong);
        }
        .page-shell{
            width:100%;
            margin:0 0 34px;
            display:grid;
            gap:16px;
        }
        .site-header{
            position:sticky;
            top:0;
            z-index:30;
            width:100vw;
            margin-left:calc(50% - 50vw);
        }
        .site-header__inner{
            width:100%;
            margin:0;
            display:flex;
            align-items:center;
            justify-content:space-between;
            gap:18px;
            min-height:72px;
            padding:12px 24px;
            border:0;
            border-bottom:1px solid var(--line);
            border-radius:0;
            background:color-mix(in srgb,var(--surface-1) 88%, rgba(var(--brand-rgb),.12) 12%);
            box-shadow:var(--shadow);
            backdrop-filter:blur(18px);
        }
        .site-brand{display:flex;align-items:center;gap:12px;min-width:0}
        .site-brand__mark{
            width:36px;
            height:36px;
            border-radius:10px;
            display:grid;
            place-items:center;
            background:linear-gradient(135deg,#587aff 0%,#243a91 100%);
            color:#fff;
            font-weight:800;
            box-shadow:none;
        }
        .site-brand__title{margin:0;font-size:1.08rem;color:var(--text-strong)}
        .site-brand__subtitle{margin:2px 0 0;color:var(--text-soft);font-size:.9rem}
        .site-nav,.site-actions,.actions,.context,.meta{display:flex;flex-wrap:wrap;gap:10px;align-items:center}
        .site-nav__link{
            display:inline-flex;
            align-items:center;
            justify-content:center;
            min-height:34px;
            padding:0 .3rem;
            border-radius:6px;
            border:1px solid transparent;
            color:var(--text-soft);
            font-weight:600;
            font-size:.96rem;
        }
        .site-nav__link:hover{text-decoration:none;color:var(--text-strong);background:rgba(var(--brand-rgb),.09);border-color:rgba(var(--brand-rgb),.18)}
        .site-nav__link.active{background:rgba(var(--brand-rgb),.16);color:var(--text-strong);border-color:rgba(var(--brand-rgb),.28)}
        .site-more{position:relative}
        .site-more[open]{z-index:25}
        .site-more__toggle{display:inline-flex;align-items:center;justify-content:center;min-height:34px;padding:0 .6rem;border-radius:8px;border:1px solid var(--line);background:rgba(255,255,255,.03);color:var(--text-soft);font-size:.96rem;font-weight:600;cursor:pointer;list-style:none}
        .site-more__toggle::-webkit-details-marker{display:none}
        .site-more__panel{position:absolute;right:0;top:calc(100% + 10px);min-width:240px;padding:12px;border-radius:14px;border:1px solid var(--line);background:var(--surface-3);box-shadow:var(--shadow);display:grid;gap:8px}
        .site-more__label{font-size:.76rem;font-weight:800;letter-spacing:.08em;text-transform:uppercase;color:var(--text-soft)}
        .site-mobile{display:none}
        .site-mobile__toggle{
            display:none;
            align-items:center;
            justify-content:center;
            gap:10px;
            cursor:pointer;
            min-height:38px;
            padding:.45rem .72rem;
            border-radius:10px;
            border:1px solid var(--line-strong);
            background:rgba(255,255,255,.04);
            color:var(--text-strong);
            font-weight:700;
        }
        .site-mobile__toggle::before{
            content:"";
            display:block;
            width:18px;
            height:2px;
            border-radius:999px;
            background:currentColor;
            box-shadow:0 -6px 0 currentColor,0 6px 0 currentColor;
        }
        .mobile-drawer{position:fixed;inset:0;display:none;z-index:55}
        .mobile-drawer.is-open{display:block}
        .mobile-drawer__scrim{position:absolute;inset:0;background:rgba(4,10,18,.58);backdrop-filter:blur(6px)}
        .mobile-drawer__panel{
            position:absolute;
            right:0;
            top:0;
            bottom:0;
            width:min(100vw,420px);
            max-width:100%;
            padding:18px 16px calc(28px + env(safe-area-inset-bottom));
            background:var(--surface-3);
            border-left:1px solid var(--line);
            box-shadow:var(--shadow);
            display:grid;
            grid-template-rows:auto 1fr auto;
            gap:18px;
            overflow:auto;
        }
        .mobile-drawer__header{display:flex;align-items:flex-start;justify-content:space-between;gap:12px}
        .mobile-drawer__title{margin:0;font-size:1.08rem;color:var(--text-strong)}
        .mobile-drawer__subtitle{margin:4px 0 0;color:var(--text-soft);font-size:.9rem}
        .mobile-drawer__close{
            display:inline-flex;
            align-items:center;
            justify-content:center;
            width:40px;
            height:40px;
            border-radius:999px;
            border:1px solid var(--line);
            background:rgba(255,255,255,.04);
            color:var(--text-strong);
            font-size:1.15rem;
            font-weight:700;
            cursor:pointer;
        }
        .mobile-drawer__section{display:grid;gap:10px}
        .mobile-drawer__label{font-size:.76rem;font-weight:800;letter-spacing:.08em;text-transform:uppercase;color:var(--text-soft)}
        .mobile-drawer__links{display:grid;gap:8px}
        .mobile-drawer__link{
            display:flex;
            align-items:center;
            justify-content:space-between;
            gap:12px;
            padding:.9rem 1rem;
            border-radius:14px;
            border:1px solid var(--line);
            background:rgba(255,255,255,.04);
            color:var(--text-strong);
            font-weight:700;
        }
        .mobile-drawer__link.active{background:rgba(88,122,255,.16);border-color:rgba(88,122,255,.28)}
        .mobile-drawer__footer{display:grid;gap:12px;padding-top:8px;border-top:1px solid var(--line)}
        .mobile-drawer__meta{display:grid;gap:4px}
        .mobile-bottom-nav{display:none}
        .mobile-bottom-nav__link,.mobile-bottom-nav__toggle{
            display:inline-flex;
            align-items:center;
            justify-content:center;
            gap:6px;
            min-height:56px;
            padding:0 8px;
            border:0;
            background:transparent;
            color:var(--text-soft);
            font:inherit;
            font-weight:700;
        }
        .mobile-bottom-nav__link.active,.mobile-bottom-nav__toggle.active{color:var(--text-strong)}
        .mobile-bottom-nav__icon{font-size:1rem;line-height:1}
        .mobile-bottom-nav__label{font-size:.74rem}
        .hero,.card,.table-card,.note{
            border:1px solid var(--line);
            border-radius:16px;
            background:var(--surface-2);
            box-shadow:var(--shadow);
        }
        .hero__title,.card h2,.card h3{font-family:"Inter Tight","Aptos","Segoe UI",sans-serif;letter-spacing:-.04em;color:var(--text-strong)}
        .sidebar__subtitle,.muted{color:var(--text-soft)}
        .content{width:min(1260px,calc(100% - 28px));margin:0 auto;min-width:0;display:grid;gap:18px}
        .hero{
            display:grid;
            grid-template-columns:minmax(0,1.4fr) minmax(260px,.6fr);
            gap:20px;
            padding:24px;
            background:linear-gradient(180deg,rgba(18,25,42,.96),rgba(13,19,33,.92));
        }
        .hero__kicker{
            margin:0 0 10px;
            color:#8aa0d0;
            text-transform:uppercase;
            letter-spacing:.15em;
            font-size:.8rem;
            font-weight:800;
        }
        .hero__title{margin:0 0 12px;font-size:clamp(1.9rem,4vw,3rem);line-height:1.05}
        .hero__lead{margin:0;color:var(--text-soft);max-width:70ch;line-height:1.7}
        .hero__actions{display:flex;flex-wrap:wrap;gap:12px;margin-top:16px}
        .grid{display:grid;gap:18px}
        .grid--2{grid-template-columns:repeat(2,minmax(0,1fr))}
        .grid--3{grid-template-columns:repeat(3,minmax(0,1fr))}
        .grid--4{grid-template-columns:repeat(4,minmax(0,1fr))}
        .metric{padding:18px;border:1px solid var(--line);border-radius:16px;background:rgba(255,255,255,.04)}
        .metric__label{color:var(--text-soft)}
        .metric__value{font-size:1.9rem;font-weight:800;margin:6px 0 8px}
        .table-card,.card{padding:22px}
        .table-card__header{display:flex;justify-content:space-between;gap:12px;align-items:flex-start;margin-bottom:16px;flex-wrap:wrap}
        table{width:100%;border-collapse:collapse}
        th,td{padding:12px 10px;border-bottom:1px solid rgba(163,183,255,.1);text-align:left;vertical-align:top}
        th{font-size:.82rem;text-transform:uppercase;letter-spacing:.08em;color:rgba(193,205,232,.7)}
        .status{display:inline-flex;align-items:center;padding:6px 10px;border-radius:999px;background:rgba(88,122,255,.14);color:#dfe8ff;font-weight:700;font-size:.84rem}
        .status--warning{background:rgba(180,83,9,.18);color:#f5c68a}
        .status--success{background:rgba(88,122,255,.14);color:#dfe8ff}
        .list-reset{margin:0;padding-left:18px}
        .list-reset li+li{margin-top:10px}
        .note{padding:16px 18px;background:rgba(88,122,255,.08);color:#dfe8ff}
        .stack{display:grid;gap:12px}
        .chip-list{display:flex;flex-wrap:wrap;gap:8px}
        .chip{display:inline-flex;align-items:center;padding:5px 10px;border-radius:999px;background:rgba(255,255,255,.04);color:#dfe8ff;font-size:.84rem;font-weight:700}
        .badge{
            display:inline-flex;
            align-items:center;
            padding:7px 12px;
            border-radius:999px;
            background:rgba(255,255,255,.04);
            color:#dfe8ff;
            font-weight:700;
            font-size:.86rem;
            border:1px solid rgba(201,214,255,.12);
            white-space:nowrap;
        }
        .badge--solid{background:linear-gradient(135deg,#587aff 0%,#243a91 100%);color:#fff}
        .pill{
            display:inline-flex;
            align-items:center;
            padding:7px 12px;
            border-radius:999px;
            border:1px solid rgba(201,214,255,.12);
            background:rgba(255,255,255,.04);
            color:#dfe8ff;
            font-size:.84rem;
            font-weight:700;
            white-space:nowrap;
        }
        @media(max-width:1180px){
            body.mobile-drawer-open{overflow:hidden}
            .content{width:min(100vw - 20px,1260px)}
            .page-shell{padding-bottom:94px}
            .site-header__inner{align-items:center;padding:12px 16px;min-height:68px;width:100%}
            .site-nav{display:none}
            .site-actions{gap:8px}
            .site-actions .pill,.site-actions .site-more{display:none}
            .site-mobile__toggle{display:inline-flex}
            .site-mobile{display:block}
            .hero,.grid--2,.grid--3,.grid--4{grid-template-columns:1fr}
            .mobile-bottom-nav{
                position:fixed;
                left:10px;
                right:10px;
                bottom:max(10px,env(safe-area-inset-bottom));
                z-index:45;
                display:grid;
                grid-template-columns:repeat(4,minmax(0,1fr));
                gap:4px;
                padding:6px;
                border:1px solid var(--line);
                border-radius:18px;
                background:var(--surface-3);
                box-shadow:var(--shadow);
            }
        }
        @media(min-width:1181px){
            .site-mobile{display:none}
        }
    </style>
</head>
<body>
    <main class="page-shell">
        <header class="site-header">
            <div class="site-header__inner">
                <a href="/dashboard/" class="site-brand">
                    <div class="site-brand__mark">K</div>
                    <div>
                        <h1 class="site-brand__title">Die Kaffeeliste</h1>
                        <p class="site-brand__subtitle">Rollen</p>
                    </div>
                </a>

                <nav class="site-nav" aria-label="Tenant-Menü">
                    <?php foreach ($tenantHeaderNavItems as $item): ?>
                        <a class="site-nav__link <?= (($item['key'] ?? '') === 'roles') ? 'active' : '' ?>" href="<?= tenant_roles_h((string) ($item['href'] ?? '/')) ?>" <?= (($item['key'] ?? '') === 'roles') ? 'aria-current="page"' : '' ?>><?= tenant_roles_h((string) ($item['label'] ?? 'Link')) ?></a>
                    <?php endforeach; ?>
                    <?php foreach ($tenantNavGroups as $group): ?>
                        <details class="site-more">
                            <summary class="site-more__toggle"><?= tenant_roles_h((string) ($group['label'] ?? 'Mehr')) ?></summary>
                            <div class="site-more__panel">
                                <div class="site-more__label"><?= tenant_roles_h((string) ($group['label'] ?? 'Mehr')) ?></div>
                                <?php foreach (($group['items'] ?? []) as $item): ?>
                                    <a class="site-nav__link <?= (($item['key'] ?? '') === 'roles') ? 'active' : '' ?>" href="<?= tenant_roles_h((string) ($item['href'] ?? '/')) ?>" <?= (($item['key'] ?? '') === 'roles') ? 'aria-current="page"' : '' ?>><?= tenant_roles_h((string) ($item['label'] ?? 'Link')) ?></a>
                                <?php endforeach; ?>
                            </div>
                        </details>
                    <?php endforeach; ?>
                </nav>

                <div class="site-actions">
                    <span class="pill">Rollenmatrix</span>
                    <details class="site-more">
                        <summary class="site-more__toggle">Konto</summary>
                        <div class="site-more__panel">
                            <div class="site-more__label">Konto</div>
                            <a class="site-nav__link" href="/profile/">Persönliche Einstellungen</a>
                            <form method="post" action="/logout/"><button type="submit" class="button button--ghost" style="width:100%">Abmelden</button></form>
                        </div>
                    </details>
                    <button type="button" class="site-mobile__toggle" data-mobile-drawer-open>Menü</button>
                </div>
            </div>
        </header>

        <?php if ($mobileBottomNavItems !== []): ?>
            <div class="mobile-drawer" data-mobile-drawer>
                <button type="button" class="mobile-drawer__scrim" aria-label="Menü schließen" data-mobile-drawer-close></button>
                <aside class="mobile-drawer__panel" aria-label="Menü">
                    <div class="mobile-drawer__header">
                        <div>
                            <h2 class="mobile-drawer__title">Menü</h2>
                            <p class="mobile-drawer__subtitle"><?= tenant_roles_h((string) ($auth['tenant_name'] ?? 'Bereich')) ?></p>
                        </div>
                        <button type="button" class="mobile-drawer__close" aria-label="Schließen" data-mobile-drawer-close>&times;</button>
                    </div>
                    <?php if ($tenantHeaderNavItems !== []): ?>
                        <div class="mobile-drawer__section">
                            <div class="mobile-drawer__label">Kernnavigation</div>
                            <div class="mobile-drawer__links">
                                <?php foreach ($tenantHeaderNavItems as $item): ?>
                                    <a class="mobile-drawer__link <?= (($item['key'] ?? '') === 'roles') ? 'active' : '' ?>" href="<?= tenant_roles_h((string) ($item['href'] ?? '/')) ?>" <?= (($item['key'] ?? '') === 'roles') ? 'aria-current="page"' : '' ?>><?= tenant_roles_h((string) ($item['label'] ?? 'Link')) ?></a>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    <?php endif; ?>
                    <?php foreach ($tenantNavGroups as $group): ?>
                        <div class="mobile-drawer__section">
                            <div class="mobile-drawer__label"><?= tenant_roles_h((string) ($group['label'] ?? 'Mehr')) ?></div>
                            <div class="mobile-drawer__links">
                                <?php foreach (($group['items'] ?? []) as $item): ?>
                                    <a class="mobile-drawer__link <?= (($item['key'] ?? '') === 'roles') ? 'active' : '' ?>" href="<?= tenant_roles_h((string) ($item['href'] ?? '/')) ?>" <?= (($item['key'] ?? '') === 'roles') ? 'aria-current="page"' : '' ?>><?= tenant_roles_h((string) ($item['label'] ?? 'Link')) ?></a>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                    <div class="mobile-drawer__footer">
                        <div class="mobile-drawer__section">
                            <div class="mobile-drawer__label">Konto</div>
                            <div class="mobile-drawer__links">
                                <a class="mobile-drawer__link" href="/profile/">Persönliche Einstellungen</a>
                            </div>
                        </div>
                        <form method="post" action="/logout/"><button type="submit" class="button button--ghost" style="width:100%">Abmelden</button></form>
                    </div>
                </aside>
            </div>
        <?php endif; ?>

        <div class="content">
        <section class="hero">
            <div>
                <p class="hero__kicker">Rollen</p>
                <h2 class="hero__title">Rollen und Rechte</h2>
                <p class="hero__lead">
                    Tenant-Admins behalten den Gesamtzugriff. Fachrollen für Finanzen, Support und Umfragen werden gezielt delegiert.
                </p>
                <div class="meta" style="margin-top:16px;">
                    <span class="badge">lokal + ADFS/OIDC</span>
                    <span class="badge">Rollenmatrix</span>
                    <span class="badge">Delegation</span>
                </div>
            </div>

            <div class="stack">
                <?php foreach (array_slice($metrics, 0, 4) as $metric): ?>
                    <article class="metric">
                        <div class="metric__label"><?= tenant_roles_h((string) ($metric['label'] ?? 'Kennzahl')) ?></div>
                        <div class="metric__value"><?= tenant_roles_h((string) ($metric['value'] ?? '-')) ?></div>
                        <div class="muted"><?= tenant_roles_h((string) ($metric['detail'] ?? '')) ?></div>
                    </article>
                <?php endforeach; ?>
            </div>
        </section>

        <section class="grid grid--2">
            <article class="table-card">
                <div class="table-card__header">
                    <div>
                        <p class="hero__kicker">Rollenmatrix</p>
                        <h2>Vollzugriff, Spezialrollen und Leserechte</h2>
                    </div>
                    <span class="pill">Tenant-weit</span>
                </div>
                <table>
                    <thead>
                        <tr>
                            <th>Rolle</th>
                            <th>Rechte</th>
                            <th>Delegation</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($roles as $role): ?>
                            <tr>
                                <td>
                                    <strong><?= tenant_roles_h((string) ($role['label'] ?? $role['key'] ?? 'Rolle')) ?></strong><br>
                                    <span class="muted"><?= tenant_roles_h((string) ($role['scope'] ?? '')) ?></span>
                                </td>
                                <td>
                                    <div class="chip-list">
                                        <?php foreach (($role['rights'] ?? []) as $right): ?>
                                            <span class="chip"><?= tenant_roles_h((string) $right) ?></span>
                                        <?php endforeach; ?>
                                    </div>
                                </td>
                                <td>
                                    <?= tenant_roles_h((string) ($role['delegation'] ?? '')) ?><br>
                                    <span class="muted"><?= tenant_roles_h((string) ($role['notes'] ?? '')) ?></span>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </article>

            <article class="card">
                <p class="hero__kicker">Delegation</p>
                <h2>Wer Rollen vergeben darf</h2>
                <div class="stack">
                    <?php foreach ($delegationRules as $rule): ?>
                        <div class="note">
                            <strong><?= tenant_roles_h((string) ($rule['from'] ?? '')) ?> → <?= tenant_roles_h((string) ($rule['to'] ?? '')) ?></strong>
                            <div><?= tenant_roles_h((string) ($rule['rule'] ?? '')) ?></div>
                            <div class="muted" style="margin-top:4px;">
                                Freigabe: <?= tenant_roles_h((string) ($rule['approval'] ?? '')) ?>
                            </div>
                            <div class="muted"><?= tenant_roles_h((string) ($rule['note'] ?? '')) ?></div>
                        </div>
                    <?php endforeach; ?>
                </div>
                <div class="note" style="margin-top:16px;">
                    Das Vier-Augen-Prinzip kann tenantweit aktiviert oder deaktiviert werden. Kritische Aktionen werden
                    dadurch kontrolliert, ohne die Facharbeit in jedem Tenant gleich stark zu bremsen.
                </div>
            </article>
        </section>

        <section class="grid grid--2" style="margin-top:18px;">
            <article class="table-card">
                <div class="table-card__header">
                    <div>
                        <p class="hero__kicker">Tenant-Snapshots</p>
                        <h2>Wie die Rollen in den Tenants aussehen</h2>
                    </div>
                    <span class="pill"><?= tenant_roles_h((string) ($tenant['tenant_key'] ?? 'berlin')) ?></span>
                </div>
                <table>
                    <thead>
                        <tr>
                            <th>Mandant</th>
                            <th>Admins</th>
                            <th>Spezialrollen</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($tenantSnapshots as $snapshot): ?>
                            <tr>
                                <td>
                                    <strong><?= tenant_roles_h((string) ($snapshot['name'] ?? 'Tenant')) ?></strong><br>
                                    <span class="muted"><?= tenant_roles_h((string) ($snapshot['tenant_key'] ?? '')) ?> · <?= tenant_roles_h((string) ($snapshot['primary_contact'] ?? '')) ?></span>
                                </td>
                                <td><?= (int) ($snapshot['admin_count'] ?? 0) ?></td>
                                <td><?= tenant_roles_implode((array) ($snapshot['special_roles'] ?? [])) ?></td>
                                <td>
                                    <?php if (($snapshot['status'] ?? 'active') === 'active'): ?>
                                        <span class="status status--success"><?= tenant_roles_h((string) ($snapshot['delegation_state'] ?? 'Aktiv')) ?></span>
                                    <?php else: ?>
                                        <span class="status status--warning"><?= tenant_roles_h((string) ($snapshot['delegation_state'] ?? 'Prüfen')) ?></span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </article>

            <article class="card">
                <p class="hero__kicker">Lesbare Sicht</p>
                <h2>Was Tenant-Admins direkt verstehen sollen</h2>
                <div class="stack">
                    <?php foreach ($permissionGroups as $group): ?>
                        <div class="note">
                            <strong><?= tenant_roles_h((string) ($group['title'] ?? 'Rechtegruppe')) ?></strong>
                            <div class="chip-list" style="margin-top:8px;">
                                <?php foreach (($group['items'] ?? []) as $item): ?>
                                    <span class="chip"><?= tenant_roles_h((string) $item) ?></span>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
                <ul class="list-reset" style="margin-top:16px;">
                    <?php foreach ($notes as $note): ?>
                        <li><?= tenant_roles_h((string) $note) ?></li>
                    <?php endforeach; ?>
                </ul>
            </article>
        </section>
        </div>
    </main>
    <?php if ($mobileBottomNavItems !== []): ?>
        <nav class="mobile-bottom-nav" aria-label="Mobile Kernnavigation">
            <?php foreach ($mobileBottomNavItems as $item): ?>
                <a class="mobile-bottom-nav__link <?= (($item['key'] ?? '') === 'roles') ? 'active' : '' ?>" href="<?= tenant_roles_h((string) ($item['href'] ?? '/')) ?>" <?= (($item['key'] ?? '') === 'roles') ? 'aria-current="page"' : '' ?>>
                    <span class="mobile-bottom-nav__icon"><?= match ((string) ($item['key'] ?? '')) {
                        'dashboard' => 'U',
                        'ledger' => 'B',
                        'payments' => 'Z',
                        default => '•',
                    } ?></span>
                    <span class="mobile-bottom-nav__label"><?= tenant_roles_h((string) ($item['label'] ?? 'Link')) ?></span>
                </a>
            <?php endforeach; ?>
            <button type="button" class="mobile-bottom-nav__toggle" data-mobile-drawer-open>
                <span class="mobile-bottom-nav__icon">+</span>
                <span class="mobile-bottom-nav__label">Menü</span>
            </button>
        </nav>
    <?php endif; ?>
    <script>
    document.addEventListener('DOMContentLoaded', function () {
        const details = document.querySelectorAll('.site-more');
        const drawer = document.querySelector('[data-mobile-drawer]');
        const drawerOpeners = document.querySelectorAll('[data-mobile-drawer-open]');
        const drawerClosers = document.querySelectorAll('[data-mobile-drawer-close]');

        function closeOthers(current) {
            details.forEach(function (other) {
                if (other !== current) {
                    other.open = false;
                }
            });
        }

        details.forEach(function (detail) {
            const summary = detail.querySelector('summary');
            if (summary) {
                summary.addEventListener('click', function () {
                    if (!detail.open) {
                        closeOthers(detail);
                    }
                });
            }

            detail.addEventListener('toggle', function () {
                if (detail.open) {
                    closeOthers(detail);
                }
            });
        });

        document.querySelectorAll('.site-more__panel a, .mobile-drawer__link').forEach(function (link) {
            link.addEventListener('click', function () {
                details.forEach(function (detail) {
                    detail.open = false;
                });
                if (drawer) {
                    drawer.classList.remove('is-open');
                    document.body.classList.remove('mobile-drawer-open');
                }
            });
        });

        if (drawer) {
            drawerOpeners.forEach(function (button) {
                button.addEventListener('click', function () {
                    drawer.classList.add('is-open');
                    document.body.classList.add('mobile-drawer-open');
                });
            });

            drawerClosers.forEach(function (button) {
                button.addEventListener('click', function () {
                    drawer.classList.remove('is-open');
                    document.body.classList.remove('mobile-drawer-open');
                });
            });
        }
    });
    </script>
</body>
</html>
