/* Enhanced Device Exchange Application Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    color: #333;
}

/* Focus styles for accessibility */
*:focus {
    outline: 2px solid #4CAF50;
    outline-offset: 2px;
}

.app-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

/* Sections */
.section {
    display: none;
    width: 100%;
    max-width: 1200px;
}

.section.active {
    display: block;
}

/* Login Section */
.login-card {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    padding: 2rem;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    text-align: center;
    max-width: 400px;
    margin: 0 auto;
}

.login-card h1 {
    margin-bottom: 2rem;
    color: #333;
    font-size: 1.8rem;
    font-weight: 600;
}

/* App Header */
.app-header {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 12px 12px 0 0;
    padding: 1.5rem 2rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.app-header h1 {
    color: #333;
    font-size: 1.8rem;
    font-weight: 600;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 1rem;
}

#current-user {
    font-weight: 500;
    color: #666;
}

/* Navigation Tabs */
.app-nav {
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(10px);
    padding: 1rem 2rem;
    display: flex;
    gap: 1rem;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    overflow-x: auto;
}

.nav-tab {
    background: transparent;
    border: 2px solid transparent;
    padding: 0.75rem 1.5rem;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 500;
    color: #666;
    transition: all 0.3s ease;
    white-space: nowrap;
    min-width: fit-content;
}

.nav-tab:hover,
.nav-tab:focus {
    background: rgba(102, 126, 234, 0.1);
    color: #667eea;
    border-color: #667eea;
}

.nav-tab.active {
    background: #667eea;
    color: white;
    border-color: #667eea;
}

/* Tab Content */
.tab-content {
    display: none;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 0 0 12px 12px;
    padding: 2rem;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.tab-content.active {
    display: block;
}

.tab-content h2 {
    margin-bottom: 2rem;
    color: #333;
    font-size: 1.5rem;
    font-weight: 600;
}

/* Scanner Section */
.scanner-container {
    max-width: 800px;
    margin: 0 auto;
}

.scanner-section {
    margin-bottom: 2rem;
}

#scanner-area {
    position: relative;
    width: 100%;
    max-width: 500px;
    margin: 0 auto 1.5rem;
    border-radius: 12px;
    overflow: hidden;
    background: #000;
    aspect-ratio: 4/3;
}

/*
 * The element with id="scanner-video" now serves as a container for the
 * html5-qrcode library. It must occupy the full width and height of its
 * parent (#scanner-area) so that the generated video element can fill the
 * available space. Using a div instead of a video prevents black feed
 * issues on some devices.
 */
#scanner-video {
    width: 100%;
    height: 100%;
    position: relative;
    /* object-fit had no effect on divs, remove it to avoid confusion */
}

#scanner-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.scanner-line {
    width: 80%;
    height: 2px;
    background: linear-gradient(90deg, transparent, #4CAF50, transparent);
    animation: scan 2s ease-in-out infinite;
}

@keyframes scan {
    0%, 100% { transform: translateY(-50px); opacity: 0; }
    50% { transform: translateY(0); opacity: 1; }
}

.scanner-controls {
    display: flex;
    gap: 1rem;
    justify-content: center;
    flex-wrap: wrap;
    margin-bottom: 1rem;
}

.manual-input {
    display: flex;
    gap: 1rem;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
}

#manual-serial {
    flex: 1;
    min-width: 250px;
    max-width: 400px;
}

/* Device Info */
.device-info {
    background: rgba(255, 255, 255, 0.8);
    border-radius: 12px;
    padding: 2rem;
    border: 1px solid rgba(0, 0, 0, 0.1);
    margin-top: 2rem;
}

.device-info h3 {
    margin-bottom: 1.5rem;
    color: #333;
    font-size: 1.2rem;
}

.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 1rem;
    margin-bottom: 2rem;
}

.info-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem;
    background: rgba(255, 255, 255, 0.8);
    border-radius: 8px;
    border: 1px solid rgba(0, 0, 0, 0.1);
}

.info-item label {
    font-weight: 500;
    color: #666;
}

.info-item span {
    font-weight: 500;
    color: #333;
}

.status-badge {
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 500;
    text-align: center;
    display: inline-block;
    min-width: 80px;
}

.status-available {
    background: #fff3cd;
    color: #856404;
}

.status-exchanged {
    background: #d4edda;
    color: #155724;
}

.status-inactive {
    background: #f8d7da;
    color: #721c24;
}

/* Booking Section */
#booking-section {
    margin: 2rem 0;
}

