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

:root {
  --bg: #f0ebe3;
  --surface: #ffffff;
  --surface2: #faf8f5;
  --border: #e2d9ce;
  --text: #2c1d0e;
  --text2: #6b5744;
  --text3: #9e8070;
  --accent: #7b4f2e;
  --accent-hover: #5e3a1f;
  --accent-light: #f4ede4;
  --green: #3a7d44;
  --green-hover: #2d6235;
  --red: #c0392b;
  --node-bg: #ffffff;
  --node-border: #d4c4b5;
  --node-shadow: rgba(0,0,0,0.08);
  --couple-line: #c8a882;
  --family-line: #b0917a;
  --radius: 12px;
  --shadow: 0 2px 12px rgba(0,0,0,0.1);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.15);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

/* ===== SCREENS ===== */
.screen { display: none; width: 100%; height: 100vh; }
.screen.active { display: flex; }

/* ===== SETUP SCREEN ===== */
#setup-screen {
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f9f3ec 0%, #ede3d6 100%);
  overflow-y: auto;
  padding: 24px 0;
}

.setup-card {
  background: var(--surface);
  border-radius: 20px;
  padding: 48px;
  width: 100%;
  max-width: 560px;
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
}

.setup-logo { font-size: 56px; text-align: center; margin-bottom: 12px; }

.setup-card h1 {
  text-align: center;
  font-size: 28px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 8px;
}

.setup-subtitle {
  text-align: center;
  color: var(--text2);
  font-size: 15px;
  margin-bottom: 32px;
  line-height: 1.5;
}

.setup-divider {
  text-align: center;
  color: var(--text3);
  font-size: 13px;
  margin: 20px 0 14px;
  position: relative;
}
.setup-divider::before,
.setup-divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 30%;
  height: 1px;
  background: var(--border);
}
.setup-divider::before { left: 0; }
.setup-divider::after  { right: 0; }

.setup-file-row {
  display: flex;
  gap: 10px;
}
.setup-file-row .btn { flex: 1; }

.setup-legal {
  text-align: center;
  margin-top: 20px;
  font-size: 12px;
  color: var(--text3);
}
.setup-legal a { color: var(--text3); text-decoration: underline; }

/* ===== FORM ELEMENTS ===== */
.form-group { margin-bottom: 20px; }

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text2);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.required { color: var(--red); }
.optional { font-weight: 400; text-transform: none; letter-spacing: 0; color: var(--text3); }

input[type="text"],
input[type="url"],
textarea {
  width: 100%;
  padding: 10px 14px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  transition: border-color 0.2s;
  outline: none;
  font-family: inherit;
}

input[type="text"]:focus,
input[type="url"]:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(123,79,46,0.1);
}

textarea { resize: vertical; }

small {
  display: block;
  margin-top: 6px;
  font-size: 12px;
  color: var(--text3);
  line-height: 1.5;
}

small a { color: var(--accent); text-decoration: none; }
small a:hover { text-decoration: underline; }

/* ===== EDIT OPTIONS ===== */
.edit-options {
  display: flex;
  align-items: flex-start;
  gap: 0;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.edit-option {
  flex: 1;
  padding: 14px;
  background: var(--surface2);
}

.edit-option input {
  border-color: transparent;
  background: var(--surface);
}

.edit-option-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--text3);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.edit-option-sep {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  flex-shrink: 0;
  font-size: 12px;
  color: var(--text3);
  background: var(--border);
  font-style: italic;
}

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  font-family: inherit;
  white-space: nowrap;
}

.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--accent);
  color: white;
  width: 100%;
  padding: 13px 20px;
  font-size: 15px;
  margin-top: 8px;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1.5px solid var(--border);
}
.btn-secondary:hover { background: var(--border); }

