/* Mobile-specific fixes for iPhone and Safari */

/* Fix for iPhone viewport issues */
@supports (-webkit-appearance: none) and (stroke-color: transparent) {
    body {
        background-attachment: scroll !important;
    }
}

/* Fix for iPhone safe areas (notch devices) */
@supports (padding: max(0px)) {
    .dashboard-header {
        padding-top: max(env(safe-area-inset-top), 1rem);
        padding-left: max(env(safe-area-inset-left), 0);
        padding-right: max(env(safe-area-inset-right), 0);
    }
    
    .sidebar {
        padding-top: max(env(safe-area-inset-top), 60px);
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }
    
    .container {
        padding-left: max(env(safe-area-inset-left), 1rem);
        padding-right: max(env(safe-area-inset-right), 1rem);
        padding-bottom: max(env(safe-area-inset-bottom), 1rem);
    }
}

/* Fix for iOS input styling */
@media screen and (-webkit-min-device-pixel-ratio: 0) {
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    select,
    textarea {
        -webkit-appearance: none;
        appearance: none;
        border-radius: 8px;
        background-color: #ffffff;
        border: 2px solid #e8f5e8;
    }
    
    button,
    input[type="submit"],
    .btn {
        -webkit-appearance: none;
        appearance: none;
        border-radius: 10px;
    }
}

/* Fix for iOS table scroll performance */
.table-responsive {
    -webkit-overflow-scrolling: touch;
    transform: translateZ(0); /* Creates a new layer for better scrolling */
}

/* Prevent iOS bounce scroll */
body {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: none;
}

/* Fix for iOS font rendering */
* {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Improve iOS form element styling */
@media screen and (max-device-width: 768px) {
    /* Prevent zoom on input focus */
    input:focus,
    select:focus,
    textarea:focus {
        font-size: 16px !important;
        transform: translateZ(0);
    }
    
    /* Fix for iOS select styling */
    select {
        background-image: none;
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
        padding-right: 30px;
        background-image: linear-gradient(45deg, transparent 50%, #0a7840 50%),
                          linear-gradient(135deg, #0a7840 50%, transparent 50%);
        background-position: calc(100% - 20px) calc(1em + 2px),
                           calc(100% - 15px) calc(1em + 2px);
        background-size: 5px 5px, 5px 5px;
        background-repeat: no-repeat;
    }
    
    /* Fix sidebar on mobile landscape */
    @media (orientation: landscape) and (max-height: 500px) {
        .sidebar.open {
            width: 80vw;
            max-width: 320px;
        }
        
        .sidebar a {
            padding: 0.8rem 2rem;
            font-size: 1rem;
        }
    }
}

/* Fix for button active states on iOS */
button:active,
input[type="submit"]:active,
.btn:active,
a:active {
    -webkit-tap-highlight-color: rgba(45, 153, 112, 0.3);
    transform: scale(0.95);
}

/* Remove iOS button styling */
button,
input[type="submit"],
.btn {
    -webkit-appearance: none;
    appearance: none;
    -webkit-user-select: none;
    user-select: none;
}

/* Fix for iOS text selection */
.dashboard-header,
.menu-btn,
button,
.btn {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* Improve iOS touch targets */
@media (pointer: coarse) {
    .nav-btn,
    button,
    .btn,
    input[type="submit"],
    a {
        min-height: 44px;
        min-width: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .sidebar a {
        min-height: 50px;
        word-wrap: break-word;
        overflow-wrap: break-word;
        white-space: normal;
        line-height: 1.3;
    }
}

/* Fix for iOS landscape keyboard issues */
@media screen and (max-device-width: 768px) and (orientation: landscape) {
    .container {
        height: auto !important;
        min-height: auto !important;
    }
    
    body {
        height: auto !important;
    }
}

/* Performance improvements for iOS */
.dashboard-card,
.prediction-panel,
.matches-table,
.sidebar {
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

/* Fix iOS sticky header if needed */
.dashboard-header {
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
}

/* Mobile sidebar text overflow fixes */
@media (max-width: 768px) {
    .sidebar {
        overflow-x: hidden;
    }
    
    .sidebar a {
        padding: 1rem 1.5rem;
        font-size: 1.1rem;
        word-wrap: break-word;
        overflow-wrap: break-word;
        white-space: normal;
        line-height: 1.4;
        text-overflow: ellipsis;
        overflow: hidden;
        display: block;
        width: 100%;
        box-sizing: border-box;
    }
    
    /* Ensure sidebar is wide enough for text */
    .sidebar.open {
        width: 100vw !important;
        max-width: none;
    }
}

@media (max-width: 480px) {
    .sidebar a {
        padding: 0.8rem 1rem;
        font-size: 1rem;
        letter-spacing: 0.5px;
    }
}

/* For very small screens, allow text wrapping if needed */
@media (max-width: 350px) {
    .sidebar a {
        white-space: normal !important;
        line-height: 1.3;
        word-break: break-word;
        hyphens: auto;
    }
    
    .sidebar.open {
        width: 100vw !important;
        max-width: none;
    }
}

/* Fix for very long Finnish words */
.sidebar a[href*="register"],
.sidebar a[href*="Rekisteröidy"] {
    word-break: break-word;
    hyphens: auto;
    -webkit-hyphens: auto;
    -moz-hyphens: auto;
}

/* Specific fix for Finnish text overflow in sidebar */
@media (max-width: 768px) {
    .sidebar {
        overflow-x: visible !important;
        overflow-y: auto;
    }
    
    .sidebar.open {
        width: 100vw !important;
        max-width: 400px;
    }
    
    .sidebar a {
        /* Ensure text doesn't get cut off */
        white-space: nowrap;
        overflow: visible !important;
        text-overflow: unset !important;
        padding: 1rem 1.5rem;
        font-size: 1rem;
        width: auto;
        box-sizing: border-box;
        letter-spacing: 0.5px;
    }
    
    /* Special handling for register link with long Finnish text */
    .sidebar a[href*="/register/"] {
        font-size: 0.95rem;
        padding: 1rem 1.2rem;
        letter-spacing: 0.3px;
    }
}