:root {
  --bg: #f4f1ea;
  --paper: #ffffff;
  --ink: #2c2a26;
  --muted: #7b7468;
  --line: #d8cfc0;
  --accent: #3f7d58;
  --accent-dark: #2f5e42;
  --accent-soft: #e7f0ea;
  --male: #4a7fb5;
  --male-bg: #eaf2fa;
  --female: #b5618a;
  --female-bg: #fbecf4;
  --unknown: #8a8275;
  --unknown-bg: #f0ede6;
  --danger: #b3402e;
  --shadow: 0 2px 6px rgba(0,0,0,.08), 0 8px 24px rgba(0,0,0,.06);
  --radius: 14px;
  --font: "Segoe UI", "Helvetica Neue", system-ui, -apple-system, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  font-size: 17px;
}

body { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 0 rgba(0,0,0,.02);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-icon { font-size: 34px; line-height: 1; }
.brand h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: .2px;
  cursor: pointer;
}
.brand h1:hover { color: var(--accent); }
.brand-sub { margin: 2px 0 0; font-size: 13px; color: var(--muted); }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; }

.btn {
  font-family: inherit;
  font-size: 16px;
  font-weight: 600;
  padding: 11px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  transition: transform .05s ease, background .15s ease, box-shadow .15s ease, border-color .15s;
  white-space: nowrap;
}
.btn:hover { background: #faf8f3; box-shadow: var(--shadow); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .4; cursor: not-allowed; box-shadow: none; background: var(--paper); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-danger { background: #fff; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-soft { background: var(--accent-soft); border-color: #cfe2d6; color: var(--accent-dark); width: 100%; text-align: left; }
.btn-soft:hover { background: #dcebe1; }
.btn-big { font-size: 18px; padding: 14px 22px; }

/* ---------- Stage ---------- */
.stage { flex: 1; display: flex; min-height: 0; }

.canvas-wrap {
  position: relative;
  flex: 1;
  overflow: hidden;
  background:
    radial-gradient(circle at 1px 1px, rgba(0,0,0,.05) 1px, transparent 0);
  background-size: 26px 26px;
  background-color: var(--bg);
}
#canvas { width: 100%; height: 100%; display: block; cursor: grab; }
#canvas.dragging { cursor: grabbing; }

/* ---------- Person card ---------- */
.node-card { cursor: pointer; }
.node-rect {
  fill: var(--paper);
  stroke: var(--line);
  stroke-width: 1.5;
  rx: 12;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,.10));
  transition: stroke .15s;
}
.node-card:hover .node-rect { stroke: var(--accent); stroke-width: 2.5; }
.node-card.selected .node-rect { stroke: var(--accent); stroke-width: 3; }
.node-accent { rx: 0; }
.node-accent.M { fill: var(--male); }
.node-accent.F { fill: var(--female); }
.node-accent.U { fill: var(--unknown); }
.node-card.M .node-rect { fill: var(--male-bg); }
.node-card.F .node-rect { fill: var(--female-bg); }
.node-card.U .node-rect { fill: var(--unknown-bg); }

