:root {
  --bg: #f5f1e8;
  --paper: #fffdf8;
  --ink: #2c2620;
  --muted: #8a7f6d;
  --line: #c9bda3;
  --accent: #8a5a2b;
  --accent-dark: #6b4420;
  --danger: #b3402f;
  --shadow: 0 2px 8px rgba(60, 45, 25, 0.12);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Georgia", "Times New Roman", serif;
  /* Калмыцкая степь с тюльпанами — фон страницы */
  background: #3a4a2c url("/static/img/steppe.jpg") center top / cover fixed no-repeat;
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  /* шапка + область дерева заполняют экран, высота считается автоматически */
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------- Шапка ---------- */
.topbar {
  position: relative;
  flex: 0 0 auto;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: var(--accent);
  color: #fff;
  box-shadow: var(--shadow);
  overflow: hidden;
}
/* неброский калмыцкий орнамент (рога-завитки) поверх коричневой шапки */
.topbar::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: url("/static/img/ornament.svg") repeat-x left center;
  background-size: auto 82%;
  opacity: 0.28;
  z-index: 0;
}
.title, .controls { position: relative; z-index: 1; }
.title h1 { margin: 0; font-size: 22px; letter-spacing: 0.5px; }
.title .subtitle { font-size: 13px; opacity: 0.85; font-style: italic; }
.controls { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; justify-content: flex-end; }
.zoom {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 6px;
  padding: 2px 6px;
}
.zoom button {
  width: 26px; height: 26px;
  border: 0; border-radius: 5px;
  background: rgba(255, 255, 255, 0.2);
  color: #fff; font-size: 16px; cursor: pointer;
}
.zoom button:hover { background: rgba(255, 255, 255, 0.35); }
#zoom-label { font-size: 12px; min-width: 38px; text-align: center; }

