<!-- Vista Privada: Lectura de página de documentación (requiere login + rol) -->
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@11.9.0/styles/github.min.css">
<script src="https://cdn.jsdelivr.net/npm/highlight.js@11.9.0/lib/highlight.min.js"></script>

<div class="bg-white rounded-lg shadow">
    <!-- Topbar de navegación -->
    <div class="border-b px-6 py-3 flex items-center gap-2 text-sm">
        <a href="/app/docs/{{.saas.Slug}}" class="flex items-center gap-1 text-gray-500 hover:text-[#8eb02f] transition-colors">
            <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                <path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7"/>
            </svg>
            {{.saas.Nombre}}
        </a>
        <span class="text-gray-300">/</span>
        <span class="text-gray-400">{{.pagina.Categoria.Nombre}}</span>
        <span class="text-gray-300">/</span>
        <span class="text-gray-800 font-medium truncate">{{.pagina.Titulo}}</span>
        {{if eq .pagina.Visibilidad "private"}}
        <span class="ml-auto text-xs text-yellow-600 bg-yellow-50 border border-yellow-200 px-2 py-0.5 rounded-full">🔒 Acceso privado</span>
        {{end}}
    </div>

    <div class="flex gap-0">
        <!-- Sidebar de navegación -->
        <aside class="hidden lg:block w-52 border-r flex-shrink-0 py-4 px-3">
            <div class="sticky top-4 space-y-4">
                {{range .grupos}}
                <div>
                    <p class="text-xs font-semibold text-gray-400 uppercase tracking-wide mb-2 px-2">{{.Categoria.Nombre}}</p>
                    <ul class="space-y-0.5">
                        {{range .Paginas}}
                        <li>
                            <a href="/app/docs/{{$.saas.Slug}}/{{.Slug}}"
                               class="flex items-center justify-between text-xs px-2 py-1.5 rounded transition-colors
                               {{if eq .Slug $.pagina.Slug}}bg-[#8eb02f] text-white font-medium{{else}}text-gray-600 hover:bg-gray-100{{end}}">
                               <span>{{.Titulo}}</span>
                               {{if eq .Visibilidad "private"}}<span class="opacity-60 text-xs">🔒</span>{{end}}
                            </a>
                        </li>
                        {{end}}
                    </ul>
                </div>
                {{end}}
            </div>
        </aside>

        <!-- Contenido -->
        <main class="flex-1 min-w-0 p-6 lg:p-8">
            <div class="mb-6 pb-4 border-b border-gray-100">
                <span class="text-xs text-gray-400 bg-gray-100 px-2 py-0.5 rounded">{{.pagina.Categoria.Nombre}}</span>
                <h1 class="text-2xl font-bold text-gray-900 mt-2">{{.pagina.Titulo}}</h1>
            </div>

            {{if eq .pagina.TipoContenido "markdown"}}
            <div id="docContent" class="prose prose-gray max-w-none"
                 data-raw="{{.pagina.Contenido}}" data-type="markdown"></div>
            {{else}}
            <div class="prose prose-gray max-w-none">{{.pagina.Contenido}}</div>
            {{end}}
        </main>
    </div>
</div>

<style>
.prose h1,.prose h2,.prose h3,.prose h4 { font-weight:700; margin-top:1.5em; margin-bottom:0.5em; color:#1f2937; }
.prose h1 { font-size:1.75rem; }
.prose h2 { font-size:1.35rem; border-bottom:1px solid #e5e7eb; padding-bottom:.25rem; }
.prose h3 { font-size:1.1rem; }
.prose p  { margin-bottom:1em; line-height:1.7; color:#374151; }
.prose ul,.prose ol { margin:0.75em 0 0.75em 1.5em; color:#374151; }
.prose li  { margin-bottom:.25em; }
.prose code { background:#f3f4f6; padding:2px 5px; border-radius:4px; font-size:.85em; font-family:monospace; }
.prose pre  { background:#1e293b; color:#e2e8f0; padding:1rem; border-radius:.5rem; overflow-x:auto; margin:1em 0; }
.prose pre code { background:transparent; padding:0; font-size:.85em; }
.prose blockquote { border-left:4px solid #8eb02f; padding-left:1rem; color:#6b7280; font-style:italic; margin:1em 0; }
.prose a   { color:#8eb02f; text-decoration:underline; }
.prose table { width:100%; border-collapse:collapse; margin:1em 0; }
.prose th,.prose td { border:1px solid #e5e7eb; padding:.5rem .75rem; text-align:left; }
.prose th  { background:#f9fafb; font-weight:600; }
</style>

<script>
document.addEventListener('DOMContentLoaded', function () {
    const el = document.getElementById('docContent');
    if (!el) return;
    const raw = el.getAttribute('data-raw') || '';
    el.removeAttribute('data-raw');
    el.innerHTML = marked.parse(raw);
    if (window.hljs) {
        el.querySelectorAll('pre code').forEach(block => hljs.highlightElement(block));
    }
});
</script>
