/* RESET & BASE */
body { 
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 
    margin: 0; 
    background: #f0f2f5; 
    color: #333; 
    height: 100vh; 
    overflow: hidden;
    font-size: 14px;
}
* { 
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}
/* Ensure mobile-only menus are hidden by default on desktop; enabled only inside mobile media queries */
/* Hide mobile-only elements on desktop (show on small screens via media query) */
@media (min-width: 769px) {
    .mobile-hamburger, #mobile-actions, .mobile-hamburger-menu { display: none !important; }
}
.hidden { display: none !important; }
button { 
    cursor: pointer;
    touch-action: manipulation;
}
.scrollable { overflow-y: auto; max-height: 500px; }

/* LOGIN & INVITATION SCREENS */
#login-screen, #invitation-accept-screen { 
    position: fixed; 
    top: 0; 
    left: 0; 
    width: 100%; 
    height: 100%; 
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    z-index: 2000; 
    display: flex; 
    justify-content: center; 
    align-items: center; 
    padding: 20px;
}
.login-box { 
    background: white; 
    padding: 30px; 
    border-radius: 12px; 
    box-shadow: 0 20px 60px rgba(0,0,0,0.3); 
    width: 100%;
    max-width: 400px;
    text-align: center; 
}
.login-box h2 { 
    color: #2c3e50; 
    margin-top: 0;
    margin-bottom: 10px;
    font-size: 1.5rem;
}
.login-box p {
    color: #666;
    margin-bottom: 25px;
    font-size: 0.9rem;
}
.login-box input, .login-box textarea, .login-box select { 
    width: 100%; 
    padding: 14px; 
    margin: 8px 0; 
    border: 1px solid #ddd; 
    border-radius: 6px; 
    font-size: 16px;
    transition: border 0.3s;
}
.login-box input:focus, .login-box textarea:focus, .login-box select:focus {
    border-color: #4a90e2;
    outline: none;
    box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
}
.login-box button { 
    width: 100%; 
    padding: 14px; 
    background: #4a90e2; 
    color: white; 
    border: none; 
    border-radius: 6px; 
    font-size: 16px; 
    font-weight: 600;
    margin-top: 10px;
    transition: background 0.3s;
}
.login-box button:hover { 
    background: #357abd; 
}
#error-msg, #invitation-error { 
    color: #dc3545; 
    font-size: 14px; 
    min-height: 20px; 
    margin-top: 10px;
}

/* INVITATION INFO */
#invitation-info {
    text-align: left;
    background: #f8f9fa;
    padding: 15px;
    border-radius: 8px;
    margin-bottom: 20px;
    border-left: 4px solid #4a90e2;
}
#invitation-info h3 {
    margin-top: 0;
    color: #2c3e50;
    font-size: 1.2rem;
}
#invitation-info p {
    margin: 8px 0;
    color: #495057;
}
#invitation-info hr {
    border: none;
    border-top: 1px solid #dee2e6;
    margin: 15px 0;
}

/* DASHBOARD LAYOUT */
#dashboard { 
    display: flex; 
    flex-direction: column; 
    height: 100vh;
    overflow: hidden;
}

/* HEADER */
header { 
    height: 64px; 
    min-height: 64px;
    background: #fff; 
    border-bottom: 1px solid #e6e9ee; 
    display: grid; 
    grid-template-columns: auto 1fr auto; 
    align-items: center; 
    gap: 16px;
    padding: 0 18px; 
    box-shadow: 0 2px 10px rgba(0,0,0,0.06); 
    z-index: 100;
    flex-shrink: 0;
}
.logo { 
    font-size: 1.1rem; 
    font-weight: bold; 
    color: #2c3e50; 
    display: flex;
    align-items: center;
    gap: 8px;
    white-space: nowrap;
}
.logo i {
    color: #4a90e2;
    font-size: 1.2rem;
}

.search-box {
    flex: 1;
    margin: 0 10px;
    position: relative;
    max-width: 300px;
}

.search-group {
    display: flex;
    align-items: center;
    gap: 8px;
}
/* Make search + filter look like a single control when grouped */
.search-group .search-box { margin: 0; }
.search-group .search-box input {
    width: 100%;
    padding: 8px 12px 8px 12px;
    border: 1px solid #ddd;
    border-radius: 20px 0 0 20px; /* left-rounded only when grouped */
    font-size: 14px;
    height: 36px;
}
.search-group .search-box i {
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: #999;
    font-size: 14px;
}

