:root {
  --bg: #faf8f4;
  --bg-card: #ffffff;
  --bg-sidebar: #1a1612;
  --text: #2c2418;
  --text-light: #7a6e5e;
  --text-inverse: #f5f0e8;
  --accent: #b5651d;
  --accent-hover: #9a5418;
  --accent-light: #f0e0cc;
  --border: #e8e0d4;
  --border-light: #f0ebe3;
  --success: #4a7c59;
  --danger: #c0392b;
  --shadow: 0 2px 12px rgba(44,36,24,0.08);
  --shadow-lg: 0 8px 32px rgba(44,36,24,0.12);
  --radius: 10px;
  --radius-sm: 6px;
  --font-display: 'Playfair Display', Georgia, serif;
  --font-body: 'Source Sans 3', system-ui, sans-serif;
  --male: #4a7c9b;
  --female: #9b5a6a;
  --diverse: #7a6e9b;
  --tree-line: #c4b8a4;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

#app { min-height: 100vh; }

/* ─── AUTH ─── */
.auth-wrapper {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  background: linear-gradient(135deg, #f5ede0 0%, #e8dcc8 100%);
}
.auth-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 3rem;
  width: 100%;
  max-width: 420px;
  text-align: center;
}
.auth-tree-icon { font-size: 3rem; margin-bottom: 0.5rem; }
.auth-card h1 {
  font-family: var(--font-display);
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 0.25rem;
}
.auth-subtitle {
  color: var(--text-light);
  font-size: 0.95rem;
  margin-bottom: 2rem;
}
.auth-link {
  margin-top: 1.5rem;
  font-size: 0.9rem;
  color: var(--text-light);
}
.auth-link a { color: var(--accent); text-decoration: none; font-weight: 500; }
.auth-link a:hover { text-decoration: underline; }

/* ─── LAYOUT ─── */
.layout {
  display: flex;
  min-height: 100vh;
}
.sidebar {
  width: 240px;
  background: var(--bg-sidebar);
  color: var(--text-inverse);
  padding: 1.5rem 0;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  position: fixed;
  height: 100vh;
  z-index: 100;
}
.sidebar-brand {
  padding: 0 1.5rem;
  margin-bottom: 2rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.sidebar-brand span { font-size: 1.6rem; }
.sidebar-brand h2 {
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text-inverse);
}
.sidebar nav { flex: 1; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1.5rem;
  color: rgba(245,240,232,0.65);
  text-decoration: none;
  font-size: 0.95rem;
  font-weight: 400;
  cursor: pointer;
  transition: all 0.15s;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
}
.nav-item:hover, .nav-item.active {
  color: var(--text-inverse);
  background: rgba(255,255,255,0.08);
}
.nav-item.active { border-left: 3px solid var(--accent); }
.nav-item svg { width: 20px; height: 20px; opacity: 0.7; }
.sidebar-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid rgba(255,255,255,0.1);
}
.sidebar-user {
  font-size: 0.85rem;
  color: rgba(245,240,232,0.5);
  margin-bottom: 0.5rem;
}
.main-content {
  flex: 1;
  margin-left: 240px;
  padding: 2rem;
  min-height: 100vh;
}

/* ─── FORMS ─── */
.form-group { margin-bottom: 1.25rem; text-align: left; }
.form-group label {
  display: block;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 0.35rem;
}
.form-row { display: flex; gap: 1rem; }
.form-row .form-group { flex: 1; }
input, select, textarea {
  width: 100%;
  padding: 0.65rem 0.9rem;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: var(--font-body);
  font-size: 0.95rem;
  color: var(--text);
  background: var(--bg-card);
  transition: border-color 0.15s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
}
.error-msg {
  background: #fef2f2;
  color: var(--danger);
  border: 1px solid #fecaca;
  padding: 0.6rem 1rem;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  margin-bottom: 1rem;
}

/* ─── BUTTONS ─── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 1.25rem;
  border-radius: var(--radius-sm);
  font-family: var(--font-body);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: all 0.15s;
  text-decoration: none;
}
.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--accent-light); color: var(--accent); }
.btn-secondary:hover { background: #e6d0b4; }
.btn-danger { background: #fef2f2; color: var(--danger); }
.btn-danger:hover { background: #fecaca; }
.btn-ghost { background: transparent; color: var(--text-light); }
.btn-ghost:hover { background: var(--border-light); }
.btn-full { width: 100%; justify-content: center; }
.btn-sm { padding: 0.4rem 0.8rem; font-size: 0.85rem; }

/* ─── CARDS ─── */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 2rem;
}
.page-header h1 {
  font-family: var(--font-display);
  font-size: 1.6rem;
  font-weight: 700;
}
.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}
.card-header h3 {
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 600;
}

