/* layout.css
 * ============================================
 * Grid extensions, spacing utilities, and layout patterns.
 * Contains main-content positioning, sidebar layout, and
 * responsive container adjustments.
 *
 * Last updated: 2025-12-25
 * See: docs/delivery/74-bootstrap-vanilla-css/74-4.md
 */

/* === ANCHOR SCROLL OFFSET === */
/* Offset scroll targets below fixed navbar for anchor navigation */
[id] {
    scroll-margin-top: calc(var(--navbar-height) + 1rem);
}

/* === CONTAINER OVERRIDES === */
/* Neutralize .row negative margins when no sidebar */
main.container-fluid > .row {
    --bs-gutter-x: 0;
}

/* Bug fix 038: Neutralize footer row gutters to prevent overflow */
footer.container-fluid .row {
    --bs-gutter-x: 0;
}

/* === MAIN CONTENT LAYOUT === */
.main-content {
    position: relative;
    left: 0 !important;
    margin-left: 0;
    width: 100%;
    min-height: calc(100vh - var(--navbar-height) - 80px);
    padding-top: calc(var(--navbar-height) + 14px);  /* Tracks navbar + breathing room */
    padding-bottom: 2rem;
    padding-left: 0;
    padding-right: 0;
    transition: none;
    /* Bug fix 038: Contain inline size to prevent child overflow from causing horizontal scroll.
     * Defense layer after removing overflow-x:hidden from html. */
    contain: inline-size;
    overflow-x: clip;
}

@media (min-width: 576px) {
    .main-content {
        padding-left: 0;
        padding-right: 0;
    }
}

/* === FOOTER === */
footer.container-fluid {
    margin-top: auto;
    background-color: var(--bs-light);
    padding: 1rem 0;
}

/* === NAVBAR LAYOUT === */
.navbar {
    transition: none !important;
    z-index: var(--z-index-navbar);
    flex-wrap: nowrap !important;

    & * {
        transition: none !important;
    }

    & .btn-link {
        --bs-btn-padding-y: 0.375rem;
        --bs-btn-padding-x: 1rem;
        --bs-btn-font-size: 0.875rem;
        border: none;
        outline: none;

        &:hover,
        &:focus,
        &:active {
            background-color: transparent;
            box-shadow: none;
            outline: none;
        }
    }

    & .btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        height: 38px;
        padding-top: 0;
        padding-bottom: 0;
        line-height: 1.5;
    }
}

.navbar-brand {
    margin-left: 1rem;
}

/* Mobile navbar adjustments */
@media (max-width: 767.98px) {
    .navbar-brand {
        margin-left: 0.5rem;
        font-size: 1rem;
        white-space: nowrap;
    }

    .mobile-nav-icons {
        gap: 0;
        flex-shrink: 1;
        flex-wrap: nowrap !important;
        min-width: 0;  /* Allow shrinking below content size */
        margin-right: 0.25rem;  /* Breathing room from edge */

        & .nav-link {
            padding: 0.375rem 0.5rem;
        }
    }

    .navbar .container-fluid {
        flex-wrap: nowrap !important;
    }
}

/* === CARD LAYOUT === */
.card {
    --bs-card-border-color: var(--bs-border-color);
    --bs-card-cap-bg: rgba(var(--bs-secondary-rgb), 0.03);
    transition: transform 0.2s ease-in-out;
    position: relative;
    z-index: var(--z-index-card);

    &:hover {
        transform: translateY(-5px);
    }

    & .stretched-link::after {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    }
}

/* === MODAL LAYOUT === */
.modal-content {
    border: 1px solid var(--bs-border-color);
    border-radius: var(--bs-border-radius);
}

.modal-footer {
    gap: 0.5rem;
    padding: 0.75rem;
}

