<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>@yield('title', 'Hojas de Tiempo') — Chronos Gessig</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">

    <style>
        :root { --primary: #1a4f8a; }
        body { background: #f4f6f9; font-size: 0.9rem; margin: 0; }

        /* NAVBAR BLANCO */
        .top-navbar {
            background: #ffffff !important;
            border-bottom: 3px solid var(--primary);
            padding: 0 1rem;
            height: 52px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            position: sticky;
            top: 0;
            z-index: 1030;
            box-shadow: 0 1px 4px rgba(0,0,0,.08);
        }
        .brand { font-weight: 700; color: var(--primary); font-size: 1rem;
                 text-decoration: none; display: flex; align-items: center; gap: 6px; }
        .brand i { font-size: 1.2rem; }
        .btn-menu-mobile { display: none; background: none; border: none;
                           font-size: 1.4rem; color: var(--primary); cursor: pointer; padding: 4px 8px; }
        @media(max-width:991px){ .btn-menu-mobile { display: block; } }

        /* SIDEBAR DESKTOP */
        .sidebar-wrap {
            width: 88px;
            height: calc(100vh - 52px);
            background: #fff;
            border-right: 1px solid #e0e0e0;
            display: flex;
            flex-direction: column;
            padding: 8px 0 16px;
            position: fixed;
            top: 52px; left: 0;
            overflow-y: auto;
            overflow-x: hidden;
            z-index: 100;
            scrollbar-width: thin;
            scrollbar-color: #ddd transparent;
        }
        .sidebar-wrap::-webkit-scrollbar { width: 3px; }
        .sidebar-wrap::-webkit-scrollbar-thumb { background: #ccc; border-radius: 2px; }
        @media(max-width:991px){ .sidebar-wrap { display: none; } }

        .sidebar-sep { font-size: .58rem; font-weight: 700; text-transform: uppercase;
                       letter-spacing: .08em; color: #bbb; text-align: center; padding: 10px 4px 4px; }

        .nav-item-icon {
            display: flex; flex-direction: column; align-items: center;
            text-decoration: none; padding: 8px 4px 6px; margin: 2px 6px;
            border-radius: 10px; color: #444; transition: background .15s;
        }
        .nav-item-icon:hover { background: #f0f4ff; color: var(--primary); }
        .nav-item-icon.active { background: #e8f0fd; color: var(--primary); }
        .nav-ic { width: 38px; height: 38px; border-radius: 10px;
                  display: flex; align-items: center; justify-content: center;
                  font-size: 1.1rem; margin-bottom: 4px; }
        .nav-lbl { font-size: .6rem; text-align: center; line-height: 1.2; }

        .ic-blue   { background: #e3f0ff; color: #1a4f8a; }
        .ic-teal   { background: #e0f7f4; color: #00796b; }
        .ic-purple { background: #f3e5f5; color: #6a1b9a; }
        .ic-amber  { background: #fff8e1; color: #f57f17; }
        .ic-coral  { background: #fce4ec; color: #c62828; }
        .ic-green  { background: #e8f5e9; color: #2e7d32; }
        .ic-pink   { background: #fce4ec; color: #880e4f; }
        .ic-active { background: var(--primary); color: #fff; }

        /* MAIN */
        .main-content { margin-left: 88px; padding: 1.25rem 1.5rem; min-height: calc(100vh - 52px); }
        @media(max-width:991px){ .main-content { margin-left: 0; padding: 1rem; } }

        /* OFFCANVAS MÓVIL */
        .offcanvas-nav { width: 240px !important; }
        .offcanvas-nav .nav-item-icon { flex-direction: row; gap: 10px;
            padding: 10px 12px; margin: 2px 8px; border-radius: 8px; }
        .offcanvas-nav .nav-ic { margin: 0; width: 32px; height: 32px; font-size: 1rem; }
        .offcanvas-nav .nav-lbl { font-size: .85rem; font-weight: 500; }
        .offcanvas-nav .sidebar-sep { text-align: left; padding: 10px 16px 4px; }

        /* Generales */
        .card { border: none; box-shadow: 0 1px 4px rgba(0,0,0,.1); }
        .card-header { background: var(--primary); color: #fff; font-weight: 600; }
        .badge-exento { background: #e3f2fd; color: #1565c0; border: 1px solid #90caf9; }
        .estado-borrador { background: #e0e0e0; color: #616161; }
        .estado-enviado  { background: #fff9c4; color: #f57f17; }
        .estado-aprobado { background: #e8f5e9; color: #2e7d32; }
        .table-grilla th, .table-grilla td { padding: 4px 6px; font-size: .8rem; }
        .cell-festivo { background: #fff3e0 !important; }
        .cell-domingo { background: #fce4ec !important; }
        .cell-sabado  { background: #f3e5f5 !important; }
        .input-hora { width: 52px; text-align: center; padding: 2px;
                      border: 1px solid #ced4da; border-radius: 4px; }
        .input-hora:focus { border-color: var(--primary); outline: none; }
        .total-dia { font-weight: 700; background: #e8f5e9 !important; }
        @media print { .sidebar-wrap,.top-navbar,.no-print { display:none!important; }
                        .main-content { margin-left: 0; } }
    </style>
    @stack('styles')
</head>
<body>

{{-- NAVBAR BLANCO --}}
<header class="top-navbar">
    <div class="d-flex align-items-center gap-2">
        @auth
        <button class="btn-menu-mobile" data-bs-toggle="offcanvas" data-bs-target="#navMobile">
            <i class="bi bi-list"></i>
        </button>
        @endauth
        <a href="{{ route('dashboard') }}" class="brand">
            <img src="{{ asset('images/logo-gessig.png') }}"
                 alt="Gessig"
                 style="height:36px;width:auto;object-fit:contain;display:block"
                 id="navbar-logo"
                 onerror="document.getElementById('navbar-logo').style.display='none';document.getElementById('brand-fallback').style.display='inline-flex'">
            <span id="brand-fallback" style="display:none;align-items:center;gap:5px">
                <i class="bi bi-clock-history"></i><span>Chronos&nbsp;Gessig</span>
            </span>
        </a>
    </div>
    @auth
    <div class="dropdown">
        <button class="btn btn-sm btn-outline-secondary dropdown-toggle" data-bs-toggle="dropdown">
            <i class="bi bi-person-circle me-1"></i>{{ Auth::user()->name }}
        </button>
        <ul class="dropdown-menu dropdown-menu-end">
            <li><span class="dropdown-item-text text-muted small">{{ Auth::user()->rol }}</span></li>
            <li><hr class="dropdown-divider"></li>
            <li>
                <form action="{{ route('logout') }}" method="POST">
                    @csrf
                    <button class="dropdown-item"><i class="bi bi-box-arrow-right me-1"></i>Salir</button>
                </form>
            </li>
        </ul>
    </div>
    @endauth
</header>

@auth
{{-- SIDEBAR DESKTOP --}}
<nav class="sidebar-wrap">

    <div class="sidebar-sep">Inicio</div>
    <a href="{{ route('dashboard') }}" class="nav-item-icon {{ request()->routeIs('dashboard') ? 'active' : '' }}">
        <div class="nav-ic {{ request()->routeIs('dashboard') ? 'ic-active' : 'ic-blue' }}"><i class="bi bi-speedometer2"></i></div>
        <span class="nav-lbl">Dashboard</span>
    </a>
    <a href="{{ route('hojas-tiempo.index') }}" class="nav-item-icon {{ request()->routeIs('hojas-tiempo.*') ? 'active' : '' }}">
        <div class="nav-ic {{ request()->routeIs('hojas-tiempo.*') ? 'ic-active' : 'ic-teal' }}"><i class="bi bi-table"></i></div>
        <span class="nav-lbl">Hojas de Tiempo</span>
    </a>

    @if(in_array(Auth::user()->rol ?? '', ['admin','supervisor','sst']))
    <div class="sidebar-sep">Reportes</div>
    <a href="{{ route('reportes.consolidado') }}" class="nav-item-icon {{ request()->routeIs('reportes.consolidado') ? 'active' : '' }}">
        <div class="nav-ic {{ request()->routeIs('reportes.consolidado') ? 'ic-active' : 'ic-purple' }}"><i class="bi bi-bar-chart-line"></i></div>
        <span class="nav-lbl">Consolidado</span>
    </a>
    @if(\App\Models\Configuracion::getBool('modulo_incapacidades', true))
<a href="{{ route('incapacidades.index') }}" class="nav-item-icon {{ request()->routeIs('incapacidades.*') ? 'active' : '' }}">
        <div class="nav-ic {{ request()->routeIs('incapacidades.*') ? 'ic-active' : 'ic-coral' }}"><i class="bi bi-bandaid"></i></div>
        <span class="nav-lbl">Incapaci­dades</span>
    </a>
@endif
    @endif

    @if(in_array(Auth::user()->rol ?? '', ['admin','supervisor']))
    <div class="sidebar-sep">Config</div>
    <a href="{{ route('empleados.index') }}" class="nav-item-icon {{ request()->routeIs('empleados.*') ? 'active' : '' }}">
        <div class="nav-ic {{ request()->routeIs('empleados.*') ? 'ic-active' : 'ic-pink' }}"><i class="bi bi-people"></i></div>
        <span class="nav-lbl">Empleados</span>
    </a>
    <a href="{{ route('periodos.index') }}" class="nav-item-icon {{ request()->routeIs('periodos.*') ? 'active' : '' }}">
        <div class="nav-ic {{ request()->routeIs('periodos.*') ? 'ic-active' : 'ic-green' }}"><i class="bi bi-calendar3"></i></div>
        <span class="nav-lbl">Períodos</span>
    </a>
    <a href="{{ route('centros-costo.index') }}" class="nav-item-icon {{ request()->routeIs('centros-costo.*') ? 'active' : '' }}">
        <div class="nav-ic {{ request()->routeIs('centros-costo.*') ? 'ic-active' : 'ic-blue' }}"><i class="bi bi-diagram-3"></i></div>
        <span class="nav-lbl">Centros de Costo</span>
    </a>
    <a href="{{ route('calendario.index') }}" class="nav-item-icon {{ request()->routeIs('calendario.*') ? 'active' : '' }}">
        <div class="nav-ic {{ request()->routeIs('calendario.*') ? 'ic-active' : 'ic-teal' }}"><i class="bi bi-calendar-event"></i></div>
        <span class="nav-lbl">Calendario</span>
    </a>
    <a href="{{ route('configuracion.index') }}" class="nav-item-icon {{ request()->routeIs('configuracion.*') ? 'active' : '' }}">
        <div class="nav-ic {{ request()->routeIs('configuracion.*') ? 'ic-active' : 'ic-purple' }}"><i class="bi bi-sliders"></i></div>
        <span class="nav-lbl">Parámetros</span>
    </a>
    @endif

</nav>

{{-- OFFCANVAS MÓVIL --}}
<div class="offcanvas offcanvas-start offcanvas-nav" tabindex="-1" id="navMobile">
    <div class="offcanvas-header" style="background:var(--primary);color:#fff;padding:.75rem 1rem;">
        <span class="fw-bold"><i class="bi bi-clock-history me-1"></i> Chronos Gessig</span>
        <button class="btn-close btn-close-white" data-bs-dismiss="offcanvas"></button>
    </div>
    <div class="offcanvas-body p-0 pt-2">
        <div class="sidebar-sep">Inicio</div>
        <a href="{{ route('dashboard') }}" class="nav-item-icon {{ request()->routeIs('dashboard') ? 'active' : '' }}" data-bs-dismiss="offcanvas">
            <div class="nav-ic ic-blue"><i class="bi bi-speedometer2"></i></div><span class="nav-lbl">Dashboard</span>
        </a>
        <a href="{{ route('hojas-tiempo.index') }}" class="nav-item-icon {{ request()->routeIs('hojas-tiempo.*') ? 'active' : '' }}" data-bs-dismiss="offcanvas">
            <div class="nav-ic ic-teal"><i class="bi bi-table"></i></div><span class="nav-lbl">Hojas de Tiempo</span>
        </a>

        @if(in_array(Auth::user()->rol ?? '', ['admin','supervisor','sst']))
        <div class="sidebar-sep">Reportes</div>
        <a href="{{ route('reportes.consolidado') }}" class="nav-item-icon {{ request()->routeIs('reportes.consolidado') ? 'active' : '' }}" data-bs-dismiss="offcanvas">
            <div class="nav-ic ic-purple"><i class="bi bi-bar-chart-line"></i></div><span class="nav-lbl">Consolidado</span>
        </a>
        @if(\App\Models\Configuracion::getBool('modulo_incapacidades', true))
<a href="{{ route('incapacidades.index') }}" class="nav-item-icon {{ request()->routeIs('incapacidades.*') ? 'active' : '' }}" data-bs-dismiss="offcanvas">
            <div class="nav-ic ic-coral"><i class="bi bi-bandaid"></i></div><span class="nav-lbl">Incapacidades</span>
        </a>
@endif
        @endif

        @if(in_array(Auth::user()->rol ?? '', ['admin','supervisor']))
        <div class="sidebar-sep">Config</div>
        <a href="{{ route('empleados.index') }}" class="nav-item-icon {{ request()->routeIs('empleados.*') ? 'active' : '' }}" data-bs-dismiss="offcanvas">
            <div class="nav-ic ic-pink"><i class="bi bi-people"></i></div><span class="nav-lbl">Empleados</span>
        </a>
        <a href="{{ route('periodos.index') }}" class="nav-item-icon {{ request()->routeIs('periodos.*') ? 'active' : '' }}" data-bs-dismiss="offcanvas">
            <div class="nav-ic ic-green"><i class="bi bi-calendar3"></i></div><span class="nav-lbl">Períodos</span>
        </a>
        <a href="{{ route('centros-costo.index') }}" class="nav-item-icon {{ request()->routeIs('centros-costo.*') ? 'active' : '' }}" data-bs-dismiss="offcanvas">
            <div class="nav-ic ic-blue"><i class="bi bi-diagram-3"></i></div><span class="nav-lbl">Centros de Costo</span>
        </a>
        <a href="{{ route('calendario.index') }}" class="nav-item-icon {{ request()->routeIs('calendario.*') ? 'active' : '' }}" data-bs-dismiss="offcanvas">
            <div class="nav-ic ic-teal"><i class="bi bi-calendar-event"></i></div><span class="nav-lbl">Calendario</span>
        </a>
        <a href="{{ route('configuracion.index') }}" class="nav-item-icon {{ request()->routeIs('configuracion.*') ? 'active' : '' }}" data-bs-dismiss="offcanvas">
            <div class="nav-ic ic-purple"><i class="bi bi-sliders"></i></div><span class="nav-lbl">Parámetros</span>
        </a>
        @endif
    </div>
</div>
@endauth

{{-- CONTENIDO --}}
<main class="main-content">
    @if(session('success'))
    <div class="alert alert-success alert-dismissible fade show">
        <i class="bi bi-check-circle me-1"></i>{{ session('success') }}
        <button class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif
    @if(session('error'))
    <div class="alert alert-danger alert-dismissible fade show">
        <i class="bi bi-exclamation-circle me-1"></i>{{ session('error') }}
        <button class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif
    @if($errors->any())
    <div class="alert alert-danger alert-dismissible fade show">
        <ul class="mb-0">@foreach($errors->all() as $e)<li>{{ $e }}</li>@endforeach</ul>
        <button class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif

    @yield('content')
</main>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
@stack('scripts')
</body>
</html>
