.glyph {
    width: var(--icon-size-small);
    height: var(--icon-size-small);
    flex-shrink: 0;
    background: currentColor;
    mask: var(--icon) center / contain no-repeat;
    -webkit-mask: var(--icon) center / contain no-repeat;

    &.refresh { --icon: url("assets/icons/refresh.svg"); }
    &.theme { --icon: url("assets/icons/sun.svg"); }
    &.search { --icon: url("assets/icons/search.svg"); }
    &.user { --icon: url("assets/icons/user.svg"); }
    &.grid { --icon: url("assets/icons/grid.svg"); }
    &.list { --icon: url("assets/icons/list.svg"); }
    &.external { --icon: url("assets/icons/external.svg"); }
    &.more { --icon: url("assets/icons/more.svg"); }
    &.heart { --icon: url("assets/icons/heart.svg"); }
    &.check { --icon: url("assets/icons/check.svg"); }
    &.error { --icon: url("assets/icons/alert.svg"); }
    &.close { --icon: url("assets/icons/close.svg"); }
}

body {
    display: grid;
    grid-template: auto minmax(0, 1fr) auto / minmax(0, 1fr);
    overflow: hidden;
}

/* Header */

#header {
    box-sizing: content-box;
    width: 100%;
    height: calc(var(--unit) * 14);
    display: flex;
    flex-direction: row;
    align-items: stretch;
    border-bottom: var(--border-width) solid var(--border);

    & > div {
        display: flex;
        flex-direction: row;
        align-items: center;
        padding: 0 var(--padding-large);
    }

    & > #tabs-area {
        flex: 1;
        min-width: 0;
        position: relative;
        padding: 0;
        --fade-size: calc(var(--unit) * 32);
        --fade-direction: to right;

        &::before,
        &::after {
            content: "";
            position: absolute;
            top: 0;
            bottom: 0;
            width: var(--fade-size);
            pointer-events: none;
            opacity: 0;
            transition: opacity 0.2s;
        }

        &::before {
            inset-inline-start: 0;
            background: linear-gradient(var(--fade-direction), var(--bg), transparent);
        }

        &::after {
            inset-inline-end: 0;
            background: linear-gradient(var(--fade-direction), transparent, var(--bg));
        }

        &::before {
            inset-inline-start: var(--home-width, 0);   /* the Kancamu tab stays where it is, so the fade starts after it */
        }

        &:has(> #tabs.fade-start)::before,
        &:has(> #tabs.fade-end)::after {
            opacity: 1;
        }
    }

    & #tabs {
        flex: 1;
        min-width: 0;
        height: 100%;
        display: flex;
        flex-direction: row;
        align-items: stretch;
        overflow-x: auto;
        overflow-y: hidden;
        scrollbar-width: none;
        scroll-padding-inline-start: var(--home-width, 0);   /* a tab that is brought into view does not stay under the Kancamu tab */

        & > .tab {
            height: 100%;
            flex-shrink: 0;
            display: flex;
            flex-direction: row;
            align-items: center;
            border-inline-end: var(--border-width) solid var(--border);
            font-size: var(--font-size-medium);
            font-weight: 700;
            transition: background 0.2s, padding-inline-end 0.2s;

            &:hover {
                background: color-mix(in srgb, var(--color) 7%, var(--bg));   /* the colour of its project, lighter than the tab that is shown (12%) */
            }

            /* A file is over the tab, and the app of the tab takes it */
            &.drop-target {
                background: var(--bg-1);
                box-shadow: inset 0 0 0 var(--border-width-thick) var(--color);
            }

            /* The Kancamu tab does not scroll with the others, and cannot be dragged */
            &.home {
                position: sticky;
                inset-inline-start: 0;
                z-index: 2;
                background: var(--bg);

                &:hover {
                    background: color-mix(in srgb, var(--color) 7%, var(--bg));
                }
            }

            &:not(.home) > .select {
                cursor: grab;
            }

            /* The tab that is being dragged follows the pointer */
            &.dragging {
                z-index: 3;
                background: var(--bg-1);
                cursor: grabbing;
                transition: none;

                & > .select {
                    cursor: grabbing;
                }
            }

            /* The tab that is shown is tinted with the colour of its project (opaque, because the Kancamu tab stays over the others) */
            &.active,
            &.active:hover {
                background: color-mix(in srgb, var(--color) 12%, var(--bg));
                box-shadow: inset 0 calc(var(--border-width-thick) * -1) 0 var(--color);
            }

            & .highlight {
                color: var(--color);
            }

            &:is(:hover, :focus-within, .active):has(> .close) {
                padding-inline-end: var(--padding-small);

                & > .select {
                    padding-inline-end: var(--padding-small);
                }
            }

            & > .select {
                transition: padding-inline-end 0.2s;
                height: 100%;
                max-width: calc(var(--unit) * 80);
                gap: var(--gap-small);
                border: none;
                background: none;
                font-size: inherit;
                font-weight: inherit;
                text-transform: none;

                &:hover {
                    background: none;
                }

                & > .app-icon {
                    width: var(--icon-size-medium);
                    height: var(--icon-size-medium);
                    flex-shrink: 0;
                    background-size: contain;
                    background-position: center;
                    background-repeat: no-repeat;
                    image-rendering: pixelated;
                }

                & > .name {
                    overflow: hidden;
                    white-space: nowrap;
                    text-overflow: ellipsis;
                }
            }

            /* The close button shows on hover, on keyboard focus, and on the tab that is active; otherwise it has no width, so the tab is narrower. The width changes smoothly. */
            & > .close {
                width: 0;
                height: var(--icon-size-large);
                padding: 0;
                border-width: 0;
                border-radius: var(--border-radius-small);
                overflow: hidden;
                opacity: 0;
                transition: width 0.2s, opacity 0.2s, background 0.2s, color 0.2s;

                &:hover {   /* closing is a red thing: a light red tint behind the cross */
                    background: color-mix(in srgb, var(--danger) 20%, transparent);
                    color: var(--danger);
                }
            }

            &:is(:hover, :focus-within, .active) > .close {
                width: var(--icon-size-large);
                opacity: 1;
            }
        }
    }

    & > .header-tools {
        flex-shrink: 0;
        align-items: stretch;
        padding: 0;
        border-inline-start: var(--border-width) solid var(--border);

        & > :is(select, button) {
            height: 100%;
            border: none;
            background-color: transparent;
            transition: background-color 0.2s;

            &:hover {
                background-color: var(--bg-1);
            }
        }

        & > :is(#theme-button, #account-button) {
            width: calc(var(--unit) * 14);
            padding: 0;
            border-inline-start: var(--border-width) solid var(--border);
        }
    }
}

