:root{
  --paper: #f2ede1;
  --paper-line: #d9cfb8;
  --ink: #2c2620;
  --ink-soft: #6b6152;
  --seal: #a5342a;
  --seal-soft: #f0d9d5;
  --male: #33587a;
  --male-soft: #cfe0ea;
  --spouse: #3f7a68;
  --spouse-soft: #d9ece4;
  --unknown: #9c7f3f;
  --unknown-soft: #ece1c3;
  --card: #faf7ef;
  --line: #b9ac8e;
  --accent: #a5342a;
  color-scheme: light;
}
@media (prefers-color-scheme: dark){
  :root:not([data-theme="light"]){
    --paper: #211d17;
    --paper-line: #3a3327;
    --ink: #ece6d8;
    --ink-soft: #b8ad97;
    --seal: #d6675a;
    --seal-soft: #402c28;
    --male: #8fb6d6;
    --male-soft: #23384a;
    --spouse: #7fc9ac;
    --spouse-soft: #1f3a32;
    --unknown: #d9bd74;
    --unknown-soft: #3a3120;
    --card: #2a251d;
    --line: #55492f;
    --accent: #d6675a;
    color-scheme: dark;
  }
}
:root[data-theme="dark"]{
  --paper: #211d17;
  --paper-line: #3a3327;
  --ink: #ece6d8;
  --ink-soft: #b8ad97;
  --seal: #d6675a;
  --seal-soft: #402c28;
  --male: #8fb6d6;
  --male-soft: #23384a;
  --spouse: #7fc9ac;
  --spouse-soft: #1f3a32;
  --unknown: #d9bd74;
  --unknown-soft: #3a3120;
  --card: #2a251d;
  --line: #55492f;
  --accent: #d6675a;
  color-scheme: dark;
}

*{ box-sizing: border-box; }
html, body{ height: auto; }
body{
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: "Noto Sans SC", "Noto Sans CJK SC", "Segoe UI", sans-serif;
  padding-inline: 16px;
  padding-block: 24px 60px;
}

.page-header{
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin: 0 0 18px;
}
.site-logo{
  width: 46px;
  height: 46px;
  flex: none;
  border-radius: 10px;
}
h1{
  font-family: "Noto Serif SC", "Noto Serif CJK SC", "Songti SC", serif;
  font-weight: 700;
  font-size: clamp(22px, 4vw, 30px);
  letter-spacing: 0.08em;
  text-align: center;
  margin: 0 0 4px;
}
.subtitle{
  text-align: center;
  color: var(--ink-soft);
  font-size: 13px;
  margin: 0;
}

