:root {
    color-scheme: light;
    --bg: #f6f7f4;
    --panel: #ffffff;
    --text: #1f2a24;
    --muted: #66736c;
    --line: #d9dfd7;
    --accent: #2f6f55;
    --accent-strong: #22523f;
    --danger: #a03232;
    --shadow: 0 10px 30px rgba(31, 42, 36, 0.08);
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 16px/1.5 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

a {
    color: var(--accent-strong);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

img {
    display: block;
    max-width: 100%;
}

input,
textarea,
select,
button,
.button {
    border-radius: 8px;
    font: inherit;
}

input,
textarea,
select {
    width: 100%;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--text);
    padding: 0.7rem 0.8rem;
}

textarea {
    resize: vertical;
}

button,
.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 0;
    background: var(--accent);
    color: #fff;
    cursor: pointer;
    min-height: 42px;
    padding: 0.65rem 1rem;
    font-weight: 650;
}

button:hover,
.button:hover {
    background: var(--accent-strong);
    text-decoration: none;
}

.button.secondary {
    background: #e8eee9;
    color: var(--accent-strong);
}

.danger {
    background: var(--danger);
}

.danger:hover {
    background: #7f2626;
}

.link-button {
    min-height: auto;
    padding: 0;
    background: transparent;
    color: var(--danger);
    font-weight: 600;
}

.link-button:hover {
    background: transparent;
    text-decoration: underline;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.8rem clamp(1rem, 3vw, 2rem);
    background: rgba(255, 255, 255, 0.94);
    border-bottom: 1px solid var(--line);
    backdrop-filter: blur(10px);
}

.brand {
    color: var(--text);
    font-weight: 800;
}

.topbar nav {
    display: flex;
    flex-wrap: wrap;
    gap: 0.8rem;
}

.page {
    width: min(1400px, 100%);
    margin: 0 auto;
    padding: clamp(1rem, 3vw, 2rem);
}

.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
    padding: clamp(1rem, 2vw, 1.4rem);
    margin-bottom: 1rem;
}

.panel > h2:first-child {
    margin-top: 0;
}

.export-panel h1 {
    margin-top: 0;
}

.export-options {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 1rem;
    margin-top: 1.2rem;
}

.export-option {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 1rem;
}

.export-option h2 {
    margin-top: 0;
}

.export-option p {
    color: var(--muted);
}

.narrow {
    max-width: 760px;
    margin-inline: auto;
}

.stamdata-panel {
    max-width: 1040px;
}

.bio-links-grid {
    display: grid;
    grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr);
    gap: 1rem;
}

.stack {
    display: grid;
    gap: 1rem;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 1rem;
}

.stamdata-date-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.stamdata-date-grid label:last-child {
    grid-column: 1;
}

.actions,
.toolbar,
.search-form,
.search-row,
.upload-form,
.tree-controls {
    display: flex;
    align-items: end;
    flex-wrap: wrap;
    gap: 0.8rem;
}

.search-row {
    align-items: stretch;
}

.inline-action-form {
    margin: 0;
}

.search-form {
    flex: 1 1 360px;
    flex-wrap: nowrap;
}

.search-form input {
    flex: 1 1 280px;
    min-width: 0;
}

.search-results {
    margin-top: 0.8rem;
}

.person-list.compact-person-list {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.55rem;
}

.compact-person-list .person-row {
    display: inline-grid;
    grid-template-columns: 42px minmax(0, auto) auto;
    width: auto;
    min-width: 220px;
    max-width: min(100%, 320px);
}

.notice {
    border-radius: 8px;
    padding: 0.8rem 1rem;
}

.notice.error {
    background: #feecec;
    color: #7d2222;
}

.muted {
    color: var(--muted);
}

.login-page {
    display: grid;
    min-height: 100vh;
    place-items: center;
}

.login-box {
    width: min(420px, calc(100vw - 2rem));
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
    padding: 2rem;
}

.person-list,
.relation-list {
    display: grid;
    gap: 0.55rem;
}

.person-row,
.relation-row {
    display: grid;
    grid-template-columns: 42px 1fr auto;
    align-items: center;
    gap: 0.7rem;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 0.55rem;
}

.relation-row {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: start;
}