:root[data-theme="light"] .glyph.theme {
    --icon: url("assets/icons/moon.svg");
}

[hidden] {
    display: none !important;
}

/* Main: the viewer of the tabs, and the projects */

#main {
    width: 100%;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

#viewer {
    width: 100%;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;

    & > iframe {
        width: 100%;
        flex: 1;
        border: none;
        background: var(--bg);
    }
}

/* The projects (shown on the first page, instead of the viewer) */

#home {
    width: 100%;
    height: auto;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: var(--gap-medium);
    padding: var(--padding-large) var(--padding-extra-large);
    overflow-y: auto;
}

#toolbar {
    width: 100%;
    height: var(--height);
    flex-shrink: 0;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: var(--gap-small);

    & > .spacer {
        flex: 1;
    }
}

select {
    width: calc(var(--unit) * 45);
}

input {
    width: calc(var(--unit) * 70);
}

/* What touches another element is square. */
#header :is(button, select),
#view-toggle button {
    border-radius: 0;
}

#view-toggle {
    height: var(--height);
    display: flex;
    flex-direction: row;

    & > button {
        width: var(--height);
        padding: 0;

        &.active {
            background: var(--fg);
            color: var(--bg);
        }
    }
}

.searchbox {
    position: relative;
    display: flex;
    align-items: center;

    & > .glyph {
        position: absolute;
        inset-inline-start: calc(var(--unit) * 4);
        color: var(--fg-1);
        pointer-events: none;
    }

    & > input {
        padding-inline-start: calc(var(--unit) * 14);
    }
}

