{{define "content"}}
    <div class="px-8 pt-6 pb-8" style="max-width:calc(100vw - 14rem)">
      <!-- Header with search -->
      <div class="flex items-center justify-between mb-8">
        <h1 class="text-2xl font-bold" style="color:var(--ds-tx)">Projects</h1>
        <div class="relative w-72">
          <svg class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4" style="color:var(--ds-tx2)" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/></svg>
          <input type="text" id="searchInput" placeholder="Search projects..." oninput="filterProjects()"
            class="w-full pl-10 pr-4 py-2 rounded-lg text-sm focus:outline-none"
            style="background:var(--ds-inp);border:1px solid var(--ds-bd);color:var(--ds-tx)">
        </div>
      </div>

      <!-- Project grid -->
      <div id="projectGrid" class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-5">
        <div style="color:var(--ds-tx2)" class="text-sm col-span-full">Loading...</div>
      </div>

      <!-- Empty state -->
      <div id="emptyState" class="hidden text-center py-24">
        <div class="w-16 h-16 mx-auto mb-4 rounded-2xl flex items-center justify-center" style="background:var(--ds-hv)">
          <svg class="w-8 h-8" style="color:var(--ds-tx2)" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"/></svg>
        </div>
        <h2 class="text-xl font-semibold mb-2" style="color:var(--ds-tx)">No projects yet</h2>
        <p class="mb-6" style="color:var(--ds-tx2)">Create your first project to get started.</p>
        <button onclick="openNewProject()" class="px-5 py-2.5 bg-[#c9a84c] hover:bg-[#b8973f] text-[#0a1628] font-semibold rounded-lg text-sm transition">Create Project</button>
      </div>
    </div>
{{end}}