/* small inline button inside search box (advanced filter) */
/* keep small-btn when used inline; filter button moved out of search box */
.search-box .small-btn {
    position: absolute;
    right: 36px;
    top: 50%;
    transform: translateY(-50%);
    background: #fff;
    border: 1px solid #ddd;
    padding: 6px 8px;
    border-radius: 6px;
    font-size: 12px;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    z-index: 120;
}
.search-box .small-btn i { font-size: 12px; color: #666; }

/* Filter button placed in header controls (outside the search input) */
.search-group .filter-btn,
.search-group .filter-btn, .controls .filter-btn {
    background: #fff;
    border: 1px solid #ddd;
    padding: 8px 12px;
    border-radius: 0 20px 20px 0; /* right-rounded to match input */
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #495057;
    font-size: 14px;
    height: 36px;
    border-left: none;
}
.controls .filter-btn:hover { background: #f1f3f5; border-color: #dcdde1; }

/* Mobile: ensure the filter button and search icon do not overlap the input text */
@media (max-width: 768px) {
    .search-box {
        max-width: none;
        margin: 0 8px;
    }
    .search-box i {
        /* search icon sits inside input on the right */
        right: 12px;
        z-index: 115;
    }
    .search-box .small-btn { right: 36px; }
}

/* Header responsive adjustments */
@media (max-width: 768px) {
    header {
        /* leave room for mobile toggle button (fixed position) */
        padding-left: 60px;
        grid-template-columns: auto 1fr auto;
        height: 56px;
    }

    .logo { font-size: 1rem; }

    /* Show only icons on header controls to save space */
    .controls button span { display: none; }

    /* Reduce welcome-text on mobile */
    #welcome-text { display: none; }

    /* Make controls buttons more compact */
    .controls button { padding: 6px 8px; font-size: 13px; }

    /* Hide export/import on mobile to reduce clutter */
    .controls button.export, .controls button.import { display: none; }

    /* Make mobile icons smaller */
    .controls button i, .search-box i, .filter-btn i, .mobile-admin-actions i { font-size: 14px; }
    .controls button { padding: 6px 6px; }

    /* Sidebar mobile admin area: show the mobile-admin-actions container */
    .mobile-admin-actions { display: block !important; }
    .mobile-admin-actions button { display: inline-flex !important; padding: 8px 10px; font-size: 13px; }

    /* Hide full controls bar on mobile; accessible via hamburger */
    .controls { display: none !important; }

    /* Mobile hamburger menu in header */
    .mobile-hamburger { display: block; position: relative; }
    /* hide the details summary since the left burger opens the menu */
    .mobile-hamburger summary { display: none; }
    .mobile-hamburger-menu { position: absolute; left: 12px; top: 56px; background: #fff; border: 1px solid #ddd; border-radius: 8px; box-shadow: 0 8px 30px rgba(0,0,0,0.12); padding: 10px; display:flex; flex-direction:column; gap:8px; z-index: 1400; min-width: 180px; }

    /* JS-driven mobile actions (left burger) */
    .mobile-actions { display: none; position: absolute; left: 12px; top: 56px; background: #fff; border: 1px solid #ddd; border-radius: 8px; box-shadow: 0 8px 30px rgba(0,0,0,0.12); padding: 10px; flex-direction:column; gap:8px; z-index:1500; min-width:180px; }
    .mobile-actions.active { display: flex; }
    .mobile-hamburger-menu button { display:flex; gap:8px; align-items:center; padding:8px 10px; border-radius:6px; border:1px solid #eee; background:#fff; }

    /* Keep only small icon buttons in the header for quick actions */
    .search-group .icon-only { display: inline-flex; margin-left: 6px; padding: 6px 8px; border-radius: 6px; border: 1px solid #ddd; background: #fff; }
    .search-group .icon-only i { font-size: 14px; }
}

/* Hide mobile-hamburger on desktop */
.mobile-hamburger { display: none; }

/* Style for collapsible mobile admin details */
.mobile-admin-actions {
    border: 1px solid #eee;
    border-radius: 8px;
    padding: 8px;
    background: #fff;
}
.mobile-admin-actions summary {
    list-style: none;
    cursor: pointer;
    font-weight: 600;
    color: #2c3e50;
    padding: 6px 8px;
}
.mobile-admin-actions summary::-webkit-details-marker { display: none; }
.mobile-admin-actions[open] summary { color: #1f3b6b; }

/* SEARCH RESULTS DROPDOWN */
#search-results {
    position: absolute;
    left: 0;
    top: calc(100% + 8px);
    width: 100%;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 8px 30px rgba(0,0,0,0.12);
    z-index: 105;
    max-height: 280px;
    overflow: auto;
    padding: 6px;
}

#search-results .rel-item {
    margin: 6px 0;
    padding: 8px 10px;
    border-radius: 6px;
    cursor: pointer;
    background: transparent;
    border: 1px solid transparent;
    display: flex;
    align-items: center;
}

#search-results .rel-item:hover {
    background: #f1f7ff;
    border-color: #4a90e2;
}

/* Thumbnail shown in quick search results */
#search-results .search-thumb {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
    margin-right: 10px;
    flex-shrink: 0;
    border: 1px solid #e6e9ee;
}

/* Ensure the input remains visually above the dropdown when focused */
.search-box input { position: relative; z-index: 110; }

.controls { 
    display: flex; 
    align-items: center;
    gap: 8px;
}
.controls button { 
    background: #f8f9fa; 
    border: 1px solid #ddd; 
    padding: 6px 10px; 
    border-radius: 6px; 
    color: #555; 
    font-size: 12px; 
    display: flex;
    align-items: center;
    gap: 4px;
    transition: all 0.3s;
    white-space: nowrap;
}
.controls button:hover { 
    background: #e9ecef; 
    border-color: #adb5bd;
}
.controls button.logout { 
    color: #dc3545; 
    border-color: #f5c6cb; 
    background: #fff; 
}

/* Header welcome text (shows logged-in user's name) */
#welcome-text {
    margin-left: 8px;
    font-size: 13px;
    color: #495057;
    line-height: 1.05;
    text-align: right;
    min-width: 140px;
}
#welcome-text strong {
    display: block;
    font-size: 14px;
    color: #1f3b6b;
    font-weight: 700;
}
.controls button.export { 
    color: #28a745; 
    border-color: #c3e6cb; 
    background: #fff; 
}
.controls button.import { 
    color: #17a2b8; 
    border-color: #bee5eb; 
    background: #fff; 
}

/* WORKSPACE */
.workspace { 
    display: flex; 
    flex: 1; 
    overflow: hidden;
    position: relative;
}

/* SIDEBAR */
.sidebar { 
    width: 380px; 
    min-width: 300px;
    max-width: 520px;
    background: #fff; 
    border-right: 1px solid #ddd; 
    display: flex; 
    flex-direction: column; 
    z-index: 30; 
    overflow: hidden; 
    box-shadow: 2px 0 10px rgba(0,0,0,0.05);
    transition: transform 0.3s ease;
    box-sizing: border-box;
    padding-right: 8px; /* small padding so content doesn't touch edge */
}

/* On small screens, when the sidebar is active, make it full-width */
@media (max-width: 768px) {
    .sidebar { width: 100% !important; min-width: 100% !important; max-width: 100% !important; left: 0; right: 0; }
    .sidebar.active { transform: translateX(0) !important; width: 100% !important; }
}

    /* ----- Revert visual tweaks for Admin list and keep Delete button small ----- */
    /* Apply these tweaks only on desktop widths so mobile full-width sidebar is not overridden */
    @media (min-width: 769px) {
    /* Restore original sidebar sizing and rel-item layout to previous behavior */
    .sidebar {
        width: 400px !important;
        min-width: 350px !important;
        max-width: 500px !important;
    }
    .tabs { z-index: auto !important; }
    .rel-item {
        align-items: center !important;
        justify-content: space-between !important;
        gap: 0 !important;
        margin-bottom: 4px !important;
    }
    .rel-item > div:last-child {
        flex: 0 0 auto !important;
        max-width: none !important;
    }
    .user-actions {
        width: auto !important;
        align-items: flex-end !important;
    }
    .user-actions .actions-row {
        justify-content: flex-end !important;
        gap: 6px !important;
    }

    /* Make delete button smaller and remove underline (override .danger-link) */
    .user-actions .action-btn.danger-link,
    .rel-item .action-btn.danger-link {
        text-decoration: none !important;
        padding: 4px 6px !important;
        font-size: 12px !important;
        background: #fff0f0 !important;
        border-color: #f5c6cb !important;
        color: #a00 !important;
    }
    }


/* PROFILE HEADER */
.profile-header { 
    padding: 15px; 
    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
    border-bottom: 1px solid #ddd; 
    display: flex; 
    flex-direction: column;
    gap: 12px;
    position: relative;
    flex-shrink: 0;
}
.profile-header-top {
    display: flex;
    align-items: flex-start; 
    gap: 15px;
    width: 100%;
}
.avatar-wrapper {
    min-width: 80px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    align-items: center;
}
.avatar-img {
    width: 80px;
    height: 80px;
    border-radius: 10px;
    overflow: hidden;
    position: relative;
    border: 3px solid #fff;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    background: #ddd;
}
.avatar-img img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.avatar-img:hover .overlay {
    opacity: 1;
}
.overlay { 
    position: absolute; 
    inset: 0; 
    background: rgba(0,0,0,0.7); 
    display: flex; 
    justify-content: center; 
    align-items: center; 
    color: white; 
    opacity: 0; 
    transition: 0.3s;
    font-size: 20px;
}

/* Small icon row beneath the avatar for quick actions (zoom / edit) */
.avatar-icons {
    display: flex;
    gap: 8px;
    margin-top: 6px;
}
.avatar-icons .icon-btn {
    background: #fff;
    border: 1px solid #ddd;
    padding: 6px 8px;
    border-radius: 6px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
}
.avatar-icons .icon-btn i { font-size: 14px; }

/* Modal tweaks for photo dialogs */
dialog#photo-zoom-modal, dialog#photo-edit-modal {
    border: none;
    border-radius: 10px;
    padding: 0;
    width: auto;
    max-width: 94vw;
}
dialog#photo-edit-modal .modal-body { padding: 18px; }

