:root {
  --bg: #f4f1ea;
  --surface: #ffffff;
  --surface-2: #f8f6f1;
  --text: #1f2a24;
  --muted: #6b766f;
  --line: #ddd7cb;
  --accent: #2f6b4f;
  --accent-2: #3f8a66;
  --accent-soft: #e3efe8;
  --danger: #b4423a;
  --male: #4a78a8;
  --female: #b65a7c;
  --unknown: #8a8f8b;
  --edge: #a9a294;
  --card-shadow: 0 1px 2px rgba(30, 40, 30, .08), 0 4px 14px rgba(30, 40, 30, .06);
  --radius: 12px;
  --top-h: 56px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141816; --surface: #1d2320; --surface-2: #232a26; --text: #e7ece8; --muted: #9aa59e;
    --line: #333c37; --accent: #5fb38a; --accent-2: #74c79e; --accent-soft: #1f3329; --danger: #e0756c;
    --male: #7aa6d6; --female: #db8aa8; --unknown: #9aa09c; --edge: #59625c;
    --card-shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 14px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141816; --surface: #1d2320; --surface-2: #232a26; --text: #e7ece8; --muted: #9aa59e;
  --line: #333c37; --accent: #5fb38a; --accent-2: #74c79e; --accent-soft: #1f3329; --danger: #e0756c;
  --male: #7aa6d6; --female: #db8aa8; --unknown: #9aa09c; --edge: #59625c;
  --card-shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 14px rgba(0, 0, 0, .25);
  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, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; }
[hidden] { display: none !important; }

/* ---------- кнопки и поля ---------- */
button, .btn {
  font: inherit; cursor: pointer; border: 1px solid var(--line); background: var(--surface);
  color: var(--text); border-radius: 9px; padding: 8px 14px; min-height: 38px;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none;
  transition: background .15s, border-color .15s;
}
button:hover, .btn:hover { border-color: var(--accent); }
button:disabled { opacity: .55; cursor: default; }
.btn-primary, button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover, button.primary:hover { background: var(--accent-2); }
button.danger { color: var(--danger); }
button.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
button.ghost { background: transparent; border-color: transparent; }
button.ghost:hover { background: var(--surface-2); }
button.icon { padding: 6px; width: 38px; }
button.small { min-height: 30px; padding: 4px 10px; font-size: 13px; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); width: 100%;
  border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px; min-height: 40px;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
textarea { min-height: 110px; resize: vertical; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field { margin-bottom: 12px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row > .field { flex: 1 1 160px; }
.check { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 15px; }
.check input { width: auto; min-height: 0; }
.muted { color: var(--muted); }
.small-text { font-size: 13px; }
.error-text { color: var(--danger); }

/* ---------- верхняя панель ---------- */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--top-h); z-index: 20;
  display: flex; align-items: center; gap: 10px; padding: 0 12px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 17px; white-space: nowrap; display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; }
.brand .logo { font-size: 22px; }
.spacer { flex: 1; }
.search { position: relative; flex: 0 1 320px; }
.search input { padding-left: 34px; }
.search::before { content: "🔍"; position: absolute; left: 10px; top: 50%; transform: translateY(-50%); font-size: 14px; opacity: .6; }
.search-results {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; max-height: 60vh; overflow: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--card-shadow); z-index: 30;
}
.search-results button { width: 100%; justify-content: flex-start; border: 0; border-radius: 0; text-align: left; }
.search-results button:hover, .search-results button.active { background: var(--accent-soft); }

.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 220px; z-index: 40;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--card-shadow); padding: 6px;
}
.menu-list button, .menu-list a { width: 100%; justify-content: flex-start; border: 0; background: none; }
.menu-list button:hover, .menu-list a:hover { background: var(--surface-2); }
.menu-list .who { padding: 8px 12px; font-size: 13px; color: var(--muted); border-bottom: 1px solid var(--line); margin-bottom: 4px; word-break: break-all; }

