:root {
  color-scheme: light;
  --bg: #f5f1e8;
  --ink: #202124;
  --muted: #6c665d;
  --line: #d4c8b5;
  --panel: #fffaf1;
  --accent: #2f6f5e;
  --accent-2: #8f3f46;
  --soft: #e9dcc8;
  --danger: #b42f2f;
  font-family: Inter, "Segoe UI", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
}

button,
input,
select {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fffdf8;
  color: var(--ink);
  cursor: pointer;
  min-height: 38px;
  padding: 8px 12px;
}

button:hover {
  border-color: var(--accent);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 20px 24px;
  border-bottom: 1px solid var(--line);
  background: #fbf6ec;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(26px, 4vw, 42px);
  line-height: 1.05;
}

.topbar p {
  margin: 8px 0 0;
  color: var(--muted);
}

.topbar__actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.admin-status {
  align-self: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--soft);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  padding: 8px 12px;
}

body:not(.is-admin) .admin-only {
  display: none;
}

body:not(.can-manage-data) .manage-only {
  display: none;
}

body.is-admin #adminGate {
  display: none;
}

.shell {
  display: grid;
  grid-template-columns: minmax(300px, 360px) 1fr;
  gap: 16px;
  padding: 16px;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 8px 24px rgb(60 45 25 / 7%);
}

.editor {
  display: grid;
  gap: 16px;
  align-content: start;
  padding: 16px;
  max-height: calc(100vh - 122px);
  overflow: auto;
}

.admin-gate {
  display: grid;
  align-content: center;
  min-height: 260px;
  padding: 22px;
  color: var(--muted);
}

.admin-gate h2 {
  margin: 0 0 8px;
  color: var(--ink);
  font-size: 20px;
}

.admin-gate p {
  margin: 0;
  line-height: 1.5;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.section-title h2 {
  margin: 0;
  font-size: 17px;
}

#countBadge {
  min-width: 28px;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--soft);
  text-align: center;
  color: var(--muted);
}

form {
  display: grid;
  gap: 11px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

input,
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fffdf8;
  color: var(--ink);
  min-height: 38px;
  padding: 8px 10px;
}

select[multiple] {
  min-height: 128px;
}

.photo-tools {
  display: flex;
  align-items: center;
  gap: 10px;
}

#photoPreview {
  width: 58px;
  height: 58px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--soft);
  object-fit: cover;
}

#photoPreview.empty {
  display: none;
}

.photo-tools button {
  flex: 1;
}

.form-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

#saveBtn {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.danger {
  color: var(--danger);
}

.people-list {
  display: grid;
  gap: 8px;
}

.person-row {
  display: grid;
  width: 100%;
  text-align: left;
  gap: 4px;
}

.person-row.active {
  border-color: var(--accent);
  background: #edf6f1;
}

.person-row__name {
  font-weight: 700;
}

.person-row__meta {
  color: var(--muted);
  font-size: 12px;
}

.workspace {
  display: grid;
  grid-template-rows: auto minmax(520px, 1fr);
  gap: 16px;
  min-width: 0;
}

.toolbar {
  display: flex;
  align-items: end;
  gap: 12px;
  padding: 12px;
}

.toolbar label {
  min-width: 180px;
}

.toolbar button {
  margin-left: auto;
}

.tree-wrap {
  overflow: auto;
  padding: 32px;
  min-height: 520px;
  background:
    linear-gradient(90deg, rgb(47 111 94 / 7%) 1px, transparent 1px),
    linear-gradient(rgb(47 111 94 / 7%) 1px, transparent 1px),
    var(--panel);
  background-size: 28px 28px;
}

body.is-map-view .tree-wrap,
body:has(#viewSelect option[value="map"]:checked) .tree-wrap {
  background: #151515;
}

.tree {
  min-width: max-content;
  display: flex;
  justify-content: center;
  padding: 12px;
}

.map-stage {
  position: relative;
  min-width: 100%;
  min-height: 100%;
}

.map-lines {
  position: absolute;
  inset: 0;
  overflow: visible;
}

.map-lines path {
  fill: none;
  stroke: rgb(132 132 132 / 55%);
  stroke-width: 3;
  stroke-linecap: round;
}

.map-node {
  position: absolute;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 8px;
  width: 150px;
  min-height: 58px;
  border: 2px solid transparent;
  border-radius: 999px;
  background: #99bd92;
  color: white;
  font-size: 13px;
  font-weight: 800;
  text-align: center;
  box-shadow: 0 6px 14px rgb(0 0 0 / 25%);
}

.map-node:hover,
.map-node.is-active {
  border-color: #f3a12b;
}

.map-node span {
  overflow-wrap: anywhere;
}

.map-node b,
.map-node em {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: rgb(0 0 0 / 18%);
  font-style: normal;
}

.map-node em {
  width: auto;
  min-width: 58px;
  border-radius: 999px;
  color: #fff6f2;
  font-size: 10px;
  padding: 0 6px;
}

.tree ul {
  position: relative;
  display: flex;
  justify-content: center;
  gap: 18px;
  padding: 34px 0 0;
  margin: 0;
  list-style: none;
}

.tree li {
  position: relative;
  display: grid;
  justify-items: center;
  padding: 0 10px;
}

