/* ============================================
   Mobile-First CSS Overrides & Fixes
   نظام تقييم لجنة الاستقطاب
   ============================================ */

/* Safe Area Support for Notch Devices */
:root {
    --safe-area-inset-top: env(safe-area-inset-top, 0px);
    --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-inset-left: env(safe-area-inset-left, 0px);
    --safe-area-inset-right: env(safe-area-inset-right, 0px);
}

/* ============================================
   Base Mobile Improvements
   ============================================ */
html,
body {
    -webkit-text-size-adjust: 100%;
    /* Prevent font scaling in landscape */
    -moz-text-size-adjust: 100%;
    text-size-adjust: 100%;
    overscroll-behavior-y: none;
    /* Prevent pull-to-refresh if not needed */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Prevent horizontal scroll on all devices */
html {
    overflow-x: hidden;
    width: 100%;
}

body {
    overflow-x: hidden;
    width: 100%;
    max-width: 100vw;
    position: relative;
}

/* ============================================
   Login Page Mobile Fixes
   ============================================ */
@media (max-width: 768px) {
    /* Ensure body allows scrolling when keyboard opens */
    body.login-page {
        overflow-y: auto !important;
        min-height: 100dvh;
        /* Dynamic viewport height */
        align-items: flex-start;
        /* Start from top on mobile to avoid centering issues with keyboard */
        padding-top: max(1rem, var(--safe-area-inset-top)) !important;
        padding-bottom: max(2rem, var(--safe-area-inset-bottom)) !important;
    }

    .login-wrapper {
        padding: 0 1rem 2rem 1rem;
        box-sizing: border-box;
        width: 100%;
        max-width: 100%;
        margin-top: auto;
        margin-bottom: auto;
    }

    .login-container {
        padding: 2rem 1.5rem !important;
        width: 100% !important;
        max-width: 100%;
        margin: 0 auto;
        border-radius: 20px !important;
        box-sizing: border-box;
        /* Simplify glass effect for performance/readability */
        backdrop-filter: none !important;
        background: rgba(255, 255, 255, 0.98) !important;
    }

    .login-logo img {
        width: 100px !important;
        /* Smaller logo on mobile */
        height: 100px !important;
    }

    .login-title {
        font-size: 1.25rem !important;
    }

    /* Ensure inputs are easy to tap and text is visible */
    .form-control {
        min-height: 48px !important;
        /* Minimum touch target */
        font-size: 16px !important;
        /* Critical: Prevent iOS zoom on focus */
        padding: 0.75rem 1rem !important;
        line-height: 1.5 !important;
        box-sizing: border-box !important;
        -webkit-appearance: none !important;
        appearance: none !important;
        /* Fix text clipping on iOS */
        -webkit-user-select: text !important;
        user-select: text !important;
    }

    .login-btn {
        min-height: 48px !important;
        font-size: 1.1rem !important;
        padding: 12px !important;
        margin-top: 1.5rem !important;
        width: 100% !important;
    }
}

/* Extra small screens */
@media (max-width: 480px) {
    .login-container {
        padding: 1.75rem 1.25rem !important;
    }

    .login-logo img {
        width: 80px !important;
        height: 80px !important;
    }
}

/* ============================================
   Public Application Form Mobile Fixes
   ============================================ */
@media (max-width: 768px) {
    /* Container adjustments */
    .container {
        padding-left: 15px !important;
        padding-right: 15px !important;
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    /* Prevent horizontal overflow */
    * {
        max-width: 100%;
    }

    /* Card adjustments */
    .card {
        border-radius: 12px !important;
        border: none !important;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05) !important;
        width: 100% !important;
        box-sizing: border-box !important;
    }

    .card-body {
        padding: 1.25rem !important;
        box-sizing: border-box !important;
    }

    /* Typography */
    h1.page-title {
        font-size: 1.5rem !important;
        line-height: 1.3 !important;
        margin-bottom: 1rem !important;
        word-wrap: break-word;
    }

    /* Form Elements - Critical fixes for input visibility */
    .form-label {
        font-size: 1rem !important;
        margin-bottom: 0.5rem !important;
        display: block;
    }

    /* All input types - prevent iOS zoom and ensure visibility */
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="number"],
    input[type="date"],
    input[type="password"],
    select.form-select,
    select.form-control,
    textarea.form-control {
        font-size: 16px !important;
        /* Critical: Prevent iOS zoom on focus */
        min-height: 48px !important;
        /* Touch friendly */
        border-radius: 8px !important;
        padding: 12px 14px !important;
        box-sizing: border-box !important;
        width: 100% !important;
        max-width: 100% !important;
        line-height: 1.5 !important;
        /* Fix text clipping on iOS Safari */
        -webkit-appearance: none !important;
        appearance: none !important;
        -webkit-user-select: text !important;
        user-select: text !important;
        /* Ensure text is visible while typing */
        -webkit-text-size-adjust: 100% !important;
    }

    /* Textarea specific */
    textarea.form-control {
        min-height: 100px !important;
        resize: vertical;
    }

    /* File Input Styling */
    input[type="file"] {
        padding: 10px !important;
        height: auto !important;
        min-height: 48px !important;
        font-size: 16px !important;
    }

    /* Radio buttons and checkboxes */
    input[type="radio"],
    input[type="checkbox"] {
        min-width: 20px !important;
        min-height: 20px !important;
        margin-left: 0.5rem;
    }

    /* Buttons - Full width on mobile */
    .btn,
    .btn-primary,
    button[type="submit"] {
        width: 100% !important;
        display: block !important;
        margin-bottom: 10px !important;
        min-height: 48px !important;
        font-size: 1.1rem !important;
        border-radius: 10px !important;
        box-sizing: border-box !important;
        padding: 12px 20px !important;
    }

    /* Row and column adjustments */
    .row {
        margin-left: -7.5px !important;
        margin-right: -7.5px !important;
    }

    .row > * {
        padding-left: 7.5px !important;
        padding-right: 7.5px !important;
    }

    /* Make columns stack on mobile */
    .col-md-6,
    .col-md-12 {
        width: 100% !important;
        flex: 0 0 100% !important;
        max-width: 100% !important;
        margin-bottom: 1rem;
    }

    /* Instruction Box */
    .alert-info {
        font-size: 0.9rem !important;
        padding: 1rem !important;
        word-wrap: break-word;
    }

    /* Timer */
    .timer-msg {
        font-size: 1rem !important;
        padding: 0.75rem !important;
        margin-bottom: 1.5rem !important;
        border-radius: 8px !important;
    }
}

