/*
 * The dock: the one navigation every page shares.
 *
 * Wide screens with a pointer: a floating bar on the left edge. Hovering or
 * focusing an icon slides its name out beside it; the sword at the top pins
 * every name open.
 *
 * Phones and touch screens: a bar along the bottom, within thumb reach, with
 * each name under its icon. A site with more than six pages shows the first
 * four and the sword opens the rest.
 *
 * States come straight from the server's flags:
 *   .is-active   <- *IconAlwaysActive   the page you are on
 *   .is-locked   <- *IconDisabled       unavailable while you are in a game
 *   .haiku-glow  <- set by script       something new to read
 */
@layer components {
    .dock {
        position: fixed;
        z-index: 50;
        left: var(--dock-gap);
        top: 50%;
        transform: translateY(-50%);
    }

    .dock__bar {
        display: flex;
        flex-direction: column;
        gap: var(--s-1);
        padding: var(--s-2);
        border-radius: 20px;
        background: color-mix(in oklab, var(--bg-raise) 72%, transparent);
        -webkit-backdrop-filter: blur(16px) saturate(1.4);
        backdrop-filter: blur(16px) saturate(1.4);
        box-shadow:
            0 0 0 1px rgb(245 212 112 / 0.22),
            inset 0 1px 0 0 rgb(255 255 255 / 0.06),
            var(--shadow);
    }

    .dock__list {
        display: flex;
        flex-direction: column;
        gap: var(--s-1);
    }

    .dock__link,
    .dock__toggle {
        position: relative;
        display: flex;
        align-items: center;
        min-width: 48px;
        height: 48px;
        border-radius: 14px;
        color: var(--ink);
        outline-offset: 0;
        transition: background-color var(--fast) var(--ease);
    }

    .dock__toggle {
        width: 100%;
        margin-bottom: var(--s-1);
    }

    /* the hairline under the sword */
    .dock__toggle::after {
        content: "";
        position: absolute;
        left: 10px;
        right: 10px;
        bottom: -3px;
        height: 1px;
        background: var(--line);
    }

    .dock__icon {
        position: relative;
        flex: none;
        display: grid;
        place-items: center;
        width: 48px;
        height: 48px;
        border-radius: 14px;
        transition:
            transform var(--fast) var(--ease),
            box-shadow var(--fast) var(--ease),
            background-color var(--fast) var(--ease);
    }

    /* Most icons are drawn at 36px. One (Downloads) is 35px: it keeps its
       own pixels in the corner of the 36px box rather than being stretched
       by one. */
    .dock__icon img {
        width: 36px;
        height: 36px;
        object-fit: none;
        object-position: left top;
    }

    .dock__link:hover .dock__icon,
    .dock__link.is-held .dock__icon,
    .dock__link:focus-visible .dock__icon,
    .dock__toggle:hover .dock__icon {
        background: rgb(115 169 206 / 0.16);
        transform: translateY(-2px);
    }

    /* An icon may bring a second drawing for when it is pointed at (the
       Downloads button lights up). Both are in the page from the start and
       sit on top of each other, so the change is instant and never waits on
       a download, which a script swapping the src could not promise. */
    .dock__icon > img {
        grid-area: 1 / 1;
    }

    .dock__icon-hover {
        visibility: hidden;
    }

    .dock__link:hover .dock__icon-hover,
    .dock__link.is-held .dock__icon-hover,
    .dock__link:focus-visible .dock__icon-hover,
    .dock__link:active .dock__icon-hover {
        visibility: visible;
    }

    .dock__toggle .dock__icon img {
        transition: transform var(--slow) var(--ease);
    }

    .dock.is-open .dock__toggle .dock__icon img {
        transform: rotate(-90deg);
    }

    /* ---- the name ---- */

    .dock__label {
        position: absolute;
        left: calc(100% + 18px);
        top: 50%;
        display: grid;
        gap: 6px;
        padding: 9px 12px 8px;
        font-family: var(--font-pixel);
        font-size: 10px;
        line-height: 1;
        letter-spacing: 0.04em;
        text-transform: uppercase;
        white-space: nowrap;
        color: var(--gold);
        background: var(--bg-deep);
        box-shadow:
            0 calc(var(--px) * -1) 0 0 var(--gold),
            0 var(--px) 0 0 var(--gold),
            calc(var(--px) * -1) 0 0 0 var(--gold),
            var(--px) 0 0 0 var(--gold),
            var(--shadow);
        opacity: 0;
        transform: translate(-8px, -50%);
        pointer-events: none;
        transition:
            opacity var(--fast) var(--ease),
            transform var(--fast) var(--ease);
    }

    /* a page may give a shorter name for the phone's bottom bar */
    .dock__short {
        display: none;
    }

    .dock__label small {
        font-family: var(--font-sans);
        font-size: 11px;
        font-weight: 500;
        letter-spacing: 0;
        text-transform: none;
        color: var(--muted);
    }

    .dock__link:hover .dock__label,
    .dock__link.is-held .dock__label,
    .dock__link:focus-visible .dock__label,
    .dock__toggle:hover .dock__label,
    .dock__toggle:focus-visible .dock__label {
        opacity: 1;
        transform: translate(0, -50%);
    }

    /* while the sword switches between pinned and hover names, nothing fades */
    .dock.is-switching .dock__label {
        transition: none;
    }

    /* The button just clicked is still under the pointer when the next page
       arrives, but a browser only finds that out a moment after it draws, so
       the button would drop its hover look and then take it up again. The
       arriving page marks it .is-held (see UiDockBoot) and it is drawn hovered
       from the first frame, without animating into it. */
    .dock__link.is-held,
    .dock__link.is-held .dock__icon,
    .dock__link.is-held .dock__label {
        transition: none;
    }

    /* pinned open: every name sits beside its icon */
    .dock.is-open .dock__link .dock__label,
    .dock.is-open .dock__toggle .dock__label {
        position: static;
        padding: 0 var(--s-4) 0 var(--s-2);
        background: none;
        box-shadow: none;
        color: var(--ink);
        opacity: 1;
        transform: none;
    }

    /* A locked page's note ("Locked while you are in a game") would make
       the pinned dock, and so the room the page gives it, a third wider
       for the length of a game. Pinned, the padlock says it; the note is
       kept for screen readers, as the link's name. */
    .dock.is-open .dock__link .dock__label small {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }

    .dock.is-open .dock__link:hover,
    .dock.is-open .dock__link.is-held,
    .dock.is-open .dock__link:focus-visible,
    .dock.is-open .dock__toggle:hover {
        background: rgb(115 169 206 / 0.1);
    }

    /* ---- states ---- */

    .dock__link.is-active .dock__icon {
        background: radial-gradient(circle at 50% 35%, rgb(47 142 195 / 0.5), rgb(0 66 114 / 0.35));
        box-shadow:
            0 0 0 2px var(--gold),
            0 0 22px -2px rgb(245 212 112 / 0.55);
    }

    .dock.is-open .dock__link.is-active .dock__label {
        color: var(--gold);
    }

    .dock__link.is-locked {
        cursor: not-allowed;
    }

    .dock__link.is-locked .dock__icon img {
        filter: grayscale(1) brightness(0.7);
        opacity: 0.5;
    }

    /* the padlock is drawn at 64px: shown at exactly half, over the corner */
    .dock__link.is-locked .dock__icon::after {
        content: "";
        position: absolute;
        right: -6px;
        bottom: -6px;
        width: 32px;
        height: 32px;
        background: var(--lock-img) center / contain no-repeat;
        image-rendering: pixelated;
    }

    .dock__link.is-locked:hover .dock__icon {
        background: none;
        transform: none;
    }

    .dock__link.is-locked .dock__label {
        color: var(--muted);
        box-shadow:
            0 calc(var(--px) * -1) 0 0 var(--line-strong),
            0 var(--px) 0 0 var(--line-strong),
            calc(var(--px) * -1) 0 0 0 var(--line-strong),
            var(--px) 0 0 0 var(--line-strong),
            var(--shadow);
    }

    /* something new: a gold ring that breathes (the haiku notification) */
    /* The script that adds this also sets an animation-delay from the clock,
       so the ring is at the same point of its breath on every page instead
       of starting again, at its brightest, each time a page opens. */
    /* The ring is also the icon's resting style, so a visitor who has asked
       for reduced motion (where the animation is cut to nothing) still sees
       a steady ring. */
    .dock__icon.haiku-glow,
    .dock__icon.is-new {
        box-shadow:
            0 0 0 2px rgb(255 220 115 / 0.95),
            0 0 14px 3px rgb(255 220 115 / 0.5);
        animation: dock-ping 2s ease-in-out infinite;
    }

    @keyframes dock-ping {
        0%,
        100% {
            box-shadow:
                0 0 0 2px rgb(255 220 115 / 0.95),
                0 0 14px 3px rgb(255 220 115 / 0.5);
        }
        50% {
            box-shadow:
                0 0 0 2px rgb(255 220 115 / 0),
                0 0 14px 3px rgb(255 220 115 / 0);
        }
    }
}