/* ---------- toolbar ---------- */
.toolbar{
  max-width: 900px;
  margin: 0 auto 20px;
  padding: 12px 16px;
  border: 1px solid var(--paper-line);
  border-radius: 10px;
  background: color-mix(in srgb, var(--card) 70%, transparent);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 14px;
  font-size: 12.5px;
  color: var(--ink-soft);
}
.toolbar-group{ display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.toolbar-label{ font-weight: 600; color: var(--ink); margin-inline-end: 2px; }
.depth-btn{
  font: inherit;
  font-size: 12.5px;
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  border-radius: 999px;
  padding: 4px 11px;
  line-height: 1.4;
}
.depth-btn:hover{ border-color: var(--accent); color: var(--accent); }
.depth-btn.active{ background: var(--accent); border-color: var(--accent); color: #fff; }

.theme-toggle{
  font: inherit;
  font-size: 15px;
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  border-radius: 999px;
  width: 30px;
  height: 30px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.theme-toggle:hover{ border-color: var(--accent); color: var(--accent); }

.search-box{
  font: inherit;
  font-size: 13px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 12px;
  background: var(--card);
  color: var(--ink);
  min-width: 160px;
}
.search-box:focus{ outline: 2px solid var(--accent); outline-offset: 1px; }
.search-status{ font-size: 12px; color: var(--ink-soft); }

.legend{
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px 20px;
  max-width: 760px;
  margin: 0 auto 26px;
  padding: 8px 16px;
  border: 1px solid var(--paper-line);
  border-radius: 10px;
  background: color-mix(in srgb, var(--card) 70%, transparent);
  font-size: 12px;
  color: var(--ink-soft);
}
.legend-item{ display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.legend-swatch{
  display: inline-flex; align-items: center; justify-content: center;
  width: 16px; height: 16px; border-radius: 4px; font-size: 11px; font-weight: 700;
}
.legend-swatch.male{ background: var(--male-soft); color: var(--male); }
.legend-swatch.female{ background: var(--seal-soft); color: var(--seal); }
.legend-swatch.kin{ border: 1.5px solid var(--male); background: transparent; }
.legend-swatch.spouse-person{ border: 1.5px solid var(--spouse); background: transparent; }
.legend-swatch.spouse-open{ border: 1.5px dashed var(--line); background: transparent; }
.legend-swatch.unknown{ border: 1.5px dashed var(--unknown); background: var(--unknown-soft); }

.scroller{ overflow-x: auto; padding-bottom: 12px; }

/* ---------- tree skeleton ---------- */
.tree, .tree ul{
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
}
.tree{ justify-content: center; margin: 0 auto; width: max-content; min-width: 100%; }
.tree li{
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  padding: 30px 10px 0;
}
.tree li::before, .tree li::after{
  content: "";
  position: absolute;
  top: 0; right: 50%;
  width: 50%; height: 30px;
  border-top: 1.5px solid var(--line);
}
.tree li::after{ right: auto; left: 50%; border-left: 1.5px solid var(--line); }
.tree li:first-child::before{ border: none; }
.tree li:last-child::after{ border: none; }
.tree li:last-child::before{ border-right: 1.5px solid var(--line); border-radius: 0 6px 0 0; }
.tree li:first-child::after{ border-radius: 6px 0 0 0; }
/* a lone child is both :first-child and :last-child, so the two rules above
   would otherwise cancel its connector out entirely — redraw it as a single
   straight line instead of the two-sided "fork" used for multiple siblings */
.tree li:only-child::before{
  display: block;
  right: auto; left: 50%;
  width: 0;
  border-top: none;
  border-right: none;
  border-radius: 0;
  border-left: 1.5px solid var(--line);
}
.tree li:only-child::after{ display: none; }
.tree > li{ padding-top: 0; }
.tree > li::before, .tree > li::after{ display: none; }
.tree ul::before{
  content: "";
  position: absolute;
  top: 0; left: 50%;
  width: 0; height: 30px;
  border-left: 1.5px solid var(--line);
}
.tree li:only-child > .children-wrap > ul::before{ height: 30px; }

/* children container that we hide/show for collapsing */
.children-wrap.collapsed{ display: none; }

/* ---------- node contents ---------- */
.node-unit{ display: flex; flex-direction: column; align-items: center; }
.couple{ display: flex; align-items: flex-end; gap: 6px; }
.slot{ display: flex; flex-direction: column; align-items: center; gap: 5px; position: relative; }
.badge{ font-size: 10px; color: var(--ink-soft); letter-spacing: 0.05em; line-height: 1.3; }
.badge.badge-hidden{ visibility: hidden; }

.person-wrap{ position: relative; }
.person-box{
  writing-mode: vertical-rl;
  text-orientation: upright;
  font-family: "Noto Serif SC", "Noto Serif CJK SC", "Songti SC", "SimSun", serif;
  font-size: 16px;
  line-height: 1.5;
  letter-spacing: 3px;
  width: 44px;
  height: 116px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--card);
  border: 1.5px solid var(--line);
  border-radius: 8px;
  box-shadow: 1px 1px 0 var(--paper-line);
}
.person-box.male,
.person-box.female{ border-color: var(--male); }
.person-box.spouse-person{ border-color: var(--spouse); }
.person-box.placeholder{
  border-style: dashed;
  border-color: var(--unknown);
  background: var(--unknown-soft);
  color: var(--unknown);
  font-family: "Noto Sans SC", "Noto Sans CJK SC", sans-serif;
}
.gender{ font-size: 13px; font-weight: 700; margin-inline-start: 3px; }
.gender.male{ color: var(--male); }
.gender.female{ color: var(--seal); }

.spouse-box{
  writing-mode: vertical-rl;
  text-orientation: upright;
  width: 44px;
  height: 116px;
  box-sizing: border-box;
  border: 1.5px dashed var(--line);
  border-radius: 8px;
  background: transparent;
}
.link-mark{ align-self: center; color: var(--ink-soft); font-size: 13px; padding-bottom: 58px; }
.spouse-cap{
  position: absolute;
  top: 100%;
  margin-top: 5px;
  color: var(--ink-soft);
  font-size: 10px;
  white-space: nowrap;
}

/* collapse toggle button, sits on the outer edge of a person box */
.toggle-btn{
  position: absolute;
  left: 50%;
  bottom: -10px;
  transform: translateX(-50%);
  width: 20px; height: 20px;
  border-radius: 50%;
  border: 1.5px solid var(--line);
  background: var(--card);
  color: var(--ink);
  font-size: 11px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 2;
  font-family: "Noto Sans SC", sans-serif;
}
.toggle-btn:hover{ border-color: var(--accent); color: var(--accent); }
.hidden-count{
  position: absolute;
  right: -6px;
  bottom: -10px;
  min-width: 16px;
  height: 16px;
  padding: 0 3px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  display: none;
  align-items: center;
  justify-content: center;
  font-family: "Noto Sans SC", sans-serif;
}
.node-unit.collapsed .hidden-count{ display: flex; }

/* search highlight */
.person-box.search-match{
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}
.person-box.search-dim{ opacity: 0.35; }

