:root {
  color-scheme: light;
  --bg: oklch(0.975 0 0);
  --bg-wash: oklch(0.955 0 0);
  --surface: oklch(1 0 0);
  --surface-raised: oklch(1 0 0);
  --ink: oklch(0.24 0.01 60);
  --ink-muted: oklch(0.49 0.012 60);
  --line: oklch(0.9 0 0);
  --line-strong: oklch(0.78 0 0);
  --edge: oklch(0.58 0.025 60);
  --edge-faint: oklch(0.76 0.02 60);
  --accent: oklch(0.72 0.145 62);
  --accent-strong: oklch(0.55 0.135 60);
  --accent-ink: oklch(0.5 0.115 60);
  --accent-soft: oklch(0.95 0.04 70);
  --warn-bg: oklch(0.97 0.03 85);
  --warn-line: oklch(0.8 0.09 85);
  --warn-ink: oklch(0.42 0.08 80);
  --shadow-card:
    0 1px 2px oklch(0.2 0.01 60 / 0.05), 0 3px 10px oklch(0.2 0.01 60 / 0.06);
  --shadow-lift:
    0 2px 4px oklch(0.2 0.01 60 / 0.07), 0 8px 22px oklch(0.2 0.01 60 / 0.12);
  --shadow-panel: -8px 0 32px oklch(0.2 0.01 60 / 0.14);
  --font-body: system-ui, -apple-system, "Segoe UI", sans-serif;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --z-topbar: 10;
  --z-panel: 20;
  --z-issues: 30;
}

* {
  box-sizing: border-box;
  margin: 0;
}
html,
body {
  height: 100%;
}
body {
  font-family: var(--font-body);
  overflow: hidden;
  background:
    radial-gradient(120% 90% at 20% 0%, var(--bg-wash) 0%, transparent 55%),
    radial-gradient(110% 100% at 100% 100%, var(--bg-wash) 0%, transparent 50%),
    var(--bg);
  color: var(--ink);
}

:focus-visible {
  outline: 2px solid var(--accent-strong);
  outline-offset: 2px;
}

#topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: var(--z-topbar);
  display: flex;
  align-items: baseline;
  gap: 14px;
  padding: 14px 22px 18px;
  background: linear-gradient(to bottom, var(--bg) 55%, transparent);
  pointer-events: none;
}
#topbar h1 {
  font-size: 21px;
  font-weight: 650;
  letter-spacing: -0.01em;
  color: var(--ink);
}
#count {
  color: var(--ink-muted);
  font-size: 13px;
}
#search {
  pointer-events: auto;
  margin-left: auto;
  align-self: center;
  padding: 9px 14px;
  min-width: 230px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  box-shadow: var(--shadow-card);
  transition:
    border-color 0.18s var(--ease-out),
    box-shadow 0.18s var(--ease-out);
}
#search::placeholder {
  color: var(--ink-muted);
}
#search:hover {
  border-color: var(--line-strong);
}
#search:focus {
  outline: none;
  border-color: var(--accent-strong);
  box-shadow:
    0 0 0 3px var(--accent-soft),
    var(--shadow-card);
}

#viewport {
  position: fixed;
  inset: 0;
  overflow: hidden;
  touch-action: none;
  cursor: grab;
}
#viewport:active {
  cursor: grabbing;
}
#world {
  position: absolute;
  transform-origin: 0 0;
}
#edges {
  position: absolute;
  inset: 0;
  overflow: visible;
  pointer-events: none;
}
#cards {
  position: absolute;
  inset: 0;
}

