html {
    scroll-behavior: smooth;
}

section>:first-child,
article>:first-child {
    /* Add margin to the titles of sections and articles to prevent the header from covering it when scrolling to them on mini-toc link click */
    scroll-margin-top: calc(var(--header-height) + 1.85rem);
}

.header-container {
    color: var(--pico-color);
    /* Prevent tabs or long content from making header wider than viewport */
    overflow-x: clip;
    margin-bottom: var(--header-margin-bottom);
    height: var(--header-height);
    position: sticky;
    z-index: 3;
    top: 0;
    width: 100%;
    left: 0;
    background: transparent;
    border-bottom: none;
}

.header {
    display: grid;
    grid-template-columns: minmax(120px, 1fr) minmax(0, 600px) minmax(120px, 1fr);
    align-items: center;
    gap: 2rem;
    margin: 1rem 2rem;

    .header-right {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        justify-self: flex-end;
        gap: 1.5rem;
        background: var(--elements-background);
        border-radius: 6px;
        border: none;
        padding: 0 1rem;
        height: 2.5rem;
    }
}

.header .logo-container {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    background-color: var(--main-background-color);
    width: fit-content;
    border-radius: 6px;
    height: 40px;
    padding-left: 1rem;
    padding-right: 1rem;
    margin-left: -1rem;
}

.brand-logo {
    flex-shrink: 0;
    min-width: 24px;
}

.brand-logo img {
    height: 28px;
    display: block;
}

.brand-logo.logo-default {
    display: block;
}

.brand-logo.logo-dark-theme {
    display: none;
}

.theme-toggle {
    margin: 0;
}

.theme-toggle__expand {
    width: 25px;
    height: 25px;
    fill: var(--pico-muted-color);
    stroke: var(--pico-muted-color);
}

@media (prefers-color-scheme: dark) {
    .brand-logo.logo-default {
        display: none;
    }

    .brand-logo.logo-dark-theme {
        display: block;
    }
}

[data-theme=dark] .brand-logo.logo-dark-theme {
    display: block;
}

[data-theme=light] .brand-logo.logo-default {
    display: block;
}

[data-theme=light] .brand-logo.logo-dark-theme {
    display: none;
}

[data-theme=dark] .brand-logo.logo-default {
    display: none;
}



/* New search input structure */
.search-container {
    position: relative;
    background: transparent;
    display: flex;
    align-items: center;

    @media print {
        display: none;
    }
}

/* Search toggle button (shown on mobile, hidden on desktop) */
.search-toggle-button {
    display: none;
    position: relative;
    right: 0;
    background: var(--elements-background);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-radius: 6px;
    border: none;
    color: var(--pico-muted-color, inherit);
    cursor: pointer;
    padding: 0.625rem;
    height: 2.5rem;
    width: 2.5rem;
}

.search-toggle-button:hover {
    background: rgba(127, 127, 127, 0.08);
}

/* Search input wrapper - hidden by default, expands on mobile */
.search-input-wrapper {
    display: flex;
    width: 100%;
    position: relative;
}

/* Close button in search wrapper */
.search-close-button {
    position: absolute;
    right: 0.4rem;
    top: 50%;
    transform: translateY(-50%);
    background: transparent;
    border: none;
    color: var(--pico-muted-color, inherit);
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 0.25rem;
    border-radius: 6px;
    z-index: 10;
}

.search-close-button:hover {
    background: rgba(127, 127, 127, 0.08);
}

input.search-input {
    display: flex;
    align-items: center;
    margin: 0;
    padding-right: 4rem;
    height: 2.5rem;
    background: var(--elements-background);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-radius: 6px;
    border: none;
    box-shadow: var(--pico-dropdown-box-shadow);
}

