:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --bg-alt: #f4f4f5;
  --fg: #18181b;
  --fg-muted: #6b7280;
  --border: #e4e4e7;
  --accent: #2563eb;
  --error-bg: #fef2f2;
  --error-fg: #b91c1c;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #18181b;
    --bg-alt: #232326;
    --fg: #f4f4f5;
    --fg-muted: #a1a1aa;
    --border: #313135;
    --accent: #60a5fa;
    --error-bg: #3f1d1d;
    --error-fg: #fca5a5;
  }
}

/* Forzado explícito por el switch (independiente de la preferencia del sistema) */
:root[data-theme="dark"] {
  --bg: #18181b;
  --bg-alt: #232326;
  --fg: #f4f4f5;
  --fg-muted: #a1a1aa;
  --border: #313135;
  --accent: #60a5fa;
  --error-bg: #3f1d1d;
  --error-fg: #fca5a5;
}
:root[data-theme="light"] {
  --bg: #ffffff;
  --bg-alt: #f4f4f5;
  --fg: #18181b;
  --fg-muted: #6b7280;
  --border: #e4e4e7;
  --accent: #2563eb;
  --error-bg: #fef2f2;
  --error-fg: #b91c1c;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
}

a { color: var(--accent); }

code {
  background: var(--bg-alt);
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-size: 0.9em;
}

pre {
  background: var(--bg-alt);
  padding: 1em;
  overflow-x: auto;
  border-radius: 6px;
}
pre code { background: none; padding: 0; }

table {
  border-collapse: collapse;
  width: 100%;
  margin: 1em 0;
  overflow-x: auto;
  display: block;
}
th, td {
  border: 1px solid var(--border);
  padding: 0.5em 0.75em;
  text-align: left;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-alt);
}
.topbar .brand { font-weight: 600; }
.topbar-actions { display: flex; gap: 0.5rem; }
.topbar-actions form { display: inline; }

button {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 0.4em 0.9em;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9em;
}
button:hover { opacity: 0.9; }

.layout {
  display: flex;
  min-height: calc(100vh - 57px);
}

.sidebar {
  width: 260px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  padding: 1rem;
  /* Fija al costado, con su propio scroll. */
  position: sticky;
  top: 0;
  align-self: flex-start;
  max-height: 100vh;
  box-sizing: border-box;
  overflow-y: auto;
}
.sidebar ul { list-style: none; margin: 0 0 0.25rem; padding: 0 0 0 0.25rem; }
.sidebar li { margin: 0; padding: 0.2rem 0; line-height: 1.3; }
.sidebar li a { display: block; text-decoration: none; }
.sidebar li a:hover { text-decoration: underline; }
.sidebar li.active a { font-weight: 700; }
.sidebar .desc { margin: 0.15rem 0 0; font-size: 0.8em; color: var(--fg-muted); }
.nav-filter {
  width: 100%;
  box-sizing: border-box;
  margin-bottom: 0.75rem;
  padding: 0.4em 0.6em;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  font-size: 0.9em;
}
.nav-empty { font-size: 0.85em; color: var(--fg-muted); }
.nav-toggle { display: none; }

/* Índice «En esta página» de los documentos largos. */
.toc {
  margin: 0 0 1.25rem;
  padding: 0.5rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-alt);
  font-size: 0.9em;
}
.toc summary { cursor: pointer; font-weight: 600; }
.toc ol { margin: 0.5rem 0 0.25rem; padding-left: 1.4rem; columns: 2; column-gap: 2rem; }
.toc li { break-inside: avoid; margin: 0.1rem 0; }

.content {
  flex: 1;
  min-width: 0;
  padding: 1.5rem 2rem;
  max-width: 900px;
}

.notice {
  padding: 0.6em 1em;
  border-radius: 6px;
  margin-bottom: 1em;
}
.notice.muted { background: var(--bg-alt); color: var(--fg-muted); font-size: 0.85em; }
.notice.error { background: var(--error-bg); color: var(--error-fg); }

