:root {
  color-scheme: light;
  --bg: #f5f2ec;
  --surface: #ffffff;
  --surface-2: #f8faf9;
  --ink: #243040;
  --muted: #667085;
  --line: #d8ded9;
  --accent: #2f6f73;
  --accent-2: #9c5f3c;
  --soft: #e8f2ef;
  --warning: #f3dec8;
  --shadow: 0 16px 40px rgba(30, 42, 50, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

.app-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  padding: 28px 36px 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.78);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent-2);
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(30px, 5vw, 48px);
  line-height: 1.02;
  letter-spacing: 0;
}

.stats {
  color: var(--muted);
  font-size: 14px;
  text-align: right;
}

.hidden {
  display: none !important;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 14px;
}

.diagnostics-button,
.icon-button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}

.diagnostics-button {
  min-height: 38px;
  padding: 0 12px;
  font-weight: 700;
}

.diagnostics-button:hover,
.icon-button:hover {
  background: var(--soft);
}

.icon-button {
  width: 36px;
  height: 36px;
  font-size: 24px;
  line-height: 1;
}

.layout {
  display: grid;
  grid-template-columns: minmax(260px, 340px) minmax(0, 1fr);
  min-height: calc(100vh - 104px);
}

.sidebar {
  border-right: 1px solid var(--line);
  padding: 22px;
  background: #fbfaf7;
}

.search-label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

#search {
  width: 100%;
  height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  color: var(--ink);
  background: #fff;
  outline: none;
}

#search:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(47, 111, 115, 0.14);
}

.view-switch {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px;
  background: var(--surface);
}

.view-button {
  min-height: 34px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-weight: 700;
}

.view-button.active {
  background: var(--accent);
  color: #fff;
}

.view-button:not(.active):hover {
  background: var(--soft);
  color: var(--ink);
}

.new-person-button {
  width: 100%;
  min-height: 40px;
  margin-top: 12px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 900;
}

.new-person-button:hover {
  filter: brightness(0.94);
}

.depth-control {
  margin-top: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--surface);
}

.depth-control input {
  width: 100%;
  accent-color: var(--accent);
}

.depth-readout {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  text-align: right;
}

.results {
  display: grid;
  gap: 6px;
  max-height: calc(100vh - 190px);
  margin-top: 16px;
  overflow: auto;
  padding-right: 4px;
}

.person-button,
.link-button {
  width: 100%;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 10px 12px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.person-button:hover,
.person-button.active,
.link-button:hover {
  border-color: var(--line);
  background: var(--soft);
}

.profile {
  padding: 28px;
}

.hero {
  display: grid;
  grid-template-columns: 156px minmax(0, 1fr);
  gap: 24px;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--line);
}

.portrait {
  width: 156px;
  height: 156px;
  border: 1px solid var(--line);
  border-radius: 8px;
  object-fit: cover;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.fallback-portrait {
  display: grid;
  place-items: center;
  width: 156px;
  height: 156px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
  color: var(--accent);
  font-size: 46px;
  font-weight: 800;
}

.name {
  margin: 0 0 8px;
  font-size: clamp(30px, 4.5vw, 54px);
  line-height: 1.04;
  letter-spacing: 0;
}

.life {
  color: var(--muted);
  font-size: 18px;
}

.tag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
}

.tag {
  border-radius: 999px;
  padding: 5px 10px;
  background: var(--surface);
  color: var(--muted);
  font-size: 13px;
  border: 1px solid var(--line);
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.copy-link-button {
  min-height: 38px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 0 12px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 800;
}

.copy-link-button:hover {
  filter: brightness(0.94);
}

.secondary-action-button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  font-weight: 800;
}

.secondary-action-button:hover {
  background: var(--soft);
}

.danger-action-button {
  min-height: 38px;
  border: 1px solid #b94a35;
  border-radius: 8px;
  padding: 0 12px;
  background: #fff3ee;
  color: #8b3a24;
  cursor: pointer;
  font-weight: 800;
}

.danger-action-button:hover {
  background: #ffe5dc;
}

.edit-panel {
  margin: -6px 0 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.edit-form {
  display: grid;
  gap: 16px;
}

.edit-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.edit-header h2 {
  margin: 0;
  font-size: 20px;
}

.edit-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.edit-grid label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.edit-grid input,
.edit-grid select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  color: var(--ink);
  background: #fff;
}

.edit-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.inline-edit-form {
  display: grid;
  gap: 10px;
  margin-top: 16px;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.inline-toggle-button {
  margin-top: 14px;
}

.inline-edit-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.inline-edit-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.inline-edit-form select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  color: var(--ink);
  background: #fff;
}

.relationship-panel {
  margin-bottom: 16px;
}

