/* Optimized lazy genealogy tree + search + responsive */
.stree-tree-page {
    --stree-line: #c5cdd8;
    --stree-card: #fff;
    --stree-accent: #2c7be5;
    --stree-dead: #e63757;
    --stree-muted: #748194;
    max-width: 100%;
    width: 100%;
    overflow-x: hidden;
    box-sizing: border-box;
}

.stree-tree-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: .75rem;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1rem;
}

.stree-tree-toolbar h4 {
    color: #344050;
    font-weight: 700;
}

.stree-tree-hint {
    color: var(--stree-muted);
    font-size: .875rem;
    margin: 0;
}

.stree-search-panel {
    background: #fff;
    border-radius: .85rem !important;
    border: 1px solid #e3e6ed !important;
}

.stree-search-panel .form-label {
    font-size: .8rem;
    color: #5e6e82;
}

.stree-search-bar {
    display: flex;
    flex-wrap: nowrap;
    align-items: flex-end;
    gap: .5rem;
}

.stree-search-field {
    flex: 1 1 0;
    min-width: 0;
}

.stree-search-field:first-child {
    flex: 0 0 120px;
}

.stree-search-field-name {
    flex: 2 1 0;
    min-width: 160px;
}

.stree-search-actions {
    display: flex;
    flex-shrink: 0;
    gap: .35rem;
    align-items: flex-end;
    padding-bottom: 1px;
}

.stree-search-status {
    font-size: .85rem;
    color: var(--stree-muted);
}

.stree-search-results {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: .5rem;
    max-height: 240px;
    overflow: auto;
}

.stree-search-item {
    display: flex;
    align-items: center;
    gap: .65rem;
    padding: .55rem .7rem;
    border: 1px solid #e3e6ed;
    border-radius: .65rem;
    background: #f9fafd;
    cursor: pointer;
    text-align: right;
    width: 100%;
}

.stree-search-item:hover,
.stree-search-item:focus {
    border-color: #b6d0f7;
    background: #eef5ff;
}