.btn-danger { background: #e53e3e; color: #fff; }
.btn-danger:hover { background: #c53030; }
.btn-icon { padding: 6px 10px; font-size: 15px; flex-shrink: 0; }

/* Edit button on node */
.edit-btn-group { cursor: pointer; }
.edit-btn-circle { fill: #c8b8a2; transition: fill 0.15s; }
.edit-btn-group:hover .edit-btn-circle { fill: #e53e3e; }
.edit-btn-label {
  font-size: 9px; font-weight: 700; fill: var(--text2);
  dominant-baseline: middle; text-anchor: middle; pointer-events: none;
}
.edit-btn-group:hover .edit-btn-label { fill: white; }

.btn-sm {
  padding: 6px 14px;
  font-size: 13px;
  background: rgba(255,255,255,0.9);
  color: var(--text);
  border: 1.5px solid var(--border);
  border-radius: 6px;
}
.btn-sm:hover { background: white; }
.btn-import-mode {
  background: #fff8e1 !important;
  color: #b45309 !important;
  border-color: #f59e0b !important;
  cursor: default;
}
.btn-import-mode:hover { background: #fff3cd !important; }

/* ===== ERROR MSG ===== */
.error-msg {
  margin-top: 12px;
  padding: 10px 14px;
  background: #fdf0ef;
  border: 1px solid #f5c6c3;
  border-radius: 8px;
  color: var(--red);
  font-size: 13px;
}
.hidden { display: none !important; }

/* ===== NAVBAR ===== */
.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  min-height: 52px;
  background: rgba(255,255,255,0.9);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 12px;
  z-index: 100;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}

.nav-brand {
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
  white-space: nowrap;
}

.nav-actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* ===== SEARCH ===== */
.search-container {
  position: relative;
  flex: 1;
  min-width: 120px;
  max-width: 260px;
  margin: 0 6px;
}

.search-input {
  width: 100%;
  padding: 5px 12px;
  border: 1.5px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  color: var(--text);
  background: var(--surface);
  outline: none;
  font-family: inherit;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(123,79,46,0.1);
}

.parent-display {
  padding: 8px 12px;
  background: var(--surface2);
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  color: var(--text);
  min-height: 38px;
  display: flex;
  align-items: center;
}

.search-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  min-width: 220px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-lg);
  z-index: 300;
  max-height: 280px;
  overflow-y: auto;
  padding: 4px;
}

.search-item {
  display: block;
  width: 100%;
  padding: 8px 12px;
  border: none;
  background: none;
  text-align: left;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  border-radius: 6px;
  font-family: inherit;
  transition: background 0.15s;
}

.search-item:hover,
.search-item.active {
  background: var(--accent-light);
  color: var(--accent);
}

.search-item mark {
  background: none;
  color: var(--accent);
  font-weight: 700;
}

/* ===== TREE SCREEN ===== */
#tree-screen { flex-direction: column; }

/* ===== MODE BAR ===== */
.mode-bar {
  position: fixed;
  top: 52px;
  left: 0; right: 0;
  height: 38px;
  background: linear-gradient(90deg, #4a2c10, #7b4f2e);
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  z-index: 99;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}
.mode-bar .btn-sm {
  background: rgba(255,255,255,0.2);
  border-color: rgba(255,255,255,0.35);
  color: white;
  font-size: 12px;
}
.mode-bar .btn-sm:hover { background: rgba(255,255,255,0.35); }

#tree-container {
  flex: 1;
  margin-top: 52px;
  overflow: hidden;
  position: relative;
  cursor: grab;
}
#tree-screen.mode-active #tree-container { margin-top: 90px; }
#tree-container:active { cursor: grabbing; }

#tree-svg {
  width: 100%;
  height: 100%;
  display: block;
  user-select: none;
}

/* ===== TREE NODES (SVG) ===== */
.node-rect {
  fill: var(--node-bg);
  stroke: var(--node-border);
  stroke-width: 1.5;
  rx: 10;
  filter: drop-shadow(0 2px 6px var(--node-shadow));
  transition: stroke 0.2s;
}

.node-rect:hover { stroke: var(--accent); stroke-width: 2; }

.node-rect.highlighted {
  stroke: var(--accent);
  stroke-width: 3;
  filter: drop-shadow(0 0 10px rgba(123,79,46,0.5));
}

.node-group { cursor: pointer; }

.avatar-bg {
  fill: var(--accent-light);
}

.avatar-initial {
  font-size: 18px;
  font-weight: 700;
  fill: var(--accent);
  dominant-baseline: central;
  text-anchor: middle;
  pointer-events: none;
}

.person-name {
  font-size: 13px;
  font-weight: 600;
  fill: var(--text);
  dominant-baseline: central;
  pointer-events: none;
}

.person-last-name {
  font-size: 11px;
  fill: var(--text2);
  dominant-baseline: central;
  pointer-events: none;
}

.person-meta {
  font-size: 10px;
  fill: var(--text3);
  dominant-baseline: central;
  pointer-events: none;
}

/* Add button on node */
.add-btn-group { cursor: pointer; }

.add-btn-circle {
  fill: var(--accent);
  transition: fill 0.15s;
}
.add-btn-group:hover .add-btn-circle { fill: var(--accent-hover); }

.add-btn-label {
  font-size: 11px;
  font-weight: 700;
  fill: white;
  dominant-baseline: central;
  text-anchor: middle;
  pointer-events: none;
}

.add-btn-large {
  font-size: 22px;
}

/* Ancestor / descendant nav buttons on node */
.nav-btn-group { cursor: pointer; }

.nav-btn-circle {
  fill: #d4c4b5;
  transition: fill 0.15s;
}
.nav-btn-group:hover .nav-btn-circle { fill: var(--accent); }

.nav-btn-label {
  font-size: 9px;
  font-weight: 700;
  fill: var(--text2);
  dominant-baseline: central;
  text-anchor: middle;
  pointer-events: none;
}
.nav-btn-group:hover .nav-btn-label { fill: white; }

/* Connection lines */
.line-spouse {
  stroke: var(--couple-line);
  stroke-width: 2.5;
  fill: none;
  stroke-dasharray: 6, 3;
}

.line-parent {
  stroke: var(--family-line);
  stroke-width: 1.8;
  fill: none;
}

/* ===== ZOOM CONTROLS ===== */
#zoom-controls {
  position: absolute;
  bottom: 24px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.zoom-btn {
  width: 36px;
  height: 36px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  background: rgba(255,255,255,0.95);
  font-size: 18px;
  cursor: pointer;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow);
  transition: background 0.2s;
}
.zoom-btn:hover { background: white; }