.relationship-form {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.relationship-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.relationship-form input {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  color: var(--ink);
  background: #fff;
}

.relationship-form input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(47, 111, 115, 0.14);
  outline: none;
}

.relationship-input-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 40px;
  gap: 8px;
}

.clear-field-button {
  width: 40px;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
  font-size: 22px;
  font-weight: 900;
  line-height: 1;
}

.clear-field-button:hover {
  background: #fff3ee;
  color: #8b3a24;
}

.relationship-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 10px;
  grid-column: 1 / -1;
}

.relationship-summary {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 14px;
  margin-bottom: 18px;
}

.relationship-summary.compact {
  margin-bottom: 14px;
}

.relationship-summary p {
  margin: 0;
}

.relationship-name {
  margin-bottom: 4px !important;
  color: var(--accent);
  font-size: 20px;
  font-weight: 900;
  text-transform: capitalize;
}

.relationship-share-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: -4px 0 18px;
}

.relationship-path-layout {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.relationship-path-section {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: rgba(255, 255, 255, 0.58);
}

.relationship-path-section h3 {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 14px;
}

.relationship-degree {
  display: grid;
  place-items: center;
  width: 84px;
  min-height: 84px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--soft);
  color: var(--accent);
}

.relationship-degree.affinity {
  border-color: var(--accent-2);
  background: #f6eee8;
  color: var(--accent-2);
}

.relationship-degree strong {
  font-size: 34px;
  line-height: 1;
}