/* Extra small screens */
@media (max-width: 480px) {
    .container {
        padding-left: 10px !important;
        padding-right: 10px !important;
    }

    .card-body {
        padding: 1rem !important;
    }

    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="number"],
    input[type="date"],
    input[type="password"],
    select.form-select,
    textarea.form-control {
        padding: 10px 12px !important;
    }
}

/* ============================================
   iOS Safari Specific Fixes
   ============================================ */
@supports (-webkit-touch-callout: none) {
    /* iOS Safari only */
    input,
    select,
    textarea {
        -webkit-user-select: text !important;
        user-select: text !important;
        /* Fix text selection and visibility */
    }

    /* Prevent zoom on focus */
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="number"],
    input[type="date"],
    input[type="password"],
    select,
    textarea {
        font-size: 16px !important;
    }

    /* Fix for input text clipping */
    input:focus,
    select:focus,
    textarea:focus {
        -webkit-appearance: none !important;
        appearance: none !important;
        transform: translateZ(0);
        /* Force hardware acceleration */
        -webkit-transform: translateZ(0);
    }
}

/* ============================================
   Android Chrome Specific Fixes
   ============================================ */
@media screen and (-webkit-min-device-pixel-ratio: 0) {
    input,
    select,
    textarea {
        font-size: 16px !important;
        /* Prevent zoom */
    }
}

/* ============================================
   General Mobile Layout Fixes
   ============================================ */
@media (max-width: 768px) {
    /* Prevent any element from causing horizontal scroll */
    img,
    video,
    iframe,
    embed,
    object {
        max-width: 100% !important;
        height: auto !important;
    }

    /* Table responsiveness */
    table {
        width: 100% !important;
        display: block !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
    }

    /* Prevent fixed widths on mobile */
    [style*="width"] {
        max-width: 100% !important;
    }

    /* Ensure all containers are responsive */
    .container-fluid,
    .container {
        width: 100% !important;
        max-width: 100% !important;
        padding-left: 15px !important;
        padding-right: 15px !important;
        box-sizing: border-box !important;
    }
}

/* ============================================
   PWA Standalone Improvements
   ============================================ */
@media all and (display-mode: standalone) {
    body {
        padding-top: max(20px, var(--safe-area-inset-top));
        padding-bottom: max(20px, var(--safe-area-inset-bottom));
        padding-left: max(20px, var(--safe-area-inset-left));
        padding-right: max(20px, var(--safe-area-inset-right));
    }
}

/* ============================================
   Touch Target Improvements
   ============================================ */
@media (max-width: 768px) {
    /* Ensure all interactive elements are touch-friendly */
    a,
    button,
    input[type="button"],
    input[type="submit"],
    input[type="radio"],
    input[type="checkbox"],
    select {
        min-height: 44px !important;
        min-width: 44px !important;
    }

    /* Increase spacing between form fields */
    .form-group,
    .mb-3 {
        margin-bottom: 1.5rem !important;
    }
}

/* ============================================
   Performance Optimizations for Mobile
   ============================================ */
@media (max-width: 768px) {
    /* Reduce shadow blur for better performance */
    * {
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1) !important;
    }

    /* Disable heavy animations on mobile */
    *,
    *::before,
    *::after {
        animation-duration: 0.3s !important;
        transition-duration: 0.3s !important;
    }
}

/* ============================================
   Auto-scroll to focused input (JavaScript helper)
   ============================================ */
/* This will be handled by JavaScript, but we ensure inputs are scrollable */
@media (max-width: 768px) {
    input:focus,
    select:focus,
    textarea:focus {
        scroll-margin-top: 100px;
        /* Space above focused element */
    }
}
