:root {
  color-scheme: dark;
  --bg: #1c1e22;
  --panel-bg: #24272c;
  --border: #34383f;
  --text: #e8e8ea;
  --text-dim: #9a9ea5;
  --accent: #4f8ef7;
  --placeholder: #5a5f67;
  --avatar-bg: #2f333a;
  --hover: rgba(79, 142, 247, 0.18);
  --badge-bg: rgba(79, 142, 247, 0.15);
  --badge-border: rgba(79, 142, 247, 0.35);
  --button-hover: rgba(255, 255, 255, 0.08);
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f6f8;
  --panel-bg: #ffffff;
  --border: #d7dce2;
  --text: #20242a;
  --text-dim: #626b76;
  --accent: #2468d5;
  --placeholder: #757e89;
  --avatar-bg: #e6eaf0;
  --hover: rgba(36, 104, 213, 0.12);
  --badge-bg: rgba(36, 104, 213, 0.1);
  --badge-border: rgba(36, 104, 213, 0.3);
  --button-hover: rgba(0, 0, 0, 0.06);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

body {
  min-height: 100vh;
}

.layout {
  display: flex;
  height: 100vh;
  position: relative;
}

#FamilyChart {
  flex: 1;
  height: 100%;
  background: var(--bg);
  --background-color: var(--bg);
  --text-color: var(--text);
  --genderless-color: var(--panel-bg);
}

:root[data-theme="light"] #FamilyChart {
  --female-color: #f0c8ce;
  --male-color: #c6dce5;
}

:root[data-theme="light"] #FamilyChart path.link {
  stroke: #667085;
}

#theme-toggle {
  position: absolute;
  top: 16px;
  right: 336px;
  z-index: 5;
  display: grid;
  width: 40px;
  height: 40px;
  padding: 0;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-bg);
  color: var(--text);
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
}

#theme-toggle:hover {
  background: var(--button-hover);
  border-color: var(--accent);
  transform: translateY(-1px);
}

#theme-toggle svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

#theme-toggle:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#FamilyChart .link.former-spouse-link {
  stroke-dasharray: 7 5;
}

#search {
  position: absolute;
  top: 16px;
  left: 16px;
  width: 280px;
  z-index: 6;
}

#family-filter-label {
  position: absolute;
  top: 64px;
  left: 16px;
  z-index: 5;
  width: 280px;
}

#family-filter {
  width: 100%;
  min-height: 40px;
  padding: 8px 36px 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  appearance: none;
  background-color: var(--panel-bg);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16' fill='none' stroke='%239a9ea5' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m4 6 4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

#family-filter:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

#search-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-bg);
  color: var(--text);
  font-size: 16px;
}

#search-input:focus {
  outline: none;
  border-color: var(--accent);
}

#search-results {
  margin: 4px 0 0;
  padding: 4px 0;
  list-style: none;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  overflow: hidden;
}

#search-results li {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  font-size: 14px;
  cursor: pointer;
}

#search-results li.active,
#search-results li:hover {
  background: var(--hover);
}

#search-results li.no-results {
  color: var(--text-dim);
  cursor: default;
}

#search-results small {
  color: var(--text-dim);
  white-space: nowrap;
}

#details-panel {
  width: 320px;
  flex-shrink: 0;
  background: var(--panel-bg);
  border-left: 1px solid var(--border);
  padding: 24px;
  overflow-y: auto;
}

#details-panel .avatar {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 8px;
  background: var(--avatar-bg);
  margin-bottom: 16px;
  border: 1px solid var(--border);
}

#details-panel h2 {
  margin: 0 0 4px 0;
  font-size: 20px;
}

#details-panel .years {
  color: var(--text-dim);
  margin-bottom: 20px;
  font-size: 14px;
}

#details-panel .field {
  margin-top: 20px;
  margin-bottom: 12px;
}

#details-panel .field-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  margin-bottom: 2px;
}

#details-panel .field-value {
  font-size: 14px;
  word-break: break-word;
}

#details-panel .relationship-list {
  margin: 4px 0 0;
  padding-left: 18px;
  font-size: 14px;
  line-height: 1.5;
}

#details-panel .relationship-list li + li {
  margin-top: 6px;
}

#details-panel .marriage-item {
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}

#details-panel .marriage-item:first-child {
  padding-top: 0;
}

#details-panel .marriage-item > div {
  margin-top: 3px;
  color: var(--text-dim);
}

#details-panel .empty-state,
#details-panel .placeholder-note {
  color: var(--text-dim);
  font-size: 14px;
  line-height: 1.5;
}

.placeholder-badge {
  display: inline-block;
  margin-top: 8px;
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 11px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text);
  background: var(--badge-bg);
  border: 1px solid var(--badge-border);
}

.close-details {
  display: none;
}

@media (max-width: 768px) {
  html,
  body {
    overflow: hidden;
  }

  .layout {
    display: block;
    height: 100vh;
    height: 100dvh;
  }

  #search {
    top: 12px;
    left: 12px;
    right: 12px;
    width: auto;
  }

  #family-filter-label {
    top: 64px;
    left: 12px;
    width: calc(100% - 72px);
  }

  #theme-toggle {
    top: 64px;
    right: 12px;
  }

  #FamilyChart {
    height: 100%;
    touch-action: none;
  }

  #FamilyChart .card-inner {
    font-size: 12px;
    line-height: 1.25;
    padding: 4px 6px;
    overflow: hidden;
  }

  #details-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-height: 55vh;
    max-height: 55dvh;
    padding: 20px 20px calc(20px + env(safe-area-inset-bottom));
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.4);
    transform: translateY(110%);
    visibility: hidden;
    transition: transform 0.25s ease, visibility 0.25s;
    z-index: 10;
  }

  #details-panel.open {
    transform: translateY(0);
    visibility: visible;
  }

  #details-panel .close-details {
    display: block;
    position: sticky;
    top: 0;
    float: right;
    width: 36px;
    height: 36px;
    margin: -8px -8px 0 8px;
    border: 0;
    border-radius: 50%;
    background: var(--button-hover);
    color: var(--text);
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
  }
}