/* ---------- холст древа ---------- */
#stage { position: fixed; inset: var(--top-h) 0 0 0; overflow: hidden; touch-action: none; cursor: grab; }
#stage.dragging { cursor: grabbing; }
#stage svg { width: 100%; height: 100%; display: block; user-select: none; -webkit-user-select: none; }
.edge { fill: none; stroke: var(--edge); stroke-width: 1.6; }
.edge.union { stroke-width: 2.2; }
.edge.divorced { stroke-dasharray: 5 4; }
.card rect.bg { fill: var(--surface); stroke: var(--line); stroke-width: 1; }
.card { cursor: pointer; }
.card:hover rect.bg { stroke: var(--accent); }
.card.selected rect.bg { stroke: var(--accent); stroke-width: 2.5; }
.card.focus rect.bg { fill: var(--accent-soft); }
.card .stripe.M { fill: var(--male); }
.card .stripe.F { fill: var(--female); }
.card .stripe.U { fill: var(--unknown); }
.card .name { font-weight: 600; font-size: 13px; fill: var(--text); }
.card .sub { font-size: 11.5px; fill: var(--muted); }
.card .avatar-bg { fill: var(--surface-2); stroke: var(--line); }
.card .avatar-letter { font-size: 15px; font-weight: 600; fill: var(--muted); }
.card.deceased rect.bg { fill: var(--surface-2); }
.card .more { fill: var(--accent); font-size: 11px; font-weight: 700; }
.expander circle { fill: var(--surface); stroke: var(--accent); }
.expander text { fill: var(--accent); font-size: 13px; font-weight: 700; }
.expander { cursor: pointer; }

.zoom-ctl { position: fixed; right: 14px; bottom: 18px; display: flex; flex-direction: column; gap: 6px; z-index: 15; }
.zoom-ctl button { width: 42px; height: 42px; padding: 0; font-size: 18px; box-shadow: var(--card-shadow); border-radius: 50%; }
.depth-ctl {
  position: fixed; left: 14px; bottom: 18px; z-index: 15; display: flex; gap: 6px; align-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: 22px; padding: 4px 10px; box-shadow: var(--card-shadow); font-size: 13px;
}
.depth-ctl select { width: auto; min-height: 30px; padding: 2px 6px; border: 0; background: transparent; }

.empty {
  position: fixed; inset: var(--top-h) 0 0 0; display: grid; place-items: center; text-align: center; padding: 24px;
}
.empty .box { max-width: 420px; }
.empty .big { font-size: 64px; }

