/* ==================== DESIGN TOKENS ==================== */
:root {
  --bg: #f6f2ea;
  --bg-canvas: #faf7f2;
  --bg-surface: #ffffff;
  --bg-subtle: #f1ece2;
  --bg-hover: #ebe4d7;
  --border: #e3dbcc;
  --border-strong: #cfc4b0;

  --text: #1c2520;
  --text-muted: #616d65;
  --text-soft: #8a948d;

  --primary: #1b4332;
  --primary-hover: #143326;
  --primary-soft: #e7f0eb;
  --accent-gold: #c59b4b;
  --accent-gold-soft: #fbf4e4;
  --accent-male: #2d6a4f;
  --accent-female: #b56576;
  --accent-other: #c59b4b;
  --danger: #b83b3b;
  --danger-soft: #fbeaea;

  --shadow-sm: 0 2px 8px rgba(20, 35, 27, 0.06);
  --shadow-md: 0 10px 28px rgba(20, 35, 27, 0.10);
  --shadow-lg: 0 20px 50px rgba(15, 28, 22, 0.18);

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --topbar-h: 64px;
  --sidebar-w: 296px;
  --drawer-w: 410px;

  --font-serif: "Cormorant Garamond", Georgia, serif;
  --font-sans: "Plus Jakarta Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

[data-theme="dark"] {
  --bg: #0c1116;
  --bg-canvas: #0f1419;
  --bg-surface: #172027;
  --bg-subtle: #1e2932;
  --bg-hover: #25323d;
  --border: #2a3742;
  --border-strong: #394a58;

  --text: #f0ede6;
  --text-muted: #9aa8b4;
  --text-soft: #6f7e8a;

  --primary: #3b8d68;
  --primary-hover: #4aa37b;
  --primary-soft: rgba(59, 141, 104, 0.16);
  --accent-gold: #d4af37;
  --accent-gold-soft: rgba(212, 175, 55, 0.15);
  --accent-male: #45a077;
  --accent-female: #d47a8e;
  --accent-other: #d4af37;
  --danger: #e05d5d;
  --danger-soft: rgba(224, 93, 93, 0.16);

  --shadow-sm: 0 2px 10px rgba(0, 0, 0, 0.25);
  --shadow-md: 0 12px 30px rgba(0, 0, 0, 0.38);
  --shadow-lg: 0 24px 55px rgba(0, 0, 0, 0.55);
}

/* ==================== RESET & BASE ==================== */
*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

.hidden {
  display: none !important;
}

/* ==================== LAYOUT SHELL ==================== */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.topbar {
  height: var(--topbar-h);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  z-index: 30;
}

.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
}
.brand:hover {
  background: var(--bg-subtle);
}

.brand-crest {
  width: 38px;
  height: 38px;
  border-radius: 11px;
  background: linear-gradient(135deg, var(--primary), #2d6a4f);
  color: #f4e6c3;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 3px 10px rgba(27, 67, 50, 0.25);
  flex-shrink: 0;
}

.brand-text h1 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 20px;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: 0.01em;
}

.brand-text span {
  display: block;
  font-size: 11.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 220px;
}

/* Navigation Tabs */
.main-nav {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-subtle);
  padding: 4px;
  border-radius: 11px;
  border: 1px solid var(--border);
}

.nav-tab {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 7px 14px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.18s;
}
.nav-tab:hover {
  color: var(--text);
}
.nav-tab.is-active {
  background: var(--bg-surface);
  color: var(--primary);
  box-shadow: var(--shadow-sm);
}

.storage-badge {
  font-size: 11.5px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 99px;
  background: var(--primary-soft);
  color: var(--primary);
  white-space: nowrap;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 9px 16px;
  font-weight: 600;
  font-size: 13.5px;
  cursor: pointer;
  transition: all 0.16s;
  white-space: nowrap;
}
.btn-sm {
  padding: 7px 12px;
  font-size: 12.5px;
}
.btn-primary {
  background: var(--primary);
  color: #ffffff;
  box-shadow: 0 2px 8px rgba(27, 67, 50, 0.22);
}
.btn-primary:hover {
  background: var(--primary-hover);
  transform: translateY(-1px);
}
.btn-secondary {
  background: var(--bg-surface);
  border-color: var(--border-strong);
  color: var(--text);
}
.btn-secondary:hover {
  background: var(--bg-subtle);
}
.btn-gold {
  background: var(--accent-gold-soft);
  border-color: var(--accent-gold);
  color: #8a641b;
}
[data-theme="dark"] .btn-gold {
  color: #e8c766;
}
.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}
.btn-ghost:hover {
  background: var(--bg-subtle);
  color: var(--text);
}
.btn-danger {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: transparent;
}
.btn-danger:hover {
  background: var(--danger);
  color: #fff;
}

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 9px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s;
}
.icon-btn:hover:not(:disabled) {
  background: var(--bg-subtle);
  color: var(--text);
}
.icon-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