#booking-section h3 {
    margin-bottom: 1rem;
    color: #333;
}

.booking-controls {
    /* Stack booking controls vertically for better mobile usability.  The
       inputs and buttons appear in separate rows.  Children can still
       contain their own inline flex layout (e.g. for scanner button). */
    display: flex;
    flex-direction: column;
    gap: 1rem;
    align-items: stretch;
}

#new-serial {
    flex: 1;
    min-width: 250px;
}

.action-buttons {
    display: flex;
    gap: 1rem;
    justify-content: center;
    flex-wrap: wrap;
    margin-top: 2rem;
}

/* Search Section */
.search-container {
    max-width: 1000px;
    margin: 0 auto;
}

.search-form {
    background: rgba(255, 255, 255, 0.8);
    border-radius: 12px;
    padding: 2rem;
    margin-bottom: 2rem;
    border: 1px solid rgba(0, 0, 0, 0.1);
}

.search-row,
.filter-row,
.sort-row {
    display: flex;
    gap: 1rem;
    margin-bottom: 1rem;
    align-items: center;
    flex-wrap: wrap;
}

.sort-row:last-child {
    margin-bottom: 0;
}

.sort-row label {
    font-weight: 500;
    color: #333;
    white-space: nowrap;
}

#search-query {
    flex: 1;
    min-width: 300px;
}

.search-results {
    background: rgba(255, 255, 255, 0.8);
    border-radius: 12px;
    padding: 2rem;
    border: 1px solid rgba(0, 0, 0, 0.1);
}

.results-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}

.results-header h3 {
    color: #333;
    font-size: 1.2rem;
}

#results-count {
    color: #666;
    font-weight: 500;
}

.results-table-container {
    overflow-x: auto;
    margin-bottom: 1rem;
}

.results-table {
    width: 100%;
    border-collapse: collapse;
    background: white;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.results-table th {
    background: #f8f9fa;
    padding: 1rem;
    text-align: left;
    font-weight: 600;
    color: #333;
    border-bottom: 2px solid #e1e5e9;
}

.results-table td {
    padding: 1rem;
    border-bottom: 1px solid #e1e5e9;
    vertical-align: middle;
}

.results-table tr:hover {
    background: rgba(102, 126, 234, 0.05);
}

.no-results {
    text-align: center;
    color: #666;
    font-style: italic;
    padding: 2rem;
}

.search-pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 1rem;
    margin-top: 1rem;
}

#search-page-info {
    font-weight: 500;
    color: #666;
}

/* Overview Section */
.overview-container {
    max-width: 800px;
    margin: 0 auto;
}

.overview-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.stat-card {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 1.5rem;
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 1rem;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.stat-icon {
    font-size: 2rem;
    opacity: 0.9;
}

.stat-content h3 {
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: 0.25rem;
}

.stat-content p {
    opacity: 0.9;
    font-size: 0.9rem;
}

.overview-actions {
    display: flex;
    gap: 1rem;
    justify-content: center;
    flex-wrap: wrap;
}

/* Form Elements */
input, select, textarea {
    padding: 0.75rem;
    border: 2px solid #e1e5e9;
    border-radius: 6px;
    font-size: 0.9rem;
    transition: border-color 0.3s ease;
    background: white;
}

input:focus, select:focus, textarea:focus {
    border-color: #667eea;
    outline: none;
}

.form-group {
    margin-bottom: 1rem;
}

.form-group input {
    width: 100%;
}

/* Buttons */
.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 500;
    font-size: 0.9rem;
    transition: all 0.3s ease;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    white-space: nowrap;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    box-shadow: 0 2px 10px rgba(102, 126, 234, 0.3);
}

.btn-primary:hover,
.btn-primary:focus {
    transform: translateY(-2px);
    box-shadow: 0 4px 20px rgba(102, 126, 234, 0.4);
}

.btn-secondary {
    background: #f8f9fa;
    color: #666;
    border: 2px solid #e1e5e9;
}

.btn-secondary:hover,
.btn-secondary:focus {
    background: #e9ecef;
    border-color: #adb5bd;
}

.btn-success {
    background: #28a745;
    color: white;
}

.btn-success:hover,
.btn-success:focus {
    background: #218838;
}

.btn-warning {
    background: #ffc107;
    color: #212529;
}

.btn-warning:hover,
.btn-warning:focus {
    background: #e0a800;
}

.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none !important;
}

.btn-sm {
    padding: 0.5rem 1rem;
    font-size: 0.8rem;
}

