:root {
  --bg: #f4f1ea;
  --grid: rgba(60, 50, 30, 0.07);
  --surface: #ffffff;
  --surface-2: #f7f5f0;
  --border: #e3ddd0;
  --text: #24221d;
  --muted: #706a5e;
  --brand: #2f6f5e;
  --brand-ink: #ffffff;
  --accent: #c2703d;
  --line: #a59d8c;
  --couple: #2f6f5e;
  --male: #3f74c9;
  --male-soft: #e5edfa;
  --female: #c9507f;
  --female-soft: #fae6ee;
  --unknown: #8d8778;
  --unknown-soft: #eeebe4;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgba(40, 30, 10, .08), 0 4px 14px rgba(40, 30, 10, .08);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171a19;
    --grid: rgba(255, 255, 255, 0.05);
    --surface: #222725;
    --surface-2: #1c201f;
    --border: #353c39;
    --text: #ece9e2;
    --muted: #a39e93;
    --brand: #5fb39b;
    --brand-ink: #0d1a16;
    --accent: #e0915d;
    --line: #6f7873;
    --couple: #5fb39b;
    --male: #7ea6ec;
    --male-soft: #23324a;
    --female: #ec88ae;
    --female-soft: #47283a;
    --unknown: #a8a394;
    --unknown-soft: #34352f;
    --danger: #f2817a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 18px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 9px;
  padding: 7px 12px;
  white-space: nowrap;
}
button:hover { border-color: var(--line); }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 1px;
}
button.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); font-weight: 600; }
button.accent { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 600; }
button.icon { width: 36px; padding: 7px 0; font-size: 16px; }
button:disabled { opacity: .4; cursor: default; }

/* ---------- Barre d'outils ---------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 8px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; color: var(--brand); }
.brand svg rect { fill: var(--brand); }
.brand svg path { stroke: var(--brand); }
.tree-name {
  border: 1px solid transparent;
  background: transparent;
  border-radius: 8px;
  padding: 6px 8px;
  font-weight: 600;
  min-width: 120px;
  width: 220px;
}
.tree-name:hover, .tree-name:focus { border-color: var(--border); background: var(--surface-2); }
.tb-group { display: flex; gap: 6px; align-items: center; }
.tb-group.right { margin-left: auto; }
.search input {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 9px;
  padding: 7px 10px;
  width: 220px;
}
.count { color: var(--muted); font-size: 13px; }
.menu-wrap { position: relative; }
.menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 6px;
  display: flex;
  flex-direction: column;
  min-width: 250px;
  z-index: 20;
}
.menu[hidden] { display: none; }
.menu button { border: 0; text-align: left; display: flex; flex-direction: column; gap: 2px; padding: 9px 10px; background: transparent; }
.menu button:hover { background: var(--surface-2); }
.menu small { color: var(--muted); }

/* ---------- Zone de l'arbre ---------- */
.main { flex: 1; display: flex; min-height: 0; position: relative; }
.viewport {
  flex: 1;
  position: relative;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  background-color: var(--bg);
  background-image: radial-gradient(var(--grid) 1.2px, transparent 1.2px);
  background-size: 22px 22px;
}
.viewport.dragging { cursor: grabbing; }
.stage { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.links { position: absolute; overflow: visible; pointer-events: none; }
.links path { fill: none; stroke: var(--line); stroke-width: 2; stroke-linejoin: round; }
.links path.couple { stroke: var(--couple); stroke-width: 3; }
.links path.divorce { stroke: var(--couple); stroke-width: 2.5; stroke-dasharray: 6 5; }

/* ---------- Cartes ---------- */
.card {
  position: absolute;
  width: 200px;
  height: 92px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 10px 12px 10px 14px;
  cursor: pointer;
  user-select: none;
  transition: box-shadow .15s, transform .15s;
}
.card::before {
  content: "";
  position: absolute;
  left: 0; top: 12px; bottom: 12px;
  width: 4px;
  border-radius: 0 4px 4px 0;
  background: var(--unknown);
}
.card.sex-M::before { background: var(--male); }
.card.sex-F::before { background: var(--female); }
.card:hover { box-shadow: 0 0 0 1px var(--line), var(--shadow); }
.card.selected { box-shadow: 0 0 0 3px var(--brand), var(--shadow); }
.card.flash { animation: flash 1s ease-out; }
@keyframes flash { 0% { box-shadow: 0 0 0 10px rgba(194, 112, 61, .45); } 100% { box-shadow: 0 0 0 3px var(--brand); } }
.card.deceased { background: var(--surface-2); }
.card.deceased .name, .card.deceased .lastname { color: var(--muted); }

.avatar {
  flex: none;
  width: 42px; height: 42px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 15px;
  background: var(--unknown-soft);
  color: var(--unknown);
  position: relative;
}
.sex-M .avatar, .avatar.sex-M { background: var(--male-soft); color: var(--male); }
.sex-F .avatar, .avatar.sex-F { background: var(--female-soft); color: var(--female); }
.deceased .avatar::after {
  content: "†";
  position: absolute;
  right: -5px; bottom: -6px;
  font-size: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 18px; height: 18px;
  display: grid; place-items: center;
  color: var(--muted);
}
.info { min-width: 0; flex: 1; line-height: 1.3; }
.info > div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.name { font-weight: 650; font-size: 14.5px; }
.lastname { font-weight: 600; font-size: 12px; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }
.dates { font-size: 12px; color: var(--muted); margin-top: 3px; font-variant-numeric: tabular-nums; }
.age { font-size: 12px; color: var(--text); }
.placeholder { color: var(--muted); font-style: italic; font-weight: 500; }

.add {
  position: absolute;
  width: 26px; height: 26px;
  padding: 0;
  border-radius: 50%;
  background: var(--brand);
  color: var(--brand-ink);
  border: 2px solid var(--surface);
  font-weight: 700;
  font-size: 16px;
  line-height: 1;
  display: grid;
  place-items: center;
  opacity: 0;
  transform: scale(.7);
  transition: opacity .12s, transform .12s;
  z-index: 2;
}
.card:hover .add, .card.selected .add { opacity: 1; transform: scale(1); }
.add:hover { background: var(--accent); }
.add-parent { left: 50%; top: -14px; margin-left: -13px; }
.add-child { left: 50%; bottom: -14px; margin-left: -13px; }
.add-spouse { right: -14px; top: 50%; margin-top: -13px; }
@media (hover: none) { .card .add { opacity: 1; transform: scale(1); } }

/* ---------- Écran vide ---------- */
.empty { position: absolute; inset: 0; display: grid; place-items: center; padding: 16px; pointer-events: none; }
.empty[hidden] { display: none; }
.empty-box {
  pointer-events: auto;
  max-width: 460px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 26px;
  text-align: center;
  cursor: default;
}
.empty-box h1 { margin: 0 0 8px; font-size: 22px; }
.empty-box p { color: var(--muted); margin: 0 0 18px; }
.empty-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }

.zoom { position: absolute; right: 14px; bottom: 14px; display: flex; flex-direction: column; gap: 6px; }
.zoom button { width: 38px; height: 38px; padding: 0; font-size: 18px; box-shadow: var(--shadow); }
.legend {
  position: absolute; left: 14px; bottom: 14px;
  display: flex; gap: 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 6px 10px; font-size: 12px; color: var(--muted);
  pointer-events: none;
}
.legend span { display: flex; align-items: center; gap: 6px; }
.legend i { display: inline-block; width: 22px; height: 0; border-top: 3px solid var(--couple); }
.legend i.l-divorce { border-top: 2.5px dashed var(--couple); }
.legend i.l-child { border-top: 2px solid var(--line); }

/* ---------- Panneau latéral ---------- */
.panel {
  width: 380px;
  flex: none;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 0 0 30px;
}
.panel[hidden] { display: none; }
.panel-head {
  position: sticky; top: 0;
  background: var(--surface);
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  z-index: 1;
}
.panel-head h2 { margin: 0; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.panel-head .sub { color: var(--muted); font-size: 13px; }
.panel-head > div:nth-child(2) { flex: 1; min-width: 0; }
.panel-head .close { border: 0; font-size: 22px; padding: 2px 8px; background: transparent; }
.panel section { padding: 14px 16px 4px; }
.panel h3 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.panel label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); margin-bottom: 10px; }
.panel input[type=text], .panel textarea, .panel select {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 9px;
  padding: 8px 10px;
  color: var(--text);
  width: 100%;
}
.panel textarea { min-height: 80px; resize: vertical; }
.panel input.invalid { border-color: var(--danger); background: color-mix(in srgb, var(--danger) 8%, var(--surface)); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.panel label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 14px; }
.seg { display: flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; margin-bottom: 10px; }
.seg button { flex: 1; border: 0; border-radius: 0; background: var(--surface-2); }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--brand); color: var(--brand-ink); font-weight: 600; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chip {
  display: inline-flex; align-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
  max-width: 100%;
}
.chip button { border: 0; background: transparent; border-radius: 0; padding: 4px 10px; overflow: hidden; text-overflow: ellipsis; }
.chip button.x { padding: 4px 9px 4px 4px; color: var(--muted); }
.chip button.x:hover { color: var(--danger); }
.chip.sex-M { border-color: color-mix(in srgb, var(--male) 40%, var(--border)); }
.chip.sex-F { border-color: color-mix(in srgb, var(--female) 40%, var(--border)); }
.chip small { color: var(--muted); padding-right: 8px; }
.muted { color: var(--muted); font-size: 13px; margin: 0 0 10px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.btn-row button { font-size: 13px; padding: 6px 10px; }
.link-btn { border: 0; background: transparent; color: var(--danger); padding: 4px 0; font-size: 13px; }

.union-box { border: 1px solid var(--border); border-radius: 12px; padding: 10px; margin-bottom: 10px; background: var(--surface-2); }
.union-box .chips { margin-bottom: 6px; }
.union-box .row2 label { margin-bottom: 6px; }
.union-box .row2 select, .union-box .row2 input { background: var(--surface); }
.union-title { display: flex; align-items: center; justify-content: space-between; gap: 6px; margin-bottom: 8px; }
.kids-label { font-size: 12px; color: var(--muted); margin: 4px 0 6px; }

.link-list { max-height: 170px; }
.danger-zone { border-top: 1px solid var(--border); margin-top: 10px; padding-top: 14px; }
.danger-zone button { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, var(--border)); }

.toast {
  position: fixed;
  left: 50%; bottom: 24px;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 50;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Mobile ---------- */
@media (max-width: 900px) {
  .search input { width: 160px; }
  .tree-name { width: 150px; }
  .count { display: none; }
}
@media (max-width: 720px) {
  .toolbar { padding: 8px 12px; gap: 6px; }
  .brand span { display: none; }
  .tree-name { flex: 1; width: auto; }
  .search { order: 10; flex: 1 1 100%; }
  .search input { width: 100%; }
  .tb-group.right { margin-left: 0; }
  .legend { display: none; }
  .panel {
    position: absolute;
    left: 0; right: 0; bottom: 0;
    width: auto;
    max-height: 70%;
    border-left: 0;
    border-top: 1px solid var(--border);
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -8px 30px rgba(0, 0, 0, .2);
    z-index: 15;
  }
}