/* Dropdown */
.dropdown {
  position: relative;
}
.dropdown-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 240px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: 6px;
  display: none;
  flex-direction: column;
  z-index: 60;
}
.dropdown-menu.is-open {
  display: flex;
}
.dropdown-menu button {
  border: none;
  background: transparent;
  text-align: left;
  padding: 9px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 8px;
}
.dropdown-menu button:hover {
  background: var(--bg-subtle);
}
.dropdown-menu hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 5px 4px;
}
.text-danger {
  color: var(--danger) !important;
}

/* ==================== WORKSPACE ==================== */
.workspace {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

/* LEFT SIDEBAR */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.24s ease;
  z-index: 20;
}
.sidebar.is-collapsed {
  margin-left: calc(-1 * var(--sidebar-w));
}

.sidebar-header {
  padding: 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0 11px;
  transition: border-color 0.15s;
}
.search-box:focus-within {
  border-color: var(--accent-gold);
  background: var(--bg-surface);
}
.search-box svg {
  color: var(--text-soft);
  flex-shrink: 0;
}
.search-box input {
  width: 100%;
  border: none;
  background: transparent;
  padding: 9px 0;
  outline: none;
  font-size: 13px;
}

.filter-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.chip {
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: 99px;
  font-size: 11.5px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.chip:hover {
  border-color: var(--accent-gold);
  color: var(--text);
}
.chip.is-active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.sidebar-count {
  padding: 8px 14px;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-soft);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.member-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 10px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.member-item {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 9px 10px;
  border-radius: 11px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: all 0.14s;
}
.member-item:hover {
  background: var(--bg-subtle);
}
.member-item.is-selected {
  background: var(--primary-soft);
  border-color: rgba(27, 67, 50, 0.25);
}

.mini-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  background: var(--bg-subtle);
  color: var(--primary);
  border: 2px solid var(--border);
}
.mini-avatar.gender-m { border-color: var(--accent-male); }
.mini-avatar.gender-f { border-color: var(--accent-female); }
.mini-avatar.gender-o { border-color: var(--accent-other); }

