/* Hellsick.UI: the look the hellsick.com games share. Plain CSS with no build step.

   Everything here is in a cascade layer, in this order (later wins, whatever the specificity):
     hs.vendor                                                  third-party CSS, such as Font Awesome (HsHead)
     hs.reset, hs.tokens, hs.base, hs.components, hs.utilities  this file, and the skins' default values
     game                                                       the app's own CSS: wrap it in @layer game { … }
     skin                                                       the skins' overrides, generated from C# (/_hs/skins.css)
   CSS outside any layer beats all of them, so an app's own stylesheet belongs in @layer game.

   Colours, fonts and surfaces are skin tokens (--hs-*), set from HsTokens in C#. The values below that a skin should
   not change (spacing, tap targets, safe areas) are fixed here. */

@layer hs.vendor, hs.reset, hs.tokens, hs.base, hs.components, hs.utilities, game, skin;

/* ── Reset ─────────────────────────────────────────────────────────────────── */
@layer hs.reset {
    *, ::before, ::after {
        box-sizing: border-box;
        border: 0 solid;
    }

    html {
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
        -webkit-tap-highlight-color: transparent;
        tab-size: 4;
    }

    body { margin: 0; }
    h1, h2, h3, h4, h5, h6, p, pre, figure, blockquote, dl, dd, hr { margin: 0; }
    ol, ul, menu { margin: 0; padding: 0; list-style: none; }
    b, strong { font-weight: bolder; }
    table { border-collapse: collapse; text-indent: 0; }
    dialog { padding: 0; }
    textarea { resize: vertical; }
    img, svg, video, canvas, audio, iframe, embed, object { display: block; vertical-align: middle; }
    img, video { max-width: 100%; height: auto; }
    [hidden] { display: none !important; }

    button, input, optgroup, select, textarea {
        margin: 0;
        padding: 0;
        color: inherit;
        font: inherit;
        letter-spacing: inherit;
        background-color: transparent;
    }

    button, [role="button"] { cursor: pointer; }
    :disabled { cursor: default; }

    /* Important in the first layer beats everything after it: motion is off everywhere for anyone who asked. */
    @media (prefers-reduced-motion: reduce) {
        *, ::before, ::after {
            animation-duration: 0.01ms !important;
            animation-iteration-count: 1 !important;
            transition-duration: 0.01ms !important;
            scroll-behavior: auto !important;
        }
    }
}

/* ── Fixed tokens: not part of a skin ──────────────────────────────────────── */
@layer hs.tokens {
    :root {
        --hs-space-1: 0.25rem;
        --hs-space-2: 0.5rem;
        --hs-space-3: 0.75rem;
        --hs-space-4: 1rem;
        --hs-space-5: 1.5rem;
        --hs-space-6: 2rem;

        /* The smallest thing a thumb should have to hit. */
        --hs-tap: 2.75rem;
        /* Side padding of a page column. */
        --hs-gutter: 1.25rem;
        /* The top bar's height, not counting the notch. */
        --hs-topbar-height: 3.125rem;

        /* The notch and home bar; zero where there are none. Needs viewport-fit=cover, which HsHead sets. */
        --hs-safe-top: env(safe-area-inset-top, 0px);
        --hs-safe-right: env(safe-area-inset-right, 0px);
        --hs-safe-bottom: env(safe-area-inset-bottom, 0px);
        --hs-safe-left: env(safe-area-inset-left, 0px);

        --hs-duration: 150ms;
        --hs-ease: ease;
    }
}

/* ── Base ──────────────────────────────────────────────────────────────────── */
@layer hs.base {
    :root {
        color-scheme: var(--hs-color-scheme);
    }

    body {
        background: var(--hs-page-backdrop), var(--hs-bg-page);
        color: var(--hs-text);
        font-family: var(--hs-font-body);
        line-height: 1.5;
    }

    h1, h2, h3, h4 {
        font-family: var(--hs-font-display);
        font-weight: 700;
        line-height: 1.2;
    }

    h1 { font-size: 2rem; }
    h2 { font-size: 1.5rem; }
    h3 { font-size: 1.25rem; }
    h4 { font-size: 1rem; }

    a { color: var(--hs-link); text-underline-offset: 0.15em; }

    code, kbd, samp, pre { font-family: var(--hs-font-mono); }

    :focus-visible {
        outline: 2px solid var(--hs-highlight);
        outline-offset: 2px;
    }

    /* A tap never waits to see whether it was the start of a double-tap zoom. */
    a, button, input, select, textarea, label, [role="button"] { touch-action: manipulation; }

    /* At 16px or more, iOS doesn't zoom the page when a field is focused, so the viewport needn't forbid zooming. */
    input, select, textarea { font-size: max(1rem, 16px); }

    ::placeholder { color: var(--hs-text-subtle); opacity: 1; }
}

