:root {
  --paper: #e8ebe5;
  --band: #dfe3dc;
  --card: #fbfcf9;
  --ink: #1d2a2e;
  --muted: #5b6866;
  --rule: #bcc4bb;
  --line: #8d9892;
  --sea: #17727a;
  --madder: #a23b3f;
  --stamp: #5a4a8c;
  --focus: #17727a;
  --panel-w: 420px;
  --serif: "Literata", Georgia, "Times New Roman", serif;
  --sans: "Source Sans 3", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #131a1c; --band: #182124; --card: #1e282b; --ink: #e2e8e4; --muted: #95a29f;
    --rule: #34403f; --line: #5f6d6a; --sea: #5bb6bb; --madder: #e0807f; --stamp: #aa9ce2; --focus: #5bb6bb;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #131a1c; --band: #182124; --card: #1e282b; --ink: #e2e8e4; --muted: #95a29f;
  --rule: #34403f; --line: #5f6d6a; --sea: #5bb6bb; --madder: #e0807f; --stamp: #aa9ce2; --focus: #5bb6bb;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.45 var(--sans);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* Top bar */
.bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 12px 24px;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 20px 10px;
  border-bottom: 1px solid var(--rule);
  background: var(--paper);
  position: relative; z-index: 6;
}
.brand h1 { font: 600 1.35rem/1.1 var(--serif); margin: 0; letter-spacing: -0.01em; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 0.85rem; }
.search { position: relative; flex: 1 1 220px; max-width: 360px; }
.search input, .field input, .field select, .field textarea {
  width: 100%; font: inherit; color: var(--ink); background: var(--card);
  border: 1px solid var(--rule); border-radius: 6px; padding: 7px 10px;
}
.search-results {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; margin: 0; padding: 4px;
  list-style: none; background: var(--card); border: 1px solid var(--rule); border-radius: 8px;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.12); max-height: 50vh; overflow: auto;
}
.search-results li { padding: 6px 10px; border-radius: 5px; cursor: pointer; }
.search-results li small { color: var(--muted); margin-left: 6px; }
.search-results li[aria-selected="true"], .search-results li:hover { background: var(--band); }
.bar-actions { display: flex; align-items: center; gap: 8px; margin-left: auto; flex-wrap: wrap; }
.save-status { color: var(--muted); font-size: 0.85rem; min-width: 5.5em; text-align: right; }
.save-status.error { color: var(--madder); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: 600 0.9rem/1 var(--sans); color: var(--paper); background: var(--ink);
  border: 1px solid var(--ink); border-radius: 6px; padding: 8px 12px; cursor: pointer;
  text-decoration: none; white-space: nowrap;
}
.btn:hover { filter: brightness(1.15); }
.btn-quiet { background: transparent; color: var(--ink); border-color: var(--rule); }
.btn-quiet:hover { background: var(--band); filter: none; }
.btn-danger { background: var(--madder); border-color: var(--madder); color: #fff; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; filter: none; }

/* Tree */
.stage { position: relative; flex: 1; min-height: 0; }
#tree { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; user-select: none; }
#tree.panning { cursor: grabbing; }
.band { fill: var(--band); }
.band-label { font: 600 13px var(--serif); fill: var(--muted); }
.band-sub { font: 11px var(--sans); fill: var(--muted); }
.link { fill: none; stroke: var(--line); stroke-width: 1.5; }
.marriage { stroke: var(--line); stroke-width: 1.5; }
.marriage-far { stroke: var(--line); stroke-width: 1.2; stroke-dasharray: 4 4; fill: none; }

.person { cursor: pointer; }
.person .card { fill: var(--card); stroke: var(--rule); stroke-width: 1; }
.person .accent { fill: var(--sea); }
.person.f .accent { fill: var(--madder); }
.person .given { font: 600 16px var(--serif); fill: var(--ink); }
.person .surname { font: 13px var(--sans); fill: var(--muted); }
.person .years { font: 13px var(--sans); fill: var(--ink); font-variant-numeric: tabular-nums; }
.person .ref { font: 600 10.5px var(--sans); fill: var(--stamp); font-variant-numeric: tabular-nums; letter-spacing: 0.02em; }
.person:hover .card { stroke: var(--line); }
.person:focus { outline: none; }
.person:focus-visible .card { stroke: var(--focus); stroke-width: 2; }
.person.selected .card { stroke: var(--ink); stroke-width: 2; }
.person.root .card { stroke-width: 2; }
.person.dim { opacity: 0.28; }
.link.dim, .marriage.dim { opacity: 0.25; }
.link.lit { stroke: var(--ink); stroke-width: 2; }

.zoom {
  position: absolute; left: 16px; bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  display: flex; background: var(--card); border: 1px solid var(--rule); border-radius: 8px; overflow: hidden;
}
.zoom button {
  font: 600 0.95rem var(--sans); color: var(--ink); background: none; border: 0;
  padding: 8px 12px; min-width: 40px; cursor: pointer;
}
.zoom button + button { border-left: 1px solid var(--rule); }
.zoom button:hover { background: var(--band); }
.highlight-note {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%);
  background: var(--card); border: 1px solid var(--rule); border-radius: 999px;
  padding: 6px 8px 6px 14px; display: flex; gap: 10px; align-items: center; font-size: 0.9rem;
}
.highlight-note button { font: inherit; background: var(--band); border: 0; border-radius: 999px; padding: 3px 10px; cursor: pointer; color: var(--ink); }
.empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); margin: 0; }