.member-info {
  min-width: 0;
  flex: 1;
}
.member-name {
  font-weight: 600;
  font-size: 13.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.member-meta {
  font-size: 11.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ==================== MAIN STAGE ==================== */
.main-stage {
  flex: 1;
  min-width: 0;
  position: relative;
  background: var(--bg-canvas);
  overflow: hidden;
}

.stage-view {
  position: absolute;
  inset: 0;
  display: none;
  overflow-y: auto;
}
.stage-view.is-active {
  display: block;
}
#view-tree {
  overflow: hidden;
}

/* Tree Canvas */
.tree-canvas {
  width: 100%;
  height: 100%;
  background-color: var(--bg-canvas);
  background-image: radial-gradient(var(--border) 1px, transparent 1px);
  background-size: 24px 24px;
}

.tree-svg {
  display: block;
  cursor: grab;
  user-select: none;
}
.tree-svg.is-grabbing {
  cursor: grabbing;
}

/* SVG Tree Nodes & Edges */
.tree-node {
  cursor: pointer;
  transition: opacity 0.2s ease;
}
.tree-node.is-dimmed {
  opacity: 0.26;
}
.node-card-bg {
  fill: var(--bg-surface);
  stroke: var(--border);
  stroke-width: 1.5;
  transition: stroke 0.18s, stroke-width 0.18s;
}
.tree-node:hover .node-card-bg {
  stroke: var(--accent-gold);
  stroke-width: 2;
}
.tree-node.is-selected .node-card-bg,
.tree-node.is-path .node-card-bg {
  stroke: var(--accent-gold);
  stroke-width: 2.6;
}
.tree-node.is-search-match .node-card-bg {
  stroke: var(--primary);
  stroke-width: 2.6;
}

.gender-m .node-accent-bar { fill: var(--accent-male); }
.gender-f .node-accent-bar { fill: var(--accent-female); }
.gender-o .node-accent-bar { fill: var(--accent-other); }

.node-avatar-fallback {
  fill: var(--bg-subtle);
  stroke: var(--accent-gold);
  stroke-width: 1.4;
}
.node-avatar-ring {
  fill: none;
  stroke: var(--accent-gold);
  stroke-width: 1.8;
}
.node-avatar-initials {
  fill: var(--primary);
  font-size: 16px;
  font-weight: 700;
}
.node-status-living {
  fill: #40916c;
  stroke: var(--bg-surface);
  stroke-width: 2;
}
.node-status-deceased {
  fill: #9b8f7e;
  stroke: var(--bg-surface);
  stroke-width: 2;
}
.node-name {
  fill: var(--text);
  font-size: 14px;
  font-weight: 700;
}
.node-sub {
  fill: var(--text-muted);
  font-size: 11.5px;
}
.node-years {
  fill: #8d6822;
  font-size: 11.5px;
  font-weight: 600;
}
[data-theme="dark"] .node-years {
  fill: #e3c15d;
}
.node-bio-badge {
  fill: var(--accent-gold);
  font-size: 12px;
}

.node-quick-btn {
  opacity: 0;
  transition: opacity 0.16s ease;
  cursor: pointer;
}
.tree-node:hover .node-quick-btn,
.tree-node.is-selected .node-quick-btn {
  opacity: 1;
}
.node-quick-btn-bg {
  fill: var(--primary);
  stroke: var(--bg-surface);
  stroke-width: 2;
}
.node-quick-btn:hover .node-quick-btn-bg {
  fill: var(--accent-gold);
}
.node-quick-btn-icon {
  fill: #ffffff;
  font-size: 14px;
  font-weight: 700;
}

.tree-edge-child {
  stroke: var(--border-strong);
  stroke-width: 2;
  transition: stroke 0.2s, stroke-width 0.2s;
}
.tree-edge-child.is-highlighted {
  stroke: var(--accent-gold);
  stroke-width: 3.5;
}
.tree-edge-marriage {
  stroke: var(--accent-gold);
  stroke-width: 2.2;
}
.tree-edge-marriage.is-divorced {
  stroke-dasharray: 5 4;
  stroke: #b57b7b;
}
.tree-edge-marriage.is-widowed {
  stroke-dasharray: 7 3;
  stroke: #9b8f7e;
}
.tree-edge-marriage.is-highlighted {
  stroke-width: 3.6;
}
.marriage-badge-circle {
  fill: var(--bg-surface);
  stroke: var(--accent-gold);
  stroke-width: 1.6;
}
.marriage-badge-circle.is-divorced {
  stroke: #b57b7b;
}
.marriage-badge-circle.is-widowed {
  stroke: #9b8f7e;
}
.marriage-badge-icon {
  fill: var(--accent-gold);
  font-size: 11px;
}
.marriage-year-label {
  fill: var(--text-muted);
  font-size: 10.5px;
  font-weight: 600;
}

/* Floating Tree Toolbars */
.tree-toolbar-top {
  position: absolute;
  top: 16px;
  left: 16px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  z-index: 15;
}

.segmented {
  display: inline-flex;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
  box-shadow: var(--shadow-sm);
}
.segmented-full {
  width: 100%;
}
.segmented-full .seg-btn {
  flex: 1;
}
.seg-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 6px 12px;
  border-radius: 7px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.seg-btn:hover {
  color: var(--text);
}
.seg-btn.is-active {
  background: var(--primary);
  color: #fff;
}

.tree-toolbar-bottom {
  position: absolute;
  bottom: 18px;
  left: 18px;
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 4px;
  box-shadow: var(--shadow-md);
  z-index: 15;
}

.zoom-btn {
  min-width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  border-radius: 7px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.zoom-btn:hover {
  background: var(--bg-subtle);
}
.zoom-btn-wide {
  padding: 0 11px;
  font-size: 12px;
}
.zoom-label {
  min-width: 44px;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}
.toolbar-sep {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 3px;
}

/* Quick Add Popover */
.quick-add-popover {
  position: fixed;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  padding: 6px;
  min-width: 220px;
  z-index: 80;
  display: flex;
  flex-direction: column;
}
.quick-add-title {
  padding: 6px 10px;
  font-size: 11.5px;
  font-weight: 700;
  color: var(--text-soft);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.quick-add-popover button {
  border: none;
  background: transparent;
  text-align: left;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
}
.quick-add-popover button:hover {
  background: var(--bg-subtle);
}

/* Empty State */
.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 12;
}
.empty-card {
  max-width: 460px;
  text-align: center;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 36px 32px;
  box-shadow: var(--shadow-md);
}
.empty-icon {
  font-size: 42px;
  margin-bottom: 10px;
}
.empty-card h2 {
  font-family: var(--font-serif);
  font-size: 28px;
  margin: 0 0 10px;
}
.empty-card p {
  color: var(--text-muted);
  margin: 0 0 22px;
}
.empty-actions {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
}

/* ==================== PROFILE DRAWER ==================== */
.profile-drawer {
  width: var(--drawer-w);
  flex-shrink: 0;
  background: var(--bg-surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 25;
  box-shadow: -8px 0 28px rgba(15, 28, 22, 0.07);
}
.drawer-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

.drawer-top-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.profile-hero {
  display: flex;
  gap: 16px;
  align-items: center;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.profile-avatar {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid var(--accent-gold);
  background: var(--bg-subtle);
  color: var(--primary);
  font-family: var(--font-serif);
  font-size: 28px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.profile-hero h2 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 26px;
  line-height: 1.15;
}
.profile-maiden {
  font-size: 12.5px;
  color: var(--text-muted);
  font-style: italic;
}
.profile-dates {
  margin-top: 4px;
  font-size: 13px;
  font-weight: 600;
  color: var(--accent-gold);
}
.profile-occ {
  font-size: 12.5px;
  color: var(--text-muted);
  margin-top: 2px;
}

.quick-rel-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin: 14px 0;
}
.quick-rel-btn {
  border: 1px solid var(--border);
  background: var(--bg-subtle);
  border-radius: 9px;
  padding: 8px 4px;
  font-size: 11.5px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  transition: all 0.15s;
}
.quick-rel-btn:hover {
  border-color: var(--accent-gold);
  background: var(--accent-gold-soft);
}

.drawer-tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}
.drawer-tab {
  border: none;
  background: transparent;
  padding: 8px 11px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.drawer-tab.is-active {
  color: var(--primary);
  border-bottom-color: var(--accent-gold);
}

.info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}
.info-card {
  background: var(--bg-subtle);
  border-radius: 10px;
  padding: 10px 12px;
}
.info-card label {
  display: block;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-soft);
  font-weight: 700;
  margin-bottom: 3px;
}
.info-card div {
  font-size: 13px;
  font-weight: 600;
}

.bio-box {
  background: var(--bg-canvas);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 13.5px;
  line-height: 1.65;
  white-space: pre-wrap;
  margin-bottom: 14px;
}
.bio-empty {
  color: var(--text-soft);
  font-style: italic;
}

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 14px;
}
.tag-pill {
  background: var(--accent-gold-soft);
  color: #8a641b;
  padding: 3px 10px;
  border-radius: 99px;
  font-size: 11.5px;
  font-weight: 600;
}
[data-theme="dark"] .tag-pill {
  color: #e8c766;
}

