/* ============================================================================
   Target U — Floating Section Navigation Overlay (`ui-section-nav`)
   Conforms to DESIGN.md v2.23:
   - 0px square corners everywhere (--theme-radius-control / --theme-radius-popover)
   - Clear 1px border-strong + --theme-shadow-overlay, no blur / glass / lift
   - 44px touch targets for trigger and list rows; uniform square tiles for
     footer back-to-top and page switcher
   - Selected / current location uses surface-selected + primary ink + underline
   - Native Popover API (`popover="auto"`) + CSS Anchor Positioning with
     progressive-enhancement fallback gated behind `@supports`
   ============================================================================ */

:where([data-runtime-theme="target-u"]) [data-section-nav-item] {
    scroll-margin-top: calc(4rem + var(--theme-spacing-lg));
}

:where([data-runtime-theme="target-u"]) .ui-section-nav {
    position: fixed;
    z-index: 45;
    inset-block-end: calc(var(--theme-spacing-xl) + env(safe-area-inset-bottom, 0px));
    inset-inline-end: var(--theme-spacing-xl);
    max-width: calc(100vw - var(--theme-spacing-2xl));
}

/* Reserve the fixed trigger's footprint under the footer so the page's last
   controls (e.g. the support link) can scroll clear of it. */
:where([data-runtime-theme="target-u"]:has([data-ui-section-nav]:not([hidden]))) .ui-footer {
    padding-block-end: calc(var(--theme-spacing-xl) + 44px + env(safe-area-inset-bottom, 0px));
}