/* Panel */
.panel {
  position: fixed; z-index: 5; top: var(--bar-h, 0px); right: 0; bottom: 0; width: min(var(--panel-w), 100%);
  background: var(--card); border-left: 1px solid var(--rule);
  box-shadow: -12px 0 32px rgb(0 0 0 / 0.08);
  overflow-y: auto; overscroll-behavior: contain;
  padding: 18px 24px calc(28px + env(safe-area-inset-bottom, 0px));
  animation: slide-in 0.18s ease-out;
}
@keyframes slide-in { from { transform: translateX(24px); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .panel { animation: none; } }
.panel-close {
  position: sticky; top: 0; float: right; margin: -4px -8px 0 0;
  width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--band);
  color: var(--ink); font-size: 1.3rem; line-height: 1; cursor: pointer;
}
.p-ref { font: 600 0.8rem var(--sans); color: var(--stamp); font-variant-numeric: tabular-nums; margin: 0 0 6px; }
.p-name { font: 600 2rem/1.1 var(--serif); margin: 0; letter-spacing: -0.015em; }
.p-surname { font: 400 1.25rem/1.2 var(--serif); margin: 2px 0 0; color: var(--muted); }
.p-relation { margin: 12px 0 0; padding-left: 10px; border-left: 3px solid var(--sea); }
.person-f .p-relation { border-left-color: var(--madder); }
.p-relation small { display: block; color: var(--muted); }
.panel h3 { font: 600 1.02rem var(--serif); margin: 26px 0 8px; padding-bottom: 6px; border-bottom: 1px solid var(--rule); }
.facts { display: grid; grid-template-columns: minmax(8.5em, 40%) 1fr; gap: 7px 14px; margin: 0; }
.facts dt { color: var(--muted); font-size: 0.88rem; }
.facts dt span, .field label span { display: block; font-size: 0.78rem; opacity: 0.85; font-style: italic; }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.facts dd small { display: block; color: var(--muted); font-size: 0.82rem; }
.muted { color: var(--muted); }
.kin { display: grid; grid-template-columns: minmax(6em, 28%) 1fr; gap: 8px 12px; align-items: start; }
.kin > span { color: var(--muted); font-size: 0.88rem; padding-top: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font: 0.9rem/1.2 var(--sans); color: var(--ink); background: var(--paper);
  border: 1px solid var(--rule); border-left: 3px solid var(--sea); border-radius: 5px;
  padding: 4px 9px; cursor: pointer; text-align: left;
}
.chip.f { border-left-color: var(--madder); }
.chip:hover { background: var(--band); }
.chip.ghost { border-style: dashed; border-left-width: 1px; color: var(--muted); cursor: default; background: none; }
.chip-x { margin-left: 6px; border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 1rem; padding: 0 2px; }
.linkish { font: inherit; color: var(--sea); background: none; border: 0; padding: 0; cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
.notes { white-space: pre-wrap; margin: 0; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 28px; }
.confirm { margin-top: 28px; padding: 14px; border: 1px solid var(--madder); border-radius: 8px; }
.confirm p { margin: 0 0 12px; }
.confirm .actions { margin-top: 0; }

/* Form */
.form-title { font: 600 1.5rem/1.2 var(--serif); margin: 0 0 4px; }
.form-sub { margin: 0; color: var(--muted); }
.form fieldset { border: 0; padding: 0; margin: 22px 0 0; }
.form legend { font: 600 1.02rem var(--serif); padding: 0 0 6px; width: 100%; border-bottom: 1px solid var(--rule); margin-bottom: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; min-width: 0; }
.grid2 .field { margin-bottom: 0; }
.field label { font-size: 0.88rem; color: var(--muted); }
.field textarea { min-height: 90px; resize: vertical; }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 0.9rem; color: var(--muted); margin: 8px 0 12px; }
.check input { margin-top: 3px; }
.form-error { color: var(--madder); margin: 16px 0 0; }
.spouse-picker { display: flex; flex-direction: column; gap: 8px; }