.name-display { 
    flex: 1; 
    min-width: 0;
}
.name-display h2 { 
    margin: 0 0 5px 0; 
    font-size: 1.3rem; 
    color: #2c3e50;
    font-weight: 600;
    line-height: 1.2;
}
.name-display .meta { 
    font-size: 12px; 
    color: #666; 
    display: block; 
    margin-bottom: 5px;
    line-height: 1.3;
}
.name-display .id-display {
    font-size: 11px;
    color: #999;
    background: #f8f9fa;
    padding: 2px 6px;
    border-radius: 3px;
    display: inline-block;
    margin-top: 3px;
}

/* PROFILE ACTIONS */
.profile-actions { 
    margin-top: 10px; 
    display: flex; 
    gap: 8px; 
    width: 100%;
    flex-wrap: wrap;
}
.primary-btn { 
    background: #4a90e2; 
    color: white; 
    border: none; 
    padding: 8px 14px; 
    border-radius: 6px; 
    font-size: 13px; 
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 5px;
    transition: background 0.3s;
    flex: 1;
    min-width: 120px;
    justify-content: center;
}
.primary-btn:hover { 
    background: #357abd; 
}
.secondary-btn {
    background: #6c757d;
    color: white;
    border: none;
    padding: 8px 14px;
    border-radius: 6px;
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 5px;
    flex: 1;
    min-width: 120px;
    justify-content: center;
}
.danger-link { 
    background: none; 
    border: none; 
    color: #dc3545; 
    font-size: 13px; 
    text-decoration: underline; 
    padding: 8px 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 5px;
    flex: 1;
    justify-content: center;
    min-width: 120px;
}

