:root {
  --bg: #f6f3ec;
  --surface: #ffffff;
  --surface-2: #efeadf;
  --text: #24261f;
  --muted: #6c6e64;
  --line: #d8d1c2;
  --accent: #3f6b4f;
  --accent-text: #ffffff;
  --male: #4f7bab;
  --female: #b8607f;
  --unknown: #8f8a7c;
  --danger: #b3412f;
  --tree-line: #8d8676;
  --selected: #e0a82e;
  --shadow: 0 1px 3px rgba(40, 35, 20, .12), 0 4px 14px rgba(40, 35, 20, .08);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1d19;
    --surface: #262923;
    --surface-2: #30332c;
    --text: #ebe8de;
    --muted: #a3a497;
    --line: #42463d;
    --accent: #7fb08e;
    --accent-text: #11140f;
    --male: #7ea6d6;
    --female: #df8fab;
    --unknown: #a8a395;
    --danger: #e0806e;
    --tree-line: #7c7a6e;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
[hidden] { display: none !important; }
h1, h2, h3 { line-height: 1.2; margin: 0; }
h2 { font-size: 1.2rem; }
h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 6px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; }

button, input, select, textarea { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  min-height: 36px;
}
button:hover { border-color: var(--accent); }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .card:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
button.link { border: 0; background: none; padding: 2px 4px; min-height: 0; color: var(--accent); text-align: left; }
button.link:hover { text-decoration: underline; }
button.icon { width: 36px; padding: 0; font-size: 1.2rem; line-height: 1; }
input, select, textarea {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 8px;
  padding: 7px 9px;
  min-height: 38px;
  width: 100%;
}
textarea { resize: vertical; }
label { display: flex; flex-direction: column; gap: 4px; font-size: .9rem; color: var(--muted); }
label > input, label > select, label > textarea { color: var(--text); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
label.check input { width: auto; min-height: 0; }

/* --- вход --- */
#login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-box {
  background: var(--surface); padding: 28px; border-radius: 16px; box-shadow: var(--shadow);
  width: min(360px, 100%); display: flex; flex-direction: column; gap: 14px; text-align: center; align-items: center;
}
.login-box label, .login-box button { width: 100%; text-align: left; }
.login-box button { text-align: center; }

/* --- каркас --- */
#app { height: 100%; display: flex; flex-direction: column; }
.top {
  display: flex; align-items: center; gap: 16px; padding: 8px 16px; background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; white-space: nowrap; }
