* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
    background: #111;
    font-family: Arial, Helvetica, sans-serif;
}

canvas {
    display: block;
}

#hud {
    position: absolute;
    z-index: 10;
    left: 16px;
    top: 16px;
    min-width: 280px;
    padding: 12px 14px;
    color: #fff;
    background: rgba(16, 24, 30, 0.74);
    border: 1px solid rgba(255,255,255,.16);
    border-radius: 8px;
    line-height: 1.45;
    font-size: 14px;
    backdrop-filter: blur(6px);
    user-select: none;
    pointer-events: none;
}

.hud-title {
    font-weight: 700;
    margin-bottom: 5px;
}

#status {
    margin-top: 7px;
    color: #d7e7ef;
}

#crosshair {
    position: absolute;
    z-index: 9;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    color: rgba(255,255,255,.92);
    font: 22px/1 monospace;
    text-shadow: 0 0 4px #000;
    user-select: none;
    pointer-events: none;
}

#loading {
    position: absolute;
    z-index: 30;
    inset: 0;
    display: grid;
    place-items: center;
    background: #1b252d;
    color: white;
}

.loading-panel {
    width: min(440px, calc(100% - 40px));
}

.loading-title {
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 12px;
}

.loading-track {
    height: 8px;
    overflow: hidden;
    border-radius: 999px;
    background: rgba(255,255,255,.16);
}

#loading-bar {
    width: 0%;
    height: 100%;
    background: #d5b13f;
    transition: width .2s ease;
}

#loading-text {
    margin-top: 8px;
    color: #c9d4da;
    font-size: 13px;
}


/* =========================================================
   MOBILE / TOUCH CONTROLS
   ========================================================= */

.touch-help,
#mobile-controls {
    display: none;
}

body.touch-device {
    touch-action: none;
    overscroll-behavior: none;
    -webkit-user-select: none;
    user-select: none;
}

body.touch-device .desktop-help {
    display: none;
}

body.touch-device .touch-help {
    display: block;
}

body.touch-device #mobile-controls {
    display: block;
}

body.touch-device #hud {
    left: 10px;
    top: 10px;
    min-width: 0;
    max-width: min(330px, calc(100vw - 20px));
    padding: 9px 11px;
    font-size: 12px;
}

body.touch-device #crosshair {
    font-size: 20px;
}

/*
   Прозрачная зона обзора занимает правую половину экрана.
   Кнопки лежат поверх неё и перехватывают свои события.
*/
#look-zone {
    position: fixed;
    z-index: 20;
    top: 0;
    right: 0;
    width: 56vw;
    height: 100vh;
    touch-action: none;
}

/* Левый аналоговый джойстик */
#joystick {
    position: fixed;
    z-index: 24;
    left: max(18px, env(safe-area-inset-left));
    bottom: max(20px, calc(env(safe-area-inset-bottom) + 12px));
    width: 150px;
    height: 150px;
    touch-action: none;
}

#joystick-base {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 132px;
    height: 132px;
    transform: translate(-50%, -50%);
    border: 2px solid rgba(255,255,255,.46);
    border-radius: 50%;
    background: rgba(18, 27, 34, .30);
    box-shadow:
        inset 0 0 24px rgba(0,0,0,.22),
        0 4px 18px rgba(0,0,0,.18);
    backdrop-filter: blur(3px);
}

#joystick-knob {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 58px;
    height: 58px;
    margin-left: -29px;
    margin-top: -29px;
    border: 2px solid rgba(255,255,255,.72);
    border-radius: 50%;
    background: rgba(230, 238, 243, .45);
    box-shadow: 0 4px 12px rgba(0,0,0,.25);
    transform: translate(0px, 0px);
    will-change: transform;
}

/* Кнопки справа */
#mobile-buttons {
    position: fixed;
    z-index: 30;
    right: max(14px, env(safe-area-inset-right));
    bottom: max(16px, calc(env(safe-area-inset-bottom) + 8px));
    width: min(220px, 43vw);
    display: grid;
    grid-template-columns: repeat(2, minmax(64px, 1fr));
    gap: 9px;
    pointer-events: auto;
}

