/* ========================================
   MODALS, FORMS, LIGHTBOX  (Royal Heritage)
   Shared by: manage/search modals (snapshot), custom alert/confirm, lightbox.
   ======================================== */

/* ---------- Overlay ---------- */
.modal-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--bl-space-4);
    background: rgba(42, 31, 28, 0.6);
    background: color-mix(in srgb, var(--bl-deep-slate) 60%, transparent);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    z-index: 5000; /* above nav (2000) */
    overscroll-behavior: contain;
}

.modal-overlay.hidden {
    display: none;
}

/* Alerts/confirms must sit above every other modal and dropdown */
.modal-overlay--alert {
    z-index: 10010;
}

/* ---------- Dialog surface ---------- */
.modal-content {
    position: relative;
    display: flex;
    flex-direction: column;
    background: var(--bl-surface);
    color: var(--bl-text-primary);
    border-radius: var(--bl-radius-lg);
    width: min(92vw, 550px);
    max-width: 100%;
    max-height: 90vh;
    max-height: 90dvh;
    box-shadow: var(--bl-shadow-lg);
    overflow: hidden;
    animation: bl-modal-in 0.22s ease-out;
}

.modal-content--sm {
    width: min(92vw, 400px);
    text-align: center;
}

@keyframes bl-modal-in {
    from { opacity: 0; transform: translateY(16px) scale(0.98); }
    to   { opacity: 1; transform: none; }
}

@keyframes bl-sheet-in {
    from { transform: translateY(100%); }
    to   { transform: none; }
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--bl-space-3);
    padding: var(--bl-space-4) var(--bl-space-5);
    background: linear-gradient(135deg, var(--bl-primary) 0%, var(--bl-secondary) 100%);
    color: var(--bl-surface);
    flex-shrink: 0;
}

.modal-header h2 {
    font-family: var(--bl-font-body);
    font-size: var(--bl-text-lg);
    font-weight: 600;
    color: var(--bl-surface);
    margin: 0;
}

.modal-header--plain {
    justify-content: center;
    background: none;
    color: var(--bl-text-primary);
    padding-bottom: 0;
}

.modal-header--plain h2 {
    color: var(--bl-text-primary);
    font-size: var(--bl-text-xl);
}

.modal-body {
    padding: var(--bl-space-4) var(--bl-space-5);
    overflow-y: auto;
}

.modal-body p {
    margin: 0;
    color: var(--bl-text-secondary);
    line-height: 1.6;
}

.modal-footer {
    display: flex;
    justify-content: center;
    gap: var(--bl-space-3);
    padding: 0 var(--bl-space-5) var(--bl-space-5);
}

.modal-footer .btn-save,
.modal-footer .btn-cancel {
    min-width: 110px;
    border-radius: var(--bl-radius-pill);
}

.close-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    margin: -8px -12px -8px 0;
    background: none;
    border: none;
    border-radius: 50%;
    color: var(--bl-surface);
    font-size: 28px;
    line-height: 1;
    cursor: pointer;
    transition: background 0.2s;
}

.close-btn:hover {
    background: rgba(255, 255, 255, 0.18);
}

.close-btn:focus-visible {
    box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.7);
}

/* ---------- Bottom sheet on phones (opt-in with .modal-overlay--sheet) ---------- */
@media (max-width: 640px) {
    .modal-overlay--sheet {
        align-items: flex-end;
        padding: 0;
    }

    .modal-overlay--sheet .modal-content {
        width: 100%;
        max-height: 92vh;
        max-height: 92dvh;
        border-radius: var(--bl-radius-lg) var(--bl-radius-lg) 0 0;
        animation: bl-sheet-in 0.26s ease-out;
    }

    .modal-overlay--sheet .modal-header {
        padding: var(--bl-space-3) var(--bl-space-4);
    }
}

/* ---------- Form layout ---------- */
.form-scroll-area {
    padding: var(--bl-space-5);
    overflow-y: auto;
    overflow-x: hidden;
    flex: 1;
    min-height: 0;
    overscroll-behavior: contain;
}

#manage-person-form {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    width: 100%;
}

.form-section-box {
    margin-bottom: var(--bl-space-5);
    padding: var(--bl-space-4) var(--bl-space-5);
    border: 1px solid var(--bl-border);
    border-radius: var(--bl-radius-lg);
    background: var(--bl-surface);
    box-shadow: var(--bl-shadow-sm);
}