.tabs { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.tabs button { border: 0; background: none; border-radius: 8px; white-space: nowrap; }
.tabs button[aria-selected="true"] { background: var(--surface-2); font-weight: 600; }
main { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr 380px; position: relative; }
.view { min-width: 0; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.foot { padding: 2px 16px; text-align: right; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.toolbar label { flex-direction: row; align-items: center; gap: 6px; }
.toolbar select { width: auto; }
.toolbar input[type="search"] { flex: 1; min-width: 180px; max-width: 420px; }
.tree-root-label { margin-right: auto; }
.zoom { display: inline-flex; gap: 4px; }

/* --- дерево --- */
.tree { flex: 1; min-height: 0; position: relative; overflow: hidden; touch-action: none; background:
  radial-gradient(circle, color-mix(in srgb, var(--line) 60%, transparent) 1px, transparent 1px) 0 0 / 22px 22px; }
.tree-svg { width: 100%; height: 100%; display: block; cursor: grab; user-select: none; -webkit-user-select: none; }
.tree-svg:active { cursor: grabbing; }
.hint { margin: 0; padding: 4px 16px 8px; font-size: .8rem; color: var(--muted); }
.line { fill: none; stroke: var(--tree-line); stroke-width: 1.6; }
.line-couple { stroke-width: 2.4; }
.line-adopted { stroke-dasharray: 6 4; }
.card { cursor: pointer; }
.card-bg { fill: var(--surface); stroke: var(--line); stroke-width: 1; filter: drop-shadow(0 1px 2px rgba(40, 35, 20, .15)); }
.card-m .card-stripe { fill: var(--male); }
.card-f .card-stripe { fill: var(--female); }
.card-u .card-stripe { fill: var(--unknown); }
.card-root .card-bg { stroke: var(--accent); stroke-width: 2.2; }
.card-selected .card-bg { stroke: var(--selected); stroke-width: 3; }
.card:hover .card-bg { stroke: var(--accent); }
.card-name { font-size: 13.5px; font-weight: 600; fill: var(--text); }
.card-dates { font-size: 12px; fill: var(--muted); }
.card-dead .card-bg { fill: var(--surface-2); }
.more-dot { fill: var(--surface); stroke: var(--tree-line); }
.more-sym { font-size: 9px; fill: var(--muted); }

/* --- карточка человека --- */
.panel { background: var(--surface); border-left: 1px solid var(--line); overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 16px; }
.panel-head { display: flex; gap: 12px; align-items: flex-start; position: relative; padding-right: 30px; }
.panel-photo { width: 72px; height: 90px; object-fit: cover; border-radius: 8px; cursor: zoom-in; flex: none; }
.close-panel { position: absolute; right: -6px; top: -6px; display: none; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }
.actions.small button { min-height: 30px; padding: 3px 10px; font-size: .85rem; }
.facts { display: grid; gap: 4px; }
.fact { display: grid; grid-template-columns: 90px 1fr; gap: 8px; }
.fact-label { color: var(--muted); }
.notes { white-space: pre-wrap; background: var(--surface-2); padding: 10px 12px; border-radius: 8px; }
.rel-list { list-style: none; margin: 0 0 6px; padding: 0; display: grid; gap: 2px; }
.rel { color: var(--muted); }
.family-box { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; margin-bottom: 8px; display: grid; gap: 4px; }
.danger-zone { border-top: 1px solid var(--line); padding-top: 12px; }

/* --- люди --- */
.people-list { list-style: none; margin: 0; padding: 8px 16px; overflow-y: auto; display: grid; gap: 4px; align-content: start; }
.person-row { display: flex; gap: 10px; align-items: center; width: 100%; text-align: left; border-color: transparent; background: none; padding: 6px 8px; }
.person-row:hover, .person-row.active { background: var(--surface); border-color: var(--line); }
.person-row img, .avatar { width: 40px; height: 40px; border-radius: 50%; object-fit: cover; flex: none; }
.avatar { display: grid; place-items: center; color: #fff; font-weight: 700; background: var(--unknown); }
.avatar-M { background: var(--male); }
.avatar-F { background: var(--female); }
.person-row-text { display: flex; flex-direction: column; min-width: 0; }

/* --- фото --- */
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.thumbs.big { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); padding: 16px; overflow-y: auto; align-content: start; }
#view-media.drag { outline: 3px dashed var(--accent); outline-offset: -8px; }
.thumb { padding: 0; overflow: hidden; display: flex; flex-direction: column; aspect-ratio: 3 / 4; }
.thumb img { width: 100%; flex: 1; min-height: 0; object-fit: cover; }
.thumb-title { font-size: .75rem; padding: 3px 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 100%; }
.doc-icon { flex: 1; display: grid; place-items: center; font-weight: 700; color: var(--danger); background: var(--surface-2); width: 100%; }
.media-view { text-align: center; background: var(--surface-2); border-radius: 8px; }
.media-view img { max-width: 100%; max-height: 60vh; display: block; margin: 0 auto; }
.pdf-view { width: 100%; height: 60vh; border: 0; }
.chips { list-style: none; margin: 0 0 6px; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--line); border-radius: 999px; padding: 2px 4px 2px 10px; }
.chip .icon { width: 26px; min-height: 26px; border: 0; }

/* --- диалоги --- */
.dlg { border: 0; border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.dlg.wide { width: min(820px, calc(100vw - 24px)); }
.dlg::backdrop { background: rgba(20, 18, 10, .45); }
.dlg form { display: flex; flex-direction: column; max-height: calc(100dvh - 24px); }
.dlg header, .dlg footer { display: flex; align-items: center; gap: 8px; padding: 12px 16px; }
.dlg header { border-bottom: 1px solid var(--line); justify-content: space-between; }
.dlg footer { border-top: 1px solid var(--line); flex-wrap: wrap; }
.dlg-body { padding: 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.spacer { flex: 1; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.span2 { grid-column: 1 / -1; }
.subhead { font-weight: 600; margin-top: 6px; border-top: 1px solid var(--line); padding-top: 8px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > .label { font-size: .9rem; color: var(--muted); }
.date-row { display: flex; gap: 6px; align-items: center; }
.date-row select { width: auto; flex: none; }
.date-row input { flex: 1; min-width: 0; }
.date-row .and { color: var(--muted); }
.choose-existing { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.pick-list { list-style: none; margin: 0; padding: 0; max-height: 50vh; overflow-y: auto; }
.partner-row, .kid-row { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.kid-row select { width: auto; }

/* --- пустое дерево, уведомления --- */
.empty { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; text-align: center; padding: 16px; background: var(--bg); }
.toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 100; width: min(420px, calc(100vw - 32px)); }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); }
.toast-error { background: var(--danger); color: #fff; }

/* --- телефон --- */
@media (max-width: 860px) {
  main { grid-template-columns: 1fr; }
  .brand span { display: none; }
  .top { gap: 8px; padding: 6px 12px; }
  .toolbar { padding: 8px 12px; }
  .tree-root-label { width: 100%; }
  .panel {
    position: fixed; left: 0; right: 0; bottom: 0; max-height: 75dvh; z-index: 20;
    border-left: 0; border-top: 1px solid var(--line); border-radius: 16px 16px 0 0; box-shadow: 0 -6px 24px rgba(0, 0, 0, .2);
    transform: translateY(105%); transition: transform .2s ease; padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }
  body.panel-open .panel { transform: none; }
  .close-panel { display: block; }
  .form-grid { grid-template-columns: 1fr; }
  .hint { display: none; }
  .foot { display: none; }
}
