:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #f0ebe3;
  --text: #2a2622;
  --muted: #6f675e;
  --line: #d9d1c5;
  --edge: #8f8578;
  --accent: #8a5a2b;
  --accent-soft: #f3e6d6;
  --focus-ring: #c27a2c;
  --male: #4a78a8;
  --female: #b5577a;
  --unknown: #9a948c;
  --danger: #a33a2a;
  --shadow: 0 1px 2px rgba(40, 30, 20, .08), 0 4px 14px rgba(40, 30, 20, .06);
  --radius: 10px;
  --topbar-h: 104px;
  --panel-w: 380px;
  --peek-h: 60px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1a18;
    --surface: #262421;
    --surface-2: #2f2c28;
    --text: #ece6dd;
    --muted: #a59d92;
    --line: #3d3934;
    --edge: #847a6e;
    --accent: #e0a868;
    --accent-soft: #3a2f22;
    --focus-ring: #e0a868;
    --male: #7aa6d6;
    --female: #df86a7;
    --unknown: #8a847c;
    --danger: #e0806f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 14px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--accent); }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- simple pages (login, messages) ---------- */
.page { display: grid; place-items: center; padding: 16px; min-height: 100%; }
.page-card {
  width: min(380px, 100%);
  background: var(--surface);
  border-radius: 14px;
  padding: 28px 24px;
  box-shadow: var(--shadow);
}
.page-card h1 { margin: 0 0 18px; font-size: 22px; }
.login-form { display: grid; gap: 14px; }
.login-form label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.login-form input {
  font: inherit; font-size: 16px; color: var(--text);
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg);
}
.form-error { color: var(--danger); margin: 0; }
.button-primary {
  padding: 11px; border: 0; border-radius: 8px; background: var(--accent); color: var(--surface);
  font-weight: 600; cursor: pointer;
}
.button-link { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 0; text-decoration: underline; }
.tree-list { list-style: none; padding: 0; margin: 0 0 20px; display: grid; gap: 8px; }
.tree-list a {
  display: block; padding: 14px 16px; border-radius: 10px; background: var(--surface-2);
  text-decoration: none; color: var(--text); font-weight: 600;
}

/* ---------- viewer ---------- */
.viewer { overflow: hidden; display: flex; flex-direction: column; height: 100dvh; }

.topbar {
  flex: none;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  position: relative;
  z-index: 20;
}
.topbar-row { display: flex; align-items: center; gap: 8px; }
.brand {
  display: flex; align-items: center; gap: 8px; min-width: 0;
  color: var(--text); text-decoration: none; font-weight: 650;
}
.brand svg { width: 26px; height: 26px; flex: none; color: var(--accent); }
.brand span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 220px; }

.search { position: relative; flex: 1; min-width: 0; max-width: 460px; margin-left: auto; }
.search input {
  width: 100%; font: inherit; font-size: 16px; color: var(--text);
  padding: 8px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--bg);
}
.search input:focus { outline: 2px solid var(--focus-ring); outline-offset: 0; border-color: transparent; }
.search-results {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0;
  list-style: none; margin: 0; padding: 4px; max-height: min(60vh, 420px); overflow: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.search-results li { display: flex; gap: 10px; align-items: baseline; padding: 9px 10px; border-radius: 8px; cursor: pointer; }
.search-results li[aria-selected="true"], .search-results li:hover { background: var(--accent-soft); }
.search-results .years { color: var(--muted); font-size: 13px; margin-left: auto; white-space: nowrap; }
.search-results .empty { color: var(--muted); cursor: default; }

.icon-button {
  display: grid; place-items: center; flex: none;
  width: 38px; height: 38px; border-radius: 10px; border: 1px solid transparent;
  background: transparent; cursor: pointer; font-size: 20px; line-height: 1;
}
.icon-button:hover { background: var(--surface-2); }
.icon-button svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }

.menu {
  position: absolute; right: 12px; top: 52px; min-width: 240px; z-index: 30;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 6px;
}
.menu-section + .menu-section { border-top: 1px solid var(--line); margin-top: 6px; padding-top: 6px; }
.menu-item {
  display: block; width: 100%; text-align: left; padding: 9px 10px; border-radius: 8px;
  border: 0; background: none; color: var(--text); text-decoration: none; cursor: pointer;
}
.menu-item:hover, .menu-item[aria-current="true"] { background: var(--accent-soft); }
.menu-meta { color: var(--muted); font-size: 13px; padding: 8px 10px; }
.menu-meta .warn { color: var(--danger); }

.workspace { flex: 1; min-height: 0; display: flex; position: relative; }
.canvas-wrap { flex: 1; min-width: 0; position: relative; overflow: hidden; }
.canvas {
  width: 100%; height: 100%; display: block; touch-action: none; user-select: none; -webkit-user-select: none;
  cursor: grab; background: var(--bg);
}
.canvas.dragging { cursor: grabbing; }
.canvas-message {
  position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); pointer-events: none;
}
.branch-bar {
  position: absolute; left: 12px; top: 12px; right: 12px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  pointer-events: none;
}
.branch-bar > * { pointer-events: auto; }
.pill {
  border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 999px;
  padding: 6px 12px; font-size: 14px; cursor: pointer; box-shadow: var(--shadow);
}
.pill:hover { border-color: var(--accent); }
.pill[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); }
.line-info {
  font-size: 13px; color: var(--muted); background: var(--surface); border-radius: 999px; padding: 4px 10px;
  box-shadow: var(--shadow);
}
.panel .line-button { margin-top: 10px; box-shadow: none; font-size: 13px; }
.zoom-controls {
  position: absolute; right: 12px; bottom: 12px; display: grid; gap: 6px;
}
.zoom-controls .icon-button { background: var(--surface); border-color: var(--line); box-shadow: var(--shadow); }

/* ---------- SVG tree ---------- */
.fade-in { animation: fade .18s ease-out; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.card { cursor: pointer; }
.card .bg { fill: var(--surface); stroke: var(--line); stroke-width: 1; }
.card:hover .bg { stroke: var(--accent); }
.card:focus { outline: none; }
.card:focus-visible .bg { stroke: var(--focus-ring); stroke-width: 2.5; }
.card .stripe { fill: var(--unknown); }
.card.m .stripe { fill: var(--male); }
.card.f .stripe { fill: var(--female); }
.card .name { fill: var(--text); font-size: 12.5px; font-weight: 600; }
.card .date { fill: var(--muted); font-size: 11.5px; }
.card.unknown .name { fill: var(--muted); font-style: italic; font-weight: 500; }
.card.focus .bg { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 2.5; }
.person .toggle { cursor: pointer; }
.person .toggle circle { fill: var(--surface); stroke: var(--accent); stroke-width: 1.5; }
.person .toggle path { stroke: var(--accent); stroke-width: 2; stroke-linecap: round; }
.person .toggle:hover circle, .person .toggle:focus-visible circle { fill: var(--accent-soft); }
.person .toggle.opened circle { fill: var(--accent); }
.person .toggle.opened path { stroke: var(--surface); }
.person .toggle:focus { outline: none; }

.family-line { fill: none; stroke: var(--edge); stroke-width: 1.8; stroke-linejoin: round; }
.family-line.dashed { stroke-dasharray: 7 5; }
.family-label { fill: var(--muted); font-size: 11px; text-anchor: middle; paint-order: stroke; stroke: var(--bg); stroke-width: 4px; }
.map-box rect { fill: none; stroke: var(--line); stroke-width: 1.5; }
.map-box text { fill: var(--text); font-size: 13px; }
.map-shape { fill: none; stroke: var(--edge); stroke-width: 1.5; stroke-dasharray: 3 4; }
/* far away only the boxes remain, text would be unreadable anyway */
.canvas.far .card text, .canvas.far .family-label, .canvas.far .map-box text { display: none; }

/* ---------- details panel ---------- */
.panel {
  width: var(--panel-w); flex: none; background: var(--surface); border-left: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.panel-handle { display: none; }
.panel-body { overflow-y: auto; padding: 18px 20px 28px; flex: 1; overscroll-behavior: contain; }
.panel h2 { font-size: 21px; line-height: 1.25; margin: 0; }
.panel .subtitle { color: var(--muted); margin: 4px 0 0; }
.panel .lifespan { margin: 8px 0 0; font-size: 15px; }
.panel section { margin-top: 20px; }
.panel h3 {
  font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
  margin: 0 0 6px; font-weight: 650;
}
.facts { margin: 0; display: grid; grid-template-columns: max-content 1fr; gap: 3px 14px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.event + .event { margin-top: 12px; }
.event-title { font-weight: 600; margin-bottom: 2px; }
.note { white-space: pre-wrap; overflow-wrap: anywhere; background: var(--surface-2); border-radius: 8px; padding: 10px 12px; margin: 6px 0 0; }
.people { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.people a {
  display: flex; align-items: baseline; gap: 8px; padding: 6px 8px; margin: 0 -8px; border-radius: 8px;
  text-decoration: none; color: var(--text);
}
.people a:hover { background: var(--surface-2); }
.people .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--unknown); flex: none; align-self: center; }
.people .dot.m { background: var(--male); }
.people .dot.f { background: var(--female); }
.people .years, .people .tag { color: var(--muted); font-size: 13px; }
.people .years { margin-left: auto; white-space: nowrap; }
.partnership + .partnership { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--line); }
.partnership .meta { color: var(--muted); font-size: 14px; margin: 2px 0 4px; }
.partnership h4 { margin: 8px 0 2px; font-size: 13px; color: var(--muted); font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 0; }
.chip { font-size: 13px; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); color: var(--text); text-decoration: none; }
.panel-footer { margin-top: 26px; color: var(--muted); font-size: 12px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--peek-h) + 20px); transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px;
  box-shadow: var(--shadow);
}

