@font-face {
    font-family: 'Atkinson Hyperlegible';
    src: url('../../assets/AtkinsonHyperlegible-Regular.woff2') format('woff2');
}
@font-face {
    font-family: 'Atkinson Hyperlegible';
    font-style: italic;
    src: url('../../assets/AtkinsonHyperlegible-Italic.woff2') format('woff2');
}
@font-face {
    font-family: 'Atkinson Hyperlegible';
    font-weight: bold;
    src: url('../../assets/AtkinsonHyperlegible-Bold.woff2') format('woff2');
}
@font-face {
    font-family: 'Atkinson Hyperlegible';
    font-weight: bold;
    font-style: italic;
    src: url('../../assets/AtkinsonHyperlegible-BoldItalic.woff2') format('woff2');
}
@font-face {
    font-family: 'Source Code Pro';
    src: url('../../assets/SourceCodePro.woff2') format('woff2');
}
@font-face {
    font-family: 'League Mono';
    /* I don't know what version of League Mono this is, so this is stolen from cohost */
    src: url('../../assets/cohost-leaguemono.woff') format('woff');
    font-weight: 100 900;
    font-stretch: 50% 200%;
}

html {
    font-family: 'Atkinson Hyperlegible', system-ui, sans-serif;

    /* --P- variable prefix to avoid confusion with variables inherited from cohost */

    --P-monospace: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, liberation mono, courier new,
        monospace;
    --P-font-size-ui: 14px;
    --P-font-size-content: 16px;
    --P-font-size-code: 13px;

    --P-ease-default: cubic-bezier(0.2, 0.3, 0, 1);

    --P-color-bg: 221 221 221;
    --P-color-fg: 0 0 0;

    --P-color-accent: 121 96 223;
    --P-color-accent-fg: 255 255 255;
    --P-color-warning: 240 126 0;
    --P-color-danger: 255 68 51;
    --P-color-danger-fg: 255 255 255;
    --P-color-shade: var(--P-color-fg);
    --P-color-shade-opacity: 0.1;

    --P-color-soft-outline: var(--P-color-fg);
    --P-color-soft-outline-opacity: 0.2;
    --P-color-form-item-bg: var(--P-color-fg);
    --P-color-form-item-bg-opacity: 0.1;

    --P-cohost-bg: rgb(255 249 242);
    --P-cohost-dark-bg: rgb(25 25 25);
    --P-card-bg: 255 255 255;
    --P-danger: #f43;
    --P-danger-content-fg: white;
    --P-error: var(--P-danger);
    --P-error-text: var(--P-error);
    --P-error-content-fg: var(--P-danger-content-fg);
    --P-item-bg: #eee;
    --P-item-selected-bg: #ddd;
    --P-separator: rgba(0, 0, 0, 0.2);
    --P-dialog-bg: #eee;
    --P-dialog-outline: rgba(0, 0, 0, 0.2);
    --P-info-box-bg: rgba(0, 0, 0, 0.1);
    --P-graph-node-bg: rgba(0, 0, 0, 0.2);
    --P-graph-node-selected-bg: rgba(0, 0, 0, 0.3);
    --P-graph-connection: rgba(0, 0, 0, 0.7);
    --P-graph-connection-highlighted: rgb(var(--P-color-accent));
    --P-resizer-rim: #0002;

    --P-toolbar-bg: 17 17 17;
    --P-toolbar-fg: 238 238 238;
    --P-toolbar-separator: #333;
    --P-toolbar-fade-button-outline: #fff4;
    --P-toolbar-fade-button-hover: #fff2;
    --P-toolbar-fade-button-active: #fff4;
    --P-toolbar-tab-selected: #444;

    --P-sidebar-bg: #ddd;
    --P-sidebar-item-hover: #0002;
    --P-sidebar-rim: #0002;

    background: rgb(var(--P-color-bg));
    color: rgb(var(--P-color-fg));
    accent-color: rgb(var(--P-color-accent));
    user-select: none;

    /* stop iOS overscrolling */
    overflow: clip;
}

@media (prefers-color-scheme: dark) {
    html {
        color-scheme: dark;

        --P-color-bg: 34 34 34;
        --P-color-fg: 255 255 255;

        --P-card-bg: 51 51 51;
        --P-item-bg: rgba(255, 255, 255, 0.1);
        --P-item-selected-bg: rgba(255, 255, 255, 0.15);
        --P-separator: rgba(255, 255, 255, 0.2);
        --P-dialog-bg: #333;
        --P-dialog-outline: rgba(255, 255, 255, 0.5);
        --P-info-box-bg: rgba(255, 255, 255, 0.1);
        --P-graph-node-bg: rgba(255, 255, 255, 0.2);
        --P-graph-node-selected-bg: rgba(255, 255, 255, 0.3);
        --P-graph-connection: rgba(255, 255, 255, 0.5);
        --P-graph-connection-highlighted: white;
        --P-resizer-rim: #fff2;

        --P-sidebar-bg: #222;
        --P-sidebar-item-hover: #fff2;
        --P-sidebar-rim: #fff2;
    }
}

