:root {
  --bg: #f7f6f3;
  --ink: #222;
  --muted: #6b6b6b;
  --line: #b9b6ae;
  --card: #ffffff;
  --card-gone: #efeeea;
  --border: #8f8c84;
  --accent: #2f4f6f;
  --ring: #c0392b;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1b;
    --ink: #eceae4;
    --muted: #a09d95;
    --line: #5a5852;
    --card: #2a2a28;
    --card-gone: #232321;
    --border: #77746c;
    --accent: #8db3d9;
    --ring: #ff7b6b;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
}
body.locked #bar, body.locked #stage { visibility: hidden; }

/* password gate */
#gate { position: fixed; inset: 0; z-index: 10; background: var(--bg);
  display: flex; align-items: center; justify-content: center; padding: 16px; }
#gate[hidden] { display: none; }
#gate form { width: 100%; max-width: 340px; text-align: center; }
#gate h1 { font-size: 26px; margin: 0 0 8px; font-weight: 600; }
#gate p { color: var(--muted); margin: 0 0 16px; }
#gate input { width: 100%; padding: 10px 12px; font-size: 16px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--card); color: var(--ink); margin-bottom: 10px; }
#gate button { width: 100%; padding: 10px; font-size: 16px; border: 0; border-radius: 6px;
  background: var(--accent); color: var(--bg); cursor: pointer; }
#gate-error { color: var(--ring) !important; min-height: 1.4em; margin: 10px 0 0 !important; }

/* top bar */
#bar { display: flex; align-items: center; gap: 12px; padding: 8px 16px; flex-wrap: wrap;
  border-bottom: 1px solid var(--line); background: var(--bg); }
#bar h1 { font-size: 18px; font-weight: 600; margin: 0; }
#search-wrap { position: relative; flex: 1 1 200px; max-width: 320px; }
#search { width: 100%; padding: 7px 10px; font-size: 15px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--card); color: var(--ink); }
#results { position: absolute; left: 0; right: 0; top: 100%; margin: 2px 0 0; padding: 0; list-style: none;
  background: var(--card); border: 1px solid var(--border); border-radius: 6px; z-index: 5;
  max-height: 260px; overflow: auto; }
#results li { padding: 7px 10px; cursor: pointer; }
#results li:hover, #results li.on { background: var(--card-gone); }
#controls { display: flex; gap: 6px; margin-left: auto; flex-wrap: wrap; }
#controls button { padding: 6px 10px; font-size: 14px; border: 1px solid var(--border);
  background: var(--card); color: var(--ink); border-radius: 6px; cursor: pointer; }
#controls button:hover { background: var(--card-gone); }

/* canvas */
#stage { position: relative; flex: 1; min-height: 0; }
#svg { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; }
#svg.drag { cursor: grabbing; }
#hint { position: absolute; left: 16px; bottom: 10px; margin: 0; font-size: 12px; color: var(--muted);
  pointer-events: none; }

/* tree drawing */
.edge { fill: none; stroke: var(--line); stroke-width: 1.5; }
.edge.divorced { stroke-dasharray: 5 4; }
.edge-label { font-size: 11px; fill: var(--muted); text-anchor: middle; }
.marry { stroke: var(--line); stroke-width: 1.5; }
.group-title { font-size: 20px; font-weight: 600; fill: var(--muted); }

.card { cursor: pointer; }
.card rect { fill: var(--card); stroke: var(--border); stroke-width: 1.2; }
.card.gone rect { fill: var(--card-gone); }
.card.unknown rect, .card.check rect { stroke-dasharray: 4 3; }
.card.stub rect { stroke-dasharray: 2 3; fill: none; }
.card:hover rect, .card:focus rect { stroke: var(--accent); stroke-width: 2; outline: none; }
.card.sel rect { stroke: var(--accent); stroke-width: 2.5; }
.card.hit rect { stroke: var(--ring); stroke-width: 3; }
.card text { fill: var(--ink); pointer-events: none; }
.card .nm { font-size: 13px; font-weight: 600; }
.card .dt { font-size: 11px; fill: var(--muted); }
.card.unknown .nm { font-style: italic; font-weight: 400; fill: var(--muted); }
.card.stub .nm { font-weight: 400; fill: var(--muted); }

