<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>{% block title %}ABREKA{% endblock %}</title>
  <style>
    :root {
      --bg: #f4f5f7;
      --surface: #ffffff;
      --text: #1a1a2e;
      --muted: #6b7280;
      --border: #e2e5ea;
      --primary: #4f46e5;
      --primary-light: #eef2ff;
      --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
      --mono: "SF Mono", "Fira Code", "Fira Mono", Menlo, Consolas, monospace;
    }
    *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
    body {
      font-family: var(--font);
      background: var(--bg);
      color: var(--text);
      line-height: 1.6;
      font-size: 14px;
    }
    a { color: var(--primary); text-decoration: none; }
    a:hover { text-decoration: underline; }

    /* Nav */
    nav {
      background: var(--text);
      color: #fff;
      padding: 0 24px;
      display: flex;
      align-items: center;
      height: 48px;
      gap: 24px;
      position: sticky;
      top: 0;
      z-index: 100;
    }
    nav .brand {
      font-weight: 700;
      font-size: 16px;
      letter-spacing: 0.5px;
      color: #fff;
    }
    nav a { color: #a5b4c8; font-size: 13px; }
    nav a:hover, nav a.active { color: #fff; text-decoration: none; }

    /* Layout */
    .container { max-width: 1200px; margin: 0 auto; padding: 24px; }
    .breadcrumb { font-size: 13px; color: var(--muted); margin-bottom: 16px; }
    .breadcrumb a { color: var(--muted); }
    .breadcrumb a:hover { color: var(--primary); }

    /* Cards */
    .card {
      background: var(--surface);
      border: 1px solid var(--border);
      border-radius: 8px;
      padding: 20px;
      margin-bottom: 16px;
    }
    .card h2 { font-size: 16px; margin-bottom: 12px; }
    .card h3 { font-size: 14px; margin-bottom: 8px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }

    /* Status badge */
    .badge {
      display: inline-block;
      padding: 2px 10px;
      border-radius: 12px;
      font-size: 12px;
      font-weight: 600;
      color: #fff;
    }

    /* Tables */
    table { width: 100%; border-collapse: collapse; font-size: 13px; }
    th { text-align: left; padding: 8px 12px; border-bottom: 2px solid var(--border); color: var(--muted); font-weight: 600; text-transform: uppercase; font-size: 11px; letter-spacing: 0.5px; }
    td { padding: 8px 12px; border-bottom: 1px solid var(--border); }
    tr:hover td { background: var(--primary-light); }

    /* Grid */
    .grid { display: grid; gap: 16px; }
    .grid-2 { grid-template-columns: 1fr 1fr; }
    .grid-3 { grid-template-columns: 1fr 1fr 1fr; }
    .grid-4 { grid-template-columns: 1fr 1fr 1fr 1fr; }
    @media (max-width: 768px) { .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; } }

    /* Stat boxes */
    .stat { text-align: center; }
    .stat .value { font-size: 32px; font-weight: 700; }
    .stat .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }

    /* Code / pre */
    pre {
      background: #f8f9fa;
      border: 1px solid var(--border);
      border-radius: 6px;
      padding: 12px;
      overflow-x: auto;
      font-family: var(--mono);
      font-size: 12px;
      line-height: 1.5;
      white-space: pre-wrap;
      word-break: break-word;
    }
    code {
      font-family: var(--mono);
      font-size: 12px;
      background: #f0f1f3;
      padding: 1px 5px;
      border-radius: 3px;
    }
    pre code { background: none; padding: 0; }

    /* Details / collapsible */
    details { margin: 4px 0; }
    details summary {
      cursor: pointer;
      font-size: 13px;
      color: var(--muted);
      padding: 4px 0;
      user-select: none;
    }
    details summary:hover { color: var(--text); }
    details[open] summary { margin-bottom: 8px; }

    /* Markdown content */
    .md-content h1 { font-size: 20px; margin: 16px 0 8px; }
    .md-content h2 { font-size: 17px; margin: 14px 0 6px; }
    .md-content h3 { font-size: 15px; margin: 12px 0 4px; }
    .md-content p { margin: 8px 0; }
    .md-content ul, .md-content ol { margin: 8px 0; padding-left: 24px; }
    .md-content li { margin: 2px 0; }
    .md-content table { margin: 8px 0; }
    .md-content pre { margin: 8px 0; }

    /* Utility */
    .text-muted { color: var(--muted); }
    .text-sm { font-size: 12px; }
    .text-mono { font-family: var(--mono); }
    .mt-1 { margin-top: 8px; }
    .mt-2 { margin-top: 16px; }
    .mb-1 { margin-bottom: 8px; }
    .mb-2 { margin-bottom: 16px; }
    .flex { display: flex; }
    .flex-wrap { flex-wrap: wrap; }
    .gap-1 { gap: 8px; }
    .gap-2 { gap: 16px; }
    .items-center { align-items: center; }

    /* Editable cards */
    .editable-card { position: relative; }
    .editable-card .file-path {
      font-family: var(--mono);
      font-size: 11px;
      color: #9ca3af;
      margin-bottom: 6px;
    }
    .editable-card .view-mode { cursor: default; }
    .editable-card .edit-mode { display: none; }
    .editable-card.editing .view-mode { display: none; }
    .editable-card.editing .edit-mode { display: block; }
    .editable-card .edit-area {
      width: 100%;
      min-height: 200px;
      font-family: var(--mono);
      font-size: 13px;
      line-height: 1.6;
      padding: 12px;
      border: 1px solid var(--border);
      border-radius: 6px;
      background: #fafbfc;
      resize: vertical;
      color: var(--text);
    }
    .editable-card .edit-area:focus {
      outline: none;
      border-color: var(--primary);
      box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.15);
    }
    .editable-card .edit-actions {
      display: flex;
      gap: 8px;
      margin-top: 8px;
    }
    .btn {
      padding: 5px 14px;
      border-radius: 5px;
      font-size: 13px;
      cursor: pointer;
      border: 1px solid var(--border);
      background: var(--surface);
      color: var(--text);
    }
    .btn:hover { background: var(--primary-light); }
    .btn-primary {
      background: var(--primary);
      color: #fff;
      border-color: var(--primary);
    }
    .btn-primary:hover { background: #4338ca; }

    {% block extra_css %}{% endblock %}
  </style>
</head>
<body>
  <nav>
    <span class="brand">ABREKA</span>
    <a href="/">Dashboard</a>
    <a href="/experiments">Experiments</a>
    <a href="/search">Search</a>
    <a href="/backlog">Backlog</a>
    <a href="/wiki">Wiki</a>
    <a href="/tree">Tree</a>
    <a href="/config">Config</a>
    <a href="/guide">User Guide</a>
  </nav>
  <div class="container">
    {% block breadcrumb %}{% endblock %}
    {% block content %}{% endblock %}
  </div>
  <script>
  document.querySelectorAll('.editable-card').forEach(card => {
    const viewEl = card.querySelector('.view-mode');
    const editEl = card.querySelector('.edit-mode');
    const textarea = card.querySelector('.edit-area');
    const endpoint = card.dataset.endpoint;
    const rawEl = card.querySelector('.raw-content');

    viewEl.addEventListener('dblclick', () => {
      textarea.value = rawEl.textContent;
      card.classList.add('editing');
      textarea.focus();
    });

    card.querySelector('.btn-save').addEventListener('click', async () => {
      const btn = card.querySelector('.btn-save');
      btn.textContent = 'Saving...';
      btn.disabled = true;
      try {
        const res = await fetch(endpoint, {
          method: 'PUT',
          headers: {'Content-Type': 'application/json'},
          body: JSON.stringify({content: textarea.value}),
        });
        if (!res.ok) throw new Error('Save failed');
        location.reload();
      } catch (e) {
        alert('Failed to save: ' + e.message);
        btn.textContent = 'Save';
        btn.disabled = false;
      }
    });

    card.querySelector('.btn-discard').addEventListener('click', () => {
      card.classList.remove('editing');
    });
  });
  </script>
</body>
</html>