.relation-name {
    min-width: 0;
}

.relation-meta {
    color: var(--text);
    text-align: right;
    white-space: nowrap;
}

.relation-edit {
    grid-column: 1 / -1;
    margin-top: 0.25rem;
}

.relation-edit summary {
    width: fit-content;
    cursor: pointer;
    color: var(--accent-strong);
    font-weight: 700;
}

.relation-edit form {
    margin-top: 0.7rem;
}

.relation-delete-form {
    margin-top: 0.7rem;
}

.relation-delete-button {
    width: 100%;
}

.person-row img {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    object-fit: cover;
}

.person-row:not(:has(img)) {
    grid-template-columns: 1fr auto;
}

.person-hero {
    display: grid;
    grid-template-columns: 160px 1fr;
    gap: 1.4rem;
    align-items: start;
}

.profile-image {
    width: 160px;
    aspect-ratio: 1;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #eef2ee;
    overflow: hidden;
}

.profile-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.profile-image-small {
    width: 120px;
    margin-bottom: 1rem;
}

.profile-upload-block {
    margin-top: 2rem;
    border-top: 1px solid var(--line);
    padding-top: 1rem;
}

.lifespan {
    color: var(--muted);
    margin-top: -0.5rem;
}

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

.relation-form {
    margin-top: 1rem;
}

.relation-form summary {
    cursor: pointer;
    color: var(--accent-strong);
    font-weight: 750;
}

.relation-form form {
    margin-top: 1rem;
}

.danger-form {
    margin-top: 2rem;
    border-top: 1px solid var(--line);
    padding-top: 1rem;
}

.gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
    gap: 1rem;
    margin-top: 1rem;
}

.gallery figure {
    margin: 0;
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
    background: #fff;
}

.gallery-thumb {
    display: block;
    width: 100%;
    min-height: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    cursor: zoom-in;
}

.gallery-thumb:hover {
    background: transparent;
}

.gallery img {
    width: 100%;
    aspect-ratio: 4 / 3;
    object-fit: cover;
}

.gallery figcaption {
    padding: 0.55rem 0.65rem;
    color: var(--muted);
    display: -webkit-box;
    font-size: 0.85rem;
    line-height: 1.35;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.lightbox[hidden] {
    display: none;
}

.lightbox {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: grid;
    place-items: center;
    padding: clamp(1rem, 3vw, 2rem);
}

.lightbox-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 22, 18, 0.78);
}

.lightbox-dialog {
    position: relative;
    z-index: 1;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-rows: minmax(280px, calc(100vh - 12rem)) auto;
    align-items: center;
    gap: 1rem;
    width: min(1100px, 100%);
    max-height: calc(100vh - 3rem);
}

.lightbox-image {
    grid-column: 2;
    width: 100%;
    height: 100%;
    max-height: calc(100vh - 12rem);
    justify-self: center;
    border-radius: 8px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.28);
    object-fit: contain;
}

.lightbox-close,
.lightbox-nav {
    color: #fff;
    background: rgba(47, 111, 85, 0.92);
}

.lightbox-close:hover,
.lightbox-nav:hover {
    background: var(--accent-strong);
}

.lightbox-close {
    position: absolute;
    top: -0.8rem;
    right: -0.8rem;
    width: 42px;
    min-height: 42px;
    padding: 0;
    border-radius: 50%;
    font-size: 1.7rem;
    line-height: 1;
}

.lightbox-nav {
    width: 48px;
    min-height: 64px;
    padding: 0;
    font-size: 2.2rem;
}

.lightbox-prev {
    grid-column: 1;
}

.lightbox-next {
    grid-column: 3;
}

.lightbox-footer {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 1rem;
    padding: 0.8rem 1rem;
    border-radius: 8px;
    background: #fff;
}

.lightbox-caption {
    margin: 0;
    color: var(--text);
    overflow-wrap: anywhere;
}

.lightbox-actions {
    display: flex;
    align-items: center;
    gap: 0.7rem;
    flex: 0 0 auto;
}

.lightbox-delete-form {
    margin: 0;
}

.lightbox-caption-form {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
    gap: 0.7rem;
}

.document-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
    gap: 0.8rem;
    margin-top: 1rem;
}