.btn {
  border: 0;
  border-radius: 6px;
  padding: 8px 14px;
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
  background: #fff;
  color: var(--accent-dark);
  box-shadow: var(--shadow);
}
.btn:hover { background: #f3ead9; }
.btn-primary { background: var(--accent-dark); color: #fff; }
.btn-primary:hover { background: #51331a; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #93281a; }
.btn-small { padding: 5px 10px; font-size: 13px; }
#admin-controls { display: flex; gap: 8px; }
.hidden { display: none !important; }

/* ---------- Область дерева ---------- */
.viewport {
  width: 100%;
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: 40px 40px 120px;
  /* лёгкая вуаль (~12% непрозрачности), чтобы фото степи было видно за деревом */
  background: rgba(245, 241, 232, 0.12);
  /* одним пальцем — прокрутка, двумя — наш пинч-зум (не страничный) */
  touch-action: pan-x pan-y;
}
.tree {
  display: inline-block;
  min-width: 100%;
  transform-origin: top center;
  transition: transform 0.12s ease-out;
}
.empty {
  text-align: center;
  color: var(--muted);
  font-style: italic;
  margin-top: 80px;
  font-size: 18px;
}

/* ---------- CSS-дерево (сверху вниз) ---------- */
.tree ul {
  position: relative;
  padding-top: 24px;
  display: flex;
  justify-content: center;
  margin: 0;
  list-style: none;
}
.tree li {
  position: relative;
  padding: 24px 10px 0;
  text-align: center;
  list-style: none;
}
/* соединительные линии */
.tree li::before,
.tree li::after {
  content: "";
  position: absolute;
  top: 0;
  right: 50%;
  width: 50%;
  height: 24px;
  border-top: 2px solid var(--line);
}
.tree li::after {
  right: auto;
  left: 50%;
  border-left: 2px solid var(--line);
}
.tree li:only-child::before,
.tree li:only-child::after { display: none; }
.tree li:only-child { padding-top: 0; }
.tree li:first-child::before,
.tree li:last-child::after { border: 0 none; }
.tree li:last-child::before {
  border-right: 2px solid var(--line);
  border-radius: 0 6px 0 0;
}
.tree li:first-child::after { border-radius: 6px 0 0 0; }
.tree ul ul::before {
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  width: 0;
  height: 24px;
  border-left: 2px solid var(--line);
}
/* корни рода — без линий сверху, в ряд с разрывом */
.tree > ul.roots { gap: 50px; padding-top: 0; }
.tree > ul.roots > li { padding-top: 0; }
.tree > ul.roots > li::before,
.tree > ul.roots > li::after { display: none; }

/* ---------- Карточка человека ---------- */
.node {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 120px;
  padding: 10px 8px 12px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  cursor: default;
  position: relative;
  vertical-align: top;
}
.node.editable { cursor: pointer; }
.node.editable:hover { border-color: var(--accent); box-shadow: 0 4px 14px rgba(138, 90, 43, 0.25); }
.node .avatar {
  width: 76px; height: 76px;
  border-radius: 50%;
  object-fit: cover;
  background: #ece4d3;
  border: 2px solid var(--line);
}
.node .avatar.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  color: var(--muted);
}
.node .name { font-weight: bold; font-size: 14px; line-height: 1.2; }
.node .years { font-size: 12px; color: var(--muted); }
.node .add-child {
  position: absolute;
  bottom: -12px;
  left: 50%;
  transform: translateX(-50%);
  width: 24px; height: 24px;
  border-radius: 50%;
  border: 0;
  background: var(--accent-dark);
  color: #fff;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  box-shadow: var(--shadow);
  z-index: 2;
  display: none;
}
.node.editable .add-child { display: block; }
.node .add-child:hover { background: #51331a; }

/* ---------- Связующий узел (братья/сёстры без родителя) ---------- */
.junction {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--line);
  border: 3px solid var(--paper);
  box-shadow: 0 0 0 2px var(--line);
  margin: 0 auto;
}
.junction.editable { cursor: pointer; }
.junction.editable:hover {
  background: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
}

/* ---------- Модальное окно ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(40, 30, 15, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
}
.modal-box {
  background: var(--paper);
  border-radius: 12px;
  padding: 24px;
  width: 320px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}
.modal-box h2 { margin: 0 0 16px; }
.modal-box label { display: block; margin-bottom: 12px; font-size: 14px; }
.modal-box input {
  width: 100%;
  padding: 8px;
  margin-top: 4px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-family: inherit;
  font-size: 14px;
}
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.error { color: var(--danger); font-size: 13px; margin-bottom: 8px; }
.hint { font-size: 13px; color: var(--muted); margin: 0 0 12px; }
.check-list { max-height: 320px; overflow-y: auto; margin-bottom: 12px; }
.check-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 4px;
  font-size: 14px;
  cursor: pointer;
  border-radius: 5px;
}
.check-item:hover { background: #f3ead9; }
.check-item input { width: auto; margin: 0; }

/* ---------- Боковая панель ---------- */
.editor {
  position: fixed;
  top: 0; right: 0;
  width: 340px;
  height: 100vh;
  background: var(--paper);
  box-shadow: -4px 0 20px rgba(0, 0, 0, 0.18);
  z-index: 30;
  display: flex;
  flex-direction: column;
  transform: translateX(0);
}
.editor-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--accent);
  color: #fff;
}
.editor-head h2 { margin: 0; font-size: 18px; }
.icon-btn {
  border: 0; background: transparent; color: #fff;
  font-size: 18px; cursor: pointer;
}
.editor-form { padding: 20px; overflow-y: auto; flex: 1; }
.editor-form label { display: block; margin-bottom: 14px; font-size: 14px; }
.editor-form input,
.editor-form select,
.editor-form textarea {
  width: 100%;
  padding: 8px;
  margin-top: 4px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-family: inherit;
  font-size: 14px;
  background: #fff;
}
.editor-form .row { display: flex; gap: 10px; }
.editor-form .row label { flex: 1; }
.photo-block {
  display: flex;
  align-items: center;
  gap: 14px;
  margin: 8px 0 18px;
  padding: 12px;
  background: #f3ead9;
  border-radius: 8px;
}
.photo-preview {
  width: 72px; height: 72px;
  border-radius: 50%;
  background: #ece4d3 center/cover no-repeat;
  border: 2px solid var(--line);
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 26px; color: var(--muted);
}
.photo-actions { display: flex; flex-direction: column; gap: 8px; }

/* ---------- Телефоны ---------- */
@media (max-width: 560px) {
  .topbar { padding: 8px 12px; gap: 8px; }
  .title h1 { font-size: 18px; }
  .title .subtitle { font-size: 11px; }
  .controls { gap: 8px; }
  .btn { padding: 6px 10px; font-size: 13px; }
  .zoom { gap: 4px; padding: 2px 4px; }
  .zoom button { width: 24px; height: 24px; font-size: 15px; }
  #zoom-label { min-width: 32px; font-size: 11px; }
  .viewport { padding: 20px 14px 80px; }
  .editor { width: 100vw; }
}
.editor-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
}
