:root {
    --primary: #3b82f6;
    --glass-bg: rgba(15, 15, 15, 0.85); 
    --glass-border: rgba(255, 255, 255, 0.15);
    --text-main: #ffffff;
    --text-muted: #a3a3a3;
    --card-radius: 16px;
    --vv-top: 0px;
    --vv-right: 0px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    background-color: #000;
    color: var(--text-main);
    overflow: hidden;
    height: 100%;
    height: 100dvh;
    width: 100%;
    width: 100dvw;
    /* manipulation lets a scrollable menu pan; the scene canvas stays touch-action: none */
    touch-action: manipulation;
    overscroll-behavior: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

/* --- Global Transitions --- */
.fade-transition {
    transition: opacity 0.3s ease, transform 0.3s ease;
    opacity: 1;
    pointer-events: auto;
}

.fade-transition.hidden {
    opacity: 0;
    pointer-events: none;
}

.ui-container.fade-transition.hidden {
    transform: translateY(-8px);
}

html.is-slim-mobile:not(.ui-ready) #mainUi {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

html.is-slim-mobile:not(.ui-ready) #toggleUiBtn .btn-icon-eye-blind {
    display: none;
}

html.is-slim-mobile:not(.ui-ready) #toggleUiBtn .btn-icon-eye-open {
    display: block;
}

/* --- A-Frame Scene --- */
a-scene {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 0; /* Behind everything */
    touch-action: none;
}

.a-canvas,
canvas.a-canvas {
    touch-action: none;
}

a-scene .a-enter-vr,
a-scene .a-enter-ar {
    display: none !important;
}

.flat-view {
    position: fixed;
    inset: 0;
    z-index: 8;
    background: #000;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.flat-view.hidden {
    display: none;
}

.flat-view canvas {
    width: 100%;
    height: 100%;
    object-fit: fill;
}

html:fullscreen,
html:-webkit-full-screen,
body.is-fullscreen {
    background: #000;
    height: 100%;
    width: 100%;
}

html:fullscreen a-scene,
html:-webkit-full-screen a-scene,
body.is-fullscreen a-scene {
    width: 100%;
    height: 100%;
}

/* A-Frame's built-in loader is z-index 1000 and would cover the UI */
.a-loader,
.a-loader-title {
    display: none !important;
    pointer-events: none !important;
}

/* --- HUD (eye + fullscreen) --- */
.hud-controls {
    position: fixed;
    top: calc(12px + var(--safe-top) + var(--vv-top));
    right: calc(12px + var(--safe-right) + var(--vv-right));
    left: auto;
    bottom: auto;
    z-index: 4000;
    display: flex;
    gap: 8px;
    pointer-events: none;
}

.floating-toggle {
    position: relative;
    top: auto;
    right: auto;
    z-index: auto;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    color: white;
    width: 48px;
    height: 48px;
    min-width: 48px;
    min-height: 48px;
    border-radius: 50%;
    cursor: pointer;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
    pointer-events: auto;
    transition: transform 0.2s, background 0.2s;
}

.floating-toggle .btn-icon {
    width: 22px;
    height: 22px;
    pointer-events: none;
}

.floating-toggle .btn-icon-eye-open {
    display: none;
}

.floating-toggle.is-ui-hidden .btn-icon-eye-blind {
    display: none;
}

.floating-toggle.is-ui-hidden .btn-icon-eye-open {
    display: block;
}

.floating-toggle .btn-icon-shrink {
    display: none;
}

.floating-toggle.is-fullscreen .btn-icon-expand {
    display: none;
}

.floating-toggle.is-fullscreen .btn-icon-shrink {
    display: block;
}

.floating-toggle:hover,
.floating-toggle:focus-visible {
    transform: scale(1.08);
    background: rgba(255, 255, 255, 0.2);
    outline: none;
}

.floating-toggle.active {
    background: rgba(74, 222, 128, 0.18);
    border-color: #4ade80;
    color: #4ade80;
}

.help-mark {
    font-size: 1.15rem;
    font-weight: 650;
    line-height: 1;
}

.help-panel {
    position: fixed;
    top: calc(68px + var(--safe-top) + var(--vv-top));
    right: calc(12px + var(--safe-right) + var(--vv-right));
    z-index: 4000;
    width: min(280px, calc(100vw - 24px));
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-radius: 14px;
    padding: 16px 16px 12px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.55);
}

