/**
 * Ahago Table of Contents — frontend styles.
 * Architecture: inline card (in content flow) + floating button + shared popover.
 *
 * Rules:
 *  - The root never sets transform/filter/contain, so the fixed FAB and
 *    popover stay anchored to the viewport.
 *  - The root never sets max-width, inline-size or margin-inline, so it keeps
 *    whatever column width the theme gives children of the content area.
 *  - Styles are NOT in a cascade layer: layered rules lose to every unlayered
 *    theme rule regardless of specificity, so a layered reset cannot stop
 *    theme bleed (e.g. `button { min-height; margin }`, `ol li { margin }`).
 *    Component rules are nested under .ahago-toc (specificity 0,2,0), which
 *    beats typical theme selectors such as `.entry-content h2` (0,1,1) and
 *    `.entry-content ol li` (0,1,2).
 *  - FAB positioning uses --ahago-toc-fab-shift-x/y; the show/hide transition
 *    only adds an entrance offset on top, so centered positions stay centered.
 */

.ahago-toc {
    /* Deliberately NOT `all: revert`: themes size every direct child of the
       content area (Blocksy/block themes: max-width + margin-inline: auto
       on `.entry-content > *`). The TOC must inherit that column width, so
       only the properties that would visibly break the card are reset here,
       and max-width / inline-size / margin-inline are never touched. */
    display: block;
    padding: 0;
    border: 0;
    background: none;
    box-shadow: none;
    float: none;

    --ahago-toc-surface: #ffffff;
    --ahago-toc-border: #e3e6ee;
    --ahago-toc-text: #1f2430;
    --ahago-toc-link: #374151;
    --ahago-toc-muted: #4b5563;
    --ahago-toc-accent: #2f6bff;
    --ahago-toc-accent-track: color-mix(in srgb, var(--ahago-toc-accent) 16%, transparent);
    /* Accent mixed toward the text color keeps AA contrast on light tints. */
    --ahago-toc-accent-text: color-mix(in srgb, var(--ahago-toc-accent) 78%, var(--ahago-toc-text));
    --ahago-toc-accent-soft: color-mix(in srgb, var(--ahago-toc-accent) 10%, transparent);
    --ahago-toc-row-hover: color-mix(in srgb, var(--ahago-toc-text) 6%, transparent);
    --ahago-toc-radius-lg: 1.25rem;
    --ahago-toc-shadow: 0 1px 2px rgba(17, 24, 39, 0.04), 0 12px 32px -12px rgba(17, 24, 39, 0.22);
    --ahago-toc-pad: clamp(0.875rem, 1.8vw, 1.125rem);
    --ahago-toc-gap: clamp(0.75rem, 3vw, 1.25rem);
    --ahago-toc-progress: 0;
    --ahago-toc-hover-top: 0;
    --ahago-toc-hover-height: 0;
    --ahago-toc-fab-size: 3.25rem;
    --ahago-toc-fab-shift-x: 0%;
    --ahago-toc-fab-shift-y: 0%;
    --ahago-toc-fab-enter: 0.5rem;

    position: relative;
    margin-block: 1.5rem;
    color: var(--ahago-toc-text);
    font-size: 0.9375rem;
    line-height: 1.5;
    text-align: start;

    &,
    & *,
    & *::before,
    & *::after {
        box-sizing: border-box;
    }

    & :focus-visible {
        outline: 2px solid var(--ahago-toc-accent);
        outline-offset: 2px;
        border-radius: 0.375rem;
    }

    /* ---------- Local reset (beats typical theme element rules) ---------- */

    & .ahago-toc__list,
    & .ahago-toc__item {
        margin: 0;
        padding: 0;
        border: 0;
        list-style: none;
        background: none;
    }

    & .ahago-toc__item::before,
    & .ahago-toc__item::marker {
        content: none;
    }

    & .ahago-toc__heading {
        margin: 0;
        padding: 0;
        border: 0;
        background: none;
        color: inherit;
        font-family: inherit;
        font-size: clamp(1rem, 1.4vw, 1.0625rem);
        font-weight: 650;
        line-height: 1.3;
        letter-spacing: -0.01em;
        text-transform: none;
    }

    & .ahago-toc__heading::before,
    & .ahago-toc__heading::after {
        content: none;
    }

    & .ahago-toc__toggle,
    & .ahago-toc__more,
    & .ahago-toc__close,
    & .ahago-toc__fab {
        margin: 0;
        padding: 0;
        min-inline-size: 0;
        min-block-size: 0;
        border: 0;
        background: none;
        box-shadow: none;
        color: inherit;
        font: inherit;
        line-height: inherit;
        letter-spacing: normal;
        text-transform: none;
        text-shadow: none;
    }

    /* ---------- Inline card ---------- */

    & .ahago-toc__card--inline {
        position: relative;
        border: 1px solid var(--ahago-toc-border);
        border-radius: var(--ahago-toc-radius-lg);
        background-color: var(--ahago-toc-surface);
        overflow: hidden;
        animation: ahago-toc-enter 280ms ease-out both;
    }

    & .ahago-toc__progress {
        position: absolute;
        inset-block-start: 0;
        inset-inline-start: 0;
        inline-size: 0.1875rem;
        block-size: 100%;
        border-radius: 999rem;
        background-color: var(--ahago-toc-accent);
        opacity: 0;
        transform-origin: top;
        scale: 1 var(--ahago-toc-progress);
        transition: scale 140ms linear, opacity 140ms linear;
    }

    &.ahago-toc--has-progress .ahago-toc__progress {
        opacity: 1;
    }

    /* ---------- Shared head (card + popover) ---------- */

    & .ahago-toc__head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 0.75rem;
        padding-block: var(--ahago-toc-pad) 0.5rem;
        padding-inline: var(--ahago-toc-pad);
    }

    & .ahago-toc__toggle {
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        font-weight: inherit;
        text-align: start;
        cursor: pointer;
    }

    & .ahago-toc__chevron {
        inline-size: 0.5rem;
        block-size: 0.5rem;
        border-inline-end: 2px solid currentColor;
        border-block-end: 2px solid currentColor;
        rotate: 45deg;
        translate: 0 -0.0625rem;
        transition: rotate 200ms ease;
    }

    &.is-collapsed .ahago-toc__chevron {
        rotate: -135deg;
        translate: 0 0.0625rem;
    }

    /* ---------- Body (collapsible) ---------- */

    & .ahago-toc__body {
        display: grid;
        grid-template-rows: 1fr;
        transition: grid-template-rows 240ms ease;
    }

    &.is-collapsed .ahago-toc__body {
        grid-template-rows: 0fr;
    }

    & .ahago-toc__body-inner {
        min-block-size: 0;
        overflow: hidden;
        padding-block: 0.25rem var(--ahago-toc-pad);
        padding-inline: var(--ahago-toc-pad);
    }

    /* ---------- List ---------- */

    & .ahago-toc__list {
        position: relative;
        display: flex;
        flex-direction: column;
        gap: 0.125rem;
    }

    & .ahago-toc__card--inline .ahago-toc__list::before {
        content: "";
        position: absolute;
        inset-block-start: var(--ahago-toc-hover-top);
        inset-inline-start: -0.5rem;
        inline-size: 0.1875rem;
        block-size: var(--ahago-toc-hover-height);
        border-radius: 999rem;
        background-color: var(--ahago-toc-accent);
        opacity: 0;
        transition: inset-block-start 160ms ease, block-size 160ms ease, opacity 160ms ease;
    }

    &.has-hover-rail .ahago-toc__card--inline .ahago-toc__list::before {
        opacity: 0.7;
    }

    & .ahago-toc__item--level-3 {
        padding-inline-start: 1rem;
    }

    & .ahago-toc__item--level-4 {
        padding-inline-start: 2rem;
    }

    & .ahago-toc__link {
        display: inline-flex;
        align-items: baseline;
        gap: 0.375rem;
        max-inline-size: 100%;
        padding-block: 0.25rem;
        border: 0;
        border-radius: 0.375rem;
        box-shadow: none;
        background: none;
        color: var(--ahago-toc-link);
        font-weight: 400;
        text-decoration: none;
        overflow-wrap: anywhere;
        transition: color 160ms ease, translate 160ms ease;

        &:hover,
        &:focus-visible {
            color: var(--ahago-toc-accent);
            text-decoration: none;
            translate: 0.2rem 0;
        }

        &.is-active {
            color: var(--ahago-toc-text);
            font-weight: 600;
        }
    }

    /* ---------- Floating button ---------- */

    & .ahago-toc__fab {
        position: fixed;
        z-index: 2147483001;
        inline-size: var(--ahago-toc-fab-size);
        block-size: var(--ahago-toc-fab-size);
        display: grid;
        place-items: center;
        border-radius: 50%;
        background-color: var(--ahago-toc-surface);
        color: var(--ahago-toc-text);
        box-shadow: var(--ahago-toc-shadow);
        opacity: 0;
        visibility: hidden;
        scale: 0.9;
        translate: var(--ahago-toc-fab-shift-x) calc(var(--ahago-toc-fab-shift-y) + var(--ahago-toc-fab-enter));
        transition: opacity 200ms ease, scale 200ms ease, translate 200ms ease, visibility 0s linear 200ms;
        cursor: pointer;

        &:focus-visible {
            outline-offset: 3px;
            border-radius: 50%;
        }
    }

    &.is-fab-visible .ahago-toc__fab {
        --ahago-toc-fab-enter: 0rem;

        opacity: 1;
        visibility: visible;
        scale: 1;
        transition: opacity 200ms ease, scale 200ms ease, translate 200ms ease;
    }

    &.is-fab-idle .ahago-toc__fab {
        opacity: 0.82;

        &:hover,
        &:focus-visible {
            opacity: 1;
        }
    }

    /* Conic progress ring around the icon. */
    & .ahago-toc__fab-ring {
        position: absolute;
        inset: 0;
        border-radius: 50%;
        padding: 0.1875rem;
        background: conic-gradient(
            var(--ahago-toc-accent) calc(var(--ahago-toc-progress) * 360deg),
            var(--ahago-toc-accent-track) 0
        );
        mask:
            linear-gradient(#000 0 0) content-box exclude,
            linear-gradient(#000 0 0);
    }

    & .ahago-toc__fab-icon {
        position: relative;
        inline-size: 1.125rem;
        block-size: 0.875rem;
        border-block: 2px solid currentColor;

        &::before {
            content: "";
            position: absolute;
            inset-block-start: calc(50% - 1px);
            inset-inline: 0;
            block-size: 2px;
            background-color: currentColor;
        }
    }

    & .ahago-toc__fab-percent {
        position: absolute;
        inset-block-end: 0.5rem;
        inset-inline: 0;
        font-size: 0.625rem;
        font-weight: 700;
        font-variant-numeric: tabular-nums;
        line-height: 1;
        text-align: center;
        color: var(--ahago-toc-accent);
        opacity: 0;
        transition: opacity 160ms ease;
    }

    &.ahago-toc--has-progress .ahago-toc__fab-icon {
        translate: 0 -0.25rem;
    }

    &.ahago-toc--has-progress .ahago-toc__fab-percent {
        opacity: 1;
    }

    /* ---------- Backdrop + popover ---------- */

    & .ahago-toc__backdrop {
        position: fixed;
        inset: 0;
        z-index: 2147482999;
        background-color: color-mix(in srgb, #0b1220 38%, transparent);
        opacity: 0;
        visibility: hidden;
        transition: opacity 200ms ease, visibility 0s linear 200ms;
    }

    &.is-open .ahago-toc__backdrop {
        opacity: 1;
        visibility: visible;
        transition: opacity 200ms ease;
    }

    & .ahago-toc__popover {
        position: fixed;
        z-index: 2147483000;
        display: flex;
        flex-direction: column;
        inline-size: min(20rem, calc(100vw - 2rem));
        max-block-size: min(70vh, 30rem);
        border: 1px solid var(--ahago-toc-border);
        border-radius: var(--ahago-toc-radius-lg);
        background-color: var(--ahago-toc-surface);
        color: var(--ahago-toc-text);
        box-shadow: var(--ahago-toc-shadow);
        opacity: 0;
        visibility: hidden;
        scale: 0.96;
        transform-origin: var(--ahago-toc-pop-origin, bottom right);
        transition: opacity 180ms ease, scale 180ms ease, visibility 0s linear 180ms;
    }

    &.is-open .ahago-toc__popover {
        opacity: 1;
        visibility: visible;
        scale: 1;
        transition: opacity 180ms ease, scale 180ms ease;
    }

    & .ahago-toc__grip {
        display: none;
    }

    & .ahago-toc__scroll {
        flex: 0 1 auto;
        min-block-size: 0;
        overflow-x: hidden;
        overflow-y: auto;
        overscroll-behavior: contain;
        scrollbar-width: thin;
        scrollbar-gutter: stable;
        padding-block: 0.25rem 0.5rem;
        padding-inline: var(--ahago-toc-pad);
    }

    & .ahago-toc__popover .ahago-toc__link {
        padding-block: 0.4375rem;
    }

    & .ahago-toc__close {
        display: inline-grid;
        place-items: center;
        inline-size: 2rem;
        block-size: 2rem;
        flex: 0 0 auto;
        border: 1px solid var(--ahago-toc-border);
        border-radius: 50%;
        color: var(--ahago-toc-muted);
        cursor: pointer;
        transition: color 160ms ease, border-color 160ms ease;

        &:hover,
        &:focus-visible {
            color: var(--ahago-toc-text);
            border-color: var(--ahago-toc-muted);
        }
    }

    & .ahago-toc__close-icon {
        position: relative;
        inline-size: 0.875rem;
        block-size: 0.875rem;

        &::before,
        &::after {
            content: "";
            position: absolute;
            inset-block-start: 50%;
            inset-inline: 0;
            block-size: 1.5px;
            background-color: currentColor;
        }

        &::before {
            rotate: 45deg;
        }

        &::after {
            rotate: -45deg;
        }
    }
}

/* ---------- Target pulse + scroll lock (outside the component) ---------- */

.ahago-toc-target-pulse {
    animation: ahago-toc-pulse 900ms ease-out;
}

.ahago-toc-locked {
    overflow: hidden;
    overscroll-behavior: none;
}

@keyframes ahago-toc-enter {
    from {
        opacity: 0;
        translate: 0 0.5rem;
    }
}

@keyframes ahago-toc-pulse {
    0% {
        background-color: color-mix(in srgb, var(--ahago-toc-accent, #2f6bff) 16%, transparent);
    }

    100% {
        background-color: transparent;
    }
}

/* =====================================================================
   1.9.0 list presentation and display options.
   Every block is gated by a root modifier class rendered by PHP, so a
   disabled option costs nothing but a few unmatched selectors.
   ===================================================================== */

.ahago-toc {
    /* ---------- Numbering: 1. / 1.1 / 1.1.1 (CSS counters, no markup) ---------- */

    &.ahago-toc--numbered {
        & .ahago-toc__list {
            counter-reset: ahago-toc-h2 ahago-toc-h3 ahago-toc-h4;
        }

        & .ahago-toc__item--level-2 {
            counter-increment: ahago-toc-h2;
            counter-reset: ahago-toc-h3 ahago-toc-h4;
        }

        & .ahago-toc__item--level-3 {
            counter-increment: ahago-toc-h3;
            counter-reset: ahago-toc-h4;
        }

        & .ahago-toc__item--level-4 {
            counter-increment: ahago-toc-h4;
        }

        & .ahago-toc__link::before {
            flex: 0 0 auto;
            min-inline-size: 1.5em;
            color: var(--ahago-toc-muted);
            font-weight: 400;
            font-variant-numeric: tabular-nums;
        }

        & .ahago-toc__item--level-2 > .ahago-toc__link::before {
            content: counter(ahago-toc-h2) ".";
        }

        & .ahago-toc__item--level-3 > .ahago-toc__link::before {
            content: counter(ahago-toc-h2) "." counter(ahago-toc-h3);
            min-inline-size: 2.25em;
        }

        & .ahago-toc__item--level-4 > .ahago-toc__link::before {
            content: counter(ahago-toc-h2) "." counter(ahago-toc-h3) "." counter(ahago-toc-h4);
            min-inline-size: 3em;
        }
    }

    /* ---------- Tree lines for sub-sections ---------- */

    &.ahago-toc--tree {
        & .ahago-toc__list {
            gap: 0;
        }

        & .ahago-toc__link {
            padding-block: 0.3125rem;
        }

        & :is(.ahago-toc__item--level-3, .ahago-toc__item--level-4) {
            margin-inline-start: 0.5rem;
            border-inline-start: 1px solid var(--ahago-toc-border);
        }

        & .ahago-toc__item--level-3 {
            padding-inline-start: 0.75rem;
        }

        & .ahago-toc__item--level-4 {
            padding-inline-start: 1.75rem;
        }
    }

    /* ---------- Full-width rows (needed by highlight / row hover) ---------- */

    &.ahago-toc--rows {
        & .ahago-toc__body-inner,
        & .ahago-toc__scroll {
            padding-inline: max(0.25rem, calc(var(--ahago-toc-pad) - 0.5rem));
        }

        & .ahago-toc__link {
            position: relative;
            display: flex;
            inline-size: 100%;
            padding-inline: 0.5rem;
            transition: color 160ms ease, background-color 160ms ease;
        }

        & .ahago-toc__card--inline .ahago-toc__list::before {
            inset-inline-start: 0;
        }
    }

    /* ---------- Active entry: accent + side marker (no bold = no reflow) ---------- */

    &.ahago-toc--active-highlight .ahago-toc__link.is-active {
        color: var(--ahago-toc-accent-text);
        font-weight: inherit;
        background-color: var(--ahago-toc-accent-soft);

        &::before {
            color: inherit;
        }

        &::after {
            content: "";
            position: absolute;
            inset-block: 0.375rem;
            inset-inline-start: 0;
            inline-size: 3px;
            border-radius: 999rem;
            background-color: var(--ahago-toc-accent);
        }
    }

    /* ---------- Row hover: tint instead of nudging the text ---------- */

    &.ahago-toc--hover-row .ahago-toc__link {
        &:hover,
        &:focus-visible {
            color: var(--ahago-toc-text);
            translate: none;
            background-color: var(--ahago-toc-row-hover);
        }
    }

    /* ---------- Rich header: icon, section count, divider ---------- */

    &.ahago-toc--rich-head {
        & .ahago-toc__heading {
            flex: 1 1 auto;
            min-inline-size: 0;
        }

        & .ahago-toc__toggle {
            inline-size: 100%;
            gap: 0.625rem;
        }

        & .ahago-toc__heading-text {
            flex: 1 1 auto;
            min-inline-size: 0;
        }

        & .ahago-toc__chevron {
            margin-inline: 0.25rem;
        }

        & .ahago-toc__card--inline .ahago-toc__head {
            padding-block-end: 0.75rem;
            border-block-end: 1px solid var(--ahago-toc-border);
            margin-block-end: 0.375rem;
            transition: border-color 200ms ease, margin 200ms ease;
        }

        &.is-collapsed .ahago-toc__card--inline .ahago-toc__head {
            border-block-end-color: transparent;
            margin-block-end: 0;
        }
    }

    & .ahago-toc__heading-icon {
        position: relative;
        flex: 0 0 auto;
        inline-size: 1rem;
        block-size: 0.75rem;
        border-block: 2px solid var(--ahago-toc-accent);

        &::before {
            content: "";
            position: absolute;
            inset-block-start: calc(50% - 1px);
            inset-inline: 0;
            block-size: 2px;
            background-color: var(--ahago-toc-accent);
        }
    }

    & .ahago-toc__count {
        flex: 0 0 auto;
        padding: 0.125rem 0.5rem;
        border-radius: 999rem;
        background-color: var(--ahago-toc-row-hover);
        color: var(--ahago-toc-muted);
        font-size: 0.75rem;
        font-weight: 500;
        line-height: 1.5;
        font-variant-numeric: tabular-nums;
        white-space: nowrap;
    }

    & .ahago-toc__read {
        flex: 0 0 auto;
        color: var(--ahago-toc-muted);
        font-size: 0.75rem;
        font-variant-numeric: tabular-nums;
        white-space: nowrap;
    }

    /* ---------- Card surface ---------- */

    &.ahago-toc--card-accent-top .ahago-toc__card--inline {
        box-shadow: inset 0 3px 0 var(--ahago-toc-accent);
    }

    /* With a top progress bar the accent line becomes its track. */
    &.ahago-toc--card-accent-top.ahago-toc--progress-bar .ahago-toc__card--inline {
        box-shadow: inset 0 3px 0 var(--ahago-toc-accent-track);
    }

    &.ahago-toc--card-tinted .ahago-toc__card--inline {
        background-color: color-mix(in srgb, var(--ahago-toc-surface) 94%, var(--ahago-toc-accent));
    }

    /* ---------- Balanced wrapping of long titles ---------- */

    &.ahago-toc--pretty .ahago-toc__link {
        text-wrap: pretty;
    }

    /* ---------- Progress as a top bar ---------- */

    &.ahago-toc--progress-bar .ahago-toc__progress {
        inset-block-start: 0;
        inset-inline: 0;
        inline-size: auto;
        block-size: 3px;
        border-radius: 0;
        transform-origin: left center;
        scale: var(--ahago-toc-progress) 1;
    }

    &.ahago-toc--progress-bar:dir(rtl) .ahago-toc__progress {
        transform-origin: right center;
    }

    /* ---------- Sections already read ---------- */

    &.ahago-toc--mark-read .ahago-toc__item.is-read > .ahago-toc__link:not(.is-active) {
        color: var(--ahago-toc-muted);

        &::after {
            content: "";
            flex: 0 0 auto;
            align-self: center;
            inline-size: 0.3em;
            block-size: 0.6em;
            margin-inline-start: auto;
            border: solid currentColor;
            border-width: 0 1.5px 1.5px 0;
            rotate: 45deg;
            translate: 0 -0.1em;
        }
    }

    /* ---------- "Show more" clamp (inline card only) ---------- */

    &.ahago-toc--clamped:not(.is-list-expanded) .ahago-toc__card--inline .ahago-toc__item--extra {
        display: none;
    }

    & .ahago-toc__more {
        display: none;
    }

    &.ahago-toc--clamped .ahago-toc__more {
        display: inline-flex;
        align-items: center;
        margin-block-start: 0.5rem;
        padding: 0.375rem 0.875rem;
        border: 1px solid var(--ahago-toc-border);
        border-radius: 999rem;
        color: var(--ahago-toc-accent-text);
        font-size: 0.8125rem;
        font-weight: 500;
        cursor: pointer;
        transition: background-color 160ms ease;

        &:hover,
        &:focus-visible {
            background-color: var(--ahago-toc-accent-soft);
        }
    }

    /* ---------- Two columns for long lists (sized by the card, not the viewport) ---------- */

    &.ahago-toc--columns .ahago-toc__card--inline {
        container: ahago-toc-card / inline-size;
    }
}

@container ahago-toc-card (min-width: 36rem) {
    .ahago-toc--columns .ahago-toc__card--inline {
        & .ahago-toc__list {
            display: block;
            columns: 2;
            column-gap: 2rem;
        }

        & .ahago-toc__item {
            break-inside: avoid;
        }

        /* The hover rail tracks a single column; hide it in two-column mode. */
        & .ahago-toc__list::before {
            display: none;
        }
    }
}

/* ---------- Server-rendered collapsed state on phones (no layout shift) ---------- */

@media (max-width: 47.9375rem) {
    .ahago-toc--start-collapsed {
        & .ahago-toc__body {
            grid-template-rows: 0fr;
        }

        & .ahago-toc__chevron {
            rotate: -135deg;
            translate: 0 0.0625rem;
        }

        &.ahago-toc--rich-head .ahago-toc__card--inline .ahago-toc__head {
            border-block-end-color: transparent;
            margin-block-end: 0;
        }
    }
}

/* ---------- Desktop hover reveal (progressive depth) ---------- */

@media (hover: hover) and (pointer: fine) {
    .ahago-toc--desktop-hover .ahago-toc__card--inline {
        & .ahago-toc__item {
            transition: opacity 200ms ease;
        }

        &:not(:hover):not(:focus-within) :is(.ahago-toc__item--level-3, .ahago-toc__item--level-4):not(.is-active-item) {
            max-block-size: 0;
            opacity: 0;
            overflow: hidden;
            pointer-events: none;
        }
    }
}

/* ---------- Desktop: popover anchors to FAB, FAB placement ---------- */

@media (min-width: 48rem) {
    .ahago-toc {
        & .ahago-toc__backdrop {
            display: none;
        }

        & .ahago-toc__popover {
            inset-block-start: var(--ahago-toc-fab-anchor-block-top, auto);
            inset-block-end: var(--ahago-toc-fab-anchor-block, auto);
            inset-inline-end: var(--ahago-toc-fab-anchor-inline, 1.5rem);
            max-block-size: min(var(--ahago-toc-pop-max, 30rem), 30rem);
        }

        &:not(.is-fab-on-desktop) .ahago-toc__fab {
            display: none;
        }
    }

    /* Inline inset for side positions: screen edge, or beside the content
       column when "hug" is on (never closer to the edge than the margin). */
    .ahago-toc {
        --ahago-toc-fab-side: max(var(--ahago-toc-desktop-fab-inline-offset), env(safe-area-inset-right, 0px));
    }

    .ahago-toc--fab-hug {
        --ahago-toc-fab-side: max(
            var(--ahago-toc-desktop-fab-inline-offset),
            calc((100vw - var(--ahago-toc-content-width, 860px)) / 2 - var(--ahago-toc-fab-size) - 0.75rem)
        );
    }

    .ahago-toc--desktop-fab-bottom-left,
    .ahago-toc--desktop-fab-bottom-center,
    .ahago-toc--desktop-fab-bottom-right {
        & .ahago-toc__fab {
            inset-block-end: max(var(--ahago-toc-desktop-fab-block-offset), env(safe-area-inset-bottom, 0px));
        }
    }

    .ahago-toc--desktop-fab-top-left,
    .ahago-toc--desktop-fab-top-center,
    .ahago-toc--desktop-fab-top-right {
        & .ahago-toc__fab {
            inset-block-start: calc(var(--ahago-toc-desktop-fab-block-offset) + var(--wp-admin--admin-bar--height, 0px));
        }
    }

    .ahago-toc--desktop-fab-middle-left,
    .ahago-toc--desktop-fab-middle-right {
        --ahago-toc-fab-shift-y: -50%;

        & .ahago-toc__fab {
            inset-block-start: calc(50% + var(--ahago-toc-desktop-fab-block-offset));
        }
    }

    .ahago-toc--desktop-fab-bottom-right,
    .ahago-toc--desktop-fab-top-right,
    .ahago-toc--desktop-fab-middle-right {
        & .ahago-toc__fab {
            inset-inline-end: var(--ahago-toc-fab-side);
        }
    }

    .ahago-toc--desktop-fab-bottom-left,
    .ahago-toc--desktop-fab-top-left,
    .ahago-toc--desktop-fab-middle-left {
        & .ahago-toc__fab {
            inset-inline-start: var(--ahago-toc-fab-side);
        }
    }

    .ahago-toc--desktop-fab-bottom-center,
    .ahago-toc--desktop-fab-top-center {
        --ahago-toc-fab-shift-x: -50%;

        & .ahago-toc__fab {
            inset-inline-start: calc(50% + var(--ahago-toc-desktop-fab-inline-offset));
        }
    }
}

/* ---------- Desktop "hug": anchor to the real column edge ----------
   Browsers with CSS Anchor Positioning place the button against the actual
   edge of the TOC root (= the content column), whatever the theme layout
   (Blocksy narrow product layout, sidebars, off-center containers). The
   manual "content column width" formula above remains the fallback. */

@supports (anchor-name: --ahago-toc-column) {
    @media (min-width: 48rem) {
        .ahago-toc--fab-hug {
            anchor-name: --ahago-toc-column;
            anchor-scope: --ahago-toc-column;
            --ahago-toc-fab-gap: 0.75rem;
            /* Both custom properties are substituted inside inset properties,
               so 100% resolves against the viewport (the fixed button's
               containing block). The anchor() fallback reproduces the manual
               width formula if the anchor cannot be resolved. */
            --ahago-toc-fab-max-inset: calc(100% - var(--ahago-toc-fab-size) - var(--ahago-toc-desktop-fab-inline-offset));

            & .ahago-toc__fab {
                /* Hiding the button when the column scrolls away would defeat its purpose. */
                position-visibility: always;
            }
        }

        .ahago-toc--fab-hug:is(.ahago-toc--desktop-fab-bottom-right, .ahago-toc--desktop-fab-top-right, .ahago-toc--desktop-fab-middle-right) .ahago-toc__fab {
            inset-inline-end: auto;
            inset-inline-start: min(
                calc(anchor(--ahago-toc-column end, calc(100% - var(--ahago-toc-fab-side) - var(--ahago-toc-fab-size) - var(--ahago-toc-fab-gap))) + var(--ahago-toc-fab-gap)),
                var(--ahago-toc-fab-max-inset)
            );
        }

        .ahago-toc--fab-hug:is(.ahago-toc--desktop-fab-bottom-left, .ahago-toc--desktop-fab-top-left, .ahago-toc--desktop-fab-middle-left) .ahago-toc__fab {
            inset-inline-start: auto;
            inset-inline-end: min(
                calc(anchor(--ahago-toc-column start, calc(100% - var(--ahago-toc-fab-side) - var(--ahago-toc-fab-size) - var(--ahago-toc-fab-gap))) + var(--ahago-toc-fab-gap)),
                var(--ahago-toc-fab-max-inset)
            );
        }
    }
}

/* ---------- Mobile: popover is a bottom sheet, FAB placement ---------- */

@media (max-width: 47.9375rem) {
    .ahago-toc {
        & .ahago-toc__popover {
            inset-inline: var(--ahago-toc-gap);
            inset-block-end: max(var(--ahago-toc-gap), env(safe-area-inset-bottom, 0px));
            inline-size: auto;
            max-block-size: min(72vh, 32rem);
            transform-origin: bottom center;
            scale: 1;
            translate: 0 calc(100% + 2rem);
            transition: translate 260ms cubic-bezier(0.2, 0.8, 0.2, 1), visibility 0s linear 260ms;
        }

        &.is-open .ahago-toc__popover {
            translate: 0 0;
            transition: translate 260ms cubic-bezier(0.2, 0.8, 0.2, 1);
        }

        &.is-open .ahago-toc__fab {
            opacity: 0;
            visibility: hidden;
            pointer-events: none;
        }

        & .ahago-toc__grip {
            display: block;
            inline-size: 2.25rem;
            block-size: 0.25rem;
            margin-block-start: 0.625rem;
            margin-inline: auto;
            border-radius: 999rem;
            background-color: color-mix(in srgb, currentColor 20%, transparent);
        }

        &:not(.is-fab-on-mobile) .ahago-toc__fab {
            display: none;
        }
    }

    .ahago-toc--mobile-fab-bottom-left,
    .ahago-toc--mobile-fab-bottom-center,
    .ahago-toc--mobile-fab-bottom-right {
        & .ahago-toc__fab {
            inset-block-end: max(var(--ahago-toc-mobile-fab-block-offset), env(safe-area-inset-bottom, 0px));
        }
    }

    .ahago-toc--mobile-fab-top-left,
    .ahago-toc--mobile-fab-top-center,
    .ahago-toc--mobile-fab-top-right {
        & .ahago-toc__fab {
            inset-block-start: calc(var(--ahago-toc-mobile-fab-block-offset) + var(--wp-admin--admin-bar--height, 0px));
        }
    }

    .ahago-toc--mobile-fab-middle-left,
    .ahago-toc--mobile-fab-middle-right {
        --ahago-toc-fab-shift-y: -50%;

        & .ahago-toc__fab {
            inset-block-start: calc(50% + var(--ahago-toc-mobile-fab-block-offset));
        }
    }

    .ahago-toc--mobile-fab-bottom-right,
    .ahago-toc--mobile-fab-top-right,
    .ahago-toc--mobile-fab-middle-right {
        & .ahago-toc__fab {
            inset-inline-end: max(var(--ahago-toc-mobile-fab-inline-offset), env(safe-area-inset-right, 0px));
        }
    }

    .ahago-toc--mobile-fab-bottom-left,
    .ahago-toc--mobile-fab-top-left,
    .ahago-toc--mobile-fab-middle-left {
        & .ahago-toc__fab {
            inset-inline-start: max(var(--ahago-toc-mobile-fab-inline-offset), env(safe-area-inset-left, 0px));
        }
    }

    .ahago-toc--mobile-fab-bottom-center,
    .ahago-toc--mobile-fab-top-center {
        --ahago-toc-fab-shift-x: -50%;

        & .ahago-toc__fab {
            inset-inline-start: calc(50% + var(--ahago-toc-mobile-fab-inline-offset));
        }
    }
}

/* ---------- Auto scheme in dark mode ----------
   Chosen schemes set these tokens inline on the root, which wins over this
   block, so it only affects "Auto". */

@media (prefers-color-scheme: dark) {
    .ahago-toc {
        --ahago-toc-surface: color-mix(in srgb, Canvas 92%, #ffffff 8%);
        --ahago-toc-border: color-mix(in srgb, CanvasText 16%, transparent);
        --ahago-toc-text: CanvasText;
        --ahago-toc-link: color-mix(in srgb, CanvasText 82%, Canvas);
        --ahago-toc-muted: color-mix(in srgb, CanvasText 62%, Canvas);
        --ahago-toc-shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 16px 40px -16px rgba(0, 0, 0, 0.55);
    }
}

/* ---------- Motion off (setting) and reduced motion (visitor) ---------- */

.ahago-toc--no-motion {
    &,
    & *,
    & *::before,
    & *::after {
        animation: none !important;
        transition: none !important;
    }
}

/* Only when "Respect reduced motion" is on (the default, as in 1.8). The
   heading pulse lives outside the root and is skipped by the controller. */
@media (prefers-reduced-motion: reduce) {
    .ahago-toc--respect-rm,
    .ahago-toc--respect-rm *,
    .ahago-toc--respect-rm *::before,
    .ahago-toc--respect-rm *::after {
        animation: none !important;
        transition: none !important;
    }
}
