:root {
    --bg-dark: #070a10;
    --bg-body: #070a10;
    --bg-surface: #0d131f;
    --bg-card: #111827;
    --bg-card-hover: #162032;
    --emerald: #10b981;
    --emerald-light: #34d399;
    --emerald-dark: #059669;
    --cyan: #06b6d4;
    --cyan-light: #22d3ee;
    --primary: #10b981;
    --primary-hover: #34d399;
    --accent: #f59e0b;
    --text-main: #f8fafc;
    --text-secondary: #cbd5e1;
    --text-muted: #94a3b8;
    --border-color: #1e293b;
    --border-light: rgba(148, 163, 184, 0.14);
}

* {
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background-color: var(--bg-dark);
    background-image:
        radial-gradient(circle at 15% 10%, rgba(16, 185, 129, 0.07), transparent 35%),
        radial-gradient(circle at 85% 15%, rgba(6, 182, 212, 0.07), transparent 35%),
        linear-gradient(to bottom, rgba(17, 24, 39, 0.3) 1px, transparent 1px),
        linear-gradient(to right, rgba(17, 24, 39, 0.3) 1px, transparent 1px);
    background-size: 100% 100%, 100% 100%, 48px 48px, 48px 48px;
    color: var(--text-main);
    margin: 0;
    padding: 0;
    line-height: 1.65;
    font-size: 14.5px;
}

/* Navbar del Visor */
.viewer-nav {
    padding: 14px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-light);
    background-color: rgba(7, 10, 16, 0.85);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    position: sticky;
    top: 0;
    z-index: 100;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    text-decoration: none !important;
    color: var(--text-main);
}

.brand-logo {
    width: 38px;
    height: 38px;
    border-radius: 9px;
    box-shadow: 0 0 18px rgba(16, 185, 129, 0.22);
    transition: transform 0.2s ease;
}

.brand:hover .brand-logo {
    transform: scale(1.05);
}

.brand-text {
    display: flex;
    flex-direction: column;
    line-height: 1.1;
}

.brand-title {
    font-size: 1.15rem;
    font-weight: 800;
    letter-spacing: -0.03em;
    color: var(--text-main);
    display: flex;
    align-items: center;
    gap: 8px;
}

.brand-tag {
    background: linear-gradient(135deg, rgba(16, 185, 129, 0.18), rgba(6, 182, 212, 0.18));
    border: 1px solid rgba(16, 185, 129, 0.35);
    color: var(--emerald-light);
    padding: 2px 7px;
    border-radius: 5px;
    font-size: 0.65rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.brand-subtitle {
    font-size: 0.7rem;
    color: var(--text-muted);
    font-weight: 500;
    margin-top: 2px;
}

.nav-links {
    display: flex;
    align-items: center;
    gap: 22px;
}

.nav-links a {
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.88rem;
    font-weight: 600;
    transition: color 0.2s ease;
}

.nav-links a:hover {
    color: var(--emerald-light);
    text-decoration: none;
}

.nav-links a.nav-back {
    color: var(--emerald-light);
    background: rgba(16, 185, 129, 0.1);
    border: 1px solid rgba(16, 185, 129, 0.28);
    padding: 6px 12px;
    border-radius: 7px;
}

.nav-links a.nav-back:hover {
    background: rgba(16, 185, 129, 0.18);
    color: #fff;
}

/* Contenedor Principal */
.viewer-main {
    padding: 32px 24px 40px;
}

.doc-container {
    max-width: 980px;
    margin: 0 auto;
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: 36px 46px;
    box-shadow: 0 12px 36px rgba(0, 0, 0, 0.55);
    position: relative;
    overflow: hidden;
}

.doc-container::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 3px;
    background: linear-gradient(90deg, var(--emerald), var(--cyan));
}

.doc-header-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 22px;
    padding-bottom: 14px;
    border-bottom: 1px solid var(--border-color);
}

.doc-header-left {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
}

.doc-header-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.doc-header-badge {
    display: inline-block;
    background-color: rgba(6, 182, 212, 0.12);
    color: var(--cyan-light);
    border: 1px solid rgba(6, 182, 212, 0.3);
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.02em;
}

.btn-back-home {
    display: inline-flex;
    align-items: center;
    color: var(--text-muted);
    text-decoration: none !important;
    font-size: 13px;
    font-weight: 600;
    padding: 5px 11px;
    border-radius: 6px;
    border: 1px solid var(--border-color);
    background-color: rgba(255, 255, 255, 0.02);
    transition: all 0.2s;
}

.btn-back-home:hover {
    color: var(--emerald-light);
    background-color: rgba(16, 185, 129, 0.08);
    border-color: rgba(16, 185, 129, 0.35);
}

