/* ==========================================================================
   system-design-vault · estilo compartido
   Plano técnico (blueprint): líneas finas, etiquetas monoespaciadas,
   paleta sobria. El color se reserva para estados y el rojo siempre es fallo.
   ========================================================================== */

:root {
  --papel: #f3f5f7;
  --papel-2: #e9eef3;
  --papel-3: #dfe6ee;
  --tinta: #15283b;
  --tinta-suave: #4b6075;
  --linea: #8ea5ba;
  --linea-suave: #c5d2de;
  --rejilla: rgba(35, 85, 135, 0.06);

  /* Estados: el único sitio donde hay color */
  --activo: #1f6fb2;   /* en curso, seleccionado, mensaje en vuelo */
  --ok: #2d7a4d;       /* completado */
  --lento: #a86b12;    /* lento, pendiente, en cola */
  --fallo: #c0392b;    /* fallo (siempre rojo) */

  --activo-fondo: rgba(31, 111, 178, 0.10);
  --ok-fondo: rgba(45, 122, 77, 0.10);
  --lento-fondo: rgba(168, 107, 18, 0.12);
  --fallo-fondo: rgba(192, 57, 43, 0.10);

  --mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  --sans: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --ancho-texto: 44rem;
  --ancho-ancho: 72rem;
  --radio: 3px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --papel: #0d1925;
    --papel-2: #122232;
    --papel-3: #182c40;
    --tinta: #dce7f1;
    --tinta-suave: #93a8bc;
    --linea: #4f6c87;
    --linea-suave: #273d52;
    --rejilla: rgba(120, 170, 220, 0.06);
    --activo: #5aaeff;
    --ok: #4cc38a;
    --lento: #e2a94b;
    --fallo: #ff6b5e;
    --activo-fondo: rgba(90, 174, 255, 0.12);
    --ok-fondo: rgba(76, 195, 138, 0.12);
    --lento-fondo: rgba(226, 169, 75, 0.14);
    --fallo-fondo: rgba(255, 107, 94, 0.13);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --papel: #0d1925;
  --papel-2: #122232;
  --papel-3: #182c40;
  --tinta: #dce7f1;
  --tinta-suave: #93a8bc;
  --linea: #4f6c87;
  --linea-suave: #273d52;
  --rejilla: rgba(120, 170, 220, 0.06);
  --activo: #5aaeff;
  --ok: #4cc38a;
  --lento: #e2a94b;
  --fallo: #ff6b5e;
  --activo-fondo: rgba(90, 174, 255, 0.12);
  --ok-fondo: rgba(76, 195, 138, 0.12);
  --lento-fondo: rgba(226, 169, 75, 0.14);
  --fallo-fondo: rgba(255, 107, 94, 0.13);
  color-scheme: dark;
}

/* ---------- Base ---------- */

*,
*::before,
*::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background-color: var(--papel);
  background-image:
    linear-gradient(var(--rejilla) 1px, transparent 1px),
    linear-gradient(90deg, var(--rejilla) 1px, transparent 1px);
  background-size: 24px 24px;
  color: var(--tinta);
  font-family: var(--sans);
  font-size: 1.0625rem;
  line-height: 1.65;
  overflow-x: hidden;
}

a { color: inherit; text-decoration-color: var(--linea); text-underline-offset: 3px; }
a:hover { text-decoration-color: var(--activo); color: var(--activo); }
:focus-visible { outline: 2px solid var(--activo); outline-offset: 2px; }

h1, h2, h3 { line-height: 1.2; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: clamp(1.9rem, 5vw, 2.6rem); margin: 0.2em 0 0.4em; }
h2 { font-size: 1.5rem; margin: 2.2em 0 0.6em; }
h3 { font-size: 1.15rem; margin: 1.8em 0 0.4em; }
p, ul, ol { margin: 0 0 1em; }
li { margin-bottom: 0.3em; }
strong { font-weight: 600; }

code, kbd, .mono { font-family: var(--mono); font-size: 0.88em; }
code {
  background: var(--papel-2);
  border: 1px solid var(--linea-suave);
  border-radius: var(--radio);
  padding: 0.05em 0.3em;
  overflow-wrap: anywhere;
}

/* ---------- Estructura de página ---------- */

