:root {
  --bg: #f5f1e8;
  --panel: #fffdf8;
  --ink: #2a251d;
  --muted: #8a7f6c;
  --line: #e3dcc9;
  --accent: #7b2d26;
  --accent-soft: #f3e3e1;
  --male: #3d6b99;
  --female: #b05f7a;
  --card: #ffffff;
  --shadow: 0 1px 3px rgba(60, 50, 30, .12);
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 15px/1.45 -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------- Top bar ---------- */
#topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 16px;
  background: linear-gradient(#fffdf8, #faf6ec);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
  z-index: 40;
}
#topbar h1 {
  font: 600 19px/1.1 Georgia, "Times New Roman", serif;
  letter-spacing: .3px;
  margin: 0;
  white-space: nowrap;
}
#search-wrap { position: relative; flex: 1; max-width: 520px; margin-left: auto; }
#search {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: #fff;
  font-size: 15px;
  outline: none;
}
#search:focus { border-color: var(--accent); }
#search-drop {
  position: absolute;
  top: calc(100% + 4px);
  left: 0; right: 0;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(60,50,30,.18);
  max-height: 380px;
  overflow-y: auto;
  z-index: 50;
}
.sr-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 12px;
  cursor: pointer;
}
.sr-item.sel, .sr-item:hover { background: var(--accent-soft); }
.sr-item .mini { width: 30px; height: 30px; font-size: 12px; flex: none; }
.sr-item .sr-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sr-item .sr-years { color: var(--muted); font-size: 12.5px; }
#topbar button, #nav-btns button, #tree-toolbar button {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  color: var(--ink);
}
#topbar button:hover:enabled, #tree-toolbar button:hover {
  border-color: var(--accent);
  color: var(--accent);
}
#topbar button:disabled { opacity: .35; cursor: default; }
#nav-btns { display: flex; gap: 4px; }

/* ---------- Layout ---------- */
#layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 440px;
  min-height: 0;
}
#tree-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  position: relative;
}
#tree-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 14px 0;
  min-height: 38px;
}
#tree-title {
  font: 400 13.5px Georgia, serif;
  color: var(--muted);
  font-style: italic;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#tree-scroll { flex: 1; overflow: auto; min-height: 0; padding: 18px; }
#tree-content { position: relative; min-width: 100%; width: max-content; margin: 0 auto; padding-bottom: 30px; }
#tree-links {
  position: absolute;
  top: 0; left: 0;
  pointer-events: none;
  z-index: 1;
}
#tree { position: relative; z-index: 2; display: flex; flex-direction: column; align-items: center; }

.trow {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 12px;
  margin: 0;
}
.trow + .trow { margin-top: 52px; }
.gpair { display: flex; gap: 12px; }
.gsep { width: 46px; }
.row-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--muted);
  align-self: center;
  margin-right: 6px;
}