button {
    height: var(--height);
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    gap: var(--gap-small);
    cursor: pointer;
    font-size: var(--font-size-small);
    font-weight: bold;
    text-transform: uppercase;
    transition: background 0.2s, color 0.2s, border-color 0.2s;
}

#reset-button {
    color: var(--danger);
    background: color-mix(in srgb, var(--danger) 10%, var(--bg));
    border-color: color-mix(in srgb, var(--danger) 40%, var(--border));

    &:hover {
        background: color-mix(in srgb, var(--danger) 20%, var(--bg));
        border-color: var(--danger);
    }

    &[hidden] {
        display: none;
    }
}

/* The home and the footer are made by the script (the buttons, the list, and the texts come from data.json), and their parts change size while they are made. Until the page is made
   (body.ready, in app.js) they are hidden, so nothing that is seen jumps; hidden parts are not counted as a layout shift. If the script does not finish, they show after 3 seconds. */
body:not(.ready) :is(#home, #footer) {
    visibility: hidden;
    animation: show-late 0s 3s forwards;
}

@keyframes show-late {
    to {
        visibility: visible;
    }
}

/* Category filter */

#category-filter {
    width: 100%;
    flex-shrink: 0;
    min-height: calc(var(--unit) * 10);   /* the room of one row of buttons is kept before they are made, so the list does not jump down */
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: var(--gap-small);

    & > button {
        width: auto;
        height: calc(var(--unit) * 10);
        color: var(--fg);
        font-size: var(--font-size-extra-small);

        &.active {
            background: var(--fg);
            color: var(--bg);
        }

        &[data-category="official"] {
            border-color: var(--yellow);

            color: color-mix(in srgb, var(--yellow) 85%, var(--fg));

            &.active {
                background: var(--yellow);
                color: var(--on-color);
            }
        }

        &[data-category="mobile"] {
            border-color: var(--blue);
            color: color-mix(in srgb, var(--blue) 85%, var(--fg));

            &.active {
                background: var(--blue);
                color: var(--on-color);
            }
        }

        &[data-category="education"] {
            border-color: var(--green);
            color: color-mix(in srgb, var(--green) 85%, var(--fg));

            &.active {
                background: var(--green);
                color: var(--on-color);
            }
        }
    }
}

/* Project list */

#project-list {
    width: 100%;
    height: auto;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, calc(var(--unit) * 100)), 1fr));
    align-items: stretch;
    border-block-start: var(--border-width) solid var(--border);
    border-inline-start: var(--border-width) solid var(--border);

    &:has(> .empty-state) {
        border: none;
    }

    &.list {
        --list-columns: 1fr calc(var(--unit) * 60) calc(var(--unit) * 80) calc(var(--unit) * 50);
        grid-template-columns: 1fr;

        & > .list-header {
            width: 100%;
            height: var(--height);
            display: grid;
            grid-template-columns: var(--list-columns);
            align-items: center;
            gap: var(--gap-small);
            padding: 0 var(--padding-large);
            border-inline-end: var(--border-width) solid var(--border);
            border-block-end: var(--border-width) solid var(--border);
            color: var(--fg-1);
            font-size: var(--font-size-small);
            text-transform: uppercase;
        }

        & > .project {
            display: grid;
            grid-template-columns: var(--list-columns);
            align-items: center;
            gap: var(--gap-small);
            padding: var(--padding-medium) var(--padding-large);

            &:hover {
                box-shadow: inset var(--border-width-thick) 0 0 var(--color);
            }

            & > .project-header {
                display: contents;

                & > .project-publisher {
                    order: 2;
                }

                & > .project-link {
                    display: none;
                }
            }

            & > .project-body {
                order: 1;
            }

            & > .project-categories {
                order: 3;
            }

            & > .project-actions {
                order: 4;
            }

            & > :is(.project-body, .project-categories, .project-actions) {
                padding-inline: 0;
            }
        }
    }

    & > .empty-state {
        grid-column: 1 / -1;
    }
}