.toast {
  position: fixed; z-index: 9; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%);
  background: var(--ink); color: var(--paper); padding: 10px 16px; border-radius: 8px; max-width: calc(100% - 32px);
}

@media (min-width: 900px) {
  body.panel-open .zoom { left: 16px; }
}
@media (max-width: 720px) {
  .bar { padding-left: 16px; padding-right: 16px; }
  .brand p { display: none; }
  .search { max-width: none; order: 3; flex-basis: 100%; }
  .bar { gap: 8px 12px; }
  .bar-actions { margin-left: 0; gap: 6px; flex: 1 1 100%; }
  .bar-actions .btn { padding: 7px 10px; }
  .save-status { order: 9; min-width: 0; margin-left: auto; }
  .panel {
    top: auto; left: 0; width: 100%; max-height: 78vh;
    border-left: 0; border-top: 1px solid var(--rule); border-radius: 14px 14px 0 0;
    box-shadow: 0 -12px 32px rgb(0 0 0 / 0.12);
    padding-left: 16px; padding-right: 16px;
    animation-name: slide-up;
  }
  @keyframes slide-up { from { transform: translateY(24px); opacity: 0; } }
  .grid2 { grid-template-columns: 1fr; gap: 0; }
  .grid2 .field { margin-bottom: 12px; }
  .facts { grid-template-columns: minmax(7em, 42%) 1fr; }
}

/* Photos */
.person .avatar-ring { fill: none; stroke: var(--sea); stroke-width: 1.5; }
.person.f .avatar-ring { stroke: var(--madder); }

.p-head.has-portrait { display: flex; gap: 16px; align-items: flex-end; padding-right: 36px; }
.p-head.has-portrait .p-name { font-size: 1.75rem; }
.p-portrait {
  flex: none; width: 108px; aspect-ratio: 4 / 5; padding: 0; border: 0; border-radius: 8px;
  overflow: hidden; cursor: zoom-in; background: var(--band);
  box-shadow: 0 0 0 1px var(--rule);
}
.p-portrait img { width: 100%; height: 100%; object-fit: cover; object-position: 50% 25%; display: block; }

.photos { border-radius: 10px; transition: background 0.15s; }
.photos.dropping { background: var(--band); outline: 2px dashed var(--sea); outline-offset: 4px; }
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; }
.photo-thumb, .photo-add {
  position: relative; aspect-ratio: 1; border-radius: 6px; overflow: hidden;
  border: 1px solid var(--rule); background: var(--band); padding: 0; cursor: pointer;
}
.photo-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo-thumb:hover img { filter: brightness(1.06); }
.photo-badge {
  position: absolute; left: 4px; bottom: 4px; font-size: 0.72rem; line-height: 1;
  padding: 3px 6px; border-radius: 4px; background: rgb(0 0 0 / 0.62); color: #fff;
}
.photo-add {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  border-style: dashed; background: none; color: var(--muted); font-size: 0.82rem; text-align: center;
}
.photo-add span[aria-hidden] { font-size: 1.6rem; line-height: 1; color: var(--ink); }
.photo-add:hover { background: var(--band); color: var(--ink); }
.photo-add:focus-within, #photo-input:focus-visible + .photo-add { outline: 2px solid var(--focus); }
.photo-hint { font-size: 0.85rem; margin: 8px 0 0; }

