:root {
    --royal-blue: #1a3a6b;
    --gold: #c9a84c;
    --gold-light: #f5ecd0;
    --gold-border: #ddc56a;
    --connector: #a09070;
    --page-bg: #f8f5ef;
    --card-bg: #fffdf7;
    --card-border: #e0d5b8;
    --text-primary: #1a3a6b;
    --text-muted: #6a7a9a;

    --branch-indent: 28px;
    --stem-offset: 20px;
    --row-top: 16px;
    --row-center: 50px;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

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

body {
    margin: 0;
    padding: 0;
    background: var(--page-bg);
    font-family: Georgia, "Times New Roman", serif;
    color: var(--text-primary);
    min-height: 100vh;
}

.royal-header {
    text-align: center;
    padding: 36px 20px 28px;
    background: linear-gradient(150deg, #1a3a6b 0%, #2c5282 55%, #1a3a6b 100%);
    color: #f0e6c8;
    border-bottom: 4px solid var(--gold);
}

.royal-crown {
    font-size: 48px;
    line-height: 1;
    margin-bottom: 10px;
    color: var(--gold);
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}

.royal-header h1 {
    font-size: 28px;
    font-weight: 700;
    letter-spacing: 2px;
    margin: 0 0 6px;
}

.royal-subtitle {
    font-size: 13px;
    letter-spacing: 4px;
    text-transform: uppercase;
    color: var(--gold);
    margin: 0 0 6px;
    opacity: 0.9;
}

.tree-source {
    margin: 0 0 18px;
    font-size: 12px;
    color: #f5ecd0;
    opacity: 0.85;
}

.tree-source code {
    background: rgba(255, 255, 255, 0.13);
    padding: 2px 6px;
    border-radius: 4px;
}

.controls {
    display: flex;
    gap: 10px;
    justify-content: center;
}

.controls--view {
    margin-bottom: 2px;
}

.controls--list,
.controls--graph {
    justify-content: flex-start;
    margin: 0 0 14px;
}

.tree-legend {
    margin: 10px 0 0;
    font-size: 12px;
    color: #f5ecd0;
}

.legend-chip {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    border-radius: 999px;
    padding: 2px 8px;
    border: 1px solid rgba(245, 236, 208, 0.55);
    background: rgba(255, 255, 255, 0.08);
}

.legend-chip.deceased {
    border-color: #c9b3ab;
    background: rgba(122, 90, 82, 0.28);
    color: #f8ece8;
}

.controls button {
    padding: 7px 20px;
    border: 1px solid var(--gold);
    background: transparent;
    color: var(--gold-light);
    border-radius: 4px;
    font-family: inherit;
    font-size: 13px;
    cursor: pointer;
    transition:
        background 0.15s,
        color 0.15s;
}

.controls button:hover {
    background: var(--gold);
    color: var(--royal-blue);
}

.controls--list button,
.controls--graph button {
    background: #f4ede0;
    color: #5f4d23;
    border-color: #b89a4a;
    box-shadow:
        0 1px 0 rgba(255, 255, 255, 0.7) inset,
        0 1px 2px rgba(42, 32, 13, 0.08);
}

.controls--list button:hover,
.controls--graph button:hover {
    background: var(--gold);
    border-color: #b89a4a;
    color: #21395f;
}

.controls--list button:focus-visible,
.controls--graph button:focus-visible {
    outline: 2px solid #8e732f;
    outline-offset: 1px;
}

.view-btn {
    min-width: 120px;
}

.view-btn.is-active {
    background: var(--gold);
    color: var(--royal-blue);
    border-color: #f0d37f;
}

main {
    padding: 32px 20px 60px;
    max-width: 1240px;
    margin: 0 auto;
}

.build-error {
    border: 1px solid #d1b8b8;
    background: #fff3f3;
    color: #6d1f1f;
    border-radius: 12px;
    padding: 18px 20px;
}

.build-error h2 {
    margin-top: 0;
}

.tree-view[hidden] {
    display: none;
}

.tree-view--graph {
    background: #ebebeb;
    border: 1px solid #d3d3d3;
    border-radius: 16px;
    padding: 18px;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.7);
}

.graph-instructions {
    margin: 0 0 10px;
    color: #676767;
    font-size: 13px;
}

.graph-canvas {
    position: relative;
    width: 100%;
    height: min(74vh, 760px);
    min-height: 420px;
    overflow: hidden;
    border-radius: 12px;
    border: 1px solid #c8c8c8;
    background:
        radial-gradient(circle at 20% 18%, rgba(255, 255, 255, 0.75) 0%, rgba(255, 255, 255, 0) 52%),
        #e5e5e5;
    touch-action: none;
}

.graph-world {
    position: absolute;
    left: 0;
    top: 0;
    transform-origin: 0 0;
}

.graph-links {
    position: absolute;
    left: 0;
    top: 0;
}

.graph-links line {
    stroke: #b8b8b8;
    stroke-width: 2;
    stroke-linecap: round;
}

.graph-links circle {
    fill: #1a1a1a;
}

.graph-nodes {
    position: absolute;
    left: 0;
    top: 0;
}

.graph-person {
    position: absolute;
    --graph-avatar-size: 102px;
    width: 190px;
    transform: translate(-50%, 0);
    text-align: center;
    user-select: none;
}

.graph-avatar {
    width: var(--graph-avatar-size);
    height: var(--graph-avatar-size);
    border-radius: 50%;
    overflow: hidden;
    margin: calc(var(--graph-avatar-size) * -0.5) auto 0;
    border: 4px solid #1f95d6;
    background: #f9f9f9;
    box-shadow:
        0 0 0 4px #ffffff,
        0 2px 8px rgba(0, 0, 0, 0.18);
    display: flex;
    align-items: center;
    justify-content: center;
}

.graph-avatar.ring-orange {
    border-color: #ea7d0b;
}

.graph-avatar.ring-blue {
    border-color: #1f95d6;
}

.graph-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.graph-person.deceased .graph-avatar img {
    filter: grayscale(100%) saturate(45%);
}

.graph-avatar-fallback {
    font-size: 24px;
    font-weight: 700;
    color: #555;
    letter-spacing: 0.6px;
}

.graph-person-name {
    margin-top: 9px;
    font-size: 27px;
    font-size: clamp(14px, 1.15vw, 27px);
    line-height: 1.08;
    font-weight: 700;
    color: #8d8d8d;
}

.graph-person-subtitle {
    margin-top: 3px;
    color: #969696;
    font-size: 20px;
    font-size: clamp(11px, 0.9vw, 20px);
    line-height: 1.2;
}

.graph-person-subtitle.is-empty {
    display: none;
}

.family-tree {
    list-style: none;
    padding: 0;
    margin: 0;
}

.person {
    position: relative;
}

.person-row {
    position: relative;
    z-index: 2;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
    transition:
        background 0.15s,
        box-shadow 0.15s,
        border-color 0.15s;
    max-width: 100%;
}

.person-row__content {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 6px 10px;
    flex: 1 1 auto;
    min-width: 0;
}

.has-children > .person-row {
    cursor: pointer;
}

.has-children > .person-row:hover {
    background: var(--gold-light);
    border-color: var(--gold);
    box-shadow: 0 3px 10px rgba(201, 168, 76, 0.25);
}

.person-row.is-active {
    border-color: #2f64b8;
    box-shadow:
        0 0 0 3px rgba(47, 100, 184, 0.2),
        0 2px 8px rgba(23, 43, 77, 0.18);
    background: #f4f8ff;
}

.person-row.deceased {
    background: #f7f4f2;
    border-color: #d8cbc3;
}

.person-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--royal-blue);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.5px;
    flex-shrink: 0;
    border: 2px solid var(--gold-border);
    user-select: none;
    overflow: hidden;
}