.relationship-degree span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.relationship-route {
  display: grid;
  gap: 12px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.relationship-route.up .relationship-link::before {
  content: "↑ ";
}

.relationship-route.down .relationship-link::before {
  content: "↓ ";
}

.relationship-route.affinity .relationship-link::before {
  content: "◇ ";
}

.relationship-route li {
  position: relative;
  display: grid;
  gap: 8px;
}

.relationship-route li:not(:last-child)::after {
  content: "";
  position: absolute;
  left: 22px;
  top: 52px;
  bottom: -12px;
  border-left: 1px solid var(--line);
}

.relationship-person {
  position: relative;
  z-index: 1;
  display: grid;
  gap: 3px;
  width: min(520px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px 14px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  text-align: left;
  box-shadow: 0 6px 16px rgba(30, 42, 50, 0.05);
}

.relationship-person span {
  font-weight: 900;
}

.relationship-person small {
  color: var(--muted);
}

.relationship-person:hover {
  background: var(--soft);
}

.relationship-route .endpoint .relationship-person {
  border-color: #d7a45e;
  background: #fff6e9;
}

.relationship-route .common-ancestor .relationship-person {
  border-color: #62a879;
  background: #e9f7ee;
}

.affinity-result {
  margin-top: 18px;
  border-top: 1px solid var(--line);
  padding-top: 18px;
}

.affinity-result .relationship-path-section {
  background: #fbf7f3;
}

.relationship-route.affinity .relationship-person {
  border-color: #dec8b4;
}

.relationship-link {
  width: min(520px, 100%);
  padding-left: 54px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.inline-edit-form textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  color: var(--ink);
  background: #fff;
  resize: vertical;
}

.fact-editor-list {
  display: grid;
  gap: 12px;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.fact-form {
  display: grid;
  gap: 10px;
}

.fact-edit-row {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--surface-2);
}

.content-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
  padding: 18px;
}

.panel.wide {
  grid-column: 1 / -1;
}

.panel h2 {
  margin: 0 0 14px;
  font-size: 18px;
}

.data-list {
  display: grid;
  gap: 10px;
  margin: 0;
}

.data-row {
  display: grid;
  grid-template-columns: 130px minmax(0, 1fr);
  gap: 12px;
}

.data-row dt {
  color: var(--muted);
}

.data-row dd {
  margin: 0;
}

.relation-list,
.fact-list,
.document-list {
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.fact-list li,
.document-list li {
  border-top: 1px solid var(--line);
  padding-top: 10px;
}

.fact-list li:first-child,
.document-list li:first-child {
  border-top: 0;
  padding-top: 0;
}

.biography-text {
  max-width: 860px;
  color: var(--ink);
  font-size: 16px;
  line-height: 1.65;
}

.biography-text p {
  margin: 0 0 12px;
}

.biography-text p:last-child {
  margin-bottom: 0;
}

.year {
  color: var(--accent-2);
  font-weight: 800;
}

.muted,
.empty-state {
  color: var(--muted);
}

.empty-state {
  padding: 50px;
  text-align: center;
}

.login-shell {
  display: grid;
  place-items: center;
  min-height: calc(100vh - 180px);
}

.login-card {
  display: grid;
  gap: 14px;
  width: min(420px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 24px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.login-card h2 {
  margin: 0 0 4px;
  font-size: 28px;
  line-height: 1.1;
}

.login-card label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.login-card input {
  width: 100%;
  height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  color: var(--ink);
  background: #fff;
  outline: none;
}

.login-card input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(47, 111, 115, 0.14);
}

.login-button {
  min-height: 44px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 900;
}

.login-message {
  margin: 0;
  border: 1px solid #e7b8a8;
  border-radius: 8px;
  padding: 10px 12px;
  background: #fff3ee;
  color: #8b3a24;
  font-size: 14px;
}

.external-link {
  color: var(--accent);
  font-weight: 700;
  text-decoration: none;
}

.external-link:hover {
  text-decoration: underline;
}

.tree-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 20px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
}

.tree-title {
  margin: 0;
  font-size: clamp(30px, 4.5vw, 52px);
  line-height: 1.04;
}

.tree-metrics {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.tree-metrics span {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 10px;
  background: var(--surface);
  color: var(--muted);
  font-size: 13px;
}

.tree-actions {
  display: grid;
  justify-items: end;
  gap: 10px;
}

.tree-layout {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.tree-panel {
  min-width: 0;
}

.tree-note {
  margin: -6px 0 12px;
  color: var(--muted);
  font-size: 13px;
}

.tree-scroll {
  max-height: calc(100vh - 260px);
  overflow: auto;
  padding: 6px 0 6px 6px;
}

.family-tree,
.family-tree ul {
  margin: 0;
  padding-left: 24px;
  list-style: none;
}

.family-tree {
  padding-left: 0;
}

.family-tree li {
  position: relative;
  padding: 0 0 10px 18px;
}

.family-tree li::before {
  content: "";
  position: absolute;
  left: 0;
  top: 18px;
  width: 14px;
  border-top: 1px solid var(--line);
}

.family-tree li::after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  border-left: 1px solid var(--line);
}

.family-tree > li {
  padding-left: 0;
}

.family-tree > li::before,
.family-tree > li::after {
  display: none;
}

.tree-person {
  display: inline-grid;
  grid-template-columns: auto auto;
  gap: 2px 8px;
  min-width: 240px;
  max-width: 340px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: linear-gradient(180deg, #ffffff 0%, #f8faf9 100%);
  box-shadow: 0 6px 16px rgba(30, 42, 50, 0.05);
}

.tree-label {
  grid-column: 1 / -1;
  color: var(--accent-2);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.tree-name {
  grid-column: 1 / -1;
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  font-weight: 800;
  text-align: left;
}

.tree-name:hover {
  color: var(--accent);
  text-decoration: underline;
}

.tree-years {
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 13px;
}

.tree-place {
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 12px;
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 50;
  transform: translateY(12px);
  opacity: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  background: var(--ink);
  color: #fff;
  font-weight: 800;
  box-shadow: var(--shadow);
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.show {
  transform: translateY(0);
  opacity: 1;
}

.diagnostics-dialog {
  width: min(760px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  color: var(--ink);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.diagnostics-dialog::backdrop {
  background: rgba(36, 48, 64, 0.32);
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
}

.dialog-header h2 {
  margin: 0;
  font-size: 20px;
}

#diagnosticsContent {
  display: grid;
  gap: 16px;
  max-height: min(680px, calc(100vh - 140px));
  overflow: auto;
  padding: 18px;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.metric {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--surface-2);
}

.metric strong {
  display: block;
  font-size: 24px;
}

.issue-list {
  display: grid;
  gap: 8px;
  margin: 0;
  padding-left: 18px;
}

@media (max-width: 820px) {
  .app-header {
    display: block;
    padding: 22px;
  }

  .stats {
    margin-top: 10px;
    text-align: left;
  }

  .header-actions {
    align-items: flex-start;
    flex-direction: column;
    margin-top: 14px;
  }

  .layout {
    display: block;
  }

  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .results {
    max-height: 240px;
  }

  .profile {
    padding: 20px;
  }

  .hero,
  .content-grid,
  .tree-layout,
  .edit-grid,
  .inline-edit-grid,
  .relationship-form {
    grid-template-columns: 1fr;
  }

  .view-switch {
    grid-template-columns: 1fr;
  }

  .relationship-summary {
    grid-template-columns: 1fr;
  }

  .relationship-path-layout {
    grid-template-columns: 1fr;
  }

  .tree-header {
    display: block;
  }

  .tree-metrics {
    justify-content: flex-start;
    margin-top: 12px;
  }

  .tree-actions {
    justify-items: start;
  }

  .metric-grid {
    grid-template-columns: 1fr;
  }
}
