@php
    $businessName = $appSettings['business_name'] ?? 'SolarFlow';
    $logo = $appSettings['logo'] ?? null;
    $configActive = request()->routeIs('settings.*', 'translations.*', 'categories.*', 'units.*', 'currencies.*');
    $transactionActive = request()->routeIs('invoices.*', 'purchases.*', 'products.*', 'payments.*', 'returns.*', 'expenses.*', 'wizards.*');
    $partyActive = request()->routeIs('customers.*', 'suppliers.*', 'reports.customer-*', 'reports.supplier-*');
    $financeActive = request()->routeIs('finance.*', 'assets.*', 'vouchers.*', 'reports.chart-of-accounts');
    $reportsActive = request()->routeIs('reports.*');
    $systemActive = request()->routeIs('backup.*', 'desktop.*');
@endphp
<!doctype html>
<html lang="{{ app()->getLocale() }}" dir="{{ in_array(app()->getLocale(), ['fa', 'ps']) ? 'rtl' : 'ltr' }}">
<head>
    <meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{{ $businessName }}</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/8.2.0/mdb.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <style>
        :root{--success:#22a06b;--ink:#18352d;--mist:#f4faf7;--line:#e7eee9}.app-bg{background:var(--mist);min-height:100vh;color:var(--ink)}.sidebar{width:280px;background:#fff;min-height:100vh;border-inline-end:1px solid var(--line);position:fixed;inset-block-start:0;inset-inline-start:0;z-index:10;overflow-y:auto}.brand{color:var(--success);font-weight:800;font-size:1.08rem;line-height:1.2}.nav-section{font-size:.72rem;font-weight:800;text-transform:uppercase;letter-spacing:.04em;color:#8aa096;margin:18px 24px 6px}.nav-link,.nav-summary,.sub-link{color:#547067;border-radius:8px;margin:3px 12px;padding:10px 12px;transition:.18s ease;text-decoration:none;display:block}.nav-summary{list-style:none;cursor:pointer}.nav-summary::-webkit-details-marker{display:none}.nav-summary:after{content:"\f105";font-family:"Font Awesome 6 Free";font-weight:900;float:inline-end}.nav-group[open] .nav-summary:after{transform:rotate(90deg)}.nav-link:hover,.nav-link.active,.nav-summary:hover,.nav-group[open] .nav-summary{background:#e4f6ed;color:#16754d;transform:translateX(3px)}.sub-list{padding:2px 0 6px;margin:0 0 0 18px;border-inline-start:2px solid #e4f6ed}.sub-link{font-size:.92rem;padding:8px 12px;margin:2px 10px}.sub-link:hover,.sub-link.active{background:#f1faf5;color:#16754d}.main{margin-inline-start:280px;padding:28px;animation:appear .32s ease}.card{border:0;border-radius:8px;box-shadow:0 5px 20px rgba(26,72,53,.06);animation:rise .3s ease}.stat-icon{width:46px;height:46px;border-radius:8px;display:grid;place-items:center;background:#e4f6ed;color:var(--success);font-size:1.2rem}.btn-success{background:var(--success)!important}.table>:not(caption)>*>*{padding:.9rem .75rem}.badge-soft{background:#e4f6ed;color:#16754d}.page-title{font-weight:750}.form-control,.form-select{border-color:#dce9e2}.form-control:focus,.form-select:focus{border-color:#75c8a3;box-shadow:0 0 0 .2rem rgba(34,160,107,.12)}.report-head{border-bottom:1px solid var(--line);padding-bottom:16px;margin-bottom:18px}.report-logo{width:44px;height:44px;object-fit:contain}.money{font-variant-numeric:tabular-nums}@keyframes appear{from{opacity:0}to{opacity:1}}@keyframes rise{from{opacity:0;transform:translateY(7px)}to{opacity:1;transform:none}}[dir=rtl] .nav-link:hover,[dir=rtl] .nav-link.active,[dir=rtl] .nav-summary:hover,[dir=rtl] .nav-group[open] .nav-summary{transform:translateX(-3px)}@media(max-width:900px){.sidebar{position:static;width:auto;min-height:auto}.main{margin-inline-start:0;padding:18px}.sidebar nav{display:flex;overflow-x:auto;align-items:flex-start}.nav-section{display:none}.nav-link,.nav-summary{white-space:nowrap;margin:3px}.sub-list{position:absolute;background:#fff;border:1px solid var(--line);box-shadow:0 10px 24px rgba(26,72,53,.08);border-radius:8px;margin:0;padding:6px;z-index:20}.nav-group{position:relative}.brand{max-width:180px}}
        @media print{.sidebar,header,.no-print,.alert{display:none!important}.main{margin:0!important;padding:0!important}.card{box-shadow:none!important;border:0!important}.app-bg{background:#fff!important}}
    </style>
    @stack('styles')
</head>
<body class="app-bg">
<aside class="sidebar py-4">
    <div class="px-4 mb-4 d-flex align-items-center gap-2">
        @if($logo)<img src="/{{ $logo }}" alt="{{ $businessName }}" style="width:34px;height:34px;object-fit:contain">@else<i class="fa-solid fa-solar-panel fa-xl text-success"></i>@endif
        <span class="brand">{{ $businessName }}</span>
    </div>
    <nav>
        <a class="nav-link {{ request()->routeIs('dashboard') ? 'active' : '' }}" href="{{ route('dashboard') }}"><i class="fa-solid fa-gauge me-2"></i>{{ __('Dashboard') }}</a>
        <a class="nav-link {{ request()->routeIs('wizards.*') ? 'active' : '' }}" href="{{ route('wizards.index') }}"><i class="fa-solid fa-wand-magic-sparkles me-2"></i>{{ __('Wizard Forms') }}</a>

        <div class="nav-section">{{ __('Operations') }}</div>
        <details class="nav-group" {{ $transactionActive ? 'open' : '' }}>
            <summary class="nav-summary"><i class="fa-solid fa-right-left me-2"></i>{{ __('Transactions') }}</summary>
            <div class="sub-list">
                <a class="sub-link" href="{{ route('invoices.index') }}"><i class="fa-solid fa-file-invoice me-2"></i>{{ __('Invoices') }}</a>
                <a class="sub-link" href="{{ route('purchases.index') }}"><i class="fa-solid fa-cart-arrow-down me-2"></i>{{ __('Purchases') }}</a>
                <a class="sub-link" href="{{ route('products.index') }}"><i class="fa-solid fa-boxes-stacked me-2"></i>{{ __('Products & Stock') }}</a>
                <a class="sub-link" href="{{ route('payments.index') }}"><i class="fa-solid fa-hand-holding-dollar me-2"></i>{{ __('Payments & Receipts') }}</a>
                <a class="sub-link" href="{{ route('expenses.index') }}"><i class="fa-solid fa-money-bill-transfer me-2"></i>{{ __('Expenses') }}</a>
                <a class="sub-link" href="{{ route('returns.index') }}"><i class="fa-solid fa-rotate-left me-2"></i>{{ __('Returns') }}</a>
            </div>
        </details>
        <details class="nav-group" {{ $partyActive ? 'open' : '' }}>
            <summary class="nav-summary"><i class="fa-solid fa-address-book me-2"></i>{{ __('Parties') }}</summary>
            <div class="sub-list">
                <a class="sub-link" href="{{ route('customers.index') }}"><i class="fa-solid fa-users me-2"></i>{{ __('Customers') }}</a>
                <a class="sub-link" href="{{ route('suppliers.index') }}"><i class="fa-solid fa-truck-field me-2"></i>{{ __('Suppliers') }}</a>
                <a class="sub-link" href="{{ route('reports.customer-balances') }}"><i class="fa-solid fa-book me-2"></i>{{ __('Customer Balances') }}</a>
                <a class="sub-link" href="{{ route('reports.supplier-balances') }}"><i class="fa-solid fa-book-open me-2"></i>{{ __('Supplier Balances') }}</a>
            </div>
        </details>

        @if(auth()->user()->role === 'manager' || auth()->user()->role === 'accountant')
            <div class="nav-section">{{ __('Accounting') }}</div>
            <details class="nav-group" {{ $financeActive ? 'open' : '' }}>
                <summary class="nav-summary"><i class="fa-solid fa-building-columns me-2"></i>{{ __('Finance') }}</summary>
                <div class="sub-list">
                    <a class="sub-link" href="{{ route('finance.index') }}"><i class="fa-solid fa-building-columns me-2"></i>{{ __('Bank & Sarafi') }}</a>
                    <a class="sub-link" href="{{ route('reports.chart-of-accounts') }}"><i class="fa-solid fa-list-check me-2"></i>{{ __('Chart of Accounts') }}</a>
                    <a class="sub-link" href="{{ route('assets.index') }}"><i class="fa-solid fa-landmark me-2"></i>{{ __('Fixed Assets') }}</a>
                    <a class="sub-link" href="{{ route('vouchers.index') }}"><i class="fa-solid fa-receipt me-2"></i>{{ __('Vouchers') }}</a>
                </div>
            </details>
        @endif

        <details class="nav-group" {{ $reportsActive ? 'open' : '' }}>
            <summary class="nav-summary"><i class="fa-solid fa-chart-line me-2"></i>{{ __('Reports') }}</summary>
            <div class="sub-list">
                <a class="sub-link" href="{{ route('reports.index') }}">{{ __('Report Center') }}</a>
                <a class="sub-link" href="{{ route('reports.sales') }}">{{ __('Sales Report') }}</a>
                <a class="sub-link" href="{{ route('reports.purchases') }}">{{ __('Purchase Report') }}</a>
                <a class="sub-link" href="{{ route('reports.stock') }}">{{ __('Stock Report') }}</a>
                <a class="sub-link" href="{{ route('reports.payments') }}">{{ __('Payments Report') }}</a>
                <a class="sub-link" href="{{ route('reports.receipts') }}">{{ __('Receipts Report') }}</a>
                <a class="sub-link" href="{{ route('reports.balance-sheet') }}">{{ __('Balance Sheet') }}</a>
                <a class="sub-link" href="{{ route('reports.income-statement') }}">{{ __('Income Statement') }}</a>
            </div>
        </details>

        @if(auth()->user()->role === 'manager')
            <div class="nav-section">{{ __('Admin') }}</div>
            <a class="nav-link {{ request()->routeIs('users.*') ? 'active' : '' }}" href="{{ route('users.index') }}"><i class="fa-solid fa-user-shield me-2"></i>{{ __('Users & Roles') }}</a>
        @endif

        <details class="nav-group" {{ $configActive ? 'open' : '' }}>
            <summary class="nav-summary"><i class="fa-solid fa-sliders me-2"></i>{{ __('Configuration') }}</summary>
            <div class="sub-list">
                <a class="sub-link" href="{{ route('settings.index') }}"><i class="fa-solid fa-building me-2"></i>{{ __('Business Settings') }}</a>
                <a class="sub-link" href="{{ route('settings.index') }}#currencies"><i class="fa-solid fa-coins me-2"></i>{{ __('Currencies') }}</a>
                <a class="sub-link" href="{{ route('settings.index') }}#categories"><i class="fa-solid fa-tags me-2"></i>{{ __('Categories') }}</a>
                <a class="sub-link" href="{{ route('settings.index') }}#units"><i class="fa-solid fa-ruler-combined me-2"></i>{{ __('Units') }}</a>
                <a class="sub-link" href="{{ route('translations.index') }}"><i class="fa-solid fa-language me-2"></i>{{ __('Translation Manager') }}</a>
            </div>
        </details>

        @if(auth()->user()->role === 'manager')
            <details class="nav-group" {{ $systemActive ? 'open' : '' }}>
                <summary class="nav-summary"><i class="fa-solid fa-computer me-2"></i>{{ __('System') }}</summary>
                <div class="sub-list">
                    <a class="sub-link" href="{{ route('backup.index') }}"><i class="fa-solid fa-database me-2"></i>{{ __('Backup & Restore') }}</a>
                    <a class="sub-link" href="{{ route('desktop.index') }}"><i class="fa-solid fa-desktop me-2"></i>{{ __('Desktop Launcher') }}</a>
                </div>
            </details>
        @endif
    </nav>
</aside>
<main class="main">
    <header class="d-flex align-items-center justify-content-between mb-4">
        <div>
            <h4 class="mb-0 page-title">@yield('title', __('Dashboard'))</h4>
            <small class="text-muted">{{ \App\Support\DualDate::format(now(), 'l, d M Y') }}</small>
        </div>
        <div class="d-flex align-items-center gap-2">
            <a class="btn btn-success btn-sm" href="{{ route('invoices.create') }}"><i class="fa-solid fa-plus me-1"></i>{{ __('New Sale') }}</a>
            <div class="dropdown">
                <button class="btn btn-light btn-sm dropdown-toggle" data-mdb-dropdown-init>{{ strtoupper(app()->getLocale()) }}</button>
                <ul class="dropdown-menu dropdown-menu-end">
                    @foreach(['en' => 'English', 'fa' => 'دری', 'ps' => 'پښتو'] as $code => $label)
                        <li><form method="POST" action="{{ route('locale', $code) }}">@csrf<button class="dropdown-item {{ app()->getLocale() === $code ? 'active' : '' }}">{{ $label }}</button></form></li>
                    @endforeach
                </ul>
            </div>
            <form method="POST" action="{{ route('logout') }}">@csrf<button title="{{ __('Logout') }}" class="btn btn-light btn-sm"><i class="fa-solid fa-right-from-bracket"></i></button></form>
        </div>
    </header>
    @if(session('success'))<div class="alert alert-success alert-dismissible fade show"><i class="fa-solid fa-circle-check me-2"></i>{{ session('success') }}<button type="button" class="btn-close" data-mdb-dismiss="alert"></button></div>@endif
    @if($errors->any())<div class="alert alert-danger"><strong>{{ __('Please correct the errors below.') }}</strong><ul class="mb-0">@foreach($errors->all() as $error)<li>{{ $error }}</li>@endforeach</ul></div>@endif
    @yield('content')
</main>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/8.2.0/mdb.umd.min.js"></script>
@stack('scripts')
</body>
</html>