.rel-group {
  margin-bottom: 14px;
}
.rel-group-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-soft);
  margin-bottom: 6px;
}
.rel-person-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--bg-subtle);
  margin-bottom: 6px;
  cursor: pointer;
  transition: background 0.14s;
}
.rel-person-row:hover {
  background: var(--bg-hover);
}
.rel-person-main {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.person-event-card {
  position: relative;
  padding: 10px 12px 10px 16px;
  border-left: 3px solid var(--accent-gold);
  background: var(--bg-subtle);
  border-radius: 0 10px 10px 0;
  margin-bottom: 10px;
}
.person-event-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}
.person-event-year {
  font-size: 11.5px;
  font-weight: 700;
  color: var(--accent-gold);
}
.person-event-title {
  font-weight: 700;
  font-size: 13.5px;
}
.person-event-desc {
  font-size: 12.5px;
  color: var(--text-muted);
  margin-top: 4px;
}

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-top: 10px;
}
.gallery-item {
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--bg-subtle);
  position: relative;
}
.gallery-item img {
  width: 100%;
  height: 115px;
  object-fit: cover;
  display: block;
}
.gallery-caption {
  padding: 6px 8px;
  font-size: 11.5px;
  color: var(--text-muted);
}
.gallery-del {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  cursor: pointer;
  font-size: 12px;
}

.drawer-footer {
  display: flex;
  gap: 8px;
  padding-top: 16px;
  margin-top: 16px;
  border-top: 1px solid var(--border);
}

/* ==================== SECONDARY VIEWS (TIMELINE / CHRONICLE / KINSHIP) ==================== */
.view-container {
  max-width: 980px;
  margin: 0 auto;
  padding: 28px 24px 60px;
}

.view-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 14px;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
}
.view-header h2 {
  margin: 0 0 4px;
  font-family: var(--font-serif);
  font-size: 32px;
}
.view-header p {
  margin: 0;
  color: var(--text-muted);
}

/* Timeline Feed */
.timeline-feed {
  position: relative;
  padding-left: 28px;
}
.timeline-feed::before {
  content: "";
  position: absolute;
  left: 9px;
  top: 6px;
  bottom: 6px;
  width: 2px;
  background: var(--border-strong);
}
.tl-item {
  position: relative;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 18px;
  margin-bottom: 14px;
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s;
}
.tl-item:hover {
  transform: translateX(3px);
  border-color: var(--accent-gold);
}
.tl-dot {
  position: absolute;
  left: -25px;
  top: 18px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent-gold);
  border: 3px solid var(--bg-canvas);
}
.tl-dot.type-birth { background: #2d6a4f; }
.tl-dot.type-marriage { background: #c59b4b; }
.tl-dot.type-death { background: #7f7970; }
.tl-dot.type-event { background: #4a6fa5; }

.tl-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.tl-date {
  font-weight: 700;
  color: var(--primary);
}
.tl-badge {
  padding: 2px 8px;
  border-radius: 99px;
  background: var(--bg-subtle);
  font-weight: 600;
  font-size: 11px;
}
.tl-title {
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 3px;
}
.tl-desc {
  font-size: 13px;
  color: var(--text-muted);
}

/* Chronicle & Stats */
.chronicle-hero {
  background: linear-gradient(135deg, var(--primary), #244f3c);
  color: #faf6ee;
  border-radius: 18px;
  padding: 28px 30px;
  margin-bottom: 22px;
  box-shadow: var(--shadow-md);
}
.chronicle-hero h2 {
  margin: 0 0 6px;
  font-family: var(--font-serif);
  font-size: 36px;
}
.chronicle-hero p {
  margin: 0;
  opacity: 0.9;
  max-width: 720px;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}
.kpi-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  box-shadow: var(--shadow-sm);
}
.kpi-value {
  font-family: var(--font-serif);
  font-size: 32px;
  font-weight: 700;
  color: var(--primary);
  line-height: 1.1;
}
.kpi-label {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
  font-weight: 600;
}

.stats-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
  margin-bottom: 24px;
}
.panel-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 20px;
  box-shadow: var(--shadow-sm);
}
.panel-card h3 {
  margin: 0 0 14px;
  font-family: var(--font-serif);
  font-size: 22px;
}
.bar-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 9px;
  font-size: 13px;
}
.bar-label {
  width: 120px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bar-track {
  flex: 1;
  height: 9px;
  background: var(--bg-subtle);
  border-radius: 99px;
  overflow: hidden;
}
.bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent-gold));
  border-radius: 99px;
}
.bar-num {
  width: 28px;
  text-align: right;
  font-weight: 700;
  color: var(--text-muted);
}