/* ─── PERSON LIST ─── */
.person-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}
.person-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  cursor: pointer;
  transition: all 0.2s;
  border: 2px solid transparent;
}
.person-card:hover { box-shadow: var(--shadow-lg); transform: translateY(-1px); }
.person-card.selected { border-color: var(--accent); background: var(--accent-light); }
.person-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 1.1rem;
  color: white;
  flex-shrink: 0;
  overflow: hidden;
}
.person-avatar.male { background: var(--male); }
.person-avatar.female { background: var(--female); }
.person-avatar.diverse { background: var(--diverse); }
.person-avatar img { width: 100%; height: 100%; object-fit: cover; }
.person-info h4 { font-size: 1rem; font-weight: 600; }
.person-info p { font-size: 0.85rem; color: var(--text-light); }

/* ─── MODAL ─── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
}
.modal {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  overflow-y: auto;
  padding: 2rem;
}
.modal h2 {
  font-family: var(--font-display);
  font-size: 1.4rem;
  margin-bottom: 1.5rem;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  margin-top: 1.5rem;
}

/* ─── TREE VIEW ─── */
.tree-controls {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}
.view-toggle {
  display: flex;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.view-toggle button {
  padding: 0.5rem 1rem;
  font-family: var(--font-body);
  font-size: 0.85rem;
  border: none;
  cursor: pointer;
  background: var(--bg-card);
  color: var(--text-light);
  transition: all 0.15s;
}
.view-toggle button.active { background: var(--accent); color: white; }

#tree-container {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-height: 500px;
  position: relative;
  overflow: hidden;
}

/* Classic tree styles */
.tree-classic {
  padding: 2rem;
  overflow: auto;
  min-height: 500px;
}
.tree-classic ul {
  display: flex;
  justify-content: center;
  list-style: none;
  padding-top: 2rem;
  position: relative;
}
.tree-classic ul::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  width: 0;
  border-left: 2px solid var(--tree-line);
  height: 2rem;
}
.tree-classic li {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 0.5rem;
  position: relative;
}
.tree-classic li::before, .tree-classic li::after {
  content: '';
  position: absolute;
  top: 0;
  width: 50%;
  height: 2rem;
  border-top: 2px solid var(--tree-line);
}
.tree-classic li::before { left: 0; border-left: 2px solid var(--tree-line); }
.tree-classic li::after { right: 0; border-right: 2px solid var(--tree-line); }
.tree-classic li:first-child::before { display: none; border-left: none; }
.tree-classic li:last-child::after { display: none; border-right: none; }
.tree-classic li:only-child::before, .tree-classic li:only-child::after {
  display: none;
}
.tree-classic > li::before, .tree-classic > li::after { display: none; }
.tree-classic ul:first-child::before { display: none; }

.tree-node {
  padding: 0.6rem 1rem;
  background: var(--bg-card);
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  text-align: center;
  min-width: 120px;
  cursor: pointer;
  transition: all 0.15s;
  font-size: 0.85rem;
  position: relative;
}
.tree-node:hover { border-color: var(--accent); box-shadow: var(--shadow); }
.tree-node .node-name { font-weight: 600; }
.tree-node .node-date { font-size: 0.75rem; color: var(--text-light); }
.tree-node.male { border-left: 4px solid var(--male); }
.tree-node.female { border-left: 4px solid var(--female); }
.tree-node.diverse { border-left: 4px solid var(--diverse); }
.tree-marriage {
  font-size: 0.7rem;
  color: var(--accent);
  padding: 0.15rem 0.4rem;
  background: var(--accent-light);
  border-radius: 3px;
  display: inline-block;
  margin: 0.25rem 0;
}

/* Interactive graph */
.graph-container {
  width: 100%;
  height: 600px;
  position: relative;
}
.graph-container canvas { display: block; }
.graph-node {
  position: absolute;
  padding: 0.5rem 0.75rem;
  background: var(--bg-card);
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  cursor: grab;
  box-shadow: var(--shadow);
  user-select: none;
  white-space: nowrap;
}
.graph-node:active { cursor: grabbing; }
.graph-node.male { border-color: var(--male); }
.graph-node.female { border-color: var(--female); }

/* ─── RELATIONSHIP FINDER ─── */
.finder-section {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}
.finder-select-area {
  display: flex;
  gap: 1rem;
  align-items: center;
  flex-wrap: wrap;
}
.person-select-box {
  flex: 1;
  min-width: 200px;
  padding: 1rem;
  border: 2px dashed var(--border);
  border-radius: var(--radius-sm);
  text-align: center;
  min-height: 80px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.person-select-box.filled {
  border-style: solid;
  border-color: var(--accent);
  background: var(--accent-light);
}
.finder-arrow {
  font-size: 1.5rem;
  color: var(--text-light);
}
.relationship-result {
  margin-top: 1.5rem;
  text-align: center;
}
.relationship-label {
  font-family: var(--font-display);
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 0.5rem;
}
.relationship-path {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 1rem;
}
.path-step {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--text-light);
}
.path-name { font-weight: 600; color: var(--text); }
.path-arrow { color: var(--accent); }

