/* Viewer Specific Styles */
body {
    margin: 0;
    overflow: hidden;  /* No body scrolling on any device */
    background: var(--gray-900);
    position: fixed;  /* Lock body position */
    width: 100%;
    height: 100%;
}

.viewer-container {
    display: flex;
    flex-direction: column;
    height: 100vh;  /* Fixed viewport height */
    height: 100dvh;  /* Dynamic viewport height for mobile browsers */
    width: 100vw;
    overflow: hidden;
}

/* Top Bar */
.viewer-header {
    background: white;
    border-bottom: 1px solid var(--gray-200);
    padding: var(--spacing-sm) var(--spacing-md) var(--spacing-md) var(--spacing-md);
    box-shadow: var(--shadow-md);
    z-index: 100;
    position: relative;
    display: flex;
    flex-direction: column;
}

/* Search Bar - Top Left */
.header-search {
    position: absolute;
    top: 55px;
    left: 15px;
    z-index: 500;
}

/* Building Title - Centered */
.building-title-main {
    text-align: center;
    padding: var(--spacing-md) 0 var(--spacing-xs) 0;
}

.building-title-main h1 {
    font-size: 1.75rem;
    margin: 0;
    color: var(--gray-900);
    font-weight: 700;
}

/* Floor Selector - Centered Under Title */
.floor-selector-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 0 var(--spacing-sm) 0;
}

.current-floor-name {
    font-size: 14px;
    color: var(--gray-600);
    font-style: italic;
}

/* Force Back Button Visibility */
.back-btn {
    position: absolute;
    left: 15px;
    top: 15px;
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    gap: 6px;
    background: var(--primary-blue);
    color: white;
    padding: 8px 16px;
    border-radius: var(--radius-md);
    text-decoration: none;
    font-weight: 600;
    font-size: 14px;
    box-shadow: var(--shadow-sm);
    z-index: 1000;
    border: 1px solid rgba(255, 255, 255, 0.2);
    transition: all 0.2s ease;
}

.back-btn svg {
    flex-shrink: 0;
}

.back-btn span {
    white-space: nowrap;
}

.back-btn:hover {
    background: linear-gradient(135deg, #1E40AF 0%, #1E3A8A 100%);
    box-shadow: 0 6px 20px rgba(59, 130, 246, 0.4);
    transform: translateX(-2px);
}

.floor-selector {
    min-width: 220px;
    max-width: 280px;
    padding: 9px 14px;
    border: 2px solid var(--primary-blue);
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 600;
    background: white;
    cursor: pointer;
    color: var(--gray-900);
    transition: all 0.2s;
    text-align: center;
}

.floor-selector:hover {
    border-color: var(--primary-dark);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.floor-selector:focus {
    outline: none;
    border-color: var(--primary-blue);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

.search-bar {
    position: relative;
    width: 280px;
}

.search-bar input {
    width: 100%;
    padding: 8px 12px 8px 35px;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius-md);
    font-size: 13px;
}

.search-bar input:focus {
    outline: none;
    border-color: var(--primary-blue);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.search-icon {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    pointer-events: none;
    z-index: 1;
    color: var(--gray-500);
}

.search-results {
    position: absolute;
    top: calc(100% + 5px);
    left: 0;
    right: 0;
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    max-height: 300px;
    overflow-y: auto;
    z-index: 200;
    display: none;
}

.search-results.visible {
    display: block;
}

.search-result-item {
    padding: var(--spacing-md);
    border-bottom: 1px solid var(--gray-100);
    cursor: pointer;
    transition: background var(--transition-fast);
}

.search-result-item:last-child {
    border-bottom: none;
}

.search-result-item:hover {
    background: var(--gray-50);
}

.search-result-name {
    font-weight: 600;
    color: var(--gray-900);
    margin-bottom: 4px;
}

.search-result-tags {
    font-size: 12px;
    color: var(--gray-500);
}

.search-result-floor {
    font-size: 12px;
    color: var(--primary-blue);
    font-weight: 500;
}

/* Viewer Canvas */
.viewer-canvas-container {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: #f5f5f5;
    touch-action: none;  /* We handle all touch here in JS */
    overscroll-behavior: contain;
    min-height: 0;  /* Critical: allows flex child to shrink */
}

.viewer-canvas-wrapper {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
    padding: 20px;  /* Compact padding */
    box-sizing: border-box;
    position: relative;
    touch-action: none;
    z-index: 1;
    min-height: 0;
}

.viewer-canvas-wrapper:active {
    cursor: grabbing;
}

#viewerCanvas {
    display: block;
    max-width: 100%;
    max-height: 100%;
    box-shadow: 0 4px 24px rgba(0,0,0,0.18);
    background: white;
    cursor: grab;
    pointer-events: auto;
    user-select: none;
    -webkit-user-select: none;
}

#viewerCanvas:active {
    cursor: grabbing;
}

/* Controls - Always visible and prominent on all devices */
.viewer-controls {
    position: fixed !important;
    bottom: 20px !important;
    right: 20px !important;
    display: flex !important;
    flex-direction: column;
    gap: 10px;
    z-index: 999 !important;  /* Above everything */
    pointer-events: auto !important;
}

.control-group {
    background: rgba(255, 255, 255, 0.98);
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2), 0 1px 3px rgba(0, 0, 0, 0.1);
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(0, 0, 0, 0.08);
}

