:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --border: #e6eaf2;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #6366f1;
  --accent-soft: #eef0ff;
  --male: #2f6fe0;
  --female: #db3f8f;
  --shadow: 0 18px 40px -24px rgba(15, 23, 42, 0.35);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 12px 22px;
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 13px;
  display: grid;
  place-items: center;
  color: #fff;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  box-shadow: 0 10px 22px -10px rgba(99, 102, 241, 0.9);
}

.brand-mark svg {
  width: 24px;
  height: 24px;
}

.brand-text h1 {
  font-size: 16px;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.01em;
}

.brand-sub {
  font: inherit;
  font-size: 12px;
  color: var(--muted);
  background: transparent;
  border: 1px dashed transparent;
  border-radius: 6px;
  padding: 2px 6px;
  margin: 0 -6px;
  min-width: 0;
  max-width: 280px;
  transition: border-color 0.2s, background 0.2s, box-shadow 0.2s;
}

.brand-sub:hover {
  border-color: var(--border);
  background: #f1f3f9;
}

.brand-sub:focus {
  outline: none;
  border-color: #c7d2fe;
  background: #fff;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.12);
}

.search {
  flex: 1;
  max-width: 460px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  height: 42px;
  border-radius: 12px;
  background: #f1f3f9;
  border: 1px solid transparent;
  transition: border-color 0.2s, background 0.2s;
}

.search:focus-within {
  background: #fff;
  border-color: #c7d2fe;
  box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.12);
}

.search svg {
  width: 18px;
  height: 18px;
  color: var(--muted);
  flex-shrink: 0;
}

.search input {
  flex: 1;
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  outline: none;
}

.search input::-webkit-search-cancel-button {
  display: none;
}

.search-clear {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  display: none;
}

.search input:not(:placeholder-shown) + .search-clear {
  display: block;
}

.topbar-actions {
  display: flex;
  gap: 8px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 42px;
  padding: 0 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s, border-color 0.15s;
}

.btn svg {
  width: 17px;
  height: 17px;
}

.btn:hover {
  border-color: #c7d2fe;
  box-shadow: 0 8px 18px -12px rgba(15, 23, 42, 0.5);
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(0);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

.btn:disabled:hover {
  border-color: var(--border);
  box-shadow: none;
  transform: none;
}

.workspace {
  flex: 1;
  display: flex;
  min-height: 0;
}

.canvas-wrap {
  position: relative;
  flex: 1;
  min-width: 0;
  background: radial-gradient(circle at 20% 15%, #eef1fb 0%, #f7f8fc 45%, #eef1f8 100%);
}

#treeCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
}

#treeCanvas:active {
  cursor: grabbing;
}

.legend {
  position: absolute;
  left: 18px;
  bottom: 18px;
  display: flex;
  gap: 16px;
  padding: 10px 14px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 12px;
  color: var(--muted);
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 7px;
}

.dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
}