/* ─── INVITE TABLE ─── */
.invite-table {
  width: 100%;
  border-collapse: collapse;
}
.invite-table th, .invite-table td {
  padding: 0.75rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--border-light);
  font-size: 0.9rem;
}
.invite-table th {
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-light);
}
.invite-link-code {
  font-family: 'Source Code Pro', monospace;
  background: var(--accent-light);
  padding: 0.2rem 0.5rem;
  border-radius: 3px;
  font-size: 0.85rem;
}
.badge {
  display: inline-block;
  padding: 0.15rem 0.6rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge-active { background: #d1fae5; color: #065f46; }
.badge-used { background: #fee2e2; color: #991b1b; }

/* ─── TOAST ─── */
.toast-container {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.toast {
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  box-shadow: var(--shadow-lg);
  animation: slideIn 0.3s ease;
  max-width: 350px;
}
.toast-success { background: #065f46; color: white; }
.toast-error { background: var(--danger); color: white; }
@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* ─── SEARCH ─── */
.search-input {
  position: relative;
}
.search-input input {
  padding-left: 2.5rem;
}
.search-input svg {
  position: absolute;
  left: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  width: 18px;
  height: 18px;
  color: var(--text-light);
}

/* ─── RESPONSIVE ─── */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: -240px;
    transition: left 0.3s;
    z-index: 200;
  }
  .sidebar.open { left: 0; }
  .main-content { margin-left: 0; }
  .mobile-menu-btn {
    display: flex !important;
    position: fixed;
    top: 1rem;
    left: 1rem;
    z-index: 150;
    background: var(--bg-sidebar);
    color: var(--text-inverse);
    border: none;
    border-radius: var(--radius-sm);
    width: 40px;
    height: 40px;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 1.2rem;
  }
  .person-grid { grid-template-columns: 1fr; }
  .finder-select-area { flex-direction: column; }
  .form-row { flex-direction: column; gap: 0; }
}
.mobile-menu-btn { display: none; }

/* File input styling */
.file-input-wrapper {
  position: relative;
}
.file-input-wrapper input[type="file"] {
  opacity: 0;
  position: absolute;
  inset: 0;
  cursor: pointer;
}
.file-input-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.65rem 0.9rem;
  border: 1.5px dashed var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  color: var(--text-light);
  cursor: pointer;
  transition: border-color 0.15s;
}
.file-input-label:hover { border-color: var(--accent); }

/* Photo preview */
.photo-preview {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid var(--border);
  margin-bottom: 0.5rem;
}

/* Loading spinner */
.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--border);
  border-top: 3px solid var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 2rem auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Copy button */
.copy-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.2rem 0.5rem;
  cursor: pointer;
  font-size: 0.75rem;
  color: var(--text-light);
}
.copy-btn:hover { background: var(--accent-light); color: var(--accent); }

/* ─── CONTEXT MENU ─── */
.context-menu {
  position: fixed;
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
  min-width: 250px;
  z-index: 1500;
  padding: 0.5rem 0;
  opacity: 0;
  transform: translateY(-8px) scale(0.96);
  transition: opacity 0.15s ease, transform 0.15s ease;
}
.context-menu.visible {
  opacity: 1;
  transform: translateY(0) scale(1);
}
.ctx-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
}
.ctx-header strong { display: block; font-size: 0.95rem; }
.ctx-header small { color: var(--text-light); font-size: 0.8rem; }
.ctx-icon { font-size: 1.5rem; }
.ctx-divider {
  height: 1px;
  background: var(--border-light);
  margin: 0.25rem 0;
}
.ctx-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  padding: 0.6rem 1rem;
  border: none;
  background: none;
  font-family: var(--font-body);
  font-size: 0.9rem;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  transition: background 0.1s;
}
.ctx-item:hover { background: var(--accent-light); }
.ctx-item.ctx-danger { color: var(--danger); }
.ctx-item.ctx-danger:hover { background: #fef2f2; }
.ctx-item-icon { font-size: 1.1rem; width: 24px; text-align: center; }

/* ─── RELATION PREVIEW IN MODAL ─── */
.ctx-relation-preview {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--accent-light);
  border-radius: var(--radius-sm);
  margin-bottom: 1.5rem;
  font-size: 0.9rem;
}
.ctx-arrow { color: var(--accent); font-weight: 700; font-size: 1.2rem; }
.ctx-new-badge {
  background: var(--accent);
  color: white;
  padding: 0.15rem 0.6rem;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 500;
}

/* ─── CONNECT PERSON LIST ─── */
.connect-person-list {
  max-height: 250px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 1rem;
}
.connect-person-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 1rem;
  cursor: pointer;
  transition: background 0.1s;
  border-bottom: 1px solid var(--border-light);
}
.connect-person-item:last-child { border-bottom: none; }
.connect-person-item:hover { background: var(--accent-light); }
.connect-person-item strong { font-size: 0.9rem; display: block; }
.connect-person-item small { color: var(--text-light); font-size: 0.8rem; }

/* ─── RELATION ITEMS IN EDIT MODAL ─── */
.relations-list {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.relation-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.75rem;
  font-size: 0.85rem;
  border-bottom: 1px solid var(--border-light);
}
.relation-item:last-child { border-bottom: none; }
.relation-item span { color: var(--text); }