.person-photo,
.details-photo {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.person-row.deceased .person-avatar {
    background: #6f6770;
    border-color: #c7b6a9;
}

.person-row.deceased .person-photo {
    filter: grayscale(100%) saturate(45%);
}

.toggle-icon {
    width: 22px;
    height: 22px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: 700;
    color: #866a21;
    background: #f6ecd1;
    border: 1px solid #d9c183;
    border-radius: 999px;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.65);
    flex-shrink: 0;
    line-height: 1;
    user-select: none;
}

.leaf-icon {
    color: #7c6f49;
    background: #f4f1e6;
    border-color: #d4ccb4;
    font-size: 18px;
}

.toggle-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: 0;
    background: transparent;
    border-radius: 999px;
    cursor: pointer;
    flex-shrink: 0;
}

.toggle-btn:hover .toggle-icon {
    background: #f0dfaf;
    border-color: #b99a47;
    color: #5f4a16;
}

.person-name {
    font-weight: 700;
    font-size: 16px;
    color: var(--royal-blue);
}

.person-row.deceased .person-name {
    color: #6f5d56;
}

.deceased-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 1px 8px;
    border-radius: 999px;
    border: 1px solid #c8b0a8;
    background: #f0eae8;
    color: #7a5a52;
    font-size: 12px;
    font-weight: 600;
    line-height: 1.4;
    white-space: nowrap;
}