/* Kinship View */
.kinship-panel {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px;
  box-shadow: var(--shadow-sm);
}
.kinship-selectors {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 14px;
  align-items: end;
  margin-bottom: 22px;
}
.swap-btn {
  border: 1px solid var(--border-strong);
  margin-bottom: 2px;
  font-size: 16px;
}
.kinship-result-box {
  background: var(--bg-canvas);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 22px;
  text-align: center;
}
.kinship-big-label {
  font-family: var(--font-serif);
  font-size: 32px;
  font-weight: 700;
  color: var(--primary);
  margin: 6px 0 8px;
}
.kinship-steps {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin: 18px 0;
}
.kinship-step-pill {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: 99px;
  font-size: 12.5px;
  font-weight: 600;
}
.kinship-step-arrow {
  color: var(--accent-gold);
  font-size: 12px;
  font-weight: 700;
}

/* ==================== MODALS & FORMS ==================== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(12, 20, 16, 0.55);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}
.modal {
  width: 100%;
  max-width: 540px;
  max-height: 90vh;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-lg {
  max-width: 680px;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 24px;
}
.modal-body {
  padding: 18px 20px;
  overflow-y: auto;
}
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.form-section-title {
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--accent-gold);
  margin: 16px 0 8px;
}
.form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 11px;
}
.form-group label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}
.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 9px 11px;
  border-radius: 9px;
  border: 1px solid var(--border-strong);
  background: var(--bg-canvas);
  outline: none;
  transition: border-color 0.15s;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  border-color: var(--primary);
  background: var(--bg-surface);
}
.form-grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.form-grid-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
.form-checkbox-row {
  margin: 4px 0 10px;
}
.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  cursor: pointer;
}
.form-hint {
  margin: 5px 0 0;
  font-size: 11.5px;
  color: var(--text-soft);
}

.form-photo-row {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 14px;
  padding: 12px;
  background: var(--bg-subtle);
  border-radius: 12px;
}
.photo-preview-wrap {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  overflow: hidden;
  border: 2px solid var(--accent-gold);
  background: var(--bg-surface);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.photo-preview {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.photo-placeholder {
  font-size: 26px;
}

/* ==================== TOASTS ==================== */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 150;
}
.toast {
  background: var(--primary);
  color: #fff;
  padding: 11px 16px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow-md);
  animation: toastIn 0.22s ease;
}
.toast.is-error {
  background: var(--danger);
}
@keyframes toastIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ==================== RESPONSIVE ==================== */
@media (max-width: 1024px) {
  .brand-text span,
  .storage-badge {
    display: none;
  }
  .sidebar {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    box-shadow: var(--shadow-lg);
  }
  .profile-drawer {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    max-width: 100%;
  }
  .stats-columns {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 720px) {
  .topbar {
    padding: 0 10px;
  }
  .nav-tab span {
    display: none;
  }
  .form-grid-2,
  .form-grid-3,
  .kinship-selectors {
    grid-template-columns: 1fr;
  }
}

/* ==================== PRINT ==================== */
@media print {
  .topbar, .sidebar, .profile-drawer, .tree-toolbar-top, .tree-toolbar-bottom, .toast-container {
    display: none !important;
  }
  .workspace, .main-stage, #view-tree, .tree-canvas {
    position: static !important;
    width: 100% !important;
    height: 100vh !important;
    overflow: visible !important;
    background: #fff !important;
  }
}

/* ==================== 2D BRANCH COLLAPSE PILL ==================== */
.node-collapse-btn {
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.16s ease;
}
.tree-node:hover .node-collapse-btn,
.tree-node.is-selected .node-collapse-btn,
.node-collapse-btn.is-collapsed {
  opacity: 1;
}
.node-collapse-bg {
  fill: var(--bg-surface);
  stroke: var(--accent-gold);
  stroke-width: 1.5;
}
.node-collapse-btn.is-collapsed .node-collapse-bg {
  fill: var(--accent-gold);
}
.node-collapse-txt {
  fill: var(--primary);
  font-size: 11px;
  font-weight: 700;
}
.node-collapse-btn.is-collapsed .node-collapse-txt {
  fill: #ffffff;
}

/* ==================== INTERACTIVE 3D TREE VIEW ==================== */
#view-tree3d {
  overflow: hidden;
  background:
    radial-gradient(ellipse at 50% 25%, rgba(197, 155, 75, 0.12) 0%, transparent 60%),
    radial-gradient(ellipse at 50% 95%, rgba(27, 67, 50, 0.10) 0%, transparent 60%),
    var(--bg-canvas);
}