.cabecera {
  border-bottom: 1px solid var(--linea-suave);
  background: color-mix(in srgb, var(--papel) 88%, transparent);
  backdrop-filter: blur(4px);
  position: sticky;
  top: 0;
  z-index: 10;
}
.cabecera__dentro {
  max-width: var(--ancho-ancho);
  margin: 0 auto;
  padding: 0.6rem 16px;
  display: flex;
  align-items: center;
  gap: 1rem;
  font-family: var(--mono);
  font-size: 0.8rem;
  color: var(--tinta-suave);
}
.cabecera__serie { text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.cabecera__serie::before { content: "▦ "; color: var(--linea); }
.cabecera__migas { display: flex; align-items: center; gap: 0.5em; min-width: 0; }
.cabecera__raiz { text-decoration: none; white-space: nowrap; flex-shrink: 0; }
.cabecera__raiz::before { content: "▦ "; color: var(--linea); }
.cabecera__migas .cabecera__serie::before { content: "/ "; }
@media (max-width: 480px) {
  .cabecera__raiz { font-size: 0; }
  .cabecera__raiz::before { content: "▦"; font-size: 0.8rem; }
}
.cabecera__hueco { flex: 1; }

.boton-tema {
  font: inherit;
  color: inherit;
  background: none;
  border: 1px solid var(--linea-suave);
  border-radius: var(--radio);
  padding: 0.2rem 0.6rem;
  cursor: pointer;
  white-space: nowrap;
}
.boton-tema:hover { border-color: var(--linea); color: var(--tinta); }

main { padding: 0 16px 4rem; }
[id] { scroll-margin-top: 4rem; }

.texto {
  max-width: var(--ancho-texto);
  margin-left: auto;
  margin-right: auto;
}
.ancho {
  max-width: var(--ancho-ancho);
  margin-left: auto;
  margin-right: auto;
}

.portada { padding-top: 3rem; }
.etiqueta-episodio {
  font-family: var(--mono);
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--tinta-suave);
}
.entradilla { font-size: 1.2rem; color: var(--tinta-suave); }

.meta {
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--tinta-suave);
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 1.2rem;
}

/* Secciones de la plantilla de episodio: numeradas como un plano */
.seccion { counter-increment: seccion; }
body { counter-reset: seccion; }
.seccion > h2::before,
.seccion > .texto > h2::before {
  content: counter(seccion, decimal-leading-zero) " ";
  font-family: var(--mono);
  font-size: 0.8em;
  font-weight: 400;
  color: var(--linea);
}

/* Nota lateral (p. ej. "banco ficticio") */
.nota {
  font-size: 0.92rem;
  border-left: 2px solid var(--linea);
  padding: 0.2rem 0 0.2rem 0.9rem;
  color: var(--tinta-suave);
  margin: 1.2rem 0;
}
.nota strong { color: var(--tinta); }

/* Aviso destacado con estado */
.destacado {
  border: 1px solid var(--linea);
  border-radius: var(--radio);
  padding: 0.9rem 1.1rem;
  margin: 1.4rem 0;
  background: var(--papel-2);
}
.destacado--fallo { border-color: var(--fallo); background: var(--fallo-fondo); }
.destacado--ok { border-color: var(--ok); background: var(--ok-fondo); }
.destacado--lento { border-color: var(--lento); background: var(--lento-fondo); }
.destacado > :last-child { margin-bottom: 0; }

/* Enlace "ver en el mapa" */
.ver-mapa {
  font-family: var(--mono);
  font-size: 0.75rem;
  white-space: nowrap;
  text-decoration: none;
  border: 1px solid var(--linea-suave);
  border-radius: var(--radio);
  padding: 0 0.35em;
  margin-left: 0.2em;
  vertical-align: 0.1em;
  color: var(--tinta-suave);
}
.ver-mapa::before { content: "⌖ "; }

/* Enlace a un escenario del laboratorio (se abre en otra ventana) */
.ver-mapa.ver-lab::before { content: none; }
.ver-lab::after { content: " ↗"; }

.abrir-lab {
  display: flex;
  justify-content: flex-end;
  margin: 0.5rem 0 -0.8rem;
}
.abrir-lab a {
  font-family: var(--mono);
  font-size: 0.8rem;
  text-decoration: none;
  border: 1px solid var(--linea);
  border-radius: var(--radio);
  padding: 0.3rem 0.7rem;
  background: var(--papel);
}