input.search-input::placeholder {
    color: var(--pico-muted-color, #6b7280);
    margin-top: 0;
}

.search-submit-button {
    position: absolute;
    right: 0.4rem;
    top: 50%;
    transform: translateY(-50%);
    background: transparent;
    border: none;
    color: var(--pico-muted-color, inherit);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.25rem;
    border-radius: 6px;
}

.search-submit-button:hover {
    background: rgba(127, 127, 127, 0.08);
}

/* Remove old input-field-based styling no longer used by header search */
.input-field {
    position: static;
}

.input-field input[type="text"] {
    padding-left: initial;
}

.input-field label {
    position: static;
    transform: none;
}

.header-dropdown {
    position: relative;
}

.header-dropdown-toggle {
    margin: 0;
    height: 2.5rem;
    padding: 0;
    display: flex;
    align-items: center;
    background: transparent;
    border: none;
    text-wrap: nowrap;
}

.menu-dropdown .header-dropdown-toggle svg {
    width: 1.8rem;
}

.header-dropdown-toggle .icon {
    width: 1.8rem;
    height: 1.8rem;
    background-color: currentColor;
}

.icon.lang-icon {
    mask: url("../images/globe.svg") no-repeat center / contain;
    -webkit-mask: url("../images/globe.svg") no-repeat center / contain;
}

.icon.sections-icon {
    mask: url("../images/sections.svg") no-repeat center / contain;
    -webkit-mask: url("../images/sections.svg") no-repeat center / contain;
}

.icon.user-icon {
    mask: url("../images/user.svg") no-repeat center / contain;
    -webkit-mask: url("../images/user.svg") no-repeat center / contain;
}

.icon.header-search-icon {
    mask: url("../images/search.svg") no-repeat center / contain;
    -webkit-mask: url("../images/search.svg") no-repeat center / contain;
}

.mobile-menu-toggle {
    display: none;
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0;
    margin: 0;
    height: 2.5rem;
    align-items: center;
    color: var(--text-default);
}

.mobile-menu-toggle .icon {
    width: 1.8rem;
    height: 1.8rem;
    background-color: var(--text-default);
}

.mobile-menu-toggle .icon.mobile-menu-icon {
    mask: url("../images/bars.svg") no-repeat center / contain;
    -webkit-mask: url("../images/bars.svg") no-repeat center / contain;
}

/* Hidden on desktop — shown via .mobile-open inside the mobile media query */
.mobile-menu {
    display: none;
}

.header-dropdown-toggle:not(.mobile-menu-toggle)>svg,
.header-dropdown-toggle:not(.mobile-menu-toggle)>span {
    opacity: 0.7;
}

.header-dropdown-toggle:not(.mobile-menu-toggle)>svg,
.header-dropdown-toggle:not(.mobile-menu-toggle)>span,
.header-right label {
    transition: all 0.5s ease;
}

.header-dropdown-toggle:not(.mobile-menu-toggle)>svg:hover,
.header-dropdown-toggle:not(.mobile-menu-toggle)>span:hover,
.header-right label:hover {
    filter: brightness(1.5);
}

.header-dropdown-toggle>span {
    display: flex;
}

.header-dropdown-content {
    display: none;
}

.header-dropdown:hover .header-dropdown-content {
    display: block;
    animation: headerDropdownSlideDown 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.header-dropdown-content {
    position: absolute;
    right: -1rem;
    overflow: auto;
    max-height: calc(100vh - var(--header-height));
    background: var(--elements-background);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-radius: 6px;
    border: none;
    width: max-content;
    opacity: 1;
    transform-origin: top;
    transition: opacity var(--pico-transition);
    padding: 0;
    -ms-overflow-style: none;
    scrollbar-width: none;

    &::-webkit-scrollbar {
        display: none;
    }
}

.header-dropdown-content li {
    list-style: none;
    margin-top: 8px;
    width: 100%;
    display: flex;
}

.header-dropdown-content a,
.header-dropdown-content span {
    padding: calc(var(--pico-form-element-spacing-vertical) * .5) var(--pico-form-element-spacing-horizontal);
    overflow: hidden;
    border-radius: 0;
    color: var(--pico-dropdown-color);
    text-decoration: none;
    text-overflow: ellipsis;
    width: 100%;
}

.header-dropdown-content a:hover,
.header-dropdown-content a[aria-current]:not([aria-current=false]) {
    background-color: var(--pico-dropdown-hover-background-color);
}

.header-search-dropdown {
    position: unset;

    .header-dropdown-content {
        right: 2rem;
        width: 600px;

        &:focus-within {
            display: block;
            animation: headerDropdownSlideDown 0.3s cubic-bezier(0.16, 1, 0.3, 1);
        }
    }
}

.header-link {
    text-wrap: nowrap;
}

#header-search-form {
    display: none;
}

#default-header-search-dropdown {
    display: none;
}

