:root {
  --bg: #f5f2ec;
  --surface: #ffffff;
  --surface-2: #f3efe7;
  --border: #e3ddd1;
  --border-strong: #cfc6b6;
  --text: #1d2722;
  --muted: #69716b;
  --accent: #2f6b55;
  --accent-strong: #234f3f;
  --accent-soft: #e2eee7;
  --on-accent: #ffffff;
  --gold: #b9862a;
  --gold-soft: #f7ecd2;
  --danger: #b2433a;
  --danger-soft: #f8e4e1;
  --link: #cbc2b2;
  --dot: #dcd5c8;
  --shadow: 0 1px 2px rgb(30 40 32 / 0.06), 0 4px 14px rgb(30 40 32 / 0.06);
  --shadow-lg: 0 18px 50px rgb(20 30 24 / 0.18);
  --avatar-l: 88%;
  --avatar-text-l: 28%;
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --serif: 'Fraunces', Georgia, 'Times New Roman', serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111613;
    --surface: #19201c;
    --surface-2: #212924;
    --border: #2c3631;
    --border-strong: #3c4842;
    --text: #e6ebe7;
    --muted: #97a29b;
    --accent: #6cc19b;
    --accent-strong: #8dd5b3;
    --accent-soft: #1d362b;
    --on-accent: #0d1a14;
    --gold: #e0b45c;
    --gold-soft: #3a3020;
    --danger: #ec8076;
    --danger-soft: #3b2220;
    --link: #39443e;
    --dot: #232b27;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 4px 14px rgb(0 0 0 / 0.25);
    --shadow-lg: 0 18px 50px rgb(0 0 0 / 0.5);
    --avatar-l: 24%;
    --avatar-text-l: 82%;
    color-scheme: dark;
  }
}

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

html, body { height: 100%; margin: 0; }
body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 30;
}
.brand { display: flex; align-items: center; gap: 12px; text-decoration: none; color: inherit; min-width: 0; }
.brand img { border-radius: 10px; flex: none; }
.brand > span { display: flex; flex-direction: column; min-width: 0; }
.brand-title { font-family: var(--serif); font-weight: 600; font-size: 20px; line-height: 1.15; letter-spacing: -0.01em; white-space: nowrap; }
.brand-sub { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.search { position: relative; flex: 1; max-width: 460px; margin-left: auto; }
.search input {
  width: 100%;
  height: 40px;
  padding: 0 14px 0 38px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  outline: none;
  transition: border-color .15s, background .15s, box-shadow .15s;
}
.search input:focus { border-color: var(--accent); background: var(--surface); box-shadow: 0 0 0 3px var(--accent-soft); }
.search-icon { position: absolute; left: 13px; top: 50%; width: 17px; height: 17px; translate: 0 -50%; color: var(--muted); pointer-events: none; }
.search-results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  margin: 0;
  padding: 6px;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  max-height: 60vh;
  overflow: auto;
}
.search-results li { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; cursor: pointer; }
.search-results li[aria-selected="true"], .search-results li:hover { background: var(--accent-soft); }
.search-results .result-name { font-weight: 500; }
.search-results .result-meta { color: var(--muted); font-size: 12.5px; }
.search-results .no-results { color: var(--muted); cursor: default; }
.search-results .no-results:hover { background: none; }

.top-actions { display: flex; align-items: center; gap: 10px; }
.segmented { display: flex; padding: 3px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; }
.segmented button {
  display: flex; align-items: center; gap: 6px;
  height: 32px; padding: 0 14px;
  border: 0; border-radius: 999px; background: transparent;
  color: var(--muted); font-weight: 500; font-size: 14px;
}
.segmented button svg { width: 16px; height: 16px; }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 38px; padding: 0 16px;
  border-radius: 10px; border: 1px solid transparent;
  font-weight: 500; font-size: 14px; white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.btn svg { width: 16px; height: 16px; }
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-strong); }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); border-color: var(--border-strong); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger-ghost { background: transparent; color: var(--danger); border-color: var(--border); }
.btn-danger-ghost:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.is-editor { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
.icon-btn { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px; border: 0; background: transparent; color: var(--muted); }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; }
.link-btn { border: 0; background: none; padding: 4px 6px; color: var(--accent); font-weight: 500; font-size: 13.5px; border-radius: 6px; }
.link-btn:hover { text-decoration: underline; }