/* ---------- панель человека (справа / снизу на телефоне) ---------- */
.panel {
  position: fixed; top: calc(var(--top-h) + 10px); right: 10px; bottom: 10px; width: 380px; z-index: 25;
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--card-shadow);
  display: flex; flex-direction: column; overflow: hidden;
  transform: translateX(calc(100% + 20px)); transition: transform .22s ease;
}
.panel.open { transform: none; }
.panel-head { display: flex; align-items: center; gap: 8px; padding: 10px 10px 0 16px; }
.panel-head .grab { display: none; }
.panel-body { overflow: auto; padding: 8px 16px 20px; flex: 1; -webkit-overflow-scrolling: touch; }
.person-hero { display: flex; gap: 14px; align-items: center; margin-bottom: 10px; }
.person-hero .photo {
  width: 84px; height: 84px; border-radius: 50%; object-fit: cover; flex: none; background: var(--surface-2);
  display: grid; place-items: center; font-size: 30px; color: var(--muted); border: 3px solid var(--line);
}
.person-hero .photo.M { border-color: var(--male); }
.person-hero .photo.F { border-color: var(--female); }
.person-hero h2 { margin: 0 0 4px; font-size: 20px; }
.facts { margin: 10px 0; display: grid; gap: 6px; }
.fact { display: flex; gap: 8px; font-size: 14px; }
.fact .k { color: var(--muted); min-width: 92px; }
.bio { white-space: pre-wrap; background: var(--surface-2); border-radius: 10px; padding: 10px 12px; font-size: 14px; }
.rel-group { margin-top: 14px; }
.rel-group h4 { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 6px 4px 10px; min-height: 32px;
  border-radius: 16px; border: 1px solid var(--line); background: var(--surface-2); font-size: 13px; cursor: pointer;
}
.chip:hover { border-color: var(--accent); }
.chip .x { border: 0; background: none; min-height: 0; padding: 0 4px; color: var(--muted); font-size: 14px; }
.chip .x:hover { color: var(--danger); }
.chip .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--unknown); }
.chip .dot.M { background: var(--male); }
.chip .dot.F { background: var(--female); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.actions.grid { display: grid; grid-template-columns: 1fr 1fr; }

/* ---------- модальные окна ---------- */
.modal-back {
  position: fixed; inset: 0; z-index: 50; background: rgba(15, 20, 17, .45);
  display: grid; place-items: center; padding: 16px; animation: fade .15s;
}
.modal {
  background: var(--surface); border-radius: 16px; width: min(560px, 100%); max-height: calc(100dvh - 32px);
  display: flex; flex-direction: column; box-shadow: 0 10px 40px rgba(0, 0, 0, .25);
}
.modal-head { display: flex; align-items: center; padding: 14px 16px 6px 20px; }
.modal-head h3 { margin: 0; flex: 1; font-size: 18px; }
.modal-body { padding: 8px 20px; overflow: auto; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 20px 16px; flex-wrap: wrap; }
@keyframes fade { from { opacity: 0; } }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 90; max-width: calc(100% - 32px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; box-shadow: var(--card-shadow);
  animation: fade .15s;
}
.toast.err { background: var(--danger); color: #fff; }

/* ---------- список людей ---------- */
.list-item { display: flex; align-items: center; gap: 10px; padding: 8px 4px; border-bottom: 1px solid var(--line); cursor: pointer; }
.list-item:hover { background: var(--surface-2); }
.list-item .av { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; background: var(--surface-2); display: grid; place-items: center; flex: none; color: var(--muted); }

/* ---------- страница входа ---------- */
.center-page { min-height: 100dvh; display: grid; place-items: center; padding: 20px; }
.auth-card { width: min(400px, 100%); background: var(--surface); border-radius: 18px; padding: 28px 24px; box-shadow: var(--card-shadow); border: 1px solid var(--line); }
.auth-card h1 { margin: 0 0 6px; font-size: 24px; text-align: center; }
.auth-card .logo { font-size: 54px; text-align: center; }
.code-input { font-size: 28px; letter-spacing: 10px; text-align: center; font-variant-numeric: tabular-nums; }
.auth-card .primary { width: 100%; margin-top: 4px; min-height: 44px; }
.notice { background: var(--accent-soft); border-radius: 10px; padding: 10px 12px; font-size: 14px; margin: 10px 0; }
.notice.warn { background: color-mix(in srgb, #e7b84a 22%, var(--surface)); }

/* ---------- телефон ---------- */
@media (max-width: 720px) {
  :root { --top-h: 52px; }
  .brand .title { display: none; }
  .search { flex: 1 1 auto; }
  .hide-mobile { display: none !important; }
  .panel {
    top: auto; left: 0; right: 0; bottom: 0; width: auto; max-height: 78dvh; height: 78dvh;
    border-radius: 18px 18px 0 0; border-bottom: 0; transform: translateY(105%);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .panel.open { transform: none; }
  .panel.peek { height: 46dvh; }
  .panel-head { position: relative; padding-top: 8px; }
  .panel-head .grab { display: block; position: absolute; left: 0; right: 56px; top: 0; height: 30px; cursor: grab; touch-action: none; }
  .panel-head .grab::after { content: ""; position: absolute; left: calc(50% + 28px); top: 8px; width: 42px; height: 5px; border-radius: 3px; background: var(--line); transform: translateX(-50%); }
  .zoom-ctl { bottom: calc(18px + env(safe-area-inset-bottom)); }
  .zoom-ctl .hide-mobile { display: none; }
  .depth-ctl { bottom: calc(18px + env(safe-area-inset-bottom)); }
  .modal-back { padding: 0; place-items: end stretch; }
  .modal { width: 100%; border-radius: 18px 18px 0 0; max-height: 92dvh; padding-bottom: env(safe-area-inset-bottom); }
  .actions.grid { grid-template-columns: 1fr 1fr; }
}

/* на десктопе кнопки масштаба сдвигаются, чтобы не прятаться под открытой карточкой */
@media (min-width: 721px) {
  body:has(.panel.open) .zoom-ctl { right: 404px; }
}
