/**
 * App Layout System
 *
 * This file controls the overall layout structure for the app pages.
 * It uses a flexbox layout to ensure:
 * 1. Fixed header menu at top (never overlaps content)
 * 2. Content area grows to fill available space (scrollable)
 * 3. Footer stays at bottom of content (doesn't ride up with large gaps)
 *
 * Layout Structure:
 * - body: 100vh height, flexbox column
 *   ├── #app-menu-desktop/mobile: Fixed header (flex-shrink: 0)
 *   └── #app-container: Main content area (flex-grow: 1, scrollable)
 *       ├── .app-title: Page title (flex-shrink: 0)
 *       ├── .app-content: Content wrapper (flex-grow: 1)
 *       │   └── .app-content-inner: Actual content
 *       └── .app-bottom-menu: Action buttons (flex-shrink: 0)
 */

/* ===================================================================
   CSS VARIABLES
   =================================================================== */

:root {
    /* App menu height - used for menu height and content padding calculations */
    --app-menu-height: 40px; /* Mobile default */
    --app-menu-height-tablet: 60px; /* Tablet and larger */

    /* WordPress admin bar heights */
    --wp-admin-bar-height-desktop: 32px;
    --wp-admin-bar-height-mobile: 46px;
}

/* ===================================================================
   BASE BODY & WRAPPER SETUP
   =================================================================== */

html,
body {
    /* Full viewport height */
    height: 100vh;
    /* Prevent any scrolling on body - only #app-container scrolls */
    overflow: hidden;
    margin: 0;
    padding: 0;
}

/* Adjust body height when WordPress admin bar is present */
body.admin-bar {
    /* Reduce height by admin bar to prevent content being cut off at bottom */
    height: calc(100vh - var(--wp-admin-bar-height-desktop));
}

@media (max-width: 782px) {
    body.admin-bar {
        /* Mobile admin bar is taller */
        height: calc(100vh - var(--wp-admin-bar-height-mobile));
    }
}

/* Single event public pages - allow natural body scrolling */
body.single-event {
    overflow: auto;
}

/* Bootstrap wrappers need to fill height */
body > .container-fluid,
body > .container-fluid > .row,
body > .container-fluid > .row > main {
    height: 100%;
    margin: 0;
    padding: 0;
}

/* ===================================================================
   SIDEBAR NAVIGATION
   =================================================================== */

.app-sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: var(--app-sidebar-width);
    background-color: var(--bg-white);
    border-right: 1px solid var(--border-color-alt);
    display: flex;
    flex-direction: column;
    z-index: 1000;
    overflow-y: auto;
    overflow-x: hidden;
}

/* Adjust sidebar for admin bar */
body.admin-bar .app-sidebar {
    top: var(--wp-admin-bar-height-desktop);
}

@media (max-width: 782px) {
    body.admin-bar .app-sidebar {
        top: var(--wp-admin-bar-height-mobile);
    }
}

.sidebar-section {
    padding: 1rem;
}

.sidebar-section.sidebar-nav {
    padding-bottom: 0;
}

.sidebar-section.sidebar-media {
    padding-top: 0;
    padding-bottom: 0;
}

.sidebar-top {
    padding-top: 16px;
    border-bottom: 1px solid var(--border-color-alt);
}

.workspace-info {
    margin-bottom: 0;
    position: relative;
}

.workspace-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 6px;
    transition: background-color 0.15s ease;
}

.workspace-header:hover {
    background-color: var(--bg-light);
}

.workspace-header.dropdown-toggle::after {
    display: none; /* Hide Bootstrap's default dropdown arrow */
}

.workspace-avatar {
    flex-shrink: 0;
}

.workspace-details {
    flex: 1;
    min-width: 0;
    overflow: hidden;
}

.workspace-name {
    font-weight: 500;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    cursor: pointer;
    line-height: 1.4;
    gap: 0.25rem;
}

.workspace-name > span {
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.workspace-name i {
    font-size: 0.7rem;
    color: var(--bs-primary);
    flex-shrink: 0;
}

.workspace-badge {
    display: inline-block;
    padding: 0.125rem 0.5rem;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-muted);
    background-color: var(--bs-gray-200);
    border-radius: 12px;
    margin-top: 0.25rem;
    white-space: nowrap;
}