{{define "scripts"}}
  <script>
    const statusColors = { active: 'bg-green-500/20 text-green-300', draft: 'bg-gray-500/20 text-gray-300', closed: 'bg-red-500/20 text-red-300', completed: 'bg-blue-500/20 text-blue-300' };
    const dealTypeLabels = { sell_side: 'Sell-side', buy_side: 'Buy-side', merger: 'Merger', restructuring: 'Restructuring', capital_raise: 'Capital Raise' };
    let allProjects = [];

    async function loadProjects() {
      try {
        const res = await fetchAPI('/api/projects');
        const projects = await res.json();
        allProjects = (projects || []).map(p => {
          const d = parseData(p.data_text);
          return { ...p, _d: d };
        });

        if (allProjects.length === 0) {
          document.getElementById('projectGrid').classList.add('hidden');
          document.getElementById('emptyState').classList.remove('hidden');
          return;
        }

        allProjects.sort((a, b) => (a._d.name || a.summary || '').localeCompare(b._d.name || b.summary || ''));
        renderProjects(allProjects);
      } catch(e) {
        document.getElementById('projectGrid').innerHTML = '<div class="text-red-400 text-sm col-span-full">Failed to load projects.</div>';
      }
    }

    function renderProjects(projects) {
      const grid = document.getElementById('projectGrid');
      grid.classList.remove('hidden');
      document.getElementById('emptyState').classList.add('hidden');

      const cards = projects.map(p => {
        const d = p._d;
        const status = d.status || 'active';
        const sc = statusColors[status] || statusColors.active;
        const name = d.name || p.summary || 'Untitled';
        const desc = d.description || '';
        const dealType = dealTypeLabels[d.deal_type] || '';
        const date = new Date(p.created_at).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' });

        return `<a href="/app/projects/${p.entry_id}" class="group block rounded-xl p-6 transition hover:scale-[1.01] relative"
          style="background:var(--ds-sf);border:1px solid var(--ds-bd)">
          <div class="flex items-start justify-between mb-3">
            <div class="flex items-center gap-3">
              <div class="w-10 h-10 rounded-lg flex items-center justify-center text-lg font-bold shrink-0 overflow-hidden"
                style="background:var(--ds-ac);color:var(--ds-act)">${p.seller_logo ? `<img src="${escHtml(p.seller_logo)}" class="w-full h-full object-contain" onerror="this.replaceWith(document.createTextNode('${escHtml(name[0])}'))">` : escHtml(name[0])}</div>
              <div>
                <h3 class="font-semibold text-base leading-tight group-hover:text-[#c9a84c] transition" style="color:var(--ds-tx)">${escHtml(name)}</h3>
                <div class="flex items-center gap-2 mt-0.5">
                  ${dealType ? `<span class="text-xs" style="color:var(--ds-tx2)">${dealType}</span>` : ''}
                  ${dealType ? '<span style="color:var(--ds-tx2);opacity:.3">·</span>' : ''}
                  <span class="text-xs" style="color:var(--ds-tx2)">${date}</span>
                </div>
              </div>
            </div>
            <div class="flex items-center gap-2 shrink-0">
              ${user.is_super_admin ? `<button onclick="event.preventDefault();event.stopPropagation();confirmDeleteProject('${p.entry_id}')"
                class="w-7 h-7 rounded-lg flex items-center justify-center opacity-0 group-hover:opacity-100 transition hover:bg-red-500/20 hover:text-red-400"
                style="color:var(--ds-tx3)" title="Delete project">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
              </button>` : ''}
              <span class="px-2 py-0.5 rounded-full text-xs font-medium ${sc} capitalize">${status}</span>
            </div>
          </div>
          ${desc ? `<p class="text-sm mb-4 line-clamp-2" style="color:var(--ds-tx2)">${escHtml(desc)}</p>` : '<div class="mb-4"></div>'}
          <div class="flex items-center gap-4 text-xs" style="color:var(--ds-tx2)">
            <span class="flex items-center gap-1">
              <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"/></svg>
              ${d.stage || p.stage || 'Pre-dataroom'}
            </span>
          </div>
        </a>`;
      });

      // Add "New Project" card
      if (user.is_super_admin) {
        cards.push(`<button onclick="openNewProject()" class="group block rounded-xl p-6 transition hover:scale-[1.01] text-left"
          style="background:var(--ds-sf);border:2px dashed var(--ds-bd)">
          <div class="flex flex-col items-center justify-center py-6">
            <div class="w-12 h-12 rounded-full flex items-center justify-center text-2xl mb-3 transition group-hover:scale-110"
              style="background:var(--ds-hv);color:var(--ds-ac)">+</div>
            <span class="font-medium text-sm" style="color:var(--ds-tx)">New Project</span>
            <span class="text-xs mt-1" style="color:var(--ds-tx2)">Start a new project</span>
          </div>
        </button>`);
      }

      grid.innerHTML = cards.join('');
    }

    function filterProjects() {
      const q = document.getElementById('searchInput').value.toLowerCase().trim();
      if (!q) { renderProjects(allProjects); return; }
      const filtered = allProjects.filter(p => {
        const d = p._d;
        const name = (d.name || p.summary || '').toLowerCase();
        const desc = (d.description || '').toLowerCase();
        return name.includes(q) || desc.includes(q);
      });
      renderProjects(filtered);
    }

    function openNewProject() {
      // Redirect to a special "new" route that opens the full wizard
      window.location.href = '/app/projects/new?wizard=1';
    }

    async function confirmDeleteProject(projectId) {
      const p = allProjects.find(x => x.entry_id === projectId);
      const name = p ? (p._d.name || p.summary || 'Untitled') : 'this project';
      if (!confirm('Delete "' + name + '"?\n\nThis will delete the project and all its data. This cannot be undone.')) return;
      try {
        const res = await fetchAPI('/api/projects/' + projectId, { method: 'DELETE' });
        if (!res.ok) { const d = await res.json(); throw new Error(d.error || 'Failed'); }
        allProjects = allProjects.filter(p => p.entry_id !== projectId);
        if (allProjects.length === 0) {
          document.getElementById('projectGrid').classList.add('hidden');
          document.getElementById('emptyState').classList.remove('hidden');
        } else {
          renderProjects(allProjects);
        }
      } catch(e) { alert('Failed to delete project: ' + e.message); }
    }

    loadProjects();
  </script>
{{end}}