.spouse-deceased-badge {
    padding: 1px 6px;
    font-size: 11px;
    font-style: normal;
    border-color: rgba(122, 90, 82, 0.4);
    background: rgba(255, 255, 255, 0.72);
    color: inherit;
}

.spouse-badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px 10px 3px 5px;
    background: var(--gold-light);
    border: 1px solid var(--gold-border);
    border-radius: 99px;
    font-size: 13px;
    color: #5a4a20;
    white-space: nowrap;
}

.spouse-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: #2d4f86;
    color: #fff;
    border: 1px solid rgba(255, 255, 255, 0.75);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    font-weight: 700;
    flex-shrink: 0;
    overflow: hidden;
}

.spouse-photo {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.marriage-icon {
    width: 19px;
    height: 19px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    border: 1px solid currentColor;
    background: rgba(255, 255, 255, 0.72);
    opacity: 0.95;
    flex-shrink: 0;
}

.marriage-icon-svg {
    width: 12px;
    height: 12px;
    display: block;
    color: inherit;
}

.spouse-badge.deceased {
    background: #f0eae8;
    border-color: #c8b0a8;
    color: #7a5a52;
    font-style: italic;
    opacity: 0.84;
}

.spouse-badge.deceased .spouse-photo {
    filter: grayscale(100%) saturate(45%);
}

.spouse-badge.external {
    background: #eaf0f8;
    border-color: #9ab0d0;
    color: #2a4a7a;
}

.ext-link {
    width: 20px;
    height: 20px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: currentColor;
    text-decoration: none;
    font-size: 11px;
    font-weight: 700;
    opacity: 0.95;
    margin-left: 4px;
    border: 1px solid currentColor;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.72);
    flex-shrink: 0;
    transition:
        opacity 0.15s,
        background 0.15s,
        border-color 0.15s;
}

.ext-link:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.95);
}

.person.deceased-person.has-children > .person-row:hover {
    background: #f3eeeb;
    border-color: #bda59c;
    box-shadow: 0 3px 10px rgba(122, 90, 82, 0.2);
}

.details-btn {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border: 1px solid #d9c183;
    border-radius: 999px;
    background: #f6ecd1;
    color: #866a21;
    cursor: pointer;
    opacity: 1;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.65);
    transition:
        opacity 0.15s,
        background 0.15s,
        border-color 0.15s,
        color 0.15s;
    padding: 0;
}

.details-btn:hover {
    opacity: 1;
    background: #f0dfaf;
    border-color: #b99a47;
    color: #5f4a16;
}

