{{define "layout"}}
<!DOCTYPE html>
<html lang="en" data-theme="midnight">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>{{.Title}}</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=DM+Sans:wght@400;500;600;700&family=Space+Mono:wght@400;700&display=swap" rel="stylesheet">
  <script src="https://cdn.tailwindcss.com"></script>
  <link rel="stylesheet" href="/static/app.css">
  <script>document.documentElement.setAttribute("data-theme",localStorage.getItem("ds_theme")||"midnight")</script>
  {{block "page-styles" .}}{{end}}
</head>
<body>
  <!-- Header -->
  <header class="bg-[#0d1f3c] border-b border-white/[0.08] px-6 py-4 flex items-center justify-between sticky top-0 z-50">
    {{block "header-left" .}}
    <a href="/app/projects" class="text-2xl font-bold text-white tracking-tight"><span class="text-[#c9a84c]">Deal</span>space</a>
    {{end}}
    <div id="testRoleBanner" style="display:none;position:fixed;bottom:0;left:0;right:0;background:#b45309;color:#fff;text-align:center;font-size:12px;padding:3px 0;z-index:999;pointer-events:none"></div>
    <div class="flex items-center gap-4">
      {{block "header-right-extra" .}}{{end}}
      <span id="userName" class="text-sm text-[#b0bec5]"></span>
      <button onclick="logout()" class="text-sm text-[#b0bec5] hover:text-white transition">Logout</button>
    </div>
  </header>

  <div class="flex">
    <!-- Sidebar -->
    <nav class="w-60 bg-[#0d1f3c] border-r border-white/[0.08] min-h-[calc(100vh-57px)] sticky top-[57px] shrink-0 flex flex-col justify-between">
      <div class="p-3 space-y-0.5">
        <a href="/app/tasks" class="sidebar-link {{if eq .ActiveNav "tasks"}}active{{end}} flex items-center gap-3 px-3 py-2.5 rounded-lg text-base font-medium text-[#b0bec5] transition">
          <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="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 2m-6 9l2 2 4-4"/></svg>
          My Tasks</a>
        <a href="/app/projects" class="sidebar-link {{if eq .ActiveNav "projects"}}active{{end}} flex items-center gap-3 px-3 py-2.5 rounded-lg text-base font-medium text-[#b0bec5] transition">
          <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="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"/></svg>
          Projects</a>
        <a href="/app/orgs" class="sidebar-link {{if eq .ActiveNav "orgs"}}active{{end}} flex items-center gap-3 px-3 py-2.5 rounded-lg text-base font-medium text-[#b0bec5] transition">
          <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 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>
          Organizations</a>
      </div>
      <div id="adminLinks" class="p-3">
        <script>if(!(JSON.parse(localStorage.getItem('ds_user')||'{}').is_super_admin))document.currentScript.parentElement.style.display='none';</script>
        <div class="border-t border-white/[0.08] mb-3"></div>
        <div class="px-3 pb-3">
          <label class="block text-xs font-medium mb-1.5" style="color:var(--ds-tx3)">Test as role</label>
          <select id="testRoleSelect" onchange="setTestRole(this.value)"
            class="w-full px-2 py-1.5 rounded text-xs focus:outline-none"
            style="background:var(--ds-bg);border:1px solid var(--ds-bd);color:var(--ds-tx2)">
            <option value="">— self (super admin) —</option>
            <option value="ib">IB Advisor</option>
            <option value="buyer">Buyer</option>
            <option value="seller">Seller</option>
            <option value="advisor">Advisor</option>
          </select>
        </div>
        <a href="/admin" class="sidebar-link {{if eq .ActiveNav "admin"}}active{{end}} flex items-center gap-3 px-3 py-2.5 rounded-lg text-base font-medium text-[#b0bec5] transition">
          <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="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.066 2.573c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.573 1.066c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.066-2.573c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
          Admin</a>
      </div>
    </nav>

    <!-- Page content -->
    <main class="flex-1">
      {{template "content" .}}
    </main>
  </div>

  <!-- Theme bar -->
  <div id="ds-theme-bar">
    <button data-t="steel" onclick="setTheme('steel')">Steel</button>
    <button data-t="brutalist" onclick="setTheme('brutalist')">Brutalist</button>
    <button data-t="midnight" onclick="setTheme('midnight')">Midnight</button>
    <button data-t="light" onclick="setTheme('light')">Light</button>
    <button data-t="slate" onclick="setTheme('slate')">Slate</button>
    <button data-t="compact" onclick="setTheme('compact')">Compact</button>
    <button data-t="executive" onclick="setTheme('executive')">Executive</button>
  </div>

  <script>
    // Auth check
    const token = localStorage.getItem('ds_token');
    if (!token) window.location.href = '/app/login';
    const user = JSON.parse(localStorage.getItem('ds_user') || '{}');
    document.getElementById('userName').textContent = user.name || user.email || '';
    if (!user.is_super_admin) document.getElementById('adminLinks').style.display = 'none';

    function fetchAPI(path, opts = {}) {
      opts.headers = { ...opts.headers, 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' };
      return fetch(path, opts).then(r => { if (r.status === 401) { localStorage.removeItem('ds_token'); window.location.href = '/app/login'; } return r; });
    }
    function logout() { fetchAPI('/api/auth/logout', { method: 'POST' }).finally(() => { localStorage.clear(); window.location.href = '/app/login'; }); }
    function parseData(t) { try { return JSON.parse(t); } catch { return {}; } }
    function escHtml(s) { if (!s) return ''; const d = document.createElement('div'); d.textContent = s; return d.innerHTML; }

    // Theme switcher
    async function setTestRole(role) {
      const sel = document.getElementById('testRoleSelect');
      sel.disabled = true;
      try {
        const res = await fetchAPI('/api/admin/test-role', { method: 'PUT', body: JSON.stringify({ role }) });
        if (!res.ok) { const e = await res.json(); alert(e.error || 'Failed'); sel.value = localStorage.getItem('ds_test_role')||''; return; }
        localStorage.setItem('ds_test_role', role);
        const banner = document.getElementById('testRoleBanner');
        if (banner) { banner.textContent = role ? '⚠ Test mode: viewing as ' + role.toUpperCase() : ''; banner.style.display = role ? 'block' : 'none'; }
        // Reload to reflect new role
        window.location.reload();
      } catch(e) { alert('Error: ' + e.message); }
      finally { sel.disabled = false; }
    }
    // Restore test role banner on load (role is server-side now)
    (function(){
      const r = localStorage.getItem('ds_test_role') || '';
      const sel = document.getElementById('testRoleSelect');
      if (sel) sel.value = r;
      const banner = document.getElementById('testRoleBanner');
      if (banner && r) { banner.textContent = '⚠ Test mode: viewing as ' + r.toUpperCase(); banner.style.display = 'block'; }
    })();

    function setTheme(t){document.documentElement.setAttribute('data-theme',t);localStorage.setItem('ds_theme',t);document.querySelectorAll('#ds-theme-bar button').forEach(b=>b.classList.toggle('active',b.getAttribute('data-t')===t))}
    setTheme(localStorage.getItem('ds_theme')||'midnight');
  </script>
  {{block "scripts" .}}{{end}}
</body>
</html>
{{end}}
