/* ================= Weirwood — theme ================= */
:root {
  --bg: #0c0d11;
  --bg-soft: #12141a;
  --panel: #171a22;
  --panel-2: #1d212c;
  --line: #2a2e3a;
  --ink: #e9e2d0;
  --muted: #9a9384;
  --faint: #6d675c;
  --gold: #c8a24e;
  --gold-soft: #8a7440;
  --danger: #b3564e;
  --radius: 16px;
  --serif: "EB Garamond", Georgia, "Times New Roman", serif;
  --display: "Cinzel", Georgia, serif;
}

:root {
  --glow-1: #1a1d28;
  --glow-2: #14161f;
  --shadow: rgba(0, 0, 0, .35);
  --shadow-strong: rgba(0, 0, 0, .5);
}

/* Day theme — toggled by the ☀️/🌙 button */
:root[data-theme="light"] {
  --bg: #f2ead7;
  --bg-soft: #eadfc6;
  --panel: #faf4e6;
  --panel-2: #fffcf3;
  --line: #d9cdb0;
  --ink: #2c2517;
  --muted: #6d6049;
  --faint: #9c8f74;
  --gold: #8a6b28;
  --gold-soft: #ab8b45;
  --danger: #a2372e;
  --glow-1: #f8f1de;
  --glow-2: #ecdfc2;
  --shadow: rgba(96, 78, 42, .14);
  --shadow-strong: rgba(96, 78, 42, .22);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  font-family: var(--serif);
  font-size: 17px;
  color: var(--ink);
  background:
    radial-gradient(1100px 500px at 50% -10%, var(--glow-1) 0%, transparent 60%),
    radial-gradient(900px 500px at 90% 110%, var(--glow-2) 0%, transparent 55%),
    var(--bg);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background .3s ease, color .3s ease;
}

/* ================= top bar ================= */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand {
  font-family: var(--display);
  font-weight: 700;
  font-size: 1.15rem;
  letter-spacing: .06em;
  color: var(--gold);
  text-decoration: none;
  white-space: nowrap;
}
.brand-mark { filter: saturate(.6); }

.search { position: relative; flex: 1; max-width: 440px; margin-inline: auto; }
.search input {
  width: 100%;
  padding: 9px 14px;
  font: inherit;
  font-size: .95rem;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  outline: none;
  transition: border-color .18s ease, box-shadow .18s ease;
}
.search input::placeholder { color: var(--faint); }
.search input:focus { border-color: var(--gold-soft); box-shadow: 0 0 0 3px rgba(200,162,78,.12); }

.search-results {
  position: absolute;
  top: calc(100% + 8px);
  left: 0; right: 0;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 18px 40px var(--shadow-strong);
}
.search-hit {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 14px;
  font: inherit;
  font-size: .95rem;
  color: var(--ink);
  background: none;
  border: 0;
  cursor: pointer;
  text-align: left;
}
.search-hit:hover { background: rgba(200,162,78,.08); }
.search-hit em { color: var(--muted); font-style: italic; }
.hit-dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.hit-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hit-family { color: var(--faint); font-size: .8rem; letter-spacing: .04em; }

.legends-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 9px 16px;
  font-family: var(--display);
  font-size: .8rem;
  font-weight: 600;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--gold);
  background: linear-gradient(180deg, rgba(200,162,78,.12), rgba(200,162,78,.04));
  border: 1px solid var(--gold-soft);
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
  transition: background .18s ease, transform .18s ease;
}
.legends-btn:hover { background: rgba(200,162,78,.18); transform: translateY(-1px); }
.legends-btn .star { filter: saturate(.55); }

.theme-btn {
  flex: none;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  font-size: 1.05rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 50%;
  cursor: pointer;
  transition: transform .18s ease, border-color .18s ease;
}
.theme-btn:hover { transform: rotate(20deg) scale(1.06); border-color: var(--gold-soft); }

/* ================= main / tree ================= */
#main {
  position: relative;
  flex: 1;
  width: 100%;
  max-width: 1060px;
  margin: 0 auto;
  padding: 34px 18px 60px;
}
.edges {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}
.edge {
  fill: none;
  stroke: var(--gold-soft);
  stroke-width: 1.6;
  opacity: .55;
}
.edge.dashed { stroke-dasharray: 6 6; opacity: .5; }

.tree { position: relative; z-index: 1; }
.tree.animate > * { animation: rise .38s cubic-bezier(.2,.7,.3,1) both; }
.tree.animate > *:nth-child(2) { animation-delay: .05s; }
.tree.animate > *:nth-child(3) { animation-delay: .1s; }
@keyframes rise {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: none; }
}

