/**
 * Ahago Table of Contents — frontend styles.
 * Architecture: inline card (in content flow) + floating button + shared popover.
 * The root never sets `transform`/`filter`/`contain`, so the fixed FAB anchors
 * to the viewport rather than the TOC card.
 */

/*
 * Only the local reset lives in a low-priority cascade layer, so it neutralises
 * UA defaults and theme resets without out-prioritising the component's own
 * styles. The main styles below stay outside any layer, so the plugin's
 * `.ahago-toc__*` rules keep normal specificity and aren't accidentally
 * overridden by unlayered theme rules. The reset stops theme bleed (box-sizing,
 * line-height, list and button resets, utility `*` resets).
 */
@layer ahago-reset;

@layer ahago-reset {
    .ahago-toc {
        all: revert;
        display: block;
    }

    .ahago-toc,
    .ahago-toc *,
    .ahago-toc *::before,
    .ahago-toc *::after {
        box-sizing: border-box;
    }

    .ahago-toc ol,
    .ahago-toc li {
        margin: 0;
        padding: 0;
        list-style: none;
    }

    .ahago-toc button {
        margin: 0;
        padding: 0;
        border: 0;
        background: none;
        font: inherit;
        color: inherit;
        line-height: inherit;
    }

}



.ahago-toc {
    --ahago-toc-bg: #ffffff;
    --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-soft: color-mix(in srgb, var(--ahago-toc-accent) 12%, transparent);
    --ahago-toc-accent-track: color-mix(in srgb, var(--ahago-toc-accent) 16%, transparent);
    --ahago-toc-radius: 0.875rem;
    --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;

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

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

.ahago-toc__fab:focus-visible {
    outline: 2px solid var(--ahago-toc-accent);
    outline-offset: 3px;
}

/* ---------- 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;
    opacity: 0;
    translate: 0 0.5rem;
    animation: ahago-toc-enter 280ms ease-out forwards;
}

.ahago-toc__progress {
    position: absolute;
    inset-block-start: 0;
    inset-inline-start: 0;
    inline-size: 0.1875rem;
    block-size: calc(var(--ahago-toc-progress) * 100%);
    border-radius: 999rem;
    background-color: var(--ahago-toc-accent);
    opacity: 0;
    transition: block-size 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__heading {
    margin-block: 0;
    font-size: clamp(1rem, 1.4vw, 1.0625rem);
    font-weight: 650;
    line-height: 1.3;
    letter-spacing: -0.01em;
}

.ahago-toc__toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0;
    border: 0;
    background-color: transparent;
    color: inherit;
    font: inherit;
    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;
}

.ahago-toc.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;
}

.ahago-toc.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;
    margin-block: 0;
    padding-inline-start: 0;
    list-style: none;
    counter-reset: ahago-toc;
}

.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;
}

.ahago-toc.has-hover-rail .ahago-toc__list::before {
    opacity: 0.7;
}

/* The hover rail is an inline-card affordance only. Inside the scrollable
   popover it must never render, or its absolute positioning would inflate the
   scroll height and create phantom empty space. */
.ahago-toc__scroll .ahago-toc__list::before {
    content: none;
}

.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;
    color: var(--ahago-toc-link);
    text-decoration: none;
    border-radius: 0.375rem;
    transition: color 160ms ease, translate 160ms ease;

    &:hover,
    &:focus-visible {
        color: var(--ahago-toc-accent);
        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;
    padding: 0;
    border: 0;
    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: 0 0.5rem;
    transition: opacity 200ms ease, scale 200ms ease, translate 200ms ease, visibility 0s linear 200ms;
    cursor: pointer;
}

.ahago-toc.is-fab-visible .ahago-toc__fab {
    opacity: 1;
    visibility: visible;
    scale: 1;
    translate: 0 0;
    transition: opacity 200ms ease, scale 200ms ease, translate 200ms ease;
}

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

.ahago-toc.is-fab-idle .ahago-toc__fab:hover,
.ahago-toc.is-fab-idle .ahago-toc__fab: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
    );
    -webkit-mask:
        linear-gradient(#000 0 0) content-box,
        linear-gradient(#000 0 0);
    -webkit-mask-composite: xor;
    mask:
        linear-gradient(#000 0 0) content-box exclude,
        linear-gradient(#000 0 0);
    mask-composite: exclude;
}

.ahago-toc__fab-icon {
    position: relative;
    inline-size: 1.125rem;
    block-size: 0.875rem;
    border-block-start: 2px solid currentColor;
    border-block-end: 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;
}

.ahago-toc.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));
    block-size: auto;
    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);
    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;
}

