:root {
  --bg:          #faf8f4;
  --surface:     #ffffff;
  --surface2:    #f3f0ea;
  --border:      #e0dbd0;
  --border2:     #ccc7b8;
  --text:        #1a1714;
  --text2:       #6b6558;
  --text3:       #9e9789;
  --accent:      #7c5c3a;
  --accent-bg:   #f0e9df;
  --teal:        #2a7a60;
  --teal-bg:     #e8f5ef;
  --purple:      #5b4a8a;
  --purple-bg:   #eeebf7;
  --rose:        #d24d57;
  --rose-bg:     #f9ebeb;
  --gold:        #b8860b;
  --font-display: 'Playfair Display', Georgia, serif;
  --font-body:    'Inter', system-ui, sans-serif;
  --radius:      12px;
  --radius-lg:   16px;
  --transition: 0.2s ease;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-tap-highlight-color: transparent; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-weight: 400;
  line-height: 1.5;
  min-height: 100dvh;
}

#loadingScreen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
}

.loading-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

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

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

.loading-title {
  font-family: var(--font-display);
  font-size: 1.2rem;
  color: var(--text2);
  font-weight: 600;
}

#errorScreen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 32px 24px;
}

.error-inner {
  text-align: center;
  max-width: 320px;
}

.error-icon {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: #fde8e8;
  color: #c0392b;
  font-size: 26px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 16px;
}

.error-title {
  font-family: var(--font-display);
  font-size: 1.2rem;
  color: var(--text);
  margin-bottom: 8px;
}

.error-msg {
  font-size: 14px;
  color: var(--text3);
  line-height: 1.6;
}

#treeScreen {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 2px 10px rgba(0,0,0,0.03);
}

.topbar-title {
  flex: 1;
  font-family: var(--font-display);
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-icon {
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text);
  text-decoration: none;
  flex-shrink: 0;
  transition: background var(--transition), border-color var(--transition);
}

.btn-icon svg { width: 22px; height: 22px; }
.btn-icon:hover { background: var(--surface2); border-color: var(--border2); }
.btn-icon:disabled { opacity: 0.3; cursor: default; }
.btn-icon:disabled:hover { background: transparent; border-color: var(--border); }

.search-panel {
  padding: 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.search-panel input {
  width: 100%;
  padding: 12px 16px;
  font-size: 16px;
  font-family: var(--font-body);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  background: var(--surface2);
  color: var(--text);
  outline: none;
  transition: border-color var(--transition);
}

.search-panel input:focus { border-color: var(--accent); }

.search-results {
  margin-top: 8px;
  max-height: 300px;
  overflow-y: auto;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
}

.search-result {
  display: flex;
  flex-direction: column;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background var(--transition);
}

.search-result:last-child { border-bottom: none; }
.search-result:hover { background: var(--surface2); }

.search-result-name {
  font-size: 15px;
  font-weight: 500;
  color: var(--text);
  font-family: var(--font-display);
}

.search-result-meta {
  font-size: 13px;
  color: var(--text3);
  margin-top: 4px;
}

.search-no-result {
  padding: 16px;
  text-align: center;
  font-size: 14px;
  color: var(--text3);
}

.tree-content {
  flex: 1;
  padding: 24px 16px 20px;
  max-width: 560px;
  margin: 0 auto;
  width: 100%;
}

.section-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text3);
  text-align: center;
  margin-bottom: 10px;
}

.connector {
  display: flex;
  justify-content: center;
  height: 24px;
  color: var(--border2);
}

.connector svg { width: 16px; height: 24px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px 20px;
  transition: background var(--transition), border-color var(--transition), transform var(--transition);
  box-shadow: 0 1px 3px rgba(0,0,0,0.02);
}

.card.clickable { cursor: pointer; }

.card.clickable:active {
  transform: scale(0.98);
  background: var(--surface2);
}

@media (hover: hover) {
  .card.clickable:hover {
    background: var(--surface2);
    border-color: var(--border2);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.05);
  }
}

.card.current {
  background: var(--surface2);
  border-color: var(--border2);
  border-left: 4px solid var(--accent);
}

.card.father  { border-left: 4px solid var(--teal); }
.card.child   { border-left: 4px solid var(--purple); }
.card.female  { border-left-color: var(--rose); }

.card-name {
  font-family: var(--font-display);
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.4;
}

.card.current .card-name { font-size: 20px; }

.card-dates {
  font-size: 13px;
  color: var(--text2);
  margin-top: 4px;
  font-weight: 400;
}

.card-tags {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.tag {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.03em;
  padding: 4px 10px;
  border-radius: 20px;
}

.tag-gen        { background: var(--accent-bg); color: var(--accent); }
.tag-children   { background: var(--purple-bg); color: var(--purple); }
.tag-patriarch  { background: #fef9e7;          color: var(--gold);  }
.tag-female     { background: var(--rose-bg);   color: var(--rose); }

.children-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.site-footer {
  text-align: center;
  padding: 16px 20px 80px;
  font-size: 14px;
  color: var(--text2);
}

.footer-link {
  display: inline-block;
  margin-bottom: 12px;
  color: var(--teal);
  font-weight: 500;
  text-decoration: none;
  border: 1px solid var(--teal);
  padding: 8px 18px;
  border-radius: 20px;
  transition: background var(--transition);
}

.footer-link:hover {
  background: var(--teal-bg);
}

.footer-text {
  font-size: 13px;
  line-height: 1.6;
}

.footer-text a {
  color: var(--accent);
  font-weight: 500;
  text-decoration: none;
}

.footer-text a:hover {
  text-decoration: underline;
}

.stat-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 10px 20px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text3);
  text-align: center;
  letter-spacing: 0.03em;
  z-index: 50;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.02);
}