[data-theme="dark"] #view-tree3d {
  background:
    radial-gradient(ellipse at 50% 25%, rgba(212, 175, 55, 0.10) 0%, transparent 58%),
    radial-gradient(ellipse at 50% 95%, rgba(45, 106, 79, 0.16) 0%, transparent 62%),
    #0b1015;
}

.tree3d-canvas {
  width: 100%;
  height: 100%;
  position: relative;
}

.scene3d {
  width: 100%;
  height: 100%;
  perspective: 1350px;
  perspective-origin: 50% 48%;
  overflow: hidden;
  position: relative;
  user-select: none;
  cursor: grab;
}
.scene3d[data-tool="orbit"] {
  cursor: move;
}
.scene3d.is-dragging {
  cursor: grabbing;
}

.scene3d-camera {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 0;
  height: 0;
  transform-style: preserve-3d;
  will-change: transform;
}
.scene3d-camera.is-animating {
  transition: transform 0.45s cubic-bezier(0.22, 1, 0.36, 1);
}

.scene3d-world {
  position: relative;
  transform-style: preserve-3d;
}

.scene3d-edges {
  position: absolute;
  overflow: visible;
  pointer-events: none;
  transform: translateZ(0px);
}

.edge3d-child {
  stroke: var(--border-strong);
  stroke-width: 2.4;
  transition: stroke 0.25s, stroke-width 0.25s, filter 0.25s;
}
.edge3d-child.is-lit {
  stroke: var(--accent-gold);
  stroke-width: 3.8;
  filter: drop-shadow(0 0 6px rgba(197, 155, 75, 0.65));
}

.edge3d-marriage {
  stroke: var(--accent-gold);
  stroke-width: 2.6;
}
.edge3d-marriage.is-divorced {
  stroke-dasharray: 5 4;
  stroke: #b57b7b;
}
.edge3d-marriage.is-widowed {
  stroke-dasharray: 7 3;
  stroke: #9b8f7e;
}
.edge3d-marriage.is-lit {
  stroke-width: 4;
  filter: drop-shadow(0 0 6px rgba(197, 155, 75, 0.7));
}

/* Floating generation terrace bands */
.scene3d-tiers {
  position: absolute;
  inset: 0;
  pointer-events: none;
  transform-style: preserve-3d;
}
.tier3d-band {
  position: absolute;
  border-radius: 22px;
  background: linear-gradient(90deg, rgba(197, 155, 75, 0.06), rgba(27, 67, 50, 0.04));
  border: 1px dashed rgba(197, 155, 75, 0.24);
  transform: translateZ(-8px);
}
[data-theme="dark"] .tier3d-band {
  background: linear-gradient(90deg, rgba(212, 175, 55, 0.06), rgba(59, 141, 104, 0.05));
  border-color: rgba(212, 175, 55, 0.2);
}
.tier3d-label {
  position: absolute;
  left: 14px;
  top: -11px;
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 2px 9px;
  border-radius: 99px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--accent-gold);
}

/* 3D Floating Person Cards */
.scene3d-cards {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
}

.card3d {
  position: absolute;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px 12px 16px;
  border-radius: 15px;
  background: var(--bg-surface);
  border: 1.5px solid var(--border);
  box-shadow:
    0 12px 24px rgba(15, 28, 22, 0.14),
    0 2px 0 rgba(197, 155, 75, 0.22);
  cursor: pointer;
  transform: translateZ(18px);
  transform-style: preserve-3d;
  transition: transform 0.26s cubic-bezier(0.22, 1, 0.36, 1), box-shadow 0.26s ease, border-color 0.2s ease, opacity 0.24s ease;
}

.card3d:hover {
  transform: translateZ(42px) scale(1.04);
  border-color: var(--accent-gold);
  box-shadow:
    0 24px 40px rgba(15, 28, 22, 0.24),
    0 0 0 2px rgba(197, 155, 75, 0.35);
  z-index: 20;
}

.card3d.is-selected {
  transform: translateZ(48px) scale(1.05);
  border-color: var(--accent-gold);
  box-shadow:
    0 26px 46px rgba(15, 28, 22, 0.28),
    0 0 0 3px rgba(197, 155, 75, 0.55);
  z-index: 30;
}

.card3d.is-lit {
  border-color: var(--accent-gold);
}

.card3d.is-dimmed {
  opacity: 0.25;
  transform: translateZ(6px);
}

.card3d-accent {
  position: absolute;
  left: 0;
  top: 12px;
  bottom: 12px;
  width: 5px;
  border-radius: 0 3px 3px 0;
  background: var(--accent-other);
}
.card3d.gender-m .card3d-accent { background: var(--accent-male); }
.card3d.gender-f .card3d-accent { background: var(--accent-female); }