.project {
    width: 100%;
    height: auto;
    display: flex;
    flex-direction: column;
    gap: var(--gap-medium);
    padding: 0 0 var(--padding-large);
    border-inline-end: var(--border-width) solid var(--border);
    border-block-end: var(--border-width) solid var(--border);
    transition: background 0.2s, box-shadow 0.2s;

    &:hover {
        background: color-mix(in srgb, var(--color) 8%, transparent);
        box-shadow: inset 0 var(--border-width-thick) 0 var(--color);
    }

    & > .project-header {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        padding: var(--padding-medium) var(--padding-large);
        border-bottom: var(--border-width) solid var(--border);
        font-size: var(--font-size-small);
        font-weight: bold;
        text-transform: uppercase;

        & > .project-publisher {
            color: color-mix(in srgb, var(--color) 85%, var(--fg));   /* the colour of the project, as small text: enough contrast */
        }

        & > .project-link {
            display: flex;
            flex-direction: row;
            align-items: center;
            gap: var(--gap-small);
            color: var(--fg-1);
            text-decoration: none;

            &:hover {
                color: var(--fg);
            }
        }
    }

    & > .project-body {
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: var(--gap-small);
        padding: 0 var(--padding-large);
        flex: 1;

        & > .app-icon {
            width: var(--icon-size-extra-large);
            height: var(--icon-size-extra-large);
            flex-shrink: 0;
            border-radius: var(--border-radius-medium);
            background-size: contain;
            background-position: center;
            background-repeat: no-repeat;
            image-rendering: pixelated;
        }

        & > .project-text {
            display: flex;
            flex-direction: column;
            gap: var(--gap-extra-small);

            & > .name {
                font-size: var(--font-size-large);
                font-weight: 700;

                & > .highlight {
                    color: var(--color);
                }
            }

            & > .description {
                color: var(--fg-1);
                font-size: var(--font-size-small);
            }
        }
    }

    & > .project-categories {
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        gap: var(--gap-small);
        padding: 0 var(--padding-large);

        &:empty {
            display: none;
        }

        & > .category {
            padding: var(--padding-extra-small) var(--padding-small);
            border: var(--border-width) solid var(--border);
            border-radius: var(--border-radius-small);
            color: var(--fg);
            font-size: var(--font-size-extra-small);
            text-transform: none;

            &[data-category="official"] {
                border-color: var(--yellow);
                color: color-mix(in srgb, var(--yellow) 85%, var(--fg));
            }

            &[data-category="mobile"] {
                border-color: var(--blue);
                color: color-mix(in srgb, var(--blue) 85%, var(--fg));
            }

            &[data-category="education"] {
                border-color: var(--green);
                color: color-mix(in srgb, var(--green) 85%, var(--fg));
            }

            /* The category that is filtered: filled, like its button in the filter (Official yellow, Mobile blue, Education green) */
            &.active {
                background: var(--fg);
                border-color: var(--fg);
                color: var(--bg);
            }

            &.active[data-category="official"] {
                background: var(--yellow);
                border-color: var(--yellow);
                color: var(--on-color);
            }

            &.active[data-category="mobile"] {
                background: var(--blue);
                border-color: var(--blue);
                color: var(--on-color);
            }

            &.active[data-category="education"] {
                background: var(--green);
                border-color: var(--green);
                color: var(--on-color);
            }
        }
    }

    & > .project-actions {
        width: 100%;
        height: var(--height);
        display: grid;
        grid-template-columns: 1fr var(--height);
        gap: var(--gap-small);
        padding: 0 var(--padding-large);

        & > button {
            width: 100%;
            padding: 0;
        }

        & > .project-open-button {
            &:hover {
                background: var(--fg);
                color: var(--bg);
            }
        }
    }
}

/* Right-to-left and non-Latin scripts */

:root[dir="rtl"] #tabs-area {
    --fade-direction: to left;
}

