<?php
declare(strict_types=1);

/**
 * layout/topbar.php (INCLUDE-FRIENDLY)
 * - Tek başına sayfa DEĞİL, component.
 * - Tek sefer assets basar.
 * - BASE URL: DocumentRoot + bu dosyanın konumundan hesaplanır (subfolder destekli)
 */

if (session_status() === PHP_SESSION_NONE) { session_start(); }

/**
 * BASE (uygulama sub-folder ise düzgün bul)
 * Örn:
 *  - /dashboard.php          => BASE = ''
 *  - /diyaliz/dashboard.php  => BASE = '/diyaliz'
 */
$docRootFs = realpath($_SERVER['DOCUMENT_ROOT'] ?? '');
$appRootFs = realpath(__DIR__ . '/..'); // layout/.. = app root
$BASE = '';

if ($docRootFs && $appRootFs) {
  $docRootFs = rtrim(str_replace('\\', '/', $docRootFs), '/');
  $appRootFs = rtrim(str_replace('\\', '/', $appRootFs), '/');

  if (str_starts_with($appRootFs, $docRootFs)) {
    $rel = substr($appRootFs, strlen($docRootFs));
    $rel = str_replace('\\', '/', $rel);
    $rel = '/' . ltrim($rel, '/');
    $BASE = rtrim($rel, '/');
    if ($BASE === '/') $BASE = '';
  }
}

/** Aktif URL path (query hariç) */
$currentPath = (string)parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH);
$currentPath = '/' . ltrim($currentPath, '/');

/** currentPath'i BASE'e göre normalize et (eşleşme için) */
if ($BASE !== '' && str_starts_with($currentPath, $BASE . '/')) {
  $currentPathNorm = substr($currentPath, strlen($BASE));
  $currentPathNorm = '/' . ltrim($currentPathNorm, '/');
} else {
  $currentPathNorm = $currentPath;
}

if (!function_exists('isActiveUrl')) {
  function isActiveUrl(string $href, string $currentPathNorm): string {
    $hrefPath = (string)parse_url($href, PHP_URL_PATH);
    $hrefPath = '/' . ltrim($hrefPath, '/');
    return ($hrefPath !== '/' && $hrefPath === $currentPathNorm) ? 'is-active' : '';
  }
}

$lang = $_SESSION['lang'] ?? ($_COOKIE['lang'] ?? 'tr');
$lang = in_array($lang, ['tr','en'], true) ? $lang : 'tr';

