:root {
  --bg: #ececec;
  --dot: #c8c8c8;
  --ink: #111827;
  --muted: #6b7280;
  --card: #ffffff;
  --line: #1f2937;
  --accent: #7c3aed;
  --accent-soft: rgba(124, 58, 237, 0.12);
  --shadow: 0 18px 50px rgba(15, 23, 42, 0.12);
  --radius: 18px;
  --node-size: 92px;
  --couple-gap: 18px;
  --level-gap: 88px;
  --sibling-gap: 28px;
  --font: "Avenir Next", "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
}

body {
  overflow: hidden;
}

button,
input,
textarea,
select {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 20px;
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(15, 23, 42, 0.08);
  z-index: 5;
}

.brand h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.brand p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.btn {
  border: 1px solid rgba(15, 23, 42, 0.12);
  background: #fff;
  color: var(--ink);
  border-radius: 999px;
  padding: 8px 14px;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.08);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-danger {
  background: #fff5f5;
  border-color: #fecaca;
  color: #b91c1c;
}

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

.edit-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 999px;
  background: #f3f4f6;
  color: var(--muted);
  font-size: 0.85rem;
}

.edit-pill.active {
  background: var(--accent-soft);
  color: #5b21b6;
}

.canvas-wrap {
  position: relative;
  overflow: hidden;
  background-color: var(--bg);
  background-image: radial-gradient(circle, var(--dot) 1px, transparent 1px);
  background-size: 22px 22px;
}

.canvas-hint {
  position: absolute;
  left: 16px;
  bottom: 16px;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.9);
  color: var(--muted);
  font-size: 0.8rem;
  box-shadow: var(--shadow);
  z-index: 2;
}

.tree-stage {
  position: absolute;
  inset: 0;
  cursor: grab;
}

.tree-stage.dragging {
  cursor: grabbing;
}

.tree-canvas {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
}

.tree-links {
  position: absolute;
  inset: 0;
  overflow: visible;
  pointer-events: none;
}

.tree-links line,
.tree-links path {
  stroke: var(--line);
  stroke-width: 2;
  fill: none;
}

.tree-nodes {
  position: relative;
}

.family-unit {
  position: absolute;
  display: flex;
  align-items: flex-start;
  gap: var(--couple-gap);
}

.person-node {
  width: calc(var(--node-size) + 24px);
  text-align: center;
  cursor: pointer;
}

.person-node .photo {
  width: var(--node-size);
  height: var(--node-size);
  margin: 0 auto;
  border-radius: 50%;
  overflow: hidden;
  border: 3px solid #fff;
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.14);
  background: #f8fafc;
}

.person-node .photo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.person-node .name {
  margin-top: 10px;
  font-size: 0.82rem;
  line-height: 1.25;
  font-weight: 600;
  word-break: break-word;
}

.person-node .meta {
  margin-top: 4px;
  font-size: 0.72rem;
  color: var(--muted);
}

.person-node:hover .photo,
.person-node.selected .photo {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.marriage-link {
  align-self: center;
  width: 28px;
  height: 2px;
  background: var(--line);
  margin-top: calc(var(--node-size) / 2);
  flex: 0 0 auto;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 20;
}

.modal-backdrop.open {
  display: flex;
}

.modal {
  width: min(720px, 100%);
  max-height: min(90vh, 900px);
  overflow: auto;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
}

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.35rem;
}

.modal-close {
  border: none;
  background: #f3f4f6;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.2rem;
}

.profile-grid {
  display: grid;
  grid-template-columns: 160px 1fr;
  gap: 18px;
}

.profile-photo {
  width: 160px;
  height: 160px;
  border-radius: 18px;
  overflow: hidden;
  background: #f8fafc;
  border: 1px solid rgba(15, 23, 42, 0.08);
}

.profile-photo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.field {
  display: grid;
  gap: 6px;
  margin-bottom: 14px;
}

.field label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
}

.field input,
.field textarea,
.field select {
  width: 100%;
  border: 1px solid rgba(15, 23, 42, 0.12);
  border-radius: 12px;
  padding: 10px 12px;
  background: #fff;
}

.field textarea {
  min-height: 96px;
  resize: vertical;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.modal-actions,
.section-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
}

.section-card {
  margin-top: 18px;
  padding: 16px;
  border-radius: 14px;
  background: #f9fafb;
  border: 1px solid rgba(15, 23, 42, 0.06);
}

.section-card h3 {
  margin: 0 0 10px;
  font-size: 0.95rem;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  padding: 12px 16px;
  border-radius: 12px;
  background: #111827;
  color: #fff;
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 30;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.status-banner {
  display: none;
  margin: 0 20px 0;
  padding: 10px 14px;
  border-radius: 12px;
  background: #fff7ed;
  color: #9a3412;
  border: 1px solid #fed7aa;
}

.status-banner.show {
  display: block;
}

@media (max-width: 720px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .profile-grid,
  .field-row {
    grid-template-columns: 1fr;
  }

  .profile-photo {
    width: 120px;
    height: 120px;
  }
}
