/* ===== CSS Custom Properties ===== */
:root {
  --color-bg: #ffffff;
  --color-bg-secondary: #f8fafc;
  --color-surface: #ffffff;
  --color-border: #e2e8f0;
  --color-text: #0f172a;
  --color-text-secondary: #64748b;
  --color-primary: #2a6e4d;
  --color-primary-hover: #1d5538;
  --color-primary-light: #e8f5e9;
  --color-accent: #f59e0b;
  --color-destructive: #ef4444;
  --color-destructive-hover: #dc2626;
  --color-success: #22c55e;
  --color-info: #3b82f6;
  --color-muted: #f1f5f9;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.1);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1);
  --radius: 0.5rem;
  --font-sans: system-ui, -apple-system, sans-serif;
  --font-serif: "Noto Serif SC", "SimSun", serif;
}

[data-theme="dark"] {
  --color-bg: #0f172a;
  --color-bg-secondary: #1e293b;
  --color-surface: #1e293b;
  --color-border: #334155;
  --color-text: #f1f5f9;
  --color-text-secondary: #94a3b8;
  --color-primary: #34d399;
  --color-primary-hover: #2aaf7a;
  --color-primary-light: #064e3b;
  --color-muted: #1e293b;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.4);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.4);
}

/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font-sans);
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.6;
  min-height: 100vh;
}
body.modal-open { overflow: hidden; }
a { color: var(--color-primary); text-decoration: none; }
a:hover { opacity: 0.8; }

/* ===== Utilities ===== */
.container { max-width: 1280px; margin: 0 auto; padding: 0 1rem; }
.flex { display: flex; }
.flex-col { flex-direction: column; }
.flex-wrap { flex-wrap: wrap; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.justify-center { justify-content: center; }
.gap-1 { gap: 0.25rem; }
.gap-2 { gap: 0.5rem; }
.gap-3 { gap: 0.75rem; }
.gap-4 { gap: 1rem; }
.gap-6 { gap: 1.5rem; }
.text-center { text-align: center; }
.text-right { text-align: right; }
.text-sm { font-size: 0.875rem; }
.text-xs { font-size: 0.75rem; }
.text-lg { font-size: 1.125rem; }
.text-xl { font-size: 1.25rem; }
.text-2xl { font-size: 1.5rem; }
.text-3xl { font-size: 1.875rem; }
.font-bold { font-weight: 700; }
.font-medium { font-weight: 500; }
.font-serif { font-family: var(--font-serif); }
.text-muted { color: var(--color-text-secondary); }
.text-primary { color: var(--color-primary); }
.text-destructive { color: var(--color-destructive); }
.text-success { color: var(--color-success); }
.mt-1 { margin-top: 0.25rem; }
.mt-2 { margin-top: 0.5rem; }
.mt-4 { margin-top: 1rem; }
.mt-6 { margin-top: 1.5rem; }
.mb-2 { margin-bottom: 0.5rem; }
.mb-4 { margin-bottom: 1rem; }
.mb-6 { margin-bottom: 1.5rem; }
.mb-8 { margin-bottom: 2rem; }
.ml-2 { margin-left: 0.5rem; }
.mr-2 { margin-right: 0.5rem; }
.p-2 { padding: 0.5rem; }
.p-3 { padding: 0.75rem; }
.p-4 { padding: 1rem; }
.p-6 { padding: 1.5rem; }
.p-8 { padding: 2rem; }
.px-4 { padding-left: 1rem; padding-right: 1rem; }
.py-2 { padding-top: 0.5rem; padding-bottom: 0.5rem; }
.py-4 { padding-top: 1rem; padding-bottom: 1rem; }
.py-6 { padding-top: 1.5rem; padding-bottom: 1.5rem; }
.py-8 { padding-top: 2rem; padding-bottom: 2rem; }
.w-full { width: 100%; }
.h-full { height: 100%; }
.min-h-screen { min-height: 100vh; }
.relative { position: relative; }
.absolute { position: absolute; }
.fixed { position: fixed; }
.overflow-auto { overflow: auto; }
.overflow-hidden { overflow: hidden; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hidden { display: none; }
.whitespace-nowrap { white-space: nowrap; }
.space-y-2 > * + * { margin-top: 0.5rem; }
.space-y-4 > * + * { margin-top: 1rem; }
.space-x-2 > * + * { margin-left: 0.5rem; }

/* ===== Navbar ===== */
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 4rem;
  padding: 0 1.5rem;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
  position: sticky;
  top: 0;
  z-index: 100;
}
.navbar-brand a {
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--color-text);
  font-family: var(--font-serif);
}
.nav-links { display: flex; align-items: center; gap: 1.5rem; }
.nav-links a {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--color-text-secondary);
  transition: color 0.15s;
}
.nav-links a:hover, .nav-links a.active { color: var(--color-primary); }
.nav-actions { display: flex; align-items: center; gap: 0.75rem; }