@keyframes P-script-not-executed {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}

#script-not-executed {
    margin: 1em auto;
    max-width: 56ch;
    animation: P-script-not-executed 2s 3s backwards;

    a {
        color: rgb(var(--P-color-accent));
    }
}

#prechoster-root {
    position: fixed;
    inset: 0;
}

.application-frame {
    position: absolute;
    display: flex;
    flex-direction: column;
    inset: 0;

    &.is-loading,
    &.is-error {
        align-items: center;
        justify-content: center;
        overflow: auto;
    }
    &.is-error > .i-error {
        background: rgb(var(--P-card-bg));
        border-radius: 0.5em;
        margin: 1em;
        padding: 1em;
    }

    > .i-toolbar {
        display: flex;
        background: rgb(var(--P-toolbar-bg));
        color: rgb(var(--P-toolbar-fg));
        border-bottom: 2px solid rgb(var(--P-color-accent));
        font-size: smaller;
        gap: 0.5em;
        height: 2.2em;
        padding: 0 0.5em;

        > .i-buttons {
            display: flex;
            flex-shrink: 0;

            > .i-spacer {
                width: 0.5em;
            }
            > .i-section {
                margin-left: 0.5em;
                border-left: 1px solid var(--P-separator);
                padding: 0 0.5em;
                display: flex;
                align-items: center;
                font-weight: bold;
                color: rgb(var(--P-toolbar-fg) / 0.7);
            }
        }

        > .i-memory-storage-warning {
            align-self: center;
            background: rgb(var(--P-color-danger));
            color: rgb(var(--P-color-danger-fg));
            padding: 0.3em;
            margin: 0;
            border: none;
            border-radius: 0.5em;

            &:active {
                opacity: 0.7;
            }
        }

        .i-memory-storage-warning-details {
            width: 90vw;
            max-width: 30em;
            padding: 1em;

            .i-title {
                font-size: 1.2em;
                font-weight: bold;
            }

            .i-error-title {
                margin-top: 1em;
                margin-bottom: 0.5em;
            }
            .i-error {
                margin: 0;
                padding: 0.5em;
                background: rgb(var(--P-card-bg));
                border-radius: 0.5em;
                overflow: auto;
            }
        }

        .i-save-formats-title {
            margin: 0.3em 0;
            text-align: center;
            font-size: 1.2em;
        }
        .i-save-formats {
            display: flex;
            padding: 0;
            margin: 0;
            list-style: none;
            border-top: 1px solid var(--P-separator);

            & li {
                flex: 1;

                & button {
                    background: none;
                    border: none;
                    color: inherit;
                    font: inherit;

                    &:active {
                        opacity: 0.5;
                    }
                }

                & + li {
                    border-left: 1px solid var(--P-separator);
                }
            }
        }
    }

    > .i-content-split {
        display: flex;
        height: 0;
        flex: 1;

        > .i-sidebar {
            display: none;

            &.is-open {
                display: flex;
                flex-direction: column;

                & > * {
                    height: 0;
                    flex: 1;
                }
            }
        }

        > .i-contents {
            display: grid;
            width: 0;
            flex: 1;
            grid-template-columns: 100%;
            grid-template-rows: 100%;

            .application-tab {
                grid-area: 1 / 1;
            }
        }
    }
}