/* ---------- Recuadro de apoyo (desplegable, reutilizable) ---------- */

.apoyo {
  border: 1px dashed var(--linea);
  border-radius: var(--radio);
  background: var(--papel-2);
  margin: 1.2rem 0;
  font-size: 0.95rem;
}
.apoyo > summary {
  cursor: pointer;
  list-style: none;
  padding: 0.55rem 0.9rem;
  font-family: var(--mono);
  font-size: 0.82rem;
  color: var(--tinta-suave);
  display: flex;
  gap: 0.6rem;
  align-items: baseline;
}
.apoyo > summary::-webkit-details-marker { display: none; }
.apoyo > summary::before {
  content: "+";
  display: inline-block;
  width: 1ch;
  color: var(--linea);
}
.apoyo[open] > summary::before { content: "−"; }
.apoyo > summary .apoyo__tipo {
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.7rem;
  color: var(--linea);
}
.apoyo > summary:hover { color: var(--tinta); }
.apoyo__cuerpo { padding: 0 1rem 0.9rem 2.4rem; }
.apoyo__cuerpo > :last-child { margin-bottom: 0; }

/* ---------- Tablas ---------- */

.tabla-envoltorio { overflow-x: auto; margin: 1.2rem 0; }
table { border-collapse: collapse; width: 100%; font-size: 0.93rem; }
th, td { text-align: left; padding: 0.5rem 0.7rem; border-bottom: 1px solid var(--linea-suave); vertical-align: top; }
th {
  font-family: var(--mono);
  font-weight: 500;
  font-size: 0.75rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--tinta-suave);
  border-bottom-color: var(--linea);
}

/* ---------- Bloques de código ---------- */

pre {
  font-family: var(--mono);
  font-size: 0.8rem;
  line-height: 1.55;
  background: var(--papel-2);
  border: 1px solid var(--linea-suave);
  border-radius: var(--radio);
  padding: 0.7rem 0.9rem;
  overflow-x: auto;
  margin: 1rem 0;
}

/* ---------- Controles ---------- */

.boton {
  font-family: var(--mono);
  font-size: 0.82rem;
  color: var(--tinta);
  background: var(--papel);
  border: 1px solid var(--linea);
  border-radius: var(--radio);
  padding: 0.45rem 0.9rem;
  cursor: pointer;
  line-height: 1.3;
}
.boton:hover:not(:disabled) { border-color: var(--activo); color: var(--activo); }
.boton:disabled { opacity: 0.45; cursor: not-allowed; }
.boton--principal { background: var(--tinta); color: var(--papel); border-color: var(--tinta); }
.boton--principal:hover:not(:disabled) { background: var(--activo); border-color: var(--activo); color: var(--papel); }
.boton--fallo { border-color: var(--fallo); color: var(--fallo); }
.boton--fallo:hover:not(:disabled) { background: var(--fallo-fondo); color: var(--fallo); border-color: var(--fallo); }

/* Control segmentado (radio buttons) */
.segmentado {
  display: inline-flex;
  border: 1px solid var(--linea);
  border-radius: var(--radio);
  overflow: hidden;
}
.segmentado input { position: absolute; opacity: 0; pointer-events: none; }
.segmentado label {
  font-family: var(--mono);
  font-size: 0.75rem;
  padding: 0.25rem 0.6rem;
  cursor: pointer;
  color: var(--tinta-suave);
  border-left: 1px solid var(--linea-suave);
}
.segmentado label:first-of-type { border-left: 0; }
.segmentado input:focus-visible + label { outline: 2px solid var(--activo); outline-offset: -2px; }
.segmentado input:checked + label { color: var(--tinta); background: var(--papel-3); }
.segmentado input[value="lento"]:checked + label { color: var(--lento); background: var(--lento-fondo); }
.segmentado input[value="caido"]:checked + label { color: var(--fallo); background: var(--fallo-fondo); }
.segmentado input[value="ok"]:checked + label { color: var(--ok); background: var(--ok-fondo); }

/* ---------- Diagramas SVG (clases comunes) ---------- */

.diagrama { display: block; width: 100%; height: auto; overflow: visible; }
.diagrama text { font-family: var(--mono); fill: var(--tinta); }