.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.login-card {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  padding: 1.5rem;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.login-card h1 { margin: 0; font-size: 1.3rem; }
.login-card label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.9em; }
.login-card input {
  padding: 0.5em 0.6em;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
}

@media (max-width: 640px) {
  .layout { flex-direction: column; }
  .sidebar {
    display: none;
    width: 100%;
    box-sizing: border-box;
    position: static;
    max-height: none;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  body.nav-open .sidebar { display: block; }
  .nav-toggle {
    display: inline-block;
    background: none;
    border: 1px solid var(--border);
    color: var(--fg);
  }
  .content { padding: 1rem; }
  .toc ol { columns: 1; }
}

.theme-switch {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  background: none;
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 0.3em 0.7em;
  border-radius: 999px;
  font-size: 0.85em;
}
.theme-switch:hover { opacity: 0.85; }

.plantuml-diagram {
  margin: 1em 0;
  padding: 1em;
  background: var(--bg-alt);
  border-radius: 6px;
  overflow-x: auto;
  text-align: center;
}
.plantuml-diagram svg { max-width: 100%; height: auto; }
.plantuml-diagram.is-loading,
.plantuml-diagram.is-error {
  text-align: left;
  color: var(--fg-muted);
  font-size: 0.85em;
}
.plantuml-diagram.is-error { color: var(--error-fg); }

/* --- Pulsar: navegación agrupada, salida de pandoc y descargas --- */
.sidebar .nav-section { margin: 0 0 0.35rem; }
.sidebar .nav-group {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.35rem 0.25rem;
  font-size: 0.75em;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--fg-muted);
  cursor: pointer;
  list-style: none;
  border-radius: 4px;
}
.sidebar .nav-group::-webkit-details-marker { display: none; }
.sidebar .nav-group::before { content: '▸'; margin-right: 0.4rem; font-size: 0.9em; }
.sidebar details[open] > .nav-group::before { content: '▾'; }
.sidebar .nav-group > span:first-child { flex: 1; }
.sidebar .nav-group:hover { background: var(--bg-alt); }
.sidebar .nav-count {
  font-size: 0.9em;
  padding: 0 0.45em;
  border-radius: 999px;
  background: var(--bg-alt);
}

.doc img { max-width: 100%; height: auto; }
.doc table caption { caption-side: bottom; color: var(--fg-muted); font-size: 0.85em; }
.doc td p, .doc th p { margin: 0.2em 0; }
.doc .underline { text-decoration: underline; }

/* Términos enlazados al glosario: discretos, con subrayado punteado y la definición en el tooltip. */
.doc a.glossary-term {
  color: inherit;
  text-decoration: underline dotted;
  text-decoration-color: var(--fg-muted);
  text-underline-offset: 0.2em;
  cursor: help;
}
.doc a.glossary-term:hover { color: var(--accent); text-decoration-color: var(--accent); }
.doc tr:target { background: color-mix(in srgb, var(--accent) 18%, transparent); }

table.downloads td:nth-child(2),
table.downloads td:nth-child(3) { white-space: nowrap; color: var(--fg-muted); }

/* --- Exportar a PDF (impresión del navegador) --- */
@media print {
  :root, :root[data-theme="dark"] {
    --bg: #ffffff;
    --bg-alt: #f4f4f5;
    --fg: #18181b;
    --fg-muted: #4b5563;
    --border: #d4d4d8;
    --accent: #1d4ed8;
  }
  @page { margin: 16mm 14mm; }
  body { background: #fff; color: #000; }
  .topbar, .sidebar { display: none !important; }
  .layout { display: block; min-height: 0; }
  .content { max-width: none; padding: 0; }
  a { color: inherit; text-decoration: underline; }
  table { display: table; overflow: visible; }
  pre, .plantuml-diagram, table, img, figure { break-inside: avoid; }
  h1, h2, h3, h4 { break-after: avoid; }
  .plantuml-diagram { background: none; padding: 0; }
}