/* ---------- Person cards ---------- */
.pcard {
  position: relative;
  width: 152px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 8px 9px;
  text-align: center;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform .12s, box-shadow .12s, border-color .12s;
}
.pcard:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 16px rgba(60,50,30,.16);
  border-color: var(--accent);
}
.pcard.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft), 0 6px 16px rgba(60,50,30,.18);
  transform: translateY(-3px);
}
.pcard.selected::after {
  content: "●";
  position: absolute;
  top: -9px;
  left: 50%;
  transform: translateX(-50%);
  color: var(--accent);
  font-size: 10px;
}
.pcard.male .avatar { border-color: var(--male); }
.pcard.female .avatar { border-color: var(--female); }
.pcard.male .pc-years { color: var(--male); }
.pcard.female .pc-years { color: var(--female); }
.avatar {
  width: 64px;
  height: 64px;
  margin: 0 auto 6px;
  border-radius: 50%;
  border: 2px solid var(--line);
  overflow: hidden;
  position: relative;
  background: #efe9da;
  display: flex;
  align-items: center;
  justify-content: center;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar .initials { font: 600 20px Georgia, serif; color: var(--muted); }
.pcard .mini { width: 34px; height: 34px; font-size: 13px; border-width: 1.5px; }
.pcard .pc-name {
  font: 500 13.5px/1.25 Georgia, serif;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-height: 34px;
}
.pcard .pc-sub {
  font-size: 11.5px;
  color: var(--muted);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pcard .pc-years { font-size: 12px; margin-top: 2px; font-variant-numeric: tabular-nums; }
.pcard .pc-ring { position: absolute; left: 8px; top: 8px; font-size: 10px; color: #c0392b; }
.pcard.unknown {
  border-style: dashed;
  box-shadow: none;
  background: transparent;
  color: var(--muted);
  cursor: default;
  width: 128px;
}
.pcard.unknown:hover { transform: none; border-color: var(--line); }
.pcard.ghost { opacity: .88; }
.pcard.ghost:hover { opacity: 1; }
.kids-group { display: flex; flex-direction: column; gap: 10px; align-items: center; }
.kids-label {
  font-size: 11.5px;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 2px 12px;
  max-width: 340px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.kids-row { display: flex; gap: 12px; flex-wrap: nowrap; }
.spouses-row { display: flex; align-items: flex-start; gap: 44px; }
.self-unit { display: flex; align-items: flex-start; gap: 10px; }
.empty-hint {
  font-size: 13px;
  color: var(--muted);
  font-style: italic;
  padding: 4px 14px;
}

/* ---------- Side panel ---------- */
#side-panel {
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--line);
  background: var(--panel);
  min-height: 0;
}
#side-panel h2 {
  font: 600 13px/1 Georgia, serif;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--muted);
  margin: 0;
  padding: 12px 16px 8px;
}
#details-pane { flex: 1.1; display: flex; flex-direction: column; min-height: 120px; overflow: hidden; }
#timeline-pane { flex: 1; display: flex; flex-direction: column; min-height: 120px; overflow: hidden; border-top: 1px solid var(--line); }
.pane-body { flex: 1; overflow-y: auto; padding: 0 16px 16px; min-height: 0; }
#panel-divider {
  height: 6px;
  cursor: ns-resize;
  background: linear-gradient(#faf6ec, #f5f1e8);
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
#panel-divider:hover { background: var(--accent-soft); }

/* details */
.d-head { display: flex; gap: 14px; align-items: center; }
.d-head .avatar { width: 84px; height: 84px; margin: 0; flex: none; font-size: 26px; }
.d-name { font: 600 20px/1.2 Georgia, serif; }
.d-sub { color: var(--muted); font-size: 13px; margin-top: 3px; }
.d-badges { margin-top: 5px; display: flex; flex-wrap: wrap; gap: 4px; }
.badge {
  font-size: 11px;
  border-radius: 10px;
  padding: 1px 8px;
  background: #efe9da;
  color: var(--ink);
}
.badge.male { background: #e3ecf6; color: var(--male); }
.badge.female { background: #f6e6ec; color: var(--female); }
.d-occu { margin-top: 6px; font-size: 13px; font-style: italic; color: #6b5f4a; }
.d-section { margin-top: 14px; }
.d-section h3 {
  font: 600 12px Georgia, serif;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--accent);
  margin: 0 0 6px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 3px;
}
.ev-item { display: flex; gap: 10px; padding: 4px 0; font-size: 13.5px; }
.ev-item .ev-ic { flex: none; width: 20px; text-align: center; }
.ev-item b { font-weight: 600; }
.ev-place { color: var(--muted); font-size: 12.5px; }
.ev-note { color: #6b5f4a; font-size: 12.5px; font-style: italic; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 16px;
  padding: 3px 10px 3px 4px;
  font-size: 12.5px;
  cursor: pointer;
  margin: 2px 3px 2px 0;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip .mini { width: 22px; height: 22px; font-size: 10px; }
.chip .chip-x { color: #999; font-size: 11px; margin-left: 2px; }
.d-note {
  font-size: 13px;
  white-space: pre-wrap;
  background: #faf6ec;
  border-left: 3px solid var(--accent-soft);
  padding: 8px 10px;
  border-radius: 0 6px 6px 0;
  margin: 4px 0;
}
details.src { font-size: 12px; color: var(--muted); margin-top: 2px; }
details.src summary { cursor: pointer; }
details.src li { overflow-wrap: anywhere; }
.hint { color: var(--muted); font-style: italic; font-size: 13.5px; padding: 20px 0; text-align: center; }

/* ---------- Timeline ---------- */
#tl-lives { padding: 0 16px 8px; }
#tl-scroll { padding-top: 4px; }
.tl-item {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 7px 6px 7px 2px;
  border-radius: 8px;
  cursor: default;
  position: relative;
}
.tl-item.clickable { cursor: pointer; }
.tl-item.clickable:hover { background: var(--accent-soft); }
.tl-year {
  flex: none;
  width: 58px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-size: 12.5px;
  color: var(--accent);
  font-weight: 600;
  padding-top: 8px;
}
.tl-dot {
  flex: none;
  width: 14px;
  display: flex;
  justify-content: center;
  padding-top: 6px;
  position: relative;
}
.tl-dot::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 22px;
  bottom: -14px;
  width: 2px;
  background: var(--line);
  transform: translateX(-50%);
}
.tl-item:last-child .tl-dot::before { display: none; }
.tl-dot span {
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--panel);
  box-shadow: 0 0 0 1.5px var(--accent);
  z-index: 1;
}
.tl-item.ev-marr .tl-dot span { background: #fff; }
.tl-item.ev-deat .tl-dot span { background: #3a3a3a; box-shadow: 0 0 0 1.5px #3a3a3a; }
.tl-item.ev-birt .tl-dot span { background: #5a8a5f; box-shadow: 0 0 0 1.5px #5a8a5f; }
.tl-body { flex: 1; min-width: 0; padding-top: 4px; }
.tl-what { font-size: 13.5px; }
.tl-what .rel { color: var(--muted); font-size: 11.5px; }
.tl-who {
  font-weight: 550;
  color: var(--accent);
  cursor: pointer;
}
.tl-who:hover { text-decoration: underline; }
.tl-place { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tl-age {
  flex: none;
  font-size: 11px;
  color: var(--muted);
  font-style: italic;
  padding-top: 7px;
  white-space: nowrap;
  max-width: 90px;
}
.tl-empty { color: var(--muted); font-style: italic; font-size: 13px; padding: 8px 0; }

/* lifespan bars */
.lives-wrap { position: relative; }
.lives-person {
  position: relative;
  height: 18px;
  display: flex;
  align-items: center;
  font-size: 11px;
  color: var(--muted);
}
.lives-person:hover .lb-name { color: var(--accent); }
.lb-name { position: absolute; left: 0; white-space: nowrap; max-width: 30%; overflow: hidden; text-overflow: ellipsis; z-index: 2; background: var(--panel); padding-right: 4px; cursor: pointer; }
.lives-track { position: absolute; left: 0; right: 0; height: 100%; }
.life-bar {
  position: absolute;
  top: 4px; bottom: 4px;
  border-radius: 3px;
  min-width: 3px;
  cursor: pointer;
}
.life-bar.self { background: var(--accent); }
.life-bar.m { background: var(--male); opacity: .55; }
.life-bar.f { background: var(--female); opacity: .55; }
.life-bar.u { background: #8a7f6c; opacity: .45; }
.lives-axis {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  margin-top: 2px;
}

/* ---------- Modal ---------- */
#modal {
  position: fixed;
  inset: 0;
  background: rgba(42, 37, 29, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
#modal[hidden], [hidden] { display: none !important; }
#modal-card {
  background: var(--panel);
  border-radius: 14px;
  padding: 22px 28px;
  max-width: 420px;
  width: 90%;
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
  position: relative;
}
#modal-close {
  position: absolute;
  top: 10px; right: 10px;
  border: none;
  background: none;
  font-size: 16px;
  cursor: pointer;
  color: var(--muted);
}
.stat-grid { display: grid; grid-template-columns: 1fr auto; gap: 6px 18px; font-size: 14.5px; }
.stat-grid b { text-align: right; font-variant-numeric: tabular-nums; }
#modal h3 { font: 600 17px Georgia, serif; margin: 0 0 12px; }

#loading {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  color: var(--muted);
  font-size: 16px;
  z-index: 200;
}

@media (max-width: 980px) {
  #layout { grid-template-columns: 1fr 340px; }
}