.nodo rect { fill: var(--papel); stroke: var(--tinta); stroke-width: 1; }
.nodo .nodo__nombre { font-size: 11px; font-weight: 500; text-anchor: middle; }
.nodo .nodo__estado { font-size: 8.5px; text-anchor: middle; fill: var(--tinta-suave); }
.nodo--lento rect { stroke: var(--lento); fill: var(--lento-fondo); }
.nodo--lento .nodo__estado { fill: var(--lento); }
.nodo--caido rect { stroke: var(--fallo); stroke-dasharray: 4 3; fill: var(--fallo-fondo); }
.nodo--caido .nodo__estado { fill: var(--fallo); }
.nodo--activo rect { stroke: var(--activo); stroke-width: 2; }
.nodo--ok rect { stroke: var(--ok); stroke-width: 1.6; }
.nodo--fallo rect { stroke: var(--fallo); stroke-width: 2; }

.arista { fill: none; stroke: var(--linea); stroke-width: 1; }
.arista--sincrona { stroke-dasharray: 5 4; }
.punta { fill: var(--linea); }

.mensaje circle { fill: var(--activo); }
.mensaje text { font-size: 8.5px; fill: var(--activo); text-anchor: middle; font-weight: 500; }
.mensaje rect { fill: var(--papel); opacity: 0.9; }
.mensaje--ok circle { fill: var(--ok); }
.mensaje--ok text { fill: var(--ok); }
.mensaje--fallo circle { fill: var(--fallo); }
.mensaje--fallo text { fill: var(--fallo); }
.mensaje--lento circle { fill: var(--lento); }
.mensaje--lento text { fill: var(--lento); }

/* ---------- Siguiente episodio ---------- */

.siguiente {
  display: block;
  text-decoration: none;
  border: 1px solid var(--linea);
  border-radius: var(--radio);
  padding: 1rem 1.2rem;
  background: var(--papel-2);
  margin: 1rem 0;
}
.siguiente:hover { border-color: var(--activo); color: inherit; }
.siguiente__etiqueta { font-family: var(--mono); font-size: 0.75rem; color: var(--tinta-suave); text-transform: uppercase; letter-spacing: 0.08em; }
.siguiente__titulo { font-size: 1.2rem; font-weight: 600; display: block; margin: 0.2rem 0; }
.siguiente--pendiente { border-style: dashed; cursor: default; }
.siguiente--pendiente:hover { border-color: var(--linea); }

.pie {
  border-top: 1px solid var(--linea-suave);
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--tinta-suave);
  padding: 1.5rem 16px 3rem;
  text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}

/* ---------- Criterio: árbol de decisión ---------- */
/* (El episodio 1 lleva una copia en línea, anterior a esta.) */

.arbol { list-style: none; padding: 0; margin: 1.5rem 0; counter-reset: pregunta; }
.arbol > li { counter-increment: pregunta; display: grid; grid-template-columns: minmax(0, 1fr) 11rem; gap: 0 1rem; margin: 0; }
.arbol__pregunta {
  border: 1px solid var(--linea);
  border-radius: var(--radio);
  padding: 0.6rem 0.9rem;
  background: var(--papel);
}
.arbol__pregunta::before {
  content: "P" counter(pregunta) " ";
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--linea);
}
.arbol__pregunta small { display: block; color: var(--tinta-suave); font-size: 0.85rem; margin-top: 0.2rem; }
.arbol__si {
  align-self: center;
  font-family: var(--mono);
  font-size: 0.8rem;
  border: 1px solid var(--linea);
  border-radius: var(--radio);
  padding: 0.4rem 0.7rem;
  background: var(--papel-2);
}
.arbol__si::before { content: "sí → "; color: var(--tinta-suave); }
.arbol__no {
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--tinta-suave);
  padding: 0.2rem 0 0.2rem 1.2rem;
  border-left: 1px solid var(--linea);
  margin-left: 1.2rem;
}
.arbol__final {
  display: block;
  font-family: var(--mono);
  font-size: 0.85rem;
  border: 1px dashed var(--linea);
  border-radius: var(--radio);
  padding: 0.6rem 0.9rem;
}
@media (max-width: 560px) {
  .arbol > li { grid-template-columns: 1fr; }
  .arbol__si { justify-self: start; margin-top: 0.4rem; }
}
.cifras { font-variant-numeric: tabular-nums; }
