:root {
    --s2047-bg: #06161a;
    --s2047-panel: rgba(6, 24, 29, 0.93);
    --s2047-line: rgba(82, 225, 228, 0.25);
    --s2047-cyan: #4de3e4;
    --s2047-cyan-deep: #13a6c6;
    --s2047-text: #edfafa;
    --s2047-muted: #8faeb1;
    --s2047-safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { min-height: 100%; margin: 0; }
body.space2047-room-theme {
    overflow: hidden;
    background: var(--s2047-bg);
    color: var(--s2047-text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body.space2047-room-theme.admin-bar .s2047-app { height: calc(100dvh - 32px); }

button, select { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.s2047-app {
    height: 100dvh;
    min-height: 520px;
    display: grid;
    grid-template-rows: 96px 1fr;
    background:
        radial-gradient(circle at 22% 16%, rgba(33, 125, 115, 0.25), transparent 36%),
        linear-gradient(160deg, #071a1e 0%, #0a2020 45%, #071619 100%);
}

.s2047-topbar {
    position: relative;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    padding: 18px clamp(18px, 3vw, 44px);
    border-bottom: 1px solid var(--s2047-line);
    background: rgba(3, 16, 21, 0.92);
    backdrop-filter: blur(18px);
}

.s2047-brand { display: flex; align-items: center; gap: 17px; min-width: 0; }
.s2047-brand-mark {
    width: 42px; height: 42px;
    flex: 0 0 auto;
    transform: rotate(45deg);
    background: linear-gradient(135deg, #76f5e8, #29bdd3);
    box-shadow: 0 0 26px rgba(77, 227, 228, 0.42);
}
.s2047-brand-copy { display: grid; gap: 4px; letter-spacing: 0.14em; min-width: 0; }
.s2047-brand-copy strong { font-size: clamp(16px, 2vw, 22px); white-space: nowrap; }
.s2047-brand-copy small { color: var(--s2047-muted); font-size: 10px; letter-spacing: 0.24em; white-space: nowrap; }

.s2047-top-actions { display: flex; gap: 10px; align-items: center; }
.s2047-btn {
    min-height: 44px;
    border-radius: 13px;
    padding: 0 18px;
    border: 1px solid transparent;
    color: var(--s2047-text);
    font-weight: 800;
    letter-spacing: 0.02em;
    cursor: pointer;
    transition: transform .18s ease, border-color .18s ease, background .18s ease;
}
.s2047-btn:hover { transform: translateY(-1px); }
.s2047-btn:active { transform: translateY(1px); }
.s2047-btn-primary {
    color: #022126;
    background: linear-gradient(135deg, #5cf0e5, #39c7eb);
    box-shadow: 0 8px 24px rgba(38, 194, 211, 0.2);
}
.s2047-btn-secondary {
    background: rgba(15, 47, 53, 0.8);
    border-color: rgba(111, 221, 225, 0.22);
}
.s2047-btn-secondary span:first-child { font-size: 22px; margin-right: 6px; }

.s2047-stage {
    position: relative;
    min-height: 0;
    overflow: hidden;
    outline: none;
    touch-action: none;
}
.s2047-stage::before {
    content: "";
    position: absolute; inset: 0;
    background:
        linear-gradient(rgba(94, 204, 187, 0.035) 1px, transparent 1px),
        linear-gradient(90deg, rgba(94, 204, 187, 0.035) 1px, transparent 1px);
    background-size: 48px 48px;
    mask-image: linear-gradient(to bottom, transparent, black 30%, black 80%, transparent);
    pointer-events: none;
}
#s2047-room-canvas { width: 100%; height: 100%; display: block; cursor: grab; }
#s2047-room-canvas:active { cursor: grabbing; }

.s2047-loading {
    position: absolute; inset: 0;
    z-index: 15;
    display: grid;
    place-content: center;
    justify-items: center;
    gap: 14px;
    color: #bde8e8;
    letter-spacing: .08em;
    background: rgba(4, 20, 24, 0.82);
    transition: opacity .3s ease, visibility .3s ease;
}
.s2047-app[data-ready="true"] .s2047-loading { opacity: 0; visibility: hidden; }
.s2047-spinner {
    width: 38px; height: 38px;
    border: 3px solid rgba(88, 231, 228, .2);
    border-top-color: #58e7e4;
    border-radius: 50%;
    animation: s2047-spin .8s linear infinite;
}
@keyframes s2047-spin { to { transform: rotate(360deg); } }

.s2047-hud {
    position: absolute;
    left: 18px; bottom: 18px;
    z-index: 8;
    display: flex;
    border: 1px solid rgba(89, 220, 220, .24);
    background: rgba(4, 24, 28, .83);
    border-radius: 15px;
    overflow: hidden;
    box-shadow: 0 18px 48px rgba(0,0,0,.22);
    backdrop-filter: blur(12px);
}
.s2047-hud > div { padding: 10px 15px; min-width: 92px; border-right: 1px solid rgba(89, 220, 220, .12); }
.s2047-hud > div:last-child { border-right: 0; }
.s2047-hud span { display: block; color: #74a3a7; font-size: 9px; letter-spacing: .14em; margin-bottom: 3px; }
.s2047-hud strong { font-size: 13px; }

.s2047-panel-toggle {
    position: absolute; top: 16px; right: 18px; z-index: 11;
    display: none;
    min-height: 40px;
    border: 1px solid var(--s2047-line);
    border-radius: 12px;
    color: var(--s2047-text);
    background: rgba(4, 26, 31, .9);
    padding: 0 14px;
    font-weight: 700;
}

.s2047-app.panel-closed .s2047-panel-toggle { display: inline-flex; align-items: center; gap: 8px; }
.s2047-panel {
    position: absolute;
    top: 18px; right: 18px; z-index: 10;
    width: min(370px, calc(100% - 36px));
    border: 1px solid rgba(86, 225, 225, .22);
    border-radius: 18px;
    padding: 18px;
    background: var(--s2047-panel);
    box-shadow: 0 25px 80px rgba(0,0,0,.36);
    backdrop-filter: blur(16px);
    transition: transform .24s ease, opacity .24s ease;
}
.s2047-panel.is-closed { transform: translateX(calc(100% + 30px)); opacity: 0; pointer-events: none; }
.s2047-panel-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.s2047-panel-head h2 { margin: 3px 0 0; font-size: 21px; }
.s2047-kicker { color: #58dfe0; font-size: 9px; letter-spacing: .16em; }
.s2047-icon-btn {
    width: 34px; height: 34px; border-radius: 10px;
    border: 1px solid rgba(98,219,220,.18);
    background: rgba(16,52,57,.75); color: white; font-size: 22px; cursor: pointer;
}
.s2047-field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.s2047-field-grid label { display: grid; gap: 6px; color: #98babb; font-size: 11px; letter-spacing: .05em; }
.s2047-field-grid select {
    width: 100%; height: 42px;
    border: 1px solid rgba(100, 213, 216, .18);
    border-radius: 11px;
    color: #eaf7f7;
    background: #0b262b;
    padding: 0 10px;
}
.s2047-panel-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 15px; }
.s2047-help {
    margin-top: 15px; padding: 12px 13px;
    border-radius: 12px;
    color: #9fb9bb;
    font-size: 12px;
    line-height: 1.55;
    background: rgba(8, 34, 39, .8);
}

.s2047-zoom-controls {
    position: absolute; right: 18px; bottom: 18px; z-index: 9;
    display: flex; gap: 8px;
}
.s2047-zoom-controls button {
    width: 54px; height: 48px;
    border: 1px solid rgba(91, 224, 227, .24);
    border-radius: 13px;
    color: white; background: rgba(8, 38, 45, .9);
    font-weight: 900; cursor: pointer;
}

.s2047-mobile-controls { display: none; }
.s2047-toast {
    position: absolute; left: 50%; bottom: 88px; z-index: 30;
    transform: translate(-50%, 18px);
    opacity: 0;
    padding: 10px 16px;
    border-radius: 999px;
    color: white;
    background: rgba(4, 28, 33, .93);
    border: 1px solid rgba(90, 229, 229, .26);
    pointer-events: none;
    transition: opacity .2s ease, transform .2s ease;
}
.s2047-toast.is-visible { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 900px) {
    body.space2047-room-theme.admin-bar .s2047-app { height: calc(100dvh - 46px); }
    .s2047-app { grid-template-rows: 82px 1fr; }
    .s2047-topbar { padding: 13px 16px; gap: 10px; }
    .s2047-brand-mark { width: 34px; height: 34px; }
    .s2047-brand { gap: 13px; }
    .s2047-brand-copy strong { font-size: 16px; }
    .s2047-brand-copy small { font-size: 7px; }
    .s2047-top-actions .s2047-btn-secondary { display: none; }
    .s2047-top-actions .s2047-btn-primary { min-height: 42px; padding: 0 13px; font-size: 12px; }

    .s2047-panel-toggle { display: inline-flex; align-items: center; gap: 8px; }
    .s2047-panel { top: 64px; max-height: calc(100% - 150px); overflow: auto; }
    .s2047-panel:not(.is-open-mobile) { transform: translateX(calc(100% + 30px)); opacity: 0; pointer-events: none; }
    .s2047-panel.is-open-mobile { transform: none; opacity: 1; pointer-events: auto; }

    .s2047-hud { left: 10px; right: 10px; bottom: calc(106px + var(--s2047-safe-bottom)); }
    .s2047-hud > div { flex: 1; min-width: 0; padding: 8px 9px; }
    .s2047-hud > div:nth-child(5) { display: none; }
    .s2047-hud span { font-size: 7px; }
    .s2047-hud strong { font-size: 11px; }

    .s2047-zoom-controls { right: 10px; bottom: calc(12px + var(--s2047-safe-bottom)); }
    .s2047-zoom-controls button { width: 44px; height: 44px; }

    .s2047-mobile-controls {
        position: absolute;
        left: 10px;
        bottom: calc(10px + var(--s2047-safe-bottom));
        z-index: 12;
        display: flex;
        align-items: center;
        gap: 8px;
    }
    .s2047-rotate-mobile,
    .s2047-dpad button {
        border: 1px solid rgba(88, 223, 225, .28);
        color: white;
        background: rgba(5, 34, 40, .92);
        box-shadow: 0 10px 30px rgba(0,0,0,.2);
    }
    .s2047-rotate-mobile { width: 44px; height: 44px; border-radius: 50%; font-size: 22px; }
    .s2047-dpad {
        width: 124px; height: 82px;
        display: grid;
        grid-template-columns: repeat(3, 38px);
        grid-template-rows: repeat(3, 26px);
        justify-content: center;
        align-content: center;
        gap: 2px;
        transform: rotate(45deg);
    }
    .s2047-dpad button { border-radius: 8px; font-size: 12px; transform: rotate(-45deg); }
    .s2047-dpad button[data-move="up"] { grid-column: 2; grid-row: 1; }
    .s2047-dpad button[data-move="left"] { grid-column: 1; grid-row: 2; }
    .s2047-dpad-centre { grid-column: 2; grid-row: 2; color: #54e5e4 !important; }
    .s2047-dpad button[data-move="right"] { grid-column: 3; grid-row: 2; }
    .s2047-dpad button[data-move="down"] { grid-column: 2; grid-row: 3; }
    .s2047-toast { bottom: calc(154px + var(--s2047-safe-bottom)); max-width: calc(100% - 30px); text-align: center; }
}

@media (max-width: 520px) {
    .s2047-brand-copy small { display: none; }
    .s2047-brand-mark { width: 30px; height: 30px; }
    .s2047-top-actions .s2047-btn-primary { max-width: 126px; }
    .s2047-field-grid { grid-template-columns: 1fr; }
    .s2047-panel { width: calc(100% - 20px); right: 10px; padding: 15px; }
    .s2047-panel-toggle { right: 10px; top: 10px; }
    .s2047-panel-toggle span:last-child { display: none; }
    .s2047-zoom-controls button:nth-child(2) { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}


/* Door Room Explorer additions */
.s2047-door-action[aria-pressed="true"] {
    color: #082a2a;
    border-color: rgba(143, 255, 211, .8);
    background: linear-gradient(135deg, #85f2c5, #59d8de);
    box-shadow: 0 8px 28px rgba(87, 230, 207, .22);
}
#s2047-door-state {
    color: #efc27c;
}
.s2047-hud > div:nth-child(4) strong {
    letter-spacing: .08em;
}
.s2047-dpad-centre {
    font-size: 15px !important;
}

@media (max-width: 900px) {
    .s2047-top-actions .s2047-door-action {
        display: inline-flex;
        width: 44px;
        min-width: 44px;
        padding: 0;
        justify-content: center;
    }
    .s2047-top-actions .s2047-door-action .s2047-btn-label {
        display: none;
    }
    .s2047-hud > div:nth-child(5) {
        display: none;
    }
    .s2047-hud > div:nth-child(4) {
        display: block;
    }
}

@media (max-width: 560px) {
    .s2047-top-actions .s2047-btn-primary {
        max-width: 112px;
    }
}