/* ===== LOADING OVERLAY ===== */
#loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(240,235,227,0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  font-size: 15px;
  color: var(--text2);
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ===== MODALS ===== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  padding: 20px;
}

.modal {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
  animation: modal-in 0.2s ease;
}

.modal-wide { max-width: 640px; }

@keyframes modal-in {
  from { opacity: 0; transform: translateY(12px) scale(0.97); }
  to   { opacity: 1; transform: translateY(0)    scale(1); }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 0;
  margin-bottom: 20px;
}

.modal-header h2 { font-size: 18px; font-weight: 700; }

.modal-close {
  width: 28px;
  height: 28px;
  border: none;
  background: var(--surface2);
  border-radius: 50%;
  cursor: pointer;
  font-size: 12px;
  color: var(--text2);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}
.modal-close:hover { background: var(--border); }

.modal form { padding: 0 24px 24px; }

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.modal-actions .btn { flex: 1; width: auto; margin-top: 0; padding: 10px 16px; font-size: 14px; }

/* ===== CONTEXT MENU ===== */
.context-menu {
  position: fixed;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-lg);
  padding: 6px;
  z-index: 400;
  min-width: 180px;
  animation: fade-in 0.12s ease;
}

@keyframes fade-in {
  from { opacity: 0; transform: scale(0.95); }
  to   { opacity: 1; transform: scale(1); }
}

.ctx-item {
  display: block;
  width: 100%;
  padding: 9px 14px;
  border: none;
  background: none;
  text-align: left;
  cursor: pointer;
  font-size: 14px;
  color: var(--text);
  border-radius: 6px;
  font-family: inherit;
  transition: background 0.15s;
}
.ctx-item:hover { background: var(--accent-light); color: var(--accent); }

/* ===== DETAIL PANEL ===== */
.detail-panel {
  position: fixed;
  top: 64px;
  left: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  width: 220px;
  box-shadow: var(--shadow-lg);
  z-index: 200;
  animation: slide-in 0.2s ease;
}