.row { display: flex; justify-content: center; gap: 18px; flex-wrap: wrap; }
.row.parents { margin-bottom: 46px; }
.row.focus-row { margin-bottom: 52px; }

/* ================= cards ================= */
.card {
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 10px 26px var(--shadow);
  transition: background .3s ease, border-color .3s ease;
}

.card.mini {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 216px;
  padding: 10px 14px 10px 10px;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
  transition: transform .18s ease, border-color .18s ease, box-shadow .18s ease;
}
.card.mini:hover {
  transform: translateY(-3px);
  border-color: var(--gold-soft);
  box-shadow: 0 14px 30px var(--shadow-strong);
}
.card.mini.unknown { cursor: default; opacity: .55; }
.card.mini.unknown:hover { transform: none; border-color: var(--line); }
.card.mini.presumed { border-style: dashed; }

.mini-info { min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.mini-relation {
  font-size: .66rem;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--faint);
}
.mini-name {
  font-family: var(--display);
  font-size: .88rem;
  font-weight: 600;
  line-height: 1.25;
}
.mini-sub {
  font-size: .8rem;
  color: var(--muted);
  font-style: italic;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* portraits */
.portrait {
  position: relative;
  flex: none;
  overflow: hidden;
  background:
    radial-gradient(120% 120% at 30% 20%, color-mix(in srgb, var(--house) 55%, #14161d) 0%, #14161d 75%);
  border: 1px solid color-mix(in srgb, var(--house) 45%, var(--line));
}
.portrait.sm { width: 56px; height: 56px; border-radius: 50%; }
.portrait.lg { width: 168px; height: 190px; border-radius: 18px; }
.portrait .init {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-family: var(--display);
  font-weight: 700;
  color: rgba(233,226,208,.8);
}
.portrait.sm .init { font-size: 1.15rem; }
.portrait.lg .init { font-size: 3rem; }
.portrait img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: top center;
  opacity: 0;
  transition: opacity .4s ease;
}
.portrait img.loaded { opacity: 1; }

/* coat of arms */
.arms {
  position: relative;
  display: block;
  width: 64px;
  height: 72px;
  margin: 14px auto 0;
}
.arms-fallback {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-family: var(--display);
  font-weight: 700;
  font-size: 1.5rem;
  color: rgba(233,226,208,.85);
  background: linear-gradient(180deg, color-mix(in srgb, var(--house) 70%, #111) 0%, color-mix(in srgb, var(--house) 40%, #111) 100%);
  border: 1px solid rgba(233,226,208,.25);
  clip-path: polygon(0 0, 100% 0, 100% 62%, 50% 100%, 0 62%);
}
.arms img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  opacity: 0;
  transition: opacity .4s ease;
  filter: drop-shadow(0 4px 10px rgba(0,0,0,.5));
}
.arms img.loaded { opacity: 1; }
.arms:has(img.loaded) .arms-fallback { opacity: 0; }

/* focus card */
.focus-card {
  display: flex;
  gap: 26px;
  width: min(720px, 100%);
  padding: 26px;
  border-color: color-mix(in srgb, var(--house) 30%, var(--line));
}
.focus-media { flex: none; text-align: center; }
.focus-info { min-width: 0; }
.focus-house {
  font-family: var(--display);
  font-size: .72rem;
  font-weight: 600;
  letter-spacing: .22em;
  text-transform: uppercase;
  color: color-mix(in srgb, var(--house) 70%, var(--ink));
  margin-bottom: 6px;
}
.focus-name {
  margin: 0;
  font-family: var(--display);
  font-size: clamp(1.4rem, 3.4vw, 2rem);
  font-weight: 700;
  line-height: 1.15;
}
.focus-epithet { color: var(--gold); font-style: italic; margin-top: 4px; font-size: 1.05rem; }
.focus-disambig { color: var(--muted); font-style: italic; font-size: .9rem; margin-top: 2px; }

.focus-facts { margin: 14px 0 0; }
.focus-facts > div { display: flex; gap: 10px; margin-bottom: 5px; }
.focus-facts dt {
  flex: none;
  width: 44px;
  font-size: .66rem;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--faint);
  padding-top: .3em;
}
.focus-facts dd { margin: 0; }

.focus-chips { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 14px; }
.chip {
  font-size: .72rem;
  letter-spacing: .05em;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  color: var(--muted);
  white-space: nowrap;
}
.chip-book { border-color: #3d4a3f; color: #a9bfa5; }
.chip-show { border-color: #4a3d5c; color: #bfa9d9; }
.chip-game { border-color: #375063; color: #9cc3dd; }
.chip-note { border-color: var(--gold-soft); color: var(--gold); }
.chip-alert { border-color: var(--danger); color: #d99a94; }
:root[data-theme="light"] .chip-book { border-color: #7f9a80; color: #3e5c40; }
:root[data-theme="light"] .chip-show { border-color: #9a86b5; color: #563d78; }
:root[data-theme="light"] .chip-game { border-color: #7d9cb4; color: #2e5674; }
:root[data-theme="light"] .chip-alert { color: #a2372e; }

.focus-links { display: flex; gap: 10px; margin-top: 16px; }
.wiki-link {
  font-size: .78rem;
  letter-spacing: .05em;
  padding: 5px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  text-decoration: none;
  transition: color .15s ease, border-color .15s ease;
}
.wiki-link:hover { color: var(--gold); border-color: var(--gold-soft); }

.focus-notes {
  margin: 16px 0 0;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  line-height: 1.55;
  font-size: .98rem;
}

/* children */
.children-label {
  text-align: center;
  font-family: var(--display);
  font-size: .72rem;
  font-weight: 600;
  letter-spacing: .24em;
  text-transform: uppercase;
  color: var(--faint);
  margin-bottom: 18px;
}
.children-groups { display: flex; flex-direction: column; gap: 26px; }
.kid-group { text-align: center; }
.group-label {
  display: inline-block;
  margin-bottom: 14px;
  padding: 5px 14px;
  font: inherit;
  font-size: .82rem;
  font-style: italic;
  color: var(--gold);
  background: rgba(200,162,78,.07);
  border: 1px solid var(--gold-soft);
  border-radius: 999px;
  cursor: pointer;
  transition: background .18s ease;
}
.group-label:hover { background: rgba(200,162,78,.16); }
.group-label.muted { color: var(--faint); border-color: var(--line); background: none; cursor: default; }

/* ================= Legends drawer ================= */
.scrim {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgba(5,6,9,.55);
  backdrop-filter: blur(2px);
  opacity: 0;
  transition: opacity .25s ease;
}
.scrim.show { opacity: 1; }
:root[data-theme="light"] .scrim { background: rgba(74, 60, 32, .35); }

.drawer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  z-index: 100;
  width: min(360px, 92vw);
  background: var(--panel);
  border-left: 1px solid var(--line);
  box-shadow: -24px 0 60px var(--shadow-strong);
  transform: translateX(102%);
  transition: transform .25s cubic-bezier(.2,.7,.3,1);
  display: flex;
  flex-direction: column;
}
.drawer.open { transform: translateX(0); }
.drawer[hidden], .scrim[hidden] { display: none; }

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 12px;
  border-bottom: 1px solid var(--line);
}
.drawer-head h2 {
  margin: 0;
  font-family: var(--display);
  font-size: 1.02rem;
  font-weight: 700;
  letter-spacing: .08em;
  color: var(--gold);
}
.drawer-close {
  font-size: 1rem;
  color: var(--muted);
  background: none;
  border: 0;
  cursor: pointer;
  padding: 6px 10px;
  border-radius: 8px;
}
.drawer-close:hover { color: var(--ink); background: var(--panel-2); }

.drawer-body { overflow-y: auto; padding: 8px 12px 24px; }
.fav-show {
  margin: 18px 8px 8px;
  font-family: var(--display);
  font-size: .68rem;
  font-weight: 600;
  letter-spacing: .2em;
  text-transform: uppercase;
  color: var(--faint);
}
.fav-list { display: flex; flex-direction: column; gap: 4px; }
.fav {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 7px 10px;
  font: inherit;
  color: inherit;
  text-align: left;
  background: none;
  border: 0;
  border-radius: 12px;
  cursor: pointer;
  transition: background .15s ease;
}
.fav:hover { background: var(--panel-2); }
.fav .portrait.sm { width: 44px; height: 44px; }
.fav-info { min-width: 0; display: flex; flex-direction: column; }
.fav-name { font-family: var(--display); font-size: .86rem; font-weight: 600; }
.fav-sub {
  font-size: .78rem;
  color: var(--muted);
  font-style: italic;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ================= footer & misc ================= */
.footer {
  padding: 18px 20px 26px;
  text-align: center;
  font-size: .82rem;
  color: var(--faint);
  border-top: 1px solid var(--line);
}
.footer a { color: var(--muted); }

.load-error {
  max-width: 560px;
  margin: 60px auto;
  padding: 24px;
  text-align: center;
  color: var(--danger);
  border: 1px dashed var(--danger);
  border-radius: var(--radius);
}

/* ================= mobile ================= */
@media (max-width: 720px) {
  body { font-size: 16px; }
  .topbar { flex-wrap: wrap; gap: 10px; padding: 10px 14px; }
  .brand { order: 1; }
  .theme-btn { order: 2; margin-left: auto; }
  .legends-btn { order: 3; padding: 8px 14px; }
  .search { order: 4; flex-basis: 100%; max-width: none; }

  #main { padding: 24px 12px 48px; }
  .row { gap: 12px; }
  .row.parents { margin-bottom: 34px; }
  .row.focus-row { margin-bottom: 40px; }

  .card.mini { width: calc(50% - 6px); min-width: 150px; padding: 8px 10px 8px 8px; }
  .portrait.sm { width: 46px; height: 46px; }

  .focus-card { flex-direction: column; align-items: center; text-align: center; padding: 20px 16px; }
  .focus-info { width: 100%; }
  .focus-facts > div { justify-content: center; }
  .focus-chips { justify-content: center; }
  .focus-notes { text-align: left; }
  .portrait.lg { width: 140px; height: 158px; }

  .drawer {
    top: auto;
    left: 0; right: 0; bottom: 0;
    width: 100%;
    height: 78vh;
    border-left: 0;
    border-top: 1px solid var(--line);
    border-radius: 20px 20px 0 0;
    transform: translateY(104%);
  }
  .drawer.open { transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* ================= Census page ================= */
.page-nav { display: flex; align-items: center; gap: 6px; margin-right: auto; }
.nav-link {
  padding: 7px 14px;
  font-family: var(--display);
  font-size: .78rem;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
  text-decoration: none;
  border-radius: 999px;
  border: 1px solid transparent;
  white-space: nowrap;
}
.nav-link:hover { color: var(--gold); border-color: var(--line); }
.nav-link.current { color: var(--gold); border-color: var(--gold-soft); background: rgba(200,162,78,.08); }

.census-main {
  flex: 1;
  width: 100%;
  max-width: 1180px;
  margin: 0 auto;
  padding: 36px 18px 60px;
}
.census-title {
  margin: 0;
  text-align: center;
  font-family: var(--display);
  font-weight: 700;
  font-size: clamp(1.5rem, 4vw, 2.2rem);
  letter-spacing: .04em;
}
.census-sub {
  margin: 10px auto 34px;
  max-width: 640px;
  text-align: center;
  color: var(--muted);
  font-style: italic;
}

.census { columns: 3 320px; column-gap: 20px; }
.census-house {
  break-inside: avoid;
  margin: 0 0 20px;
  padding: 16px 16px 10px;
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border: 1px solid var(--line);
  border-left: 3px solid color-mix(in srgb, var(--house) 65%, var(--line));
  border-radius: var(--radius);
  box-shadow: 0 10px 26px var(--shadow);
}
.census-house-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-bottom: 10px;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--line);
}
.census-house-head h2 {
  flex: 1;
  margin: 0;
  font-family: var(--display);
  font-size: .95rem;
  font-weight: 700;
  letter-spacing: .06em;
}
.census-arms { width: 26px; height: 30px; margin: 0; flex: none; }
.census-arms .arms-fallback { font-size: .8rem; }
.census-count {
  font-size: .72rem;
  color: var(--faint);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 9px;
}

.census-row { display: flex; align-items: center; gap: 2px; }
.census-go {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 5px 6px;
  border-radius: 8px;
  text-decoration: none;
  color: var(--ink);
  font-size: .93rem;
  line-height: 1.35;
}
.census-go:hover { background: rgba(200,162,78,.08); }
.census-wiki {
  flex: none;
  padding: 3px 7px;
  border-radius: 8px;
  text-decoration: none;
  font-size: .8rem;
  color: var(--faint);
  opacity: .7;
}
.census-wiki:hover { color: var(--gold); background: rgba(200,162,78,.1); opacity: 1; }
.census-row.presumed .census-name::after { content: " *"; color: var(--danger); }
.census-name { min-width: 0; }
.census-name em { color: var(--muted); font-style: italic; font-size: .86em; }
.census-years {
  margin-left: auto;
  flex: none;
  color: var(--faint);
  font-size: .78rem;
  font-variant-numeric: tabular-nums;
}
.census-flag {
  flex: none;
  font-size: .64rem;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--gold);
  border: 1px solid var(--gold-soft);
  border-radius: 999px;
  padding: 1px 7px;
  white-space: nowrap;
}
.census-flag.muted { color: var(--faint); border-color: var(--line); }
.census-book { flex: none; font-size: .72rem; opacity: .55; }

@media (max-width: 720px) {
  .census { columns: 1; }
  .census-main { padding: 24px 12px 48px; }
  .page-nav { order: 2; margin-left: auto; margin-right: 0; }
  .census-link { order: 1; padding: 7px 10px; }
  .census-row { padding: 8px 6px; }
}
