:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #efeae2;
  --ink: #23201c;
  --ink-2: #5d564c;
  --ink-3: #8b8378;
  --line: #ddd5c8;
  --accent: #2f5d50;
  --accent-ink: #ffffff;
  --accent-soft: #dfe9e5;
  --male: #5b7fa6;
  --female: #b06a7d;
  --unknown: #8b8378;
  --warn-bg: #fbf1d9;
  --warn-ink: #6b4e0f;
  --danger: #a33a2c;
  --shadow: 0 1px 2px rgba(35, 32, 28, .06), 0 2px 8px rgba(35, 32, 28, .06);
  --radius: 10px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1917;
    --surface: #24221f;
    --surface-2: #2d2a26;
    --ink: #ece7df;
    --ink-2: #bdb5a9;
    --ink-3: #8e877c;
    --line: #3b3732;
    --accent: #7fb3a2;
    --accent-ink: #12201b;
    --accent-soft: #23332e;
    --male: #8eaed2;
    --female: #d897a8;
    --warn-bg: #3a3020;
    --warn-ink: #f0d79c;
    --danger: #e57b6c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 2px 8px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
    --bg: #1a1917;
    --surface: #24221f;
    --surface-2: #2d2a26;
    --ink: #ece7df;
    --ink-2: #bdb5a9;
    --ink-3: #8e877c;
    --line: #3b3732;
    --accent: #7fb3a2;
    --accent-ink: #12201b;
    --accent-soft: #23332e;
    --male: #8eaed2;
    --female: #d897a8;
    --warn-bg: #3a3020;
    --warn-ink: #f0d79c;
    --danger: #e57b6c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 2px 8px rgba(0, 0, 0, .25);
    color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 var(--sans); min-height: 100vh; display: flex; flex-direction: column; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.2; margin: 0 0 .5em; }
h1 { font-size: 2rem; }
h2 { font-size: 1.25rem; margin-top: 1.6em; }
h3 { font-size: 1.05rem; }
button, input, select, textarea { font: inherit; color: inherit; }