.form-section-box h3 {
    font-size: var(--bl-text-sm);
    color: var(--bl-text-secondary);
    margin-bottom: var(--bl-space-4);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-left: 4px solid var(--bl-primary);
    padding-left: var(--bl-space-3);
}

.form-hint {
    margin: 0 0 var(--bl-space-2);
    font-size: var(--bl-text-sm);
    font-weight: 400;
    color: var(--bl-text-muted);
}

.required {
    color: var(--bl-danger);
    font-weight: 700;
    margin-left: 2px;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--bl-space-4);
}

.form-group {
    margin-bottom: var(--bl-space-4);
}

.form-group label {
    display: block;
    font-size: var(--bl-text-sm);
    font-weight: 600;
    color: var(--bl-text-secondary);
    margin-bottom: 6px;
}

.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    min-height: 44px;
    padding: 10px 14px;
    border: 1px solid var(--bl-border-focus);
    border-radius: var(--bl-radius-md);
    font-size: var(--bl-text-md);
    transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
    background: var(--bl-surface);
    color: var(--bl-text-primary);
}

.form-group textarea {
    resize: vertical;
}

.form-group input::placeholder,
.form-group textarea::placeholder {
    color: var(--bl-text-muted);
}

.form-group input[type="file"] {
    padding: 8px 10px;
    font-size: var(--bl-text-sm);
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--bl-secondary);
    box-shadow: var(--bl-focus-ring);
}

.form-group input:disabled {
    background: var(--bl-surface-subtle);
    color: var(--bl-text-muted);
    cursor: not-allowed;
}

.privacy-group {
    background: var(--bl-bg);
    padding: var(--bl-space-3);
    border-radius: var(--bl-radius-md);
    border: 1px dashed var(--bl-border-focus);
}

.privacy-group.compact {
    margin-top: -10px;
}

.form-check-row {
    display: flex;
    align-items: center;
    gap: var(--bl-space-3);
    margin: var(--bl-space-4) 0 0;
    padding: var(--bl-space-3);
    min-height: 48px;
    background: var(--bl-success-soft);
    border: 1px solid var(--bl-success-soft);
    border-radius: var(--bl-radius-md);
}

.form-check-row input[type="checkbox"] {
    width: 22px;
    height: 22px;
    cursor: pointer;
    accent-color: var(--bl-success);
}

.form-check-row label {
    font-weight: 600;
    color: var(--bl-success);
    cursor: pointer;
    margin-bottom: 0;
}

.read-only-field {
    background-color: var(--bl-surface-subtle);
    color: var(--bl-text-muted);
    cursor: not-allowed;
    border-style: dashed;
}

/* ---------- Buttons ---------- */
.form-actions {
    display: flex;
    gap: var(--bl-space-3);
    justify-content: flex-end;
    padding: var(--bl-space-4) var(--bl-space-5) calc(var(--bl-space-4) + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--bl-border);
    background: var(--bl-surface);
    flex-shrink: 0;
}

.btn-save,
.btn-create-new {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    background: linear-gradient(135deg, var(--bl-primary), var(--bl-secondary));
    color: var(--bl-surface);
    border: none;
    padding: 10px 24px;
    border-radius: var(--bl-radius-sm);
    font-size: var(--bl-text-sm);
    font-weight: 600;
    cursor: pointer;
    box-shadow: var(--bl-shadow-md);
    transition: transform 0.2s, box-shadow 0.2s;
}

.btn-save:hover,
.btn-create-new:hover {
    transform: translateY(-1px);
    box-shadow: var(--bl-shadow-lg);
}

.btn-create-new {
    display: flex;
    width: calc(100% - 50px);
    margin: var(--bl-space-5) auto;
    padding: 14px;
    border-radius: var(--bl-radius-md);
    text-align: center;
}

.btn-cancel {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    background: var(--bl-surface-subtle);
    color: var(--bl-text-secondary);
    border: 1px solid var(--bl-border-focus);
    padding: 10px 24px;
    border-radius: var(--bl-radius-sm);
    font-size: var(--bl-text-sm);
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}

.btn-cancel:hover {
    background: var(--bl-border);
    color: var(--bl-text-primary);
}

/* ---------- Camera (profile photo capture) ---------- */
.photo-capture-row {
    display: flex;
    align-items: center;
    gap: var(--bl-space-3);
    margin-top: var(--bl-space-2);
}

.btn-camera {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    background: var(--bl-info-soft);
    color: var(--bl-info);
    border: 1px solid var(--bl-info-soft);
    border-radius: var(--bl-radius-sm);
    cursor: pointer;
    font-size: var(--bl-text-md);
}