.edit-banner {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 20px;
  background: var(--gold-soft);
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
  position: relative; z-index: 25;
}
.edit-banner .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--gold); flex: none; box-shadow: 0 0 0 4px rgb(185 134 42 / .2); }
.edit-banner-text { flex: 1; }

/* ---------- Main area ---------- */
main { flex: 1; position: relative; overflow: hidden; min-height: 0; }
.tree-view, .list-view { position: absolute; inset: 0; }

.viewport {
  position: absolute; inset: 0;
  overflow: hidden;
  touch-action: none;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  background-image: radial-gradient(var(--dot) 1.2px, transparent 1.2px);
  background-size: 24px 24px;
}
.viewport.dragging { cursor: grabbing; }
.stage { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
.stage.animate { transition: transform .5s cubic-bezier(.2, .8, .2, 1); }
.links { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
.links path { fill: none; stroke: var(--link); stroke-width: 1.8; transition: stroke .2s; }
.links path.active { stroke: var(--accent); stroke-width: 2.6; }
.links path.dim { opacity: .35; }

.gen-label {
  position: absolute; top: 0; left: 0;
  white-space: nowrap;
  font-size: 11.5px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase;
  color: var(--muted);
  padding-left: 4px;
}

.node {
  position: absolute; left: 0; top: 0;
  width: var(--node-w); height: var(--node-h);
  display: flex; align-items: center; gap: 11px;
  padding: 0 18px 0 11px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: border-color .15s, box-shadow .15s, opacity .2s;
}
.node:hover { border-color: var(--border-strong); box-shadow: 0 2px 4px rgb(30 40 32 / .08), 0 8px 22px rgb(30 40 32 / .1); }
.node.lineage { border-color: var(--accent); }
.node.selected { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft), var(--shadow); }
.node.match { border-color: var(--gold); box-shadow: 0 0 0 4px var(--gold-soft), var(--shadow); }
.node.dim { opacity: .38; }
.node-text { display: flex; flex-direction: column; min-width: 0; }
.node-name { font-weight: 600; font-size: 14.5px; line-height: 1.25; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.node-meta { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.node-toggle {
  position: absolute; right: -13px; top: 50%; translate: 0 -50%;
  min-width: 26px; height: 26px; padding: 0 6px;
  border-radius: 13px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--muted);
  font-size: 12px; font-weight: 600; line-height: 1;
  display: grid; place-items: center;
}
.node-toggle:hover { color: var(--accent); border-color: var(--accent); }
.node-toggle.collapsed { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }

.avatar {
  --hue: 150;
  flex: none;
  display: grid; place-items: center;
  width: 38px; height: 38px; border-radius: 50%;
  background: hsl(var(--hue) 40% var(--avatar-l));
  color: hsl(var(--hue) 45% var(--avatar-text-l));
  font-weight: 600; font-size: 13.5px; letter-spacing: .02em;
}
.avatar.sm { width: 28px; height: 28px; font-size: 11px; }
.avatar.lg { width: 64px; height: 64px; font-size: 21px; font-family: var(--serif); }

.floating { position: absolute; z-index: 10; margin: 0; }
.zoom-controls {
  left: 16px; bottom: 16px;
  display: flex; align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 3px;
}
.zoom-controls button { height: 34px; min-width: 34px; border: 0; border-radius: 9px; background: transparent; display: grid; place-items: center; color: var(--text); }
.zoom-controls button:hover { background: var(--surface-2); }
.zoom-controls button svg { width: 17px; height: 17px; }
.zoom-controls .fit { padding: 0 10px; font-size: 13px; font-weight: 500; }
.hint { left: 50%; bottom: 22px; translate: -50% 0; color: var(--muted); font-size: 12.5px; pointer-events: none; white-space: nowrap; }
.hint kbd { font: inherit; font-size: 11px; padding: 1px 5px; border: 1px solid var(--border-strong); border-radius: 4px; background: var(--surface); }
.stats-chip {
  right: 16px; bottom: 16px;
  padding: 7px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow);
  font-size: 12.5px; color: var(--muted);
  transition: opacity .2s;
}
body.panel-open .stats-chip, body.panel-open .hint { opacity: 0; pointer-events: none; }
body.view-list .stats-chip, body.view-list .hint { display: none; }

/* ---------- List view ---------- */
.list-view { overflow: auto; padding: 24px 20px 80px; }
.list-inner { max-width: 760px; margin: 0 auto; transition: margin .3s; }
.list-toolbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; gap: 12px; }
.list-toolbar .stats { margin: 0; color: var(--muted); font-size: 13.5px; }
.tree-list, .tree-list ul { list-style: none; margin: 0; padding: 0; }
.tree-list ul { margin-left: 21px; padding-left: 14px; border-left: 1.5px solid var(--border); }
.list-row { display: flex; align-items: center; gap: 4px; padding: 2px 0; scroll-margin-top: 12px; }
.caret, .caret-spacer { flex: none; width: 28px; height: 28px; }
.caret { display: grid; place-items: center; border: 0; background: none; border-radius: 8px; color: var(--muted); }
.caret:hover { background: var(--surface-2); color: var(--text); }
.caret svg { width: 16px; height: 16px; transition: rotate .15s; }
.caret[aria-expanded="true"] svg { rotate: 90deg; }
.list-name {
  display: flex; align-items: center; gap: 10px;
  min-width: 0;
  padding: 6px 12px 6px 6px;
  border: 1px solid transparent;
  border-radius: 12px;
  background: none;
  text-align: left;
}
.list-name:hover { background: var(--surface); border-color: var(--border); }
.list-row.selected .list-name { background: var(--accent-soft); border-color: var(--accent); }
.list-row.match .list-name { background: var(--gold-soft); border-color: var(--gold); }
.list-name .name { font-weight: 500; }
.list-name .count { color: var(--muted); font-size: 12.5px; }
.chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px; font-weight: 500;
  white-space: nowrap;
}
.chip svg { width: 12px; height: 12px; }