.btn-download-pdf {
    background: linear-gradient(135deg, var(--emerald) 0%, var(--cyan) 100%);
    color: #04130e !important;
    text-decoration: none !important;
    padding: 7px 15px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    box-shadow: 0 4px 14px rgba(16, 185, 129, 0.25);
    transition: all 0.2s ease;
}

.btn-download-pdf:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 20px rgba(6, 182, 212, 0.35);
    filter: brightness(1.05);
}

/* Pestañas (Dossier FIMCA y documentos multi-capítulo) */
.tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 24px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 14px;
    flex-wrap: wrap;
}

.tab-btn {
    background: var(--bg-surface);
    color: var(--text-muted);
    border: 1px solid var(--border-color);
    padding: 9px 16px;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 700;
    font-size: 13px;
    font-family: 'Inter', sans-serif;
    transition: all 0.2s;
    flex: 1 1 auto;
    text-align: center;
}

.tab-btn:hover {
    background: var(--bg-card-hover);
    color: var(--text-main);
    border-color: rgba(16, 185, 129, 0.35);
}

.tab-btn.active {
    background: linear-gradient(135deg, rgba(16, 185, 129, 0.2), rgba(6, 182, 212, 0.2));
    color: var(--emerald-light);
    border-color: var(--emerald);
    box-shadow: 0 0 15px rgba(16, 185, 129, 0.15);
}

/* Tipografía de Contenidos Markdown */
h1,
h2,
h3,
h4 {
    color: #f8fafc;
    margin-top: 1.5em;
    margin-bottom: 0.6em;
    line-height: 1.3;
    letter-spacing: -0.01em;
}

h1 {
    font-size: 26px;
    font-weight: 800;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 12px;
    color: var(--emerald-light);
    margin-top: 0;
}

h2 {
    font-size: 20px;
    font-weight: 700;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    padding-bottom: 6px;
    color: var(--cyan-light);
}

h3 {
    font-size: 16.5px;
    font-weight: 700;
    color: #e2e8f0;
}

p,
ul,
ol {
    margin-top: 0.6em;
    margin-bottom: 0.85em;
    color: var(--text-secondary);
}

li {
    margin-bottom: 0.35em;
}

strong {
    color: #f8fafc;
}

/* ÍNDICE DE CONTENIDOS (PANEL LATERAL) */
.toc-toggle-btn {
    position: fixed;
    bottom: 24px;
    right: 24px;
    background: linear-gradient(135deg, var(--emerald) 0%, var(--cyan) 100%);
    color: #04130e;
    border: none;
    padding: 12px 20px;
    border-radius: 30px;
    font-weight: 800;
    font-size: 13.5px;
    font-family: 'Inter', sans-serif;
    cursor: pointer;
    box-shadow: 0 6px 20px rgba(16, 185, 129, 0.35);
    z-index: 1000;
    transition: transform 0.2s, box-shadow 0.2s;
}

.toc-toggle-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(6, 182, 212, 0.45);
}

.toc-sidebar {
    position: fixed;
    top: 0;
    right: -340px;
    width: 340px;
    height: 100vh;
    background-color: var(--bg-surface);
    border-left: 1px solid var(--border-color);
    box-shadow: -8px 0 30px rgba(0, 0, 0, 0.65);
    z-index: 1001;
    transition: right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.toc-sidebar.open {
    right: 0;
}

.toc-sidebar-header {
    padding: 18px 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: var(--bg-dark);
}

.toc-sidebar-header h3 {
    margin: 0;
    font-size: 15px;
    color: var(--emerald-light);
}

.toc-close-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 18px;
    cursor: pointer;
    padding: 4px;
    transition: color 0.2s;
}

.toc-close-btn:hover {
    color: #f87171;
}

.toc-list {
    list-style: none;
    padding: 20px;
    margin: 0;
    overflow-y: auto;
    flex-grow: 1;
}

.toc-item {
    margin-bottom: 10px;
    line-height: 1.4;
}

.toc-h1 {
    margin-left: 0;
    font-weight: 700;
    margin-top: 12px;
}

.toc-h2 {
    margin-left: 14px;
    font-weight: 500;
}

.toc-h3 {
    margin-left: 28px;
    font-size: 0.88em;
    opacity: 0.78;
}

.toc-item a {
    color: var(--text-secondary);
    text-decoration: none;
    transition: color 0.15s ease;
    display: block;
}

.toc-item a:hover {
    color: var(--emerald-light);
    text-decoration: none;
}

/* Tablas */
table {
    width: 100%;
    border-collapse: collapse;
    margin: 22px 0;
    font-size: 13px;
}

th,
td {
    border: 1px solid var(--border-color);
    padding: 9px 12px;
    text-align: left;
}

th {
    background-color: var(--bg-surface);
    color: var(--emerald-light);
    font-weight: 700;
}

