<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}{{ session.company_name or 'Unternehmen' }} - LinkedIn Posts{% endblock %}</title>
    <link rel="icon" type="image/png" href="/static/favicon.png">
    <link rel="preload" href="/static/tailwind.css" as="style">
    <link rel="stylesheet" href="/static/tailwind.css">
    <style>
        body { background-color: #3d4848; }
        .nav-link.active { background-color: #ffc700; color: #2d3838; }
        .nav-link.active svg { stroke: #2d3838; }
        .btn-primary { background-color: #ffc700; color: #2d3838; }
        .btn-primary:hover { background-color: #e6b300; }
        .sidebar-bg { background-color: #2d3838; }
        .card-bg { background-color: #4a5858; border-color: #5a6868; }
        .input-bg { background-color: #3d4848; border-color: #5a6868; }
        .input-bg:focus { border-color: #ffc700; outline: none; }
        ::-webkit-scrollbar { width: 8px; height: 8px; }
        ::-webkit-scrollbar-track { background: #3d4848; }
        ::-webkit-scrollbar-thumb { background: #5a6868; border-radius: 4px; }
        ::-webkit-scrollbar-thumb:hover { background: #6a7878; }

        /* Sidebar collapse styles */
        aside { transition: width 0.3s ease; }
        aside.collapsed { width: 4rem; }
        aside.collapsed .sidebar-text { display: none; }
        aside.collapsed .nav-link { justify-content: center; padding-left: 1rem; padding-right: 1rem; }
        aside.collapsed .logo-container { justify-content: center; }
        aside.collapsed .logo-full { display: none; }
        aside.collapsed .company-info { display: none; }
        aside.collapsed .section-title { display: none; }
        /* Center company profile when collapsed */
        aside.collapsed .p-4.border-b > div { justify-content: center; }
        /* Center bottom links when collapsed */
        aside.collapsed .p-4.border-t a { justify-content: center; padding-left: 1rem; padding-right: 1rem; }
        main { transition: margin-left 0.3s ease; }
        main.sidebar-collapsed { margin-left: 4rem; }
        .toggle-btn { cursor: pointer; transition: transform 0.3s ease; }
        aside.collapsed .toggle-btn { transform: rotate(180deg); }
        /* Hide nav scrollbar when sidebar collapsed */
        aside.collapsed nav { overflow-y: hidden; overflow-x: hidden; }
        /* Subtle scrollbar inside nav */
        nav::-webkit-scrollbar { width: 4px; }
        nav::-webkit-scrollbar-track { background: transparent; }
        nav::-webkit-scrollbar-thumb { background: #4a5858; border-radius: 4px; }
        nav::-webkit-scrollbar-thumb:hover { background: #5a6868; }
    </style>

    <!-- Prevent sidebar flash on page load -->
    <script>
    (function() {
        const isCollapsed = localStorage.getItem('sidebarCollapsed') === 'true';
        if (isCollapsed) {
            document.documentElement.classList.add('sidebar-loading-collapsed');
        }
    })();
    </script>
    <style>
        html.sidebar-loading-collapsed aside#sidebar { width: 4rem; }
        html.sidebar-loading-collapsed aside#sidebar .sidebar-text { display: none; }
        html.sidebar-loading-collapsed aside#sidebar .nav-link { justify-content: center; padding-left: 1rem; padding-right: 1rem; }
        html.sidebar-loading-collapsed aside#sidebar .logo-full { display: none; }
        html.sidebar-loading-collapsed aside#sidebar .company-info { display: none; }
        html.sidebar-loading-collapsed aside#sidebar .section-title { display: none; }
        html.sidebar-loading-collapsed aside#sidebar .p-4.border-b > div { justify-content: center; }
        html.sidebar-loading-collapsed aside#sidebar .p-4.border-t a { justify-content: center; padding-left: 1rem; padding-right: 1rem; }
        html.sidebar-loading-collapsed main#mainContent { margin-left: 4rem; }
    </style>

    {% block head %}{% endblock %}
</head>
<body class="text-gray-100 min-h-screen flex {% if limit_reached %}pt-6{% endif %}">

    {% if limit_reached %}
    <!-- Token Limit Banner -->
    <div class="fixed top-0 left-0 right-0 z-[9999] bg-red-600 text-white text-xs font-medium flex items-center justify-center gap-2 px-4" style="height: 1.5rem;">
        <svg class="w-3.5 h-3.5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/></svg>
        Token-Limit erreicht – keine KI-Aktionen mehr heute möglich. Morgen wird das Limit zurückgesetzt.
    </div>
    {% endif %}

    <!-- Sidebar -->
    <aside id="sidebar" class="w-64 sidebar-bg border-r border-gray-600 flex flex-col fixed h-full top-0" {% if limit_reached %}style="top: 1.5rem; height: calc(100vh - 1.5rem);"{% endif %}>
        <div class="p-4 border-b border-gray-600">
            <div class="flex items-center justify-between gap-3 logo-container">
                <div class="logo-full">
                    <img src="/static/logo.png" alt="Logo" class="h-15 w-auto">
                </div>
                <button onclick="toggleSidebar()" class="toggle-btn text-gray-400 hover:text-white">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/>
                    </svg>
                </button>
            </div>
        </div>

        <!-- Company Profile -->
        {% if session %}
        <div class="p-4 border-b border-gray-600">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 rounded-lg overflow-hidden bg-brand-highlight flex items-center justify-center flex-shrink-0">
                    <svg class="w-6 h-6 text-brand-bg-dark" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"/>
                    </svg>
                </div>
                <div class="flex-1 min-w-0 company-info">
                    <p class="text-white font-medium text-sm truncate">{{ session.company_name or 'Unternehmen' }}</p>
                    <p class="text-gray-400 text-xs truncate">{{ session.email or '' }}</p>
                </div>
            </div>
        </div>
        {% endif %}

        <nav class="flex-1 p-4 space-y-2 overflow-y-auto min-h-0">
            <a href="/" class="nav-link flex items-center gap-3 px-4 py-3 rounded-lg text-gray-300 hover:bg-brand-bg-light transition-colors {% if page == 'home' %}active{% endif %}">
                <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"/></svg>
                <span class="sidebar-text">Dashboard</span>
            </a>
            <a href="/company/accounts" class="nav-link flex items-center gap-3 px-4 py-3 rounded-lg text-gray-300 hover:bg-brand-bg-light transition-colors {% if page == 'accounts' %}active{% endif %}">
                <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"/></svg>
                <span class="sidebar-text">Mitarbeiter</span>
            </a>
            <a href="/company/strategy" class="nav-link flex items-center gap-3 px-4 py-3 rounded-lg text-gray-300 hover:bg-brand-bg-light transition-colors {% if page == 'strategy' %}active{% endif %}">
                <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/></svg>
                <span class="sidebar-text">Strategie</span>
            </a>

            <div class="pt-4 mt-4 border-t border-gray-600">
                <p class="px-4 text-xs text-gray-500 uppercase tracking-wider mb-2 section-title">Mitarbeiter-Aktionen</p>
                <a href="/company/manage" class="nav-link flex items-center gap-3 px-4 py-3 rounded-lg text-gray-300 hover:bg-brand-bg-light transition-colors {% if page == 'manage' %}active{% endif %}">
                    <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
                    <span class="sidebar-text">Inhalte verwalten</span>
                </a>
                <a href="/company/calendar" class="nav-link flex items-center gap-3 px-4 py-3 rounded-lg text-gray-300 hover:bg-brand-bg-light transition-colors {% if page == 'calendar' %}active{% endif %}">
                    <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"/></svg>
                    <span class="sidebar-text">Posting-Kalender</span>
                </a>
            </div>
        </nav>

        <div class="p-4 border-t border-gray-600 space-y-2">
            <a href="/logout" class="flex items-center gap-2 text-gray-400 hover:text-gray-200 text-sm transition-colors">
                <svg class="w-4 h-4 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"/></svg>
                <span class="sidebar-text">Logout</span>
            </a>
        </div>
    </aside>

    <!-- Main Content -->
    <main id="mainContent" class="flex-1 ml-64">
        <div class="p-8">
            {% block content %}{% endblock %}
        </div>
    </main>

    <!-- HTTPS Image Proxy for Supabase Storage -->
    <script>
    (function() {
        function proxySupabaseImages() {
            document.querySelectorAll('img[src*="/storage/v1/object/public/"]').forEach(img => {
                const originalSrc = img.src;
                if (originalSrc.startsWith('http://')) {
                    const match = originalSrc.match(/\/storage\/v1\/object\/public\/([^\/]+)\/(.+)/);
                    if (match) {
                        const bucket = match[1];
                        const path = match[2];
                        img.src = `/proxy/image/${bucket}/${path}`;
                    }
                }
            });
        }

        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', proxySupabaseImages);
        } else {
            proxySupabaseImages();
        }

        const observer = new MutationObserver(proxySupabaseImages);
        observer.observe(document.body, { childList: true, subtree: true });
    })();
    </script>

    <!-- Sidebar Toggle Script -->
    <script>
    function toggleSidebar() {
        const sidebar = document.getElementById('sidebar');
        const mainContent = document.getElementById('mainContent');

        sidebar.classList.toggle('collapsed');
        mainContent.classList.toggle('sidebar-collapsed');

        // Save state to localStorage
        const isCollapsed = sidebar.classList.contains('collapsed');
        localStorage.setItem('sidebarCollapsed', isCollapsed);
    }

    // Apply proper classes after DOM loads and remove loading class
    document.addEventListener('DOMContentLoaded', function() {
        const isCollapsed = localStorage.getItem('sidebarCollapsed') === 'true';
        const sidebar = document.getElementById('sidebar');
        const mainContent = document.getElementById('mainContent');

        if (isCollapsed) {
            sidebar.classList.add('collapsed');
            mainContent.classList.add('sidebar-collapsed');
        }

        // Remove loading class
        document.documentElement.classList.remove('sidebar-loading-collapsed');
    });
    </script>

    {% block scripts %}{% endblock %}
    <script>
    if ('serviceWorker' in navigator) {
        window.addEventListener('load', function() {
            navigator.serviceWorker.register('/sw.js').catch(function() {});
        });
    }
    </script>
</body>
</html>