.lightbox {
  position: fixed; inset: 0; z-index: 8; background: rgb(10 14 15 / 0.88);
  display: flex; align-items: center; justify-content: center;
  padding: calc(16px + env(safe-area-inset-top, 0px)) 16px calc(16px + env(safe-area-inset-bottom, 0px));
}
.lb-frame {
  position: relative; display: flex; flex-direction: column; max-width: min(1100px, 100%); max-height: 100%;
  background: var(--card); border-radius: 12px; overflow: hidden;
}
.lb-stage { position: relative; background: #0d1112; display: flex; justify-content: center; min-height: 0; flex: 1; }
.lb-stage img { display: block; max-width: 100%; max-height: calc(100vh - 220px); object-fit: contain; }
.lb-close {
  position: absolute; top: 10px; right: 10px; z-index: 1; width: 38px; height: 38px; border-radius: 50%;
  border: 0; background: rgb(0 0 0 / 0.55); color: #fff; font-size: 1.4rem; line-height: 1; cursor: pointer;
}
.lb-nav {
  position: absolute; top: 50%; transform: translateY(-50%); width: 44px; height: 64px; border: 0;
  background: rgb(0 0 0 / 0.45); color: #fff; font-size: 2rem; cursor: pointer;
}
.lb-nav.prev { left: 0; border-radius: 0 8px 8px 0; }
.lb-nav.next { right: 0; border-radius: 8px 0 0 8px; }
.lb-bar { padding: 14px 16px 16px; display: flex; flex-direction: column; gap: 10px; }
.lb-who { margin: 0; font: 600 1.05rem var(--serif); }
.lb-bar input { font: inherit; color: var(--ink); background: var(--paper); border: 1px solid var(--rule); border-radius: 6px; padding: 8px 10px; width: 100%; }
.lb-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.lb-note { font-size: 0.9rem; }

/* Relation block */
.p-relation strong { display: block; font: 600 1.05rem/1.3 var(--serif); }
.p-relation small { display: block; color: var(--muted); font-size: 0.86rem; margin-top: 2px; }
.edited { font-size: 0.85rem; margin: 18px 0 0; }
.lb-caption { margin: 0; }
#edit-actions { margin-top: 10px; }

/* Sign-in and invite screens */
.auth {
  flex: 1; overflow-y: auto; display: grid; place-items: center;
  padding: calc(24px + env(safe-area-inset-top, 0px)) 16px calc(24px + env(safe-area-inset-bottom, 0px));
}
.auth-card {
  width: min(420px, 100%); background: var(--card); border: 1px solid var(--rule); border-radius: 14px;
  padding: 28px 28px 24px; box-shadow: 0 16px 48px rgb(0 0 0 / 0.08);
}
.auth-brand { font: 600 0.95rem var(--serif); color: var(--sea); margin: 0 0 18px; padding-bottom: 14px; border-bottom: 1px solid var(--rule); }
.auth-card h1 { font: 600 1.65rem/1.2 var(--serif); margin: 0 0 8px; letter-spacing: -0.01em; }
.auth-sub { color: var(--muted); margin: 0 0 20px; }
.auth-note { background: var(--band); border-radius: 8px; padding: 10px 12px; margin: 0 0 16px; }
.auth-foot { font-size: 0.88rem; margin: 18px 0 0; }
.btn-wide { width: 100%; padding: 11px 14px; font-size: 1rem; }
.field-hint { font-size: 0.84rem; margin: -6px 0 12px; }
.auth .form-error { margin: 0 0 12px; }

/* Toolbar popovers */
.pop-wrap { position: relative; }
.focus-btn { max-width: 260px; }
.focus-btn #focus-name { overflow: hidden; text-overflow: ellipsis; }
.focus-label { color: var(--muted); font-weight: 400; }
.pop {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 6; width: 300px; max-width: calc(100vw - 32px);
  background: var(--card); border: 1px solid var(--rule); border-radius: 10px; box-shadow: 0 12px 32px rgb(0 0 0 / 0.14);
  padding: 8px;
}
.focus-pop label { display: block; font-size: 0.85rem; color: var(--muted); padding: 4px 6px 6px; }
.focus-pop input { width: 100%; font: inherit; color: var(--ink); background: var(--paper); border: 1px solid var(--rule); border-radius: 6px; padding: 7px 10px; }
.pop-list { list-style: none; margin: 6px 0 0; padding: 0; max-height: min(50vh, 360px); overflow-y: auto; }
.pop-list li { display: flex; justify-content: space-between; gap: 10px; padding: 7px 8px; border-radius: 6px; cursor: pointer; }
.pop-list li small { color: var(--muted); white-space: nowrap; }
.pop-list li[aria-selected="true"], .pop-list li:hover { background: var(--band); }
.pop-list li.current span { font-weight: 600; }
.pop-list .empty-item { color: var(--muted); cursor: default; }
.pop-head { padding: 6px 8px 10px; border-bottom: 1px solid var(--rule); }
.pop-head strong { display: block; }
.pop-head small, .pop-item small { display: block; color: var(--muted); font-size: 0.82rem; }
.pop-section { padding: 6px 0; }
.pop-section + .pop-section { border-top: 1px solid var(--rule); }
.pop-item {
  display: block; width: 100%; text-align: left; font: inherit; color: var(--ink); background: none; border: 0;
  padding: 8px; border-radius: 6px; cursor: pointer; text-decoration: none;
}
.pop-item:hover { background: var(--band); }
.pop-hint { font-size: 0.86rem; color: var(--muted); margin: 4px 8px; }

/* Family access */
.access-list { list-style: none; margin: 0; padding: 0; }
.access-row { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px 12px; padding: 12px 0; border-bottom: 1px solid var(--rule); }
.access-row.off .access-who { opacity: 0.55; }
.access-who { min-width: 0; flex: 1 1 180px; }
.access-who small { display: block; color: var(--muted); font-size: 0.84rem; }
.access-controls { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.access-controls select { font: inherit; font-size: 0.88rem; color: var(--ink); background: var(--card); border: 1px solid var(--rule); border-radius: 6px; padding: 5px 8px; }
.btn-small { padding: 6px 9px; font-size: 0.84rem; }
.access-note { font-size: 0.85rem; }
.link-box { margin-top: 14px; padding: 12px; border: 1px solid var(--sea); border-radius: 8px; background: var(--paper); }
.link-box p { margin: 0 0 8px; }
.link-row { display: flex; gap: 8px; }
.link-row input { flex: 1; min-width: 0; font: 0.88rem var(--sans); color: var(--ink); background: var(--card); border: 1px solid var(--rule); border-radius: 6px; padding: 7px 9px; }

@media (max-width: 720px) {
  .focus-label { display: none; }
  .focus-btn { max-width: 150px; }
  .pop { position: fixed; left: 16px; right: 16px; top: calc(env(safe-area-inset-top, 0px) + 96px); width: auto; max-width: none; }
}

/* Language switch */
.lang-switch { display: inline-flex; border: 1px solid var(--rule); border-radius: 6px; overflow: hidden; flex: none; }
.lang-switch button {
  font: 600 0.8rem/1 var(--sans); letter-spacing: 0.02em; color: var(--muted); background: none;
  border: 0; padding: 8px 9px; cursor: pointer;
}
.lang-switch button + button { border-left: 1px solid var(--rule); }
.lang-switch button[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
.lang-switch button:not([aria-pressed="true"]):hover { background: var(--band); color: var(--ink); }
.auth-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin: 0 0 18px; padding-bottom: 14px; border-bottom: 1px solid var(--rule); }
.auth-top .auth-brand { margin: 0; padding: 0; border: 0; }