.card3d-avatar-wrap {
  position: relative;
  flex-shrink: 0;
}
.card3d-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--accent-gold);
  background: var(--bg-subtle);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  color: var(--primary);
}
.card3d-status {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 2px solid var(--bg-surface);
}
.card3d-status.is-living { background: #40916c; }
.card3d-status.is-deceased { background: #9b8f7e; }

.card3d-body {
  min-width: 0;
  flex: 1;
}
.card3d-name {
  font-weight: 700;
  font-size: 13.5px;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card3d-sub {
  font-size: 11.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}
.card3d-years {
  font-size: 11.5px;
  font-weight: 700;
  color: #8d6822;
  margin-top: 4px;
}
[data-theme="dark"] .card3d-years {
  color: #e3c15d;
}

.card3d-add {
  position: absolute;
  top: -9px;
  right: -9px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid var(--bg-surface);
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.16s, background 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.card3d:hover .card3d-add,
.card3d.is-selected .card3d-add {
  opacity: 1;
}
.card3d-add:hover {
  background: var(--accent-gold);
}

.card3d-collapse {
  position: absolute;
  left: 50%;
  bottom: -11px;
  transform: translateX(-50%);
  height: 22px;
  min-width: 28px;
  padding: 0 8px;
  border-radius: 99px;
  border: 1.5px solid var(--accent-gold);
  background: var(--bg-surface);
  color: var(--primary);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: all 0.15s;
}
.card3d-collapse:hover {
  background: var(--accent-gold-soft);
}
.card3d-collapse.is-collapsed {
  background: var(--accent-gold);
  color: #fff;
}

/* 3D HUD Toolbars */
.hud3d-top-bar {
  position: absolute;
  top: 14px;
  left: 14px;
  right: 14px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  z-index: 20;
  pointer-events: none;
}
.hud3d-top-left,
.hud3d-top-right {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  pointer-events: auto;
}
.hud3d-top-bar .btn-sm {
  padding: 6px 10px;
  font-size: 12px;
}
.hud3d-top-bar .seg-btn {
  padding: 5px 10px;
  font-size: 12px;
}
.hud3d-bottom {
  position: absolute;
  bottom: 16px;
  left: 16px;
  right: 16px;
  width: fit-content;
  max-width: calc(100% - 32px);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px 12px;
  box-shadow: var(--shadow-md);
  z-index: 20;
}
.hud3d-sliders {
  display: flex;
  align-items: center;
  gap: 12px;
}
.slider3d {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-muted);
}
.slider3d input[type="range"] {
  width: 78px;
  accent-color: var(--primary);
  cursor: pointer;
}
.hud3d-hint {
  font-size: 11px;
  color: var(--text-soft);
  padding-left: 4px;
}

@media (max-width: 900px) {
  .hud3d-hint {
    display: none;
  }
}

/* ==================== TRUE XYZ 3D SPHERE & DRAGGABLE NODES ==================== */
.stage3d-xyz {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
  user-select: none;
  cursor: grab;
}
.stage3d-xyz[data-tool="orbit"] {
  cursor: grab;
}
.stage3d-xyz.is-dragging {
  cursor: grabbing;
}

.stage3d-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
}

.stage3d-cards {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.card3d-xyz {
  position: absolute;
  left: 0;
  top: 0;
  width: 214px;
  min-height: 82px;
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 10px 13px 10px 15px;
  border-radius: 14px;
  background: var(--bg-surface);
  border: 1.5px solid var(--border);
  box-shadow: 0 10px 26px rgba(15, 28, 22, 0.16);
  pointer-events: auto;
  cursor: grab;
  will-change: transform;
  transition: border-color 0.18s ease, box-shadow 0.18s ease, opacity 0.2s ease;
}

.card3d-xyz:hover {
  border-color: var(--accent-gold);
  box-shadow:
    0 16px 34px rgba(15, 28, 22, 0.24),
    0 0 0 2px rgba(197, 155, 75, 0.35);
}

.card3d-xyz.is-selected {
  border-color: var(--accent-gold);
  box-shadow:
    0 18px 38px rgba(15, 28, 22, 0.28),
    0 0 0 3px rgba(197, 155, 75, 0.55);
}

.card3d-xyz.is-dragging-node {
  cursor: grabbing;
  border-color: var(--primary);
  box-shadow:
    0 22px 44px rgba(27, 67, 50, 0.32),
    0 0 0 3px rgba(197, 155, 75, 0.7);
  z-index: 999 !important;
}

.card3d-xyz.is-dimmed {
  opacity: 0.22 !important;
}

.card3d-xyz.gender-m .card3d-accent { background: var(--accent-male); }
.card3d-xyz.gender-f .card3d-accent { background: var(--accent-female); }
.card3d-xyz:hover .card3d-add,
.card3d-xyz.is-selected .card3d-add {
  opacity: 1;
}

/* ==================== AUTH OVERLAY & USERS MANAGEMENT ==================== */
.auth-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background:
    radial-gradient(ellipse at 50% 25%, rgba(197, 155, 75, 0.16) 0%, transparent 60%),
    radial-gradient(ellipse at 50% 95%, rgba(27, 67, 50, 0.14) 0%, transparent 60%),
    var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.auth-card {
  width: 100%;
  max-width: 410px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 28px;
  box-shadow: var(--shadow-lg);
}

.auth-crest {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--primary), #2d6a4f);
  color: #f4e6c3;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  margin: 0 auto 14px;
  box-shadow: 0 6px 18px rgba(27, 67, 50, 0.25);
}

.auth-card h2 {
  margin: 0 0 6px;
  text-align: center;
  font-family: var(--font-serif);
  font-size: 28px;
}

.auth-card p {
  margin: 0 0 20px;
  text-align: center;
  font-size: 13px;
  color: var(--text-muted);
}

.auth-error {
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid rgba(184, 59, 59, 0.25);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 12.5px;
  font-weight: 600;
  margin-bottom: 14px;
}

.user-menu-info {
  padding: 8px 12px 10px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
  font-size: 12px;
  color: var(--text-muted);
}
.user-menu-info strong {
  display: block;
  color: var(--text);
  font-size: 13.5px;
}

.guest-setting-row {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 11px 14px;
  margin-bottom: 12px;
  font-size: 13px;
}

.users-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
  max-height: 240px;
  overflow-y: auto;
}

.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 11px;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
}
.user-row.is-inactive {
  opacity: 0.55;
}
.user-row-main {
  min-width: 0;
  flex: 1;
}
.user-row-name {
  font-weight: 700;
  font-size: 13.5px;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.user-row-perms {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 5px;
}
.perm-badge {
  font-size: 10.5px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 99px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--text-muted);
}
.perm-badge.is-admin {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.user-row-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.user-editor-box {
  background: var(--bg-canvas);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
}

.perms-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 6px;
}
.perm-check {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 10px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--bg-surface);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}
.perm-check:has(input:checked) {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
}