/* TABS */
.tabs { 
    display: flex; 
    border-bottom: 1px solid #ddd; 
    background: #f8f9fa; 
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-shrink: 0;
    position: relative;
    z-index: 40; /* keep tabs above other sidebar content */
}
.tabs::-webkit-scrollbar {
    display: none;
}
.tab-link { 
    padding: 12px 15px; 
    border: none; 
    background: transparent; 
    border-bottom: 3px solid transparent; 
    font-size: 13px; 
    color: #666; 
    font-weight: 500;
    transition: all 0.3s;
    white-space: nowrap;
    min-width: 70px;
    text-align: center;
}
.tab-link:hover { 
    background: #e9ecef; 
    color: #495057;
}
.tab-link.active { 
    border-bottom: 3px solid #4a90e2; 
    color: #4a90e2; 
    font-weight: 600; 
    background: #fff; 
}

.tab-content { 
    padding: 15px; 
    display: none; 
    overflow-y: auto; 
    flex: 1;
    -webkit-overflow-scrolling: touch;
}
.tab-content.active { 
    display: block; 
}

/* FORMS */
.form-section { 
    margin-bottom: 20px; 
}
label { 
    display: block; 
    font-size: 12px; 
    font-weight: 600; 
    color: #495057; 
    margin-bottom: 5px; 
    margin-top: 12px; 
}
input, select, textarea { 
    width: 100%; 
    padding: 10px; 
    border: 1px solid #ced4da; 
    border-radius: 6px; 
    font-size: 14px; 
    background: #fff; 
    transition: border 0.3s;
}
input:focus, select:focus, textarea:focus {
    border-color: #4a90e2;
    outline: none;
    box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
}
/* Password show/hide wrapper and button */
.pw-wrap { position: relative; display: flex; align-items: center; }
.pw-wrap .pw-toggle-input { width: 100%; padding-right: 34px; box-sizing: border-box; }
.pw-toggle { position: absolute; right: 8px; background: transparent; border: none; cursor: pointer; width:22px; height:22px; display:flex; align-items:center; justify-content:center; color:#6c757d; border-radius:0; }
.pw-toggle:focus { outline: 2px solid rgba(108,117,125,0.12); box-shadow: 0 0 0 3px rgba(108,117,125,0.06); }
.pw-toggle i { font-size: 14px; pointer-events: none; }
.pw-toggle[title] { line-height: 1; }
.pw-toggle:hover { color: #4a90e2; }

/* Prevent the global .login-box button rule from expanding the small inline toggle */
.login-box .pw-wrap .pw-toggle {
    width: 22px;
    height: 22px;
    right: 8px;
    background: transparent;
    border: none;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #6c757d;
    border-radius: 0;
    position: absolute;
}
.pw-toggle[title] { line-height: 1; }

.form-row { 
    display: flex; 
    gap: 10px; 
    margin-bottom: 12px;
    flex-wrap: wrap;
}
.form-group {
    flex: 1;
    min-width: 120px;
}

/* FAMILY LISTS & INVITATION LISTS */
.family-list { 
    margin-top: 10px; 
}
.family-group { 
    margin-bottom: 15px; 
    background: #f8f9fa;
    padding: 12px;
    border-radius: 8px;
    border: 1px solid #e9ecef;
}
.group-header { 
    font-size: 13px; 
    font-weight: 600; 
    color: #495057; 
    border-bottom: 1px solid #dee2e6; 
    padding-bottom: 8px; 
    margin-bottom: 8px; 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
}
.add-mini { 
    background: #fff; 
    border: 1px solid #4a90e2; 
    padding: 4px 10px; 
    font-size: 11px; 
    border-radius: 4px; 
    color: #4a90e2; 
    font-weight: 500;
    transition: all 0.3s;
    white-space: nowrap;
}
.add-mini:hover { 
    background: #4a90e2; 
    color: white; 
}

.list-container { 
    font-size: 13px; 
}
.rel-item { 
    padding: 8px 10px; 
    border-radius: 6px; 
    cursor: pointer; 
    color: #495057; 
    display: flex; 
    align-items: flex-start; 
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 8px;
    background: white;
    border: 1px solid #e9ecef;
    transition: all 0.2s;
    box-sizing: border-box;
}
.rel-item:hover { 
    background: #e7f3ff; 
    border-color: #4a90e2;
}
.rel-item .actions {
    display: flex;
    gap: 5px;
    opacity: 1;
}
.rel-item .action-btn {
    background: none;
    border: none;
    color: #6c757d;
    cursor: pointer;
    padding: 2px 5px;
    font-size: 12px;
}
.rel-item .action-btn:hover {
    color: #4a90e2;
}

/* Admin users list actions layout */
.user-actions {
    display: flex;
    flex-direction: column;
    gap: 6px;
    align-items: flex-end;
    width: 100%;
}
.user-actions .badges-row {
    display: flex;
    gap: 6px;
    align-items: center;
}
.user-actions .actions-row {
    display: flex;
    gap: 6px;
}

/* Keep action buttons inside the sidebar: allow content to shrink and actions to wrap */
.rel-item {
    flex-wrap: wrap;
}
.rel-item > div:first-child {
    /* left column (user text) should be allowed to shrink to keep actions visible */
    flex: 1 1 140px;
    min-width: 0; /* allow text truncation */
    overflow: hidden;
}
.rel-item > div:last-child {
    /* right column (actions) constrained so it doesn't overflow the sidebar */
    flex: 0 0 200px;
    max-width: 240px;
    box-sizing: border-box;
}
.rel-item > div:first-child strong,
.rel-item > div:first-child div {
    display: block;
    /* Allow meta text to wrap so details like joined date are visible */
    overflow: visible;
    text-overflow: unset;
    white-space: normal;
    overflow-wrap: anywhere;
}
.user-actions {
    flex: 0 0 auto;
}
.user-actions .actions-row {
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 6px;
}
.user-actions .action-btn {
    white-space: nowrap;
}
.user-actions .action-btn {
    background: #f8f9fa;
    border: 1px solid #ddd;
    padding: 6px 8px;
    border-radius: 6px;
    font-size: 12px;
    color: #333;
}
.user-actions .action-btn.danger-link {
    background: #fff0f0;
    border-color: #f5c6cb;
    color: #a00;
}
.user-actions .action-btn:hover {
    background: #e9ecef;
}
.can-edit-label {
    font-size: 12px;
    color: #555;
    display: flex;
    gap: 6px;
    align-items: center;
}
.empty-msg { 
    font-size: 12px; 
    color: #6c757d; 
    font-style: italic; 
    padding: 10px; 
    text-align: center;
    background: #f8f9fa;
    border-radius: 6px;
}

/* TREE WRAPPER */
.tree-wrapper { 
    flex: 1; 
    background: #f4f7f6; 
    position: relative;
    overflow: hidden;
}
#tree.dark, .tree-wrapper.dark {
    background: #0f1720; /* deep background */
}
#tree.dark .ft-link path, .tree-wrapper.dark .ft-link path {
    stroke: rgba(255,255,255,0.6);
}
#tree.dark .ft-link.active path, .tree-wrapper.dark .ft-link.active path,
#tree.dark .ft-link:hover path, .tree-wrapper.dark .ft-link:hover path {
    stroke: #ffd166;
    filter: drop-shadow(0 6px 12px rgba(0,0,0,0.5));
}
.tree-wrapper.dark .tree-controls {
    background: rgba(20,20,30,0.8);
    box-shadow: 0 6px 18px rgba(0,0,0,0.5);
}
.tree-wrapper.dark .tree-controls button {
    background: transparent;
    border-color: rgba(255,255,255,0.06);
    color: #e6eef8;
}
.tree-wrapper.dark .selected-node svg rect {
    stroke: #ffd166 !important;
}