$TXT = [
  'tr' => [
    'brand' => 'NephroLink',
    'dashboard' => 'Genel Bakış',
    'search' => 'Menüde ara...',
    'no_result' => 'Eşleşme yok',

    'grp_patient' => 'Hasta İşlemleri',
    'hasta_ekle' => 'Yeni Hasta Kaydı',
    'hasta_liste' => 'Hasta Listesi',
    'hasta_onam' => 'Onam & Arşiv',
    'dislab_gonder' => 'Dışlab Hasta Gönderimi',
    'dislab_izle' => 'Dışlab Hasta İzle',

    'grp_seans' => 'Seans & Randevu',
    'seans_baslat' => 'Seans Başlat',
    'aktif_seans' => 'Aktif Seans Takibi',
    'seans_takvimi' => 'Haftalık Planlama',
    'bugunku_seanslar' => 'Bugünkü Akış',
    'yatak_doluluk' => 'Cihaz/Yatak Durumu',

    'grp_klinik' => 'Klinik & Tıbbi',
    'vizit_notlari' => 'Doktor Vizit Notları',
    'hemsire_notlari' => 'Hemşire Gözlem',
    'lab_sonuclari' => 'Laboratuvar Sonuçları',

    'grp_loji' => 'Lojistik & Depo',
    'stok_takip' => 'Sarf Malzeme Stok',
    'stok_liste' => 'Malzeme Listesi',
    'stok_hareket' => 'Depo Hareketleri',

    'grp_other' => 'Diğer Modüller',
    'test_tanimlari' => 'Test Tanımları',
    'doktor_tanimlari' => 'Doktor Tanımları',
    'kurum_tanimlari' => 'Kurum Tanımları',
    'dislab_sync' => 'Dışlab Test Senk.',
    'xml_import' => 'XML İçe Aktar',
    'tanimlar' => 'Sistem Tanımları',
    'dislab_tanimlari' => 'Dış Lab Tanımları',
    'cihazlar' => 'Cihaz Tanımları',
    'cihaz_bakimlari' => 'Cihaz Bakımları',
    'log_listesi' => 'Sistem Logları',
    'ayarlar' => 'Sistem Ayarları',

    'logout' => 'Güvenli Çıkış',
    'menu' => 'Menü',
  ],
  'en' => [
    'brand' => 'NephroLink',
    'dashboard' => 'Overview',
    'search' => 'Search...',
    'no_result' => 'No matches',

    'grp_patient' => 'Patient Operations',
    'hasta_ekle' => 'New Patient',
    'hasta_liste' => 'Patient List',
    'hasta_onam' => 'Consents & Archive',
    'dislab_gonder' => 'External Lab Transfer',
    'dislab_izle' => 'External Lab Tracking',

    'grp_seans' => 'Sessions & Appointments',
    'seans_baslat' => 'Start Session',
    'aktif_seans' => 'Active Session Tracking',
    'seans_takvimi' => 'Weekly Planning',
    'bugunku_seanslar' => 'Todays Flow',
    'yatak_doluluk' => 'Device/Bed Status',

    'grp_klinik' => 'Clinical',
    'vizit_notlari' => 'Doctor Rounds Notes',
    'hemsire_notlari' => 'Nursing Observation',
    'lab_sonuclari' => 'Lab Results',

    'grp_loji' => 'Logistics & Inventory',
    'stok_takip' => 'Consumables Stock',
    'stok_liste' => 'Materials List',
    'stok_hareket' => 'Warehouse Movements',

    'grp_other' => 'Other Modules',
    'test_tanimlari' => 'Test Definitions',
    'doktor_tanimlari' => 'Doctor Definitions',
    'kurum_tanimlari' => 'Institution Definitions',
    'dislab_sync' => 'External Lab Test Sync',
    'xml_import' => 'Import XML',
    'tanimlar' => 'System Definitions',
    'dislab_tanimlari' => 'External Lab Definitions',
    'cihazlar' => 'Device Definitions',
    'cihaz_bakimlari' => 'Device Maintenance',
    'log_listesi' => 'System Logs',
    'ayarlar' => 'System Settings',

    'logout' => 'Sign Out',
    'menu' => 'Menu',
  ]
];

function __t(array $TXT, string $lang, string $key): string {
  return htmlspecialchars($TXT[$lang][$key] ?? $key, ENT_QUOTES, 'UTF-8');
}

$full = $_SESSION['full_name'] ?? 'User';
$role = $_SESSION['role'] ?? 'staff';

function initials2(string $name): string {
  $parts = preg_split('/\s+/', trim($name));
  $a = strtoupper(mb_substr($parts[0] ?? 'N', 0, 1));
  $b = strtoupper(mb_substr($parts[count($parts)-1] ?? 'L', 0, 1));
  return $a . $b;
}

/** Direkt path menü */
$dashboardHref = $BASE . '/dashboard.php';
$logoutHref    = $BASE . '/logout.php';