/* ---------- pinned open: the page makes room ---------- */

/* With every name shown the dock is two to three times as wide. On a wide
   screen that costs nothing: the page sits in a centred column well clear of
   it, and must not move. On a narrower one the open dock would lie over the
   left of the page, so the page gives way, by exactly as much as it takes to
   clear the dock and no more.

   --dock-open is the open dock's real width, measured by ui.js and
   UiDockBoot (the names differ per site); 232px stands in until they have.
   The middle term is the room that puts the column's left edge at the dock's
   right edge: twice the dock, less the space the screen has beyond the
   column. It is clamped between the usual room and the whole dock.
   Not on phones, where .is-open is the sheet. */
@media (min-width: 768px) and (hover: hover), (min-width: 768px) and (pointer: fine) {
    @layer components {
        html:not(.ui-hidden) body:has(.dock.is-open) .shell:not(.shell--bare) {
            --dock-left: clamp(
                calc(var(--dock-size) + var(--dock-gap) * 2),
                calc(var(--dock-open, 232px) * 2 - 100vw + var(--container)),
                var(--dock-open, 232px)
            );
        }

        /* the game fills the whole width, so there it always gives way */
        html:not(.ui-hidden) body:has(.dock.is-open) .shell--game {
            --dock-left: var(--dock-open, 232px);
        }
    }
}

