@font-face { font-family: Fraunces; font-weight: 400; font-display: swap; src: url(fonts/fraunces-latin-400-normal.woff2) format("woff2"); }
@font-face { font-family: Fraunces; font-weight: 600; font-display: swap; src: url(fonts/fraunces-latin-600-normal.woff2) format("woff2"); }
@font-face { font-family: Inter; font-weight: 400; font-display: swap; src: url(fonts/inter-latin-400-normal.woff2) format("woff2"); }
@font-face { font-family: Inter; font-weight: 500; font-display: swap; src: url(fonts/inter-latin-500-normal.woff2) format("woff2"); }
@font-face { font-family: Inter; font-weight: 600; font-display: swap; src: url(fonts/inter-latin-600-normal.woff2) format("woff2"); }

:root {
  color-scheme: light dark;
  --bg: #f4f1ea;
  --dot: #dcd6c8;
  --surface: #fffdf9;
  --surface-2: #f0ece2;
  --ink: #23302b;
  --muted: #6c7670;
  --border: #e0dacc;
  --accent: #2f6b57;
  --accent-ink: #ffffff;
  --father: #3f7396;
  --mother: #b4607c;
  --line-strong: #39463f;
  --line-soft: #8b958d;
  --line-faint: #c6c2b4;
  --shadow: 0 1px 2px rgb(40 35 20 / .06), 0 6px 18px rgb(40 35 20 / .07);
  --shadow-lg: 0 12px 40px rgb(40 35 20 / .18);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141916;
    --dot: #252c28;
    --surface: #1e2522;
    --surface-2: #28302c;
    --ink: #e9ebe4;
    --muted: #9aa49c;
    --border: #323b36;
    --accent: #6fbf9f;
    --accent-ink: #10201a;
    --father: #7fb2d3;
    --mother: #dd93ad;
    --line-strong: #cfd6cd;
    --line-soft: #8d978f;
    --line-faint: #46504a;
    --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 6px 18px rgb(0 0 0 / .25);
    --shadow-lg: 0 12px 40px rgb(0 0 0 / .5);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 400 15px/1.5 Inter, system-ui, sans-serif;
  overflow: hidden;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { font-family: Fraunces, Georgia, serif; font-weight: 600; margin: 0; }
.muted { color: var(--muted); }

/* Toppen */
.topbar {
  position: fixed; inset: 12px 12px auto; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 8px 10px 8px 18px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
}
.brand { font-size: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search { position: relative; flex: 1; max-width: 380px; margin-left: auto; min-width: 120px; }
.search input {
  width: 100%; height: 38px; padding: 0 14px;
  background: var(--surface-2); border: 1px solid transparent; border-radius: 10px; outline: none;
}
.search input:focus { border-color: var(--accent); }
.results {
  position: absolute; top: 44px; left: 0; right: 0; margin: 0; padding: 6px; list-style: none;
  max-height: 60vh; overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow-lg);
}
.results li.muted { padding: 8px 10px; }
.results button {
  display: flex; flex-direction: column; width: 100%; padding: 7px 10px; text-align: left;
  background: none; border: 0; border-radius: 8px;
}
.results button:hover { background: var(--surface-2); }
.results small { color: var(--muted); }
.actions { display: flex; gap: 6px; }
.ghost, .primary {
  height: 38px; padding: 0 14px; border-radius: 10px; font-weight: 500; white-space: nowrap;
}
.ghost { background: none; border: 1px solid var(--border); }
.ghost:hover { background: var(--surface-2); }
.primary { background: var(--accent); color: var(--accent-ink); border: 0; }

/* Trädytan */
.viewport {
  position: fixed; inset: 0; overflow: hidden; touch-action: none; cursor: grab; user-select: none;
  background-color: var(--bg);
  background-image: radial-gradient(var(--dot) 1px, transparent 1px);
  background-size: 22px 22px;
}
.viewport.dragging { cursor: grabbing; }
.world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.world.animate { transition: transform .45s cubic-bezier(.22, .8, .3, 1); }
.world-inner { position: absolute; }
.lines { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }

.line { fill: none; stroke-width: 2; stroke: var(--line-strong); }
.conf-good.line, svg .conf-good { stroke: var(--line-soft); }
.line.conf-mid { stroke: var(--line-soft); stroke-dasharray: 8 6; }
.line.conf-low { stroke: var(--line-soft); stroke-dasharray: 1.5 6; stroke-linecap: round; }
.line.sibling { stroke: var(--line-faint); stroke-width: 1.25; }
.line.father { stroke: color-mix(in srgb, var(--father) 75%, var(--line-strong)); }
.line.mother { stroke: color-mix(in srgb, var(--mother) 75%, var(--line-strong)); }
.line.conf-good.father { stroke: color-mix(in srgb, var(--father) 60%, var(--bg)); }
.line.conf-good.mother { stroke: color-mix(in srgb, var(--mother) 60%, var(--bg)); }
.line.conf-none:not(.sibling) { stroke: var(--line-faint); stroke-width: 1.25; }
.line.unknown-line { stroke: var(--line-faint); stroke-width: 1.25; stroke-dasharray: 5 5; }
.hit { fill: none; stroke: transparent; stroke-width: 14; pointer-events: stroke; cursor: help; }

.line-label {
  position: absolute; transform: translate(-50%, -50%);
  padding: 0 6px; border-radius: 99px; font-size: 11px; font-weight: 600; line-height: 18px;
  background: var(--surface); border: 1px solid var(--border); color: var(--muted);
  pointer-events: none;
}

.card {
  position: absolute; display: flex; flex-direction: column; justify-content: center;
  padding: 0 14px 0 16px; text-align: left;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); overflow: hidden;
  transition: border-color .15s, box-shadow .15s, transform .15s;
}
button.card:hover { border-color: var(--accent); transform: translateY(-1px); }
.card::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--border);
}
.card.male::before { background: var(--father); }
.card.female::before { background: var(--mother); }
.card .name {
  font-family: Fraunces, Georgia, serif; font-weight: 600; font-size: 15px; line-height: 1.2;
  overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
}
.card .meta { font-size: 12.5px; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card.sibling { background: color-mix(in srgb, var(--surface) 55%, var(--bg)); box-shadow: none; }
.card.sibling .name { font-size: 13.5px; font-weight: 400; -webkit-line-clamp: 1; }
.card.sibling .meta { font-size: 12px; margin-top: 0; }
.card.root { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.card.root .meta { color: inherit; opacity: .8; }
.card.root::before { display: none; }
.card.unknown { background: none; border: 1.5px dashed var(--line-soft); box-shadow: none; }
.card.unknown::before { display: none; }
.card.unknown .name { font-weight: 400; color: var(--muted); }
.card.selected { outline: 3px solid var(--accent); outline-offset: 2px; }

/* Långt utzoomat: bara större namn. */
.far .card .meta, .far .line-label { display: none; }
.far .card .name { font-size: 24px; -webkit-line-clamp: 2; font-weight: 600; line-height: 1.1; }
.far .card.sibling .name { font-size: 20px; }

/* Zoomknappar och teckenförklaring */
.zoom {
  position: fixed; left: 12px; bottom: 12px; z-index: 15; display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); overflow: hidden;
}
.zoom button {
  width: 42px; height: 42px; display: grid; place-items: center; font-size: 20px;
  background: none; border: 0; border-bottom: 1px solid var(--border);
}
.zoom button:last-child { border-bottom: 0; }
.zoom button:hover { background: var(--surface-2); }

.legend {
  position: fixed; left: 66px; bottom: 12px; z-index: 15; max-width: 400px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
  font-size: 13px;
}
.legend summary { padding: 8px 14px; cursor: pointer; font-weight: 500; }
.legend ul { list-style: none; margin: 0; padding: 0 14px 12px; display: grid; gap: 7px; }
.legend li { display: flex; gap: 10px; align-items: center; }
.legend svg { flex: none; }
.legend li svg + svg { margin-left: -6px; }

/* Personkort */
.panel {
  position: fixed; top: 0; right: 0; bottom: 0; width: 440px; max-width: 100%; z-index: 30;
  padding: 26px 26px 40px; overflow-y: auto; overscroll-behavior: contain;
  background: var(--surface); border-left: 1px solid var(--border); box-shadow: var(--shadow-lg);
  animation: slide-in .28s cubic-bezier(.22, .8, .3, 1);
}
@keyframes slide-in { from { transform: translateX(40px); opacity: 0; } }
.panel-close, .dialog-close {
  position: absolute; top: 14px; right: 14px; z-index: 2; width: 36px; height: 36px;
  font-size: 22px; line-height: 1; background: var(--surface-2); border: 0; border-radius: 50%;
}
.panel-close { position: sticky; float: right; top: 0; margin: -10px -10px 0 0; }
.eyebrow { margin: 0 0 2px; font-size: 12px; letter-spacing: .08em; text-transform: uppercase; color: var(--accent); font-weight: 600; }
.panel-head h2 { font-size: 27px; line-height: 1.15; padding-right: 30px; }
.alt { margin: 6px 0 0; color: var(--muted); font-size: 14px; }

.map { height: 210px; margin-top: 18px; border-radius: 14px; overflow: hidden; border: 1px solid var(--border); z-index: 0; }
@media (prefers-color-scheme: dark) {
  .map .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.9) contrast(.9); }
}
.map-caption { margin: 6px 2px 0; font-size: 13px; color: var(--muted); }
.no-map { margin: 16px 0 0; padding: 14px; text-align: center; font-size: 14px; color: var(--muted); border: 1px dashed var(--border); border-radius: 14px; }
.pin span {
  display: block; width: 26px; height: 26px; margin-top: 1px;
  background: var(--accent); border: 3px solid #fff; border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg); box-shadow: 0 2px 6px rgb(0 0 0 / .35);
}