tr:nth-child(even) {
    background-color: rgba(255, 255, 255, 0.015);
}

tr:hover {
    background-color: rgba(16, 185, 129, 0.04);
}

blockquote {
    border-left: 4px solid var(--emerald);
    background-color: rgba(16, 185, 129, 0.06);
    padding: 12px 18px;
    margin: 18px 0;
    border-radius: 0 8px 8px 0;
    color: var(--text-secondary);
}

code {
    background-color: rgba(255, 255, 255, 0.07);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 12.5px;
    color: var(--cyan-light);
}

pre {
    background-color: #05080d !important;
    border: 1px solid var(--border-color);
    padding: 16px;
    border-radius: 8px;
    overflow-x: auto;
}

pre code {
    background-color: transparent !important;
    padding: 0;
    border-radius: 0;
    color: #e2e8f0;
}

/* MERMAID INTERACTIVE WRAPPER */
.mermaid-wrapper {
    background-color: var(--bg-surface) !important;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    margin: 24px 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
    transition: border-color 0.2s;
}

.mermaid-wrapper.fullscreen {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9999;
    margin: 0;
    border-radius: 0;
    height: 100vh;
}

.mermaid-wrapper:hover {
    border-color: rgba(16, 185, 129, 0.4);
}

.mermaid-header {
    background-color: var(--bg-surface);
    border-bottom: 1px solid var(--border-color);
    padding: 8px 14px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    user-select: none;
}

.mermaid-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    font-weight: 700;
    color: var(--emerald-light);
}

.mermaid-controls {
    display: flex;
    gap: 6px;
}

.m-btn {
    background-color: rgba(255, 255, 255, 0.05);
    border: 1px solid #334155;
    color: #cbd5e1;
    border-radius: 5px;
    padding: 3px 9px;
    cursor: pointer;
    font-size: 12px;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.m-btn:hover {
    background-color: rgba(16, 185, 129, 0.2);
    border-color: var(--emerald);
    color: #fff;
}

.mermaid-viewport {
    position: relative;
    width: 100%;
    height: auto;
    min-height: 200px;
    overflow: hidden;
    display: block;
    text-align: center;
    background-color: var(--bg-surface);
    background-image: radial-gradient(circle, rgba(56, 189, 248, 0.12) 1px, transparent 1px);
    background-size: 20px 20px;
    padding: 24px;
    box-sizing: border-box;
    cursor: grab;
    touch-action: pan-y;
    user-select: none;
    -webkit-user-select: none;
}

.mermaid {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
}

.mermaid svg {
    width: 100% !important;
    max-width: 900px !important;
    height: auto !important;
}

/* Subgrafos / Clusters con fondo armónico con el tema oscuro */
.mermaid svg .cluster rect {
    fill: #0d131f !important;
    stroke: #334155 !important;
    stroke-width: 1.5px !important;
    rx: 6px !important;
    ry: 6px !important;
}

.mermaid svg .cluster .nodeLabel,
.mermaid svg .cluster text {
    fill: #38bdf8 !important;
    font-weight: 700 !important;
    font-size: 13px !important;
}

/* Nodos con fondo oscuro sutil y texto claro legible */
.mermaid svg .node rect,
.mermaid svg .node circle,
.mermaid svg .node polygon {
    fill: #162032 !important;
    stroke: #38bdf8 !important;
    stroke-width: 1.5px !important;
}

.mermaid svg .node .label,
.mermaid svg .node text {
    fill: #f1f5f9 !important;
    font-size: 12px !important;
}

/* Flechas y enlaces */
.mermaid svg .edgePath path {
    stroke: #64748b !important;
    stroke-width: 1.5px !important;
}

.mermaid svg .edgeLabel {
    background-color: #0d131f !important;
    color: #94a3b8 !important;
    fill: #94a3b8 !important;
    font-size: 11px !important;
}

.mermaid-viewport:active {
    cursor: grabbing;
}

.mermaid-wrapper.fullscreen .mermaid-viewport {
    height: calc(100vh - 38px);
    flex-grow: 1;
}

.mermaid-wrapper.collapsed .mermaid-viewport {
    display: none;
}

.katex-display {
    overflow-x: auto;
    overflow-y: hidden;
    padding: 8px 0;
    max-width: 100%;
}

hr {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 28px 0;
}

a {
    color: var(--emerald-light);
    text-decoration: none;
}

a:hover {
    color: var(--cyan-light);
    text-decoration: underline;
}

.loading {
    text-align: center;
    padding: 48px;
    color: var(--text-muted);
    font-style: italic;
}

.error {
    color: #ef4444;
    background-color: rgba(239, 68, 68, 0.1);
    border: 1px solid #ef4444;
    padding: 16px;
    border-radius: 8px;
    margin-top: 20px;
}

/* ── Notas al Pie (marked-footnote) ─────────────────────────────── */
sup a[id^="fnref-"],
sup a[href^="#fn-"] {
    color: var(--cyan-light);
    text-decoration: none;
    font-weight: 700;
    font-size: 0.72em;
    padding: 0 2px;
    border-bottom: 1px dotted var(--cyan);
    transition: color 0.15s, border-color 0.15s;
}

sup a[id^="fnref-"]:hover,
sup a[href^="#fn-"]:hover {
    color: var(--emerald-light);
    border-color: var(--emerald-light);
}

section.footnotes {
    margin-top: 56px;
    padding-top: 24px;
    border-top: 2px solid var(--border-color);
    position: relative;
}

section.footnotes::before {
    content: "Notas al Pie";
    position: absolute;
    top: -13px;
    left: 0;
    background: var(--bg-card);
    padding: 0 10px 0 0;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--emerald-light);
}