/* Loading Overlay */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(5px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    color: white;
}

.loading-spinner {
    width: 50px;
    height: 50px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top: 4px solid white;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 1rem;
}

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

/* Message Toast */
.message-toast {
    position: fixed;
    top: 2rem;
    right: 2rem;
    background: white;
    border-radius: 8px;
    padding: 1rem 1.5rem;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    display: flex;
    align-items: center;
    gap: 1rem;
    z-index: 1001;
    max-width: 400px;
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

.message-toast.success {
    border-left: 4px solid #28a745;
}

.message-toast.error {
    border-left: 4px solid #dc3545;
}

.message-toast.warning {
    border-left: 4px solid #ffc107;
}

.close-toast {
    background: none;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    color: #666;
    padding: 0.25rem;
    border-radius: 4px;
}

.close-toast:hover,
.close-toast:focus {
    background: #f8f9fa;
    color: #333;
}

/* Responsive Design */
@media (max-width: 768px) {
    .app-container {
        padding: 0.5rem;
    }
    
    .app-header {
        padding: 1rem;
        flex-direction: column;
        gap: 1rem;
        align-items: flex-start;
    }
    
    .header-actions {
        width: 100%;
        justify-content: space-between;
    }
    
    .app-nav {
        padding: 1rem;
        flex-wrap: wrap;
    }
    
    .tab-content {
        padding: 1rem;
    }
    
    .scanner-controls,
    .manual-input,
    .booking-controls,
    .action-buttons {
        flex-direction: column;
        align-items: stretch;
    }
    
    .search-row,
    .filter-row,
    .sort-row {
        flex-direction: column;
        align-items: stretch;
    }
    
    .results-header {
        flex-direction: column;
        gap: 0.5rem;
        align-items: flex-start;
    }
    
    .overview-stats {
        grid-template-columns: 1fr;
    }
    
    .overview-actions {
        flex-direction: column;
        align-items: stretch;
    }
    
    .info-grid {
        grid-template-columns: 1fr;
    }
    
    .message-toast {
        top: 1rem;
        right: 1rem;
        left: 1rem;
        max-width: none;
    }
}

@media (max-width: 480px) {
    .login-card {
        padding: 1.5rem;
    }
    
    .tab-content {
        padding: 1rem;
    }
    
    .device-info {
        padding: 1rem;
    }
    
    .search-form {
        padding: 1rem;
    }
    
    .search-results {
        padding: 1rem;
    }
    
    .results-table th,
    .results-table td {
        padding: 0.5rem;
        font-size: 0.85rem;
    }
    
    .stat-card {
        padding: 1rem;
    }
    
    .stat-content h3 {
        font-size: 1.5rem;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .section {
        background: white;
        border: 2px solid #000;
    }
    
    .btn-primary {
        background: #000;
        border: 2px solid #000;
    }
    
    .btn-secondary {
        background: white;
        border: 2px solid #000;
        color: #000;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    .scanner-line {
        animation: none;
    }
}


/* Mobile camera optimizations */
@media screen and (max-width: 768px) {
    .scanner-container {
        max-width: 100%;
        margin: 0;
    }
    
    #scanner-video {
        /* Ensure video fills container on mobile */
        width: 100%;
        height: 100%;
        object-fit: cover;
        /* Prevent video from being too small on mobile */
        min-height: 250px;
    }
    
    /* Improve touch targets for mobile */
    .btn {
        min-height: 44px;
        min-width: 44px;
        padding: 0.75rem 1rem;
    }
    
    /* Better spacing for mobile scanner controls */
    .scanner-controls {
        gap: 0.75rem;
        flex-wrap: wrap;
        justify-content: center;
    }
    
    /* Optimize manual input for mobile */
    .manual-input {
        width: 100%;
        margin-top: 1rem;
    }
    
    .manual-input input {
        font-size: 16px; /* Prevent zoom on iOS */
        padding: 0.75rem;
    }
}

/* iOS Safari specific fixes */
@supports (-webkit-appearance: none) {
    #scanner-video {
        -webkit-playsinline: true;
        playsinline: true;
    }
}

/* Android Chrome specific fixes */
@media screen and (max-width: 768px) and (-webkit-min-device-pixel-ratio: 1) {
    .scanner-container {
        /* Ensure proper aspect ratio on Android */
        aspect-ratio: 4/3;
    }
    
    #scanner-video {
        /* Better video rendering on Android */
        transform: scaleX(-1); /* Mirror for front camera */
    }
    
    /* Restore normal orientation for back camera */
    .scanner-container.back-camera #scanner-video {
        transform: none;
    }
}