.primary-details-btn {
    margin-right: 4px;
}

.details-btn:focus,
.ext-link:focus,
.toggle-btn:focus,
.spouse-details-btn:focus {
    outline: 2px solid var(--gold);
    outline-offset: 1px;
}

.spouse-details-btn {
    width: 19px;
    height: 19px;
    border-radius: 999px;
    border: 1px solid currentColor;
    background: rgba(255, 255, 255, 0.72);
    color: inherit;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    opacity: 0.95;
    cursor: pointer;
    transition:
        opacity 0.15s,
        background 0.15s,
        border-color 0.15s;
}

.spouse-details-btn:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.95);
    border-color: currentColor;
}

.spouse-details-btn svg {
    width: 12px;
    height: 12px;
}

.children {
    list-style: none;
    padding: 0 0 0 var(--branch-indent);
    margin: 8px 0 0 var(--stem-offset);
    border-left: 2px dashed var(--connector);
}

.children > li {
    padding-top: var(--row-top);
    position: relative;
}

.children > li::before {
    content: "";
    position: absolute;
    left: calc(-1 * var(--branch-indent));
    top: var(--row-center);
    width: var(--branch-indent);
    border-top: 2px dashed var(--connector);
}

.children > li:last-child::after {
    content: "";
    position: absolute;
    left: calc(-1 * var(--stem-offset) - 2px);
    top: calc(var(--row-center) + 2px);
    bottom: 0;
    width: calc(var(--stem-offset) + 4px);
    background: var(--page-bg);
    z-index: 1;
}

.children.collapsed {
    display: none;
}

.details-overlay {
    position: fixed;
    inset: 0;
    background: rgba(10, 20, 50, 0.55);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    backdrop-filter: blur(2px);
}

.details-overlay[hidden] {
    display: none;
}

.details-dialog {
    position: relative;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-top: 4px solid var(--gold);
    border-radius: 12px;
    padding: 32px 28px 28px;
    max-width: 430px;
    width: 100%;
    box-shadow: 0 24px 64px rgba(0, 0, 0, 0.35);
}

.details-close {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 30px;
    height: 30px;
    border: none;
    background: transparent;
    font-size: 16px;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.details-close:hover {
    background: #f0e8d8;
    color: var(--royal-blue);
}

.details-avatar-wrap {
    display: flex;
    justify-content: center;
    margin-bottom: 16px;
}

.details-avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: var(--royal-blue);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    font-weight: 700;
    border: 3px solid var(--gold);
    box-shadow: 0 4px 16px rgba(201, 168, 76, 0.3);
    overflow: hidden;
}

.details-name {
    font-size: 20px;
    font-weight: 700;
    color: var(--royal-blue);
    text-align: center;
    margin: 0 0 16px;
    line-height: 1.3;
}

.details-spouses {
    display: flex;
    flex-direction: column;
    gap: 7px;
    margin-bottom: 20px;
}

.details-spouse-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--gold-light);
    border: 1px solid var(--gold-border);
    border-radius: 8px;
    font-size: 14px;
    color: #5a4a20;
}

.details-spouse-row.deceased {
    background: #f0eae8;
    border-color: #c8b0a8;
    color: #7a5a52;
    font-style: italic;
}

.details-spouse-row.external {
    background: #eaf0f8;
    border-color: #9ab0d0;
    color: #2a4a7a;
}

.details-spouse-icon {
    font-size: 16px;
    flex-shrink: 0;
    color: var(--gold);
}

.details-spouse-row.deceased .details-spouse-icon {
    color: #a08070;
}
.details-spouse-row.external .details-spouse-icon {
    color: #5080b0;
}

.details-spouse-ext-tag {
    margin-left: auto;
    font-size: 11px;
    font-style: normal;
    opacity: 0.7;
    background: #c8ddf0;
    border-radius: 4px;
    padding: 1px 6px;
}

.details-meta {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding-top: 16px;
    border-top: 1px dashed var(--card-border);
}

