/* Motion (docs/superpowers/specs/2026-11-19-motion-design.md).
   Loaded last on every page. Movement answers the visitor; nothing here moves by itself for long.
   Every moving rule sits inside prefers-reduced-motion: no-preference, so the default is the end state. */

/* ---------- S4. Reading progress: a 2px bar on the header's lower edge, tied to the scroll position.
   It mirrors the reader's own scrolling, so it stays under reduced motion. ---------- */
@supports (animation-timeline: scroll()) {
    #navigation::before,
    .researchNav::after {
        content: "";
        position: absolute;
        inset: auto 0 -1px 0;
        height: 2px;
        background: var(--accent, #2f5d50);
        transform-origin: 0 50%;
        scale: 0 1;
        pointer-events: none;
        animation: read-progress linear both;
        animation-timeline: scroll(root block);
    }

    @keyframes read-progress {
        to { scale: 1 1; }
    }
}

/* ---------- S3. Navigation hairline: shown only if the next page takes more than 200ms. ---------- */
html.is-leaving::after {
    content: "";
    position: fixed;
    inset: 0 0 auto 0;
    z-index: 2000;
    height: 2px;
    background: var(--accent, #2f5d50);
    opacity: 0;
    pointer-events: none;
    animation: nav-hairline 1.1s var(--ease-standard) 200ms 8;
}

@keyframes nav-hairline {
    from { opacity: 1; translate: -100% 0; scale: .4 1; }
    to { opacity: 1; translate: 100% 0; scale: .4 1; }
}

@media (prefers-reduced-motion: reduce) {
    html.is-leaving::after {
        animation: none;
        opacity: .6;
    }
}

/* ---------- S2. The loading mark: eight glyphs stepped through a one-character window. ---------- */
.loadingMark {
    display: inline-block;
    /* The glyphs come from a symbol fallback font and are close to 1em wide. */
    width: 1em;
    height: 1em;
    text-align: center;
    font-variant-emoji: text;
    overflow: hidden;
    line-height: 1;
    vertical-align: -0.1em;
    color: var(--accent, currentColor);
}

.loadingMark > span {
    display: block;
    white-space: pre;
    /* Reduced motion and idle states rest on the full star. */
    translate: 0 -4em;
}

.turnStatus .loadingMark {
    margin-right: .5ch;
}

/* In the overlay bar the mark shows only while the conversation frame is loading. */
.chatOverlayBar .loadingMark {
    visibility: hidden;
}

.chatOverlay[data-loading] .chatOverlayBar .loadingMark {
    visibility: visible;
}

@media (prefers-reduced-motion: no-preference) {
    .turnStatus--pending .loadingMark > span,
    .chatOverlay[data-loading] .loadingMark > span {
        animation: loading-mark 1.2s steps(8, end) infinite;
    }

    @keyframes loading-mark {
        from { translate: 0 0; }
        to { translate: 0 -8em; }
    }

    /* The label beside it carries a slow sheen while it waits. */
    .turnStatus--pending {
        background: linear-gradient(90deg, currentColor 33%, color-mix(in srgb, currentColor 45%, transparent) 50%, currentColor 66%) 100% 0 / 300% 100% no-repeat;
        -webkit-background-clip: text;
        background-clip: text;
        -webkit-text-fill-color: transparent;
        animation: label-sheen 1.5s linear infinite;
    }

    .turnStatus--pending {
        width: fit-content;
    }

    .turnStatus--pending .loadingMark {
        -webkit-text-fill-color: currentColor;
    }

    @media (forced-colors: active) {
        .turnStatus--pending {
            background: none;
            -webkit-text-fill-color: currentColor;
            animation: none;
        }
    }

    @keyframes label-sheen {
        to { background-position: 0 0; }
    }
}

/* ---------- S1. The wordmark: "Tony Koo" narrows to "T K" over the first 160px of scroll. ---------- */
.wmRest,
.wmRest > b {
    font: inherit;
}

.wmRest {
    display: inline-grid;
    grid-template-columns: 1fr;
    vertical-align: bottom;
}

.wmRest > b {
    overflow: hidden;
    min-width: 0;
    white-space: nowrap;
}

@media (prefers-reduced-motion: no-preference) {
    @supports (animation-timeline: scroll()) {
        .wmRest {
            animation: wordmark-collapse linear both;
            animation-timeline: scroll(root block);
            animation-range: 0 160px;
        }

        @keyframes wordmark-collapse {
            from { animation-timing-function: var(--ease-inout-quart); }
            to { grid-template-columns: 0fr; opacity: 0; }
        }

        /* R7. The header gains a hairline shadow once the page has moved. */
        .researchNav {
            animation: header-edge linear both;
            animation-timeline: scroll(root block);
            animation-range: 0 8px;
        }

        @keyframes header-edge {
            from { box-shadow: 0 1px 0 transparent; }
            to { box-shadow: 0 1px 0 var(--line, #dcd7c9), 0 6px 16px -12px rgb(0 0 0 / .35); }
        }
    }
}

/* ---------- S5. Pages hand over: a short cross-fade; the header holds still;
   a featured card's title grows into its report's title. ---------- */
@media (prefers-reduced-motion: no-preference) {
    @view-transition {
        navigation: auto;
    }

    /* Inner pages share one header, so it holds still between them. The homepage header is a
       different size, so it cross-fades with the page. */
    .researchNav {
        view-transition-name: site-nav;
    }

    .workTitle {
        view-transition-class: work-title;
    }

    ::view-transition-old(root) {
        animation: 90ms ease-out both page-out;
    }

    ::view-transition-new(root) {
        animation: 180ms ease-in both page-in;
    }

    ::view-transition-group(.work-title) {
        animation-duration: 320ms;
        animation-timing-function: var(--ease-out);
    }

    @keyframes page-out {
        to { opacity: 0; }
    }

    @keyframes page-in {
        from { opacity: 0; }
    }
}

/* ---------- E1–E4. One staged entrance per page, played once. Keyframes have a "from" only,
   so without animation support everything is simply in place. ---------- */
@media (prefers-reduced-motion: no-preference) {
    @keyframes rise {
        from { opacity: 0; translate: 0 8px; }
    }

    .fieldIntro .heroTitle .title-word,
    .fieldIntro .heroTitle .title-line,
    .fieldIntro .heroActions,
    .fieldIntro .heroProofLine,
    .fieldIntro .proofItem {
        animation: rise var(--dur-enter) var(--ease-out) backwards;
    }

    .fieldIntro .heroTitle .title-line { animation-delay: calc(1 * var(--step)); }
    .fieldIntro .heroActions { animation-delay: calc(2 * var(--step)); }
    .fieldIntro .heroProofLine { animation-delay: calc(3 * var(--step)); }
    .fieldIntro .proofItem:nth-child(1) { animation-delay: calc(3 * var(--step)); }
    .fieldIntro .proofItem:nth-child(2) { animation-delay: calc(4 * var(--step)); }
    .fieldIntro .proofItem:nth-child(3) { animation-delay: calc(5 * var(--step)); }

    /* Inner pages: label, title and deck. */
    :is(.articleHero, .researchHero, .reportHeroCopy) > :nth-child(-n + 3):not([style*="view-transition-name"]) {
        animation: rise 400ms var(--ease-out) var(--rise-delay, 0ms) backwards;
    }

    :is(.articleHero, .researchHero, .reportHeroCopy) > :nth-child(2) { --rise-delay: 60ms; }
    :is(.articleHero, .researchHero, .reportHeroCopy) > :nth-child(3) { --rise-delay: 120ms; }

    /* E3. A caret after the claim fades in, breathes three times, then fades out. */
    .claimCaret {
        display: inline-block;
        width: 2px;
        height: .9em;
        margin-left: 4px;
        vertical-align: -0.08em;
        background: currentColor;
        opacity: 0;
        animation: caret-life 4.6s linear 500ms 1;
    }

    @keyframes caret-life {
        0% { opacity: 0; }
        5%, 15% { opacity: 1; }
        30% { opacity: .3; }
        45% { opacity: 1; }
        60% { opacity: .3; }
        75% { opacity: 1; }
        90% { opacity: .3; }
        100% { opacity: 0; }
    }

    /* E4. The Ask launcher arrives after the page has settled. */
    .findAskLauncher {
        animation: launcher-in 400ms var(--ease-spring) 600ms backwards;
    }

    @keyframes launcher-in {
        from { opacity: 0; scale: .92; }
    }
}

/* ---------- R1–R10. Responses. Colour steps are for everyone; movement is for pointers
   and for visitors who allow motion. ---------- */
.workCard {
    transition: background-color var(--dur-hover) var(--ease-standard);
}

.workListTitle,
.logEntry a {
    transition: color var(--dur-hover) var(--ease-standard);
}

@media (hover: hover) {
    .workCard:hover {
        background-color: var(--surface-2);
    }

    .workList > li:hover .workListTitle {
        color: var(--lab-accent);
    }
}

.workCard:has(.workLink:focus-visible) {
    background-color: var(--surface-2);
}

@media (prefers-reduced-motion: no-preference) {
    .workArt,
    .workLink > span,
    .questMark {
        transition: scale var(--dur-hover) var(--ease-out), translate var(--dur-hover) var(--ease-out);
    }

    .workLink,
    .workList a,
    .heroTextLink,
    .logEntry a {
        transition: text-underline-offset var(--dur-hover) var(--ease-standard), text-decoration-color var(--dur-hover) var(--ease-standard), color var(--dur-hover) var(--ease-standard), scale var(--dur-press) var(--ease-out);
    }

    .workLink:active {
        scale: .98;
    }

    [data-theme-toggle],
    #darkModeToggle {
        transition: scale var(--dur-press) var(--ease-out), border-color var(--dur-hover) var(--ease-standard);
    }

    :is([data-theme-toggle], #darkModeToggle):active {
        scale: .94;
    }

    /* R5. After the visitor toggles the theme, the icon settles in. Never on page load.
       Some icons are replaced and some are redrawn in place, so each theme names its own
       (identical) keyframes: the change of name restarts the animation either way. */
    .did-toggle :is([data-theme-toggle], #darkModeToggle) svg {
        animation: icon-in-day var(--dur-hover) var(--ease-out) backwards;
    }

    .did-toggle body.dark-mode :is([data-theme-toggle], #darkModeToggle) svg {
        animation-name: icon-in-night;
    }

    @keyframes icon-in-day {
        from { opacity: 0; scale: .9; filter: blur(2px); }
    }

    @keyframes icon-in-night {
        from { opacity: 0; scale: .9; filter: blur(2px); }
    }

    /* R4. Header links: an underline grows from the left. */
    .researchNavLinks a {
        position: relative;
    }

    .researchNavLinks a::after {
        content: "";
        position: absolute;
        inset: auto 0 10px 0;
        height: 1px;
        background: currentColor;
        transform-origin: 0 50%;
        scale: 0 1;
        transition: scale var(--dur-hover) var(--ease-out);
    }

    /* R6. Collapsible sections: the height animates where the browser can, and the content fades in. */
    .resumeDetails {
        interpolate-size: allow-keywords;
    }

    .resumeDetails::details-content {
        block-size: 0;
        overflow: clip;
        overflow-clip-margin: 8px; /* room for focus rings */
        opacity: 0;
        transition: block-size var(--dur-ui) var(--ease-out), opacity var(--dur-ui) var(--ease-out), content-visibility var(--dur-ui) allow-discrete;
    }

    .resumeDetails[open]::details-content {
        block-size: auto;
        opacity: 1;
    }

    /* R8. The Ask dialog opens from the launcher's corner. Where the browser can keep it in the
       top layer while it leaves (overlay), it also fades out, a little faster. */
    .chatOverlay[open] {
        transform-origin: 100% 100%;
        transition: opacity var(--dur-ui) var(--ease-out), scale var(--dur-ui) var(--ease-out);
    }

    .chatOverlay[open]::backdrop {
        transition: opacity var(--dur-ui) var(--ease-out);
    }

    @supports (overlay: auto) {
        .chatOverlay {
            transform-origin: 100% 100%;
            opacity: 0;
            scale: .96;
            transition: opacity 160ms var(--ease-out), scale 160ms var(--ease-out), display 160ms allow-discrete, overlay 160ms allow-discrete;
        }

        .chatOverlay[open] {
            opacity: 1;
            scale: 1;
            transition: opacity var(--dur-ui) var(--ease-out), scale var(--dur-ui) var(--ease-out), display var(--dur-ui) allow-discrete, overlay var(--dur-ui) allow-discrete;
        }

        .chatOverlay::backdrop {
            opacity: 0;
            transition: opacity 160ms var(--ease-out), display 160ms allow-discrete, overlay 160ms allow-discrete;
        }

        .chatOverlay[open]::backdrop {
            opacity: 1;
            transition: opacity var(--dur-ui) var(--ease-out), display var(--dur-ui) allow-discrete, overlay var(--dur-ui) allow-discrete;
        }
    }

    /* Starting styles come last: they add no specificity, so a later open rule would override them. */
    @starting-style {
        .chatOverlay[open] {
            opacity: 0;
            scale: .96;
        }

        .chatOverlay[open]::backdrop {
            opacity: 0;
        }
    }

    /* R10. Figures on reports: the frame takes the accent on hover. */
    .reportShell figure {
        transition: background-color var(--dur-hover) var(--ease-standard);
    }

    @media (hover: hover) {
        .workCard:hover .workArt {
            scale: 1.02;
        }

        .workCard:hover .workLink > span,
        .workList > li:hover a {
            translate: 3px 0;
        }

        .logEntry:hover .questMark {
            translate: 2px 0;
        }

        :is(.workLink, .workList a, .heroTextLink, .logEntry a):hover {
            text-underline-offset: 5px;
        }

        .researchNavLinks a:hover::after {
            scale: 1 1;
        }

        .reportShell figure:hover {
            background-color: var(--surface-2);
        }
    }
}

/* The theme changes at once: while it switches, nothing fades (Rauno Freiberg's interface guidelines).
   js/motion.js holds this class for two frames around the toggle. */
body,
body.dark-mode,
.theme-switching *,
.theme-switching *::before,
.theme-switching *::after {
    transition: none !important;
}

/* A featured title hugs its text in every mode, so its handover box matches the words. */
.workTitle {
    width: fit-content;
}

/* 3. The homepage brand keeps its full width while its letters collapse, so the centred links stay put. */
.navbar-brand .wordmark {
    display: inline-block;
    min-width: 4.6em;
}