/* === DROPDOWN LAYOUT === */
.dropdown-menu {
    --bs-dropdown-min-width: 240px;
    --bs-dropdown-padding-y: 0.5rem;
    --bs-dropdown-border-color: var(--bs-border-color);
    --bs-dropdown-border-radius: var(--bs-border-radius);
    --bs-dropdown-box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
}

.dropdown-item {
    --bs-dropdown-item-padding-y: 0.75rem;
    --bs-dropdown-item-padding-x: 1.25rem;
    display: flex;
    align-items: center;

    &:active {
        background-color: var(--bs-primary);
    }
}

.dropdown-header {
    --bs-dropdown-header-color: var(--bs-secondary);
    --bs-dropdown-header-padding-y: 0.5rem;
    --bs-dropdown-header-padding-x: 1.25rem;
    font-weight: 600;
}

/* === STICKY HEADER === */
/* Fix: Prevent navbar text wrapping in mid-desktop range instead of increasing height */
@media (min-width: 768px) and (max-width: 1050px) {
    .navbar-nav {
        flex-wrap: nowrap !important;  /* Prevent wrapping to second line */
    }
    .navbar-nav .nav-link {
        padding-left: 0.375rem;
        padding-right: 0.375rem;
        font-size: 0.875rem;  /* Slightly smaller text to fit */
        white-space: nowrap;
    }
}

.sticky-top {
    position: sticky !important;
    top: var(--sticky-top-offset) !important;
    z-index: var(--z-index-sticky);
    backdrop-filter: blur(10px);
    background-color: rgba(255, 255, 255, 0.95) !important;
    box-shadow: 0 -3px 0 0 white;
    padding-bottom: 1rem;
}

/* Cover gap when navbar height varies - use CSS var for consistency */
.sticky-top::before {
    content: '';
    position: absolute;
    top: calc(-1 * var(--navbar-height, 60px));
    left: 0;
    right: 0;
    height: var(--navbar-height, 60px);
    background: inherit;
}

@media (max-width: 767.98px) {
    .sticky-top {
        z-index: calc(var(--z-index-navbar) - 1);
    }
}

.sticky-top + .container {
    padding-top: 0.5rem;
}

#stickyHeader {
    --sticky-header-offset: 24px;
    background-color: white;
    border-bottom: 3px solid var(--bs-primary);
    margin-top: -7px;
    margin-left: calc(-1 * var(--sticky-header-offset));
    margin-right: calc(-1 * var(--sticky-header-offset));
    padding-left: var(--sticky-header-offset);
    padding-right: var(--sticky-header-offset);
    padding-top: 0.75rem;
    padding-bottom: 0.75rem;
    /* Bug fix 038: Prevent negative margins from causing horizontal overflow.
     * Clip content that extends beyond container bounds. */
    overflow-x: clip;

    & .btn-sm {
        padding-top: 0.15rem;
        padding-bottom: 0.15rem;
        font-size: 0.8rem;
    }

    & .sort-pill-toggle .btn {
        padding-top: 0.15rem;
        padding-bottom: 0.15rem;
    }
}

/* === SIDEBAR LAYOUT === */
.sidebar {
    position: fixed;
    top: var(--navbar-height);
    left: calc(-1 * var(--sidebar-width));
    height: calc(100vh - var(--navbar-height));
    width: var(--sidebar-width);
    z-index: var(--z-index-sidebar);
    background-color: var(--bs-body-bg);
    border-right: 1px solid var(--bs-border-color);
    padding: 1rem;
    transition: left 0.3s ease-in-out;
    overflow-y: auto;

    &.show {
        left: 0;
    }
}

/* Mobile: Narrower sidebar for better proportions */
@media (max-width: 767.98px) {
    .sidebar {
        --sidebar-width: 260px;
    }
}

.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: var(--z-index-overlay);

    &.show {
        display: block;
    }
}