.control-group .btn-icon {
    min-width: 44px;  /* Touch-friendly on all devices */
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    background: white;
    border: 1px solid #e5e7eb;
    cursor: pointer;
    border-radius: 8px;
    color: var(--gray-700);
}

.control-group .btn-icon:hover {
    transform: scale(1.1);
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
    background: #f9fafb;
    border-color: #3B82F6;
    color: #3B82F6;
}

.control-group .btn-icon:active {
    transform: scale(0.95);
}

.control-group .btn-icon svg {
    pointer-events: none;
}

@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.8;
    }
}

/* Welcome Overlay */
.welcome-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(5px);
    z-index: 250;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--spacing-lg);
    animation: fadeIn 0.3s ease;
}

.welcome-content {
    background: white;
    border-radius: var(--radius-xl);
    padding: var(--spacing-xl);
    max-width: 500px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    animation: slideUp 0.4s ease;
    border: 1px solid var(--gray-200);
}

.welcome-content h2 {
    margin: 0 0 var(--spacing-lg) 0;
    color: var(--gray-900);
    text-align: center;
    font-size: 1.75rem;
}

.welcome-instructions {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
}

.instruction-item {
    display: flex;
    align-items: flex-start;
    gap: var(--spacing-md);
    padding: var(--spacing-md);
    background: var(--gray-50);
    border-radius: var(--radius-md);
}

.instruction-icon {
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #EFF6FF;
    border-radius: var(--radius-md);
}

.instruction-item p {
    margin: 0;
    color: var(--gray-700);
    line-height: 1.5;
}

.instruction-item strong {
    color: var(--gray-900);
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Help Popup (Smaller, Centered) */
.help-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(3px);
    z-index: 300;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--spacing-md);
    animation: fadeIn 0.2s ease;
}

.help-popup {
    background: white;
    border-radius: var(--radius-lg);
    padding: var(--spacing-lg);
    max-width: 400px;
    width: 100%;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    animation: slideUp 0.3s ease;
    position: relative;
    border: 1px solid var(--gray-200);
}

.help-close-btn {
    position: absolute;
    top: 12px;
    right: 12px;
    background: transparent;
    border: none;
    font-size: 1.5rem;
    color: var(--gray-500);
    cursor: pointer;
    padding: 4px 8px;
    line-height: 1;
    border-radius: var(--radius-sm);
    transition: all 0.2s;
}

.help-close-btn:hover {
    background: var(--gray-100);
    color: var(--gray-900);
}

.help-popup h3 {
    margin: 0 0 var(--spacing-md) 0;
    color: var(--gray-900);
    font-size: 1.25rem;
    padding-right: 30px;
}

.help-shortcuts {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-xs);
}

.help-item {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    padding: var(--spacing-xs) var(--spacing-sm);
    border-radius: var(--radius-sm);
    transition: background 0.2s;
}

.help-item:hover {
    background: var(--gray-50);
}

.help-key {
    font-family: 'Courier New', monospace;
    font-weight: 600;
    color: var(--primary-blue);
    min-width: 80px;
    font-size: 0.9rem;
}

.help-desc {
    color: var(--gray-700);
    font-size: 0.9rem;
}

/* Object Info Popup */
.object-popup {
    position: fixed;
    background: white;
    border-radius: var(--radius-lg);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2), 0 2px 8px rgba(0, 0, 0, 0.1);
    padding: 0;
    min-width: 260px;
    max-width: min(380px, calc(100vw - 32px));
    z-index: 300;
    display: none;
    border: 1px solid var(--gray-200);
    overflow: hidden;
    animation: popupSlideIn 0.2s ease;
}

.object-popup.visible {
    display: block;
}

@keyframes popupSlideIn {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

.popup-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 12px 10px 16px;
    border-bottom: 1px solid var(--gray-100);
    gap: 8px;
}

.popup-title {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.popup-title h3 {
    margin: 0;
    color: var(--gray-900);
    font-size: 1.05rem;
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Close button - always large, always visible */
.popup-close {
    flex-shrink: 0;
    background: var(--gray-100);
    border: none;
    font-size: 18px;
    color: var(--gray-600);
    cursor: pointer;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    transition: all 0.15s;
    font-weight: 700;
}

.popup-close:hover, .popup-close:active {
    background: var(--gray-200);
    color: var(--gray-900);
}

.popup-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 10px 16px 12px;
    max-height: 80px;
    overflow-y: auto;
}

.popup-actions {
    display: flex;
    gap: var(--spacing-sm);
}

/* Loading State */
.loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.95);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

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