.tog { cursor: pointer; }
.tog circle { fill: var(--card); stroke: var(--border); stroke-width: 1.2; }
.tog text { font-size: 11px; fill: var(--ink); text-anchor: middle; dominant-baseline: central; pointer-events: none; }
.tog:hover circle { stroke: var(--accent); }

/* details panel */
#panel { position: absolute; top: 0; right: 0; bottom: 0; width: 340px; max-width: 100%; z-index: 4;
  background: var(--card); border-left: 1px solid var(--border); padding: 18px 18px 24px; overflow: auto; }
#panel[hidden] { display: none; }
#panel-close { position: absolute; top: 8px; right: 10px; font-size: 24px; line-height: 1; border: 0;
  background: none; color: var(--muted); cursor: pointer; }
#panel h2 { font-size: 20px; margin: 0 28px 2px 0; font-weight: 600; }
#panel .nick { color: var(--muted); margin: 0 0 10px; }
#panel h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted);
  margin: 18px 0 4px; font-weight: 600; }
#panel p { margin: 0 0 4px; }
#panel ul { margin: 0; padding: 0; list-style: none; }
#panel li { margin: 2px 0; }
#panel a.link { color: var(--accent); cursor: pointer; text-decoration: underline; }
#panel .warn { border-left: 3px solid var(--ring); padding-left: 8px; margin-top: 12px; }
#panel .dim { color: var(--muted); }

@media (max-width: 640px) {
  #panel { top: auto; width: 100%; height: 55%; border-left: 0; border-top: 1px solid var(--border); }
  #hint { display: none; }
  #bar h1 { display: none; }
}

/* edit mode */
#edit-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; padding: 6px 16px;
  background: var(--card-gone); border-bottom: 2px solid var(--accent); font-size: 14px; }
#edit-bar .ed-hint { color: var(--muted); }
#edit-bar .ed-spacer { flex: 1; }
#edit-bar .ed-msg { color: var(--ring); }
#edit-bar button, .ed-box button, .dlg-card button { padding: 6px 10px; font-size: 14px; border: 1px solid var(--border);
  background: var(--card); color: var(--ink); border-radius: 6px; cursor: pointer; }
#edit-bar button:disabled { opacity: .45; cursor: default; }
#edit-bar button:hover:not(:disabled), .ed-box button:hover, .dlg-card button:hover { background: var(--card-gone); }
.ed-box { margin-top: 18px; border: 1px solid var(--border); border-radius: 6px; padding: 8px 12px 12px; }
.ed-box summary { cursor: pointer; font-weight: 600; padding: 4px 0; }
.ed-box h4 { margin: 16px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.ed-field { display: block; margin: 8px 0; }
.ed-field span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 2px; }
.ed-field input, .ed-field textarea, .ed-field select { width: 100%; padding: 6px 8px; font: inherit; font-size: 14px;
  border: 1px solid var(--border); border-radius: 5px; background: var(--bg); color: var(--ink); }
.ed-check { display: block; margin: 6px 0; font-size: 14px; }
.ed-note { color: var(--ring); font-size: 13px; min-height: 1.2em; margin: 6px 0 0 !important; }
.ed-union { border-top: 1px solid var(--line); margin-top: 10px; padding-top: 6px; }
#export-dlg { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .45); display: flex;
  align-items: center; justify-content: center; padding: 16px; }
.dlg-card { background: var(--card); color: var(--ink); border: 1px solid var(--border); border-radius: 8px;
  max-width: 560px; width: 100%; max-height: 90vh; overflow: auto; padding: 18px 20px; }
.dlg-card h2 { margin: 0 0 8px; font-size: 20px; }
.dlg-card ul { margin: 0 0 12px; padding-left: 20px; }
.dlg-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