/* ---------- small screens: the details become a bottom sheet ---------- */
@media (max-width: 760px) {
  .topbar { padding: 6px 8px; padding-top: max(6px, env(safe-area-inset-top)); }
  .brand span { display: none; }
  .search { max-width: none; }

  .panel {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 25; width: auto;
    height: min(78dvh, 640px); border-left: 0; border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0; box-shadow: 0 -6px 24px rgba(0, 0, 0, .14);
    transform: translateY(calc(100% - var(--peek-h) - env(safe-area-inset-bottom)));
    transition: transform .22s ease;
  }
  .panel.open { transform: none; }
  .panel-handle {
    display: flex; flex-direction: column; align-items: center; gap: 4px;
    width: 100%; min-height: var(--peek-h); border: 0; background: none; padding: 6px 16px 8px; cursor: pointer;
    touch-action: none;
  }
  .grip { width: 38px; height: 4px; border-radius: 2px; background: var(--line); }
  .panel-peek { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
  .panel-peek .years { font-weight: 400; color: var(--muted); margin-left: 8px; }
  .panel-body { padding-top: 0; padding-bottom: calc(28px + env(safe-area-inset-bottom)); }
  .panel:not(.open) .panel-body { visibility: hidden; }
  .canvas-wrap { padding-bottom: var(--peek-h); }
  .branch-bar {
  position: absolute; left: 12px; top: 12px; right: 12px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  pointer-events: none;
}
.branch-bar > * { pointer-events: auto; }
.pill {
  border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 999px;
  padding: 6px 12px; font-size: 14px; cursor: pointer; box-shadow: var(--shadow);
}
.pill:hover { border-color: var(--accent); }
.pill[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); }
.line-info {
  font-size: 13px; color: var(--muted); background: var(--surface); border-radius: 999px; padding: 4px 10px;
  box-shadow: var(--shadow);
}
.panel .line-button { margin-top: 10px; box-shadow: none; font-size: 13px; }
.zoom-controls { bottom: calc(var(--peek-h) + 12px); }
  .zoom-controls #zoom-in, .zoom-controls #zoom-out { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .panel { transition: none; }
  .fade-in { animation: none; }
}