/* Cards — fixed 150x160 footprint, everything inside */
.card {
  position: absolute;
  width: 150px;
  height: 160px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 6px;
  padding: 10px 10px 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow-card);
  cursor: pointer;
  transition:
    border-color 0.18s var(--ease-out),
    box-shadow 0.18s var(--ease-out),
    opacity 0.25s var(--ease-out),
    filter 0.25s var(--ease-out),
    translate 0.18s var(--ease-out);
}
.card:hover {
  border-color: var(--line-strong);
  box-shadow: var(--shadow-lift);
  translate: 0 -1px;
}
.card:hover .avatar {
  border-color: var(--accent);
}
.card.deceased {
  filter: saturate(0.55);
  opacity: 0.88;
}
.avatar {
  width: 84px;
  height: 84px;
  flex: none;
  border-radius: 50%;
  overflow: hidden;
  background: var(--accent-soft);
  color: var(--accent-ink);
  border: 2px solid var(--surface-raised);
  box-shadow: inset 0 0 0 1px var(--line);
  display: grid;
  place-items: center;
  font-weight: 650;
  font-size: 26px;
  transition: border-color 0.18s var(--ease-out);
}
.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.meta {
  min-width: 0;
  max-width: 100%;
}
.name {
  font-size: 13.5px;
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: -0.005em;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.years {
  font-size: 11.5px;
  color: var(--ink-muted);
  font-variant-numeric: tabular-nums;
  margin-top: 2px;
}

/* Edges — dash pattern carries union status; weight/opacity carry routing */
.edge {
  fill: none;
  stroke: var(--edge);
  stroke-width: 1.5;
}
.edge-dashed {
  stroke-dasharray: 7 5;
}
.edge-dotted {
  stroke-dasharray: 2 4;
  stroke-linecap: round;
}
.edge-neutral {
  stroke: var(--edge-faint);
  stroke-width: 1.25;
}
.edge-relation {
  stroke: var(--edge-faint);
  stroke-width: 1.25;
  stroke-dasharray: 1 5;
  stroke-linecap: round;
}
.edge-crossrow {
  opacity: 0.55;
  stroke-width: 1.25;
}
.junction {
  fill: var(--edge);
  stroke: var(--bg);
  stroke-width: 1.5;
}

/* Detail panel */
#panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 340px;
  z-index: var(--z-panel);
  display: flex;
  flex-direction: column;
  gap: 18px;
  background: var(--surface);
  box-shadow: var(--shadow-panel);
  border-left: 1px solid var(--line);
  overflow-y: auto;
  padding: 24px 22px;
  animation: panel-in 0.32s var(--ease-out);
}
#panel[hidden] {
  display: none;
}
@keyframes panel-in {
  from {
    translate: 24px 0;
    opacity: 0;
  }
}
.panel-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 36px;
  height: 36px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: none;
  cursor: pointer;
  transition: background 0.15s var(--ease-out);
}
.panel-close span {
  position: absolute;
  inset: 0;
  margin: auto;
  width: 16px;
  height: 2px;
  border-radius: 1px;
  background: var(--ink-muted);
  rotate: 45deg;
  transition: background 0.15s var(--ease-out);
}
.panel-close span + span {
  rotate: -45deg;
}
.panel-close:hover {
  background: var(--accent-soft);
}
.panel-close:hover span {
  background: var(--accent-ink);
}
.panel-head {
  text-align: center;
  padding-top: 6px;
}
.panel-head h2 {
  font-size: 21px;
  font-weight: 650;
  letter-spacing: -0.01em;
  text-wrap: balance;
}
.panel-avatar {
  width: 92px;
  height: 92px;
  margin: 0 auto 12px;
  font-size: 30px;
  border-width: 3px;
  box-shadow:
    inset 0 0 0 1px var(--line),
    var(--shadow-card);
}
.panel-dates {
  color: var(--ink-muted);
  font-size: 13px;
  margin-top: 5px;
}
.panel-section h3 {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-muted);
  font-weight: 600;
  margin-bottom: 8px;
}
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.chip {
  border: 1px solid var(--line);
  background: var(--surface-raised);
  color: var(--ink);
  border-radius: 999px;
  padding: 5px 12px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition:
    border-color 0.15s var(--ease-out),
    background 0.15s var(--ease-out);
}
.chip:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.panel-notes {
  font-size: 14px;
  line-height: 1.55;
  white-space: pre-line;
  color: var(--ink);
  max-width: 65ch;
}
.lineage-btn {
  margin-top: auto;
  padding: 11px 14px;
  border-radius: 999px;
  border: 1px solid var(--ink);
  background: var(--ink);
  color: oklch(1 0 0);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition:
    background 0.15s var(--ease-out),
    box-shadow 0.15s var(--ease-out),
    translate 0.15s var(--ease-out);
}
.lineage-btn:hover {
  background: oklch(0.32 0.02 60);
  box-shadow: var(--shadow-lift);
  translate: 0 -1px;
}
.lineage-btn:active {
  background: oklch(0.18 0.01 60);
  translate: 0 0;
}

