{% extends 'base.html' %}

{% block title %}Form Templates — AgentForms{% endblock %}

{% block head_extra %}
<style nonce="{{ csp_nonce }}">
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: system-ui, -apple-system, sans-serif; background: #f5f5f5; color: #333; }
        a { color: inherit; text-decoration: none; }

        .container { max-width: 1100px; margin: 0 auto; padding: 24px 16px; }

        /* Page header */
        .page-header { margin-bottom: 24px; }
        .page-header h1 { font-size: 28px; font-weight: 700; margin-bottom: 6px; color: #1a1a2e; }
        .page-header p { color: #666; font-size: 15px; line-height: 1.5; }
        .page-header .count { font-size: 13px; color: #888; margin-top: 4px; }

        /* Filters */
        .filters-bar {
            background: white;
            border-radius: 8px;
            padding: 14px 16px;
            margin-bottom: 20px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.08);
        }
        .filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
        .filter-btn {
            padding: 6px 14px;
            border: 1px solid #e0e0e0;
            background: white;
            border-radius: 20px;
            cursor: pointer;
            font-size: 13px;
            transition: all 0.15s;
            color: #555;
            font-weight: 500;
        }
        .filter-btn:hover { border-color: #1a1a2e; color: #1a1a2e; }
        .filter-btn.active { background: #1a1a2e; color: white; border-color: #1a1a2e; }
        .search-box {
            padding: 6px 12px;
            border: 1px solid #e0e0e0;
            border-radius: 4px;
            font-size: 13px;
            width: 220px;
            margin-left: auto;
        }
        .search-box:focus { outline: none; border-color: #1a1a2e; }

        /* Template grid */
        .template-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(310px, 1fr));
            gap: 16px;
        }

        .template-card {
            background: white;
            border: 1px solid #e9ecef;
            border-radius: 8px;
            padding: 18px;
            transition: all 0.15s;
            position: relative;
            display: flex;
            flex-direction: column;
        }
        .template-card:hover {
            border-color: #1a1a2e;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
            transform: translateY(-1px);
        }
        .template-card.featured {
            border-left: 3px solid #27ae60;
        }

        .template-card .card-header {
            display: flex;
            align-items: flex-start;
            justify-content: space-between;
            margin-bottom: 10px;
        }
        .template-card h3 {
            font-size: 16px;
            font-weight: 600;
            color: #1a1a2e;
            line-height: 1.3;
        }
        .template-card .icon { font-size: 20px; margin-right: 10px; flex-shrink: 0; }

        .template-card .category-badge {
            display: inline-block;
            padding: 2px 10px;
            background: #f0f4ff;
            color: #1a5276;
            border-radius: 12px;
            font-size: 11px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 10px;
        }

        .template-card .description {
            font-size: 13px;
            color: #666;
            margin-bottom: 14px;
            line-height: 1.5;
            flex: 1;
        }

        .template-card .meta {
            display: flex;
            gap: 6px;
            flex-wrap: wrap;
            margin-bottom: 14px;
        }
        .template-card .meta .badge {
            padding: 2px 8px;
            background: #f5f5f5;
            border-radius: 12px;
            font-size: 11px;
            color: #777;
        }
        .template-card .meta .badge.conditional { background: #fef3e2; color: #e67e22; }
        .template-card .meta .badge.multi-step { background: #e8f5e9; color: #27ae60; }
        .template-card .meta .badge.featured-badge { background: #fff3cd; color: #856404; }

        .template-card .actions { display: flex; gap: 8px; margin-top: auto; }
        .template-card .actions .btn {
            padding: 7px 14px;
            border-radius: 4px;
            font-size: 13px;
            font-weight: 500;
            cursor: pointer;
            text-align: center;
            transition: all 0.15s;
            border: 1px solid transparent;
            flex: 1;
        }
        .template-card .actions .btn-outline {
            background: white;
            border-color: #e0e0e0;
            color: #555;
        }
        .template-card .actions .btn-outline:hover {
            border-color: #1a1a2e;
            color: #1a1a2e;
        }
        .template-card .actions .btn-primary {
            background: #1a1a2e;
            color: white;
        }
        .template-card .actions .btn-primary:hover {
            background: #2d2d4a;
        }

        /* Preview modal */
        .modal-overlay {
            display: none;
            position: fixed;
            top: 0; left: 0; right: 0; bottom: 0;
            background: rgba(0,0,0,0.5);
            z-index: 100;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .modal-overlay.active { display: flex; }
        .modal {
            background: white;
            border-radius: 8px;
            max-width: 600px;
            width: 100%;
            max-height: 85vh;
            overflow-y: auto;
            padding: 24px;
            position: relative;
        }
        .modal .close-btn {
            position: absolute;
            top: 16px; right: 16px;
            background: none; border: none;
            font-size: 22px; cursor: pointer;
            color: #888; line-height: 1;
        }
        .modal .close-btn:hover { color: #333; }
        .modal h2 { font-size: 20px; margin-bottom: 6px; color: #1a1a2e; }
        .modal .description { color: #666; margin-bottom: 16px; line-height: 1.5; }

        .modal .field-list { margin-bottom: 16px; }
        .modal .field-item {
            padding: 8px 0;
            border-bottom: 1px solid #f0f0f0;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .modal .field-item:last-child { border-bottom: none; }
        .modal .field-label { font-weight: 500; font-size: 14px; }
        .modal .field-meta { font-size: 12px; color: #888; }
        .modal .field-badge {
            display: inline-block;
            padding: 1px 6px;
            background: #f0f0f0;
            border-radius: 3px;
            font-size: 11px;
            margin-left: 4px;
        }
        .modal .field-badge.required { background: #fde8e8; color: #c0392b; }
        .modal .field-badge.conditional { background: #fef3e2; color: #e67e22; }
        .modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }

        /* Steps section in modal */
        .modal .steps-section { margin-bottom: 16px; }
        .modal .steps-section h4 { font-size: 14px; color: #1a1a2e; margin-bottom: 8px; }
        .modal .step-group { padding: 8px 12px; background: #f8f9fa; border-radius: 4px; margin-bottom: 4px; }
        .modal .step-group .step-title { font-weight: 600; font-size: 13px; }
        .modal .step-group .step-fields { font-size: 12px; color: #666; margin-top: 2px; }

        /* Toast */
        .toast {
            position: fixed;
            bottom: 20px; right: 20px;
            background: #27ae60;
            color: white;
            padding: 12px 20px;
            border-radius: 4px;
            font-size: 14px;
            z-index: 200;
            transform: translateY(100px);
            transition: transform 0.3s;
        }
        .toast.show { transform: translateY(0); }
        .toast.error { background: #e74c3c; }

        /* Loading */
        .loading { text-align: center; padding: 40px; color: #888; }
        .loading .spinner {
            display: inline-block;
            width: 24px; height: 24px;
            border: 2px solid #e9ecef;
            border-top-color: #1a1a2e;
            border-radius: 50%;
            animation: spin 0.6s linear infinite;
        }
        @keyframes spin { to { transform: rotate(360deg); } }

        /* Empty state */
        .empty-state { text-align: center; padding: 40px; color: #888; }
        .empty-state p { font-size: 15px; margin-top: 8px; }

        @media (max-width: 640px) {
            .container { padding: 16px; }
            .template-grid { grid-template-columns: 1fr; }
            .filters { flex-direction: column; }
            .search-box { width: 100%; margin-left: 0; }
            .page-header h1 { font-size: 24px; }
        }
    </style>
{% endblock %}

{% block content %}

    <div class="container">
        <div class="page-header">
            <h1>Form Templates</h1>
            <p>Start with a pre-built form — customize fields, add your branding, and go live in minutes.</p>
            <div class="count" id="templateCount"></div>
        </div>

        <!-- Filters -->
        <div class="filters-bar">
            <div class="filters" id="filterBar">
                <button class="filter-btn active" data-category="all">All Templates</button>
                <div id="categoryFilters" class="filters" style="display:contents;"></div>
                <input type="text" class="search-box" id="searchBox" placeholder="Search templates..." />
            </div>
        </div>

        <!-- Template grid -->
        <div class="template-grid" id="templateGrid">
            <div class="loading">
                <div class="spinner"></div>
                <p style="margin-top:8px;">Loading templates...</p>
            </div>
        </div>
    </div>

    <!-- Preview Modal -->
    <div class="modal-overlay" id="previewModal">
        <div class="modal" id="modalContent">
            <button class="close-btn" style="position:absolute;top:16px;right:16px;background:none;border:none;font-size:22px;cursor:pointer;color:#888;">&times;</button>
            <div class="loading"><div class="spinner"></div></div>
        </div>
    </div>

    <!-- Toast -->
    <div class="toast" id="toast"></div>
{% endblock %}

{% block scripts %}
<script src="/static/js/templates-browser-inline.js"></script>
{% endblock %}