/* === FLOATING ACTION BUTTONS === */
.floating-actions {
    position: fixed;
    right: 1rem;
    bottom: 1rem;
    z-index: var(--z-index-floating-button);
    display: flex;
    flex-direction: column;
    gap: 0.5rem;

    & .btn {
        width: 48px;
        height: 48px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: var(--bs-primary);
        color: white;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
        transition: transform 0.2s ease-in-out, background-color 0.2s ease-in-out;

        &:hover {
            transform: scale(1.1);
            background-color: var(--bs-primary-dark);
        }

        &:active {
            transform: scale(0.95);
        }
    }
}

#mobile-collection-floating-button,
#mobile-account-floating-button {
    position: fixed;
    bottom: 1rem;
    right: 1rem;
    z-index: var(--z-index-floating-button);
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background-color: var(--bs-primary);
    color: white;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    padding: 0;
    line-height: 1;

    &:hover {
        background-color: var(--bs-primary-dark);
    }
}

/* Position floating buttons above mini-console on mobile */
@media (max-width: 767.98px) {
    .floating-actions,
    #mobile-collection-floating-button,
    #mobile-account-floating-button {
        bottom: calc(50px + 1rem + env(safe-area-inset-bottom, 0px));
    }
}

/* === MOBILE ALPHABET SIDEBAR === */
.alphabet-sidebar {
    position: fixed;
    top: calc(var(--navbar-height) + 1rem);
    right: 0;
    height: calc(100vh - var(--navbar-height) - 2rem);
    width: 30px;
    background-color: rgba(255, 255, 255, 0.9);
    z-index: var(--z-index-sidebar);
    display: flex;
    flex-direction: column;
    padding: 0;
    box-shadow: -2px 0 5px rgba(0, 0, 0, 0.1);
}

/* === DESKTOP OVERRIDES === */
@media (min-width: 768px) {
    .alphabet-sidebar {
        display: none;
    }

    .has-sidebar .sidebar {
        position: fixed;
        top: var(--navbar-height);
        left: 0;
        height: calc(100vh - var(--navbar-height));
        width: var(--sidebar-width);
        z-index: var(--z-index-sidebar);
        background-color: var(--bs-body-bg);
        border-right: 1px solid var(--bs-border-color);
        padding: 1rem;
        overflow-y: auto;
    }

    .has-sidebar .main-content {
        margin-left: var(--sidebar-width);
        width: calc(100% - var(--sidebar-width));
        padding-left: 15px;
        padding-right: 15px;
        box-sizing: border-box;
    }

    .has-sidebar .row {
        margin-left: 0;
        margin-right: 0;
    }

    .sidebar-overlay,
    .floating-actions {
        display: none;
    }

    .has-sidebar footer .container {
        margin-left: var(--sidebar-width);
        width: calc(100% - var(--sidebar-width));
    }
}

/* === MOBILE LAYOUT OVERRIDES === */
@media (max-width: 767.98px) {
    /* Let content flow naturally on mobile - don't force viewport height */
    .main-content {
        min-height: auto;
        padding-bottom: 0.5rem; /* Tighter on mobile (was 2rem) */
    }
}

