<!DOCTYPE html>
<html lang="en" data-theme="dark">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>{{ title }}</title>
  <!-- Apply theme before paint to prevent flash -->
  <script>
    (function() {
      const t = localStorage.getItem('dc-theme') || 'system';
      document.documentElement.setAttribute('data-theme', t);
    })();
  </script>
  <style>
    /* ── Reset & Base ─────────────────────────────────────────────── */
    *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

    /* ── Dark theme (default) ────────────────────────────────────── */
    :root {
      --bg:         #09090b;
      --surface:    #111113;
      --surface2:   #1a1a1d;
      --border:     rgba(255,255,255,0.07);
      --border2:    rgba(255,255,255,0.12);
      --text:       #e2e8f0;
      --text-muted: #64748b;
      --text-dim:   #94a3b8;
      --accent:     #f59e0b;
      --accent2:    #3b82f6;
      --green:      #22c55e;
      --red:        #ef4444;
      --purple:     #8b5cf6;
      --font-mono:  'SF Mono', 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
      --font-sans:  -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
      --radius:     8px;
      --radius-lg:  12px;
      --sidebar-w:  220px;
      --hover-bg:   rgba(255,255,255,0.04);
      --hover-td:   rgba(255,255,255,0.02);
    }

    /* ── Light theme ─────────────────────────────────────────────── */
    :root[data-theme="light"] {
      --bg:         #f8f8f9;
      --surface:    #ffffff;
      --surface2:   #f1f1f3;
      --border:     rgba(0,0,0,0.08);
      --border2:    rgba(0,0,0,0.14);
      --text:       #0f0f10;
      --text-muted: #71717a;
      --text-dim:   #52525b;
      --accent:     #d97706;
      --accent2:    #2563eb;
      --green:      #16a34a;
      --red:        #dc2626;
      --purple:     #7c3aed;
      --hover-bg:   rgba(0,0,0,0.03);
      --hover-td:   rgba(0,0,0,0.015);
    }

    /* ── System theme: follow OS ─────────────────────────────────── */
    @media (prefers-color-scheme: light) {
      :root[data-theme="system"] {
        --bg:         #f8f8f9;
        --surface:    #ffffff;
        --surface2:   #f1f1f3;
        --border:     rgba(0,0,0,0.08);
        --border2:    rgba(0,0,0,0.14);
        --text:       #0f0f10;
        --text-muted: #71717a;
        --text-dim:   #52525b;
        --accent:     #d97706;
        --accent2:    #2563eb;
        --green:      #16a34a;
        --red:        #dc2626;
        --purple:     #7c3aed;
        --hover-bg:   rgba(0,0,0,0.03);
        --hover-td:   rgba(0,0,0,0.015);
      }
    }

    html, body { height: 100%; }

    body {
      font-family: var(--font-sans);
      background: var(--bg);
      color: var(--text);
      display: flex;
      font-size: 14px;
      line-height: 1.5;
      transition: background 0.2s, color 0.2s;
    }

    /* ── Sidebar ─────────────────────────────────────────────────── */
    .sidebar {
      width: var(--sidebar-w);
      flex-shrink: 0;
      background: var(--surface);
      border-right: 1px solid var(--border);
      display: flex;
      flex-direction: column;
      padding: 20px 0;
      height: 100vh;
      position: sticky;
      top: 0;
    }

    .sidebar-logo {
      padding: 0 20px 20px;
      border-bottom: 1px solid var(--border);
      margin-bottom: 12px;
    }

    .sidebar-logo .duck { font-size: 22px; line-height: 1; }

    .sidebar-logo .name {
      font-size: 16px;
      font-weight: 700;
      color: var(--accent);
      font-family: var(--font-mono);
      letter-spacing: -0.5px;
    }

    .sidebar-logo .tagline {
      font-size: 10px;
      color: var(--text-muted);
      margin-top: 2px;
    }

    .nav-item {
      display: flex;
      align-items: center;
      gap: 10px;
      padding: 9px 20px;
      color: var(--text-dim);
      text-decoration: none;
      font-size: 13px;
      font-weight: 500;
      border-left: 2px solid transparent;
      transition: all 0.15s;
    }
    .nav-item:hover { background: var(--hover-bg); color: var(--text); }
    .nav-item.active {
      color: var(--accent);
      border-left-color: var(--accent);
      background: rgba(245,158,11,0.06);
    }
    .nav-item .icon { font-size: 15px; width: 18px; }

    /* ── Sidebar bottom: version + theme toggle ───────────────────── */
    .sidebar-bottom {
      margin-top: auto;
      padding: 14px 20px;
      border-top: 1px solid var(--border);
      display: flex;
      flex-direction: column;
      gap: 10px;
    }

    .version-badge {
      font-size: 10px;
      font-family: var(--font-mono);
      color: var(--text-muted);
      background: var(--surface2);
      border: 1px solid var(--border);
      padding: 3px 8px;
      border-radius: 4px;
      width: fit-content;
    }

    /* ── Theme switcher ──────────────────────────────────────────── */
    .theme-switcher {
      display: flex;
      gap: 3px;
      background: var(--surface2);
      border: 1px solid var(--border);
      border-radius: 6px;
      padding: 3px;
    }
    .theme-btn {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 4px 0;
      border-radius: 4px;
      border: none;
      background: transparent;
      cursor: pointer;
      font-size: 13px;
      color: var(--text-muted);
      transition: all 0.15s;
      position: relative;
    }
    .theme-btn:hover { color: var(--text); }
    .theme-btn.active {
      background: var(--surface);
      color: var(--accent);
      box-shadow: 0 1px 3px rgba(0,0,0,0.15);
    }
    .theme-label {
      font-size: 9px;
      font-weight: 600;
      color: var(--text-muted);
      text-transform: uppercase;
      letter-spacing: 0.5px;
    }

    /* ── Main Content ────────────────────────────────────────────── */
    .main {
      flex: 1;
      display: flex;
      flex-direction: column;
      min-height: 100vh;
      overflow: hidden;
    }

    .page-header {
      padding: 16px 24px;
      border-bottom: 1px solid var(--border);
      display: flex;
      align-items: center;
      justify-content: space-between;
      background: var(--surface);
    }

    .page-title { font-size: 15px; font-weight: 600; color: var(--text); }

    .page-content { flex: 1; overflow-y: auto; padding: 24px; }

    /* ── Common Components ───────────────────────────────────────── */
    .card {
      background: var(--surface);
      border: 1px solid var(--border);
      border-radius: var(--radius-lg);
      padding: 20px;
    }

    .badge {
      display: inline-flex;
      align-items: center;
      padding: 2px 8px;
      border-radius: 4px;
      font-size: 10px;
      font-weight: 700;
      letter-spacing: 0.5px;
      text-transform: uppercase;
      font-family: var(--font-mono);
    }

    .badge-safe    { background: rgba(34,197,94,0.12);   color: #22c55e; }
    .badge-notify  { background: rgba(59,130,246,0.12);  color: #3b82f6; }
    .badge-ask     { background: rgba(245,158,11,0.12);  color: #f59e0b; }
    .badge-block   { background: rgba(239,68,68,0.12);   color: #ef4444; }
    .badge-approved{ background: rgba(34,197,94,0.12);   color: #22c55e; }
    .badge-denied  { background: rgba(239,68,68,0.12);   color: #ef4444; }
    .badge-blocked { background: rgba(239,68,68,0.15);   color: #ef4444; }
    .badge-notified{ background: rgba(59,130,246,0.12);  color: #3b82f6; }
    .badge-auto    { background: rgba(100,116,139,0.12); color: #94a3b8; }

    .btn {
      display: inline-flex;
      align-items: center;
      gap: 6px;
      padding: 7px 14px;
      border-radius: var(--radius);
      font-size: 12px;
      font-weight: 600;
      cursor: pointer;
      border: none;
      transition: all 0.15s;
      font-family: inherit;
    }
    .btn-primary { background: var(--accent); color: #09090b; }
    .btn-primary:hover { filter: brightness(1.1); }
    .btn-ghost {
      background: transparent;
      color: var(--text-dim);
      border: 1px solid var(--border2);
    }
    .btn-ghost:hover { background: var(--hover-bg); color: var(--text); }
    .btn-danger {
      background: rgba(239,68,68,0.12);
      color: var(--red);
      border: 1px solid rgba(239,68,68,0.2);
    }
    .btn-danger:hover { background: rgba(239,68,68,0.2); }

    .stat-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
      gap: 12px;
      margin-bottom: 24px;
    }
    .stat-card {
      background: var(--surface2);
      border: 1px solid var(--border);
      border-radius: var(--radius);
      padding: 16px;
      text-align: center;
    }
    .stat-value { font-size: 24px; font-weight: 800; font-family: var(--font-mono); line-height: 1; }
    .stat-label { font-size: 10px; color: var(--text-muted); margin-top: 6px; text-transform: uppercase; letter-spacing: 0.5px; }

    table { width: 100%; border-collapse: collapse; font-size: 12px; }
    th {
      text-align: left;
      padding: 8px 12px;
      font-size: 10px;
      font-weight: 700;
      letter-spacing: 1px;
      text-transform: uppercase;
      color: var(--text-muted);
      border-bottom: 1px solid var(--border);
    }
    td {
      padding: 10px 12px;
      border-bottom: 1px solid var(--border);
      color: var(--text-dim);
      vertical-align: middle;
    }
    tr:last-child td { border-bottom: none; }
    tr:hover td { background: var(--hover-td); }

    .mono       { font-family: var(--font-mono); }
    .text-muted { color: var(--text-muted); }
    .text-dim   { color: var(--text-dim); }
    .text-green { color: var(--green); }
    .text-red   { color: var(--red); }
    .text-accent{ color: var(--accent); }

    /* ── Approval Modal ───────────────────────────────────────────── */
    .modal-overlay {
      position: fixed; inset: 0;
      background: rgba(0,0,0,0.6);
      backdrop-filter: blur(4px);
      display: none; align-items: center; justify-content: center;
      z-index: 1000;
    }
    .modal-overlay.show { display: flex; }
    .modal {
      background: var(--surface);
      border: 1px solid var(--border2);
      border-radius: var(--radius-lg);
      padding: 28px;
      max-width: 480px;
      width: 90%;
    }
    .modal-title  { font-size: 16px; font-weight: 700; margin-bottom: 4px; }
    .modal-desc   { color: var(--text-dim); font-size: 13px; margin-bottom: 16px; }
    .modal-details{
      background: var(--surface2); border: 1px solid var(--border);
      border-radius: var(--radius); padding: 12px;
      font-family: var(--font-mono); font-size: 11px; color: var(--text-dim); margin-bottom: 16px;
    }
    .modal-meta   { display: flex; gap: 16px; font-size: 11px; color: var(--text-muted); margin-bottom: 20px; }
    .modal-actions{ display: flex; gap: 10px; justify-content: flex-end; }

    @media (max-width: 640px) {
      .sidebar { display: none; }
      .stat-grid { grid-template-columns: repeat(2, 1fr); }
    }
  </style>
  {% block head %}{% endblock %}
</head>
<body>
  <!-- Sidebar -->
  <nav class="sidebar">
    <div class="sidebar-logo">
      <div class="duck">🦆🤖</div>
      <div class="name">DuckClaw</div>
      <div class="tagline">Secure · Local · Yours</div>
    </div>

    <a href="/" class="nav-item {% if page == 'chat' %}active{% endif %}">
      <span class="icon">💬</span> Chat
    </a>
    <a href="/memory" class="nav-item {% if page == 'memory' %}active{% endif %}">
      <span class="icon">🧠</span> Memory
    </a>
    <a href="/audit" class="nav-item {% if page == 'audit' %}active{% endif %}">
      <span class="icon">📋</span> Audit Log
    </a>
    <a href="/database" class="nav-item {% if page == 'database' %}active{% endif %}">
      <span class="icon">🗄️</span> Database
    </a>
    <a href="/logs" class="nav-item {% if page == 'logs' %}active{% endif %}">
      <span class="icon">🪵</span> Logs
    </a>
    <a href="/settings" class="nav-item {% if page == 'settings' %}active{% endif %}">
      <span class="icon">⚙️</span> Settings
    </a>

    <div class="sidebar-bottom">
      <span class="version-badge">v0.1.1</span>

      <!-- Theme switcher -->
      <div>
        <div class="theme-label" style="margin-bottom:6px;">Theme</div>
        <div class="theme-switcher" role="group" aria-label="Theme">
          <button class="theme-btn" data-theme="light"  title="Light"  onclick="setTheme('light')">☀️</button>
          <button class="theme-btn" data-theme="system" title="System" onclick="setTheme('system')">💻</button>
          <button class="theme-btn" data-theme="dark"   title="Dark"   onclick="setTheme('dark')">🌙</button>
        </div>
      </div>
    </div>
  </nav>

  <!-- Main -->
  <div class="main">
    {% block content %}{% endblock %}
  </div>

  <!-- Approval Modal -->
  <div class="modal-overlay" id="approvalModal">
    <div class="modal">
      <div class="modal-title" id="modalTitle">⚠️ Permission Required</div>
      <div class="modal-desc" id="modalDesc"></div>
      <div class="modal-details" id="modalDetails"></div>
      <div class="modal-meta" id="modalMeta"></div>
      <div class="modal-actions">
        <button class="btn btn-danger" onclick="handleApproval(false)">✗ Deny</button>
        <button class="btn btn-primary" onclick="handleApproval(true)">✓ Approve</button>
      </div>
    </div>
  </div>

  <script>
  // ── Theme management ────────────────────────────────────────────
  function setTheme(t) {
    document.documentElement.setAttribute('data-theme', t);
    localStorage.setItem('dc-theme', t);
    updateThemeBtns(t);
  }

  function updateThemeBtns(t) {
    document.querySelectorAll('.theme-btn').forEach(btn => {
      btn.classList.toggle('active', btn.dataset.theme === t);
    });
  }

  // Sync button state on load
  updateThemeBtns(localStorage.getItem('dc-theme') || 'system');
  </script>

  {% block scripts %}{% endblock %}
</body>
</html>