/* Trigger control: 44px standard control height, optically centered single line */
:where([data-runtime-theme="target-u"]) .ui-section-nav__trigger {
    anchor-name: --ui-section-nav-trigger;
    display: inline-flex;
    align-items: center;
    gap: var(--theme-spacing-sm);
    box-sizing: border-box;
    height: 44px;
    max-width: min(24rem, calc(100vw - var(--theme-spacing-2xl)));
    padding: 0 var(--theme-spacing-md);
    border: 1px solid var(--theme-color-border-strong);
    border-radius: var(--theme-radius-control);
    background: var(--theme-color-surface);
    color: var(--theme-color-foreground);
    box-shadow: var(--theme-shadow-overlay);
    font-family: var(--theme-font-sans);
    font-size: var(--theme-text-control);
    font-weight: var(--theme-text-control--font-weight);
    line-height: 1;
    letter-spacing: var(--theme-text-control--letter-spacing);
    cursor: pointer;
    transition:
        background-color var(--theme-motion-duration-interaction),
        border-color var(--theme-motion-duration-interaction),
        color var(--theme-motion-duration-interaction);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__trigger-icon {
    display: inline-flex;
    flex: none;
    align-items: center;
    justify-content: center;
    color: var(--theme-color-primary);
}

/* Semantic selected-square marker inside the vertical section-rail icon (DESIGN.md Icons). */
:where([data-runtime-theme="target-u"]) .ui-section-nav__icon-marker {
    fill: currentColor;
    stroke: none;
}

/* Baseline-align Chinese kicker + separator + current title so differing font
   sizes share one steady reading line without the smaller kicker floating high. */
:where([data-runtime-theme="target-u"]) .ui-section-nav__trigger-label {
    display: inline-flex;
    align-items: baseline;
    gap: 6px;
    min-width: 0;
    line-height: 1.25;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__trigger-kicker {
    flex: none;
    color: var(--theme-color-muted);
    font-size: var(--theme-text-supporting);
    font-weight: var(--theme-text-supporting--font-weight);
    line-height: 1.25;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__trigger-sep {
    flex: none;
    color: var(--theme-color-border-strong);
    font-size: var(--theme-text-supporting);
    line-height: 1.25;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__trigger-current {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 1.25;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__trigger-divider {
    flex: none;
    width: 1px;
    height: 14px;
    background: var(--theme-color-border);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__trigger-progress {
    display: inline-flex;
    flex: none;
    align-items: center;
    color: var(--theme-color-muted);
    font-family: var(--theme-font-data);
    font-size: 13px;
    font-weight: var(--theme-text-data--font-weight);
    font-variant-numeric: tabular-nums;
    line-height: 1;
    white-space: nowrap;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__chevron {
    flex: none;
    color: var(--theme-color-muted);
    transition:
        transform var(--theme-motion-duration-interaction),
        color var(--theme-motion-duration-interaction);
}

@media (hover: hover) and (pointer: fine) {
    :where([data-runtime-theme="target-u"]) .ui-section-nav__trigger:hover {
        border-color: var(--theme-color-primary);
        background: var(--theme-color-surface-muted);
        color: var(--theme-color-primary);
    }

    :where([data-runtime-theme="target-u"]) .ui-section-nav__trigger:hover .ui-section-nav__chevron {
        color: var(--theme-color-primary);
    }
}

/* Expanded state is stronger than hover (DESIGN.md Field / Filter contract) */
:where([data-runtime-theme="target-u"]) .ui-section-nav__trigger[aria-expanded="true"] {
    border-color: var(--theme-color-primary);
    background: var(--theme-color-primary-soft);
    color: var(--theme-color-primary);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__trigger[aria-expanded="true"] .ui-section-nav__trigger-kicker,
:where([data-runtime-theme="target-u"]) .ui-section-nav__trigger[aria-expanded="true"] .ui-section-nav__trigger-progress,
:where([data-runtime-theme="target-u"]) .ui-section-nav__trigger[aria-expanded="true"] .ui-section-nav__chevron {
    color: var(--theme-color-primary);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__trigger[aria-expanded="true"] .ui-section-nav__chevron {
    transform: rotate(180deg);
}

:where([data-runtime-theme="target-u"]:not([data-focus-modality="pointer"]))
.ui-section-nav__trigger:focus-visible,
:where([data-runtime-theme="target-u"]:not([data-focus-modality="pointer"]))
.ui-section-nav__link:focus-visible,
:where([data-runtime-theme="target-u"]:not([data-focus-modality="pointer"]))
.ui-section-nav__top-btn:focus-visible,
:where([data-runtime-theme="target-u"]:not([data-focus-modality="pointer"]))
.ui-section-nav__page-btn:focus-visible {
    outline: var(--theme-focus-ring-width) solid var(--theme-color-focus);
    outline-offset: var(--theme-focus-ring-gap);
    box-shadow: var(--theme-focus-ring-halo);
}

/* Headerless overlay panel: top-layer popover anchored above bottom-right trigger */
:where([data-runtime-theme="target-u"]) .ui-section-nav__panel {
    box-sizing: border-box;
    width: min(22.5rem, calc(100vw - var(--theme-spacing-2xl)));
    max-height: min(64dvh, 29rem);
    padding: 0;
    border: 1px solid var(--theme-color-border-strong);
    border-radius: var(--theme-radius-popover);
    background: var(--theme-color-surface);
    color: var(--theme-color-foreground);
    box-shadow: var(--theme-shadow-overlay);
    overflow: hidden;
    transform-origin: bottom right;
}

/* Fallback layout when anchor positioning is unavailable: fixed above bottom-right trigger */
@supports not (position-area: block-start) {
    :where([data-runtime-theme="target-u"]) .ui-section-nav__panel {
        position: fixed;
        inset: auto var(--theme-spacing-xl)
            calc(var(--theme-spacing-xl) + 44px + var(--theme-spacing-sm) + env(safe-area-inset-bottom, 0px))
            auto;
        margin: 0;
    }
}

/* Progressive enhancement with CSS Anchor Positioning */
@supports (position-area: block-start) {
    :where([data-runtime-theme="target-u"]) .ui-section-nav__panel {
        position-anchor: --ui-section-nav-trigger;
        inset: auto;
        margin: 0;
        margin-block-end: var(--theme-spacing-sm);
        position-area: block-start span-inline-start;
        position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
    }
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__panel:is(:popover-open, .\:popover-open, [data-section-nav-open="true"]) {
    display: flex;
    flex-direction: column;
}

@supports not selector(:popover-open) {
    :where([data-runtime-theme="target-u"]) .ui-section-nav__panel:not(.\:popover-open):not([data-section-nav-open="true"]) {
        display: none;
    }
}

@media (prefers-reduced-motion: no-preference) {
    :where([data-runtime-theme="target-u"]) .ui-section-nav__panel:is(:popover-open, .\:popover-open, [data-section-nav-open="true"]) {
        animation: ui-section-nav-enter var(--theme-motion-duration-overlay) var(--theme-motion-ease-overlay) both;
    }
}

@keyframes ui-section-nav-enter {
    from {
        opacity: 0;
        transform: translateY(6px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Quick filter search (shown automatically when sections >= 8) */
:where([data-runtime-theme="target-u"]) .ui-section-nav__search {
    flex: 0 0 auto;
    padding: var(--theme-spacing-sm);
    border-block-end: 1px solid var(--theme-color-border);
    background: var(--theme-color-surface);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__search .ui-search {
    width: 100%;
    margin: 0;
}

/* Scrollable body: 4px padding preserves the 3px outer focus-visible ring */
:where([data-runtime-theme="target-u"]) .ui-section-nav__body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: var(--theme-spacing-xs);
    scrollbar-color: var(--theme-color-border-strong) transparent;
    scrollbar-width: thin;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__list {
    display: grid;
    gap: 2px;
    margin: 0;
    padding: 0;
    list-style: none;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__item--footer-start {
    margin-block-start: var(--theme-spacing-xs);
    padding-block-start: var(--theme-spacing-xs);
    border-block-start: 1px solid var(--theme-color-border);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__link {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;
    gap: var(--theme-spacing-sm);
    box-sizing: border-box;
    min-height: 44px;
    padding: var(--theme-spacing-sm) var(--theme-spacing-md);
    border-radius: var(--theme-radius-control);
    color: var(--theme-color-foreground);
    font-family: var(--theme-font-sans);
    font-size: var(--theme-text-dense-label);
    font-weight: var(--theme-text-dense-label--font-weight);
    line-height: 1.3;
    letter-spacing: var(--theme-text-dense-label--letter-spacing);
    text-decoration: none;
    transition:
        background-color var(--theme-motion-duration-interaction),
        color var(--theme-motion-duration-interaction);
}

@media (hover: hover) and (pointer: fine) {
    :where([data-runtime-theme="target-u"]) .ui-section-nav__link:hover {
        background: color-mix(in oklch, var(--theme-color-primary-soft) 70%, var(--theme-color-surface));
        color: var(--theme-color-primary-light);
    }
}

/* Current location follows DESIGN.md v2.20 nav-menu-item-current */
:where([data-runtime-theme="target-u"]) .ui-section-nav__link[aria-current="location"] {
    background: var(--theme-color-surface-selected);
    color: var(--theme-color-primary);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__link[aria-current="location"] .ui-section-nav__item-title {
    text-decoration: underline;
    text-underline-offset: 0.2em;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__link[aria-current="location"] .ui-section-nav__index,
:where([data-runtime-theme="target-u"]) .ui-section-nav__link[aria-current="location"] .ui-section-nav__count {
    color: var(--theme-color-primary);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__marker {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 1.25rem;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__swatch {
    display: inline-block;
    flex: none;
    width: 8px;
    height: 8px;
    border-radius: var(--theme-radius-none);
    background: var(--theme-color-primary);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__swatch--dream {
    background: var(--theme-color-dream-text);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__swatch--aggressive {
    background: var(--theme-color-aggressive-text);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__swatch--realistic {
    background: var(--theme-color-realistic-text);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__swatch--safe {
    background: var(--theme-color-safe-text);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__swatch--low_prob {
    background: var(--theme-color-low-prob-text);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__swatch--failed {
    background: var(--theme-color-failed-text);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__index {
    color: var(--theme-color-muted);
    font-family: var(--theme-font-data);
    font-size: var(--theme-text-supporting);
    font-variant-numeric: tabular-nums;
    line-height: 1;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__item-title {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__count {
    color: var(--theme-color-muted);
    font-family: var(--theme-font-data);
    font-size: var(--theme-text-supporting);
    font-variant-numeric: tabular-nums;
    line-height: 1;
    white-space: nowrap;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__empty {
    margin: 0;
    padding: var(--theme-spacing-lg) var(--theme-spacing-md);
    text-align: center;
    color: var(--theme-color-muted);
}

/* Footer: Back-to-top icon on far left + right-aligned pagination zone
   (optional active-group label directly above its own square tiles). */
:where([data-runtime-theme="target-u"]) .ui-section-nav__footer {
    display: flex;
    flex: 0 0 auto;
    align-items: flex-end;
    justify-content: space-between;
    gap: var(--theme-spacing-sm);
    padding: var(--theme-spacing-sm);
    border-block-start: 1px solid var(--theme-color-border);
    background: var(--theme-color-surface-muted);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__pager-zone {
    display: flex;
    flex: 0 1 auto;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    min-width: 0;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__pager-context {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--theme-spacing-md);
    font-family: var(--theme-font-sans);
    font-size: var(--theme-text-supporting);
    font-weight: var(--theme-text-supporting--font-weight);
    line-height: 1.25;
    color: var(--theme-color-foreground);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__pager-context-label {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__pager-context-name {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__pager-context-meta {
    flex: none;
    color: var(--theme-color-muted);
    font-family: var(--theme-font-data);
    font-size: var(--theme-text-supporting);
    font-variant-numeric: tabular-nums;
    line-height: 1;
    white-space: nowrap;
}

/* Uniform 38x38 square tiles for Back-to-top icon and page buttons */
:where([data-runtime-theme="target-u"]) .ui-section-nav__top-btn,
:where([data-runtime-theme="target-u"]) .ui-section-nav__page-btn {
    box-sizing: border-box;
    display: inline-flex;
    flex: none;
    align-items: center;
    justify-content: center;
    min-width: 38px;
    height: 38px;
    padding: 0 6px;
    border: 1px solid var(--theme-color-border-strong);
    border-radius: var(--theme-radius-control);
    background: var(--theme-color-surface);
    color: var(--theme-color-foreground);
    font-family: var(--theme-font-data);
    font-size: 13px;
    font-weight: var(--theme-text-data--font-weight);
    font-variant-numeric: tabular-nums;
    line-height: 1;
    text-decoration: none;
    cursor: pointer;
    transition:
        background-color var(--theme-motion-duration-interaction),
        border-color var(--theme-motion-duration-interaction),
        color var(--theme-motion-duration-interaction);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__top-btn {
    width: 38px;
    padding: 0;
    color: var(--theme-color-primary);
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__pager {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: var(--theme-spacing-xs);
    min-height: 38px;
    min-width: 0;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__pager[aria-busy="true"] {
    opacity: 0.65;
    pointer-events: none;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__page-btn--current {
    border-color: var(--theme-color-primary);
    background: var(--theme-color-surface-selected);
    color: var(--theme-color-primary);
    font-weight: 600;
    cursor: default;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__page-btn[aria-disabled="true"] {
    border-color: var(--theme-color-border);
    background: var(--theme-color-surface-muted);
    color: var(--theme-color-muted);
    opacity: 0.55;
    cursor: not-allowed;
}

@media (hover: hover) and (pointer: fine) {
    :where([data-runtime-theme="target-u"]) .ui-section-nav__top-btn:hover,
    :where([data-runtime-theme="target-u"]) a.ui-section-nav__page-btn:hover {
        border-color: var(--theme-color-primary);
        background: var(--theme-color-surface-muted);
        color: var(--theme-color-primary);
    }
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__page-ellipsis {
    display: inline-flex;
    flex: none;
    align-items: center;
    justify-content: center;
    min-width: 14px;
    color: var(--theme-color-muted);
    font-size: var(--theme-text-supporting);
    line-height: 1;
}

:where([data-runtime-theme="target-u"]) .ui-section-nav__pager-note {
    color: var(--theme-color-muted);
    font-family: var(--theme-font-sans);
    font-size: var(--theme-text-supporting);
    line-height: 1.3;
    padding-inline: var(--theme-spacing-xs);
}

/* Mobile adaptation */
@media (max-width: 767px) {
    :where([data-runtime-theme="target-u"]) .ui-section-nav {
        inset-block-end: calc(var(--theme-spacing-lg) + env(safe-area-inset-bottom, 0px));
        inset-inline-end: var(--theme-spacing-lg);
    }

    :where([data-runtime-theme="target-u"]:has([data-ui-section-nav]:not([hidden]))) .ui-footer {
        padding-block-end: calc(var(--theme-spacing-lg) + 44px + env(safe-area-inset-bottom, 0px));
    }

    :where([data-runtime-theme="target-u"]) .ui-section-nav__trigger {
        max-width: calc(100vw - var(--theme-spacing-2xl));
        padding-inline: var(--theme-spacing-md);
    }

    :where([data-runtime-theme="target-u"]) .ui-section-nav__trigger-kicker,
    :where([data-runtime-theme="target-u"]) .ui-section-nav__trigger-sep {
        display: none;
    }

    @supports not (position-area: block-start) {
        :where([data-runtime-theme="target-u"]) .ui-section-nav__panel {
            inset-inline-end: var(--theme-spacing-lg);
            inset-block-end: calc(var(--theme-spacing-lg) + 44px + var(--theme-spacing-sm) + env(safe-area-inset-bottom, 0px));
        }
    }
}