$menu = [
  ['group' => 'grp_patient', 'icon' => 'bi bi-person-heart', 'items' => [
    ['href'=>$BASE.'/HastaModulu/hastaEkle.php',      'i'=>'bi bi-person-plus',        'k'=>'hasta_ekle'],
    ['href'=>$BASE.'/HastaModulu/hastaListe.php',     'i'=>'bi bi-people',             'k'=>'hasta_liste'],
    ['href'=>$BASE.'/HastaModulu/onamArsiv.php',      'i'=>'bi bi-file-earmark-lock',  'k'=>'hasta_onam'],
    ['href'=>$BASE.'/HastaModulu/dislabGonder.php',   'i'=>'bi bi-box-arrow-up',       'k'=>'dislab_gonder'],
    ['href'=>$BASE.'/HastaModulu/dislabIzle.php',     'i'=>'bi bi-search-heart',       'k'=>'dislab_izle'],
  ]],
  ['group' => 'grp_seans', 'icon' => 'bi bi-calendar-check', 'items' => [
    ['href'=>$BASE.'/SeansRandevuModulu/seans-baslat.php',      'i'=>'bi bi-play-circle',   'k'=>'seans_baslat'],
    ['href'=>$BASE.'/SeansRandevuModulu/aktif-seans-takip.php', 'i'=>'bi bi-stopwatch',         'k'=>'aktif_seans'],
    ['href'=>$BASE.'/SeansRandevuModulu/seans-takvimi.php',     'i'=>'bi bi-calendar3',     'k'=>'seans_takvimi'],
    ['href'=>$BASE.'/SeansRandevuModulu/bugunku-seanslar.php',  'i'=>'bi bi-clock-history', 'k'=>'bugunku_seanslar'],
    ['href'=>$BASE.'/SeansRandevuModulu/yatak-doluluk.php',     'i'=>'bi bi-hospital',      'k'=>'yatak_doluluk'],
  ]],
  ['group' => 'grp_klinik', 'icon' => 'bi bi-heart-pulse', 'items' => [
    ['href'=>$BASE.'/HastaModulu/vizit-notlari.php',  'i'=>'bi bi-person-badge',     'k'=>'vizit_notlari'],
    ['href'=>$BASE.'/HastaModulu/hemsire-gozlem.php', 'i'=>'bi bi-clipboard2-pulse', 'k'=>'hemsire_notlari'],
    ['href'=>$BASE.'/HastaModulu/labSonuclari.php',  'i'=>'bi bi-graph-up',         'k'=>'lab_sonuclari'],
  ]],
  ['group' => 'grp_loji', 'icon' => 'bi bi-box-seam', 'items' => [
    ['href'=>$BASE.'/LojistikDepoModulu/stok-takip.php',   'i'=>'bi bi-box-seam',         'k'=>'stok_takip'],
    ['href'=>$BASE.'/LojistikDepoModulu/stok-liste.php',   'i'=>'bi bi-boxes',            'k'=>'stok_liste'],
    ['href'=>$BASE.'/LojistikDepoModulu/stok-hareket.php', 'i'=>'bi bi-arrow-left-right', 'k'=>'stok_hareket'],
  ]],
  ['group' => 'grp_other', 'icon' => 'bi bi-grid-3x3-gap', 'items' => [
    ['href'=>$BASE.'/SistemModulu/test-tanimlari.php',    'i'=>'bi bi-list-check',      'k'=>'test_tanimlari'],
    ['href'=>$BASE.'/SistemModulu/doktor-tanimlari.php',  'i'=>'bi bi-person-badge',    'k'=>'doktor_tanimlari'],
    ['href'=>$BASE.'/SistemModulu/kurum-tanimlari.php',   'i'=>'bi bi-building',        'k'=>'kurum_tanimlari'],
    ['href'=>$BASE.'/SistemModulu/dislab-testler.php',    'i'=>'bi bi-cloud-arrow-up',  'k'=>'dislab_sync'],
    ['href'=>$BASE.'/SistemModulu/xml-import.php',        'i'=>'bi bi-file-earmark-arrow-up',  'k'=>'xml_import'],
    ['href'=>$BASE.'/SistemModulu/tanimlar.php',          'i'=>'bi bi-sliders2',        'k'=>'tanimlar'],
    ['href'=>$BASE.'/SistemModulu/dislab-tanimlari.php',  'i'=>'bi bi-building-gear',   'k'=>'dislab_tanimlari'],
    ['href'=>$BASE.'/SistemModulu/cihazlar.php',          'i'=>'bi bi-cpu',             'k'=>'cihazlar'],
    ['href'=>$BASE.'/SistemModulu/cihaz-bakimlari.php',   'i'=>'bi bi-tools',           'k'=>'cihaz_bakimlari'],
    ['href'=>$BASE.'/SistemModulu/loglar.php',       'i'=>'bi bi-journal-text',    'k'=>'log_listesi'],
    ['href'=>$BASE.'/SistemModulu/ayarlar.php',           'i'=>'bi bi-gear',            'k'=>'ayarlar'],
  ]],
];