/* ---------- a long dock on a short screen ---------- */

/* hresh.io lists twelve pages. On a laptop-height window the cells tighten;
   on a very short one the list runs in two columns. Wide screens with a
   pointer only: phones use the bottom bar below. */
@media (min-width: 768px) and (hover: hover) and (max-height: 820px) {
    @layer components {
        .dock__list:has(> :nth-child(9)) {
            gap: 0;
        }

        .dock__list:has(> :nth-child(9)) .dock__link,
        .dock__list:has(> :nth-child(9)) .dock__icon {
            height: 42px;
        }
    }
}

@media (min-width: 768px) and (hover: hover) and (max-height: 640px) {
    @layer components {
        .dock:not(.is-open) .dock__list:has(> :nth-child(9)) {
            display: grid;
            grid-auto-flow: column;
            grid-template-rows: repeat(6, auto);
        }

        .dock__label {
            z-index: 2;
        }

        /* Pinned open, the names sit beside their icons in one column, which
           can be taller than the window; it scrolls rather than lose the
           sword (the way to unpin it) off the top. */
        .dock.is-open .dock__bar {
            max-height: calc(100dvh - var(--dock-gap) * 2);
            overflow-y: auto;
        }

        /* two columns need more room beside the page */
        body:has(.dock__list > :nth-child(9)) .shell {
            --dock-left: 148px;
        }
    }
}

/* ---------- phones and touch screens: bottom bar ---------- */