.application-frame > .i-toolbar > .i-buttons > .i-toolbar-button {
    font: inherit;
    color: inherit;
    background: none;
    border: 1px solid var(--P-toolbar-fade-button-outline);
    border-radius: 0.5em;
    margin: 0.15em;
    padding: 0.2em 0.4em;
    transition: background 0.2s, border-color 0.2s;

    &.is-icon {
        padding: 0.1em;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    &:disabled {
        opacity: 0.5;
        border-color: transparent;

        & svg {
            opacity: 0.3;
        }
    }

    &:not(:disabled):hover {
        background: var(--P-toolbar-fade-button-hover);
    }

    &:not(:disabled):active {
        background: var(--P-toolbar-fade-button-active);
        transition: background 0.05s;
    }

    &.is-drop-highlighted {
        background: var(--P-toolbar-fade-button-active);
        animation: P-toolbar-button-drop-highlight 0.5s infinite;
    }
}

@keyframes P-toolbar-button-drop-highlight {
    0%,
    100% {
        border-color: currentColor;
    }
    50% {
        border-color: color-mix(in lab, currentColor, #7774);
    }
}

.application-tab {
    display: grid;
    position: relative;
    grid-template-columns: 100%;
    grid-template-rows: 100%;

    > .i-contents {
        display: grid;
        grid-area: 1 / 1;
        grid-template-columns: 100%;
        grid-template-rows: 100%;

        > * {
            grid-area: 1 / 1;
        }

        &.is-error {
            display: block;
            overflow: auto;
            padding: 1em;

            & pre {
                font-size: 0.8em;
                line-height: 1.5;

                > .i-line {
                    display: flex;
                    gap: 0.5em;

                    > .i-gutter {
                        text-align: right;
                        width: 4em;
                        opacity: 0.5;
                    }
                }
            }
        }

        > .i-loading {
            grid-area: 1 / 1;
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 1;

            > .i-loading-contents {
                background: var(--P-dialog-bg);
                padding: 1em;
                border-radius: 0.5em;
                display: flex;
                flex-direction: column;
                gap: 0.5em;
                align-items: center;

                > .i-loading-icon {
                    width: 2em;
                    height: 2em;
                    border: 4px solid rgb(var(--P-color-accent));
                    border-top-color: transparent;
                    border-radius: 50%;
                    animation: P-application-tab-loading-icon-spin 1s infinite linear;
                }
            }
        }
    }

    &.is-background {
        opacity: 0;
    }
    &.is-background,
    &.is-background * {
        visibility: hidden;
    }
}
@keyframes P-application-tab-loading-icon-spin {
    100% {
        transform: rotate(1turn);
    }
}

.application-tabs {
    display: flex;
    flex: 1;

    > .i-tab {
        display: flex;
        flex-direction: row-reverse;
        width: 0;
        background: none;
        color: inherit;
        font: inherit;
        border-left: 1px solid var(--P-toolbar-separator);
        border-right: none;
        flex-basis: 16ch;
        margin: 0;
        flex-shrink: 1;
        gap: 0.2em;
        padding: 0.2em 1.5em 0.2em 0.4em;
        align-items: center;
        cursor: default;
        transition: background 0.2s;

        &:last-child {
            border-right: 1px solid var(--P-toolbar-separator);
        }

        &.is-active {
            box-shadow: inset 0 -3px rgb(var(--P-color-accent));
            background: var(--P-toolbar-tab-selected);
        }
        &:not(.is-active):hover {
            background: var(--P-toolbar-fade-button-hover);
        }
        &:not(.is-active):active {
            background: var(--P-toolbar-fade-button-active);
            transition: background 0.05s;
        }

        > .i-close {
            background: none;
            border: none;
            color: inherit;
            font: inherit;
            flex-shrink: 0;
            padding: 0;
            margin: 0.1em;
            width: 1.3em;
            aspect-ratio: 1;
            border-radius: 0.2em;
            position: relative;
            opacity: 0.5;
            transition: all 0.2s;

            &::before,
            &::after {
                content: '';
                position: absolute;
                background: currentColor;
                border-radius: 1px;
                margin-left: -0.4em;
                margin-top: -0.5px;
                width: 0.8em;
                height: 1px;
                transform: rotate(45deg);
            }
            &::after {
                transform: rotate(-45deg);
            }

            &:hover {
                background: var(--P-toolbar-fade-button-hover);
                opacity: 0.7;
            }
            &:active {
                background: var(--P-toolbar-fade-button-active);
                opacity: 1;
                transition: background 0.05s;
            }
        }

        > .i-tab-interactable {
            flex: 1;
            width: 0;
            display: flex;

            > .i-title {
                flex: 1;
                text-align: center;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;

                > .i-virtual-title {
                    font-style: italic;
                }
            }
        }
    }
}

.application-no-open-document {
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: default;

    > .i-contents {
        display: grid;
        grid-template-rows: 1fr;
        place-items: center;
        text-align: center;
        padding: 1em;
        gap: 1em;
        animation: P-application-no-open-document-fade-in 6s 2s backwards;
        position: relative;

        & h3 {
            opacity: 0.7;
        }

        & img {
            width: 16em;
            max-width: 100%;
        }

        > .i-nothing {
            left: 50%;
            top: 64px;
            font-size: 1.5em;
            font-weight: bold;
            color: rgb(var(--P-color-fg) / 0.5);
        }

        .i-inline-icon {
            display: inline-block;
            vertical-align: middle;
            stroke-width: 1.7px;
        }
    }
}

@keyframes P-application-no-open-document-fade-in {
    0% {
        opacity: 0;
    }
}

.application-open-from-url {
    .i-dialog-contents {
        width: 80vw;
        max-width: 64ch;
        padding: 0.75em;

        > .i-title {
            margin: 0;
            font-size: 1.2em;
        }

        > .i-url {
            border: 1px solid var(--P-separator);
            border-radius: 0.5em;
            padding: 0.5em;
            margin: 0.5em 0;
            white-space: pre-wrap;
            overflow-wrap: break-word;
            user-select: text;
        }

        > .i-actions {
            display: flex;
            justify-content: flex-end;
            gap: 1em;
        }
    }
}