[config-searchtype="inline"] {
    #header-search-form {
        display: block;
    }
}

[config-searchtype="icon"] {
    #default-header-search-dropdown {
        display: block;
    }
}

@media (max-width: 1175px) {

    /* Adjust grid layout for tablets to prevent dropdown overlap with search */
    .header {
        grid-template-columns: minmax(80px, 1fr) minmax(0, 450px) minmax(100px, 1fr);
        gap: 1.5rem;
    }
}

/* Responsive header tweaks */
@media (max-width: 1000px) {
    .brand-logo {
        margin-top: 0;
    }

    .header-container {
        position: relative;
    }

    .header {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 0;
        margin-bottom: 0;
        margin-left: 1rem;
        margin-right: 1rem;
    }

    .header .logo-container {
        justify-content: flex-start;
        width: fit-content;
    }

    .mobile-menu-toggle {
        display: flex;
    }

    /* Hide the desktop header-right on mobile */
    .header-right-desktop {
        display: none !important;
    }

    /* Hide desktop search center on mobile */
    .header-center {
        display: none;
    }

    /* ═══════════════════════════════════════════
     * Mobile menu panel
     * ═══════════════════════════════════════════ */

    .mobile-menu {
        display: none;
        flex-direction: column;
        width: 100%;
        max-width: 30rem;
        position: absolute;
        top: 100%;
        right: 0;
        max-height: calc(100vh - var(--header-height, 4rem));
        overflow-y: auto;
        order: 10;
        background: var(--elements-background);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        border-top: 1px solid var(--pico-muted-border-color);
        border-left: 1px solid var(--pico-muted-border-color);
        border-bottom: 1px solid var(--pico-muted-border-color);
        border-bottom-left-radius: var(--pico-border-radius, 6px);
        box-shadow: var(--pico-dropdown-box-shadow);
        padding: 0.75rem 0;
        animation: headerDropdownSlideDown 0.3s cubic-bezier(0.16, 1, 0.3, 1);
        -webkit-overflow-scrolling: touch;
    }

    .mobile-menu.mobile-open {
        display: flex;
    }

    /* ─── Search (always visible, full width) ─── */

    .mobile-menu-search {
        padding: 0.5rem 0.75rem;
        width: 100%;
    }

    .mobile-menu-search .search-container {
        width: 100%;
    }

    .mobile-menu-search input.search-input {
        width: 100%;
    }

    /* ─── Menu items ─── */

    .mobile-menu-item {
        padding: 0.5rem 1rem 1rem 1rem;
    }

    /* ─── Click-to-open dropdowns (sections, language, auth) ─── */

    .mobile-menu-dropdown {
        position: relative;
    }

    .mobile-menu-dropdown-toggle {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        width: 100%;
        padding: 0 0.5rem;
        height: 40px;
        background: transparent;
        border: none;
        cursor: pointer;
        color: var(--text-default);
        font-size: 1rem;
        font-weight: normal;
    }

    /* Highlight toggle on hover or when expanded */
    .mobile-menu-item:hover .mobile-menu-dropdown-toggle .icon,
    .mobile-menu-item:hover .mobile-menu-dropdown-toggle .mobile-menu-label,
    .mobile-menu-item:hover .mobile-menu-dropdown-toggle .mobile-menu-chevron,
    .mobile-menu-dropdown-toggle:hover .icon,
    .mobile-menu-dropdown-toggle:hover .mobile-menu-label,
    .mobile-menu-dropdown-toggle:hover .mobile-menu-chevron,
    .mobile-menu-dropdown-toggle[aria-expanded="true"] .icon,
    .mobile-menu-dropdown-toggle[aria-expanded="true"] .mobile-menu-label,
    .mobile-menu-dropdown-toggle[aria-expanded="true"] .mobile-menu-chevron {
        opacity: 1;
    }

    .mobile-menu-dropdown-toggle .icon {
        width: 1.5rem;
        height: 1.5rem;
        flex-shrink: 0;
        background-color: var(--text-default);
        opacity: 0.7;
        transition: all 0.5s ease;
    }

    .mobile-menu-label {
        flex: 1;
        text-align: left;
        font-size: 1rem;
        font-weight: normal;
        color: var(--text-default);
        opacity: 0.7;
        transition: all 0.5s ease;
    }

    .mobile-menu-chevron {
        display: inline-block;
        width: 0.5rem;
        height: 0.5rem;
        border-right: 2px solid var(--pico-muted-color);
        border-bottom: 2px solid var(--pico-muted-color);
        transform: rotate(45deg);
        transition: transform 0.2s ease, filter 0.5s ease, border-color 0.5s ease;
        flex-shrink: 0;
        opacity: 0.7;
    }

    .mobile-menu-dropdown-toggle[aria-expanded="true"] .mobile-menu-chevron {
        transform: rotate(-135deg);
    }

    /* Dropdown content – hidden by default, shown on click via JS */
    .mobile-menu-dropdown-content {
        display: none;
        list-style: none;
        padding: 0 0 0 2.15rem;
        margin: 0;
    }

    .mobile-menu-dropdown-content li {
        list-style: none;
        margin: 0;
    }

    .mobile-menu-dropdown-content a {
        display: block;
        padding: 0.4rem 0.5rem;
        color: var(--text-default);
        text-decoration: none;
        border-radius: 4px;
    }

    .mobile-menu-dropdown-content a:hover,
    .mobile-menu-dropdown-content a[aria-current]:not([aria-current="false"]) {
        background-color: var(--pico-dropdown-hover-background-color);
    }

    /* ─── Footer row (auth + theme toggle) ─── */

    .mobile-menu-footer-row {
        display: flex;
        align-items: flex-start;
        padding-bottom: 0.3rem;
    }

    .mobile-menu-footer-row .mobile-menu-item {
        flex: 1;
        padding-bottom: 0;
        /* Align vertically with theme toggle */
    }

    .mobile-menu-footer-row .theme-toggle-container {
        flex: 0 0 auto;
        padding-right: 1.5rem;
        display: flex;
        align-items: center;
        height: 40px;
        /* Match menu item row height */
        margin-top: 0.5rem;
    }

    .mobile-menu-footer-row .theme-toggle {
        margin: 0;
    }

    /* ─── Auth link (logged-out state) ─── */

    .mobile-menu-login-link {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        padding: 0.5rem 0.25rem;
        color: var(--text-default);
        text-decoration: none;
        font-size: 1rem;
        font-weight: normal;
        height: 40px;
    }

    .mobile-menu-login-link:hover .icon,
    .mobile-menu-login-link:hover .mobile-menu-label {
        opacity: 1;
    }

    .mobile-menu-login-link .icon {
        width: 1.5rem;
        height: 1.5rem;
        flex-shrink: 0;
        background-color: var(--text-default);
        opacity: 0.7;
        transition: all 0.5s ease;
    }

    .mobile-menu-login-link .mobile-menu-label {
        opacity: 0.7;
        transition: all 0.5s ease;
    }

    @media print {
        .mobile-menu {
            display: none !important;
        }
    }
}

@keyframes headerDropdownSlideDown {
    from {
        opacity: 0;
        transform: translateY(-12px) scale(0.98);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}