@media (max-width: 767px), (hover: none) and (pointer: coarse) {
    @layer components {
        .dock {
            left: 10px;
            right: 10px;
            top: auto;
            bottom: calc(10px + env(safe-area-inset-bottom, 0px));
            transform: none;
        }

        /* Page content scrolls underneath the bar here, so it is nearly
           solid; on the desktop edge it can stay glassy. */
        .dock__bar {
            flex-direction: row;
            align-items: flex-end;
            padding: 6px;
            background: color-mix(in oklab, var(--bg-raise) 94%, transparent);
        }

        .dock__list {
            flex: 1;
            flex-direction: row;
            gap: 2px;
        }

        .dock__list > li {
            flex: 1 1 0;
            min-width: 0;
        }

        .dock__link,
        .dock__toggle {
            flex-direction: column;
            justify-content: center;
            gap: 2px;
            height: auto;
            min-width: 0;
            padding: 2px 0 6px;
        }

        .dock__icon {
            width: 44px;
            height: 40px;
        }

        /* the name is always shown under the icon */
        .dock__link .dock__label,
        .dock__toggle .dock__label,
        .dock.is-open .dock__link .dock__label,
        .dock.is-open .dock__toggle .dock__label {
            position: static;
            display: block;
            max-width: 100%;
            padding: 0;
            overflow: hidden;
            text-overflow: ellipsis;
            font-family: var(--font-sans);
            font-size: 10px;
            font-weight: 600;
            letter-spacing: -0.02em;
            text-transform: none;
            color: var(--muted);
            background: none;
            box-shadow: none;
            opacity: 1;
            transform: none;
        }

        /* The long name and the locked note are hidden only from sight: the
           short name is aria-hidden, so these are what a screen reader reads
           as the link's name. display:none would leave it with none. */
        .dock__label small,
        .dock__long {
            position: absolute;
            width: 1px;
            height: 1px;
            overflow: hidden;
            clip-path: inset(50%);
            white-space: nowrap;
        }

        .dock__short {
            display: inline;
        }

        .dock__link.is-active .dock__label {
            color: var(--gold);
        }

        .dock__link:hover .dock__icon,
        .dock__toggle:hover .dock__icon {
            transform: none;
        }

        /* Six pages or fewer fit across; the sword has nothing to open. */
        .dock__toggle {
            display: none;
            flex: 0 0 56px;
            width: auto;
            margin: 0;
        }

        .dock__toggle::after {
            display: none;
        }

        /* More than six: show four, and the sword opens the rest as a grid. */
        .dock__bar:has(.dock__list > :nth-child(7)) .dock__toggle {
            display: flex;
            order: 2;
        }

        .dock:not(.is-open) .dock__list:has(> :nth-child(7)) > :nth-child(n + 5) {
            display: none;
        }

        /* The page being shown may be one of those tucked away. The sword
           then wears the mark, so the bar still says where the visitor is. */
        .dock:not(.is-open) .dock__bar:has(.dock__list > :nth-child(7)):has(.dock__list > :nth-child(n + 5) .is-active) .dock__toggle .dock__icon {
            background: radial-gradient(circle at 50% 35%, rgb(47 142 195 / 0.5), rgb(0 66 114 / 0.35));
            box-shadow:
                0 0 0 2px var(--gold),
                0 0 22px -2px rgb(245 212 112 / 0.55);
        }

        .dock:not(.is-open) .dock__bar:has(.dock__list > :nth-child(7)):has(.dock__list > :nth-child(n + 5) .is-active) .dock__toggle .dock__label {
            color: var(--gold);
        }

        .dock.is-open .dock__list:has(> :nth-child(7)) {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            row-gap: var(--s-2);
        }
    }
}

/* the narrowest phones: six names share 300px, so they are set a size down
   ("Downloads" would otherwise be cut short) */
@media (max-width: 359px) {
    @layer components {
        .dock__link .dock__label,
        .dock__toggle .dock__label,
        .dock.is-open .dock__link .dock__label,
        .dock.is-open .dock__toggle .dock__label {
            font-size: 9px;
            letter-spacing: -0.05em;
        }

        .dock__bar {
            padding-inline: 4px;
        }

        .dock__list {
            gap: 1px;
        }
    }
}