@keyframes slide-in {
  from { opacity: 0; transform: translateX(-10px); }
  to   { opacity: 1; transform: translateX(0); }
}

.detail-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 24px;
  height: 24px;
  border: none;
  background: var(--surface2);
  border-radius: 50%;
  cursor: pointer;
  font-size: 11px;
  color: var(--text2);
  display: flex;
  align-items: center;
  justify-content: center;
}
.detail-close:hover { background: var(--border); }

.detail-avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--accent-light);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 700;
  color: var(--accent);
  margin: 0 auto 12px;
  overflow: hidden;
}

.detail-avatar img { width: 100%; height: 100%; object-fit: cover; }

.detail-name {
  font-size: 16px;
  font-weight: 700;
  text-align: center;
  color: var(--text);
  margin-bottom: 4px;
}

.detail-meta {
  font-size: 12px;
  color: var(--text3);
  text-align: center;
  margin-bottom: 10px;
}

.detail-comments {
  font-size: 13px;
  color: var(--text2);
  line-height: 1.5;
  border-top: 1px solid var(--border);
  padding-top: 10px;
  margin-top: 4px;
  word-wrap: break-word;
}

/* ===== HELP MODAL ===== */
.help-content { padding: 0 24px; }
.help-content ol { padding-left: 20px; line-height: 2; font-size: 14px; color: var(--text2); }
.help-content li { margin-bottom: 4px; }
.help-content strong { color: var(--text); }

.code-block {
  background: #1e1e2e;
  color: #cdd6f4;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 12px;
  line-height: 1.6;
  padding: 16px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 12px 0;
  white-space: pre;
}

.help-content .btn-secondary { width: auto; margin-top: 0; padding: 8px 16px; font-size: 13px; }
.help-content .modal-actions { padding: 0 24px 24px; }

/* ===== EMPTY STATE ===== */
.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text3);
}

.empty-state .emoji { font-size: 48px; }
.empty-state h3 { font-size: 18px; color: var(--text2); }
.empty-state p { font-size: 14px; max-width: 320px; text-align: center; line-height: 1.5; }

/* ===== TOAST ===== */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: #1e1e2e;
  color: white;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 1000;
  transition: transform 0.3s ease;
  pointer-events: none;
}
#toast.show { transform: translateX(-50%) translateY(0); }
#toast.success { background: var(--green); }
#toast.error   { background: var(--red); }

/* Safe area insets — handles notches, status bars, and gesture nav bars */
.navbar {
  padding-top:    max(8px,  env(safe-area-inset-top));
  padding-left:   max(12px, env(safe-area-inset-left));
  padding-right:  max(12px, env(safe-area-inset-right));
}
#zoom-controls {
  bottom: max(16px, calc(env(safe-area-inset-bottom) + 16px));
  right:  max(16px, env(safe-area-inset-right));
}
.detail-panel {
  bottom: max(0px, env(safe-area-inset-bottom));
}
.mode-bar {
  padding-bottom: env(safe-area-inset-bottom);
}

/* ===== MOBILE ===== */
@media (max-width: 600px) {
  .nav-brand { font-size: 14px; }
  .btn-sm    { padding: 5px 10px; font-size: 12px; }

  .search-container { min-width: 100px; margin: 0 4px; }

  /* Setup card */
  .setup-card { padding: 28px 20px; }
  .edit-options { flex-direction: column; }
  .edit-option-sep { text-align: center; }

  /* Zoom controls — keep above Android gesture bar */
  #zoom-controls {
    bottom: max(80px, calc(env(safe-area-inset-bottom) + 80px));
    right: 16px;
  }

  /* Detail panel — full width, slides up from bottom, no left offset */
  .detail-panel {
    left: 0;
    right: 0;
    width: 100%;
    bottom: max(0px, env(safe-area-inset-bottom));
    top: auto;
    border-radius: 16px 16px 0 0;
    padding-bottom: max(20px, calc(env(safe-area-inset-bottom) + 20px));
  }

  /* Modals full-width */
  .modal { width: 95vw; margin: 0 auto; }
}