section.footnotes ol {
    padding-left: 0;
    list-style: none;
    counter-reset: footnote-counter;
}

section.footnotes ol li {
    counter-increment: footnote-counter;
    display: flex;
    gap: 14px;
    align-items: flex-start;
    margin-bottom: 14px;
    font-size: 13px;
    color: var(--text-muted);
    line-height: 1.6;
}

section.footnotes ol li::before {
    content: counter(footnote-counter);
    min-width: 22px;
    height: 22px;
    background: rgba(16, 185, 129, 0.12);
    color: var(--emerald-light);
    border: 1px solid rgba(16, 185, 129, 0.3);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: 700;
    flex-shrink: 0;
    margin-top: 2px;
}

section.footnotes ol li p {
    margin: 0;
}

section.footnotes a.footnote-backref {
    color: var(--emerald-light);
    text-decoration: none;
    margin-left: 6px;
    font-size: 13px;
    opacity: 0.75;
    transition: opacity 0.15s;
}

section.footnotes a.footnote-backref:hover {
    opacity: 1;
    color: var(--cyan-light);
}

section.footnotes ol li:target {
    background: rgba(16, 185, 129, 0.08);
    border-radius: 6px;
    padding: 6px 10px;
    margin-left: -10px;
}

/* COPY BUTTON */
.code-wrapper {
    position: relative;
}

.btn-copy {
    position: absolute;
    top: 8px;
    right: 8px;
    background: #1f2937;
    color: #cbd5e1;
    border: 1px solid #374151;
    border-radius: 4px;
    padding: 4px 8px;
    font-size: 11px;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s, background 0.2s;
    z-index: 10;
}

.code-wrapper:hover .btn-copy {
    opacity: 1;
}

.btn-copy:hover {
    background: #374151;
    color: #fff;
}

.btn-copy.copied {
    background: var(--emerald);
    color: #04130e;
    font-weight: 700;
    border-color: var(--emerald-dark);
}

/* Footer */
.viewer-footer {
    padding: 36px 40px;
    border-top: 1px solid var(--border-color);
    color: var(--text-muted);
    margin-top: 40px;
    font-size: 0.88rem;
    background: rgba(7, 10, 16, 0.65);
}

.footer-inner {
    max-width: 980px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 16px;
}

.footer-brand {
    display: flex;
    align-items: center;
    gap: 12px;
}

.footer-brand img {
    width: 30px;
    height: 30px;
    border-radius: 7px;
}

/* SOPORTE MÓVIL Y TOUCH */
@media (max-width: 768px) {
    .viewer-nav {
        padding: 14px 16px;
        flex-direction: column;
        gap: 12px;
    }

    .nav-links {
        gap: 12px;
        flex-wrap: wrap;
        justify-content: center;
    }

    .viewer-main {
        padding: 16px 12px;
    }

    .doc-container {
        padding: 22px 16px;
        border-radius: 10px;
    }

    .tab-btn {
        padding: 7px 10px;
        font-size: 12.5px;
        flex: 1 1 calc(33% - 10px);
    }

    h1 {
        font-size: 22px;
        margin-top: 12px;
        margin-bottom: 14px;
    }

    h2 {
        font-size: 18.5px;
        margin-top: 22px;
    }

    h3 {
        font-size: 16.5px;
    }

    table {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    th,
    td {
        padding: 8px 10px;
        font-size: 12.5px;
        white-space: nowrap;
    }

    .mermaid-header {
        padding: 10px 12px;
        flex-wrap: wrap;
        gap: 8px;
    }

    .m-btn {
        padding: 6px 10px;
        min-width: 34px;
        min-height: 34px;
        font-size: 13px;
    }

    .footer-inner {
        flex-direction: column;
        text-align: center;
    }
}
