:root {
  /* Paper theme (default): parchment like Melquíades' manuscripts */
  --bg: #f4eee0;
  --surface: #faf6ec;
  --surface-2: #ede5d1;
  --border: #d8ccb0;
  --text: #231d14;
  --text-soft: #3d3526;
  --muted: #6b5f4a;
  --gold: #9a6b1f;
  --gold-soft: rgba(154, 107, 31, 0.14);
  --family-line: #5f584b;
  --butterfly: #e5ad0c;
  --ring-external: #8e8570;
  --selected: #231d14;
  --hint-bg: rgba(250, 246, 236, 0.92);
  --label-halo: #f4eee0;
  --grain-opacity: 0.35;

  --rel-lover: #c23b5a;
  --rel-suitor: #8a5bb0;
  --rel-friend: #3e8a57;
  --rel-conflict: #c2651a;
  --rel-killed: #b3261e;
  --rel-mentor: #2f74a6;
  --rel-other: #857a66;

  --sans: "Inter", system-ui, sans-serif;
}

/* Night theme: the original dark look */
:root[data-theme="dark"] {
  --bg: #10130f;
  --surface: #181c16;
  --surface-2: #20261d;
  --border: #2f372b;
  --text: #efe7d2;
  --text-soft: #d9d2bd;
  --muted: #a39d88;
  --gold: #d9b45a;
  --gold-soft: rgba(217, 180, 90, 0.18);
  --family-line: #b5af9e;
  --butterfly: #f2cf3c;
  --ring-external: #7d8575;
  --selected: #fff3c4;
  --hint-bg: rgba(24, 28, 22, 0.85);
  --label-halo: #10130f;
  --grain-opacity: 0;

  --rel-lover: #e46a86;
  --rel-suitor: #c392e0;
  --rel-friend: #6fc28f;
  --rel-conflict: #e89c4f;
  --rel-killed: #e0463a;
  --rel-mentor: #62aede;
  --rel-other: #9d9b8b;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
}

body {
  position: relative;
}

/* The map fills the first screen; the text guide follows below it. */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100svh;
}

/* Subtle paper grain over the whole page */
body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: var(--grain-opacity);
  mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.45 0 0 0 0 0.38 0 0 0 0 0.25 0 0 0 0.35 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-wrap: wrap;
}

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

.butterfly {
  width: 30px;
  height: 30px;
  fill: var(--butterfly);
  filter: drop-shadow(0 0 5px rgba(229, 173, 12, 0.4));
}

h1 {
  margin: 0;
  font-family: var(--sans);
  font-weight: 600;
  font-size: 22px;
  letter-spacing: -0.2px;
  line-height: 1.15;
}

.subtitle {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.controls {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.search input {
  width: 240px;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
}

.search input:focus {
  outline: 2px solid var(--gold);
  outline-offset: 1px;
}

.season {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.season button {
  padding: 8px 12px;
  transition: background-color 0.2s, color 0.2s;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  cursor: pointer;
}

.season button.active {
  background: var(--gold-soft);
  color: var(--gold);
  font-weight: 600;
}

.theme-toggle {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.theme-toggle svg { width: 18px; height: 18px; }
.theme-toggle .sun { display: none; }
:root[data-theme="dark"] .theme-toggle .sun { display: block; }
:root[data-theme="dark"] .theme-toggle .moon { display: none; }

/* Legend / filters */
.legend {
  display: flex;
  gap: 8px;
  padding: 10px 24px;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: none;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 5px 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
}

.chip.off {
  opacity: 0.4;
}

.chip-all.active {
  background: var(--gold-soft);
  border-color: var(--gold);
  color: var(--gold);
  font-weight: 600;
}

.node.faded { opacity: 0.18; transition: opacity 0.2s; }

.chip .swatch {
  width: 18px;
  height: 0;
  border-top: 3px solid currentColor;
}

.chip .swatch.dashed { border-top-style: dashed; }
.chip .swatch.dotted { border-top-style: dotted; }

/* Stage */
.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

#map {
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
}

#map:active { cursor: grabbing; }

.gen-label {
  fill: var(--muted);
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 500;
}

.gen-rule {
  stroke: var(--border);
  stroke-dasharray: 2 6;
}

.edge-family {
  fill: none;
  stroke: var(--family-line);
  stroke-width: 1.75;
  opacity: 0.8;
}

.edge-family.adoptive { stroke-dasharray: 6 5; }

.edge-spouse {
  stroke: var(--family-line);
  stroke-width: 2.5;
}

.edge-rel {
  fill: none;
  stroke-width: 2;
  opacity: 0.8;
}

.edge-rel.suitor { stroke-dasharray: 2 5; stroke-linecap: round; }
.edge-rel.other { stroke-dasharray: 7 5; }

.node { cursor: pointer; outline: none; }

.node:focus-visible .ring { stroke: var(--selected); stroke-width: 5; }

.node .ring {
  fill: var(--surface-2);
  stroke-width: 3;
}

.node.family .ring { stroke: var(--gold); }
.node.external .ring { stroke: var(--ring-external); }

.node .initials {
  fill: var(--muted);
  font-family: var(--sans);
  font-size: 16px;
  font-weight: 600;
  text-anchor: middle;
  dominant-baseline: central;
}

.node .name {
  fill: var(--text);
  font-size: 12.5px;
  font-weight: 500;
  text-anchor: middle;
  paint-order: stroke;
  stroke: var(--label-halo);
  stroke-width: 4px;
  stroke-linejoin: round;
}


.node:hover .ring,
.node.selected .ring { stroke-width: 5; }

.node.selected .ring { stroke: var(--selected); }

/* Hover focus */
.focus-mode .node,
.focus-mode .edge-family,
.focus-mode .edge-spouse,
.focus-mode .edge-rel { opacity: 0.12; transition: opacity 0.15s; }

.focus-mode .lit { opacity: 1 !important; }
.focus-mode .edge-rel.lit { stroke-width: 3; }

.zoom-controls {
  position: absolute;
  left: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.zoom-controls button {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
}

.hint {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  margin: 0;
  padding: 6px 12px;
  border-radius: 8px;
  background: var(--hint-bg);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
  pointer-events: none;
  white-space: nowrap;
}

/* Side panel */
.panel {
  position: absolute;
  top: 0;
  right: 0;
  width: 360px;
  max-width: 100%;
  height: 100%;
  padding: 24px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  transform: translateX(100%);
  transition: transform 0.25s ease;
}

.panel.open { transform: translateX(0); }

.panel-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
}

.panel-photo {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid var(--gold);
  background: var(--surface-2);
  display: grid;
  place-items: center;
  font-family: var(--sans);
  font-size: 32px;
  color: var(--muted);
}

.panel-photo.external { border-color: var(--ring-external); }

.panel h2 {
  margin: 16px 0 6px;
  font-family: var(--sans);
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.2px;
  line-height: 1.2;
}

.panel .tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--gold-soft);
  color: var(--gold);
  font-size: 12px;
}

.panel .tag.external {
  background: var(--surface-2);
  color: var(--muted);
}

.panel p.bio {
  line-height: 1.55;
  color: var(--text-soft);
}

.panel h3 {
  margin: 20px 0 8px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  color: var(--muted);
}

.actors,
.relations {
  list-style: none;
  margin: 0;
  padding: 0;
}

.actors li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
}