.help-panel.hidden {
    display: none;
}

.help-panel h3 {
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 10px;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-muted);
}

.help-panel ul {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.help-panel li {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    font-size: 0.8rem;
    color: #e5e5e5;
}

.help-panel li span {
    color: #fff;
    font-weight: 600;
    font-size: 0.72rem;
    letter-spacing: 0.02em;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 6px;
    padding: 2px 7px;
    white-space: nowrap;
}

.scene-hud {
    position: fixed;
    left: calc(16px + env(safe-area-inset-left, 0px));
    top: calc(16px + var(--safe-top) + var(--vv-top));
    z-index: 3500;
    max-width: min(52vw, 360px);
    pointer-events: none;
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 8px 10px;
    padding: 8px 12px;
    border-radius: 999px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    color: #fff;
    font-size: 0.8rem;
}

.scene-hud.hidden {
    display: none;
}

.scene-hud #sceneHudIndex {
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.scene-hud #sceneHudName {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 220px;
}

.scene-hud-paused {
    color: #fbbf24;
    font-weight: 600;
}

.scene-hud-paused.hidden {
    display: none;
}

.look-hud {
    position: fixed;
    left: 50%;
    bottom: calc(16px + var(--safe-bottom));
    transform: translateX(-50%);
    z-index: 3000;
    width: min(380px, calc(100vw - 32px));
    pointer-events: none;
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 8px;
    border-radius: 14px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.look-hud.hidden {
    display: none;
}

.look-hud.is-wander {
    border-color: rgba(74, 222, 128, 0.45);
}

.look-hud canvas {
    display: block;
    width: 100%;
    height: 72px;
    border-radius: 8px;
    background: #000;
}

.look-hud-readout {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    font-size: 0.78rem;
    font-variant-numeric: tabular-nums;
    color: #fff;
    padding: 0 2px;
}

.look-hud-readout span {
    flex: 1;
    text-align: center;
    white-space: nowrap;
}

#lookHeading { color: #93c5fd; }
#lookPitch { color: #fde68a; }
#lookZoom { color: #86efac; }

.edge-nav {
    position: fixed;
    top: 50%;
    transform: translateY(-50%);
    z-index: 3500;
    width: 48px;
    height: 64px;
    border: 1px solid var(--glass-border);
    border-radius: 14px;
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: auto;
    touch-action: manipulation;
}

.edge-nav .btn-icon {
    width: 20px;
    height: 20px;
}

.edge-nav-prev {
    left: calc(10px + env(safe-area-inset-left, 0px));
}

.edge-nav-next {
    right: calc(10px + env(safe-area-inset-right, 0px));
}

.edge-nav.hidden {
    display: none;
}

.edge-nav:hover,
.edge-nav:focus-visible {
    background: rgba(255, 255, 255, 0.18);
    outline: none;
}

.hint-toast {
    position: fixed;
    top: calc(16px + var(--safe-top) + var(--vv-top));
    left: 50%;
    transform: translateX(-50%);
    z-index: 3600;
    pointer-events: none;
    padding: 10px 16px;
    border-radius: 999px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    color: #fff;
    font-size: 0.82rem;
    text-align: center;
    white-space: nowrap;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
    transition: opacity 0.35s ease;
}

.hint-toast.hidden {
    opacity: 0;
}

.drop-overlay {
    position: fixed;
    inset: 0;
    z-index: 5000;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(15, 23, 42, 0.55);
    border: 3px dashed rgba(59, 130, 246, 0.85);
    color: #fff;
    font-size: 1.15rem;
    font-weight: 600;
    pointer-events: none;
}

.drop-overlay.hidden {
    display: none;
}

/* --- Main Controls UI --- */
.ui-container {
    position: absolute;
    top: 20px; 
    left: 20px;
    width: 320px;
    max-width: 90vw; 
    z-index: 2000; /* Above A-Frame loader chrome (z-index 1000) */
    max-height: calc(100dvh - 40px - var(--safe-top)); 
    display: flex;
    flex-direction: column;
    pointer-events: auto;
    touch-action: auto;
    overscroll-behavior: contain;
}

.glass-panel {
    background: var(--glass-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid var(--glass-border);
    border-radius: var(--card-radius);
    padding: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.7);
    overflow-y: auto;
    overscroll-behavior: contain;
    flex: 0 1 auto;
    min-height: 0;
    -webkit-overflow-scrolling: touch;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    padding-bottom: 12px;
}

.card-heading {
    min-width: 0;
}

.viewer-subtitle {
    margin-top: 4px;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-muted);
    line-height: 1.35;
}

