/* ===========================================================================
   THEMES
   Every visual choice (colour, corner radius, font, grid) flows from these
   custom properties, so a theme is just a block that overrides them.
   --card-radius is read by app.js to round the SVG cards.
   =========================================================================== */
:root,
[data-theme="midnight"] {
  --bg:#0f1419; --panel:#171d26; --panel-2:#1e2632; --line:#2b3543;
  --text:#e6edf3; --text-soft:#c7d1dd; --muted:#8b97a7;
  --accent:#4f9dff; --accent-2:#2f7be0; --accent-fg:#fff;
  --female:#e86a9b; --male:#4f9dff; --neutral:#9b8bff;
  --danger:#ff5d5d; --ok:#3fcaa0;
  --shadow:0 6px 24px rgba(0,0,0,.35);
  --radius:10px; --radius-2:8px; --radius-pill:20px; --card-radius:10px;
  --font:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;
  --node-font:var(--font);
  --node-fill:#1e2632; --node-sel-fill:#25303f;
  --link:#5a6b80; --link-spouse:#9b8bff;
  --toolbar-bg:rgba(23,29,38,.72);
  --canvas-bg:radial-gradient(circle at 1px 1px,#1c2531 1px,transparent 0) 0 0/22px 22px,#0f1419;
  --wire:#18e0cf; --wire-strong:#18c2b4; --wire-fg:#03201e;
}

/* Sleek — graphite, flat, sharp corners, violet accent, no grid */
[data-theme="sleek"] {
  --bg:#0c0d10; --panel:#141519; --panel-2:#1b1d23; --line:#2a2d36;
  --text:#f2f3f5; --text-soft:#c3c6cf; --muted:#878b96;
  --accent:#7c6cff; --accent-2:#6354f0; --accent-fg:#fff;
  --female:#ff7eb6; --male:#5aa0ff; --neutral:#b69bff;
  --danger:#ff6b6b; --ok:#46d39a;
  --shadow:0 12px 34px rgba(0,0,0,.55);
  --radius:3px; --radius-2:3px; --radius-pill:3px; --card-radius:2px;
  --node-fill:#16181d; --node-sel-fill:#241f33;
  --link:#3a3f4b; --link-spouse:#7c6cff;
  --toolbar-bg:rgba(20,21,25,.72);
  --canvas-bg:#0c0d10;
}

/* Blueprint — navy, cyan ink, monospace, hard grid, square corners */
[data-theme="blueprint"] {
  --bg:#0a2647; --panel:#0d2c54; --panel-2:#103a6b; --line:#2e5a92;
  --text:#eaf2ff; --text-soft:#bcd4f5; --muted:#7fa8d8;
  --accent:#5fd3ff; --accent-2:#34b6ef; --accent-fg:#06243f;
  --female:#ff9ec7; --male:#7fd0ff; --neutral:#c0a3ff;
  --danger:#ff8a8a; --ok:#6fe3c0;
  --shadow:0 8px 26px rgba(0,0,0,.45);
  --radius:0; --radius-2:0; --radius-pill:0; --card-radius:0;
  --font:"SFMono-Regular",Consolas,"Liberation Mono",Menlo,monospace;
  --node-font:var(--font);
  --node-fill:rgba(13,44,84,.55); --node-sel-fill:#14457f;
  --link:#5b86b8; --link-spouse:#5fd3ff;
  --toolbar-bg:rgba(10,38,71,.82);
  --canvas-bg:
    linear-gradient(rgba(95,140,200,.18) 1px,transparent 1px) 0 0/26px 26px,
    linear-gradient(90deg,rgba(95,140,200,.18) 1px,transparent 1px) 0 0/26px 26px,
    #0a2647;
}

/* Parchment — warm cream, serif ink, light heritage chart */
[data-theme="parchment"] {
  --bg:#efe6d0; --panel:#f6efdd; --panel-2:#efe4c9; --line:#cdb88f;
  --text:#3a2c1c; --text-soft:#5a4632; --muted:#8a7355;
  --accent:#9a5b34; --accent-2:#7d4626; --accent-fg:#fdf6e6;
  --female:#b5536e; --male:#3f6f8f; --neutral:#7a6aa0;
  --danger:#b23b3b; --ok:#5b7d3a;
  --shadow:0 6px 18px rgba(120,90,40,.2);
  --radius:4px; --radius-2:3px; --radius-pill:14px; --card-radius:4px;
  --font:Georgia,"Iowan Old Style","Times New Roman",serif;
  --node-font:var(--font);
  --node-fill:#fbf5e6; --node-sel-fill:#f0e1bd;
  --link:#a98b5c; --link-spouse:#9a5b34;
  --toolbar-bg:rgba(246,239,221,.86);
  --canvas-bg:
    radial-gradient(circle at 1px 1px,rgba(160,130,80,.18) 1px,transparent 0) 0 0/26px 26px,
    #efe6d0;
}

/* Terminal — black, phosphor green, monospace, square */
[data-theme="terminal"] {
  --bg:#05080a; --panel:#0a0f0c; --panel-2:#0d140f; --line:#1c3a26;
  --text:#9dffc0; --text-soft:#5fd99a; --muted:#3f8f63;
  --accent:#33ff88; --accent-2:#19d96b; --accent-fg:#04130a;
  --female:#ff6ad5; --male:#42e0ff; --neutral:#caff5e;
  --danger:#ff5f5f; --ok:#33ff88;
  --shadow:0 8px 26px rgba(0,0,0,.6);
  --radius:0; --radius-2:0; --radius-pill:0; --card-radius:0;
  --font:ui-monospace,"Cascadia Code",Consolas,"Liberation Mono",monospace;
  --node-font:var(--font);
  --node-fill:#07110b; --node-sel-fill:#0f2417;
  --link:#1f6b3f; --link-spouse:#33ff88;
  --toolbar-bg:rgba(5,8,10,.85);
  --canvas-bg:
    linear-gradient(rgba(40,120,70,.16) 1px,transparent 1px) 0 0/24px 24px,
    linear-gradient(90deg,rgba(40,120,70,.16) 1px,transparent 1px) 0 0/24px 24px,
    #05080a;
}

/* Daylight — clean light, blue accent */
[data-theme="daylight"] {
  --bg:#eef1f5; --panel:#ffffff; --panel-2:#f4f6fa; --line:#d7dde6;
  --text:#1c2530; --text-soft:#41505f; --muted:#74808f;
  --accent:#2f7be0; --accent-2:#1f63bf; --accent-fg:#fff;
  --female:#e0568f; --male:#2f7be0; --neutral:#7b6cf0;
  --danger:#e04545; --ok:#1f9e72;
  --shadow:0 8px 24px rgba(30,50,80,.12);
  --radius:8px; --radius-2:6px; --radius-pill:20px; --card-radius:8px;
  --node-fill:#ffffff; --node-sel-fill:#e6f0ff;
  --link:#9aa7b6; --link-spouse:#7b6cf0;
  --toolbar-bg:rgba(255,255,255,.8);
  --canvas-bg:radial-gradient(circle at 1px 1px,#d4dae2 1px,transparent 0) 0 0/22px 22px,#eef1f5;
}

/* =========================================================================== */
* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font: 14px/1.45 var(--font);
}
body { display: flex; flex-direction: column; overflow: hidden; }
body, .topbar, .sidebar, .canvas-wrap, .btn, .tab, input, select, textarea {
  transition: background-color .25s ease, color .25s ease, border-color .25s ease;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 8px 14px; background: var(--panel);
  border-bottom: 1px solid var(--line); flex: none;
}
.brand { display: flex; align-items: baseline; gap: 8px; }
.logo { font-size: 20px; }
.brand-name { font-weight: 700; font-size: 18px; letter-spacing: .3px; }
.brand-sub { color: var(--muted); font-size: 12px; }

.tabs { display: flex; gap: 4px; align-items: center; flex: 1; overflow-x: auto; }
.tab {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 6px 12px; border-radius: var(--radius-2) var(--radius-2) 0 0;
  background: var(--panel-2); border: 1px solid var(--line); border-bottom: none;
  color: var(--muted); cursor: pointer; white-space: nowrap; font-size: 13px;
}
.tab.active { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.tab.big { background: transparent; border-style: dashed; }
.tab.big.active { background: var(--neutral); border-color: var(--neutral); color: var(--accent-fg); }
.tab .tab-edit { background: transparent; border: none; color: inherit; font: inherit; width: auto; min-width: 40px; }
.tab .tab-x { opacity: .6; font-size: 12px; }
.tab .tab-x:hover { opacity: 1; color: var(--danger); }
.tab-add {
  border: 1px dashed var(--line); background: transparent; color: var(--muted);
  border-radius: var(--radius-2); width: 30px; height: 30px; cursor: pointer; font-size: 18px;
}
.tab-add:hover { color: var(--text); border-color: var(--accent); }

.topbar-actions { display: flex; align-items: center; gap: 8px; flex: none; }

/* ---------- Buttons ---------- */
.btn {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: var(--radius-2);
  padding: 7px 12px; cursor: pointer; font-size: 13px; font-family: inherit;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn.primary:hover { background: var(--accent-2); }
.btn.icon { width: 32px; height: 32px; padding: 0; font-size: 16px; }
.btn.block { width: 100%; }
.btn.tiny { padding: 3px 8px; font-size: 12px; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .4; cursor: not-allowed; }

.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: 110%; z-index: 50;
  background: var(--panel-2); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  min-width: 220px; padding: 6px; display: none;
}
.menu-list.open { display: block; }
.menu-list button {
  display: block; width: 100%; text-align: left;
  background: transparent; border: none; color: var(--text);
  padding: 9px 10px; border-radius: var(--radius-2); cursor: pointer; font-size: 13px; font-family: inherit;
}
.menu-list button:hover { background: var(--panel); }
.menu-list button.checked { color: var(--accent); font-weight: 600; }
.menu-list .swatch { display: inline-block; width: 10px; height: 10px; margin-right: 8px; border-radius: 2px; vertical-align: middle; }

/* ---------- Workspace ---------- */
.workspace { display: flex; flex: 1; min-height: 0; }
.sidebar {
  width: 320px; flex: none; background: var(--panel);
  border-right: 1px solid var(--line); overflow-y: auto; padding: 14px;
}
.sidebar h3 { margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
.section { margin-bottom: 18px; }

.field { margin-bottom: 8px; }
.field label { display: block; font-size: 11px; color: var(--muted); margin-bottom: 3px; }
input, select, textarea {
  width: 100%; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: var(--radius-2); padding: 7px 9px; font: inherit;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
textarea { resize: vertical; min-height: 48px; }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; }

.primary-box {
  background: var(--panel-2); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 10px; margin-bottom: 14px;
}
.primary-box .who { font-weight: 600; }
.primary-box .hint { color: var(--muted); font-size: 12px; }

.people-list { display: flex; flex-direction: column; gap: 4px; }
.person-row {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 9px; border-radius: var(--radius-2); cursor: pointer;
  border: 1px solid transparent;
}
.person-row:hover { background: var(--panel-2); }
.person-row.selected { background: var(--panel-2); border-color: var(--accent); }
.person-row.primary-tag { box-shadow: inset 3px 0 0 var(--neutral); }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.dot.male { background: var(--male); }
.dot.female { background: var(--female); }
.dot.other, .dot.unknown { background: var(--neutral); }
.person-row .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person-row .rel { color: var(--muted); font-size: 11px; flex: none; max-width: 45%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: right; }

.editor { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.editor .add-rel { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin: 10px 0; }
.rel-group { margin-top: 10px; }
.rel-group h4 { margin: 0 0 5px; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius-pill); padding: 3px 6px 3px 10px; margin: 0 4px 4px 0; font-size: 12px;
}
.chip .x { cursor: pointer; opacity: .6; }
.chip .x:hover { opacity: 1; color: var(--danger); }

.inline-form { background: var(--panel); border: 1px dashed var(--line); border-radius: var(--radius-2); padding: 10px; margin-top: 8px; }
.inline-form .actions { display: flex; gap: 6px; margin-top: 8px; }

/* ---------- Canvas ---------- */
.canvas-wrap { flex: 1; min-width: 0; position: relative; display: flex; flex-direction: column; background: var(--canvas-bg); }
.canvas-toolbar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; border-bottom: 1px solid var(--line);
  background: var(--toolbar-bg); backdrop-filter: blur(4px);
}
.canvas-title { font-weight: 600; }
.spacer { flex: 1; }
.zoom-controls { display: flex; gap: 4px; }
.stage { flex: 1; width: 100%; height: 100%; cursor: grab; touch-action: none; }
.stage.panning { cursor: grabbing; }

.node-card { cursor: grab; }
.node-card:active { cursor: grabbing; }
.node-rect { fill: var(--node-fill); stroke: var(--line); stroke-width: 1.5; }
.node-card.male .node-rect { stroke: var(--male); }
.node-card.female .node-rect { stroke: var(--female); }
.node-card.other .node-rect, .node-card.unknown .node-rect { stroke: var(--neutral); }
.node-card.primary .node-rect { stroke: var(--neutral); stroke-width: 3; }
.node-card.selected .node-rect { fill: var(--node-sel-fill); stroke: var(--accent); stroke-width: 3; }
.node-strip { fill: var(--neutral); }
.node-card.male .node-strip { fill: var(--male); }
.node-card.female .node-strip { fill: var(--female); }
.node-card.other .node-strip, .node-card.unknown .node-strip { fill: var(--neutral); }
.node-name { fill: var(--text); font-weight: 600; font-size: 14px; font-family: var(--node-font); }
.node-rel { fill: var(--accent); font-size: 11px; font-family: var(--node-font); }
.node-dates { fill: var(--muted); font-size: 11px; font-family: var(--node-font); }
.link { fill: none; stroke: var(--link); stroke-width: 2; }
.link.spouse { stroke: var(--link-spouse); }
.link.spouse-ex { stroke: var(--link); stroke-dasharray: 5 5; }
.link.parent { stroke: var(--link); }
.union-dot { fill: var(--link-spouse); }
.tree-label { fill: var(--muted); font-size: 18px; font-weight: 700; font-family: var(--node-font); }
.tree-frame { fill: none; stroke: var(--line); stroke-dasharray: 6 6; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  padding: 10px 16px; border-radius: var(--radius); box-shadow: var(--shadow); z-index: 100;
}
.toast.err { border-color: var(--danger); }
.toast.ok { border-color: var(--ok); }

.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- Connect (circuit-board) mode ---------- */
#connectBtn.primary { background: var(--wire-strong); border-color: var(--wire-strong); color: var(--wire-fg); font-weight: 600; }
.stage.connect-mode { cursor: crosshair; }
.stage.connect-mode .node-card { cursor: crosshair; }
.stage.connect-mode .node-card .node-rect { stroke-dasharray: 4 3; }
.stage.connect-mode .node-card:hover .node-rect { stroke: var(--wire); stroke-width: 3; }
.wire-temp {
  fill: none; stroke: var(--wire); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round;
  pointer-events: none; filter: drop-shadow(0 0 5px var(--wire)); stroke-dasharray: 9 6;
}
.connect-menu {
  position: absolute; z-index: 60; min-width: 230px;
  background: var(--panel-2); border: 1px solid var(--wire-strong); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 8px;
}
.connect-menu .cm-title { font-size: 12px; color: var(--muted); padding: 4px 6px 8px; }
.connect-menu button {
  display: block; width: 100%; text-align: left; background: transparent;
  border: none; color: var(--text); padding: 8px 10px; border-radius: var(--radius-2);
  cursor: pointer; font-size: 13px; font-family: inherit;
}
.connect-menu button:hover { background: var(--panel); }
.connect-menu .cm-cancel { color: var(--muted); border-top: 1px solid var(--line); margin-top: 4px; }

/* ---------- Help / How-to view ---------- */
.help-view {
  position: absolute; inset: 49px 0 0 0; overflow-y: auto;
  padding: 32px 40px 80px; background: var(--bg);
}
.help-doc { max-width: 820px; margin: 0 auto; }
.help-doc h1 { font-size: 26px; margin: 0 0 6px; }
.help-doc .lead { color: var(--muted); margin: 0 0 28px; font-size: 15px; }
.help-doc h2 { font-size: 18px; margin: 30px 0 10px; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
.help-doc p, .help-doc li { color: var(--text-soft); }
.help-doc ol, .help-doc ul { padding-left: 22px; }
.help-doc li { margin: 6px 0; }
.help-doc b { color: var(--text); }
.help-doc kbd {
  background: var(--panel-2); border: 1px solid var(--line); border-bottom-width: 2px;
  border-radius: var(--radius-2); padding: 1px 6px; font-size: 12px; font-family: inherit;
}
.help-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; margin: 14px 0; }
.help-card.accent { border-color: var(--wire-strong); }
.help-pill {
  display: inline-block; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: var(--radius-2); padding: 1px 7px; font-size: 12px; color: var(--accent); margin: 0 2px;
}
.help-toc { display: flex; flex-direction: column; gap: 4px; }
.help-toc a { color: var(--muted); text-decoration: none; padding: 7px 9px; border-radius: var(--radius-2); font-size: 13px; }
.help-toc a:hover { background: var(--panel-2); color: var(--text); }