/* Hide mutation controls globally when user lacks permissions */
body.no-perm-add .node-quick-btn,
body.no-perm-add .card3d-add {
  display: none !important;
}

@media (max-width: 680px) {
  .perms-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ==================== MOBILE TOUCH PERFORMANCE & ERGONOMICS ==================== */
.tree-canvas,
.tree-svg,
.stage3d-xyz,
.card3d-xyz {
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.tree-viewport {
  transform-origin: 0 0;
  will-change: transform;
}

@media (max-width: 768px) {
  :root {
    --topbar-h: 56px;
  }

  .app-shell {
    height: 100dvh;
  }

  .topbar {
    padding: 0 8px;
    gap: 6px;
  }

  .brand {
    padding: 2px 4px;
    gap: 7px;
  }
  .brand-crest {
    width: 32px;
    height: 32px;
    border-radius: 9px;
  }
  .brand-text h1 {
    font-size: 15px;
    max-width: 115px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .topbar-right {
    gap: 4px;
  }
  #btnMoreMenu span,
  #btnAddPerson span {
    display: none;
  }
  #btnMoreMenu,
  #btnAddPerson {
    padding: 7px 9px;
  }
  .user-pill {
    max-width: 96px;
    overflow: hidden;
    text-overflow: ellipsis;
    padding: 6px 8px;
    font-size: 11.5px;
  }

  /* Compact navigation bar */
  .main-nav {
    padding: 3px;
    gap: 2px;
  }
  .nav-tab {
    padding: 6px 8px;
  }

  /* Compact 2D & 3D toolbars on phone */
  .tree-toolbar-top {
    top: 10px;
    left: 10px;
    right: 10px;
    gap: 6px;
  }
  .tree-toolbar-top .seg-btn {
    padding: 5px 8px;
    font-size: 11.5px;
  }
  .tree-toolbar-bottom {
    bottom: 12px;
    left: 10px;
    padding: 3px;
  }

  .hud3d-top-bar {
    top: 8px;
    left: 8px;
    right: 8px;
    gap: 5px;
  }
  .hud3d-top-bar .btn-sm,
  .hud3d-top-bar .seg-btn {
    padding: 5px 7px;
    font-size: 11px;
  }
  #btnResetNodes3D,
  #btnCollapseGen3D {
    display: none;
  }
  .hud3d-bottom {
    bottom: 10px;
    left: 8px;
    right: 8px;
    width: auto;
    max-width: none;
    justify-content: center;
    padding: 5px 8px;
    gap: 6px;
  }
  .hud3d-sliders {
    display: none;
  }

  /* Profile drawer becomes a comfortable bottom sheet on phones */
  .profile-drawer {
    position: fixed;
    left: 0;
    right: 0;
    top: auto;
    bottom: 0;
    width: 100%;
    height: 76dvh;
    border-left: none;
    border-top: 2px solid var(--accent-gold);
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -12px 36px rgba(10, 20, 15, 0.25);
    z-index: 50;
  }

  .sidebar {
    width: min(86vw, 310px);
    z-index: 45;
  }
  .sidebar.is-collapsed {
    margin-left: calc(-1 * min(86vw, 310px));
  }

  /* Slightly more compact 3D cards on phone screens */
  .card3d-xyz {
    width: 186px;
    min-height: 74px;
    padding: 8px 10px 8px 13px;
    box-shadow: 0 4px 12px rgba(15, 28, 22, 0.14);
  }
  .card3d-avatar {
    width: 40px;
    height: 40px;
    font-size: 13px;
  }
}