@media (min-width: 600px) {
  .tree-content { padding: 32px 24px 20px; }
  .card-name { font-size: 18px; }
  .card.current .card-name { font-size: 22px; }
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.tree-content > * {
  animation: fadeIn 0.25s ease forwards;
}


/* ── Relation Finder Modal ──────────────────────────────────── */

.rel-modal {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.rel-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(26, 23, 20, 0.5);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}

.rel-sheet {
  position: relative;
  width: 100%;
  max-height: 88dvh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 24px);
  animation: sheetUp 0.28s cubic-bezier(0.32, 0.72, 0, 1) forwards;
}

@keyframes sheetUp {
  from { transform: translateY(40px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

@media (min-width: 600px) {
  .rel-modal {
    align-items: center;
  }
  .rel-sheet {
    width: 520px;
    border-radius: 22px;
    max-height: 84dvh;
  }
}

.rel-handle {
  width: 40px;
  height: 4px;
  background: var(--border2);
  border-radius: 2px;
  margin: 12px auto 0;
}

.rel-sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px 4px;
}

.rel-sheet-title {
  font-family: var(--font-display);
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}

.rel-persons {
  display: flex;
  align-items: stretch;
  gap: 8px;
  padding: 12px 16px 16px;
}

.rel-person-btn {
  flex: 1;
  min-width: 0;
  background: var(--surface2);
  border: 1.5px dashed var(--border2);
  border-radius: var(--radius);
  padding: 12px 12px;
  text-align: left;
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition), box-shadow var(--transition);
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.rel-person-btn:hover {
  border-color: var(--accent);
  background: var(--accent-bg);
}

.rel-person-btn.has-person {
  border-style: solid;
  border-color: var(--accent);
  background: var(--accent-bg);
  box-shadow: 0 2px 8px rgba(124, 92, 58, 0.1);
}

.rel-person-role {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text3);
}

.rel-person-btn.has-person .rel-person-role {
  color: var(--accent);
}

.rel-person-name {
  font-family: var(--font-display);
  font-size: 14px;
  font-weight: 600;
  color: var(--text2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.rel-person-btn.has-person .rel-person-name {
  color: var(--text);
}

.rel-swap-btn {
  width: 40px;
  flex-shrink: 0;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text2);
  transition: background var(--transition), color var(--transition), transform var(--transition);
}

.rel-swap-btn:hover {
  background: var(--surface);
  color: var(--text);
  transform: rotate(180deg);
}

.rel-swap-btn svg { width: 18px; height: 18px; }

.rel-picker-wrap {
  border-top: 1px solid var(--border);
  padding: 0 16px 12px;
}

.rel-picker-for {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent);
  padding: 12px 0 8px;
}

.rel-picker-input {
  width: 100%;
  padding: 11px 14px;
  font-size: 15px;
  font-family: var(--font-body);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  background: var(--surface2);
  color: var(--text);
  outline: none;
  transition: border-color var(--transition);
}

.rel-picker-input:focus { border-color: var(--accent); }

.rel-picker-list {
  margin-top: 8px;
  max-height: 220px;
  overflow-y: auto;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
}

.rel-result {
  padding: 0 16px 4px;
}

.rel-msg {
  text-align: center;
  padding: 28px 16px;
  font-size: 15px;
  color: var(--text2);
}

.rel-msg-none {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.rel-msg-icon {
  font-size: 32px;
  color: var(--text3);
}

.rel-badge-wrap {
  text-align: center;
  padding: 20px 0 8px;
  border-top: 1px solid var(--border);
}

.rel-badge {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
  background: var(--accent-bg);
  border: 1.5px solid var(--accent);
  border-radius: 20px;
  padding: 8px 24px;
}

.rel-badge-num {
  font-family: var(--font-display);
  font-size: 48px;
  font-weight: 600;
  color: var(--accent);
  line-height: 1;
}

.rel-badge-unit {
  font-size: 22px;
  font-weight: 500;
  color: var(--accent);
  opacity: 0.8;
}

.rel-label {
  font-family: var(--font-display);
  font-size: 16px;
  color: var(--text2);
  margin-top: 10px;
}

.rel-chain-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text3);
  text-align: center;
  margin: 18px 0 12px;
}

.rel-chain {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-bottom: 12px;
}

.rel-pc {
  width: 100%;
  max-width: 340px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  cursor: pointer;
  overflow: hidden;
  transition: transform var(--transition), box-shadow var(--transition);
}

.rel-pc:hover {
  transform: scale(1.01);
  box-shadow: 0 4px 12px rgba(0,0,0,0.07);
}

.rel-pc-inner {
  position: relative;
  padding: 10px 40px 10px 14px;
  text-align: left;
}

.rel-pc-a {
  border-left: 4px solid var(--accent);
  background: var(--accent-bg);
}

.rel-pc-b {
  border-left: 4px solid var(--teal);
  background: var(--teal-bg);
}

.rel-pc-ca {
  border-left: 4px solid var(--gold);
  background: #fef9e7;
}

.rel-pc-name {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.3;
}

.rel-pc-year {
  font-size: 12px;
  color: var(--text3);
  margin-top: 3px;
}

.rel-pc-ca-tag {
  display: inline-block;
  margin-top: 5px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--gold);
  background: #fde68a44;
  padding: 2px 8px;
  border-radius: 10px;
}

.rel-pc-badge {
  position: absolute;
  top: 10px;
  right: 12px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

.rel-pc-badge-a {
  background: var(--accent);
  color: #fff;
}

.rel-pc-badge-b {
  background: var(--teal);
  color: #fff;
}

.rel-arrow {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 4px 0;
  color: var(--text3);
}

.rel-arrow-sym {
  font-size: 18px;
  line-height: 1;
}

.rel-arrow-lbl {
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: lowercase;
}