/* Lineage pill — active-mode indicator and off-switch */
#lineage-pill {
  position: fixed;
  top: 70px;
  left: 50%;
  translate: -50% 0;
  z-index: var(--z-topbar);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px 8px 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow-card);
  cursor: pointer;
  animation: pill-in 0.25s var(--ease-out);
  transition:
    border-color 0.15s var(--ease-out),
    box-shadow 0.15s var(--ease-out);
}
#lineage-pill[hidden] {
  display: none;
}
@keyframes pill-in {
  from {
    translate: -50% -8px;
    opacity: 0;
  }
}
#lineage-pill:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow-lift);
}
.pill-cross {
  position: relative;
  width: 18px;
  height: 18px;
}
.pill-cross span {
  position: absolute;
  inset: 0;
  margin: auto;
  width: 10px;
  height: 2px;
  border-radius: 1px;
  background: var(--ink-muted);
  rotate: 45deg;
}
.pill-cross span + span {
  rotate: -45deg;
}
#lineage-pill:hover .pill-cross span {
  background: var(--accent-ink);
}

/* Recenter button */
#recenter {
  position: fixed;
  bottom: 16px;
  right: 16px;
  z-index: var(--z-topbar);
  width: 42px;
  height: 42px;
  padding: 0;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  color: var(--ink-muted);
  box-shadow: var(--shadow-card);
  cursor: pointer;
  transition:
    border-color 0.15s var(--ease-out),
    color 0.15s var(--ease-out),
    box-shadow 0.15s var(--ease-out);
}
#recenter svg {
  width: 20px;
  height: 20px;
}
#recenter:hover {
  border-color: var(--accent);
  color: var(--accent-ink);
  box-shadow: var(--shadow-lift);
}

/* Issues toast */
#issues {
  position: fixed;
  bottom: 16px;
  left: 16px;
  z-index: var(--z-issues);
  max-width: 420px;
  background: var(--warn-bg);
  border: 1px solid var(--warn-line);
  color: var(--ink);
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 13px;
  box-shadow: var(--shadow-card);
}
#issues ul {
  margin: 0 0 8px 18px;
}
#issues button {
  border: none;
  background: none;
  color: var(--warn-ink);
  cursor: pointer;
  padding: 0;
  font: inherit;
  font-size: 13px;
  text-decoration: underline;
}

/* States driven by JS */
.card.dimmed {
  opacity: 0.18;
  filter: grayscale(0.8);
}
@keyframes pulse {
  0%,
  100% {
    box-shadow: var(--shadow-card);
  }
  50% {
    box-shadow:
      0 0 0 10px var(--accent-soft),
      var(--shadow-lift);
    border-color: var(--accent);
  }
}
.card.pulse {
  animation: pulse 0.6s ease 2;
}
@keyframes shake {
  25% {
    translate: -4px 0;
  }
  75% {
    translate: 4px 0;
  }
}
#search.no-match {
  animation: shake 0.2s linear 2;
  border-color: var(--accent-strong);
}

/* Mobile ≤640px — panel becomes a bottom sheet */
@media (max-width: 640px) {
  #topbar {
    flex-wrap: wrap;
    gap: 4px 10px;
    padding: 10px 14px 14px;
  }
  #topbar h1 {
    font-size: 18px;
  }
  #search {
    margin-left: 0;
    width: 100%;
    min-width: 0;
    order: 3;
  }
  #panel {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-height: 60vh;
    border-left: none;
    border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -8px 32px oklch(0.2 0.01 60 / 0.2);
    padding-bottom: calc(22px + env(safe-area-inset-bottom));
    animation-name: panel-up;
  }
  @keyframes panel-up {
    from {
      translate: 0 32px;
      opacity: 0;
    }
  }
  #issues {
    left: 8px;
    right: 8px;
    bottom: 8px;
    max-width: none;
  }
  #lineage-pill {
    top: 108px;
  }
}

@media (prefers-reduced-motion: reduce) {
  #panel {
    animation: none;
  }
  .card,
  .avatar,
  #search,
  .chip,
  .lineage-btn,
  .panel-close,
  #lineage-pill,
  #recenter {
    transition: none;
  }
  #lineage-pill {
    animation: none;
  }
  .card.pulse {
    animation: none;
    border-color: var(--accent);
    box-shadow: var(--shadow-lift);
  }
  #search.no-match {
    animation: none;
  }
}
