/* ===== Core Layout ===== */
.qr-scanner-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.85);
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    transition: opacity 0.3s ease-out;
    pointer-events: none;
}

.qr-scanner-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.scanner-container {
    width: 90%;
    max-width: 500px;
    background: linear-gradient(145deg, #2a2a2a, #1a1a1a);
    border-radius: 20px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4),
    0 0 0 1px rgba(255, 255, 255, 0.1),
    inset 0 1px 1px rgba(255, 255, 255, 0.1);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    animation: floatIn 0.4s cubic-bezier(0.19, 1, 0.22, 1) forwards;
    max-height: 90vh;
    margin: 0 auto;
    /* Remove the fixed header/footer heights to allow content to expand */
    position: relative;
}

@keyframes floatIn {
    0% { transform: translateY(30px) scale(0.95); }
    100% { transform: translateY(0) scale(1); }
}

/* ===== Header - Now Hidden but Maintained for Structure ===== */
.scanner-header {
    /* Hide the header but keep it in DOM for compatibility */
    display: none;
}

/* ===== Content - Now Takes Full Container Space ===== */
.scanner-content {
    flex: 1 1 auto;
    overflow: hidden;
    position: relative;
    display: flex;
    align-items: center;
    flex-direction: column;
    padding: 0; /* Remove padding to maximize space */
    min-height: 0;
    transition: all 0.3s ease-out;
    /* Take full height of container */
    height: 100%;
}

.scanner-viewport {
    position: relative;
    flex: 1;
    margin: 0; /* Remove margins to maximize space */
    border-radius: 12px;
    overflow: hidden;
    background-color: #000;
    aspect-ratio: 1;
    max-height: none; /* Remove height restrictions */
    width: 100%;
    height: 100%; /* Take full available height */
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3),
    inset 0 0 0 1px rgba(255, 255, 255, 0.1);
    transition: all 0.3s ease-out;
}

.scanner-video-container {
    width: 100%;
    height: 100%;
    overflow: hidden;
    position: absolute;
    top: 0;
    left: 0;
}

/* ===== Footer - Now Hidden but Maintained for Structure ===== */
.scanner-footer {
    /* Hide the footer but keep it in DOM for compatibility */
    display: none;
}

