:root {
  --bg: #f2f5f0;
  --bg-2: #e6ede4;
  --paper: #ffffff;
  --ink: #23302a;
  --muted: #6e7d75;
  --line: #a9c4b2;
  --accent: #3d7a5e;
  --m: #5b8bc0;
  --f: #d9788f;
  --m-soft: #e5eef8;
  --f-soft: #fbe8ec;
  --shadow: 0 1px 2px rgba(35, 60, 45, .06), 0 6px 18px rgba(35, 60, 45, .08);
  --shadow-hover: 0 2px 4px rgba(35, 60, 45, .08), 0 14px 28px rgba(35, 60, 45, .14);
  --serif: "Lora", Georgia, serif;
  --sans: "Montserrat", system-ui, -apple-system, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161c19;
    --bg-2: #1d2521;
    --paper: #232c27;
    --ink: #e8efe9;
    --muted: #9aaba1;
    --line: #4f6b5b;
    --accent: #7fc4a0;
    --m: #8db5e0;
    --f: #ee9fb1;
    --m-soft: #26323f;
    --f-soft: #3a2a30;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 18px rgba(0, 0, 0, .3);
    --shadow-hover: 0 2px 4px rgba(0, 0, 0, .35), 0 14px 28px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  font-family: var(--sans);
  color: var(--ink);
  background: var(--bg);
  background-image: radial-gradient(ellipse at top, var(--paper) 0%, transparent 60%),
                    radial-gradient(ellipse at bottom, var(--bg-2) 0%, transparent 70%);
  background-attachment: fixed;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }

/* ---------- Верхня панель ---------- */

.page-tree { height: 100vh; height: 100dvh; overflow: hidden; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 14px 24px;
  border-bottom: 1px solid color-mix(in srgb, var(--line) 40%, transparent);
  background: color-mix(in srgb, var(--paper) 80%, transparent);
  backdrop-filter: blur(8px);
  position: relative;
  z-index: 10;
}

.brand h1 {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 30px;
  margin: 0;
  letter-spacing: .01em;
}
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.brand p:empty { display: none; }

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

select, input[type="search"] {
  font: inherit;
  font-size: 14px;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid color-mix(in srgb, var(--line) 60%, transparent);
  border-radius: 999px;
  padding: 8px 14px;
  outline: none;
}
select:focus, input[type="search"]:focus { border-color: var(--accent); }

.search { position: relative; }
.search input { width: 240px; }

.search-results {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  width: 300px;
  margin: 0;
  padding: 6px;
  list-style: none;
  background: var(--paper);
  border-radius: 14px;
  box-shadow: var(--shadow);
  border: 1px solid color-mix(in srgb, var(--line) 40%, transparent);
}
.search-results button {
  display: flex; gap: 10px; align-items: center;
  width: 100%;
  padding: 8px;
  font: inherit; color: inherit; text-align: left;
  background: none; border: 0; border-radius: 10px;
  cursor: pointer;
}
.search-results button:hover { background: var(--bg-2); }
.search-results small { display: block; color: var(--muted); font-size: 12px; }
.search-results .none { padding: 10px; color: var(--muted); font-size: 14px; }

/* ---------- Полотно дерева ---------- */

.viewport {
  flex: 1;
  position: relative;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  user-select: none;
}
.viewport.dragging { cursor: grabbing; }

.tree {
  position: absolute;
  top: 0; left: 0;
  transform-origin: 0 0;
  padding: 24px;
  width: max-content;
}
.tree.animate { transition: transform .4s ease; }

.empty { padding: 40px; color: var(--muted); }

.tree { display: flex; flex-direction: column; align-items: center; }

.tree ul {
  display: flex;
  justify-content: center;
  margin: 0;
  padding: 0;
  position: relative;
}
.tree li {
  list-style: none;
  position: relative;
  padding: 0 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Верхній ряд (людина з братами й сестрами): лінії сходяться донизу до батьків */
.sibs.has-parents { padding-bottom: 28px; }
.sibs.has-parents > li { padding-bottom: 28px; }
.sibs.has-parents::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 50%;
  height: 28px;
  border-left: 2px solid var(--line);
}
.sibs.has-parents > li::before,
.sibs.has-parents > li::after {
  content: "";
  position: absolute;
  bottom: 0;
  right: 50%;
  width: 50%;
  height: 28px;
  border-bottom: 2px solid var(--line);
}
.sibs.has-parents > li::after { right: auto; left: 50%; border-left: 2px solid var(--line); }
.sibs.has-parents > li:first-child::before,
.sibs.has-parents > li:last-child::after { border: 0 none; }
.sibs.has-parents > li:last-child::before { border-right: 2px solid var(--line); border-radius: 0 0 10px 0; }
.sibs.has-parents > li:first-child::after { border-radius: 0 0 0 10px; }
.sibs.has-parents > li:only-child { padding-bottom: 0; }
.sibs.has-parents > li:only-child::before,
.sibs.has-parents > li:only-child::after { display: none; }
.sibs > li { justify-content: flex-end; }