/* Header */
.site-header { display: flex; align-items: center; gap: 16px; padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); font-family: var(--serif); font-size: 1.15rem; }
.brand:hover { text-decoration: none; }
.brand-mark { width: 32px; height: 32px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 700; }
.brand-sub { color: var(--ink-2); font-weight: 400; }
.main-nav { display: flex; gap: 4px; }
.main-nav a { padding: 6px 12px; border-radius: 6px; color: var(--ink-2); }
.main-nav a.active, .main-nav a:hover { background: var(--surface-2); color: var(--ink); text-decoration: none; }
.header-tools { margin-left: auto; display: flex; align-items: center; gap: 8px; position: relative; }
.search-wrap { position: relative; }
#search { width: 220px; padding: 7px 12px; border: 1px solid var(--line); border-radius: 999px; background: var(--bg); }
#search:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.search-results { position: absolute; top: calc(100% + 6px); right: 0; width: 300px; max-height: 60vh; overflow: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); z-index: 30; }
.search-results a { display: flex; gap: 10px; align-items: center; padding: 8px 12px; color: var(--ink); }
.search-results a:hover, .search-results a.sel { background: var(--surface-2); text-decoration: none; }
.search-results .muted { padding: 10px 12px; }
.menu { position: absolute; top: calc(100% + 6px); right: 0; min-width: 220px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 6px; z-index: 30; display: flex; flex-direction: column; }
.menu[hidden] { display: none; }
.menu button { text-align: left; background: none; border: 0; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.menu button:hover { background: var(--surface-2); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 6px 0; width: 100%; }

.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; color: var(--ink); white-space: nowrap; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { padding: 5px 10px; font-size: .9rem; }
.btn:disabled { opacity: .5; cursor: default; }

.banner { background: var(--warn-bg); color: var(--warn-ink); padding: 8px 20px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; font-size: .92rem; }
.banner .btn { padding: 3px 10px; font-size: .88rem; }
.loading, .muted { color: var(--ink-3); }
main { flex: 1; display: flex; flex-direction: column; outline: none; }
.page { max-width: 980px; width: 100%; margin: 0 auto; padding: 28px 20px 60px; }

/* Tree */
.tree-bar { display: flex; align-items: center; gap: 10px; padding: 10px 20px; border-bottom: 1px solid var(--line); background: var(--surface); flex-wrap: wrap; font-size: .92rem; }
.tree-bar .focus-name { font-family: var(--serif); font-size: 1.1rem; font-weight: 600; }
.tree-bar label { color: var(--ink-2); display: flex; align-items: center; gap: 6px; }
.tree-bar select { padding: 3px 6px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.tree-bar .spacer { flex: 1; }
.tree-bar .siblings { color: var(--ink-2); }
.tree-viewport { flex: 1; overflow: auto; position: relative; cursor: grab; min-height: 420px; background-image: radial-gradient(var(--line) 1px, transparent 1px); background-size: 22px 22px; }
.tree-viewport.dragging { cursor: grabbing; user-select: none; }
.tree-canvas { position: relative; transform-origin: 0 0; }
.tree-canvas svg { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
.tree-canvas svg path { fill: none; stroke: var(--ink-3); stroke-width: 1.5; }
.gen-label { position: absolute; left: 8px; font-size: .75rem; color: var(--ink-3); text-transform: uppercase; letter-spacing: .06em; }

.pcard { position: absolute; width: 150px; height: 164px; background: var(--surface); border: 1px solid var(--line); border-top: 3px solid var(--unknown); border-radius: var(--radius); box-shadow: var(--shadow); padding: 10px 8px 8px; text-align: center; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 4px; transition: transform .12s ease, box-shadow .12s ease; }
.pcard:hover { transform: translateY(-2px); box-shadow: 0 4px 14px rgba(35, 32, 28, .14); }
.pcard.M { border-top-color: var(--male); }
.pcard.F { border-top-color: var(--female); }
.pcard.focus { outline: 2px solid var(--accent); outline-offset: 2px; }
.pcard .name { font-weight: 600; font-size: .86rem; line-height: 1.2; max-height: 3.6em; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; }
.pcard .dates { font-size: .78rem; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.pcard .more { position: absolute; right: 4px; top: 4px; width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; color: var(--ink-3); font-size: .8rem; }
.pcard .more:hover { background: var(--surface-2); color: var(--accent); text-decoration: none; }
.pcard .badge { position: absolute; left: 6px; top: 6px; font-size: .7rem; color: var(--ink-3); }
.pcard.hasmore-up::before, .pcard.hasmore-down::after { content: ""; position: absolute; left: 50%; width: 10px; height: 10px; border-radius: 50%; background: var(--surface); border: 1.5px solid var(--ink-3); transform: translateX(-50%); }
.pcard.hasmore-up::before { top: -9px; }
.pcard.hasmore-down::after { bottom: -7px; }

.avatar { width: 64px; height: 64px; border-radius: 50%; object-fit: cover; background: var(--surface-2); flex: none; display: block; }
.avatar.sm { width: 32px; height: 32px; }
.avatar.lg { width: 160px; height: 160px; border-radius: 14px; }
.zoom-ctl { position: absolute; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 4px; z-index: 5; }
.zoom-ctl .btn { width: 36px; height: 36px; padding: 0; justify-content: center; box-shadow: var(--shadow); }

/* Person page */
.person-head { display: flex; gap: 24px; align-items: flex-start; flex-wrap: wrap; }
.person-head .info { flex: 1; min-width: 240px; }
.person-head h1 { margin-bottom: .2em; }
.lifespan { font-size: 1.1rem; color: var(--ink-2); margin-bottom: .6em; }
.alt-names { color: var(--ink-3); font-size: .92rem; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.facts { width: 100%; border-collapse: collapse; }
.facts th, .facts td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.facts th { width: 150px; color: var(--ink-2); font-weight: 500; }
.family-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.family-box { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.family-box h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); font-family: var(--sans); margin-bottom: 8px; }
.plink { display: flex; align-items: center; gap: 10px; padding: 5px 0; color: var(--ink); }
.plink:hover { text-decoration: none; color: var(--accent); }
.plink .d { color: var(--ink-3); font-size: .85rem; }
.notes p { white-space: pre-wrap; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin: 0 0 10px; }
.gallery { display: flex; gap: 10px; flex-wrap: wrap; }
.gallery img { width: 120px; height: 120px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); }
.note-box { background: var(--warn-bg); color: var(--warn-ink); border-radius: var(--radius); padding: 12px 14px; font-size: .92rem; }
.note-box ul { margin: 6px 0 0; padding-left: 20px; }
.sources { color: var(--ink-2); font-size: .9rem; }
.private-note { color: var(--ink-3); font-style: italic; }

/* People list */
.list-tools { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; align-items: center; }
.list-tools input, .list-tools select { padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.list-tools input { flex: 1; min-width: 200px; }
.people-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 8px; }
.people-list a { display: flex; align-items: center; gap: 12px; padding: 10px 12px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); color: var(--ink); }
.people-list a:hover { border-color: var(--accent); text-decoration: none; }
.people-list .nm { font-weight: 600; }
.people-list .d { color: var(--ink-3); font-size: .85rem; }
.letter { grid-column: 1 / -1; font-family: var(--serif); font-size: 1.2rem; margin-top: 12px; color: var(--ink-2); }

/* Edit form */
.form { display: grid; gap: 16px; }
.form fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px 16px; background: var(--surface); margin: 0; }
.form legend { padding: 0 6px; font-weight: 600; font-family: var(--serif); }
.row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: .85rem; color: var(--ink-2); }
.field input, .field select, .field textarea, .ev-row input, .ev-row select { padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); width: 100%; }
.field textarea { min-height: 90px; resize: vertical; }
.check { flex-direction: row; align-items: center; gap: 8px; }
.check input { width: auto; }
.ev-row { display: grid; grid-template-columns: 140px 150px 1fr 1fr 36px; gap: 8px; margin-bottom: 8px; align-items: center; }
.ev-head { font-size: .8rem; color: var(--ink-3); }
.rel-list { list-style: none; padding: 0; margin: 0 0 10px; }
.rel-list li { display: flex; align-items: center; gap: 8px; padding: 4px 0; }
.rel-list li .role { color: var(--ink-3); font-size: .85rem; width: 70px; }
.rel-add { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.rel-add select, .rel-add input { padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
.form-actions { display: flex; gap: 8px; position: sticky; bottom: 0; padding: 12px 0; background: linear-gradient(transparent, var(--bg) 30%); }
.hint { font-size: .85rem; color: var(--ink-3); }
.photo-edit { display: flex; gap: 10px; flex-wrap: wrap; }
.photo-edit figure { margin: 0; position: relative; }
.photo-edit img { width: 90px; height: 90px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); display: block; }
.photo-edit button { position: absolute; top: 3px; right: 3px; border-radius: 50%; width: 22px; height: 22px; padding: 0; border: 0; background: rgba(0, 0, 0, .6); color: #fff; cursor: pointer; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); z-index: 50; max-width: 90vw; }
.dialog-back { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); display: grid; place-items: center; z-index: 60; padding: 16px; }
.dialog { background: var(--surface); border-radius: var(--radius); padding: 20px; width: 100%; max-width: 380px; box-shadow: var(--shadow); }
.dialog .field { margin: 12px 0; }

@media (max-width: 720px) {
  .site-header { padding: 8px 16px; gap: 8px; }
  .header-tools { margin-left: 0; width: 100%; }
  .search-wrap { flex: 1; }
  #search { width: 100%; }
  .search-results { width: 100%; }
  .page { padding: 20px 16px 50px; }
  h1 { font-size: 1.6rem; }
  .avatar.lg { width: 110px; height: 110px; }
  .ev-row { grid-template-columns: 1fr 1fr; }
  .ev-row .ev-place, .ev-row .ev-val { grid-column: span 2; }
  .facts th { width: 110px; }
  .tree-bar { padding: 8px 16px; }
}

/* Home */
.home h1 { font-size: 2.3rem; margin-bottom: .4em; }
.quote { font-family: var(--serif); font-style: italic; font-size: 1.12rem; color: var(--ink-2); border-left: 3px solid var(--accent); margin: 0 0 24px; padding: 4px 0 4px 18px; max-width: 760px; }
.home figure { margin: 0; }
.home figure img { width: 100%; display: block; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface-2); }
.home figcaption { font-size: .85rem; color: var(--ink-3); margin-top: 6px; }
.home .hero { margin-bottom: 24px; }
.story { max-width: 720px; font-size: 1.05rem; line-height: 1.7; }
.story p { margin: 0 0 1em; }
.line { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 8px; counter-reset: gen; }
.line li { counter-increment: gen; }
.line a { display: flex; align-items: center; gap: 10px; padding: 8px 12px 8px 10px; background: var(--surface); border: 1px solid var(--line); border-radius: 999px; color: var(--ink); }
.line a:hover { border-color: var(--accent); text-decoration: none; }
.line li:not(:last-child) a::after { content: "→"; color: var(--ink-3); margin-left: 4px; }
.line .nm { font-weight: 600; font-size: .9rem; display: block; line-height: 1.2; }
.line .d { color: var(--ink-3); font-size: .8rem; display: block; }
.home-gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; margin-top: 32px; }
.home-gallery img { aspect-ratio: 4 / 3; object-fit: cover; }
[hidden] { display: none !important; }
.tree-viewport { flex: none; height: calc(100vh - 118px); height: calc(100dvh - 118px); }
.home .hero img { max-height: 440px; object-fit: cover; object-position: 50% 60%; }
@media (max-width: 720px) { .tree-viewport { height: 72vh; height: 72dvh; } }
.tree-wrap { position: relative; }
.intro { font-size: 1.15rem; color: var(--ink-2); max-width: 720px; margin: 0 0 20px; }
.quote cite { display: block; font-style: normal; font-family: var(--sans); font-size: .82rem; color: var(--ink-3); margin-top: 8px; }
.story h2 { margin-top: 1.8em; }
.story figure.inline { margin: 8px 0 24px; }
.story figure.inline img { max-height: 520px; width: auto; max-width: 100%; }
.src-list { color: var(--ink-2); font-size: .92rem; padding-left: 20px; max-width: 720px; }
.story figure.inline + figure.inline { margin-top: -8px; }