/* ===== New Floating Button Styles ===== */
.floating-scanner-title {
    position: absolute;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    background-color: rgba(0, 0, 0, 0.7);
    color: white;
    padding: 8px 16px;
    border-radius: 20px;
    font-size: clamp(0.85rem, 2.5vw, 1rem);
    font-weight: 600;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    z-index: 15;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    max-width: 80%;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.floating-close-button {
    position: absolute;
    top: 20px;
    right: 20px;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    border: none;
    border-radius: 50%;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 15;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    transition: all 0.2s ease;
    font-size: 1.2rem;
}

.floating-close-button:hover {
    background-color: rgba(255, 255, 255, 0.2);
    transform: scale(1.05);
}

.floating-action-buttons {
    position: absolute;
    bottom: 10px;
    display: flex;
    z-index: 15;
}

.floating-switch-button {
    background-color: rgba(98, 0, 238, 0.8);
    color: white;
    border: none;
    padding: 12px 20px;
    border-radius: 25px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-size: 0.9rem;
    font-weight: 500;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    transition: all 0.2s ease;
    white-space: nowrap;
    min-height: 44px; /* Ensure touch-friendly size */
}

.floating-switch-button:hover {
    background-color: rgba(98, 0, 238, 1);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(98, 0, 238, 0.3);
}

/* ===== Scanner Frame - Adjusted for Full Screen ===== */
.scanner-frame {
    position: absolute;
    top: 15%;
    left: 15%;
    right: 15%;
    bottom: 15%;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 12px !important;
    box-shadow: 0 0 0 5000px rgba(0, 0, 0, 0.5);
    pointer-events: none;
    z-index: 10;
    transition: all 0.3s ease-out;
}

.scanner-corner {
    position: absolute;
    width: 30px;
    height: 30px;
    border: 4px solid #6200ee;
    opacity: 0.8;
    z-index: 100;
}

.scanner-corner-topleft {
    top: -4px;
    left: -4px;
    border-right: none;
    border-bottom: none;
    border-top-left-radius: 12px;
}

.scanner-corner-topright {
    top: -4px;
    right: -4px;
    border-left: none;
    border-bottom: none;
    border-top-right-radius: 12px;
}

.scanner-corner-bottomleft {
    bottom: -4px;
    left: -4px;
    border-right: none;
    border-top: none;
    border-bottom-left-radius: 12px;
}

.scanner-corner-bottomright {
    bottom: -4px;
    right: -4px;
    border-left: none;
    border-top: none;
    border-bottom-right-radius: 12px;
}

.scanner-line {
    position: absolute;
    height: 2px;
    background: linear-gradient(90deg,
    rgba(98, 0, 238, 0),
    rgba(98, 0, 238, 0.8) 20%,
    rgba(98, 0, 238, 1) 50%,
    rgba(98, 0, 238, 0.8) 80%,
    rgba(98, 0, 238, 0));
    width: 100%;
    top: 50%;
    transform: translateY(-50%);
    box-shadow: 0 0 8px rgba(98, 0, 238, 0.5);
    opacity: 0;
}

.scanner-line.scanning {
    opacity: 1;
    animation: scanLine 2s ease-in-out infinite;
}

@keyframes scanLine {
    0% { top: 20%; }
    50% { top: 80%; }
    100% { top: 20%; }
}

/* ===== Message - Updated for Floating Layout ===== */
.scanner-message {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    text-align: center;
    padding: 10px 20px;
    border-radius: 20px;
    background-color: rgba(0, 0, 0, 0.7);
    font-size: 0.9rem;
    max-width: 80%;
    z-index: 5;
    white-space: normal;
    overflow-wrap: break-word;
    word-break: break-word;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

/* ===== Status Overlays ===== */
.scanner-error, .scanner-success-overlay {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: rgba(0, 0, 0, 0.8);
    padding: 20px;
    border-radius: 15px;
    text-align: center;
    color: white;
    max-width: 80%;
    z-index: 20;
    animation: fadeIn 0.3s ease-out;
    backdrop-filter: blur(15px);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.scanner-success-overlay {
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    transform: none;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 25;
    max-width: none;
}

.error-icon {
    font-size: 2rem;
    color: #ff5252;
    margin-bottom: 10px;
}

.success-animation {
    text-align: center;
}

.checkmark-circle {
    width: 80px;
    height: 80px;
    position: relative;
    display: inline-block;
    vertical-align: top;
    margin-bottom: 20px;
}

.checkmark-circle .checkmark {
    border-radius: 5px;
}

.checkmark-circle .checkmark.draw:after {
    animation-delay: 300ms;
    animation-duration: 1s;
    animation-timing-function: ease;
    animation-name: checkmark;
    transform: scaleX(-1) rotate(135deg);
    animation-fill-mode: forwards;
}

.checkmark-circle .checkmark:after {
    opacity: 1;
    height: 40px;
    width: 20px;
    transform-origin: left top;
    border-right: 8px solid #4CAF50;
    border-top: 8px solid #4CAF50;
    border-radius: 2px !important;
    content: '';
    left: 30px;
    top: 45px;
    position: absolute;
}

.success-message {
    color: white;
    font-size: 1rem;
    max-width: 250px;
    margin: 0 auto;
}

@keyframes checkmark {
    0% { height: 0; width: 0; opacity: 1; }
    20% { height: 0; width: 20px; opacity: 1; }
    40% { height: 40px; width: 20px; opacity: 1; }
    100% { height: 40px; width: 20px; opacity: 1; }
}

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

/* ===== Video Element Control ===== */
.scanner-video-container video {
    object-fit: cover !important;
    width: 100% !important;
    height: 100% !important;
    position: absolute !important;
    top: 0 !important;
    left: 0 !important;
    z-index: 1 !important;

    /* Critical: Prevent blurry scaling on mobile */
    image-rendering: auto !important;
    image-rendering: crisp-edges !important;
    image-rendering: -webkit-optimize-contrast !important;

    /* Ensure no transform scaling that could cause blur */
    transform: none !important;
    -webkit-transform: none !important;

    /* Prevent any CSS scaling */
    max-width: none !important;
    max-height: none !important;
    min-width: auto !important;
    min-height: auto !important;

    /* Hardware acceleration for smooth video */
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    will-change: auto;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

.scanner-video-container canvas {
    position: absolute !important;
    top: 0 !important;
    left: 0 !important;
    width: 100% !important;
    height: 100% !important;
    z-index: 2 !important;
}

/* ===== Hide Library Elements ===== */
.qr-region,
.qr-shaded-region,
div[class*="code-scanner-region"],
.html5-qrcode-element {
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    display: none !important;
}

/* ===== Landscape Mode Adjustments ===== */
@media (orientation: landscape) {
    .scanner-container {
        max-height: 95vh;
        width: 95%;
        max-width: 800px; /* Allow wider on landscape */
    }

    .scanner-content {
        padding: 0;
    }

    .scanner-frame {
        top: 10%;
        left: 20%;
        right: 20%;
        bottom: 10%;
    }

    .floating-scanner-title {
        top: 15px;
        font-size: 0.9rem;
        padding: 6px 12px;
    }

    .floating-close-button {
        top: 15px;
        right: 15px;
        width: 40px;
        height: 40px;
    }

    .floating-action-buttons {
        bottom: 8px;
    }

    .floating-switch-button {
        padding: 10px 16px;
        font-size: 0.85rem;
    }
}

/* ===== Small Height Landscape ===== */
@media (orientation: landscape) and (max-height: 450px) {
    .floating-scanner-title {
        top: 10px;
        font-size: 0.8rem;
        padding: 4px 8px;
    }

    .floating-close-button {
        top: 10px;
        right: 10px;
        width: 36px;
        height: 36px;
        font-size: 1rem;
    }

    .floating-action-buttons {
        bottom: 5px;
    }

    .floating-switch-button {
        padding: 8px 12px;
        font-size: 0.8rem;
        min-height: 36px;
    }

    .scanner-frame {
        top: 8%;
        left: 15%;
        right: 15%;
        bottom: 8%;
    }
}

/* ===== Mobile Screens ===== */
@media (max-width: 576px) {
    .scanner-container {
        width: 95%;
        border-radius: 15px;
        max-height: 95vh;
    }

    .floating-scanner-title {
        font-size: clamp(0.8rem, 3vw, 0.9rem);
        max-width: 70%;
        top: 15px;
    }

    .floating-close-button {
        width: 42px;
        height: 42px;
        top: 15px;
        right: 15px;
    }

    .floating-action-buttons {
        bottom: 8px;
    }

    .floating-switch-button {
        width: auto;
        justify-content: center;
        padding: 12px 16px;
    }
}

/* ===== Very Small Screens ===== */
@media (max-width: 375px) {
    .floating-scanner-title {
        font-size: 0.75rem;
        padding: 6px 10px;
        max-width: 65%;
    }

    .floating-close-button {
        width: 38px;
        height: 38px;
        font-size: 0.9rem;
    }

    .floating-switch-button {
        font-size: 0.8rem;
        padding: 10px 12px;
    }
}

/* ===== Switch Button Visibility Control ===== */
.floating-switch-button[style*="display: none"] {
    display: none !important;
}

/* ===== Extra Small Landscape Screens ===== */
@media (orientation: landscape) and (max-width: 500px) and (max-height: 350px) {
    .scanner-content {
        padding: 0;
    }

    .scanner-frame {
        top: 5%;
        left: 10%;
        right: 10%;
        bottom: 5%;
    }

    .floating-scanner-title {
        display: none; /* Hide title on very small screens */
    }

    .floating-close-button {
        top: 5px;
        right: 5px;
        width: 32px;
        height: 32px;
        font-size: 0.9rem;
    }

    .floating-action-buttons {
        bottom: 5px;
    }

    .floating-switch-button {
        padding: 6px 10px;
        font-size: 0.75rem;
        min-height: 32px;
    }
}

/* Mobile-specific scanner frame adjustments */
@media (max-width: 768px) {
    .scanner-frame {
        /* Larger scan area on mobile for easier detection */
        top: 10%;
        left: 10%;
        right: 10%;
        bottom: 10%;
        border-width: 3px;
    }

    .scanner-corner {
        width: 40px;
        height: 40px;
        border-width: 5px;
    }

    /* Make scanner line more visible on mobile */
    .scanner-line {
        height: 3px;
        box-shadow: 0 0 12px rgba(98, 0, 238, 0.8);
    }
}

/* Enhanced mobile viewport handling */
@media (orientation: portrait) and (max-width: 576px) {
    .scanner-viewport {
        /* Ensure viewport uses optimal aspect ratio */
        aspect-ratio: 3/4;
        max-height: 70vh;
    }

    .scanner-frame {
        /* Even larger scan area in portrait */
        top: 8%;
        left: 8%;
        right: 8%;
        bottom: 8%;
    }
}

@media (orientation: landscape) and (max-height: 500px) {
    .scanner-viewport {
        /* Optimize for landscape mobile */
        aspect-ratio: 16/9;
        max-height: 85vh;
    }

    .scanner-frame {
        /* Adjust for landscape orientation */
        top: 5%;
        left: 20%;
        right: 20%;
        bottom: 5%;
    }
}

/* Hide any HTML5-QRCode library UI elements that might interfere */
#scanner-video-container div[id*="qr-region"],
#scanner-video-container div[id*="qr-shaded-region"],
#scanner-video-container div[class*="qr"],
#scanner-video-container .html5-qrcode-element {
    display: none !important;
    visibility: hidden !important;
    opacity: 0 !important;
}

/* Ensure container maintains proper aspect ratio */
.scanner-video-container {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: #000;

    /* Prevent any unwanted scaling */
    contain: layout style paint;
}

/* High DPI display optimizations */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .scanner-video-container video {
        /* Force browser to use native resolution on high DPI displays */
        image-rendering: -webkit-optimize-contrast;
        image-rendering: optimize-contrast;
        image-rendering: pixelated;
    }
}

/* iOS Safari specific fixes */
@supports (-webkit-touch-callout: none) {
    .scanner-video-container video {
        /* iOS Safari video optimizations */
        -webkit-transform: translate3d(0, 0, 0);
        -webkit-perspective: 1000px;
        -webkit-backface-visibility: hidden;
    }
}

/* Android Chrome specific fixes */
@media screen and (-webkit-min-device-pixel-ratio: 0) {
    .scanner-video-container video {
        /* Android Chrome optimizations */
        transform: translateZ(0);
        will-change: transform;
    }
}

/* Focus feedback animation */
@keyframes focusPulse {
    0% {
        transform: scale(0.5);
        opacity: 1;
        border-color: #6200ee;
    }
    50% {
        transform: scale(1.2);
        opacity: 0.7;
        border-color: #03dac6;
    }
    100% {
        transform: scale(1.5);
        opacity: 0;
        border-color: #6200ee;
    }
}

/* Enhanced scanner line animation for mobile */
@media (max-width: 768px) {
    .scanner-line.scanning {
        animation: mobileScanLine 1.5s ease-in-out infinite;
    }
}

@keyframes mobileScanLine {
    0%, 100% {
        top: 15%;
        opacity: 0.8;
    }
    50% {
        top: 85%;
        opacity: 1;
    }
}

/* Tap-to-focus indicator styles */
.focus-indicator {
    position: absolute;
    width: 50px;
    height: 50px;
    border: 2px solid #6200ee;
    border-radius: 50%;
    pointer-events: none;
    z-index: 1000;
    animation: focusPulse 1s ease-out forwards;
}

/* Enhanced message styling for mobile */
@media (max-width: 576px) {
    .scanner-message {
        font-size: 0.85rem;
        padding: 8px 16px;
        max-width: 90%;
    }

    .floating-scanner-title {
        font-size: 0.8rem;
        padding: 6px 12px;
    }
}

/* Improve button touch targets on mobile */
@media (max-width: 768px) {
    .floating-close-button,
    .floating-switch-button {
        min-width: 48px;
        min-height: 48px;
        padding: 12px;
    }

    .floating-switch-button {
        font-size: 0.85rem;
    }
}