/** Assets sadece 1 kere basılsın */
if (!defined('NEPHROLINK_TOPBAR_ASSETS')) {
  define('NEPHROLINK_TOPBAR_ASSETS', true);
  ?>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
  <style>
    :root{
      --bg:#f8fafc; --card:#ffffff; --ink:#0f172a; --muted:#64748b;
      --border:#e2e8f0; --accent:#2563eb; --accent-soft:#eff6ff; --h:70px;
    }
    *{ box-sizing:border-box; -webkit-font-smoothing:antialiased; }
    body{ margin:0; font-family:"Segoe UI", system-ui, sans-serif; background:var(--bg); color:var(--ink); }

    .topbar{ position:sticky; top:0; z-index:1000; height:var(--h);
      background:var(--card); border-bottom:1px solid var(--border);
      box-shadow:0 1px 3px rgba(0,0,0,0.05);
    }
    .topbar-inner{ height:100%; width:100%; padding:0 24px; display:flex; align-items:center; gap:10px; min-width:0; }

    .brand{ display:flex; align-items:center; gap:10px; text-decoration:none; color:var(--ink);
      font-weight:700; font-size:1.3rem; flex-shrink:0; margin-right:20px; white-space:nowrap;
    }
    .brand .logo{ width:38px; height:38px; border-radius:10px; background:var(--accent); color:#fff;
      display:grid; place-items:center; font-size:20px;
    }

    /* IMPORTANT: Bootstrap çakışmasın diye class isimlerini izole ettik */
    .tb-nav{ display:flex; align-items:center; gap:4px; flex-wrap:wrap; min-width:0; }
    .tb-spacer{ flex:1; }
    .tb-item{ position:relative; }

    .tb-btn{ display:flex; align-items:center; gap:8px; padding:10px 14px; border-radius:8px;
      background:transparent; border:none; color:var(--muted); font-weight:600; font-size:.95rem;
      cursor:pointer; transition:.2s; white-space:nowrap;
    }
    .tb-btn:hover, .tb-item.open .tb-btn{ background:var(--accent-soft); color:var(--accent); }
    .tb-btn.is-active{ background:var(--accent-soft); color:var(--accent); }

    .tb-panel{ position:absolute; top: calc(100% + 6px); left:0; min-width:320px; max-width:420px;
      background:#fff; border:1px solid var(--border); border-radius:12px;
      box-shadow:0 10px 25px -5px rgba(0,0,0,0.1);
      padding:10px; display:none; z-index:1100;
    }
    .tb-item.open .tb-panel{ display:block; animation:tbFade .18s ease; }
    @keyframes tbFade{ from{opacity:0; transform:translateY(8px);} to{opacity:1; transform:translateY(0);} }

    .tb-search{ display:flex; align-items:center; gap:10px; padding:10px 10px; border:1px solid var(--border);
      border-radius:10px; background:#f1f5f9; margin-bottom:8px;
    }
    .tb-search i{ color:#94a3b8; }
    .tb-search input{ width:100%; border:0; outline:0; background:transparent; font:inherit; font-weight:700; color:var(--ink); }

    .tb-empty{ display:none; padding:10px 12px; border-radius:10px; background:#f8fafc; color:#94a3b8; font-weight:700; font-size:.9rem; }

    .tb-link{ display:flex; align-items:center; gap:10px; padding:10px 12px; border-radius:8px;
      text-decoration:none; color:var(--ink); font-size:.9rem; font-weight:600; transition:.2s;
    }
    .tb-link i{ font-size:1.1rem; color:var(--muted); width:20px; text-align:center; }
    .tb-link:hover{ background:var(--accent-soft); color:var(--accent); }
    .tb-link.is-active{ background:var(--accent-soft); color:var(--accent); font-weight:800; }

    .right-group{ display:flex; align-items:center; gap:12px; flex-shrink:0; }
    .user-area{ display:flex; align-items:center; gap:12px; padding-left:15px; border-left:1px solid var(--border); }
    .ava{ width:36px; height:36px; border-radius:10px; background:#e2e8f0; display:grid; place-items:center;
      font-weight:800; font-size:.8rem; color:#475569;
    }
    .uinfo{ display:flex; flex-direction:column; line-height:1.2; }
    .uinfo b{ font-size:.85rem; white-space:nowrap; }
    .uinfo span{ font-size:.75rem; color:var(--muted); text-transform:lowercase; }

    .logout-btn{ color:#ef4444; font-size:1.2rem; text-decoration:none; padding:6px; border-radius:10px;
      display:grid; place-items:center; transition:.2s;
    }
    .logout-btn:hover{ background:#fff1f2; transform:scale(1.05); }

    .mobile-toggle{ display:none; font-size:24px; cursor:pointer; color:var(--muted); margin-right:10px; }

    @media (max-width:1024px){
      .tb-nav, .tb-spacer, .user-area{ display:none; }
      .mobile-toggle{ display:block; }
      .brand{ margin-right:0; font-size:1.15rem; }
    }

    .m-overlay{ position:fixed; inset:0; background: rgba(15,23,42,.38); backdrop-filter: blur(2px);
      display:none; z-index:2000;
    }
    body.m-open .m-overlay{ display:block; }

    .m-drawer{ position:fixed; top:0; left:-380px; width:360px; max-width:92vw; height:100vh; background:#fff;
      border-right:1px solid var(--border); box-shadow: 18px 0 45px rgba(15,23,42,.18);
      z-index:2100; transition:left .25s ease; display:flex; flex-direction:column;
    }
    body.m-open .m-drawer{ left:0; }

    .m-head{ height:var(--h); padding:0 16px; display:flex; align-items:center; justify-content:space-between;
      border-bottom:1px solid var(--border);
    }
    .m-title{ font-weight:900; color:var(--ink); display:flex; align-items:center; gap:10px; }
    .m-close{ width:40px; height:40px; border-radius:12px; border:1px solid var(--border); background:#fff;
      cursor:pointer; display:grid; place-items:center; color:var(--muted);
    }
    .m-close:hover{ background:#f8fafc; color:var(--accent); }

    .m-body{ padding:14px; overflow:auto; flex:1; }
    .m-search{ display:flex; align-items:center; gap:10px; padding:12px 12px; border:1px solid var(--border);
      border-radius:14px; background:#f1f5f9; margin-bottom:12px;
    }
    .m-search i{ color:#94a3b8; }
    .m-search input{ width:100%; border:0; outline:0; background:transparent; font:inherit; font-weight:800; }

    .m-group{ border:1px solid #f1f5f9; border-radius:16px; padding:10px; margin-bottom:12px; }
    .m-gt{ font-size:.72rem; letter-spacing:1.35px; text-transform:uppercase; color:#334155; font-weight:900; padding:6px 6px 10px; }

    .m-link{ display:flex; align-items:center; gap:12px; padding:10px 12px; border-radius:14px;
      text-decoration:none; color:var(--muted); font-weight:800; transition:.16s; margin-bottom:6px;
    }
    .m-link i{ width:22px; text-align:center; color:#94a3b8; font-size:1.05rem; }
    .m-link:hover{ background:#f8fafc; color:var(--accent); }
    .m-link:hover i{ color:var(--accent); }
    .m-link.is-active{ background:var(--accent-soft); color:var(--accent); }

    .m-foot{ border-top:1px solid var(--border); padding:12px 14px; display:flex; align-items:center;
      justify-content:space-between; gap:12px; background:#fff;
    }
    .m-user{ display:flex; align-items:center; gap:10px; min-width:0; }
    .m-uinfo{ min-width:0; line-height:1.1; }
    .m-uinfo b{ display:block; font-size:.85rem; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; max-width:140px; }
    .m-uinfo span{ display:block; font-size:.75rem; color:var(--muted); text-transform:lowercase; }

    .m-logout{ display:flex; align-items:center; gap:8px; padding:10px 12px; border-radius:12px;
      border:1px solid #ffe4e6; background:#fff; text-decoration:none; color:#ef4444; font-weight:900; white-space:nowrap;
    }
    .m-logout:hover{ background:#fff1f2; }
  </style>
  <?php
}

/** Script sadece 1 kere basılsın */
if (!defined('NEPHROLINK_TOPBAR_SCRIPT')) {
  define('NEPHROLINK_TOPBAR_SCRIPT', true);
  ?>
  <script>
  document.addEventListener('DOMContentLoaded', () => {
    const dropdowns = document.querySelectorAll('.tb-dd');

    function closeAll(except=null){
      dropdowns.forEach(dd=>{
        if (dd !== except) dd.classList.remove('open');
        const btn = dd.querySelector('.tb-btn');
        if (btn) btn.setAttribute('aria-expanded', dd.classList.contains('open') ? 'true':'false');

        const inp = dd.querySelector('.tb-search-input');
        const empty = dd.querySelector('.tb-empty');
        const links = dd.querySelectorAll('.tb-link');
        if (inp) inp.value = '';
        links.forEach(a=> a.style.display = 'flex');
        if (empty) empty.style.display = 'none';
      });
    }

    dropdowns.forEach(dd=>{
      const btn = dd.querySelector('.tb-btn');
      const panel = dd.querySelector('.tb-panel');
      const input = dd.querySelector('.tb-search-input');
      const empty = dd.querySelector('.tb-empty');
      const links = dd.querySelectorAll('.tb-link');

      btn?.addEventListener('click', (e)=>{
        e.stopPropagation();
        const willOpen = !dd.classList.contains('open');
        closeAll(dd);
        dd.classList.toggle('open', willOpen);
        btn.setAttribute('aria-expanded', willOpen ? 'true' : 'false');
        if (willOpen && input) setTimeout(()=> input.focus(), 0);
      });

      panel?.addEventListener('click', (e)=> e.stopPropagation());

      input?.addEventListener('input', function(){
        const q = (this.value || '').toLowerCase().trim();
        let any = false;
        links.forEach(a=>{
          const t = (a.textContent || '').toLowerCase();
          const ok = !q || t.includes(q);
          a.style.display = ok ? 'flex' : 'none';
          if (ok) any = true;
        });
        if (empty) empty.style.display = any ? 'none' : 'block';
      });
    });

    document.addEventListener('click', ()=> closeAll(null));
    document.addEventListener('keydown', (e)=>{ if(e.key === 'Escape') closeAll(null); });

    // Mobil drawer
    const body = document.body;
    const mOpen = document.getElementById('mOpen');
    const mClose = document.getElementById('mClose');
    const mOverlay = document.getElementById('mOverlay');

    function openMobile(){
      body.classList.add('m-open');
      document.getElementById('mDrawer')?.setAttribute('aria-hidden','false');
      const inp = document.getElementById('mSearch');
      if (inp){
        inp.value = '';
        document.querySelectorAll('.m-link').forEach(a=> a.style.display='flex');
        document.querySelectorAll('[data-mgroup]').forEach(g=> g.style.display='');
        setTimeout(()=> inp.focus(), 0);
      }
    }
    function closeMobile(){
      body.classList.remove('m-open');
      document.getElementById('mDrawer')?.setAttribute('aria-hidden','true');
    }

    mOpen?.addEventListener('click', openMobile);
    mClose?.addEventListener('click', closeMobile);
    mOverlay?.addEventListener('click', closeMobile);
    document.addEventListener('keydown', (e)=>{ if(e.key==='Escape') closeMobile(); });

    const mSearch = document.getElementById('mSearch');
    mSearch?.addEventListener('input', function(){
      const q = (this.value || '').toLowerCase().trim();
      const groups = document.querySelectorAll('[data-mgroup]');
      groups.forEach(g=>{
        const links = Array.from(g.querySelectorAll('.m-link'));
        let any = false;
        links.forEach(a=>{
          const t = (a.getAttribute('data-text') || a.textContent || '').toLowerCase();
          const ok = !q || t.includes(q);
          a.style.display = ok ? 'flex' : 'none';
          if (ok) any = true;
        });
        g.style.display = any ? '' : 'none';
      });
    });
  });
  </script>
  <?php
}

?>

<!-- Mobil overlay + drawer -->
<div class="m-overlay" id="mOverlay"></div>

<aside class="m-drawer" id="mDrawer" aria-hidden="true">
  <div class="m-head">
    <div class="m-title"><i class="bi bi-list" style="color:var(--accent)"></i> <?= __t($TXT,$lang,'menu') ?></div>
    <button class="m-close" id="mClose" type="button" aria-label="Close menu"><i class="bi bi-x-lg"></i></button>
  </div>

  <div class="m-body">
    <div class="m-search">
      <i class="bi bi-search"></i>
      <input id="mSearch" type="search" placeholder="<?= __t($TXT,$lang,'search') ?>" autocomplete="off">
    </div>

    <div class="m-group" data-mgroup>
      <div class="m-gt"><?= __t($TXT,$lang,'dashboard') ?></div>
      <a class="m-link <?= isActiveUrl($dashboardHref, $currentPathNorm) ?>"
         href="<?= htmlspecialchars($dashboardHref) ?>"
         data-text="<?= htmlspecialchars(mb_strtolower(__t($TXT,$lang,'dashboard'))) ?>">
        <i class="bi bi-grid-1x2"></i> <?= __t($TXT,$lang,'dashboard') ?>
      </a>
    </div>

    <?php foreach ($menu as $block): ?>
      <div class="m-group" data-mgroup>
        <div class="m-gt"><?= __t($TXT,$lang,$block['group']) ?></div>
        <?php foreach ($block['items'] as $it): ?>
          <a href="<?= htmlspecialchars($it['href']) ?>"
             class="m-link <?= isActiveUrl($it['href'], $currentPathNorm) ?>"
             data-text="<?= htmlspecialchars(mb_strtolower(__t($TXT,$lang,$it['k']))) ?>">
            <i class="<?= htmlspecialchars($it['i']) ?>"></i> <?= __t($TXT,$lang,$it['k']) ?>
          </a>
        <?php endforeach; ?>
      </div>
    <?php endforeach; ?>
  </div>

  <div class="m-foot">
    <div class="m-user">
      <div class="ava"><?= initials2($full) ?></div>
      <div class="m-uinfo">
        <b><?= htmlspecialchars($full) ?></b>
        <span><?= htmlspecialchars($role) ?></span>
      </div>
    </div>
    <a href="<?= htmlspecialchars($logoutHref) ?>" class="m-logout">
      <i class="bi bi-power"></i> <?= __t($TXT,$lang,'logout') ?>
    </a>
  </div>
</aside>

<header class="topbar">
  <div class="topbar-inner">

    <!-- Hamburger solda -->
    <i class="bi bi-list mobile-toggle" id="mOpen" aria-label="Open menu"></i>

    <a href="<?= htmlspecialchars($dashboardHref) ?>" class="brand">
      <div class="logo"><i class="bi bi-droplet-fill"></i></div>
      <span><?= __t($TXT,$lang,'brand') ?></span>
    </a>

    <nav class="tb-nav">
      <div class="tb-item">
        <a href="<?= htmlspecialchars($dashboardHref) ?>" class="tb-btn <?= isActiveUrl($dashboardHref, $currentPathNorm) ?>">
          <i class="bi bi-grid-1x2"></i> <?= __t($TXT,$lang,'dashboard') ?>
        </a>
      </div>

      <?php foreach ($menu as $block): ?>
        <div class="tb-item tb-dd">
          <button class="tb-btn" type="button" aria-expanded="false">
            <i class="<?= htmlspecialchars($block['icon']) ?>"></i>
            <?= __t($TXT,$lang,$block['group']) ?>
            <i class="bi bi-chevron-down" style="font-size:.7rem; margin-left:5px;"></i>
          </button>

          <div class="tb-panel">
            <div class="tb-search">
              <i class="bi bi-search"></i>
              <input class="tb-search-input" type="text" placeholder="<?= __t($TXT,$lang,'search') ?>" autocomplete="off">
            </div>

            <div class="tb-empty"><?= __t($TXT,$lang,'no_result') ?></div>

            <?php foreach ($block['items'] as $it): ?>
              <a href="<?= htmlspecialchars($it['href']) ?>" class="tb-link <?= isActiveUrl($it['href'], $currentPathNorm) ?>">
                <i class="<?= htmlspecialchars($it['i']) ?>"></i> <?= __t($TXT,$lang,$it['k']) ?>
              </a>
            <?php endforeach; ?>
          </div>
        </div>
      <?php endforeach; ?>
    </nav>

    <div class="tb-spacer"></div>

    <div class="right-group">
      <div class="user-area">
        <div class="ava"><?= initials2($full) ?></div>
        <div class="uinfo">
          <b><?= htmlspecialchars($full) ?></b>
          <span><?= htmlspecialchars($role) ?></span>
        </div>
        <a href="<?= htmlspecialchars($logoutHref) ?>" class="logout-btn" title="<?= __t($TXT,$lang,'logout') ?>">
          <i class="bi bi-power"></i>
        </a>
      </div>
    </div>
  </div>
</header>