.btn-camera:hover {
    border-color: var(--bl-secondary);
}

.camera-status {
    font-size: var(--bl-text-xs);
    color: var(--bl-text-muted);
}

/* JS reveals these with inline style.display */
.camera-container {
    display: none;
    margin-top: 10px;
    position: relative;
}

.camera-video {
    width: 100%;
    border-radius: var(--bl-radius-md);
    background: var(--bl-deep-slate);
}

.camera-actions {
    display: flex;
    gap: var(--bl-space-2);
    margin-top: var(--bl-space-2);
}

.camera-actions button {
    flex: 1;
    min-height: 44px;
    border: none;
    border-radius: var(--bl-radius-sm);
    color: var(--bl-surface);
    font-weight: 600;
    cursor: pointer;
}

.btn-camera-capture {
    background: var(--bl-primary);
}

.btn-camera-cancel {
    background: var(--bl-text-secondary);
}

.camera-canvas {
    display: none;
}

.camera-thumb {
    display: none;
    align-items: center;
    gap: var(--bl-space-2);
    margin-top: var(--bl-space-2);
}

.camera-thumb img {
    width: 80px;
    height: 80px;
    object-fit: cover;
    border-radius: 50%;
    border: 2px solid var(--bl-primary);
}

.btn-camera-remove {
    min-height: 44px;
    padding: 0 var(--bl-space-3);
    background: none;
    border: none;
    color: var(--bl-danger);
    cursor: pointer;
    font-size: var(--bl-text-sm);
}

/* ---------- Add-relative search modal ---------- */
.search-container {
    padding: var(--bl-space-5);
    padding-bottom: var(--bl-space-3);
}

#relation-search-input {
    width: 100%;
    min-height: 48px;
    padding: 12px 14px;
    border: 2px solid var(--bl-border);
    border-radius: var(--bl-radius-md);
    font-size: var(--bl-text-md);
    margin-bottom: var(--bl-space-3);
    color: var(--bl-text-primary);
    background: var(--bl-surface);
}

#relation-search-input:focus {
    outline: none;
    border-color: var(--bl-secondary);
    box-shadow: var(--bl-focus-ring);
}

.search-results {
    max-height: 250px;
    overflow-y: auto;
}

.search-results .search-result-item {
    display: flex;
    align-items: center;
    gap: var(--bl-space-3);
    padding: 10px;
    min-height: 56px;
    border-radius: var(--bl-radius-sm);
    cursor: pointer;
    transition: background 0.2s;
    color: var(--bl-text-primary);
}

.search-results .search-result-item:hover,
.search-results .search-result-item:focus-visible {
    background: var(--bl-surface-subtle);
}

.search-results .search-result-item strong {
    color: var(--bl-text-primary);
}

.search-results .search-result-item small {
    color: var(--bl-text-muted);
}

.search-results .search-result-item img {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid var(--bl-secondary);
    flex-shrink: 0;
}

.result-info {
    flex: 1;
    min-width: 0;
}

.result-info .result-name {
    display: block;
    font-weight: 600;
    color: var(--bl-text-primary);
}

.result-info .result-meta {
    display: block;
    font-size: var(--bl-text-xs);
    color: var(--bl-text-muted);
}

.search-divider {
    display: flex;
    align-items: center;
    text-align: center;
    padding: 0 var(--bl-space-5);
    color: var(--bl-text-muted);
}

.search-divider::before,
.search-divider::after {
    content: '';
    flex: 1;
    border-bottom: 1px solid var(--bl-border);
}

.search-divider span {
    padding: 0 var(--bl-space-4);
    font-size: var(--bl-text-xs);
    font-weight: 600;
}

/* ---------- Gallery bits still rendered by modals.js ---------- */
.gallery-item {
    position: relative;
    aspect-ratio: 1;
}

.gallery-item-delete {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.2s;
    color: var(--bl-surface);
    background: rgba(42, 31, 28, 0.5);
    border: none;
    cursor: pointer;
}

.gallery-item-delete:hover {
    transform: scale(1.1);
    background: var(--bl-danger);
}

.upload-progress {
    font-size: var(--bl-text-xs);
    color: var(--bl-primary);
    margin-top: var(--bl-space-2);
    font-weight: 600;
}

/* ---------- Lightbox ---------- */
.lightbox {
    position: fixed;
    inset: 0;
    background: rgba(2, 6, 23, 0.92);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 6000;
}

.lightbox.hidden {
    display: none;
}