.details-meta-row {
    display: flex;
    align-items: baseline;
    gap: 8px;
    font-size: 14px;
}

.details-meta-row--small .details-meta-value {
    font-size: 12px;
    color: var(--text-muted);
}

.details-meta-icon {
    flex-shrink: 0;
    font-size: 13px;
}

.details-meta-label {
    color: var(--text-muted);
    font-size: 12px;
    min-width: 90px;
}

.details-meta-value {
    color: var(--text-primary);
}

.details-placeholder {
    color: #b0a88a;
    font-style: italic;
}

.details-bio {
    margin-top: 16px;
    padding-top: 14px;
    border-top: 1px dashed var(--card-border);
}

.details-bio-title {
    margin: 0 0 8px;
    font-size: 15px;
    color: var(--royal-blue);
}

.details-bio-content {
    font-size: 14px;
    line-height: 1.55;
    color: var(--text-primary);
}

.details-bio-content:focus {
    outline: 2px solid var(--gold);
    outline-offset: 2px;
    border-radius: 6px;
}

.details-bio-content > :first-child {
    margin-top: 0;
}

.details-bio-content > :last-child {
    margin-bottom: 0;
}

.details-bio-content h1,
.details-bio-content h2,
.details-bio-content h3,
.details-bio-content h4 {
    color: var(--royal-blue);
    margin: 14px 0 8px;
    line-height: 1.3;
}

.details-bio-content h1 { font-size: 18px; }
.details-bio-content h2 { font-size: 16px; }
.details-bio-content h3 { font-size: 15px; }
.details-bio-content h4 { font-size: 14px; }

.details-bio-content p {
    margin: 8px 0;
}

.details-bio-content ul,
.details-bio-content ol {
    margin: 8px 0;
    padding-left: 20px;
}

.details-bio-content blockquote {
    margin: 10px 0;
    padding: 8px 10px;
    border-left: 3px solid var(--gold-border);
    background: #f8f1db;
    color: #574727;
    border-radius: 4px;
}

.details-bio-content img {
    display: block;
    width: 100%;
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    border: 1px solid var(--card-border);
    margin: 10px 0;
}

@media (max-width: 600px) {
    :root {
        --branch-indent: 18px;
        --stem-offset: 14px;
        --row-top: 14px;
        --row-center: 40px;
    }

    main {
        padding: 20px 12px 40px;
    }

    .controls--view {
        flex-wrap: wrap;
    }

    .view-btn {
        min-width: 110px;
    }

    .royal-header h1 {
        font-size: 20px;
        letter-spacing: 1px;
    }

    .tree-view--graph {
        padding: 12px;
    }

    .graph-canvas {
        min-height: 360px;
        height: min(70vh, 620px);
    }

    .graph-person {
        --graph-avatar-size: 86px;
        width: 154px;
    }

    .graph-avatar-fallback {
        font-size: 19px;
    }

    .person-avatar {
        width: 40px;
        height: 40px;
        font-size: 10px;
    }

    .toggle-icon {
        width: 20px;
        height: 20px;
        font-size: 14px;
    }
    .leaf-icon {
        font-size: 16px;
    }

    .person-name {
        font-size: 14px;
        white-space: normal;
    }

    .spouse-badge {
        font-size: 12px;
        padding: 2px 8px 2px 4px;
        white-space: normal;
    }
    .spouse-avatar {
        width: 40px;
        height: 40px;
        font-size: 9px;
    }
    .spouse-details-btn {
        width: 18px;
        height: 18px;
    }
    .marriage-icon {
        width: 18px;
        height: 18px;
    }
    .marriage-icon-svg {
        width: 11px;
        height: 11px;
    }

    .person-row {
        padding: 8px 10px;
    }

    .details-dialog {
        padding: 24px 18px 20px;
    }

    .details-avatar {
        width: 64px;
        height: 64px;
        font-size: 20px;
    }

    .details-name {
        font-size: 17px;
    }
}