.document-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.8rem;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 0.65rem;
}

.document-item a {
    display: flex;
    align-items: center;
    gap: 0.7rem;
    min-width: 0;
}

.document-item img,
.pdf-icon {
    width: 48px;
    height: 48px;
    border-radius: 6px;
    flex: 0 0 auto;
}

.document-item img {
    object-fit: cover;
}

.pdf-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: #e8eee9;
    color: var(--accent-strong);
    font-size: 0.8rem;
    font-weight: 800;
}

.link-list {
    display: grid;
    gap: 0.55rem;
    margin-bottom: 1rem;
}

.link-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.8rem;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 0.55rem 0.65rem;
}

.link-row a {
    min-width: 0;
    overflow-wrap: anywhere;
}

.link-create-form {
    border-top: 1px solid var(--line);
    padding-top: 1rem;
}

.tree-shell {
    min-height: calc(100vh - 240px);
}

@media (min-width: 1700px) {
    .tree-shell {
        width: calc(100vw - 4rem);
        max-width: none;
        margin-left: 50%;
        transform: translateX(-50%);
    }
}

.tree-controls {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 0.8rem;
}

.tree-control-group,
.tree-zoom-controls {
    display: flex;
    align-items: end;
    gap: 0.7rem;
}

.tree-controls label {
    min-width: 180px;
}

.tree-canvas {
    height: calc(100vh - 360px);
    min-height: 460px;
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
    background:
        linear-gradient(#eef3ef 1px, transparent 1px),
        linear-gradient(90deg, #eef3ef 1px, transparent 1px),
        #fbfcfb;
    background-size: 32px 32px;
    touch-action: none;
}

.tree-svg {
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
    user-select: none;
}

.tree-line {
    stroke: #9aa9a0;
    stroke-width: 2;
    fill: none;
}

.tree-line.partner {
    stroke: #6f88a6;
    stroke-dasharray: 6 5;
}

.tree-line.certainty-strong {
    stroke: #d8b700;
}

.tree-line.certainty-medium {
    stroke: #e58a1f;
}

.tree-line.certainty-speculative {
    stroke: #c7352f;
}

.tree-line.highlighted {
    stroke: #5ee38a;
    stroke-width: 3.5;
}

.node-card rect {
    fill: #fff;
    stroke: #cfd8d1;
    stroke-width: 1.5;
    rx: 8;
}

.node-card.root rect {
    stroke: var(--accent);
    stroke-width: 3;
}

.node-card text {
    fill: var(--text);
    font-size: 13px;
}

.node-card .birth-name,
.node-card .years {
    fill: var(--muted);
    font-size: 11px;
}

.person-card-portrait .node-card text {
    font-size: 12px;
}

.person-card-portrait .node-card .birth-name,
.person-card-portrait .node-card .years {
    font-size: 11px;
}

.tree-context-menu {
    position: fixed;
    z-index: 1000;
    min-width: 180px;
    padding: 0.35rem;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    box-shadow: 0 12px 30px rgba(20, 35, 28, 0.16);
}

.tree-context-menu-title {
    padding: 0.45rem 0.55rem;
    color: var(--muted);
    font-size: 0.85rem;
    border-bottom: 1px solid var(--line);
    margin-bottom: 0.25rem;
}

.tree-context-menu a {
    display: block;
    padding: 0.5rem 0.55rem;
    border-radius: 6px;
    color: var(--accent);
    font-weight: 700;
}

.tree-context-menu a:hover {
    background: #edf3ef;
    text-decoration: none;
}

@media (max-width: 900px) {
    .grid-3,
    .person-hero,
    .bio-links-grid {
        grid-template-columns: 1fr;
    }

    .stamdata-date-grid {
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    }

    .stamdata-date-grid label:last-child {
        grid-column: auto;
    }

    .profile-image {
        width: 120px;
    }
}

@media (max-width: 640px) {
    .topbar {
        align-items: flex-start;
        flex-direction: column;
    }

    .person-row,
    .relation-row {
        grid-template-columns: 1fr;
    }

    .relation-meta {
        text-align: left;
        white-space: normal;
    }

    .tree-canvas {
        min-height: 520px;
    }
}