/* ── Components ────────────────────────────────────────────────────────────── */
@layer hs.components {
    /* The page frame (HsShell): fills the screen, clears the notch and home bar, and keeps a footer at the bottom. */
    .hs-shell {
        display: flex;
        flex-direction: column;
        min-height: 100vh;
        min-height: 100dvh;
        padding: var(--hs-safe-top) var(--hs-safe-right) var(--hs-safe-bottom) var(--hs-safe-left);
        overscroll-behavior-y: none;
    }

    .hs-shell > footer, .hs-shell > .hs-footer { margin-top: auto; }

    /* The parallax backdrop (HsParallax). The page scrolls inside .hs-parallax; with perspective: 1px, a layer pushed
       back by translateZ(-depth) and scaled by (1 + depth) keeps its size but drifts slower than the content. Layer 0
       is drawn first (furthest back).

       Each layer reads its settings from variables, set per scene ([data-scene]) and per skin:
           --hs-parallax-l{N}-image      url(…) (required)
           --hs-parallax-l{N}-depth      how far back: 0 = moves with the page, bigger = slower (skies are about 40)
           --hs-parallax-l{N}-offset     pushes the layer down by a CSS length, e.g. calc(var(--hs-parallax-height) * 0.06)
           --hs-parallax-l{N}-size       background-size; default is the art box (width × width ÷ aspect)
           --hs-parallax-l{N}-position   background-position; default "center top"
       plus the scene-wide --hs-parallax-bg / -aspect / -min-width / -max-width and the base plate's -base-depth /
       -base-offset / -base-overlap, whose defaults are skin tokens (HsTokens). --hs-parallax-width and -height are the
       art box, worked out here, for offsets and sizes to use. A skin that leaves a layer unset gets the default skin's,
       so set its image to none to hide it. */
    .hs-parallax {
        --hs-parallax-width: clamp(var(--hs-parallax-min-width), 100vw, max(var(--hs-parallax-min-width), var(--hs-parallax-max-width)));
        --hs-parallax-height: calc(var(--hs-parallax-width) / var(--hs-parallax-aspect));
        position: relative;
        height: 100vh;
        height: 100dvh;
        overflow-x: hidden;
        overflow-y: auto;
        perspective: 1px;
        perspective-origin: top;
        background-color: var(--hs-parallax-bg);
    }

    .hs-parallax-layer {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: var(--hs-parallax-height);
        background-repeat: no-repeat;
        transform-origin: top;
        transform: translateZ(calc(var(--depth) * -1px)) scale(calc(1 + var(--depth))) translateY(var(--offset));
        pointer-events: none;
    }

    /* The base plate: drawn after the layers, so it covers the art's bottom edge and carries the scene colour on down
       the page, moving with the front-most layer. */
    .hs-parallax-base {
        --top: calc(var(--hs-parallax-height) + var(--hs-parallax-base-offset) - var(--hs-parallax-base-overlap));
        position: absolute;
        left: 0;
        right: 0;
        top: var(--top);
        height: 8192px;
        background-color: var(--hs-parallax-bg);
        /* Scale about the top of the view (like the layers), not the plate's own top edge, or it creeps up over the art. */
        transform-origin: 50% calc(var(--top) * -1);
        transform: translateZ(calc(var(--hs-parallax-base-depth) * -1px)) scale(calc(1 + var(--hs-parallax-base-depth)));
        pointer-events: none;
    }

    .hs-parallax-content {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
    }

    /* The bar fixed across the top of a game (HsTopBar). Its height, not counting the notch, is --hs-topbar-height,
       so a page can clear it; HsShell's padding already clears the notch. */
    .hs-topbar {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 40;
        min-height: calc(var(--hs-topbar-height) + var(--hs-safe-top));
        padding: calc(var(--hs-space-2) + var(--hs-safe-top)) calc(var(--hs-space-2) + var(--hs-safe-right))
            var(--hs-space-2) calc(var(--hs-space-2) + var(--hs-safe-left));
        border-bottom: 2px solid var(--hs-border);
        background-color: var(--hs-bg-card);
        background-image: var(--hs-panel-texture);
        box-shadow: var(--hs-shadow-lg);
    }

    .hs-topbar-inner {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: var(--hs-space-3);
    }

    /* The game's name with "a hellsick game" beside it, on the name's baseline. On a phone the tagline goes under the
       name instead, both on tighter lines, so the bar keeps its height and leaves room for the right-hand side; the
       icon then stands beside the two, as tall as both together. */
    .hs-topbar-brand {
        display: flex;
        align-items: baseline;
        flex-wrap: wrap;
        column-gap: var(--hs-space-3);
        min-width: 0;
    }

    .hs-topbar-home {
        display: flex;
        align-items: center;
        gap: var(--hs-space-2);
        color: var(--hs-text);
        font-size: 1.5rem;
        line-height: 2rem;
        text-decoration: none;

        & h1 { font-size: inherit; line-height: inherit; }
    }

    .hs-topbar-tagline {
        color: var(--hs-text-subtle);
        font-family: var(--hs-font-body);
        font-size: 0.75rem;
        line-height: 1rem;
        letter-spacing: 0.04em;
        text-decoration: none;
        white-space: nowrap;

        &:hover { color: var(--hs-link); text-decoration: underline; }
    }

    @media (max-width: 30rem) {
        .hs-topbar-brand { flex-direction: column; align-items: flex-start; }
        .hs-topbar-home { font-size: 1.25rem; line-height: 1.25rem; }
        .hs-topbar-tagline { font-size: 0.6875rem; line-height: 0.75rem; }

        /* The icon stays inside the home link, so it is still part of what you tap, but sits in the brand's padding. */
        .hs-topbar-brand-icon {
            position: relative;
            padding-left: calc(2rem + var(--hs-space-2));

            & .hs-topbar-home > i {
                position: absolute;
                left: 0;
                top: 0;
                width: 2rem;
                font-size: 2rem;
                line-height: 2rem;
                text-align: center;
            }
        }
    }

    .hs-topbar-actions {
        display: flex;
        align-items: center;
        gap: var(--hs-space-3);
    }

    /* Blazor's unhandled-error banner (HsErrorUi). Light and plain on purpose, whatever the skin: it has to be
       readable over anything, and it holds a stack trace someone may need to copy. Blazor shows it by setting
       display:block on it. */
    .hs-error-ui {
        position: fixed;
        bottom: 0;
        left: 0;
        z-index: 1000;
        display: none;
        width: 100%;
        max-height: 45vh;
        overflow-y: auto;
        padding: 0.6rem 1.25rem calc(0.7rem + var(--hs-safe-bottom));
        background: lightyellow;
        box-shadow: 0 -1px 2px rgb(0 0 0 / 0.2);
        color: #1b1b1b;
        color-scheme: light only;

        & .dismiss {
            position: absolute;
            top: 0.5rem;
            right: 0.75rem;
            cursor: pointer;
        }

        & .error-header {
            display: flex;
            align-items: baseline;
            gap: 0.75rem;
            padding-right: 1.5rem;
        }

        & .error-title { font-weight: 600; }

        & a { color: #1b1b1b; }

        & .copy {
            padding: 0.05rem 0.5rem;
            border: 1px solid rgb(0 0 0 / 0.25);
            border-radius: 0.25rem;
            background: rgb(0 0 0 / 0.06);
            color: #1b1b1b;
            font-size: 0.85em;

            &:hover { background: rgb(0 0 0 / 0.12); }
        }

        & pre {
            margin-top: 0.5rem;
            padding: 0.5rem 0.6rem;
            overflow-x: auto;
            border: 1px solid rgb(0 0 0 / 0.15);
            border-radius: 0.25rem;
            background: rgb(0 0 0 / 0.05);
            color: #1b1b1b;
            font-size: 0.78rem;
            line-height: 1.35;
            white-space: pre-wrap;
            word-break: break-word;
            user-select: text;
        }
    }

    /* The reconnect dialog (HsReconnectModal). Which lines show is driven by the classes Blazor puts on it. */
    .hs-reconnect {
        width: min(20rem, calc(100% - 2 * var(--hs-gutter)));
        margin: 20vh auto;
        padding: 2rem;
        border: 1px solid var(--hs-border);
        border-radius: var(--hs-radius-lg);
        background-color: var(--hs-bg-modal);
        box-shadow: var(--hs-shadow-lg);
        color: var(--hs-text);
        opacity: 0;
        transition: display 0.5s allow-discrete, overlay 0.5s allow-discrete;
        animation: hs-reconnect-fade-out 0.5s both;

        &[open] {
            animation: hs-reconnect-slide-up 1.5s cubic-bezier(.05, .89, .25, 1.02) 0.3s, hs-reconnect-fade-in 0.5s ease-in-out 0.3s;
            animation-fill-mode: both;
        }

        &::backdrop {
            background-color: rgb(0 0 0 / 0.5);
            animation: hs-reconnect-fade-in 0.5s ease-in-out;
        }

        & .components-reconnect-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 1rem;
        }

        & p { text-align: center; }

        &.components-reconnect-retrying, &.components-reconnect-failed { display: block; }

        & .components-reconnect-first-attempt-visible,
        & .components-reconnect-repeated-attempt-visible,
        & .components-reconnect-failed-visible,
        & .components-pause-visible,
        & .components-resume-failed-visible,
        & .components-rejoining-animation {
            display: none;
        }

        &.components-reconnect-show .components-reconnect-first-attempt-visible,
        &.components-reconnect-show .components-rejoining-animation,
        &.components-reconnect-paused .components-pause-visible,
        &.components-reconnect-resume-failed .components-resume-failed-visible,
        &.components-reconnect-retrying .components-reconnect-repeated-attempt-visible,
        &.components-reconnect-retrying .components-rejoining-animation,
        &.components-reconnect-failed .components-reconnect-failed-visible {
            display: block;
        }

        & .hs-btn.components-reconnect-failed-visible,
        & .hs-btn.components-pause-visible,
        & .hs-btn.components-resume-failed-visible { display: none; }

        &.components-reconnect-failed .hs-btn.components-reconnect-failed-visible,
        &.components-reconnect-paused .hs-btn.components-pause-visible,
        &.components-reconnect-resume-failed .hs-btn.components-resume-failed-visible { display: inline-flex; }

        & .components-rejoining-animation {
            position: relative;
            width: 80px;
            height: 80px;

            & div {
                position: absolute;
                border: 3px solid var(--hs-accent-1);
                border-radius: 50%;
                animation: hs-reconnect-ripple 1.5s cubic-bezier(0, 0.2, 0.8, 1) infinite;
            }

            & div:nth-child(2) { animation-delay: -0.5s; }
        }
    }

    @keyframes hs-reconnect-slide-up {
        from { transform: translateY(30px) scale(0.95); }
        to { transform: translateY(0); }
    }

    @keyframes hs-reconnect-fade-in {
        from { opacity: 0; }
        to { opacity: 1; }
    }

    @keyframes hs-reconnect-fade-out {
        from { opacity: 1; }
        to { opacity: 0; }
    }

    @keyframes hs-reconnect-ripple {
        0%, 4.9% { top: 40px; left: 40px; width: 0; height: 0; opacity: 0; }
        5% { top: 40px; left: 40px; width: 0; height: 0; opacity: 1; }
        100% { top: 0; left: 0; width: 80px; height: 80px; opacity: 0; }
    }

    .hs-card {
        background-color: var(--hs-bg-card);
        background-image: var(--hs-panel-texture);
        border: 1px solid var(--hs-border);
        border-radius: var(--hs-radius-lg);
        box-shadow: var(--hs-shadow);
    }

    .hs-btn {
        --hs-btn-bg: var(--hs-btn-neutral);
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: var(--hs-space-2);
        min-height: var(--hs-tap);
        padding: var(--hs-space-2) var(--hs-space-4);
        border: 1px solid transparent;
        border-radius: var(--hs-radius);
        background-color: var(--hs-btn-bg);
        color: var(--hs-btn-text);
        font-family: var(--hs-font-body);
        font-weight: 600;
        line-height: 1.2;
        text-decoration: none;
        transition: background-color var(--hs-duration) var(--hs-ease), border-color var(--hs-duration) var(--hs-ease);

        &:hover:not(:disabled) { background-color: color-mix(in srgb, var(--hs-btn-bg) 85%, white); }

        &:disabled {
            background-color: color-mix(in srgb, var(--hs-btn-bg) 50%, black);
            color: color-mix(in srgb, var(--hs-btn-text) 55%, transparent);
            cursor: not-allowed;
        }
    }

    .hs-btn-ok { --hs-btn-bg: var(--hs-btn-ok); }
    .hs-btn-alert { --hs-btn-bg: var(--hs-btn-alert); }
    .hs-btn-alternate { --hs-btn-bg: var(--hs-btn-alternate); }

    /* A quiet button: the card colour with a border that lights up. */
    .hs-btn-outline {
        --hs-btn-bg: var(--hs-bg-card);
        border-color: var(--hs-border);
        color: var(--hs-text);

        &:hover:not(:disabled), &:focus-visible {
            background-color: var(--hs-bg-card);
            border-color: var(--hs-accent-1);
        }
    }

    .hs-input {
        min-height: var(--hs-tap);
        padding: var(--hs-space-2) var(--hs-space-3);
        border: 1px solid var(--hs-border);
        border-radius: var(--hs-radius);
        background-color: var(--hs-bg-input);
        color: var(--hs-text-input);

        &:focus-visible { border-color: var(--hs-highlight); outline-offset: 0; }
        &[aria-invalid="true"], &.invalid { border-color: var(--hs-invalid); }
    }

    .hs-badge {
        display: inline-block;
        padding: 0.05rem 0.4rem;
        border: 1px solid var(--hs-accent-1);
        border-radius: var(--hs-radius-sm);
        color: var(--hs-accent-1);
        font-size: 0.75rem;
        line-height: 1.4;
        vertical-align: middle;
    }

    .hs-alert {
        padding: var(--hs-space-2) var(--hs-space-3);
        border: 1px solid var(--hs-alert-border);
        border-radius: var(--hs-radius);
        background-color: var(--hs-alert-bg);
        color: var(--hs-alert-text);
    }

    .hs-divider { border-top: 1px solid var(--hs-divider); }

    .hs-text-subtle { color: var(--hs-text-subtle); }
    .hs-text-accent { color: var(--hs-accent-1); }

    /* A readable column of text: the privacy and terms pages every app has. */
    .hs-prose {
        max-width: 42rem;
        line-height: 1.6;

        & p, & ul, & ol { margin-bottom: 1em; }
        & ul { padding-left: 1.25rem; list-style: disc; }
        & ol { padding-left: 1.25rem; list-style: decimal; }
        & li + li { margin-top: 0.5em; }
        & h2 { margin: 2rem 0 0.5rem; font-size: 1.25rem; }
        & h3 { margin: 1.5rem 0 0.5rem; font-size: 1.1rem; }
    }
}

/* ── Layout utilities ──────────────────────────────────────────────────────── */
/* A few layout shapes, each taking its spacing from a custom property instead of a class per value:
   <div class="hs-stack" style="--gap: var(--hs-space-3)">. Anything more particular is a class in the app's CSS. */
@layer hs.utilities {
    /* Top to bottom. */
    .hs-stack { display: flex; flex-direction: column; gap: var(--gap, var(--hs-space-3)); }

    /* Side by side, centred on one line. */
    .hs-row { display: flex; align-items: center; gap: var(--gap, var(--hs-space-2)); }

    /* Side by side, wrapping onto more lines as needed. */
    .hs-cluster { display: flex; flex-wrap: wrap; align-items: center; gap: var(--gap, var(--hs-space-2)); }

    /* As many columns as fit, each at least --min wide. */
    .hs-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(min(var(--min, 16rem), 100%), 1fr));
        gap: var(--gap, var(--hs-space-4));
    }

    /* A centred column, at most --max wide, with the page gutter at the sides. */
    .hs-center {
        width: 100%;
        max-width: var(--max, 60rem);
        margin-inline: auto;
        padding-inline: var(--hs-gutter);
    }

    .hs-visually-hidden {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }
}