.facts { display: grid; grid-template-columns: 76px 1fr; gap: 4px 12px; margin: 18px 0; }
.facts:empty { display: none; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }

.panel section { margin-top: 24px; padding-top: 18px; border-top: 1px solid var(--border); }
.panel h3 { font-size: 17px; margin-bottom: 8px; }
.panel h4 { margin: 12px 0 4px; font-size: 13px; color: var(--muted); font-weight: 500; }
.panel section p { margin: 6px 0; }
.person-link {
  display: flex; justify-content: space-between; gap: 12px; align-items: baseline; width: 100%;
  padding: 7px 10px; margin: 0 -10px; width: calc(100% + 20px);
  text-align: left; background: none; border: 0; border-radius: 8px;
}
.person-link:hover { background: var(--surface-2); }
.person-link small { color: var(--muted); white-space: nowrap; }
.pill {
  display: inline-block; margin-top: 8px; padding: 1px 9px; border-radius: 99px;
  font-size: 12.5px; font-weight: 600; background: var(--surface-2);
}
.basis { font-size: 14px; color: var(--muted); }
.family + .family { margin-top: 16px; padding-top: 12px; border-top: 1px dashed var(--border); }

.timeline { list-style: none; margin: 0; padding: 0 0 0 14px; border-left: 2px solid var(--border); }
.timeline li { position: relative; padding: 0 0 12px; display: flex; flex-direction: column; font-size: 14.5px; }
.timeline li::before {
  content: ""; position: absolute; left: -20px; top: 7px; width: 10px; height: 10px;
  border-radius: 50%; background: var(--accent); border: 2px solid var(--surface);
}
.period { font-size: 12.5px; color: var(--muted); }