:root[dir="rtl"] #project-list.list > .project:hover {
    box-shadow: inset calc(var(--border-width-thick) * -1) 0 0 var(--color);
}

/* Footer */

#footer {
    width: 100%;
    height: var(--height);
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 0 var(--padding-extra-large);
    border-top: var(--border-width) solid var(--border);
    color: var(--fg-1);
    font-size: var(--font-size-small);
}

/* The buttons of the footer: the pages About and Terms and conditions */
#footer :is(button, a.button) {
    flex: none;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: var(--gap-small);
    height: 100%;
    border: 0;
    border-radius: 0;
    background: transparent;
    color: inherit;
    font-size: inherit;
    font-weight: 400;
    text-transform: none;
}

#footer :is(button, a.button):hover {
    color: var(--fg);
}

/* The text of the support link is yellow, so it stands out in the footer (the hover keeps it yellow) */
#footer #support-link,
#footer #support-link:hover {
    color: var(--yellow);
}

#about-button {
    margin-inline-start: auto;   /* the buttons are at the end of the footer */
}

/* A page of the main: the text, one part under the other */
#page {
    width: 100%;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: var(--gap-medium);
    padding: var(--padding-large) var(--padding-extra-large);
    overflow-y: auto;
}

.page-back { flex: none; }
.page-title { max-width: calc(var(--unit) * 200); font-size: var(--font-size-extra-large); font-weight: 700; }
.page-updated { max-width: calc(var(--unit) * 200); color: var(--fg-1); font-size: var(--font-size-extra-small); }
.page-heading { max-width: calc(var(--unit) * 200); font-size: var(--font-size-large); font-weight: 700; }
.page-text { max-width: calc(var(--unit) * 200); color: var(--fg-1); line-height: 1.5; }
.page-links { display: flex; flex-direction: column; gap: var(--gap-small); }
.page-link { color: var(--fg); text-decoration: underline; }

@media (max-width: 640px) {
    #home, #page {
        padding: var(--padding-medium);
    }

    /* On a narrow screen the footer is tighter, and the support link keeps only its heart (a square, with its name in the title) */
    #footer { padding: 0 var(--padding-medium); }
    #support-link { width: var(--height); padding: 0; justify-content: center; }
    #support-link > span { display: none; }

    #project-list.list {
        --list-columns: 1fr calc(var(--unit) * 50);

        & > .list-header > :is(.publisher, .category),
        & > .project > .project-header > .project-publisher,
        & > .project > .project-categories {
            display: none;
        }
    }

    #toolbar {
        flex-wrap: wrap;
        height: auto;

        & > .spacer {
            display: none;
        }
    }
}

/* Controls that keep their size have a set width: a multiple of the height (RULES.md, Control Width) */
#reset-button {
    width: calc(var(--height) * 3);
}

/* Splash: a layer over the content for a moment when the site opens (it takes the click that closes it) */

.splash {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--padding-large);
    background: color-mix(in srgb, var(--bg) 85%, transparent);
    -webkit-backdrop-filter: blur(var(--unit));   /* a little blur of the page behind it */
    backdrop-filter: blur(var(--unit));
    transition: opacity 0.4s;
    cursor: pointer;

    &[hidden] { display: none; }
    &.leaving { opacity: 0; }
}

/* The card is 5:3 (wide), and 3:5 on a narrow screen (tall). Two parts are the brand and three parts are the picture, so the picture is a square in both. */
.splash-card {
    display: grid;
    grid-template-columns: minmax(0, 2fr) minmax(0, 3fr);
    aspect-ratio: 5 / 3;
    width: min(calc(var(--height) * 40 / 3), 100%, calc(var(--vh) * 80 * 5 / 3));   /* 13.33 heights wide, so 8 heights tall */
    overflow: hidden;
    background: var(--bg);
    border: var(--border-width-thick) solid var(--border);   /* the dark gray of the lines, 2 thick, so the card stands out from the page behind it */
    border-radius: var(--border-radius-extra-large);
    animation: splash-in 0.5s;
}

.splash-info {
    display: flex;
    flex-direction: column;
    gap: var(--gap-small);
    min-width: 0;
    padding: var(--padding-extra-large);
}