/* Body-level helper to slightly darken entire app when tree dark mode is active */
body.tree-dark {
    background: #0b1120;
    color: #e6eef8;
}
#tree { 
    width: 100%; 
    height: 100%; 
}

/* Visual selected node marker (applies to FamilyTree node element) */
#tree .selected-node svg rect {
    stroke-width: 3 !important;
    stroke: var(--selected-border, #ffb84d) !important;
}
/* Slight glow for visibility */
#tree .selected-node {
    filter: drop-shadow(0 6px 12px rgba(0,0,0,0.08));
}

/* TREE CONTROLS */
.tree-controls {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 100;
    display: flex;
    gap: 6px;
    background: white;
    padding: 6px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

/* FamilyTree link styling */
#tree .ft-link path {
  stroke: rgba(44,62,80,0.8);
  stroke-width: 2.5px;
  fill: none;
  transition: stroke 120ms, stroke-width 120ms;
}
#tree .ft-link.active path,
#tree .ft-link:hover path {
  stroke: var(--selected-border, #ffb84d);
  stroke-width: 4px;
  filter: drop-shadow(0 6px 12px rgba(0,0,0,0.12));
}
.tree-controls button {
    background: #f8f9fa;
    border: 1px solid #dee2e6;
    padding: 5px 8px;
    border-radius: 4px;
    color: #495057;
    font-size: 11px;
    display: flex;
    align-items: center;
    gap: 3px;
}
.tree-controls button:hover {
    background: #e9ecef;
}

