/* ====================== tokens ====================== */
:root {
  --fondo: #f6f5f1;
  --fondo-2: #ffffff;
  --fondo-3: #ecebe5;
  --texto: #1f2421;
  --suave: #6b736e;
  --borde: #ddd9cf;
  --linea: #b9b3a5;
  --acento: #2f6f4e;
  --acento-claro: #e3efe7;
  --peligro: #a83232;
  --sombra: 0 1px 2px rgba(31, 36, 33, .06), 0 4px 12px rgba(31, 36, 33, .06);
  --r: 10px;

  --hombre: #dbe7f3;
  --hombre-b: #9dbcd8;
  --mujer: #f6dfe4;
  --mujer-b: #dba9b6;
  --neutro: #f3ecd9;
  --neutro-b: #d5c9a8;
  --yo: #d9ead0;
  --yo-b: #86ab72;
}

@media (prefers-color-scheme: dark) {
  :root {
    --fondo: #16191a;
    --fondo-2: #1e2223;
    --fondo-3: #272c2d;
    --texto: #e8e6e0;
    --suave: #9aa19d;
    --borde: #363c3d;
    --linea: #4c5455;
    --acento: #6fbf8f;
    --acento-claro: #23342b;
    --peligro: #e07373;
    --sombra: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 12px rgba(0, 0, 0, .25);

    --hombre: #24323f; --hombre-b: #3f5b73;
    --mujer: #3a2830;  --mujer-b: #6d4653;
    --neutro: #33301f; --neutro-b: #5f5836;
    --yo: #27351f;     --yo-b: #567244;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--fondo);
  color: var(--texto);
  -webkit-text-size-adjust: 100%;
}

.oculto { display: none !important; }

h1, h2, h3 { margin: 0 0 .6rem; line-height: 1.25; }
h2 { font-size: 1.05rem; letter-spacing: .01em; }

/* ====================== controles ====================== */
.btn {
  font: inherit;
  padding: .45rem .8rem;
  border: 1px solid var(--borde);
  border-radius: 8px;
  background: var(--fondo-2);
  color: var(--texto);
  cursor: pointer;
  white-space: nowrap;
  transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--fondo-3); }