.node-name { font: 700 17px var(--font); fill: var(--ink); }
.node-maiden { font: 400 13px var(--font); fill: var(--muted); font-style: italic; }
.node-dates { font: 400 14px var(--font); fill: #5b554b; }
.node-place { font: 400 12px var(--font); fill: var(--muted); }
.node-avatar { font-size: 22px; }

.link-line { stroke: #b3a892; stroke-width: 2.5; fill: none; }
.link-couple { stroke: var(--accent); stroke-width: 3; }

/* ---------- Zoom controls ---------- */
.zoom-controls {
  position: absolute;
  right: 18px;
  bottom: 54px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.zoom-btn {
  width: 48px; height: 48px;
  font-size: 24px; font-weight: 700;
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 12px;
  cursor: pointer;
  box-shadow: var(--shadow);
  color: var(--ink);
}
.zoom-btn:hover { background: #faf8f3; color: var(--accent); }
.zoom-fit { font-size: 22px; }

.hint {
  position: absolute;
  left: 50%; bottom: 12px;
  transform: translateX(-50%);
  margin: 0;
  font-size: 13px;
  color: var(--muted);
  background: rgba(255,255,255,.8);
  padding: 4px 14px;
  border-radius: 20px;
  white-space: nowrap;
  pointer-events: none;
}

/* ---------- Empty state ---------- */
.empty-state {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  pointer-events: none;
}
.empty-card {
  pointer-events: auto;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 40px 44px;
  text-align: center;
  max-width: 460px;
  box-shadow: var(--shadow);
}
.empty-emoji { font-size: 56px; }
.empty-card h2 { margin: 12px 0 8px; font-size: 24px; }
.empty-card p { color: var(--muted); font-size: 16px; line-height: 1.5; margin: 0 0 24px; }
.empty-actions { display: flex; flex-direction: column; gap: 12px; }

/* ---------- Editor panel ---------- */
.editor {
  width: 380px;
  background: var(--paper);
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  box-shadow: -4px 0 16px rgba(0,0,0,.04);
}
.editor-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
}
.editor-head h2 { margin: 0; font-size: 20px; }
.icon-btn {
  border: none; background: transparent; font-size: 20px;
  cursor: pointer; color: var(--muted); width: 36px; height: 36px; border-radius: 8px;
}
.icon-btn:hover { background: #f0ede6; color: var(--ink); }

.editor-body { padding: 16px 20px; overflow-y: auto; flex: 1; }
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 14px; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
.field input, .field select, .field textarea {
  width: 100%;
  font-family: inherit;
  font-size: 16px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fdfcf9;
  color: var(--ink);
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none; border-color: var(--accent); background: #fff;
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.field textarea { resize: vertical; }
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }

.editor-relations { padding: 12px 20px; border-top: 1px solid var(--line); background: #fbfaf6; }
.editor-relations h3 { margin: 4px 0 12px; font-size: 15px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.rel-buttons { display: flex; flex-direction: column; gap: 9px; }
.rel-hint { margin: 0 0 10px; font-size: 13px; color: var(--muted); line-height: 1.4; }
.rel-empty { margin: 8px 0 0; font-size: 13px; color: var(--muted); font-style: italic; }
.link-select {
  width: 100%;
  font-family: inherit;
  font-size: 16px;
  padding: 10px 12px;
  margin-bottom: 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fdfcf9;
  color: var(--ink);
}
.link-select:focus { outline: none; border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px var(--accent-soft); }

/* ---------- Sekcje zwijane w edytorze ---------- */
.rel-section { border-top: 1px solid var(--line); margin-top: 8px; }
.sec-head {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: none;
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--muted);
  padding: 14px 2px 12px;
}
.sec-head:hover { color: var(--accent-dark); }
.chev { font-size: 13px; transition: transform .18s ease; }
.rel-section.collapsed .chev { transform: rotate(-90deg); }
.rel-section.collapsed .sec-body { display: none; }
.sec-body { padding: 0 2px 12px; }

/* ---------- Lista obecnych powiązań ---------- */
.conn-list { display: flex; flex-direction: column; gap: 12px; }
.conn-group-label { font-size: 12px; font-weight: 700; color: var(--muted); margin: 2px 0 6px; text-transform: uppercase; letter-spacing: .4px; }
.conn-item {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: #fbfaf6;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 10px 8px 12px;
  font-size: 15px;
  margin-bottom: 6px;
}
.conn-item:last-child { margin-bottom: 0; }
.conn-x {
  border: none; background: transparent; color: var(--muted);
  width: 30px; height: 30px; border-radius: 7px; cursor: pointer; font-size: 15px; flex-shrink: 0;
}
.conn-x:hover { background: var(--danger); color: #fff; }
.conn-none { font-size: 14px; color: var(--muted); font-style: italic; margin: 2px 0; }

.editor-foot { padding: 14px 20px; border-top: 1px solid var(--line); }
.editor-foot .btn { width: 100%; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%; bottom: 28px;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 12px 22px;
  border-radius: 30px;
  font-size: 15px;
  font-weight: 600;
  box-shadow: var(--shadow);
  z-index: 100;
  transition: opacity .25s, transform .25s;
}
.toast.hidden { opacity: 0; transform: translateX(-50%) translateY(12px); pointer-events: none; }

.hidden { display: none !important; }

@media (max-width: 820px) {
  .editor { position: absolute; right: 0; top: 0; bottom: 0; z-index: 50; width: min(380px, 92vw); }
  .brand-sub { display: none; }
}