/* ---------- Side panel ---------- */
.panel {
  position: absolute; top: 14px; right: 14px; bottom: 14px;
  width: 384px; max-width: calc(100% - 28px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow-lg);
  overflow: auto;
  z-index: 20;
  transform: translateX(calc(100% + 28px));
  visibility: hidden;
  transition: transform .32s cubic-bezier(.2, .8, .2, 1), visibility 0s .32s;
}
body.panel-open .panel { transform: none; visibility: visible; transition: transform .32s cubic-bezier(.2, .8, .2, 1); }
.panel-close { position: absolute; top: 12px; right: 12px; z-index: 1; }
#panel-body { padding: 24px; }
.panel-head { display: flex; align-items: center; gap: 16px; padding-right: 28px; }
.panel-head h2 { margin: 0; font-family: var(--serif); font-weight: 600; font-size: 24px; line-height: 1.2; letter-spacing: -0.01em; }
.panel-head .chip { margin-top: 6px; }
.lineage { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 4px; margin: 18px 0 4px; font-size: 13px; color: var(--muted); }
.lineage button { border: 0; background: none; padding: 2px 4px; border-radius: 6px; color: var(--accent); font-size: 13px; }
.lineage button:hover { background: var(--accent-soft); }
.lineage .sep { color: var(--border-strong); }
.lineage .current { color: var(--text); font-weight: 500; padding: 2px 4px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 18px 0 0; padding: 16px; background: var(--surface-2); border-radius: 14px; font-size: 14px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; font-weight: 500; overflow-wrap: anywhere; }
.facts dd button { border: 0; background: none; padding: 0; color: var(--accent); font-weight: 500; text-align: left; }
.facts dd button:hover { text-decoration: underline; }
.panel h3 { margin: 22px 0 10px; font-size: 12px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.notes p { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.child-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.child-list button {
  width: 100%;
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  text-align: left;
}
.child-list button:hover { border-color: var(--accent); background: var(--accent-soft); }
.child-list .name { flex: 1; font-weight: 500; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.child-list .sub { color: var(--muted); font-size: 12.5px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.edit-actions { margin-top: 24px; padding-top: 20px; border-top: 1px solid var(--border); display: grid; gap: 8px; }
.edit-actions .row { display: flex; gap: 8px; flex-wrap: wrap; }
.edit-actions .row .btn { flex: 1; }
.viewer-note { margin-top: 24px; padding: 12px 14px; border-radius: 12px; background: var(--surface-2); font-size: 13px; color: var(--muted); }

/* ---------- Empty state ---------- */
.empty { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; gap: 12px; padding: 24px; }
.empty h2 { margin: 0; font-family: var(--serif); font-weight: 600; }
.empty p { margin: 0; color: var(--muted); }

/* ---------- Dialogs ---------- */
.dialog {
  width: min(540px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
}
.dialog-sm { width: min(420px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgb(14 20 17 / .45); backdrop-filter: blur(3px); }
.dialog[open] { animation: pop .18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }
.dialog form { padding: 24px; display: grid; gap: 14px; }
.dialog header h2 { margin: 0 0 4px; font-family: var(--serif); font-weight: 600; font-size: 22px; }
.dialog header p { margin: 0; font-size: 14px; }
.dialog footer { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }
.field { display: grid; gap: 6px; min-width: 0; }
.field > span { font-size: 13px; font-weight: 500; }
.field > span em { font-style: normal; color: var(--muted); font-weight: 400; font-size: 12px; margin-left: 4px; }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: var(--surface);
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.field textarea { resize: vertical; min-height: 76px; }
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-error { margin: 0; padding: 9px 12px; border-radius: 10px; background: var(--danger-soft); color: var(--danger); font-size: 13.5px; }

/* ---------- Toasts ---------- */
.toasts { position: fixed; left: 50%; top: 18px; translate: -50% 0; z-index: 100; display: grid; gap: 8px; pointer-events: none; }
.toast {
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px; font-weight: 500;
  box-shadow: var(--shadow-lg);
  animation: toast-in .2s ease-out;
}
.toast.error { background: var(--danger); color: #fff; }
.toast.leaving { opacity: 0; transition: opacity .3s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } }

/* ---------- Responsive ---------- */
@media (min-width: 900px) {
  body.panel-open .list-inner { margin-right: max(0px, calc(400px - (100vw - 760px) / 2)); }
}

@media (max-width: 860px) {
  .topbar { flex-wrap: wrap; gap: 10px 12px; padding: 10px 16px; }
  .search { order: 3; flex-basis: 100%; max-width: none; margin-left: 0; }
  .top-actions { margin-left: auto; }
  .hint { display: none; }
}

@media (max-width: 640px) {
  .brand-title { font-size: 17px; }
  .brand img { width: 30px; height: 30px; }
  .brand-sub { display: none; }
  .segmented button { padding: 0 10px; }
  #auth-btn span { display: none; }
  #auth-btn { padding: 0 10px; }
  .edit-banner { padding: 8px 16px; font-size: 12.5px; }
  .edit-banner-text { display: none; }
  .edit-banner::after { content: 'Editing mode'; flex: 1; order: 1; font-weight: 600; }
  .edit-banner .link-btn { order: 2; }
  .list-view { padding: 16px 12px 80px; }
  body.panel-open .list-view { padding-bottom: 80vh; }
  .tree-list ul { margin-left: 13px; padding-left: 8px; }
  .panel {
    top: auto; left: 8px; right: 8px; bottom: 8px;
    width: auto; max-width: none;
    max-height: 74%;
    border-radius: 20px;
    transform: translateY(calc(100% + 16px));
  }
  #panel-body { padding: 20px; }
  .field-row { grid-template-columns: 1fr; }
  .stats-chip { display: none; }
}

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