.lightbox img {
    max-width: 92%;
    max-height: 88vh;
    max-height: 88dvh;
    border-radius: 4px;
    box-shadow: var(--bl-shadow-lg);
}

.lightbox-close,
.lightbox-nav {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    color: var(--bl-surface);
    background: rgba(255, 255, 255, 0.12);
    border: none;
    cursor: pointer;
    transition: background 0.2s;
    z-index: 6001;
    user-select: none;
}

.lightbox-close {
    top: max(16px, env(safe-area-inset-top, 0px));
    right: max(16px, env(safe-area-inset-right, 0px));
    border-radius: 50%;
    font-size: 32px;
    line-height: 1;
}

.lightbox-nav {
    top: 50%;
    transform: translateY(-50%);
    font-size: 28px;
    padding: 10px 16px;
    border-radius: var(--bl-radius-sm);
}

.lightbox-close:hover,
.lightbox-nav:hover {
    background: rgba(255, 255, 255, 0.3);
}

.lightbox-nav.prev { left: max(12px, env(safe-area-inset-left, 0px)); }
.lightbox-nav.next { right: max(12px, env(safe-area-inset-right, 0px)); }

/* ---------- Mobile form tuning ---------- */
@media (max-width: 640px) {
    .form-row {
        grid-template-columns: 1fr;
        gap: 0;
    }

    .form-scroll-area {
        padding: var(--bl-space-4);
    }

    .form-section-box {
        padding: var(--bl-space-4);
        margin-bottom: var(--bl-space-4);
    }

    /* >=16px prevents iOS focus-zoom */
    .form-group input,
    .form-group select,
    .form-group textarea,
    #relation-search-input {
        font-size: 16px;
    }

    .form-actions {
        padding-left: var(--bl-space-4);
        padding-right: var(--bl-space-4);
    }

    .form-actions .btn-save,
    .form-actions .btn-cancel {
        flex: 1;
    }
}

@media (prefers-reduced-motion: reduce) {
    .modal-content,
    .modal-overlay--sheet .modal-content {
        animation: none;
    }

    .btn-save,
    .btn-create-new {
        transition: none;
    }
}

/* ========================================
   POST CREATOR / FEED SHELL HELPERS
   (shared by posts/feed.html and persons/snapshot.html)
   ======================================== */
.post-creator--fixed {
    flex-shrink: 0;
    margin-bottom: var(--bl-space-5);
}

.post-creator-title {
    margin: 0 0 var(--bl-space-4);
    font-size: var(--bl-text-lg);
}

.post-media-preview--feed {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin-bottom: var(--bl-space-4);
}

.media-btn-wrapper {
    position: relative;
}

.feed-scroll {
    overflow-y: auto;
    flex: 1;
    padding-right: 5px;
    scrollbar-width: thin;
    scrollbar-color: var(--bl-border-focus) transparent;
}

.feed-section--panel {
    display: flex;
    flex-direction: column;
    max-height: 80vh;
    max-height: 80dvh;
}

.toggle-creator-row {
    text-align: end;
    margin-bottom: 10px;
}

.btn-post.btn-new-post {
    background: var(--bl-primary);
    color: var(--bl-surface);
    border: 1px solid var(--bl-primary);
    padding: 8px 20px;
    min-height: 44px;
    font-weight: 700;
    border-radius: var(--bl-radius-md);
    box-shadow: var(--bl-shadow-sm);
}

.btn-post.btn-new-post:hover {
    background: var(--bl-primary-hover);
}

/* ---------- "Do they share parents?" popup (add sibling) ---------- */
.sp-dialog { padding: 22px 20px 16px; gap: 10px; text-align: left; }
.sp-dialog h2 { margin: 0; font-size: 1.15rem; }
.sp-intro { margin: 0 0 6px; color: var(--bl-text-secondary); font-size: 0.9rem; line-height: 1.45; }
.sp-option {
    display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
    width: 100%; padding: 12px 14px; cursor: pointer; text-align: left;
    background: var(--bl-surface); color: var(--bl-text-primary);
    border: 1px solid var(--bl-border); border-radius: var(--bl-radius-md, 10px);
}
.sp-option span { color: var(--bl-text-secondary); font-size: 0.82rem; }
.sp-option:hover, .sp-option:focus-visible { border-color: var(--bl-primary, #b8741a); outline: none; }
.sp-option--primary { border-color: var(--bl-primary, #b8741a); }
.sp-cancel {
    margin-top: 4px; padding: 10px; background: none; border: 0; cursor: pointer;
    color: var(--bl-text-secondary); font-size: 0.9rem;
}