.viewer-subtitle.hidden {
    display: none;
}

.viewer-boot-error {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    text-align: center;
    color: #fff;
}

.header-actions { display: flex; gap: 10px; align-items: center; }

#helpMenuBtn {
    display: none;
}

.icon-btn.active {
    color: #4ade80;
}
h2 { font-size: 1.1rem; font-weight: 600; letter-spacing: 0.5px; }

.icon-btn, .close-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    font-size: 1.2rem;
    color: white;
    transition: transform 0.2s;
    padding: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.icon-btn:hover, .close-btn:hover { transform: scale(1.2); }
.icon-btn:disabled { opacity: 0.3; cursor: default; transform: none; }

.btn-icon {
    display: inline-block;
    width: 1.15em;
    height: 1.15em;
    flex-shrink: 0;
    background-color: currentColor;
    -webkit-mask-size: contain;
    mask-size: contain;
    -webkit-mask-repeat: no-repeat;
    mask-repeat: no-repeat;
    -webkit-mask-position: center;
    mask-position: center;
}

.icon-btn .btn-icon {
    width: 20px;
    height: 20px;
}

.btn-icon-bin {
    -webkit-mask-image: url('/assets/SVGs/bin.svg');
    mask-image: url('/assets/SVGs/bin.svg');
}

.btn-icon-hand {
    -webkit-mask-image: url('/assets/SVGs/hand-point.svg');
    mask-image: url('/assets/SVGs/hand-point.svg');
}

.btn-icon-tilt {
    -webkit-mask-image: url('/assets/SVGs/tilt.svg');
    mask-image: url('/assets/SVGs/tilt.svg');
}

.btn-icon-auto-pan {
    -webkit-mask-image: url('/assets/SVGs/scroll-left-right.svg');
    mask-image: url('/assets/SVGs/scroll-left-right.svg');
}

.btn-icon-left {
    -webkit-mask-image: url('/assets/SVGs/Left-ArrowIcons.svg');
    mask-image: url('/assets/SVGs/Left-ArrowIcons.svg');
}

.btn-icon-right {
    -webkit-mask-image: url('/assets/SVGs/Right-ArrowIcons.svg');
    mask-image: url('/assets/SVGs/Right-ArrowIcons.svg');
}

.btn-icon-eye-open {
    -webkit-mask-image: url('/assets/SVGs/eye-open-icon.svg');
    mask-image: url('/assets/SVGs/eye-open-icon.svg');
}

.btn-icon-eye-blind {
    -webkit-mask-image: url('/assets/SVGs/eye-blind-icon.svg');
    mask-image: url('/assets/SVGs/eye-blind-icon.svg');
}

.btn-icon-pin {
    -webkit-mask-image: url('/assets/SVGs/pin.svg');
    mask-image: url('/assets/SVGs/pin.svg');
}

.btn-icon-wander {
    -webkit-mask-image: url('/assets/SVGs/fly.svg');
    mask-image: url('/assets/SVGs/fly.svg');
}

.btn-icon-expand {
    -webkit-mask-image: url('/assets/SVGs/expand.svg');
    mask-image: url('/assets/SVGs/expand.svg');
}

.btn-icon-shrink {
    -webkit-mask-image: url('/assets/SVGs/shrink.svg');
    mask-image: url('/assets/SVGs/shrink.svg');
}

svg.btn-icon {
    background-color: transparent;
    -webkit-mask-image: none;
    mask-image: none;
    display: block;
    overflow: visible;
}

.control-group { margin-bottom: 16px; }