/* ===== Main Content ===== */
.main-content { flex: 1; min-height: calc(100vh - 4rem); }

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
  font-weight: 500;
  border-radius: var(--radius);
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.15s;
  line-height: 1.5;
  font-family: inherit;
}
.btn:hover { opacity: 0.9; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--color-primary); color: #fff; }
.btn-primary:hover { background: var(--color-primary-hover); }
.btn-outline { background: transparent; border-color: var(--color-border); color: var(--color-text); }
.btn-outline:hover { background: var(--color-muted); }
.btn-ghost { background: transparent; color: var(--color-text); }
.btn-ghost:hover { background: var(--color-muted); }
.btn-destructive { background: var(--color-destructive); color: #fff; }
.btn-destructive:hover { background: var(--color-destructive-hover); }
.btn-sm { padding: 0.25rem 0.75rem; font-size: 0.75rem; }
.btn-icon { padding: 0.5rem; width: 2.25rem; height: 2.25rem; }
.btn-group { display: flex; gap: 0.5rem; }

/* ===== Forms ===== */
.form-group { margin-bottom: 1rem; }
.form-group label {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  margin-bottom: 0.25rem;
  color: var(--color-text);
}
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.form-row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 1rem; }
.input, .select, textarea.input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-bg);
  color: var(--color-text);
  font-family: inherit;
  transition: border-color 0.15s;
}
.input:focus, .select:focus, textarea.input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
}
textarea.input { resize: vertical; min-height: 80px; }
.checkbox-label { display: flex; align-items: center; gap: 0.5rem; cursor: pointer; font-size: 0.875rem; }
.checkbox-label input[type="checkbox"] { width: 1rem; height: 1rem; accent-color: var(--color-primary); }

/* ===== Cards ===== */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.card-header {
  padding: 1.25rem 1.5rem 0.5rem;
  border-bottom: 1px solid var(--color-border);
}
.card-body { padding: 1.5rem; }
.card-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--color-border);
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

/* ===== Tables ===== */
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}
.data-table thead { background: var(--color-muted); }
.data-table th {
  padding: 0.75rem 0.5rem;
  text-align: left;
  font-weight: 600;
  color: var(--color-text-secondary);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  white-space: nowrap;
  border-bottom: 2px solid var(--color-border);
}
.data-table td {
  padding: 0.625rem 0.5rem;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}
.data-table tbody tr:hover { background: var(--color-muted); }
.data-table .cell-actions { white-space: nowrap; display: flex; gap: 0.25rem; }

/* ===== Modals ===== */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0,0,0,0.5);
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.modal-content {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  max-width: 720px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  border: 1px solid var(--color-border);
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--color-border);
}
.modal-body { padding: 1.5rem; }
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--color-border);
}

/* ===== Popover ===== */
.popover-wrapper { position: relative; display: inline; }
.popover-trigger {
  cursor: pointer;
  border-bottom: 1px dotted var(--color-text-secondary);
}
.popover-trigger:hover { color: var(--color-primary); }
.popover-content {
  display: none;
  position: fixed;
  z-index: 300;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 0.75rem;
  min-width: 260px;
  max-width: 360px;
}
.popover-content.show { display: block; }
.popover-content .popover-item {
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--color-border);
  font-size: 0.8125rem;
}
.popover-content .popover-item:last-child { border-bottom: 0; }