.dot.male {
  background: linear-gradient(135deg, #4f8ff7, #2f6fe0);
}

.dot.female {
  background: linear-gradient(135deg, #f472b6, #db3f8f);
}

.line {
  width: 18px;
  height: 2px;
  border-radius: 2px;
  background: #c3ccda;
}

.zoom-controls {
  position: absolute;
  right: 18px;
  bottom: 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 6px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.zoom-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 0;
  background: transparent;
  color: #334155;
  font-size: 20px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.zoom-btn:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

.hint {
  position: absolute;
  left: 50%;
  top: 16px;
  transform: translateX(-50%);
  padding: 9px 16px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.82);
  color: #e2e8f0;
  font-size: 12.5px;
  white-space: nowrap;
  transition: opacity 0.6s, transform 0.6s;
}

.hint.fade {
  opacity: 0;
  transform: translateX(-50%) translateY(-12px);
  pointer-events: none;
}

.details {
  width: 350px;
  flex-shrink: 0;
  background: var(--panel);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  position: relative;
}

.details-empty {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px 30px;
  color: var(--muted);
}

.empty-icon {
  width: 64px;
  height: 64px;
  border-radius: 20px;
  display: grid;
  place-items: center;
  background: var(--accent-soft);
  color: var(--accent);
  margin-bottom: 18px;
}

.empty-icon svg {
  width: 32px;
  height: 32px;
}

.details-empty h3 {
  margin: 0 0 8px;
  color: var(--text);
  font-size: 16px;
}

.details-empty p {
  margin: 0;
  font-size: 13.5px;
  line-height: 1.6;
}

.details-content {
  padding: 24px 22px 30px;
}

.details-tools {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  margin: -6px 0 10px;
}

.icon-btn {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  border: 0;
  background: #f1f3f9;
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.icon-btn svg {
  width: 16px;
  height: 16px;
}

.icon-btn:hover {
  background: #e6eaf2;
  color: var(--text);
}

#editBtn:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

#deleteBtn:hover {
  background: #fee2e2;
  color: #dc2626;
}

.profile-head {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 20px;
}

.avatar {
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 700;
  border-radius: 50%;
  flex-shrink: 0;
}

.avatar-lg {
  width: 68px;
  height: 68px;
  font-size: 24px;
  box-shadow: 0 12px 24px -14px rgba(15, 23, 42, 0.8);
}

.avatar.male {
  background: linear-gradient(135deg, #4f8ff7, #2f6fe0);
}

.avatar.female {
  background: linear-gradient(135deg, #f472b6, #db3f8f);
}

.avatar.unknown {
  background: linear-gradient(135deg, #94a3b8, #64748b);
}

.profile-head h2 {
  margin: 0 0 4px;
  font-size: 19px;
  letter-spacing: -0.02em;
}

.lifespan {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 13px;
}

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
}

.badge.male {
  background: #e8f1ff;
  color: var(--male);
}

.badge.female {
  background: #fdeaf4;
  color: var(--female);
}

.badge.unknown {
  background: #eef1f6;
  color: var(--muted);
}

.info-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px 16px;
  margin: 0 0 18px;
  padding: 16px;
  border-radius: 14px;
  background: #f7f8fc;
  font-size: 13px;
}

.info-grid dt {
  color: var(--muted);
}

.info-grid dd {
  margin: 0;
  text-align: right;
  font-weight: 600;
}

.bio {
  margin: 0 0 22px;
  font-size: 13.5px;
  line-height: 1.65;
  color: #475569;
}

.relations {
  display: flex;
  flex-direction: column;
  gap: 18px;
  margin-bottom: 24px;
}

.relation-group h4 {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 9px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.add-btn {
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  padding: 0;
  border-radius: 6px;
  border: 1px dashed #c7d2fe;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 15px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s, transform 0.15s;
}

.add-btn:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  transform: scale(1.08);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 5px 12px 5px 5px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #fff;
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s, box-shadow 0.15s;
}

.chip:hover {
  transform: translateY(-1px);
  border-color: #c7d2fe;
  box-shadow: 0 8px 16px -12px rgba(15, 23, 42, 0.6);
}

.chip-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 10px;
  color: #fff;
  background: #64748b;
}

.chip.male .chip-avatar {
  background: linear-gradient(135deg, #4f8ff7, #2f6fe0);
}

.chip.female .chip-avatar {
  background: linear-gradient(135deg, #f472b6, #db3f8f);
}

.chip-year {
  font-weight: 500;
  color: #94a3b8;
  white-space: nowrap;
}

.chip-empty {
  font-size: 12.5px;
  color: #94a3b8;
  font-style: italic;
}

.focus-btn {
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  height: 46px;
  border: 0;
  border-radius: 13px;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: #fff;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 14px 26px -14px rgba(99, 102, 241, 0.9);
  transition: transform 0.15s, box-shadow 0.15s;
}

.focus-btn svg {
  width: 18px;
  height: 18px;
}

.focus-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 18px 30px -14px rgba(99, 102, 241, 0.95);
}

.btn-primary {
  border: 0;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: #fff;
  box-shadow: 0 12px 22px -14px rgba(99, 102, 241, 0.95);
}

.btn-primary:hover {
  border-color: transparent;
  box-shadow: 0 16px 26px -14px rgba(99, 102, 241, 0.95);
}

.btn-danger {
  border: 0;
  background: linear-gradient(135deg, #ef4444, #f43f5e);
  color: #fff;
  box-shadow: 0 12px 22px -14px rgba(239, 68, 68, 0.9);
}

.btn-danger:hover {
  border-color: transparent;
  box-shadow: 0 16px 26px -14px rgba(239, 68, 68, 0.95);
}

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(4px);
  animation: fade-in 0.18s ease;
}

.modal {
  width: min(560px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  background: #fff;
  border-radius: 20px;
  box-shadow: 0 40px 80px -30px rgba(15, 23, 42, 0.6);
  animation: pop-in 0.2s ease;
}

.modal-sm {
  width: min(420px, 100%);
}

.confirm-message {
  margin: 0;
  font-size: 13.5px;
  line-height: 1.55;
  color: #475569;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 22px 0;
}

.modal-head h3 {
  margin: 0;
  font-size: 17px;
  letter-spacing: -0.01em;
}

.modal-body {
  padding: 18px 22px 22px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.modal-body label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12.5px;
  font-weight: 600;
  color: #475569;
}

.modal-body input,
.modal-body select,
.modal-body textarea {
  font: inherit;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text);
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #f9fafc;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}

.modal-body textarea {
  resize: vertical;
  min-height: 76px;
}

.modal-body input:focus,
.modal-body select:focus,
.modal-body textarea:focus {
  background: #fff;
  border-color: #c7d2fe;
  box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.12);
}

.form-error {
  margin: 0;
  padding: 9px 12px;
  border-radius: 10px;
  background: #fef2f2;
  color: #dc2626;
  font-size: 12.5px;
  font-weight: 500;
}

.form-note {
  margin: 0;
  padding: 9px 12px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: #4f46e5;
  font-size: 12.5px;
  font-weight: 500;
}

.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 4px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%) translateY(12px);
  z-index: 200;
  padding: 11px 20px;
  border-radius: 12px;
  background: #0f172a;
  color: #f8fafc;
  font-size: 13px;
  font-weight: 500;
  box-shadow: 0 20px 40px -20px rgba(15, 23, 42, 0.9);
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@keyframes fade-in {
  from { opacity: 0; }
}

@keyframes pop-in {
  from { opacity: 0; transform: translateY(12px) scale(0.98); }
}

.hub-tooltip {
  position: absolute;
  z-index: 6;
  pointer-events: none;
  transform: translate(-50%, -100%);
  background: #0f172a;
  color: #fff;
  font-size: 12.5px;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 8px;
  white-space: nowrap;
  box-shadow: 0 12px 24px -12px rgba(15, 23, 42, 0.7);
}

.hub-tooltip::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 100%;
  transform: translateX(-50%);
  border: 5px solid transparent;
  border-top-color: #0f172a;
}

@media (max-width: 900px) {
  .brand-text span,
  .brand-sub,
  .btn span {
    display: none;
  }

  .form-row {
    grid-template-columns: 1fr;
  }

  .btn {
    padding: 0 12px;
  }

  .details {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 20;
    width: min(350px, 88vw);
    box-shadow: -20px 0 50px -30px rgba(15, 23, 42, 0.7);
    transform: translateX(105%);
    transition: transform 0.28s ease;
  }

  .details.open {
    transform: translateX(0);
  }

  .legend {
    display: none;
  }
}