.tree li::before,
.tree li::after {
  content: "";
  position: absolute;
  top: 0;
  width: 50%;
  height: 24px;
  border-top: 2px solid var(--line);
}

.tree li::before {
  right: 50%;
}

.tree li::after {
  left: 50%;
  border-left: 2px solid var(--line);
}

.tree li:only-child::before,
.tree li:only-child::after {
  display: none;
}

.tree li:first-child::before,
.tree li:last-child::after {
  border-top: 0;
}

.tree ul::before {
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  height: 34px;
  border-left: 2px solid var(--line);
}

.tree > ul {
  padding-top: 0;
}

.tree > ul::before {
  display: none;
}

.node {
  position: relative;
  z-index: 1;
  min-width: 168px;
  max-width: 220px;
  border: 1px solid var(--line);
  border-top: 4px solid var(--accent);
  border-radius: 8px;
  background: #fffdf8;
  padding: 12px;
  text-align: center;
  box-shadow: 0 10px 18px rgb(60 45 25 / 8%);
  cursor: pointer;
}

.node__toggle {
  position: absolute;
  top: 6px;
  right: 6px;
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  min-height: 26px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  font-size: 18px;
  font-weight: 800;
  line-height: 1;
  padding: 0;
}

.node__photo {
  display: grid;
  place-items: center;
  width: 58px;
  height: 58px;
  margin: 0 auto 8px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--soft);
  color: var(--accent);
  font-weight: 800;
  object-fit: cover;
}

.node__photo--empty {
  font-size: 18px;
}

.node__name {
  font-weight: 800;
  overflow-wrap: anywhere;
}

.node__years,
.node__contact {
  margin-top: 5px;
  color: var(--muted);
  font-size: 12px;
  overflow-wrap: anywhere;
}

.node__contact {
  color: var(--ink);
}

.line-break {
  position: relative;
  margin-top: 18px;
  border: 1px dashed var(--danger);
  border-radius: 8px;
  background: #fff4f1;
  color: var(--danger);
  font-size: 13px;
  font-weight: 700;
  padding: 9px 12px;
  text-align: center;
  max-width: 220px;
}

.line-break::before {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 100%;
  height: 18px;
  border-left: 2px dashed var(--danger);
}

.empty-state {
  display: grid;
  place-items: center;
  width: min(620px, 80vw);
  min-height: 360px;
  color: var(--muted);
  text-align: center;
  line-height: 1.5;
}

.branch-list {
  display: grid;
  gap: 6px;
  width: min(760px, calc(100vw - 48px));
  min-width: min(760px, calc(100vw - 48px));
}

.branch-list__item {
  display: grid;
  gap: 6px;
}

.branch-list__row {
  display: grid;
  grid-template-columns: 24px 1fr auto;
  align-items: center;
  gap: 8px;
  width: 100%;
  margin-left: calc(var(--depth) * 18px);
  border-left: 4px solid var(--accent);
  text-align: left;
}

.branch-list__row span {
  color: var(--accent);
  font-size: 18px;
  font-weight: 900;
  text-align: center;
}

.branch-list__row em {
  color: var(--danger);
  font-size: 12px;
  font-style: normal;
  font-weight: 800;
}

@media (max-width: 900px) {
  .topbar,
  .shell {
    display: grid;
  }

  .shell {
    grid-template-columns: 1fr;
  }

  .editor {
    max-height: none;
  }

  .toolbar {
    position: sticky;
    top: 0;
    z-index: 5;
    align-items: stretch;
    flex-wrap: wrap;
  }

  .toolbar label {
    min-width: min(240px, 100%);
    flex: 1;
  }

  .toolbar button {
    margin-left: 0;
  }

  .workspace {
    grid-template-rows: auto minmax(70vh, 1fr);
  }

  .tree-wrap {
    min-height: 70vh;
    padding: 12px;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .tree {
    justify-content: flex-start;
    padding: 0;
  }

  .map-node {
    width: 138px;
    min-height: 54px;
    font-size: 12px;
  }

  .branch-list {
    width: calc(100vw - 68px);
    min-width: calc(100vw - 68px);
  }

  .branch-list__row {
    margin-left: calc(var(--depth) * 10px);
    grid-template-columns: 22px minmax(0, 1fr);
  }

  .branch-list__row em {
    grid-column: 2;
  }

  .tree ul {
    gap: 10px;
    padding-top: 28px;
  }

  .tree li {
    padding: 0 4px;
  }

  .node {
    min-width: 128px;
    max-width: 150px;
    padding: 10px 8px;
  }

  .node__photo {
    width: 42px;
    height: 42px;
    margin-bottom: 6px;
  }

  .node__name {
    font-size: 13px;
  }

  .node__years,
  .node__contact {
    font-size: 11px;
  }

  .line-break {
    max-width: 150px;
    font-size: 11px;
    padding: 7px 8px;
  }
}

@media print {
  .topbar__actions,
  .editor,
  .toolbar {
    display: none;
  }

  body {
    background: white;
  }

  .topbar,
  .shell,
  .tree-wrap {
    border: 0;
    box-shadow: none;
    padding: 0;
  }

  .shell {
    display: block;
  }
}