.ahago-toc.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-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%;
    background-color: transparent;
    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 ---------- */

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

.ahago-toc-locked {
    overflow: hidden;
}

@keyframes ahago-toc-enter {
    to {
        opacity: 1;
        translate: 0 0;
    }
}

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

    100% {
        background-color: transparent;
    }
}

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

@media (hover: hover) and (pointer: fine) {
    .ahago-toc--desktop-hover .ahago-toc__card--inline:not(:hover):not(:focus-within) {
        & .ahago-toc__item--level-3:not(.is-active-item),
        & .ahago-toc__item--level-4:not(.is-active-item) {
            max-block-size: 0;
            margin-block: 0;
            opacity: 0;
            overflow: hidden;
            pointer-events: none;
        }

        & .ahago-toc__item {
            transition: max-block-size 200ms ease, opacity 200ms ease;
        }
    }
}

/* ---------- Popover placement: desktop anchors to FAB, mobile is a sheet ---------- */

@media (min-width: 48rem) {
    .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);
    }
}

@media (max-width: 47.9375rem) {
    .ahago-toc.is-open .ahago-toc__fab {
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
    }
}

@media (max-width: 47.9375rem) {
    .ahago-toc__popover {
        inset-inline: var(--ahago-toc-gap);
        inset-block-end: max(var(--ahago-toc-gap), env(safe-area-inset-bottom));
        inline-size: auto;
        block-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;
    }

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

    .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);
    }
}

/* ---------- FAB placement (device-scoped) ---------- */

@media (min-width: 48rem) {
    /* Hug the content column: place the button just outside the centered
       content edge. Pure CSS — no DOM measuring. Falls back to the user's
       offset when the viewport is narrower than the content width. */
    .ahago-toc--fab-hug {
        --ahago-toc-fab-inline-resolved: max(
            var(--ahago-toc-desktop-fab-inline-offset),
            calc((100vw - var(--ahago-toc-content-width, 860px)) / 2 - var(--ahago-toc-fab-size) - 0.5rem)
        );
    }

    .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));
        inset-inline-end: max(var(--ahago-toc-fab-inline-resolved, var(--ahago-toc-desktop-fab-inline-offset)), env(safe-area-inset-right));
    }

    .ahago-toc--desktop-fab-bottom-left .ahago-toc__fab {
        inset-block-end: max(var(--ahago-toc-desktop-fab-block-offset), env(safe-area-inset-bottom));
        inset-inline-start: max(var(--ahago-toc-desktop-fab-inline-offset), env(safe-area-inset-left));
    }

    .ahago-toc--desktop-fab-bottom-center .ahago-toc__fab {
        inset-block-end: max(var(--ahago-toc-desktop-fab-block-offset), env(safe-area-inset-bottom));
        inset-inline-start: calc(50% + var(--ahago-toc-desktop-fab-inline-offset));
        translate: -50% 0;
    }

    .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));
        inset-inline-end: max(var(--ahago-toc-fab-inline-resolved, var(--ahago-toc-desktop-fab-inline-offset)), env(safe-area-inset-right));
    }

    .ahago-toc--desktop-fab-top-left .ahago-toc__fab {
        inset-block-start: calc(var(--ahago-toc-desktop-fab-block-offset) + var(--wp-admin--admin-bar--height, 0px));
        inset-inline-start: max(var(--ahago-toc-desktop-fab-inline-offset), env(safe-area-inset-left));
    }

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

    .ahago-toc--desktop-fab-middle-right .ahago-toc__fab {
        inset-block-start: calc(50% + var(--ahago-toc-desktop-fab-block-offset));
        inset-inline-end: max(var(--ahago-toc-fab-inline-resolved, var(--ahago-toc-desktop-fab-inline-offset)), env(safe-area-inset-right));
        translate: 0 -50%;
    }

    .ahago-toc--desktop-fab-middle-left .ahago-toc__fab {
        inset-block-start: calc(50% + var(--ahago-toc-desktop-fab-block-offset));
        inset-inline-start: max(var(--ahago-toc-desktop-fab-inline-offset), env(safe-area-inset-left));
        translate: 0 -50%;
    }

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

    /* Hug the content column instead of the screen edge. */
    .ahago-toc--fab-anchor-content.ahago-toc--desktop-fab-bottom-right .ahago-toc__fab,
    .ahago-toc--fab-anchor-content.ahago-toc--desktop-fab-top-right .ahago-toc__fab,
    .ahago-toc--fab-anchor-content.ahago-toc--desktop-fab-middle-right .ahago-toc__fab {
        inset-inline-end: calc(
            var(--ahago-toc-content-edge-right, 0px) + var(--ahago-toc-desktop-fab-inline-offset)
        );
    }

    .ahago-toc--fab-anchor-content.ahago-toc--desktop-fab-bottom-left .ahago-toc__fab,
    .ahago-toc--fab-anchor-content.ahago-toc--desktop-fab-top-left .ahago-toc__fab,
    .ahago-toc--fab-anchor-content.ahago-toc--desktop-fab-middle-left .ahago-toc__fab {
        inset-inline-start: calc(
            var(--ahago-toc-content-edge-left, 0px) + var(--ahago-toc-desktop-fab-inline-offset)
        );
    }
}