/* Tier-specific badge colors */
.workspace-badge--free {
    color: var(--text-muted);
    background-color: var(--bs-gray-200);
}

.workspace-badge--trial {
    color: var(--bs-info-text-emphasis, #0dcaf0);
    background-color: rgba(var(--bs-info-rgb), 0.1);
}

.workspace-badge--paid {
    color: var(--bs-success-text-emphasis, #198754);
    background-color: rgba(var(--bs-success-rgb), 0.1);
}

.workspace-badge--lapsed {
    color: var(--bs-warning-text-emphasis, #ffc107);
    background-color: rgba(var(--bs-warning-rgb), 0.1);
}

.workspace-badge--admin {
    color: var(--blue-primary);
    background-color: rgba(var(--blue-primary-rgb), 0.1);
}

.sidebar-nav {
    padding-top: 0.5rem;
}

.sidebar-section-header {
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    color: var(--text-muted);
    letter-spacing: 0.05em;
    margin-bottom: 0.75rem;
    padding: 0 0.75rem;
}

.sidebar-nav-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.sidebar-nav-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.625rem 0.75rem;
    color: var(--text-body);
    text-decoration: none;
    border-radius: 6px;
    font-size: 0.9rem;
    transition: background-color 0.15s ease;
    margin-bottom: 0.25rem;
}

.sidebar-nav-item:hover {
    background-color: var(--bs-gray-200);
}

.sidebar-nav-item.active {
    background-color: var(--blue-lighter);
    color: var(--blue-text);
    font-weight: 500;
}

.sidebar-nav-item.active i,
.sidebar-nav-item i {
    width: 20px;
    text-align: center;
    font-size: 1rem;
    color: var(--bs-primary);
}

.workspace-dropdown {
    margin-top: 0.5rem;
    border: 1px solid var(--border-color-alt);
    border-radius: 6px;
    box-shadow: 0 2px 8px rgba(var(--bs-black-rgb), 0.1);
    padding: 0.25rem 0;
    min-width: 200px;
}

.workspace-dropdown .dropdown-item {
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
}

.workspace-dropdown .dropdown-item:hover {
    background-color: var(--bg-light);
}

.workspace-dropdown .dropdown-item.active {
    background-color: var(--blue-lighter);
    color: var(--blue-text);
}

.workspace-dropdown .dropdown-item i {
    color: var(--bs-primary);
}

.sidebar-media {
    margin-top: 0;
    margin-bottom: 2rem;
    border-top: none;
}

.sidebar-bottom {
    border-top: 1px solid var(--border-color-alt);
    padding-top: 1rem;
    padding-bottom: 1rem;
    margin-top: auto;
}

/* ===================================================================
   MAIN CONTENT AREA
   =================================================================== */

#app-container {
    /* Fill parent height */
    height: 100%;
    /* Flexbox column for internal structure */
    display: flex;
    flex-direction: column;
    /* Allow internal scrolling */
    overflow-y: auto;
    overflow-x: hidden;
    /* Margin left to account for sidebar */
    margin-left: var(--app-sidebar-width);
    /* No padding by default - will be added responsively based on screen size */
    padding-left: 0;
    padding-right: 0;
    padding: 16px;
}

/* ===================================================================
   APP HEADER (Page Title Area)
   Note: This is just the title area, not the navigation menu
   =================================================================== */

.app-title {
    /* Don't shrink */
    flex-shrink: 0;
    /* Spacing - top for itself, bottom to push content down */

    /* Center content */
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
}

.app-title h2,
.app-title h5 {
    margin: 0;
    text-align: center;
}

.app-title h2 {
    font-size: 1.9rem;
}

.app-title h5 {
    font-size: 1.25rem;
}

/* ===================================================================
   APP CONTENT (Scrollable Content Area)
   =================================================================== */

.app-content {
    /* Grow to fill available space */
    flex: 1;
    /* Allow scrolling if content is long */
    overflow-y: auto;
    overflow-x: hidden;
    /* Center content */
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
}

.app-content-inner {
    /* Constrain width for readability */
    width: 100%;
    max-width: 740px;
    /* Allow content to grow */
    flex: 1;
}

/* Narrower max-width for form pages (event/album/life-story editors) */
.app-content-inner.form-width {
    max-width: 640px;
}

/* ===================================================================
   APP BOTTOM MENU (Action Buttons Footer)
   =================================================================== */

.app-bottom-menu {
    /* Don't shrink */
    flex-shrink: 0;
    /* Spacing - top padding pushes content up, bottom padding for menu itself */
    /* Use safe-area-inset-bottom to account for device home indicators (iPhone, etc.) */
    padding: 10px 15px;
    /* Visual separation */
    box-shadow: 0 -1px 10px rgba(var(--bs-black-rgb), 0.1);
    background-color: var(--bg-white);
    /* Rounded corners */
    border-radius: 20px;
    /* Layout */
    display: flex;
    justify-content: center;
    z-index: 100;
    /* Normal flow on mobile/tablet - sticky only on desktop */
}

/* Lower z-index when modal or offcanvas is open so their buttons are accessible */
body.modal-open .app-bottom-menu,
body.offcanvas-open .app-bottom-menu {
    z-index: 10;
}

.app-bottom-menu-inner {
    /* Fixed width - only shrinks on narrow mobile screens via parent constraints */
    width: 580px;
    max-width: 100%; /* Allow shrinking on screens narrower than this */
    display: flex;
    justify-content: center;
    gap: 10px;
}

/* ===================================================================
   RESPONSIVE ADJUSTMENTS
   =================================================================== */

/* Mobile - hide sidebar, show hamburger menu */
@media (max-width: 767.98px) {
    .app-sidebar {
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        z-index: 1002;
        box-shadow: 2px 0 8px rgba(0, 0, 0, 0.15);
    }

    .app-sidebar.open {
        transform: translateX(0);
    }

    #app-container {
        margin-left: 0;
        /* Add padding on mobile since sidebar is collapsed - more room available */
        padding-left: 16px;
        padding-right: 16px;
        /* Reserve top space for hamburger menu (left) and future upgrade banner (right) */
        padding-top: 16px;
    }

    /* Mobile menu toggle button */
    .mobile-menu-toggle {
        position: fixed;
        top: 1rem;
        left: 1rem;
        z-index: 1001;
        background: var(--bg-white);
        border: 1px solid var(--border-color);
        border-radius: 6px;
        padding: 0.5rem 0.75rem;
        box-shadow: 0 2px 4px rgba(var(--bs-black-rgb), 0.1);
        cursor: pointer;
    }

    .mobile-menu-toggle i {
        color: var(--bs-primary);
    }

    body.admin-bar .mobile-menu-toggle {
        top: calc(var(--wp-admin-bar-height-mobile) + 1rem);
    }

    /* Sidebar backdrop */
    .sidebar-backdrop {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: var(--bg-overlay-light);
        z-index: 1001;
        opacity: 0;
        visibility: hidden;
        transition:
            opacity 0.3s ease,
            visibility 0.3s ease;
    }

    .sidebar-backdrop.show {
        opacity: 1;
        visibility: visible;
    }
}