.actors img,
.actors .no-photo {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-2);
}

.actors .role { color: var(--muted); font-size: 12px; }

.actors li { align-items: flex-start; }

.actor-links {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 5px;
}

.actor-link {
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text-soft);
  font-size: 11.5px;
  text-decoration: none;
  transition: border-color 0.2s, color 0.2s;
}

.actor-link:hover,
.actor-link:focus-visible {
  border-color: var(--gold);
  color: var(--gold);
}

.relations li button {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  width: 100%;
  padding: 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.relations li button:hover { background: var(--surface-2); }

.relations .dot {
  flex: none;
  width: 10px;
  height: 10px;
  margin-top: 5px;
  border-radius: 50%;
}

.relations .who { font-weight: 600; }
.relations .what { color: var(--muted); font-size: 13px; }

.credits {
  padding: 8px 24px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
}

.credits a { color: var(--gold); }

.guide-link {
  position: absolute;
  right: 16px;
  bottom: 16px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  text-decoration: none;
}

.guide-link:hover { border-color: var(--gold); color: var(--gold); }

/* Text guide (crawlable content) */
.guide {
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.guide-inner {
  max-width: 820px;
  margin: 0 auto;
  padding: 48px 24px 64px;
  line-height: 1.6;
}

.guide h2 {
  margin: 0 0 12px;
  font-size: 28px;
  letter-spacing: -0.3px;
  line-height: 1.2;
}

.guide h3 {
  margin: 36px 0 12px;
  font-size: 20px;
}

.guide h3.guide-gen {
  margin-top: 24px;
  font-size: 15px;
  color: var(--gold);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

.guide-lead { font-size: 16px; color: var(--text-soft); }
.guide-note { color: var(--muted); font-size: 13px; }

.guide-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 12px;
}

.guide-list li {
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
}

.guide-list h4 { margin: 0 0 4px; font-size: 16px; }
.guide-list p { margin: 4px 0 0; color: var(--text-soft); }
.guide-list .guide-rels { color: var(--muted); font-size: 13.5px; }

.guide-season {
  margin-left: 6px;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--gold-soft);
  color: var(--gold);
  font-size: 11px;
  font-weight: 600;
  vertical-align: middle;
}

.guide-faq details {
  border-bottom: 1px solid var(--border);
  padding: 12px 0;
}

.guide-faq summary {
  cursor: pointer;
  font-weight: 600;
}

.guide-faq details p { margin: 8px 0 0; color: var(--text-soft); }

/* Mobile */
@media (max-width: 720px) {
  .topbar { padding: 12px 16px; }
  h1 { font-size: 19px; }
  .controls { width: 100%; flex-direction: column; align-items: stretch; gap: 8px; }
  .search input { width: 100%; }
  .season button { flex: 1; }
  .legend { padding: 8px 16px; }
  .hint { display: none; }
  .guide h2 { font-size: 23px; }
  .guide-inner { padding: 32px 16px 48px; }

  .panel {
    top: auto;
    bottom: 0;
    width: 100%;
    height: 70%;
    border-left: 0;
    border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    transform: translateY(100%);
  }

  .panel.open { transform: translateY(0); }
}
