:root {
    color-scheme: dark;
    --bg: #0b0f14;
    --fg: #e6eef7;
    --muted: #8aa0b4;
    --accent: #4cc2ff;
    --accent-2: #2a90ff;
    --track: rgba(255, 255, 255, 0.08);
    --danger: #ff7a7a;
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    width: 100vw;
    height: 100vh;
    /* dvh — dynamic viewport height: учитывает адрес-бар iOS/Android, */
    /* чтобы Unity не резало нижнюю полоску при его появлении. */
    height: 100dvh;
    background: var(--bg);
    color: var(--fg);
    font-family: system-ui, -apple-system, Segoe UI, Roboto, "Helvetica Neue", Arial, sans-serif;
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

#stage {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    display: block;
}

#unity-canvas {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    display: block;
    background: #000;
    outline: none;
    touch-action: none;
    image-rendering: auto;
}

#loader {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: clamp(12px, 2.2vh, 18px);
    padding: 24px;
    background: radial-gradient(1200px 700px at 50% 30%, #10202e 0%, #0b0f14 70%);
    transition: opacity 280ms ease;
    overflow: hidden;
    --star-x: 0px;
    --star-y: 0px;
    --planet-a-x: 0px;
    --planet-a-y: 0px;
    --planet-b-x: 0px;
    --planet-b-y: 0px;
    --planet-c-x: 0px;
    --planet-c-y: 0px;
    --portal-x: 0px;
    --portal-y: 0px;
}

#loader::before {
    content: "";
    position: absolute;
    inset: 0;
    background:
        linear-gradient(180deg, rgba(255, 255, 255, 0.035), transparent 18%),
        radial-gradient(900px 380px at 50% 38%, rgba(80, 230, 210, 0.11), transparent 70%);
    pointer-events: none;
}

#loader::after {
    content: "";
    position: absolute;
    inset: 0;
    background:
        linear-gradient(180deg, transparent 0%, rgba(0, 0, 0, 0.2) 100%),
        repeating-linear-gradient(180deg, rgba(255, 255, 255, 0.018) 0 1px, transparent 1px 4px);
    opacity: 0.7;
    animation: bg-panel-pulse 6.4s ease-in-out infinite;
    pointer-events: none;
}

#loader-bg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: none;
    opacity: 0.78;
    pointer-events: none;
}

#loader-space {
    position: absolute;
    inset: 0;
    overflow: hidden;
    pointer-events: none;
}

#loader-space::before,
#loader-space::after {
    content: "";
    position: absolute;
    inset: -35%;
    background-repeat: repeat;
    animation: star-drift 42s linear infinite;
}

#loader-space::before {
    background-image:
        radial-gradient(circle, rgba(255, 255, 255, 0.9) 0 1px, transparent 1.6px),
        radial-gradient(circle, rgba(137, 239, 255, 0.82) 0 1px, transparent 1.7px),
        radial-gradient(circle, rgba(255, 218, 130, 0.7) 0 1px, transparent 1.5px);
    background-size: 84px 84px, 132px 132px, 186px 186px;
    background-position: 12px 22px, 48px 78px, 130px 24px;
    opacity: 0.64;
}

#loader-space::after {
    background-image:
        radial-gradient(circle, rgba(255, 255, 255, 0.72) 0 1px, transparent 1.4px),
        radial-gradient(circle, rgba(93, 214, 255, 0.6) 0 1.2px, transparent 1.8px);
    background-size: 220px 220px, 310px 310px;
    background-position: 22px 90px, 170px 40px;
    opacity: 0.42;
    animation-duration: 70s;
    animation-direction: reverse;
}

.space-planet {
    position: absolute;
    z-index: 1;
    display: block;
    border-radius: 50%;
    filter: drop-shadow(0 0 28px rgba(76, 194, 255, 0.24));
    animation: planet-float 9s ease-in-out infinite;
    --planet-x: 0px;
    --planet-y: 0px;
}

