/* Language selector mega-menu override */
.md-header__option .md-select {
    position: relative;
}

/* Override the default mkdocs i18n language dropdown */
.md-header__option .md-select__inner {
    position: absolute;
    top: 100%;
    right: 0;
    width: 800px;
    height: auto !important;
    max-height: none !important;
    background-color: var(--md-default-bg-color);
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.15);
    border-radius: 8px;
    z-index: 100;
    overflow-y: visible;
    padding: 1rem;

    /* Using block instead of table */
    display: block;

    /* Hidden by default, only shown on hover or focus-within */
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: all 0.2s ease-in-out;
}

/* The actual list that contains the items */
.md-header__option .md-select__inner .md-select__list {
    margin: 0;
    padding: 0;
    list-style: none;
    max-height: 80vh !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;

    /* Modern Grid Layout instead of buggy column-count */
    display: grid !important;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.5rem;
}

.md-header__option .md-select:hover .md-select__inner,
.md-header__option .md-select:focus-within .md-select__inner {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

/* Individual language items in the grid */
.md-select__item {
    display: flex;
    align-items: center;
    width: 100%;
    padding: 0.6rem 0.8rem !important;
    border-radius: 6px;
    text-align: left;
    transition: background-color 0.2s, color 0.2s;
    font-size: 0.85rem;
    box-sizing: border-box;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.md-select__item:hover {
    background-color: var(--md-accent-fg-color--transparent);
    color: var(--md-accent-fg-color);
}

/* Target Mobile Viewport to collapse to fewer columns and adapt sizing */
@media screen and (max-width: 900px) {
    .md-header__option .md-select__inner {
        width: 600px;
    }

    .md-header__option .md-select__inner .md-select__list {
        grid-template-columns: repeat(3, 1fr);
    }
}

@media screen and (max-width: 768px) {
    .md-header__option .md-select__inner {
        width: 400px;
        max-width: calc(100vw - 32px);
        /* Avoid screen edge overflow */
        right: -10px !important;
        left: auto !important;
        padding: 0.75rem;
    }

    .md-header__option .md-select__inner .md-select__list {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media screen and (max-width: 480px) {
    .md-header__option .md-select__inner {
        width: calc(100vw - 32px);
        right: -16px !important;
        /* Fine-tune for typical mobile margins to align box correctly */
        max-height: 85vh !important;
        /* Limit overall box height */
        overflow-y: auto;
    }

    .md-header__option .md-select__inner .md-select__list {
        /* Keep 2 columns to avoid endless scrolling, font is adjusted */
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 0.25rem;
    }

    .md-select__item {
        padding: 0.6rem 0.6rem !important;
        font-size: 0.8rem;
    }
}

/* RTL Support */
[dir="rtl"] .md-header__option .md-select__inner {
    right: auto !important;
    left: 0;
}

@media screen and (max-width: 768px) {
    [dir="rtl"] .md-header__option .md-select__inner {
        left: -10px !important;
    }
}

@media screen and (max-width: 480px) {
    [dir="rtl"] .md-header__option .md-select__inner {
        left: -16px !important;
    }
}

[dir="rtl"] .md-select__item {
    text-align: right;
}