/* Landscape orientation optimizations */
@media screen and (orientation: landscape) and (max-height: 500px) {
    .scanner-container {
        max-height: 70vh;
    }
    
    .main-content {
        padding: 0.5rem;
    }
}

/*
 * Modal für die Korrektur eines gebuchten Geräts.
 * Der Modal wird verwendet, wenn Techniker ihre eigenen Buchungen korrigieren möchten.
 */
#user-correct-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

#user-correct-modal .modal-content {
    background: rgba(255, 255, 255, 0.97);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    padding: 2rem;
    width: 90%;
    max-width: 420px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

#user-correct-modal .modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

#user-correct-modal .modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: #333;
}

#user-correct-modal .form-group {
    margin-bottom: 1rem;
}

#user-correct-modal input {
    width: 100%;
    padding: 0.75rem;
    border: 2px solid #e1e5e9;
    border-radius: 6px;
    font-size: 0.9rem;
    transition: border-color 0.3s ease;
    background: white;
}

#user-correct-modal input:focus {
    border-color: #667eea;
    outline: none;
}

/* Unknown device modal styles. Mirrors the user-correct modal but adapts size for multiple inputs. */
#unknown-device-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

#unknown-device-modal .modal-content {
    background: rgba(255, 255, 255, 0.97);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    padding: 2rem;
    width: 90%;
    max-width: 520px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

#unknown-device-modal .modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

#unknown-device-modal .modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: #333;
}

#unknown-device-modal .form-group {
    margin-bottom: 1rem;
}

#unknown-device-modal .form-group label {
    display: block;
    margin-bottom: 0.25rem;
    font-weight: 600;
}

#unknown-device-modal .form-group input {
    width: 100%;
    padding: 0.65rem;
    border: 2px solid #e1e5e9;
    border-radius: 6px;
    font-size: 0.9rem;
    transition: border-color 0.3s ease;
    background: white;
}

#unknown-device-modal .form-group input:focus {
    border-color: #667eea;
    outline: none;
}

/*
 * Unknown device scanner modal styles.  When the user launches the QR‑Scanner
 * from the unknown device form, this modal covers the entire application
 * (similar to the other modals) and centers the video feed on screen.  A
 * higher z-index ensures the scanner is always above the underlying
 * unknown device form so that the video does not appear "behind" the form.
 */
#unknown-scan-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: none;
    align-items: center;
    justify-content: center;
    /* Use a z-index higher than other modals (1000) so this scanner
       overlay always appears in front. */
    z-index: 1100;
}

#unknown-scan-modal .modal-content {
    background: rgba(255, 255, 255, 0.97);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    padding: 2rem;
    width: 90%;
    max-width: 520px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

#unknown-scan-modal .modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

#unknown-scan-modal .modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: #333;
}

#unknown-device-modal .form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-top: 1rem;
}

/*
 * Mirror correction for the unknown device scanner.  When scanning
 * using the back camera in the unknown device modal, we add a
 * `.back-camera` class to the container so the video feed is not
 * horizontally flipped.  This mirrors the behavior of the main
 * scanner container which uses `.scanner-container.back-camera`.
 */
#unknown-scanner-container.back-camera #unknown-scanner-video {
    transform: none;
}

/*
 * Disable mirroring on the actual <video> element when using the back camera.
 * The html5‑qrcode library sometimes applies a transform to the video tag
 * directly.  By overriding it here with !important we ensure the preview
 * remains correctly oriented.
 */
.scanner-container.back-camera video,
#unknown-scanner-container.back-camera video,
#unknown-scan-modal.back-camera video {
    transform: none !important;
}

/*
 * Ensure that the scanner container within the unknown device modal is
 * always rendered above the underlying form fields. Without an explicit
 * z-index, the generated video element can appear behind inputs or
 * labels, making it seem wie der Scanner "hinter dem Formular" liegt.
 */
#unknown-scanner-container {
    position: relative;
    z-index: 1100;
}


/* =====================================================================
   Offline-Modus (Stand 07/2026)
   ===================================================================== */
.conn-status {
    font-size: 0.85rem;
    font-weight: 600;
    padding: 4px 10px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.15);
    color: #2ecc71;
    white-space: nowrap;
}

.conn-status.offline {
    color: #f39c12;
}

#sync-btn {
    white-space: nowrap;
}