@media (max-width: 575.98px) {
    /* Make bg-light transparent on mobile */
    .container-fluid.bg-light {
        background-color: transparent !important;
    }

    /* Bug fix 038: Tighter mobile edge padding for cards (0.25rem from 0.5rem).
     * BEFORE: 0.5rem (~18px total with card padding)
     * AFTER: 0.25rem (~14px total with card padding)
     * Footer excluded - keeps standard padding for readability.
     * See: specs/038-mobile-style-refresh/spec.md FR-005 */
    .container-fluid:not(footer) {
        padding-left: 0.25rem !important;
        padding-right: 0.25rem !important;
    }

    /* Footer keeps standard padding on mobile */
    footer.container-fluid {
        padding-left: 0.5rem !important;
        padding-right: 0.5rem !important;
    }

    /* Bug fix 038: Restore inner container padding for footer readability */
    footer.container-fluid > .container {
        padding-left: 0.5rem !important;
        padding-right: 0.5rem !important;
    }

    .container-fluid.py-3 {
        padding-top: 0.5rem !important;
        padding-bottom: 0.5rem !important;
    }

    .container-fluid > .container {
        padding-left: 0 !important;
        padding-right: 0 !important;
        max-width: 100% !important;
    }

    /* Bug fix 038: Neutralize row gutters when container padding is zeroed.
     * Without this, .row negative margins (-12px) cause 4px overflow. */
    .container-fluid > .container .row {
        --bs-gutter-x: 0;
    }

    /* Bug fix 038: Eliminate negative margins on mobile to prevent overflow.
     * Desktop uses negative margins for full-bleed effect, mobile uses tight padding. */
    #stickyHeader {
        --sticky-header-offset: 0;
        margin-left: 0;
        margin-right: 0;
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }

    .sticky-top {
        top: var(--sticky-top-offset) !important;
    }

    .row.mb-4 {
        margin-bottom: 0.75rem !important;
    }

    h2.text-center.mb-3 {
        font-size: 1.25rem;
        margin-bottom: 0.5rem !important;
    }
}

/* === HERO SECTION LAYOUT === */
.display-4 {
    font-size: 1.8rem;
    line-height: 1.2;
    margin-bottom: 1rem;
}

.lead {
    font-size: 1.1rem;
}

/* === HEADER LAYOUT === */
.header-container {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: space-between;
    margin-bottom: 1rem;
    flex-wrap: wrap;
    position: relative;
    z-index: var(--z-index-header);
}

.header-title {
    margin: 0;
    font-size: 1.5rem;
    flex-shrink: 0;
}

/* === FILTER/SORT LAYOUT === */
.filter-sort-container-wrapper {
    flex-grow: 1;
    width: 100%;
    box-sizing: border-box;
}

.filter-sort-flex {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 1rem;
    margin-top: 1rem;
}

.filter-item {
    width: 100%;
}

@media (min-width: 768px) {
    .filter-sort-flex {
        flex-direction: row;
        align-items: center;
    }

    .filter-item {
        width: auto;
    }
}

/* === QUOTE LIST LAYOUT === */
.quote-list {
    display: flex;
    flex-direction: column;
}

/* === COLLECTION BUTTON LAYOUT === */
.collection-button-wrapper {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: 1rem;
}

/* === SORT PILL TOGGLE === */
.sort-pill-toggle {
    border-radius: 50rem;
    overflow: hidden;

    & .btn {
        border-radius: 0;
        padding: 0.25rem 0.5rem;

        &:first-child {
            border-top-left-radius: 50rem;
            border-bottom-left-radius: 50rem;
        }

        &:last-child {
            border-top-right-radius: 50rem;
            border-bottom-right-radius: 50rem;
        }
    }
}

/* === INFO TOGGLE CHEVRON === */
/* Rotates chevron based on aria-expanded state (originator/source detail pages) */
.info-toggle-icon {
    text-decoration: none;
    padding: 0;
    line-height: 1;
    display: flex;
    align-items: flex-start;
    margin-top: 0.2rem;

    &:hover {
        text-decoration: none;
    }
}

.info-chevron {
    display: inline-block;
    transition: transform 0.35s ease;
    font-size: 1.1rem;
    font-weight: bold;
}

/* Chevron points right when collapsed, down when expanded */
.info-toggle-icon[aria-expanded="false"] .info-chevron {
    transform: rotate(-90deg);
}

.info-toggle-icon[aria-expanded="true"] .info-chevron {
    transform: rotate(0deg);
}

/* Larger chevron on mobile for better touch targets */
@media (max-width: 768px) {
    .info-chevron {
        font-size: 1.3rem;
    }
}

/* === TOUCH DEVICE OPTIMIZATIONS === */
@media (hover: none) {
    .sidebar {
        -webkit-overflow-scrolling: touch;
    }
}