/* ===== Pagination ===== */
.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 0;
  font-size: 0.875rem;
}
.pagination-info { color: var(--color-text-secondary); }
.pagination-btns { display: flex; align-items: center; gap: 0.25rem; }

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 500;
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius);
  font-size: 0.875rem;
  box-shadow: var(--shadow-md);
  animation: toast-in 0.3s ease;
  max-width: 360px;
}
.toast-info { background: var(--color-info); color: #fff; }
.toast-success { background: var(--color-success); color: #fff; }
.toast-error { background: var(--color-destructive); color: #fff; }
.toast-out { opacity: 0; transform: translateY(1rem); transition: all 0.3s ease; }
@keyframes toast-in { from { opacity: 0; transform: translateY(1rem); } to { opacity: 1; transform: translateY(0); } }

/* ===== Badge ===== */
.badge {
  display: inline-block;
  padding: 0.125rem 0.5rem;
  font-size: 0.6875rem;
  font-weight: 600;
  border-radius: 9999px;
  border: 1px solid var(--color-border);
}
.badge-success { background: #dcfce7; color: #166534; border-color: #86efac; }
.badge-destructive { background: #fef2f2; color: #991b1b; border-color: #fca5a5; }
.badge-muted { background: var(--color-muted); color: var(--color-text-secondary); }
.badge-info { background: #dbeafe; color: #1e40af; border-color: #93c5fd; }
[data-theme="dark"] .badge-success { background: #064e3b; color: #6ee7b7; }
[data-theme="dark"] .badge-destructive { background: #450a0a; color: #fca5a5; }
[data-theme="dark"] .badge-info { background: #172554; color: #93c5fd; }

/* ===== Search bar ===== */
.search-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}
.search-bar .input { max-width: 320px; }

/* ===== Toolbar ===== */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

/* ===== Spinner ===== */
.spinner {
  display: inline-block;
  width: 1.25rem;
  height: 1.25rem;
  border: 2px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Loading ===== */
.loading-overlay {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4rem;
  color: var(--color-text-secondary);
}

/* ===== Auth pages ===== */
.auth-page {
  display: flex;
  min-height: 100vh;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
.auth-card {
  width: 100%;
  max-width: 400px;
}
.auth-card .card-header { text-align: center; }

/* ===== Graph page ===== */
.graph-container {
  width: 100%;
  height: calc(100vh - 4rem - 4rem);
  min-height: 500px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--color-bg-secondary);
}
.graph-container svg { width: 100%; height: 100%; }

/* Node detail modal */
.node-detail-name {
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 0.75rem;
  padding-bottom: 0.75rem;
  border-bottom: 2px solid var(--color-primary);
  color: var(--color-primary);
}
.node-detail-table { width: 100%; border-collapse: collapse; }
.node-detail-table td { padding: 0.35rem 0; vertical-align: top; font-size: 0.875rem; }
.node-detail-table .label {
  width: 60px;
  color: var(--color-text-secondary);
  font-weight: 500;
  white-space: nowrap;
}
.node-detail-section {
  margin-top: 0.75rem;
  padding-top: 0.5rem;
  font-weight: 600;
  font-size: 0.875rem;
  color: var(--color-primary);
  border-top: 1px solid var(--color-border);
}
.node-detail-spouse {
  padding: 0.5rem 0.75rem;
  margin-bottom: 0.5rem;
  background: var(--color-bg-secondary);
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
}
.node-detail-spouse .spouse-name {
  font-weight: 600;
  font-size: 0.875rem;
  margin-bottom: 0.25rem;
  color: var(--color-text);
}
.node-detail-spouse .spouse-info {
  font-size: 0.8125rem;
  color: var(--color-text-secondary);
  line-height: 1.6;
}

/* ===== Statistics ===== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 1.25rem;
  text-align: center;
}
.stat-card .stat-value { font-size: 2rem; font-weight: 700; color: var(--color-primary); }
.stat-card .stat-label { font-size: 0.875rem; color: var(--color-text-secondary); margin-top: 0.25rem; }
.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 1.5rem;
}
.chart-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 1rem;
}
.chart-card h3 {
  font-size: 0.875rem;
  font-weight: 600;
  margin-bottom: 0.75rem;
  color: var(--color-text-secondary);
}
.chart-wrap { position: relative; width: 100%; height: 300px; }

/* ===== Timeline ===== */
.timeline {
  position: relative;
  padding: 2rem 0;
}
.timeline::before {
  content: "";
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 2px;
  height: 100%;
  background: var(--color-border);
}
.timeline-item {
  display: flex;
  margin-bottom: 2rem;
  position: relative;
}
.timeline-item:nth-child(odd) { flex-direction: row; }
.timeline-item:nth-child(even) { flex-direction: row-reverse; }
.timeline-marker {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--color-primary);
  border: 3px solid var(--color-bg);
  z-index: 10;
}
.timeline-content {
  width: calc(50% - 2rem);
  padding: 1rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
}
.timeline-content h4 { font-weight: 600; }
.timeline-content .timeline-meta { font-size: 0.75rem; color: var(--color-text-secondary); }

/* ===== Biography Book ===== */
.book-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 60vh;
  gap: 1rem;
  padding: 2rem;
}
.book-page {
  max-width: 600px;
  width: 100%;
  min-height: 500px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 2.5rem;
  font-family: var(--font-serif);
}
.book-page.cover {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  background: linear-gradient(135deg, #78350f, #92400e, #78350f);
  color: #fef3c7;
}
.book-page.cover h1 { font-size: 2.5rem; letter-spacing: 0.1em; margin-bottom: 1rem; }
.book-page.cover .divider { width: 6rem; height: 1px; background: rgba(253,230,138,0.6); margin: 1.5rem 0; }
.book-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; font-size: 0.875rem; margin-bottom: 1.5rem; }
.book-grid .book-label { color: var(--color-text-secondary); font-size: 0.6875rem; text-transform: uppercase; letter-spacing: 0.05em; }
.book-section { margin-top: 1.5rem; }
.book-section h3 { font-size: 0.75rem; color: var(--color-text-secondary); text-transform: uppercase; margin-bottom: 0.5rem; }
.book-footer { margin-top: 2rem; text-align: center; color: var(--color-text-secondary); font-size: 0.75rem; }

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .nav-links { display: none; }
  .nav-links.open {
    display: flex;
    flex-direction: column;
    position: absolute;
    top: 4rem;
    left: 0;
    right: 0;
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    padding: 1rem;
    z-index: 99;
  }
  .form-row, .form-row-3 { grid-template-columns: 1fr; }
  .charts-grid { grid-template-columns: 1fr; }
  .timeline::before { left: 1rem; }
  .timeline-marker { left: 1rem; }
  .timeline-item, .timeline-item:nth-child(even) { flex-direction: row; }
  .timeline-content { width: calc(100% - 3rem); margin-left: 2rem; }
  .book-container { padding: 1rem; }
  .book-page { padding: 1.5rem; min-height: 400px; }
  .md-hidden { display: flex; }
}
@media (min-width: 769px) {
  .md-hidden { display: none; }
  .md-flex { display: flex; }
}

/* ===== Dropdown ===== */
.dropdown { position: relative; display: inline-block; }
.dropdown-menu {
  display: none;
  position: absolute;
  right: 0;
  top: 100%;
  min-width: 180px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  z-index: 150;
  padding: 0.25rem 0;
}
.dropdown-menu.show { display: block; }
.dropdown-item {
  display: block;
  width: 100%;
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
  text-align: left;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-text);
  font-family: inherit;
}
.dropdown-item:hover { background: var(--color-muted); }
.dropdown-divider { height: 1px; background: var(--color-border); margin: 0.25rem 0; }

/* ===== Empty State ===== */
.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--color-text-secondary);
}
.empty-state p { margin-top: 0.5rem; }

/* ===== Top loader ===== */
.top-loader {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  background: var(--color-primary);
  z-index: 999;
  transition: width 0.3s ease;
  width: 0;
}

/* ===== Alert ===== */
.alert {
  padding: 1rem;
  border-radius: var(--radius);
  margin-bottom: 1rem;
  font-size: 0.875rem;
}
.alert-error { background: #fef2f2; color: #991b1b; border: 1px solid #fca5a5; }
.alert-info { background: #dbeafe; color: #1e40af; border: 1px solid #93c5fd; }
.alert-success { background: #dcfce7; color: #166534; border: 1px solid #86efac; }
[data-theme="dark"] .alert-error { background: #450a0a; color: #fca5a5; }
[data-theme="dark"] .alert-info { background: #172554; color: #93c5fd; }
[data-theme="dark"] .alert-success { background: #064e3b; color: #6ee7b7; }

/* ===== Admin only ===== */
.admin-only { display: none; }
.is-admin .admin-only { display: revert; }

/* ===== Icon ===== */
.icon-sun, .icon-moon { font-size: 1.125rem; }
[data-theme="light"] .icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: none; }
