:root {
    --mgd-toc-rail-w: 44px;
    --mgd-toc-panel-w: 320px;
    --mgd-toc-radius: 16px;
    --mgd-toc-shadow: 0 12px 34px rgba(0, 0, 0, 0.14);
    --mgd-toc-border: 1px solid rgba(0, 0, 0, 0.12);
    --mgd-toc-bg: #fff;
    --mgd-toc-text: #111;
    --mgd-toc-max-h: 72vh;
    --mgd-toc-peek: 16px;
    --mgd-toc-left: 24px;
    --mgd-toc-top: 120px;
    --mgd-toc-mobile-h: 54vh;
    --mgd-toc-mobile-peek: 48px;
}

.mgd-toc-anchor {
    scroll-margin-top: var(--mgd-toc-scroll-offset, 96px);
}

.mgd-toc {
    position: fixed;
    left: var(--mgd-toc-left);
    top: var(--mgd-toc-top);
    z-index: 99999;
    display: grid;
    grid-template-columns: var(--mgd-toc-panel-w) var(--mgd-toc-rail-w);
    align-items: stretch;
    max-height: var(--mgd-toc-max-h);
    height: auto;
    background: var(--mgd-toc-bg);
    color: var(--mgd-toc-text);
    font-size: 14px;
    border: var(--mgd-toc-border);
    border-radius: var(--mgd-toc-radius);
    overflow: hidden;
    box-shadow: var(--mgd-toc-shadow);
    transition: transform 0.32s cubic-bezier(0.2, 0.8, 0.2, 1);
    will-change: transform;
}

.mgd-toc__body {
    grid-column: 1;
    grid-row: 1;
    align-self: stretch;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.mgd-toc__nav {
    overflow: auto;
    padding: 14px 14px 16px;
    max-height: var(--mgd-toc-max-h);
    min-height: 0;
    scrollbar-width: thin;
}

.mgd-toc__header {
    grid-column: 2;
    grid-row: 1;
    align-self: stretch;
    min-height: 0;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: stretch;
    background: rgba(0, 0, 0, 0.02);
    border-left: 1px solid rgba(0, 0, 0, 0.1);
}

.mgd-toc__title {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px 0;
    font-weight: 700;
    letter-spacing: 0.2px;
    writing-mode: vertical-rl;
    transform: rotate(180deg);
    user-select: none;
}

.mgd-toc__toggle {
    appearance: none !important;
    border: 0;
    background: transparent;
    cursor: pointer;
    min-height: 100px;
    display: grid;
    place-items: center;
    border-radius: 0 0 15px 0 !important;
    padding: 12px 0 !important;
    border-top: 1px solid rgba(0, 0, 0, 0.2);
}

.mgd-toc__toggle:hover {
    background: rgba(0, 0, 0, 0.05);
}

.mgd-toc__toggle:focus {
    outline: 0 !important;
    box-shadow: none !important;
}

.mgd-toc__toggle svg {
    width: 18px;
    height: 18px;
    opacity: 0.85;
    transition: transform 0.25s ease, opacity 0.2s ease;
}

.mgd-toc__list {
    margin: 0;
    padding-left: 0;
}

.mgd-toc__item {
    margin: 7px 0;
    list-style: none;
}

.mgd-toc__item--level-3 {
    padding-left: 12px;
}

.mgd-toc__item--level-4 {
    padding-left: 24px;
}

.mgd-toc__item--level-5 {
    padding-left: 36px;
}

.mgd-toc__item--level-6 {
    padding-left: 48px;
}

.mgd-toc__link {
    text-decoration: none;
    display: inline-block;
    line-height: 1.25;
    border-radius: 10px;
    padding: 5px 8px;
    transition: background 0.2s ease;
}

.mgd-toc__link:hover {
    background: rgba(0, 0, 0, 0.06);
    text-decoration: none;
}

.mgd-toc.is-collapsed {
    transform: translateX(calc(-1 * (var(--mgd-toc-panel-w) + var(--mgd-toc-rail-w) - var(--mgd-toc-peek))));
}

.mgd-toc.is-collapsed .mgd-toc__toggle svg {
    transform: rotate(180deg);
    opacity: 0.95;
}

@media (max-width: 1024px) {
    .mgd-toc {
        left: 0;
        right: 0;
        top: auto;
        bottom: env(safe-area-inset-bottom, 0px);
        width: 100%;
        height: min(var(--mgd-toc-mobile-h), calc(100svh - 120px));
        max-height: min(var(--mgd-toc-mobile-h), calc(100svh - 120px));
        grid-template-columns: 1fr;
        grid-template-rows: var(--mgd-toc-mobile-peek) 1fr;
        border-radius: 16px 16px 0 0;
        transform: translateY(calc(var(--mgd-toc-mobile-h) - var(--mgd-toc-mobile-peek)));
    }

    .mgd-toc__header {
        grid-column: 1;
        grid-row: 1;
        flex-direction: row;
        justify-content: space-between;
        align-items: stretch;
        border-left: none;
        border-bottom: 1px solid rgba(0, 0, 0, 0.1);
        padding: 0;
        min-height: var(--mgd-toc-mobile-peek);
    }

    .mgd-toc__title {
        writing-mode: horizontal-tb;
        transform: none;
        padding: 0 12px;
    }

    .mgd-toc__toggle {
        border-top: none;
        padding: 6px 10px !important;
        border-radius: 999px !important;
        background: rgba(0, 0, 0, 0.05);
        min-width: 100px;
        min-height: 10px;
    }

    .mgd-toc__body {
        grid-column: 1;
        grid-row: 2;
        max-height: var(--mgd-toc-mobile-h);
    }

    .mgd-toc__nav {
        max-height: var(--mgd-toc-mobile-h);
    }

    .mgd-toc.is-collapsed {
        transform: translateY(calc(var(--mgd-toc-mobile-h) - var(--mgd-toc-mobile-peek)));
    }

    .mgd-toc.is-open {
        transform: translateY(0);
    }

    .mgd-toc__toggle svg {
        transform: rotate(-90deg);
    }

    .mgd-toc.is-open .mgd-toc__toggle svg {
        transform: rotate(90deg);
    }
}

@media (prefers-reduced-motion: reduce) {
    .mgd-toc {
        transition: none !important;
    }
}