.mobile-btn {
    min-height: 52px;
    padding: 8px 7px;
    border: 1px solid rgba(255,255,255,.45);
    border-radius: 14px;
    color: #fff;
    background: rgba(20, 31, 39, .64);
    font: 700 12px/1 Arial, sans-serif;
    letter-spacing: .02em;
    box-shadow: 0 4px 14px rgba(0,0,0,.22);
    backdrop-filter: blur(5px);
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
}

.mobile-btn:active,
.mobile-btn.is-active {
    transform: scale(.96);
    background: rgba(91, 119, 135, .78);
}

.mobile-btn-primary {
    min-height: 62px;
    grid-row: span 2;
    font-size: 13px;
}

@media (max-width: 620px) {
    #joystick {
        width: 132px;
        height: 132px;
    }

    #joystick-base {
        width: 116px;
        height: 116px;
    }

    #joystick-knob {
        width: 52px;
        height: 52px;
        margin-left: -26px;
        margin-top: -26px;
    }

    #mobile-buttons {
        width: min(195px, 44vw);
        gap: 7px;
    }

    .mobile-btn {
        min-height: 48px;
        font-size: 11px;
    }

    .mobile-btn-primary {
        min-height: 56px;
    }
}

/*
   В альбомной ориентации HUD стараемся не закрывать сцену.
*/
@media (orientation: landscape) and (max-height: 520px) {
    body.touch-device #hud {
        max-width: 260px;
        font-size: 10px;
        line-height: 1.3;
    }

    body.touch-device #status {
        display: none;
    }

    #joystick {
        width: 122px;
        height: 122px;
        bottom: 10px;
    }

    #joystick-base {
        width: 108px;
        height: 108px;
    }

    #mobile-buttons {
        width: 184px;
        bottom: 10px;
    }

    .mobile-btn {
        min-height: 43px;
    }
}


/* =========================================================
   PROJECTOR WEB BROWSER
   ========================================================= */

#projector-browser {
    display: none;
    position: fixed;
    z-index: 1000;
    inset: 0;
    padding: 3vh 3vw;
    background: rgba(5, 8, 11, .78);
    box-sizing: border-box;
}

#projector-browser.is-open {
    display: flex;
    align-items: center;
    justify-content: center;
}

#projector-browser-window {
    width: min(1500px, 94vw);
    height: min(900px, 92vh);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid rgba(255,255,255,.30);
    border-radius: 12px;
    background: #111820;
    box-shadow: 0 18px 70px rgba(0,0,0,.55);
}

#projector-browser-bar {
    min-height: 48px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 7px 10px 7px 16px;
    color: #fff;
    background: #17232d;
    border-bottom: 1px solid rgba(255,255,255,.16);
    box-sizing: border-box;
}

#projector-browser-title {
    overflow: hidden;
    font: 700 14px/1.2 Arial, sans-serif;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.projector-browser-actions {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

.projector-browser-actions button {
    min-height: 34px;
    padding: 6px 11px;
    border: 1px solid rgba(255,255,255,.30);
    border-radius: 7px;
    color: #fff;
    background: #304454;
    cursor: pointer;
}

.projector-browser-actions button:hover {
    background: #405d70;
}

#projector-frame {
    flex: 1 1 auto;
    width: 100%;
    min-height: 0;
    border: 0;
    background: #fff;
}

body.touch-device #projector-browser {
    padding:
        max(6px, env(safe-area-inset-top))
        max(6px, env(safe-area-inset-right))
        max(6px, env(safe-area-inset-bottom))
        max(6px, env(safe-area-inset-left));
}

body.touch-device #projector-browser-window {
    width: 100%;
    height: 100%;
    border-radius: 8px;
}

@media (max-width: 650px) {
    #projector-browser-bar {
        align-items: stretch;
        min-height: 78px;
        flex-direction: column;
        padding: 7px;
        gap: 5px;
    }

    .projector-browser-actions {
        display: grid;
        grid-template-columns: 1fr 1fr;
    }
}