.space-planet-a {
    --planet-x: var(--planet-a-x);
    --planet-y: var(--planet-a-y);
    left: max(44px, 8vw);
    top: max(46px, 9vh);
    width: clamp(88px, 10vw, 142px);
    height: clamp(88px, 10vw, 142px);
    background:
        radial-gradient(circle at 31% 26%, #adfff2 0 7%, transparent 8%),
        radial-gradient(circle at 34% 30%, #55f1d7 0 18%, #1285a8 48%, #07162d 100%);
}

.space-planet-b {
    --planet-x: var(--planet-b-x);
    --planet-y: var(--planet-b-y);
    right: max(48px, 9vw);
    top: max(58px, 12vh);
    width: clamp(58px, 6.8vw, 94px);
    height: clamp(58px, 6.8vw, 94px);
    background:
        radial-gradient(circle at 34% 27%, #ffe7a4 0 12%, #ffbf57 36%, #9a4f3d 72%, #180b18 100%);
    animation-delay: -2.4s;
}

.space-planet-b::before {
    content: "";
    position: absolute;
    left: -38%;
    top: 38%;
    width: 176%;
    height: 24%;
    border: 2px solid rgba(255, 221, 146, 0.58);
    border-left-color: rgba(255, 221, 146, 0.18);
    border-right-color: rgba(255, 221, 146, 0.22);
    border-radius: 50%;
    transform: rotate(-18deg);
}

.space-planet-c {
    --planet-x: var(--planet-c-x);
    --planet-y: var(--planet-c-y);
    right: max(70px, 13vw);
    bottom: max(52px, 9vh);
    width: clamp(70px, 8vw, 112px);
    height: clamp(70px, 8vw, 112px);
    background:
        radial-gradient(circle at 33% 28%, #9bd0ff 0 10%, #4d83ff 40%, #1c2f80 74%, #090d24 100%);
    opacity: 0.82;
    animation-delay: -5.2s;
}

#loader.done {
    opacity: 0;
    pointer-events: none;
}

#loader-portal {
    position: relative;
    z-index: 1;
    width: clamp(168px, 28vmin, 232px);
    height: clamp(168px, 28vmin, 232px);
    display: grid;
    place-items: center;
    padding: 0;
    border: 0;
    background: transparent;
    cursor: pointer;
    outline: none;
    animation: portal-float 4.8s ease-in-out infinite;
}

#loader-portal::before {
    content: "";
    position: absolute;
    inset: 12%;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(87, 255, 196, 0.22), rgba(76, 194, 255, 0.08) 48%, transparent 70%);
    filter: blur(10px);
    animation: portal-breathe 2.8s ease-in-out infinite;
}

#loader-portal::after {
    content: "";
    position: absolute;
    inset: 18%;
    border-radius: 50%;
    border: 1px solid rgba(116, 232, 255, 0.36);
    box-shadow: 0 0 22px rgba(76, 194, 255, 0.3), inset 0 0 18px rgba(87, 255, 196, 0.18);
    animation: portal-scan 2.2s ease-in-out infinite;
}

#loader-portal.is-pulsing {
    animation: portal-pop 420ms ease-out, portal-float 4.8s ease-in-out 420ms infinite;
}

.portal-ring {
    position: absolute;
    border-radius: 50%;
    pointer-events: none;
}

.portal-ring-outer {
    inset: 6%;
    border: 2px solid rgba(76, 194, 255, 0.2);
    border-top-color: rgba(118, 246, 255, 0.95);
    border-right-color: rgba(87, 255, 196, 0.64);
    filter: drop-shadow(0 0 14px rgba(76, 194, 255, 0.44));
    animation: portal-spin 7s linear infinite;
}

.portal-ring-inner {
    inset: 20%;
    border: 1px dashed rgba(255, 207, 91, 0.48);
    border-left-color: rgba(118, 246, 255, 0.9);
    animation: portal-spin 9s linear infinite reverse;
}

.portal-symbol {
    position: absolute;
    z-index: 2;
    color: rgba(160, 239, 255, 0.72);
    font: 700 14px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
    text-shadow: 0 0 10px rgba(76, 194, 255, 0.66);
    pointer-events: none;
}

.portal-symbol-1 {
    left: 7%;
    top: 24%;
    animation: portal-symbol-drift 5.6s ease-in-out infinite;
}

.portal-symbol-2 {
    right: 12%;
    top: 18%;
    animation: portal-symbol-drift 6.2s ease-in-out 0.8s infinite reverse;
}

.portal-symbol-3 {
    right: 7%;
    bottom: 24%;
    animation: portal-symbol-drift 5.8s ease-in-out 1.4s infinite;
}

#loader-icon {
    position: relative;
    z-index: 3;
    width: clamp(118px, 20vmin, 168px);
    height: clamp(118px, 20vmin, 168px);
    display: block;
    border-radius: 24%;
    filter: drop-shadow(0 0 18px rgba(76, 194, 255, 0.38));
    animation: icon-glow 3.4s ease-in-out infinite;
}

#loader-title {
    position: relative;
    z-index: 1;
    font-size: clamp(20px, 3vw, 32px);
    font-weight: 600;
    letter-spacing: 0.02em;
    color: var(--fg);
    text-align: center;
    padding: 0 24px;
}

#loader-bar {
    position: relative;
    z-index: 1;
    width: min(360px, 72vw);
    height: 6px;
    border-radius: 999px;
    background: var(--track);
    overflow: hidden;
}

#loader-fill {
    width: 0;
    height: 100%;
    background: linear-gradient(90deg, var(--accent), var(--accent-2));
    transition: width 120ms linear;
}

#loader-hint {
    position: relative;
    z-index: 1;
    font-size: 13px;
    color: var(--muted);
    letter-spacing: 0.04em;
}

#error {
    position: absolute;
    inset: 0;
    margin: auto;
    max-width: 520px;
    height: fit-content;
    padding: 28px 28px 24px;
    text-align: center;
    background: rgba(20, 26, 34, 0.95);
    border: 1px solid rgba(255, 122, 122, 0.25);
    border-radius: 14px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    align-items: center;
    justify-content: center;
}