/* Tablet */
@media (min-width: 768px) and (max-width: 991.98px) {
    .app-content-inner {
        max-width: 700px;
    }
}

/* Desktop only - make bottom menu sticky */
@media (min-width: 992px) {
    .app-bottom-menu {
        position: sticky;
        bottom: 0;
    }
}

/* Touch Device Floating Bar - applies to any touch device (phones, tablets, iPads) regardless of size */
@media (pointer: coarse) and (hover: none) {
    /* Override padding for touch devices with floating menu */
    .app-content {
        /* Default padding for floating menu - accounts for app bottom menu + browser chrome */
        padding-bottom: calc(105px + env(safe-area-inset-bottom));
    }

    /* Reduced padding for pages without app bottom menu - only accounts for browser chrome */
    .app-content.no-app-bottom-menu {
        /* Half the padding since we only need browser chrome buffer, not our own menu */
        padding-bottom: calc(52px + env(safe-area-inset-bottom));
    }

    .app-bottom-menu {
        /* Fixed positioning - floating bar that stays above browser chrome */
        position: fixed;
        /* Use safe-area-inset-bottom (for device home indicators) + buffer for browser UI */
        /* Browser bars typically hide on scroll, so 40px buffer is usually enough */
        bottom: calc(5px + env(safe-area-inset-bottom));
        /* Mobile default: centered on viewport with 5% margins */
        left: 5%;
        right: 5%;
        max-width: 580px;
        margin: 0 auto;

        /* Enhanced shadow for floating effect */
        box-shadow:
            0 4px 24px rgba(var(--bs-black-rgb), 0.2),
            0 2px 8px rgba(var(--bs-black-rgb), 0.1);

        /* Semi-transparent background with backdrop blur for modern floating effect */
        background-color: rgba(var(--bs-white-rgb), 0.85);
        backdrop-filter: blur(15px);
        -webkit-backdrop-filter: blur(15px);

        /* Higher z-index to stay above content */
        z-index: 1020;
    }

    /* Tablet+ with sidebar visible: offset left to account for sidebar */
    @media (min-width: 768px) {
        .app-bottom-menu {
            /* Offset left by sidebar width + 5% margin of remaining workspace */
            left: calc(var(--app-sidebar-width) + 5%);
            /* Right margin stays at 5% of viewport */
            right: 5%;
        }
    }

    /* Prevent button wrapping on small screens */
    .app-bottom-menu-inner {
        gap: 6px;
        flex-wrap: nowrap;
        overflow-x: visible;
        scrollbar-width: none; /* Firefox */
    }

    .app-bottom-menu-inner::-webkit-scrollbar {
        display: none; /* Chrome/Safari */
    }

    .action-buttons {
        overflow-x: visible;
    }
}