.btn:active { transform: translateY(1px); }
.btn.primario { background: var(--acento); border-color: var(--acento); color: #fff; }
.btn.primario:hover { filter: brightness(1.08); }
.btn.peligro { color: var(--peligro); border-color: color-mix(in srgb, var(--peligro) 40%, var(--borde)); }
.btn.peligro:hover { background: color-mix(in srgb, var(--peligro) 12%, var(--fondo-2)); }
.btn.fantasma { border-color: transparent; background: transparent; }
.btn.fantasma:hover { background: var(--fondo-3); }
.btn.ancho { width: 100%; justify-content: center; margin-top: .4rem; }
.btn.mini { padding: .22rem .5rem; font-size: .82rem; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

input, select, textarea {
  font: inherit;
  width: 100%;
  padding: .45rem .6rem;
  border: 1px solid var(--borde);
  border-radius: 8px;
  background: var(--fondo-2);
  color: var(--texto);
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid color-mix(in srgb, var(--acento) 45%, transparent);
  outline-offset: 1px;
  border-color: var(--acento);
}
textarea { resize: vertical; min-height: 70px; }
label { display: block; font-size: .82rem; color: var(--suave); margin-bottom: .55rem; }
label > input, label > select, label > textarea { margin-top: .2rem; color: var(--texto); }

.error {
  background: color-mix(in srgb, var(--peligro) 12%, var(--fondo-2));
  border: 1px solid color-mix(in srgb, var(--peligro) 35%, var(--borde));
  color: var(--peligro);
  padding: .5rem .7rem; border-radius: 8px; font-size: .87rem; margin: .5rem 0;
}
.apunte { color: var(--suave); font-size: .86rem; margin: 0 0 .8rem; }

/* ====================== acceso ====================== */
.acceso { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.tarjeta-acceso {
  width: 100%; max-width: 380px; background: var(--fondo-2);
  border: 1px solid var(--borde); border-radius: 14px; padding: 1.6rem; box-shadow: var(--sombra);
}
.marca { font-size: 1.3rem; text-align: center; margin-bottom: 1.2rem; }
.pestanas-acceso { display: flex; gap: .3rem; margin-bottom: 1.1rem; background: var(--fondo-3); padding: .25rem; border-radius: 9px; }
.pest-acc {
  flex: 1; font: inherit; font-size: .88rem; padding: .4rem; border: 0; border-radius: 7px;
  background: transparent; color: var(--suave); cursor: pointer;
}
.pest-acc.activa { background: var(--fondo-2); color: var(--texto); box-shadow: var(--sombra); }
.nota-primera {
  font-size: .82rem; background: var(--acento-claro); border-radius: 8px;
  padding: .55rem .7rem; margin: .2rem 0 .6rem; color: var(--texto);
}

/* ====================== barra ====================== */
.barra {
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: .5rem 1rem; background: var(--fondo-2);
  border-bottom: 1px solid var(--borde); position: sticky; top: 0; z-index: 30;
}
.marca-min { font-weight: 600; font-size: .95rem; white-space: nowrap; }
.pestanas { display: flex; gap: .2rem; flex: 1; overflow-x: auto; }
.pest {
  font: inherit; font-size: .88rem; padding: .4rem .75rem; border: 0; border-radius: 8px;
  background: transparent; color: var(--suave); cursor: pointer; white-space: nowrap;
}
.pest:hover { background: var(--fondo-3); }
.pest.activa { background: var(--acento-claro); color: var(--acento); font-weight: 600; }
.usuario-barra { display: flex; align-items: center; gap: .35rem; }
.quien { font-size: .82rem; color: var(--suave); white-space: nowrap; }
.quien b { color: var(--texto); }

/* ====================== vistas ====================== */
.vista { padding: 1rem; max-width: 1200px; margin: 0 auto; }
#vistaArbol { max-width: none; padding: 0; }

.herramientas {
  display: flex; align-items: center; gap: .4rem; flex-wrap: wrap;
  padding: .6rem 1rem; border-bottom: 1px solid var(--borde); background: var(--fondo-2);
  position: sticky; top: 49px; z-index: 20;
}
.separador { width: 1px; height: 22px; background: var(--borde); margin: 0 .25rem; }
.buscar { max-width: 300px; }
.select { width: auto; min-width: 140px; }
.zoom { font-size: .8rem; color: var(--suave); min-width: 42px; text-align: center; }

.panel {
  background: var(--fondo-2); border: 1px solid var(--borde);
  border-radius: var(--r); padding: 1rem; margin-bottom: 1rem;
}
.fila-form { display: flex; gap: .6rem; align-items: flex-end; flex-wrap: wrap; margin-bottom: .8rem; }
.fila-form label { flex: 1; min-width: 180px; margin: 0; }
.fila-form .buscar { flex: 1; max-width: none; }

.vacio { text-align: center; padding: 4rem 1rem; color: var(--suave); }

/* ====================== lienzo del arbol ====================== */
.lienzo {
  overflow: auto; padding: 2.5rem 1.5rem 4rem;
  height: calc(100vh - 105px); cursor: grab; background:
    radial-gradient(circle at 1px 1px, color-mix(in srgb, var(--linea) 45%, transparent) 1px, transparent 0) 0 0 / 22px 22px;
}
.lienzo.arrastrando { cursor: grabbing; user-select: none; }
.arbol {
  display: flex; gap: 3rem; justify-content: center; align-items: flex-start;
  transform-origin: top center; width: max-content; margin: 0 auto;
  transition: transform .12s ease-out; position: relative;
}

/* Los conectores entre generaciones. Van detras de las fichas y no reciben
   clics; los dibuja dibujarConectores() midiendo cada ficha. */
#conectores {
  position: absolute; top: 0; left: 0; overflow: visible;
  pointer-events: none; z-index: 0;
}
.arbol .nodo { position: relative; z-index: 1; }

/* --- nodo recursivo --- */
.nodo { display: flex; flex-direction: column; align-items: center; }

.pareja { display: flex; align-items: stretch; position: relative; }
.lazo {
  width: 26px; align-self: center; height: 2px; background: var(--linea); position: relative;
}
.lazo[data-tipo="pareja"] { background: repeating-linear-gradient(90deg, var(--linea) 0 4px, transparent 4px 8px); }
.lazo .etiqueta-lazo {
  position: absolute; top: -16px; left: 50%; transform: translateX(-50%);
  font-size: .62rem; color: var(--suave); white-space: nowrap; background: var(--fondo);
  padding: 0 3px; border-radius: 3px;
}

/* El espacio vertical entre generaciones lo reserva el padding de .rama;
   las lineas que lo cruzan las traza el SVG de #conectores. */
.descendencia { display: flex; justify-content: center; }
.rama { padding: 40px 14px 0; display: flex; flex-direction: column; align-items: center; }

/* --- ficha de persona --- */
.ficha {
  width: 148px; background: var(--neutro); border: 1.5px solid var(--neutro-b);
  border-radius: var(--r); padding: .5rem; text-align: center; cursor: pointer;
  box-shadow: var(--sombra); position: relative; transition: transform .1s, box-shadow .1s;
}
.ficha:hover { transform: translateY(-2px); box-shadow: 0 6px 18px rgba(0, 0, 0, .14); }
.ficha[data-sexo="m"] { background: var(--hombre); border-color: var(--hombre-b); }
.ficha[data-sexo="f"] { background: var(--mujer); border-color: var(--mujer-b); }
.ficha.yo { background: var(--yo); border-color: var(--yo-b); border-width: 2.5px; }
.ficha[data-certeza="supuesto"] { border-style: dashed; opacity: .82; }
.ficha.resaltada { outline: 3px solid var(--acento); outline-offset: 2px; }
.ficha.apagada { opacity: .25; }

.retrato {
  width: 52px; height: 52px; border-radius: 50%; object-fit: cover; display: block;
  margin: 0 auto .35rem; border: 2px solid rgba(255, 255, 255, .75); background: var(--fondo-3);
}
.inicial {
  width: 52px; height: 52px; border-radius: 50%; display: grid; place-items: center;
  margin: 0 auto .35rem; font-size: 1.15rem; font-weight: 600;
  background: rgba(255, 255, 255, .55); color: var(--texto);
  border: 2px solid rgba(255, 255, 255, .75);
}
@media (prefers-color-scheme: dark) {
  .inicial { background: rgba(255, 255, 255, .08); border-color: rgba(255, 255, 255, .14); }
  .retrato { border-color: rgba(255, 255, 255, .14); }
}

.nombre-ficha { font-size: .8rem; font-weight: 600; line-height: 1.25; word-break: break-word; }
.apodo-ficha { font-size: .68rem; color: var(--suave); font-style: italic; }
.fechas-ficha { font-size: .7rem; color: var(--suave); margin-top: .2rem; }
.insignia {
  position: absolute; top: -7px; right: -7px; font-size: .6rem; padding: .1rem .35rem;
  border-radius: 99px; background: var(--fondo-2); border: 1px solid var(--borde); color: var(--suave);
}
/* Abre la familia de quien entro por matrimonio, que llega plegada. */
.mas-familia {
  display: block; width: 100%; margin-top: .4rem; padding: .18rem;
  font: inherit; font-size: .66rem; cursor: pointer;
  color: var(--acento); background: color-mix(in srgb, var(--acento) 12%, transparent);
  border: 1px dashed color-mix(in srgb, var(--acento) 45%, transparent); border-radius: 6px;
}
.mas-familia:hover { background: color-mix(in srgb, var(--acento) 22%, transparent); }

.rotulo-herramienta { margin: 0; font-size: .78rem; color: var(--suave); white-space: nowrap; }

/* ====================== tablas ====================== */
.tabla-envoltorio { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .87rem; }
th, td { text-align: left; padding: .5rem .6rem; border-bottom: 1px solid var(--borde); vertical-align: middle; }
th { font-size: .74rem; text-transform: uppercase; letter-spacing: .05em; color: var(--suave); font-weight: 600; }
tbody tr:hover { background: var(--fondo-3); }
td.acciones { text-align: right; white-space: nowrap; }
td.acciones .btn { margin-left: .25rem; }
.mini-foto { width: 30px; height: 30px; border-radius: 50%; object-fit: cover; background: var(--fondo-3); }

.chip {
  display: inline-block; font-size: .72rem; padding: .12rem .5rem; border-radius: 99px;
  background: var(--fondo-3); border: 1px solid var(--borde); color: var(--suave);
}
.chip.verde { background: var(--acento-claro); border-color: var(--acento); color: var(--acento); }
.chip.rojo  { background: color-mix(in srgb, var(--peligro) 12%, var(--fondo-2)); border-color: var(--peligro); color: var(--peligro); }

.token-nuevo {
  background: var(--acento-claro); border: 1px solid var(--acento);
  border-radius: var(--r); padding: .8rem; margin-top: .6rem;
}
.token-valor {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: .95rem;
  word-break: break-all; background: var(--fondo-2); padding: .5rem .6rem;
  border-radius: 7px; border: 1px solid var(--borde); margin: .5rem 0;
}

/* ====================== modal ====================== */
.capa {
  position: fixed; inset: 0; background: rgba(15, 18, 17, .5); backdrop-filter: blur(2px);
  display: grid; place-items: center; padding: 16px; z-index: 100;
}
.modal {
  background: var(--fondo-2); border: 1px solid var(--borde); border-radius: 14px;
  width: 100%; max-width: 560px; max-height: 90vh; overflow-y: auto;
  padding: 1.3rem; position: relative; box-shadow: 0 12px 40px rgba(0, 0, 0, .25);
}
.cerrar {
  position: absolute; top: .6rem; right: .7rem; font-size: 1.4rem; line-height: 1;
  background: none; border: 0; color: var(--suave); cursor: pointer; padding: .2rem .4rem; border-radius: 6px;
}
.cerrar:hover { background: var(--fondo-3); color: var(--texto); }
.rejilla-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 .7rem; }
.acciones-modal { display: flex; gap: .5rem; justify-content: flex-end; flex-wrap: wrap; margin-top: 1rem; padding-top: .9rem; border-top: 1px solid var(--borde); }
.acciones-modal .izq { margin-right: auto; }

.seccion-modal { border-top: 1px solid var(--borde); margin-top: 1rem; padding-top: .9rem; }
.lista-rel { list-style: none; padding: 0; margin: 0; }
.lista-rel li {
  display: flex; align-items: center; gap: .5rem; padding: .35rem 0;
  border-bottom: 1px dashed var(--borde); font-size: .87rem;
}
.lista-rel li:last-child { border-bottom: 0; }
.lista-rel .crecer { flex: 1; }
.enlace { background: none; border: 0; color: var(--acento); cursor: pointer; padding: 0; font: inherit; text-align: left; }
.enlace:hover { text-decoration: underline; }

.previa-foto {
  width: 96px; height: 96px; border-radius: 12px; object-fit: cover;
  background: var(--fondo-3); border: 1px solid var(--borde); display: block;
}
.zona-foto { display: flex; gap: .8rem; align-items: center; margin-bottom: .8rem; }

.dato { margin: .3rem 0; font-size: .88rem; }
.dato span { color: var(--suave); display: inline-block; min-width: 96px; font-size: .8rem; }

/* ====================== album ====================== */
.album { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: .5rem; }
.album-item { margin: 0; cursor: pointer; position: relative; }
.album-item img {
  width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 8px;
  background: var(--fondo-3); border: 1px solid var(--borde);
  transition: transform .1s, box-shadow .1s; display: block;
}
.album-item:hover img { transform: scale(1.03); box-shadow: var(--sombra); }
.album-anio {
  position: absolute; top: .3rem; right: .3rem; font-size: .62rem;
  background: rgba(0, 0, 0, .6); color: #fff; padding: .05rem .3rem; border-radius: 4px;
}
.album-item figcaption {
  font-size: .68rem; color: var(--suave); margin-top: .22rem; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}

/* ====================== historias ====================== */
.historia {
  border-left: 3px solid var(--acento); padding: .1rem 0 .1rem .7rem; margin: .8rem 0;
}
.historia h4 { margin: 0 0 .25rem; font-size: .9rem; }
.historia p { margin: 0; white-space: pre-wrap; font-size: .88rem; line-height: 1.55; }
.historia footer {
  display: flex; justify-content: space-between; gap: .6rem; flex-wrap: wrap;
  font-size: .74rem; color: var(--suave); margin-top: .4rem;
}
.historia footer .enlace { font-size: .74rem; margin-left: .5rem; }

/* ====================== aviso flotante ====================== */
.aviso {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--texto); color: var(--fondo); padding: .6rem 1.1rem;
  border-radius: 99px; font-size: .87rem; z-index: 200; box-shadow: var(--sombra);
  max-width: calc(100vw - 32px); text-align: center;
}
.aviso.malo { background: var(--peligro); color: #fff; }

/* ====================== responsive ====================== */
@media (max-width: 720px) {
  .barra { padding: .5rem .7rem; gap: .5rem; }
  .marca-min { display: none; }
  .quien { display: none; }
  .vista { padding: .7rem; }
  .herramientas { top: 45px; padding: .5rem .7rem; }
  .lienzo { height: calc(100vh - 150px); padding: 1.5rem .8rem 3rem; }
  .buscar { max-width: none; flex: 1 1 100%; }
  .rejilla-2 { grid-template-columns: 1fr; }
  .ficha { width: 126px; }
  .modal { padding: 1rem; max-height: 94vh; }
}