.slider-stack {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

/* File Input Buttons */
.file-actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.file-btn {
    display: block;
    background: var(--primary);
    color: white;
    padding: 12px;
    border-radius: 10px;
    cursor: pointer;
    text-align: center;
    font-weight: 600;
    font-size: 0.95rem;
    transition: background 0.2s, transform 0.1s;
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}
.file-btn:hover { background: #2563eb; transform: translateY(-1px); }
.file-btn:active { transform: translateY(1px); }
.file-btn-secondary {
    background: rgba(255, 255, 255, 0.1);
    box-shadow: none;
    font-weight: 500;
}
.file-btn-secondary:hover { background: rgba(255, 255, 255, 0.18); }
input[type="file"] { display: none; }

#fileCount {
    display: block;
    margin-top: 8px;
    font-size: 0.75rem;
    color: var(--text-muted);
    text-align: center;
}

/* Slider */
.slider-box label {
    display: flex;
    justify-content: space-between;
    font-size: 0.85rem;
    margin-bottom: 10px;
    color: var(--text-muted);
}

.slider-wrap {
    --slider-track-h: 18px;
    --slider-thumb-s: 12px;
    --slider-inset: 8px;
    height: var(--slider-track-h);
    padding: 0 var(--slider-inset);
    display: flex;
    align-items: center;
    box-sizing: border-box;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.12);
    border: 1px solid rgba(255, 255, 255, 0.12);
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.35);
}

.slider-wrap input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    display: block;
    width: 100%;
    height: var(--slider-track-h);
    margin: 0;
    padding: 0;
    background: transparent;
    cursor: pointer;
    touch-action: none;
}

.slider-wrap input[type="range"]:focus {
    outline: none;
}

.slider-wrap input[type="range"]:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 3px;
    border-radius: 999px;
}

.slider-wrap input[type="range"]::-webkit-slider-runnable-track {
    -webkit-appearance: none;
    height: var(--slider-track-h);
    background: transparent;
    border: none;
}

.slider-wrap input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: var(--slider-thumb-s);
    height: var(--slider-thumb-s);
    margin-top: calc((var(--slider-track-h) - var(--slider-thumb-s)) / 2);
    border: none;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.45), 0 0 0 1px rgba(255, 255, 255, 0.15);
    cursor: pointer;
}

.slider-wrap input[type="range"]::-moz-range-track {
    height: var(--slider-track-h);
    background: transparent;
    border: none;
}

.slider-wrap input[type="range"]::-moz-range-thumb {
    width: var(--slider-thumb-s);
    height: var(--slider-thumb-s);
    border: none;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.45), 0 0 0 1px rgba(255, 255, 255, 0.15);
    cursor: pointer;
}

.slider-wrap input[type="range"]::-moz-range-progress {
    background: transparent;
    height: var(--slider-track-h);
    border: none;
}

.slider-box.is-disabled {
    opacity: 0.4;
}

.slider-box.is-disabled .slider-wrap {
    pointer-events: none;
}

.slider-wrap input[type="range"]:disabled {
    cursor: not-allowed;
}