/* Tablet and mobile - shrink button text */
@media (max-width: 991.98px) {
    .app-bottom-menu .btn-outline-dark {
        font-size: 0.8rem;
    }
}

/* ===================================================================
   BREADCRUMB NAVIGATION
   =================================================================== */

.breadcrumb-nav {
    width: 100%;
    font-size: 0.9rem;
    padding: 0.75rem 0.5rem;
}

.breadcrumb-nav .breadcrumb {
    background: transparent;
    padding: 0;
    margin: 0;
    justify-content: flex-start;
}

/* .breadcrumb-item uses Bootstrap .d-flex .align-items-center in template */

.breadcrumb-nav .breadcrumb-item + .breadcrumb-item::before {
    content: "/";
    color: var(--text-muted);
    font-size: 0.9rem;
    margin: 0 0.25rem;
}

.breadcrumb-nav .breadcrumb-link {
    color: var(--text-dark);
    text-decoration: underline;
    transition: color 0.15s ease-in-out;
}

.breadcrumb-nav .breadcrumb-link:hover {
    color: var(--text-light);
    text-decoration: underline;
}

.breadcrumb-nav .breadcrumb-item.active .breadcrumb-label {
    color: var(--text-muted);
    font-weight: 500;
}

.breadcrumb-nav .breadcrumb-label {
    max-width: 20ch;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Life story switcher popover styling */
.life-story-switcher {
    cursor: pointer;
}

.life-story-popover {
    min-width: 200px;
    max-width: 300px;
}

.life-story-popover a {
    display: block;
    padding: 0.5rem 0.75rem;
    text-decoration: none;
    border-radius: 0.25rem;
    transition: background-color 0.15s ease-in-out;
}

.life-story-popover a:hover {
    background-color: var(--bg-light);
}

.life-story-popover a.text-primary {
    background-color: var(--blue-lighter);
}

/* Mobile breadcrumb adjustments */
@media (max-width: 767.98px) {
    .breadcrumb-nav {
        font-size: 0.8rem;
        padding: 0.5rem 0.25rem;
    }

    .breadcrumb-nav .breadcrumb-label {
        max-width: 15ch;
    }

    .breadcrumb-nav .breadcrumb-item + .breadcrumb-item::before {
        margin: 0;
        font-size: 0.9rem;
    }
}

/* ===================================================================
   UTILITY CLASSES
   =================================================================== */

/* Admin bar adjustments for media edit modal */
body.admin-bar #mediaEditModal .modal-body {
    padding-bottom: var(--wp-admin-bar-height-desktop) !important;
}

@media (max-width: 782px) {
    body.admin-bar #mediaEditModal .modal-body {
        padding-bottom: var(--wp-admin-bar-height-mobile) !important;
    }
}