@media (max-width: 47.9375rem) {
    .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));
        inset-inline-end: max(var(--ahago-toc-fab-inline-resolved, var(--ahago-toc-mobile-fab-inline-offset)), env(safe-area-inset-right));
    }

    .ahago-toc--mobile-fab-bottom-left .ahago-toc__fab {
        inset-block-end: max(var(--ahago-toc-mobile-fab-block-offset), env(safe-area-inset-bottom));
        inset-inline-start: max(var(--ahago-toc-mobile-fab-inline-offset), env(safe-area-inset-left));
    }

    .ahago-toc--mobile-fab-bottom-center .ahago-toc__fab {
        inset-block-end: max(var(--ahago-toc-mobile-fab-block-offset), env(safe-area-inset-bottom));
        inset-inline-start: calc(50% + var(--ahago-toc-mobile-fab-inline-offset));
        translate: -50% 0;
    }

    .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));
        inset-inline-end: max(var(--ahago-toc-fab-inline-resolved, var(--ahago-toc-mobile-fab-inline-offset)), env(safe-area-inset-right));
    }

    .ahago-toc--mobile-fab-top-left .ahago-toc__fab {
        inset-block-start: calc(var(--ahago-toc-mobile-fab-block-offset) + var(--wp-admin--admin-bar--height, 0px));
        inset-inline-start: max(var(--ahago-toc-mobile-fab-inline-offset), env(safe-area-inset-left));
    }

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

    .ahago-toc--mobile-fab-middle-right .ahago-toc__fab {
        inset-block-start: calc(50% + var(--ahago-toc-mobile-fab-block-offset));
        inset-inline-end: max(var(--ahago-toc-fab-inline-resolved, var(--ahago-toc-mobile-fab-inline-offset)), env(safe-area-inset-right));
        translate: 0 -50%;
    }

    .ahago-toc--mobile-fab-middle-left .ahago-toc__fab {
        inset-block-start: calc(50% + var(--ahago-toc-mobile-fab-block-offset));
        inset-inline-start: max(var(--ahago-toc-mobile-fab-inline-offset), env(safe-area-inset-left));
        translate: 0 -50%;
    }

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

/* ---------- Dark scheme ---------- */

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

/* ---------- Reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
    .ahago-toc *,
    .ahago-toc-target-pulse {
        animation-duration: 1ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 1ms !important;
        scroll-behavior: auto !important;
    }

    .ahago-toc__card--inline {
        opacity: 1;
        translate: 0 0;
    }
}