.auto-pan-extras {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.auto-pan-extras .slider-box {
    flex: 1;
    min-width: 0;
}

.auto-dir-btn {
    flex: 0 0 64px;
    width: 64px;
    min-height: 48px;
    flex-direction: column;
    gap: 2px;
    padding: 8px 4px;
    font-size: 0.75rem;
}

.auto-dir-btn .btn-icon {
    width: 16px;
    height: 16px;
}

.auto-dir-btn .btn-icon-left {
    display: none;
}

.auto-dir-btn.is-reversed .btn-icon-right {
    display: none;
}

.auto-dir-btn.is-reversed .btn-icon-left {
    display: block;
}

.auto-dir-btn:disabled {
    opacity: 0.4;
}

/* Mode + sensor buttons */
.mode-controls {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
}

.sensor-controls { display: flex; gap: 8px; }

.extra-toggles {
    display: flex;
    gap: 8px;
}

.extra-toggles .sensor-btn {
    flex: 1;
}

.extra-toggles .btn-icon {
    width: 16px;
    height: 16px;
}

.sensor-btn {
    background: linear-gradient(135deg, rgba(255,255,255,0.1), rgba(255,255,255,0.05));
    border: 1px solid var(--glass-border);
    color: white;
    padding: 12px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.2s;
    box-sizing: border-box;
    width: 100%;
}

.mode-controls .sensor-btn {
    flex-direction: column;
    gap: 4px;
    padding: 10px 6px;
    font-size: 0.8rem;
    min-width: 0;
    min-height: 58px;
}

.mode-controls .btn-icon {
    width: 22px;
    height: 22px;
}

.sensor-btn:hover {
    background: rgba(255,255,255,0.15);
    border-color: rgba(255,255,255,0.3);
}

.sensor-btn.active {
    background: rgba(74, 222, 128, 0.2);
    border-color: #4ade80;
    color: #4ade80;
}

.sensor-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.nav-controls { display: flex; gap: 10px; }
.nav-controls button {
    flex: 1;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid var(--glass-border);
    color: white;
    padding: 12px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}
.nav-arrow-btn {
    flex: 0 0 52px;
    padding: 12px 0;
}
.nav-arrow-btn .btn-icon {
    width: 18px;
    height: 18px;
}
.nav-controls button:hover:not(:disabled) { background: rgba(255, 255, 255, 0.2); }
.nav-controls button:disabled { opacity: 0.3; cursor: not-allowed; }

.file-info-box {
    text-align: center;
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 12px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.debug-details {
    margin-top: 4px;
    border-radius: 8px;
    border: 1px solid rgba(255,255,255,0.05);
    background: rgba(0, 0, 0, 0.35);
}

.debug-details summary {
    cursor: pointer;
    list-style: none;
    padding: 8px 10px;
    font-size: 0.75rem;
    color: var(--text-muted);
    user-select: none;
}

.debug-details summary::-webkit-details-marker {
    display: none;
}

.debug-details summary::after {
    content: '▸';
    float: right;
    opacity: 0.7;
}

.debug-details[open] summary::after {
    content: '▾';
}

.debug-log {
    margin-top: 0;
    padding: 0 10px 10px;
    background: transparent;
    border-radius: 0;
    font-family: 'Menlo', 'Monaco', monospace;
    font-size: 0.7rem;
    height: 80px;
    overflow-y: auto;
    color: #4ade80; 
    border: none;
    line-height: 1.4;
    -webkit-user-select: text;
    user-select: text;
}

/* --- Gallery Sidebar --- */
.gallery-modal {
    position: fixed; 
    top: 0;
    right: 0;
    bottom: 0;
    width: 320px; 
    background: rgba(10, 10, 10, 0.95);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-left: 1px solid var(--glass-border);
    z-index: 2100;
    touch-action: manipulation;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.3s ease;
    box-shadow: -5px 0 30px rgba(0,0,0,0.8);
}

.gallery-modal.hidden {
    transform: translateX(100%); 
    opacity: 0;
    pointer-events: none;
}

body.is-gallery-open #hudControls,
body.is-gallery-open .scene-hud {
    visibility: hidden;
    pointer-events: none;
}

.gallery-header {
    padding: 20px;
    border-bottom: 1px solid var(--glass-border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(255,255,255,0.03);
    flex-shrink: 0; 
}
.gallery-header h3 { font-size: 1rem; color: #fff; }
.gallery-header .close-btn { font-size: 1.8rem; padding: 0 10px; }

.gallery-grid {
    flex: 1 1 0%;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px; 
}

.gallery-group {
    margin: 8px 0 0;
    font-size: 0.72rem;
    font-weight: 650;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-muted);
}

/* Custom Scrollbar */
.gallery-grid::-webkit-scrollbar { width: 6px; }
.gallery-grid::-webkit-scrollbar-track { background: transparent; }
.gallery-grid::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.2); border-radius: 4px; }

/* Thumbnails */
.thumb-card {
    position: relative;
    width: 100%;
    height: 160px; 
    min-height: 160px;
    flex-shrink: 0;
    border-radius: 12px;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid transparent;
    background: #000;
    transition: transform 0.2s, border-color 0.2s;
    box-shadow: 0 4px 10px rgba(0,0,0,0.5);
}

.thumb-card:hover,
.thumb-card:focus-visible { border-color: rgba(255,255,255,0.4); transform: scale(1.02); outline: none; }

.thumb-card.active {
    border-color: var(--primary);
    box-shadow: 0 0 20px rgba(59, 130, 246, 0.5);
}

.thumb-card img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.thumb-card img.flipped {
    transform: scaleY(-1);
}

.thumb-label {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(to top, rgba(0,0,0,0.95), transparent);
    color: white;
    font-size: 0.8rem;
    padding: 12px;
    padding-top: 30px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* --- Responsive Adjustments --- */
@media (max-width: 600px) {
    .ui-container {
        top: auto;
        bottom: calc(24px + var(--safe-bottom));
        left: 50%;
        transform: translateX(-50%);
        width: min(95vw, 420px);
        max-height: calc(50dvh - var(--safe-bottom));
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        touch-action: pan-y;
    }

    .ui-container.fade-transition.hidden {
        transform: translateX(-50%) translateY(8px);
    }
    
    .debug-log, .debug-details { display: none; }

    .hint-toast {
        top: calc(72px + var(--safe-top) + var(--vv-top));
        white-space: normal;
        max-width: min(92vw, 420px);
        left: 50%;
    }

    .scene-hud {
        top: calc(72px + var(--safe-top) + var(--vv-top));
        max-width: calc(100vw - 88px);
    }

    .look-hud {
        top: calc(72px + var(--safe-top) + var(--vv-top));
        bottom: auto;
        width: min(240px, calc(100vw - 48px));
    }

    .look-hud canvas {
        height: auto;
        aspect-ratio: 2 / 1;
    }

    body.is-ui-hidden .look-hud {
        top: auto;
        bottom: calc(16px + var(--safe-bottom));
    }
    
    .gallery-modal {
        width: 100%; 
        border-left: none;
        padding-top: var(--safe-top);
    }
    
    .nav-controls button, .file-btn, .sensor-btn {
        padding: 14px; 
    }
}

/* Slim phones: portrait, and short landscape. Collapsed by default, scrollable menu. */
@media (max-width: 600px) and (orientation: portrait),
       (max-height: 500px) and (orientation: landscape) and (pointer: coarse) {
    :root {
        --ui-scale: 0.88;
        --slim-menu-h: 50dvh;
        --look-hud-bottom: calc(16px + var(--safe-bottom));
    }

    .scene-hud {
        top: calc(8px + var(--safe-top) + var(--vv-top));
        left: calc(8px + env(safe-area-inset-left, 0px));
        max-width: calc(100vw - 128px);
        padding: 6px 10px;
        font-size: 0.75rem;
        z-index: 4100;
    }

    .scene-hud #sceneHudName {
        max-width: min(48vw, 200px);
    }

    #hudControls #helpBtn {
        display: none;
    }

    #helpMenuBtn {
        display: flex;
    }

    .help-panel {
        top: auto;
        bottom: calc(var(--slim-menu-h) + 8px);
        right: 12px;
        left: 12px;
        width: auto;
        max-height: calc(50dvh - 24px);
        overflow-y: auto;
    }

    .look-hud {
        display: none !important;
    }

    #lookHudBtn {
        display: none;
    }

    body:not(.is-ui-hidden) {
        --look-hud-bottom: calc(var(--slim-menu-h) + 8px);
    }

    .edge-nav {
        top: auto;
        transform: none;
        width: 44px;
        height: 44px;
        min-width: 44px;
        min-height: 44px;
        border-radius: 12px;
        bottom: calc(var(--look-hud-bottom) + 8px);
    }

    .edge-nav .btn-icon {
        width: 18px;
        height: 18px;
    }

    .edge-nav-prev {
        left: calc(8px + env(safe-area-inset-left, 0px));
    }

    .edge-nav-next {
        right: calc(8px + env(safe-area-inset-right, 0px));
    }

    .ui-container {
        top: auto;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        max-width: none;
        height: var(--slim-menu-h);
        max-height: var(--slim-menu-h);
        transform: none;
        overflow: hidden;
        padding-bottom: var(--safe-bottom);
        touch-action: pan-y;
    }

    .ui-container.fade-transition.hidden {
        transform: translateY(16px);
    }

    .glass-panel {
        display: flex;
        flex-direction: column;
        flex: 1 1 auto;
        min-height: 0;
        height: auto;
        max-height: none;
        overflow: hidden;
        touch-action: manipulation;
        padding: calc(12px * var(--ui-scale, 0.88)) calc(14px * var(--ui-scale, 0.88));
        padding-bottom: max(calc(12px * var(--ui-scale, 0.88)), var(--safe-bottom));
        border-radius: 16px 16px 0 0;
        border-bottom: none;
        box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.55);
    }

    .panel-body {
        flex: 1 1 0%;
        min-height: 0;
        overflow-x: hidden;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        touch-action: pan-y;
        scrollbar-width: thin;
        scrollbar-color: rgba(255, 255, 255, 0.35) transparent;
    }

    .panel-body::-webkit-scrollbar {
        width: 6px;
    }

    .panel-body::-webkit-scrollbar-thumb {
        background: rgba(255, 255, 255, 0.35);
        border-radius: 999px;
    }

    .nav-controls {
        flex-shrink: 0;
        position: relative;
        z-index: 2;
        margin-top: calc(8px * var(--ui-scale, 0.88));
        padding-top: calc(8px * var(--ui-scale, 0.88));
        background: rgba(15, 15, 15, 0.96);
    }

    .slider-wrap {
        --slider-track-h: 36px;
        --slider-thumb-s: 26px;
        touch-action: none;
    }

    .slider-wrap,
    .slider-wrap input[type="range"] {
        touch-action: none;
    }

    .card-header {
        position: sticky;
        top: 0;
        z-index: 2;
        margin-bottom: calc(10px * var(--ui-scale, 0.88));
        padding-bottom: calc(8px * var(--ui-scale, 0.88));
        background: rgba(15, 15, 15, 0.94);
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px);
    }

    h2 {
        font-size: calc(1rem * var(--ui-scale, 0.88));
    }

    .control-group {
        margin-bottom: calc(10px * var(--ui-scale, 0.88));
    }

    .slider-stack {
        gap: calc(8px * var(--ui-scale, 0.88));
    }

    .file-actions {
        gap: calc(6px * var(--ui-scale, 0.88));
    }

    .file-btn {
        padding: calc(10px * var(--ui-scale, 0.88));
        font-size: calc(0.85rem * var(--ui-scale, 0.88));
        border-radius: 8px;
    }

    #fileCount {
        margin-top: calc(4px * var(--ui-scale, 0.88));
        font-size: calc(0.7rem * var(--ui-scale, 0.88));
    }

    .slider-box label {
        font-size: calc(0.78rem * var(--ui-scale, 0.88));
        margin-bottom: calc(6px * var(--ui-scale, 0.88));
    }

    .mode-controls {
        gap: calc(6px * var(--ui-scale, 0.88));
    }

    .mode-controls .sensor-btn {
        min-height: max(44px, calc(48px * var(--ui-scale, 0.88)));
        padding: calc(6px * var(--ui-scale, 0.88)) 4px;
        font-size: calc(0.72rem * var(--ui-scale, 0.88));
        gap: 2px;
    }

    .mode-controls .btn-icon {
        width: calc(18px * var(--ui-scale, 0.88));
        height: calc(18px * var(--ui-scale, 0.88));
    }

    .sensor-btn {
        padding: calc(10px * var(--ui-scale, 0.88));
        font-size: calc(0.82rem * var(--ui-scale, 0.88));
    }

    .nav-controls {
        gap: calc(8px * var(--ui-scale, 0.88));
    }

    .nav-controls button,
    .file-btn,
    .sensor-btn,
    .icon-btn,
    .close-btn,
    .app-modal-btn {
        min-height: 44px;
        touch-action: manipulation;
    }

    .icon-btn,
    .close-btn {
        min-width: 44px;
    }

    .nav-arrow-btn {
        flex: 0 0 48px;
    }

    .file-info-box {
        flex-shrink: 0;
        font-size: calc(0.72rem * var(--ui-scale, 0.88));
        margin-top: calc(6px * var(--ui-scale, 0.88));
        margin-bottom: 0;
    }

    .debug-log,
    .debug-details {
        display: none;
    }

    .hint-toast {
        top: calc(64px + var(--safe-top) + var(--vv-top));
        white-space: normal;
        max-width: min(78vw, 360px);
    }

    .gallery-modal {
        width: 100%;
        border-left: none;
        padding-top: var(--safe-top);
        padding-bottom: var(--safe-bottom);
    }

    .gallery-header .close-btn {
        min-width: 44px;
        min-height: 44px;
    }

    .thumb-card {
        height: 112px;
        min-height: 112px;
    }
}