/* Предки: від кожної пари лінії розходяться донизу до їхніх батьків */
.anc ul:not(.sibs) { padding-top: 28px; }
.anc ul:not(.sibs) > li { padding-top: 28px; }
.anc ul:not(.sibs) > li::before,
.anc ul:not(.sibs) > li::after {
  content: "";
  position: absolute;
  top: 0;
  right: 50%;
  width: 50%;
  height: 28px;
  border-top: 2px solid var(--line);
}
.anc ul:not(.sibs) > li::after { right: auto; left: 50%; border-left: 2px solid var(--line); }
.anc ul:not(.sibs) > li:only-child::before,
.anc ul:not(.sibs) > li:only-child::after { display: none; }
.anc ul:not(.sibs) > li:only-child { padding-top: 0; }
.anc ul:not(.sibs) > li:first-child::before,
.anc ul:not(.sibs) > li:last-child::after { border: 0 none; }
.anc ul:not(.sibs) > li:last-child::before { border-right: 2px solid var(--line); border-radius: 0 10px 0 0; }
.anc ul:not(.sibs) > li:first-child::after { border-radius: 10px 0 0 0; }
.anc ul:not(.sibs)::before {
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  height: 28px;
  border-left: 2px solid var(--line);
}

.couple {
  display: flex;
  align-items: center;
  gap: 10px;
  position: relative;
  z-index: 1;
}

.rings { display: inline-flex; color: var(--line); }
.rings svg { width: 28px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.6; }

/* ---------- Картка людини ---------- */

.card {
  width: 136px;
  padding: 14px 10px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  color: var(--ink);
  background: var(--paper);
  border-radius: 16px;
  border-top: 3px solid var(--m);
  box-shadow: var(--shadow);
  transition: transform .15s ease, box-shadow .15s ease;
  -webkit-user-drag: none;
}
.card.f { border-top-color: var(--f); }
.card:hover { transform: translateY(-3px); box-shadow: var(--shadow-hover); }
.card.focused { outline: 3px solid var(--accent); outline-offset: 3px; }
.card.me { box-shadow: 0 0 0 2px var(--accent), var(--shadow); }
.card.ghost {
  background: transparent;
  border: 2px dashed var(--line);
  box-shadow: none;
  color: var(--muted);
}
.card.ghost:hover { transform: none; }
.card.ghost .avatar { background: var(--bg-2); color: var(--muted); }

.card-name {
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.15;
  text-align: center;
}
.card-name b { display: block; font-weight: 600; }
.card-years { font-size: 12px; color: var(--muted); letter-spacing: .02em; }
.card-note {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--accent);
}

.sib-dash { width: 22px; border-top: 2px dashed var(--line); flex: none; }

/* Ряд предків: пара по центру, брати й сестри з сім'ями по боках */
.gen-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: end;
  column-gap: 10px;
}
.gen-side { display: flex; align-items: flex-end; gap: 10px; }
.gen-side.left { justify-content: flex-end; }
.gen-side.right { justify-content: flex-start; }
/* Пунктир між братом/сестрою і предком на рівні середини картки */
.gen-side > .sib-dash { margin-bottom: 72px; }
/* Лінія згори доходить до пари, навіть якщо бокові гілки вищі за неї */
.gen-mid {
  align-self: stretch;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.gen-mid::before { content: ""; flex: 1; border-left: 2px solid var(--line); }

/* Брати й сестри предків з сім'ями: пунктирна рамка, менші картки */
.side-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px;
  background: color-mix(in srgb, var(--bg-2) 70%, transparent);
  border: 1.5px dashed var(--line);
  border-radius: 20px;
}
.side-group li { padding-left: 8px; padding-right: 8px; }
.card.side {
  width: 116px;
  padding: 10px 8px;
  gap: 4px;
  box-shadow: none;
}
.card.side .avatar { width: 46px; height: 46px; font-size: 18px; }
.card.side .card-name { font-size: 15px; }
.card.side .card-note {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg-2);
  font-size: 10px;
}

.card.deceased .avatar { filter: grayscale(.6); }

/* ---------- Аватари ---------- */

.avatar {
  width: 64px; height: 64px;
  border-radius: 50%;
  object-fit: cover;
  flex: none;
  -webkit-user-drag: none;
}
.avatar-empty {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: var(--serif);
  font-size: 24px;
  font-weight: 600;
  background: var(--m-soft);
  color: var(--m);
}
.avatar-empty.f { background: var(--f-soft); color: var(--f); }
.avatar-sm { width: 40px; height: 40px; font-size: 16px; }
.avatar-xl { width: 200px; height: 200px; font-size: 64px; border: 6px solid var(--paper); box-shadow: var(--shadow); }

/* ---------- Масштаб ---------- */