.stree-search-item img {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.stree-search-item-text {
    flex: 1;
    min-width: 0;
    text-align: right;
}

.stree-search-name-stack {
    align-items: flex-start;
}

.stree-search-item .meta {
    font-size: .72rem;
    color: var(--stree-muted);
}

.stree-tree-shell {
    background: linear-gradient(180deg, #f8fafc 0%, #eef2f7 100%);
    border-radius: 1rem;
    border: 1px solid #e3e6ed;
    min-height: 50vh;
    max-height: min(75vh, 900px);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    padding: 1.25rem;
    direction: ltr;
    position: relative;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.stree-tree {
    display: inline-block;
    min-width: 100%;
    text-align: center;
    white-space: nowrap;
}

.stree-tree ul {
    padding-top: 1.25rem;
    position: relative;
    display: flex;
    justify-content: center;
    margin: 0;
    padding-left: 0;
    padding-right: 0;
}

.stree-tree li {
    list-style: none;
    text-align: center;
    position: relative;
    padding: 1.25rem 8px 0;
    float: none;
}

.stree-tree li::before,
.stree-tree li::after {
    content: "";
    position: absolute;
    top: 0;
    right: 50%;
    border-top: 2px solid var(--stree-line);
    width: 50%;
    height: 1.25rem;
}

.stree-tree li::after {
    right: auto;
    left: 50%;
    border-left: 2px solid var(--stree-line);
}

.stree-tree li:only-child::before,
.stree-tree li:only-child::after {
    display: none;
}

.stree-tree li:only-child {
    padding-top: 0;
}

.stree-tree li:first-child::before,
.stree-tree li:last-child::after {
    border: 0 none;
}

.stree-tree li:last-child::before {
    border-right: 2px solid var(--stree-line);
    border-radius: 0 6px 0 0;
}

.stree-tree li:first-child::after {
    border-radius: 6px 0 0 0;
}

.stree-tree ul ul::before {
    content: "";
    position: absolute;
    top: 0;
    left: 50%;
    border-left: 2px solid var(--stree-line);
    width: 0;
    height: 1.25rem;
}

.stree-node {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    gap: .35rem;
    vertical-align: top;
}

.stree-card {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    width: 118px;
    padding: .55rem .4rem .65rem;
    background: var(--stree-card);
    border: 1px solid #e3e6ed;
    border-radius: .85rem;
    box-shadow: 0 4px 14px rgba(33, 50, 91, .06);
    text-decoration: none;
    color: inherit;
    transition: transform .15s ease, box-shadow .15s ease, border-color .15s ease;
    white-space: normal;
}

.stree-card-text {
    width: 100%;
}

.stree-card:hover {
    transform: translateY(-2px);
    border-color: #b6d0f7;
    box-shadow: 0 8px 20px rgba(44, 123, 229, .12);
    color: inherit;
}

.stree-card.is-dead {
    border-color: #f5c2c7;
}

.stree-card.is-highlight {
    border-color: var(--stree-accent);
    box-shadow: 0 0 0 3px rgba(44, 123, 229, .25);
    animation: streePulse 1.2s ease 2;
}

.stree-spouse.is-highlight {
    border-color: var(--stree-accent);
    box-shadow: 0 0 0 3px rgba(44, 123, 229, .25);
    animation: streePulse 1.2s ease 2;
}

@keyframes streePulse {
    0%, 100% { box-shadow: 0 0 0 3px rgba(44, 123, 229, .25); }
    50% { box-shadow: 0 0 0 6px rgba(44, 123, 229, .12); }
}

.stree-avatar {
    width: 52px;
    height: 52px;
    border-radius: 50%;
    object-fit: cover;
    background: transparent;
    border: 2px solid #fff;
    box-shadow: 0 2px 8px rgba(15, 34, 58, .12);
    flex-shrink: 0;
}

.stree-avatar.is-sadat {
    box-shadow: 0 2px 10px rgba(25, 135, 84, .35);
}

.stree-avatar.is-dead {
    box-shadow: 0 2px 10px rgba(108, 117, 125, .35);
}

.stree-avatar.is-external {
    box-shadow: 0 2px 10px rgba(220, 53, 69, .35);
}

.stree-legend-avatar {
    width: 18px;
    height: 18px;
    vertical-align: -3px;
    margin-inline: 2px;
}

.stree-name {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: .15rem;
    margin: .35rem 0 0;
    font-size: .78rem;
    font-weight: 600;
    line-height: 1.25;
    color: #344050;
    max-width: 118px;
    text-align: center;
}

.stree-name-stack,
.stree-spouse-name-stack,
.stree-search-name-stack {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: .1rem;
    max-width: 100%;
    min-width: 0;
}

.stree-name-first,
.stree-spouse-name-first,
.stree-search-name-first {
    display: block;
    max-width: 100%;
    font-weight: 700;
    line-height: 1.2;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

.stree-name-last,
.stree-spouse-name-last,
.stree-search-name-last {
    display: block;
    max-width: 100%;
    font-size: .68rem;
    font-weight: 500;
    line-height: 1.15;
    color: #5e6e82;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

.stree-name-stack.is-long .stree-name-first,
.stree-spouse-name-stack.is-long .stree-spouse-name-first,
.stree-search-name-stack.is-long .stree-search-name-first {
    font-size: .68rem;
}

.stree-name-stack.is-long .stree-name-last,
.stree-spouse-name-stack.is-long .stree-spouse-name-last,
.stree-search-name-stack.is-long .stree-search-name-last {
    font-size: .62rem;
}

.stree-name-stack.is-xlong .stree-name-first,
.stree-spouse-name-stack.is-xlong .stree-spouse-name-first,
.stree-search-name-stack.is-xlong .stree-search-name-first {
    font-size: .62rem;
}

.stree-name-stack.is-xlong .stree-name-last,
.stree-spouse-name-stack.is-xlong .stree-spouse-name-last,
.stree-search-name-stack.is-xlong .stree-search-name-last {
    font-size: .56rem;
}

.stree-name-text {
    display: inline-block;
    max-width: 100%;
}

@media (max-width: 767.98px) {
    .stree-search-bar {
        flex-wrap: wrap;
    }

    .stree-search-field:first-child,
    .stree-search-field-name {
        flex: 1 1 calc(50% - .25rem);
        min-width: 120px;
    }

    .stree-search-field-name {
        flex: 1 1 100%;
    }

    .stree-search-actions {
        flex: 1 1 100%;
    }
}

.stree-card.is-dead .stree-name {
    color: var(--stree-dead);
}

.stree-card.is-dead .stree-name-last {
    color: #c06176;
}

.stree-meta {
    font-size: .68rem;
    color: var(--stree-muted);
    margin-top: .15rem;
}

/* Lightweight polygamy marriage groups (not person cards) */
.stree-marriage-groups {
    list-style: none;
    margin: .35rem 0 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: .35rem;
    width: min(100%, 220px);
}

.stree-marriage-group-li {
    list-style: none;
}

.stree-marriage-group {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: .35rem .5rem;
    border: 1px dashed #c5d0de;
    background: #f7f9fc;
    border-radius: .55rem;
    padding: .35rem .5rem;
    max-width: 100%;
}

.stree-marriage-group.is-ended {
    border-color: #d0d4db;
    background: #f3f4f6;
    color: #748194;
}

.stree-marriage-group.is-unknown {
    border-style: solid;
    border-color: #e3e6ed;
    background: #fff;
}

.stree-marriage-group-title {
    font-size: .78rem;
    font-weight: 700;
    color: #344050;
    line-height: 1.3;
    overflow-wrap: anywhere;
    flex: 1 1 8rem;
    min-width: 0;
}

.stree-marriage-group-meta {
    font-size: .7rem;
    color: var(--stree-muted);
    white-space: nowrap;
}

.stree-marriage-expand {
    margin-inline-start: auto;
}

.stree-spouses {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: .2rem;
    max-width: 130px;
    width: 100%;
}

.stree-spouse {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: .2rem;
    width: 100%;
    border: 1px solid #e3e6ed;
    background: #f8fafc;
    border-radius: .55rem;
    padding: .28rem .35rem;
    font-size: .68rem;
    line-height: 1.25;
    color: #5e6e82;
    cursor: pointer;
    white-space: normal;
    text-align: center;
    transition: background .15s ease, border-color .15s ease, color .15s ease;
}

.stree-spouse:hover,
.stree-spouse:focus {
    background: #eef5ff;
    border-color: #9ec0f0;
    color: #2c7be5;
    outline: none;
}

.stree-spouse.is-profile {
    background: #fff8f0;
    border-color: #f0d6b8;
}

.stree-spouse.is-profile:hover,
.stree-spouse.is-profile:focus {
    background: #fff1e0;
    border-color: #e0b888;
}

.stree-spouse.is-ended {
    background: #f3f4f6;
    border-color: #d0d4db;
    color: #6b7280;
}

.stree-spouse.is-ended:hover,
.stree-spouse.is-ended:focus {
    background: #eef2f6;
    border-color: #9aa4b2;
    color: #374151;
}

.stree-spouse-name {
    min-width: 0;
    width: 100%;
}

.stree-spouse-name-first,
.stree-spouse-name-last {
    color: inherit;
}

.stree-type-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 1rem;
    height: 1rem;
    font-size: .7rem;
    line-height: 1;
}

.stree-name-badges {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .2rem;
    flex-shrink: 0;
}

.stree-dead-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 1rem;
    height: 1rem;
}

.stree-tombstone-icon {
    display: block;
    width: 1rem;
    height: 1rem;
    object-fit: contain;
}

.stree-type-badge.is-family {
    color: #f6c343;
}

.stree-type-badge.is-mother {
    color: #2c7be5;
}

.stree-type-badge.is-partner {
    color: #b36b00;
}

.stree-marital-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 1rem;
    height: 1rem;
    font-size: .65rem;
    line-height: 1;
}

.stree-marital-badge.is-married {
    color: #e63757;
}

.stree-marital-badge.is-single {
    color: #748194;
}

.stree-marital-badge.is-alone {
    color: #9da9bb;
}

.stree-spouse .stree-type-badge {
    margin-top: 0;
}

.stree-icon-ring {
    display: inline-block;
    width: .85rem;
    height: .85rem;
    vertical-align: -1px;
}

.stree-icon-ring--inline {
    width: .75rem;
    height: .75rem;
    color: #b36b00;
    margin: 0 .15rem;
}

.stree-expand {
    width: 28px;
    height: 28px;
    min-width: 28px;
    border-radius: 999px;
    border: 1px solid #d8e2ef;
    background: #fff;
    color: var(--stree-accent);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    padding: 0;
    line-height: 1;
    flex-shrink: 0;
    transition: background .15s ease, transform .15s ease;
}

.stree-expand:hover {
    background: #e7f1ff;
}

.stree-expand[disabled] {
    opacity: .55;
    cursor: wait;
}

.stree-expand .spinner-border {
    width: .85rem;
    height: .85rem;
    border-width: .15rem;
}

.stree-empty,
.stree-error,
.stree-loading-root {
    padding: 2rem 1rem;
    color: var(--stree-muted);
    text-align: center;
}

/* -------- Mobile: nested accordion list (RTL) -------- */
@media (max-width: 767.98px) {
    .stree-tree-page {
        padding-left: .75rem !important;
        padding-right: .75rem !important;
    }

    .stree-tree-shell,
    .stree-tree-shell.is-mobile {
        direction: rtl;
        padding: .65rem;
        overflow-x: auto;
        overflow-y: visible;
        -webkit-overflow-scrolling: touch;
        min-height: 40vh;
    }

    .stree-tree {
        display: block;
        width: 100%;
        min-width: 0 !important;
        max-width: 100%;
        white-space: normal !important;
        text-align: right;
    }

    .stree-tree ul {
        display: block !important;
        width: 100%;
        margin: 0;
        padding: 0 0 0 0;
        padding-top: 0 !important;
        justify-content: initial;
        border: 0;
    }

    /* hierarchy via a thin line — do not shrink card width */
    .stree-tree ul ul {
        margin-top: .35rem;
        margin-right: 0;
        padding-right: .55rem;
        width: 100%;
        max-width: 100%;
        min-width: 0;
        box-sizing: border-box;
        border-right: 2px solid #d8e2ef;
    }

    .stree-tree ul ul ul ul {
        padding-right: .4rem;
    }

    .stree-tree > ul {
        margin-right: 0;
        padding-right: 0;
        border-right: 0;
    }

    .stree-tree li {
        display: block;
        width: 100%;
        max-width: 100%;
        padding: .4rem 0;
        text-align: right;
        box-sizing: border-box;
    }

    .stree-tree li::before,
    .stree-tree li::after,
    .stree-tree ul ul::before {
        content: none !important;
        display: none !important;
        border: 0 !important;
    }

    .stree-node {
        display: flex !important;
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        justify-content: flex-start;
        gap: .55rem;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }

    .stree-marriage-groups {
        width: 100%;
        max-width: 100%;
    }

    .stree-marriage-group {
        width: 100%;
        box-sizing: border-box;
    }

    .stree-marriage-expand {
        width: 36px;
        height: 36px;
        min-width: 36px;
    }

    /* button first visually on the right in RTL row */
    .stree-expand {
        order: 2;
        width: 36px;
        height: 36px;
        min-width: 36px;
    }

    .stree-card {
        order: 1;
        display: flex !important;
        flex-direction: row;
        align-items: center;
        justify-content: flex-start;
        gap: .65rem;
        width: auto;
        flex: 1 1 0;
        min-width: 0;
        max-width: calc(100% - 44px);
        padding: .55rem .7rem;
        text-align: right;
        box-sizing: border-box;
    }

    .stree-card:hover {
        transform: none;
    }

    .stree-avatar {
        width: 42px;
        height: 42px;
    }

    .stree-card-text {
        flex: 1 1 auto;
        min-width: 0;
        text-align: right;
    }

    .stree-name {
        margin: 0;
        max-width: none;
        width: 100%;
        font-size: .86rem;
        line-height: 1.45;
        text-align: right;
    }

    .stree-meta {
        margin-top: .2rem;
        font-size: .72rem;
        line-height: 1.4;
        text-align: right;
        white-space: normal;
    }

    .stree-spouses {
        order: 3;
        flex: 1 0 100%;
        max-width: none;
        width: 100%;
        align-items: stretch;
        margin-top: .15rem;
        padding-right: 0;
    }

    .stree-spouse {
        justify-content: flex-start;
        text-align: right;
        padding: .35rem .55rem;
        font-size: .75rem;
    }

    .stree-name {
        justify-content: flex-start;
    }

    .stree-search-results {
        grid-template-columns: 1fr;
        max-height: 200px;
    }

    .stree-tree-toolbar h4 {
        font-size: 1.15rem;
    }
}

@media (max-width: 575.98px) {
    .stree-tree ul ul {
        margin-right: 0;
        padding-right: .45rem;
        width: 100%;
    }

    .stree-card {
        padding: .5rem .6rem;
        max-width: calc(100% - 40px);
    }

    .stree-name {
        font-size: .82rem;
    }
}