@media (max-height: 500px) and (orientation: landscape) and (pointer: coarse) {
    :root {
        --slim-menu-h: min(68dvh, 320px);
    }
}

/* 2D Loader Overlay — never intercepts clicks; UI stays above it */
.loader-overlay {
    position: absolute;
    top: 0; 
    left: 0;
    width: 100%; 
    height: 100%;
    background: rgba(0, 0, 0, 0.25);
    z-index: 10;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: white;
    pointer-events: none;
    transition: opacity 0.3s;
}

.loader-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.hires-chip {
    position: absolute;
    bottom: 30px;
    right: 20px;
    z-index: 1500;
    pointer-events: none;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(12px);
    color: white;
    font-size: 0.8rem;
    font-weight: 500;
    padding: 8px 14px;
    border-radius: 999px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
    transition: opacity 0.25s;
}

.hires-chip.hidden {
    opacity: 0;
}

@media (max-width: 600px) and (orientation: portrait),
       (max-height: 500px) and (orientation: landscape) and (pointer: coarse) {
    .hires-chip {
        bottom: calc(var(--look-hud-bottom) + 60px);
        right: calc(12px + env(safe-area-inset-right, 0px));
        max-width: calc(100vw - 128px);
    }
}

/* Spinner Animation */
.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top: 4px solid var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 15px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* App modal (motion permission). Explicit colors: A-Frame's default
   dialog is white and inherits this page's white text, so it looks blank. */