.source { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; padding: 6px 0; font-size: 14px; }
.copy {
  padding: 1px 8px; font: 500 12.5px ui-monospace, monospace;
  background: var(--surface-2); border: 0; border-radius: 6px;
}

.tooltip {
  position: fixed; z-index: 40; max-width: 280px; padding: 7px 10px; pointer-events: none;
  font-size: 13px; background: var(--ink); color: var(--bg); border-radius: 8px;
}

/* Dialoger */
.dialog {
  width: min(560px, calc(100vw - 32px)); max-height: 84vh; padding: 26px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow-lg);
}
.dialog::backdrop { background: rgb(15 20 18 / .4); backdrop-filter: blur(2px); }
.dialog h2 { font-size: 24px; }
.dialog h3 { font-size: 17px; margin: 22px 0 8px; }

.stat-tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 16px 0; }
.stat-tiles div { padding: 12px; background: var(--surface-2); border-radius: 12px; }
.stat-tiles b { display: block; font: 600 26px Fraunces, Georgia, serif; }
.stat-tiles span { font-size: 13px; color: var(--muted); }
.bars, .place-list { list-style: none; margin: 0; padding: 0; }
.bars li { display: grid; grid-template-columns: 96px 1fr 28px; gap: 10px; align-items: center; font-size: 14px; padding: 2px 0; }
.bars i { display: block; height: 10px; background: var(--accent); border-radius: 4px; }
.bars li > :last-child { grid-column: 3; text-align: right; }
.bars li { position: relative; }
.place-list li { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; font-size: 14px; border-bottom: 1px solid var(--border); }

.load-error { position: absolute; inset: 40% 0 auto; text-align: center; color: var(--muted); }

@media (min-width: 761px) {
  .panel-open .topbar { right: 452px; }
}
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; padding: 8px 10px; }
  .brand { flex: 1; font-size: 17px; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .ghost { height: 34px; padding: 0 10px; font-size: 13.5px; }
  .panel {
    top: auto; left: 0; right: 0; width: 100%; height: 78vh; padding: 20px 16px 32px;
    border-left: 0; border-top: 1px solid var(--border); border-radius: 18px 18px 0 0;
    animation-name: slide-up;
  }
  .legend { max-width: calc(100vw - 80px); }
}
@keyframes slide-up { from { transform: translateY(40px); opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  .world.animate, .panel, .card { transition: none; animation: none; }
}