.loading-overlay h2 {
    margin-top: var(--spacing-lg);
    color: var(--gray-700);
}

/* Error State */
.error-state {
    text-align: center;
    padding: var(--spacing-2xl);
    color: var(--gray-600);
}

.error-state h2 {
    color: var(--danger);
    margin-bottom: var(--spacing-md);
}

/* Highlight Effect */
.highlight-overlay {
    position: absolute;
    border: 3px solid var(--primary-blue);
    background: rgba(59, 130, 246, 0.1);
    border-radius: var(--radius-md);
    pointer-events: none;
    animation: highlightPulse 1.5s ease-in-out infinite;
    z-index: 50;
}

@keyframes highlightPulse {

    0%,
    100% {
        opacity: 1;
        transform: scale(1);
    }

    50% {
        opacity: 0.6;
        transform: scale(1.05);
    }
}

/* Path Line */
.path-line {
    position: absolute;
    height: 4px;
    background: linear-gradient(90deg, var(--primary-blue), var(--success));
    border-radius: 2px;
    pointer-events: none;
    z-index: 40;
    animation: pathFlow 2s linear infinite;
}

@keyframes pathFlow {
    0% {
        background-position: 0 0;
    }

    100% {
        background-position: 40px 0;
    }
}

/* Responsive */
@media (max-width: 768px) {
    /* ── Header ─────────────────────────────────────────────
       Row 1: [← Home]  [Search …………………]
       Row 2: Building title
       Row 3: Floor selector
    ──────────────────────────────────────────────────────── */
    .viewer-header {
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        gap: 8px;
        padding: 8px 12px;
        flex-shrink: 0;
    }

    /* Hide version indicator on mobile */
    .version-indicator {
        display: none !important;
    }

    /* Back Button — inline flow (no more absolute positioning) */
    .back-btn {
        position: static;
        flex-shrink: 0;
        order: 0;
        padding: 6px 12px;
        font-size: 13px;
        min-height: 36px;
    }

    /* Search Bar — fills the rest of the first row */
    .header-search {
        position: static;
        order: 1;
        flex: 1 1 120px;
        min-width: 0;
        padding: 0;
        z-index: auto;
    }

    .search-bar {
        width: 100%;
    }

    .search-bar input {
        min-height: 36px;
        font-size: 14px;
    }

    /* Building Title — full row */
    .building-title-main {
        order: 2;
        width: 100%;
        padding: 4px 0;
    }

    .building-title-main h1 {
        font-size: 1.1rem;
    }

    /* Floor Selector — full row */
    .floor-selector-wrapper {
        order: 3;
        width: 100%;
        padding: 0 0 4px;
    }

    .floor-selector {
        width: 100%;
        min-height: 40px;
        font-size: 14px;
    }

    /* ── Canvas — native browser scroll ────────────────────
       Container becomes a scroll viewport; wrapper and canvas
       can be larger than the screen.  1-finger = scroll;
       2-finger pinch = our custom zoom handler.
    ──────────────────────────────────────────────────────── */
    .viewer-canvas-container {
        overflow: scroll;
        -webkit-overflow-scrolling: touch;
        touch-action: pan-x pan-y;  /* 1-finger scroll by browser */
        overscroll-behavior: contain;
    }

    .viewer-canvas-wrapper {
        /* Let content size the wrapper; don’t force-centre */
        display: block;
        width:  fit-content;
        min-width:  100%;
        height: fit-content;
        min-height: 100%;
        padding: 0;
        cursor: default;
        touch-action: pan-x pan-y;
    }

    #viewerCanvas {
        max-width:  none;
        max-height: none;
        cursor: default;
    }

    /* Controls - Bottom right for easier thumb access */
    .viewer-controls {
        bottom: 15px !important;
        right:  15px !important;
        gap: 8px;
    }

    .control-group {
        padding: 8px;
        gap: 6px;
    }

    .control-group .btn-icon {
        min-width:  42px;
        min-height: 42px;
    }

    /* Object Popup - compact card, leaves room for the right-side controls */
    .object-popup {
        left:   12px !important;
        right:  78px !important;  /* 60px controls + ~18px gap */
        bottom: 16px !important;
        top:    auto !important;
        min-width: 0;
        max-width: none;
        border-radius: 14px;
    }

    .popup-tags {
        max-height: 56px;
    }

    /* Help Popup - Full width on mobile */
    .help-popup {
        max-width: 95%;
        max-height: 80vh;
    }

    /* Welcome overlay - adjust for mobile */
    .welcome-content {
        max-width: 95%;
        padding: var(--spacing-lg);
    }

    .welcome-content h2 {
        font-size: 1.5rem;
    }
}