#error h2 {
    margin: 0;
    color: var(--danger);
    font-size: 18px;
}

#error p {
    margin: 0;
    color: var(--muted);
    font-size: 14px;
    word-break: break-word;
}

#error .error-actions {
    display: flex;
    gap: 10px;
    margin-top: 8px;
    flex-wrap: wrap;
    justify-content: center;
}

#error .error-actions button {
    padding: 10px 18px;
    background: var(--accent);
    color: #001220;
    border: 0;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
}

#error .error-actions button:hover {
    background: var(--accent-2);
    color: #fff;
}

#error .error-actions button.secondary {
    background: rgba(255, 255, 255, 0.06);
    color: var(--muted);
    border: 1px solid rgba(255, 255, 255, 0.12);
}

#error .error-actions button.secondary:hover {
    background: rgba(255, 255, 255, 0.12);
    color: #e8edf5;
}

#error-close {
    position: absolute;
    top: 8px;
    right: 10px;
    width: 28px;
    height: 28px;
    padding: 0;
    background: transparent;
    color: var(--muted);
    border: 0;
    border-radius: 6px;
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
}

#error-close:hover {
    background: rgba(255, 255, 255, 0.08);
    color: #fff;
}

@media (max-width: 520px) {
    #loader {
        justify-content: center;
        gap: 13px;
        padding: 20px 18px;
        background: radial-gradient(520px 420px at 50% 38%, #102633 0%, #0b0f14 72%);
    }

    #loader-bg {
        opacity: 0.68;
    }

    .space-planet-a {
        left: -24px;
        top: 74px;
        width: 92px;
        height: 92px;
    }

    .space-planet-b {
        right: -12px;
        top: 92px;
        width: 58px;
        height: 58px;
    }

    .space-planet-c {
        right: 20px;
        bottom: 76px;
        width: 72px;
        height: 72px;
    }

    #loader-portal {
        width: clamp(174px, 48vw, 198px);
        height: clamp(174px, 48vw, 198px);
    }

    #loader-icon {
        width: clamp(124px, 34vw, 144px);
        height: clamp(124px, 34vw, 144px);
    }

    .portal-symbol {
        font-size: 12px;
    }

    .portal-symbol-1 {
        left: 5%;
        top: 28%;
    }

    .portal-symbol-2 {
        right: 7%;
        top: 22%;
    }

    .portal-symbol-3 {
        right: 5%;
        bottom: 28%;
    }

    #loader-title {
        max-width: 92vw;
        font-size: clamp(18px, 5.3vw, 22px);
        line-height: 1.15;
    }

    #loader-bar {
        width: min(268px, 70vw);
    }
}

@keyframes portal-float {
    0%, 100% {
        transform: translate3d(var(--portal-x), var(--portal-y), 0);
    }
    50% {
        transform: translate3d(var(--portal-x), calc(var(--portal-y) - 8px), 0);
    }
}

@keyframes bg-panel-pulse {
    0%, 100% {
        opacity: 0.56;
    }
    50% {
        opacity: 0.76;
    }
}

@keyframes star-drift {
    from {
        transform: translate3d(var(--star-x), var(--star-y), 0);
    }
    to {
        transform: translate3d(calc(var(--star-x) - 90px), calc(var(--star-y) + 120px), 0);
    }
}

@keyframes planet-float {
    0%, 100% {
        transform: translate3d(var(--planet-x), var(--planet-y), 0);
    }
    50% {
        transform: translate3d(var(--planet-x), calc(var(--planet-y) - 12px), 0);
    }
}

@keyframes portal-breathe {
    0%, 100% {
        opacity: 0.7;
        transform: scale(0.92);
    }
    50% {
        opacity: 1;
        transform: scale(1.08);
    }
}

@keyframes portal-scan {
    0%, 100% {
        opacity: 0.45;
        transform: scale(0.94);
    }
    50% {
        opacity: 0.85;
        transform: scale(1.04);
    }
}

@keyframes portal-spin {
    to {
        transform: rotate(360deg);
    }
}

@keyframes portal-symbol-drift {
    0%, 100% {
        opacity: 0.38;
        transform: translate3d(0, 0, 0);
    }
    50% {
        opacity: 0.95;
        transform: translate3d(0, -10px, 0);
    }
}

@keyframes icon-glow {
    0%, 100% {
        filter: drop-shadow(0 0 18px rgba(76, 194, 255, 0.38));
    }
    50% {
        filter: drop-shadow(0 0 30px rgba(87, 255, 196, 0.5));
    }
}

@keyframes portal-pop {
    0% {
        transform: translate3d(var(--portal-x), var(--portal-y), 0) scale(1);
    }
    42% {
        transform: translate3d(var(--portal-x), var(--portal-y), 0) scale(1.08);
    }
    100% {
        transform: translate3d(var(--portal-x), var(--portal-y), 0) scale(1);
    }
}