.app-modal {
    position: fixed;
    inset: 0;
    z-index: 6000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    padding-bottom: max(24px, var(--safe-bottom));
    background: rgba(0, 0, 0, 0.62);
    color: #f5f5f5;
}

.app-modal.hidden {
    display: none;
}

.app-modal-card {
    width: min(360px, 100%);
    background: rgba(18, 18, 20, 0.96);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-radius: 16px;
    padding: 22px 20px 18px;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.55);
    color: #f5f5f5;
    text-align: center;
}

.app-modal-icon {
    width: 28px;
    height: 28px;
    margin: 0 auto 12px;
    color: #93c5fd;
}

.app-modal-card h3 {
    color: #ffffff;
    font-size: 1.05rem;
    font-weight: 650;
    letter-spacing: 0.01em;
    margin-bottom: 10px;
}

.app-modal-card p {
    color: #d4d4d4;
    font-size: 0.9rem;
    line-height: 1.45;
    margin-bottom: 18px;
}

.app-modal-actions {
    display: flex;
    gap: 10px;
}

.app-modal-btn {
    flex: 1;
    border: 1px solid var(--glass-border);
    border-radius: 10px;
    padding: 12px 14px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    color: #ffffff;
}

.app-modal-btn-secondary {
    background: rgba(255, 255, 255, 0.1);
}