.zoom {
  position: fixed;
  right: 20px; bottom: 20px;
  display: flex;
  flex-direction: column;
  background: var(--paper);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
  z-index: 5;
}
.zoom button {
  width: 42px; height: 42px;
  font-size: 20px;
  color: var(--ink);
  background: none;
  border: 0;
  cursor: pointer;
}
.zoom button + button { border-top: 1px solid color-mix(in srgb, var(--line) 30%, transparent); }
.zoom button:hover { background: var(--bg-2); }

.hint {
  position: fixed;
  left: 20px; bottom: 16px;
  margin: 0;
  max-width: 420px;
  font-size: 12px;
  color: var(--muted);
  pointer-events: none;
}

/* ---------- Сторінка людини ---------- */

.crumbs { max-width: 920px; margin: 0 auto; padding: 20px 16px 0; }
.crumbs a { text-decoration: none; font-size: 14px; }
.crumbs a:hover { text-decoration: underline; }

.person { max-width: 920px; margin: 0 auto; padding: 12px 16px 48px; }

.hero {
  display: flex;
  gap: 36px;
  align-items: center;
  padding: 28px 0 32px;
}
.hero-text h1 {
  font-family: var(--serif);
  font-weight: 500;
  font-size: 52px;
  line-height: 1;
  margin: 0 0 6px;
}
.hero-text h1 span { font-weight: 600; display: block; }
.patronymic { font-family: var(--serif); font-style: italic; font-size: 22px; margin: 0 0 4px; color: var(--muted); }
.maiden { margin: 0 0 12px; font-size: 14px; color: var(--muted); }

.vitals { display: grid; gap: 10px; margin: 18px 0 0; }
.vitals div { display: grid; grid-template-columns: 120px 1fr; gap: 12px; }
.vitals dt { color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .06em; padding-top: 2px; }
.vitals dd { margin: 0; }
.place { display: block; color: var(--muted); font-size: 14px; }

.panel {
  background: var(--paper);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 24px 28px;
  margin-bottom: 20px;
}
.panel h2 {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 26px;
  margin: 0 0 16px;
  color: var(--accent);
}

.facts { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px 24px; margin: 0; }
.facts dt { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.facts dd { margin: 4px 0 0; }
.facts .wide { grid-column: 1 / -1; }
.facts ul { margin: 0; padding-left: 18px; }
.facts li { line-height: 1.6; }

.bio p { line-height: 1.7; margin: 0 0 12px; }
.bio p:first-child::first-letter {
  font-family: var(--serif);
  float: left;
  font-size: 54px;
  line-height: .9;
  padding: 4px 8px 0 0;
  color: var(--accent);
}

.timeline { list-style: none; margin: 0; padding: 0 0 0 4px; }
.timeline li {
  display: grid;
  grid-template-columns: 140px 1fr;
  gap: 16px;
  padding: 0 0 16px 20px;
  position: relative;
  border-left: 2px solid color-mix(in srgb, var(--line) 50%, transparent);
}
.timeline li:last-child { padding-bottom: 0; }
.timeline li::before {
  content: "";
  position: absolute;
  left: -7px; top: 3px;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--paper);
  border: 2px solid var(--line);
}
.timeline time { color: var(--muted); font-size: 14px; }
.timeline p { margin: 4px 0 0; color: var(--muted); }

.rel-group + .rel-group { margin-top: 18px; }
.rel-group h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 0 0 10px; font-weight: 600; }
.mini-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.mini {
  display: flex; gap: 12px; align-items: center;
  padding: 10px 12px;
  border-radius: 14px;
  text-decoration: none;
  color: var(--ink);
  background: var(--bg);
  border-left: 3px solid var(--m);
  transition: background .15s;
}
.mini.f { border-left-color: var(--f); }
.mini:hover { background: var(--bg-2); }
.mini b { font-weight: 600; display: block; }
.mini small { color: var(--muted); font-size: 12px; }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; }
.gallery figure { margin: 0; }
.gallery img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 12px; display: block; }
.gallery figcaption { font-size: 13px; color: var(--muted); margin-top: 6px; }

.sources { margin: 8px 4px 0; }
.sources h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 0 0 8px; font-weight: 600; }
.sources ul { margin: 0; padding-left: 18px; }
.sources li { font-size: 13px; line-height: 1.8; }
.sources a { color: var(--muted); }

.to-tree { text-align: center; margin-top: 28px; }

/* ---------- Мобільні ---------- */

@media (max-width: 640px) {
  .topbar { padding: 12px 16px; }
  .brand h1 { font-size: 24px; }
  .tools, .search, .search input { width: 100%; }
  .search-results { width: 100%; }
  .hint { display: none; }

  .hero { flex-direction: column; text-align: center; gap: 16px; }
  .hero-text h1 { font-size: 40px; }
  .avatar-xl { width: 150px; height: 150px; font-size: 48px; }
  .vitals div { grid-template-columns: 1fr; gap: 2px; text-align: center; }
  .panel { padding: 20px; }
  .timeline li { grid-template-columns: 1fr; gap: 2px; }
}