/* MOBILE SIDEBAR TOGGLE */
.mobile-toggle {
    display: none;
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 101;
    background: white;
    border: 1px solid #ddd;
    padding: 8px 10px;
    border-radius: 6px;
    color: #495057;
    font-size: 14px;
}

/* MODALS */
dialog { 
    border: none; 
    border-radius: 12px; 
    padding: 0; 
    box-shadow: 0 25px 100px rgba(0,0,0,0.4); 
    width: 90%;
    max-width: 500px;
    max-height: 90vh;
    overflow: hidden; 
    margin: auto;
    position: fixed;
    z-index: 9999;
}
dialog::backdrop { 
    background: rgba(0,0,0,0.5); 
    backdrop-filter: blur(3px);
}
.modal-header { 
    padding: 15px; 
    background: #f8f9fa; 
    border-bottom: 1px solid #ddd; 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
}
.modal-header h3 { 
    margin: 0; 
    font-size: 16px; 
    color: #2c3e50;
}
.close-btn { 
    background: none; 
    border: none; 
    font-size: 22px; 
    color: #6c757d; 
    cursor: pointer;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
}
.close-btn:hover { 
    background: #e9ecef; 
    color: #495057;
}
.modal-body { 
    padding: 15px; 
    max-height: calc(90vh - 130px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}
.modal-footer { 
    margin-top: 15px; 
    text-align: right; 
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding-top: 15px;
    border-top: 1px solid #eee;
}
.modal-tabs {
    display: flex;
    border-bottom: 1px solid #ddd;
    margin-bottom: 15px;
    overflow-x: auto;
}
.modal-tab {
    padding: 8px 15px;
    background: none;
    border: none;
    border-bottom: 3px solid transparent;
    color: #666;
    cursor: pointer;
    white-space: nowrap;
    font-size: 13px;
}
.modal-tab.active {
    border-bottom-color: #4a90e2;
    color: #4a90e2;
    font-weight: 600;
}

/* ALERTS */
.alert-box {
    background: #e7f3ff;
    border: 1px solid #b3d7ff;
    color: #0062cc;
    padding: 10px 12px;
    border-radius: 6px;
    margin-bottom: 15px;
    font-size: 13px;
    line-height: 1.4;
}
.alert-box.success {
    background: #d4edda;
    border-color: #c3e6cb;
    color: #155724;
}
.alert-box.warning {
    background: #fff3cd;
    border-color: #ffeaa7;
    color: #856404;
}
.alert-box.error {
    background: #f8d7da;
    border-color: #f5c6cb;
    color: #721c24;
}

/* TIMELINE */
.timeline {
    margin-top: 15px;
}
.timeline-item {
    padding: 12px;
    border-left: 3px solid #4a90e2;
    background: #f8f9fa;
    margin-bottom: 8px;
    border-radius: 0 6px 6px 0;
}
.timeline-date {
    font-size: 11px;
    color: #6c757d;
    margin-bottom: 4px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.timeline-content {
    font-size: 13px;
}

/* Make timeline action buttons visually match the Invitation/user action buttons
   so Edit and Delete icons appear identical in size, padding and spacing. */
.timeline .action-btn {
    /* Match Invitations: icon-only, no border/background */
    background: transparent;
    border: none;
    padding: 0;
    font-size: 12px;
    color: #333;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.timeline .action-btn:hover { background: transparent; }
.timeline .action-btn + .action-btn { margin-left: 4px; }
.timeline .action-btn .action-icon {
    width: 20px;
    height: 20px;
    font-size: 14px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}


/* BADGES FOR INVITATIONS/USERS */
.status-badge {
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 11px;
    font-weight: 500;
}
.badge-pending {
    background: #ffc107;
    color: #856404;
}
.badge-accepted {
    background: #28a745;
    color: white;
}
.badge-admin {
    background: #dc3545;
    color: white;
}
.badge-member {
    background: #6c757d;
    color: white;
}
.badge-active {
    background: #28a745;
    color: white;
}
.badge-inactive {
    background: #ffc107;
    color: #856404;
}
    
    /* Ensure badges inside user cards look compact and sit on the right like Invitations */
    .rel-item .badges-row {
        display: flex;
        gap: 6px;
        align-items: center;
        /* small vertical offset to visually match Invitations */
        margin-top: 2px;
        margin-left: 0;
        justify-content: flex-end; /* push badges to the right column */
    }

/* Make the delete/trash icon a consistent tappable square */
.action-icon {
    display: inline-flex;
    width: 36px;
    height: 36px;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    box-sizing: border-box;
    font-size: 14px;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}
.action-icon:hover {
    background: rgba(0,0,0,0.04);
}
.action-icon.danger-link,
.danger-link.action-icon {
    color: #a00;
}
.action-icon.danger-link:hover,
.danger-link.action-icon:hover {
    background: rgba(255,0,0,0.06);
}

/* Stack actions into a column on narrow screens for touch targets */
@media (max-width: 600px) {
    .user-actions .actions-row {
        flex-direction: column;
        align-items: stretch;
    }
    .user-actions .action-btn {
        width: 100%;
    }
    .user-actions .action-btn + .action-btn {
        margin-top: 8px;
    }
    /* Keep the icon square but align it with buttons when stacked */
    .action-icon {
        width: 40px;
        height: 40px;
        margin-left: 0;
    }
}

/* RESPONSIVE DESIGN */
@media (max-width: 1024px) {
    .sidebar { width: 340px; min-width: 260px; }
    .rel-item > div:last-child { flex: 0 0 160px; max-width: 180px; }
    .user-actions .action-btn { padding: 5px 6px; font-size: 12px; }
    }
    
    .profile-header {
    .sidebar { width: 300px; min-width: 240px; }
    .rel-item > div:last-child { flex: 0 0 140px; max-width: 160px; }
    .tab-link { padding: 10px 8px; min-width: 60px; }
    .avatar-img {
        width: 70px;
        height: 70px;
    }
    
    .name-display h2 {
        font-size: 1.2rem;
    }
    
    .tab-content {
        padding: 12px;
    }
}

@media (max-width: 768px) {
    /* MOBILE LAYOUT */
    .workspace {
        flex-direction: column;
    }
    
    .sidebar {
        width: 100%;
        max-width: 100%;
        /* Give the sidebar more vertical room on mobile so tabs/content are usable */
        height: 65vh;
        min-height: 360px;
        border-right: none;
        border-bottom: 1px solid #ddd;
        position: absolute;
        top: 60px;
        left: 0;
        right: 0;
        bottom: auto;
        z-index: 99;
        transform: translateX(-100%);
    }
    
    .sidebar.active {
        transform: translateX(0);
    }
    
    .tree-wrapper {
        /* Reduce tree area so sidebar has more space for content */
        height: 35vh;
        min-height: 200px;
    }
    
    /* MOBILE HEADER */
    header {
        padding: 0 10px;
    }
    
    .logo {
        font-size: 1rem;
    }
    
    .search-box {
        margin: 0 8px;
        max-width: 200px;
    }
    
    .search-box input {
        padding: 6px 30px 6px 10px;
        font-size: 13px;
    }
    
    .controls button {
        padding: 5px 8px;
        font-size: 11px;
    }
    
    .controls button span {
        display: none;
    }

    /* Make profile action buttons less aggressive on mobile while keeping touch targets */
    .profile-actions {
        gap: 8px;
        flex-wrap: wrap;
    }
    .profile-actions .primary-btn,
    .profile-actions .secondary-btn,
    .profile-actions .danger-link {
        padding: 8px 10px;
        font-size: 13px;
        min-width: auto;
        flex: 0 0 auto;
    }

    /* Ensure tabs/content use available vertical space */
    .sidebar .tabs { flex: 0 0 auto; }
    .sidebar .tab-content { flex: 1 1 auto; min-height: 0; overflow-y: auto; }
    
    /* MOBILE TOGGLE BUTTON */
    .mobile-toggle {
        display: block;
    }
    
    /* ADJUST TREE CONTROLS */
    .tree-controls {
        top: 5px;
        right: 5px;
        padding: 5px;
    }
    
    .tree-controls button {
        padding: 4px 6px;
        font-size: 10px;
    }
}

@media (max-width: 480px) {
    /* SMALL MOBILE */
    .login-box {
        padding: 20px;
    }
    
    .login-box h2 {
        font-size: 1.3rem;
    }
    
    .login-box input, .login-box textarea, .login-box select {
        padding: 12px;
        font-size: 15px;
    }
    
    .profile-header-top {
        flex-direction: column;
        gap: 10px;
    }
    
    .avatar-img {
        width: 60px;
        height: 60px;
        align-self: center;
    }
    
    .name-display h2 {
        text-align: center;
        font-size: 1.1rem;
    }
    
    .name-display .meta {
        text-align: center;
    }
    
    .profile-actions {
        flex-direction: column;
        gap: 6px;
        align-items: stretch;
    }

    /* Make action buttons compact but still touch-friendly */
    .primary-btn, .secondary-btn, .danger-link {
        min-width: auto;
        padding: 8px 10px;
        font-size: 13px;
        border-radius: 6px;
    }

    /* Reduce spacing inside form sections for better density */
    .tab-content { padding: 8px; }
    .form-section { margin-bottom: 12px; }
    label { margin-top: 8px; margin-bottom: 6px; }
    input, select, textarea { padding: 9px; font-size: 14px; }
    .group-header { padding-bottom: 6px; margin-bottom: 6px; }

    /* Ensure tab content is scrollable and uses remaining height */
    .sidebar { display: flex; flex-direction: column; }
    .sidebar .tabs { flex: 0 0 auto; }
    .sidebar .tab-content { flex: 1 1 auto; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; }
    
    .form-row {
        flex-direction: column;
        gap: 8px;
    }
    
    .form-group {
        min-width: 100%;
    }
    
    .tabs {
        justify-content: flex-start;
    }
    
    .tab-link {
        padding: 10px 12px;
        min-width: 60px;
        font-size: 12px;
    }
    
    dialog {
        width: 95%;
    }
    
    .modal-body {
        padding: 12px;
    }
}

/* TOUCH FRIENDLY */
@media (hover: none) and (pointer: coarse) {
    button, .rel-item, .tab-link {
        min-height: 44px;
    }
    
    input, select, textarea {
        font-size: 16px;
        padding: 12px;
    }
    
    .rel-item .actions {
        opacity: 1;
    }
    
    .add-mini {
        padding: 6px 12px;
        min-height: 32px;
    }
}

/* LOADING STATES */
.loading {
    position: relative;
    opacity: 0.7;
}
.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 30px;
    height: 30px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid #4a90e2;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: translate(-50%, -50%) rotate(0deg); }
    100% { transform: translate(-50%, -50%) rotate(360deg); }
}
i.fas.fa-trash.danger-link.action-icon {
  min-width: unset !important;
  flex-grow: 0 !important;
  width: auto !important;
  padding: 10px 10px !important; /* Adjusted to align with buttons */
}

.actions-row {
  flex-direction: row !important; /* For horizontal layout on mobile */
  flex-wrap: nowrap !important; /* Prevent wrapping on mobile */
}