.app-modal-btn-primary {
    background: var(--primary);
    border-color: #2563eb;
}

.app-modal-btn-primary:disabled,
.app-modal-btn-secondary:disabled {
    opacity: 0.5;
    cursor: default;
}

/* Safety net if A-Frame still injects its permission dialog */
div.a-modal {
    color: #f5f5f5;
}

div.a-dialog {
    background-color: rgba(18, 18, 20, 0.96) !important;
    color: #f5f5f5 !important;
    height: auto !important;
    min-height: 180px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

div.a-dialog-text,
div.a-dialog .a-dialog-text {
    color: #e5e5e5 !important;
}

button.a-dialog-button {
    color: #ffffff !important;
}

button.a-dialog-allow-button,
button.a-dialog-ok-button {
    background-color: #3b82f6 !important;
    color: #ffffff !important;
}

button.a-dialog-deny-button {
    background-color: rgba(255, 255, 255, 0.12) !important;
    color: #ffffff !important;
}

button,
.file-btn {
    touch-action: manipulation;
}

@media (hover: none) {
    .floating-toggle:hover,
    .icon-btn:hover,
    .close-btn:hover,
    .file-btn:hover,
    .thumb-card:hover,
    .edge-nav:hover {
        transform: none;
    }

    .file-btn:hover {
        background: var(--primary);
    }

    .file-btn-secondary:hover {
        background: rgba(255, 255, 255, 0.1);
    }

    .sensor-btn:active:not(:disabled),
    .nav-controls button:active:not(:disabled),
    .edge-nav:active,
    .floating-toggle:active,
    .icon-btn:active,
    .thumb-card:active {
        filter: brightness(1.15);
    }
}