/* The brand (the mark and the name) and the tagline are at the top, and the hint and the copyright are at the bottom (the auto margin of the tagline takes the free height) */
.splash-brand {
    display: flex;
    align-items: center;
    gap: var(--gap-small);
}

.splash-mark {
    flex: none;
    width: var(--icon-size-large);   /* the mark of a logo, next to a name of --font-size-extra-large */
    height: var(--icon-size-large);
    background: url("favicon.png") center / contain no-repeat;
    image-rendering: pixelated;
}

.splash-name {
    font-size: var(--font-size-extra-large);
    font-weight: 700;
    line-height: 1;

    & .highlight { color: var(--highlight, var(--blue)); }
}

.splash-tagline { color: var(--fg-1); margin-block-end: auto; }
.splash-hint, .splash-copyright { font-size: var(--font-size-extra-small); color: var(--fg-1); }

/* The images of the splash (assets/images, the list of data.json, set by app.js): squares in a row that slides, covering their part of the card */
.splash-art {
    position: relative;
    overflow: hidden;
    background: var(--bg-1);
}

/* A gradient in the middle of the card: the card is 5 parts across (2 of the brand and 3 of the image), and the gradient is the third part, the one part that is in the middle,
   where the brand meets the image: the colour of the brand fades into the image. One part is a third of the image (calc(100% / 3)), which is 3 heights.
   On a narrow screen the card is 5 parts tall, and the gradient is the one part at the top of the image. */
.splash-art::after {
    content: "";
    position: absolute;
    inset-block: 0;
    inset-inline-start: 0;
    width: calc(100% / 3);
    background: linear-gradient(to right, var(--bg), transparent);
    pointer-events: none;
}

/* The author of the image that shows: in the top corner at the end of the image, on a dark ground so it can be read on any image */
.splash-credit {
    position: absolute;
    inset-block-start: var(--padding-medium);
    inset-inline-end: var(--padding-medium);
    max-width: calc(100% - var(--padding-medium) * 2);
    padding: var(--padding-extra-small) var(--padding-small);
    font-size: var(--font-size-extra-small);
    color: var(--white);
    background: color-mix(in srgb, var(--black) 55%, transparent);
    border-radius: var(--border-radius-medium);
    pointer-events: none;
}

/* The dots of the images: at the bottom of the image, one for each image, and the one of the image that shows is lit */
.splash-dots {
    position: absolute;
    inset-inline: 0;
    inset-block-end: var(--padding-medium);
    display: flex;
    justify-content: center;
    gap: var(--gap-small);
    pointer-events: none;
}

.splash-dot {
    width: calc(var(--unit) * 2);
    height: calc(var(--unit) * 2);
    background: color-mix(in srgb, var(--white) 40%, transparent);
    border-radius: var(--border-radius-large);
    transition: background 0.3s;

    &.on { background: var(--white); }
}

:root[dir="rtl"] .splash-art::after {
    background: linear-gradient(to left, var(--bg), transparent);
}

.splash-track {
    display: flex;
    height: 100%;

    &.moving { transition: transform 0.8s ease-in-out; }
}

.splash-slide {
    flex: none;
    width: 100%;
    height: 100%;
    background: center / cover no-repeat;
}

@keyframes splash-in {
    from { opacity: 0; transform: scale(0.96); }
}

@media (prefers-reduced-motion: reduce) {
    .splash, .splash-card, .splash-track { animation: none; transition: none; }
}

@media (max-width: 48rem) {
    .splash-card { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 2fr) minmax(0, 3fr); aspect-ratio: 3 / 5; width: min(calc(var(--height) * 7), 100%, calc(var(--vh) * 80 * 3 / 5)); }   /* 7 heights wide, so 11.67 heights tall */
    .splash-info { padding: var(--padding-large); }
    .splash-art::after, :root[dir="rtl"] .splash-art::after { inset: 0 0 auto; width: auto; height: calc(100% / 3); background: linear-gradient(to bottom, var(--bg), transparent); }
}
