<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}Hierarchy Management{% endblock %}</title>
    <!-- HTMX 2.0 -->
    <script src="https://unpkg.com/htmx.org@2.0.4"></script>
    <style>
        :root {
            --primary: #2563eb;
            --primary-dark: #1d4ed8;
            --secondary: #64748b;
            --success: #22c55e;
            --danger: #ef4444;
            --warning: #f59e0b;
            --bg: #f8fafc;
            --card-bg: #ffffff;
            --border: #e2e8f0;
            --text: #1e293b;
            --text-muted: #64748b;
        }
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: var(--bg);
            color: var(--text);
            line-height: 1.6;
        }
        .container { max-width: 1200px; margin: 0 auto; padding: 2rem; }
        .card {
            background: var(--card-bg);
            border-radius: 8px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            padding: 1.5rem;
            margin-bottom: 1.5rem;
        }
        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 1rem;
            padding-bottom: 1rem;
            border-bottom: 1px solid var(--border);
        }
        h1, h2, h3 { font-weight: 600; }
        h1 { font-size: 1.75rem; }
        h2 { font-size: 1.25rem; }
        h3 { font-size: 1rem; }
        .btn {
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
            padding: 0.5rem 1rem;
            border-radius: 6px;
            font-size: 0.875rem;
            font-weight: 500;
            text-decoration: none;
            cursor: pointer;
            border: none;
            transition: all 0.15s;
        }
        .btn-primary { background: var(--primary); color: white; }
        .btn-primary:hover { background: var(--primary-dark); }
        .btn-secondary { background: var(--secondary); color: white; }
        .btn-danger { background: var(--danger); color: white; }
        .btn-sm { padding: 0.25rem 0.5rem; font-size: 0.75rem; }
        .badge {
            display: inline-block;
            padding: 0.25rem 0.5rem;
            border-radius: 4px;
            font-size: 0.75rem;
            font-weight: 500;
        }
        .badge-owner { background: #dbeafe; color: #1e40af; }
        .badge-manager { background: #dcfce7; color: #166534; }
        .badge-viewer { background: #f3e8ff; color: #6b21a8; }
        .badge-admin { background: #fef3c7; color: #92400e; }
        .badge-lead { background: #fed7aa; color: #c2410c; }
        .badge-member { background: #e0e7ff; color: #3730a3; }
        .breadcrumb {
            display: flex;
            gap: 0.5rem;
            align-items: center;
            margin-bottom: 1.5rem;
            font-size: 0.875rem;
            color: var(--text-muted);
        }
        .breadcrumb a { color: var(--primary); text-decoration: none; }
        .breadcrumb a:hover { text-decoration: underline; }
        .form-group { margin-bottom: 1rem; }
        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 500;
            font-size: 0.875rem;
        }
        .form-control {
            width: 100%;
            padding: 0.5rem 0.75rem;
            border: 1px solid var(--border);
            border-radius: 6px;
            font-size: 0.875rem;
        }
        .form-control:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
        }
        select.form-control { cursor: pointer; }
        table { width: 100%; border-collapse: collapse; }
        th, td {
            padding: 0.75rem;
            text-align: left;
            border-bottom: 1px solid var(--border);
        }
        th { font-weight: 600; font-size: 0.75rem; text-transform: uppercase; color: var(--text-muted); }
        .empty-state {
            text-align: center;
            padding: 3rem;
            color: var(--text-muted);
        }
        .alert {
            padding: 1rem;
            border-radius: 6px;
            margin-bottom: 1rem;
        }
        .alert-success { background: #dcfce7; color: #166534; }
        .alert-error { background: #fee2e2; color: #991b1b; }
        nav {
            background: var(--card-bg);
            border-bottom: 1px solid var(--border);
            padding: 1rem 2rem;
            margin-bottom: 2rem;
        }
        nav a { color: var(--text); text-decoration: none; font-weight: 500; }
        /* HTMX loading states */
        .htmx-request { opacity: 0.7; pointer-events: none; }
        .htmx-request.htmx-indicator { opacity: 1; }
        .htmx-indicator { display: none; }
        .htmx-request .htmx-indicator,
        .htmx-request.htmx-indicator { display: inline-block; }
        @keyframes spin { to { transform: rotate(360deg); } }
        .spinner {
            width: 16px; height: 16px;
            border: 2px solid var(--border);
            border-top-color: var(--primary);
            border-radius: 50%;
            animation: spin 0.6s linear infinite;
        }
        /* Toast notifications */
        .toast-container {
            position: fixed; bottom: 1rem; right: 1rem;
            z-index: 1000; display: flex; flex-direction: column; gap: 0.5rem;
        }
        .toast {
            padding: 0.75rem 1rem; border-radius: 6px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
            animation: slideIn 0.3s ease;
        }
        .toast-success { background: #22c55e; color: white; }
        .toast-error { background: #ef4444; color: white; }
        @keyframes slideIn { from { transform: translateX(100%); opacity: 0; } }
        {% block extra_styles %}{% endblock %}
    </style>
</head>
<body>
    <nav>
        <a href="{% url 'rebac:hierarchy_tree' tenant_pk=tenant.pk %}">{{ tenant.name }} - Hierarchy</a>
    </nav>
    <div class="container">
        {% if messages %}
            {% for message in messages %}
                <div class="alert alert-{{ message.tags }}">{{ message }}</div>
            {% endfor %}
        {% endif %}
        {% block content %}{% endblock %}
    </div>
    <!-- Toast container for notifications -->
    <div id="toast-container" class="toast-container"></div>

    <script>
        // HTMX event handlers for notifications
        document.body.addEventListener('htmx:afterSwap', function(evt) {
            // Check for success message in response headers
            const msg = evt.detail.xhr.getResponseHeader('HX-Trigger-After-Swap');
            if (msg) showToast(msg, 'success');
        });

        document.body.addEventListener('htmx:responseError', function(evt) {
            showToast('An error occurred. Please try again.', 'error');
        });

        function showToast(message, type = 'success') {
            const container = document.getElementById('toast-container');
            const toast = document.createElement('div');
            toast.className = `toast toast-${type}`;
            toast.textContent = message;
            container.appendChild(toast);
            setTimeout(() => toast.remove(), 3000);
        }

        // Expose for manual use
        window.RebacUI = { showToast };
    </script>
    {% block extra_scripts %}{% endblock %}
</body>
</html>
