/* =====================================================
   FREEWORKDZ v2.0 — MOBILE ENHANCEMENTS
   Optimisations critiques pour mobile (iOS + Android)
   ===================================================== */

/* ═══════════════════════════════════════════════════════
   1. SAFE AREA (iPhone X+ notch / Dynamic Island)
═══════════════════════════════════════════════════════ */
@supports (padding: env(safe-area-inset-top)) {
    body {
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
    }

    .navbar {
        padding-top: env(safe-area-inset-top) !important;
    }

    .sidebar {
        padding-top: calc(env(safe-area-inset-top) + var(--navbar-height, 70px)) !important;
        padding-bottom: env(safe-area-inset-bottom) !important;
    }

    /* Bottom fixed elements */
    #pwaInstallBanner,
    .mobile-bottom-nav,
    .sticky-mobile-cta {
        padding-bottom: calc(0.875rem + env(safe-area-inset-bottom)) !important;
    }
}


/* ═══════════════════════════════════════════════════════
   2. FORMS — Empêcher le zoom iOS sur focus
   (iOS zoome automatiquement si font-size < 16px)
═══════════════════════════════════════════════════════ */
@media (max-width: 767px) {
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="number"],
    input[type="password"],
    input[type="search"],
    input[type="url"],
    input[type="date"],
    input[type="time"],
    select,
    textarea,
    .form-control {
        font-size: 16px !important;       /* Empêche le zoom iOS */
        -webkit-appearance: none;         /* Look natif iOS désactivé */
        appearance: none;
        border-radius: 10px;              /* Bordures uniformes */
    }

    /* Restaurer le radius des input groups */
    .input-group .form-control {
        border-radius: 0;
    }
    .input-group .form-control:first-child { border-top-left-radius: 10px; border-bottom-left-radius: 10px; }
    .input-group .form-control:last-child  { border-top-right-radius: 10px; border-bottom-right-radius: 10px; }

    /* Select avec icône custom */
    select.form-control {
        background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%236b7280'%3e%3cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'/%3e%3c/svg%3e");
        background-repeat: no-repeat;
        background-position: right 0.75rem center;
        background-size: 1rem;
        padding-right: 2.5rem;
    }

    /* Textarea — taille minimale pour confort */
    textarea.form-control {
        min-height: 90px;
        line-height: 1.5;
    }
}


/* ═══════════════════════════════════════════════════════
   3. TAP HIGHLIGHTING (touch feedback)
═══════════════════════════════════════════════════════ */
* {
    -webkit-tap-highlight-color: rgba(99, 102, 241, 0.15);
}

a, button, [role="button"], .btn, .card, .clickable {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    cursor: pointer;
}

/* Permettre la sélection sur le contenu */
p, h1, h2, h3, h4, h5, h6, .selectable {
    -webkit-user-select: text;
    user-select: text;
}

/* Active state pour feedback tactile */
@media (hover: none) and (pointer: coarse) {
    .btn:active,
    .card:active,
    .nav-link:active,
    .sidebar-link:active {
        transform: scale(0.98);
        transition: transform 0.1s ease;
    }
}


/* ═══════════════════════════════════════════════════════
   4. LAZY LOADING IMAGES — placeholder pendant chargement
═══════════════════════════════════════════════════════ */
img[loading="lazy"] {
    background: linear-gradient(110deg, #ececec 8%, #f5f5f5 18%, #ececec 33%);
    background-size: 200% 100%;
    animation: shimmer-loading 1.4s linear infinite;
}

img[loading="lazy"][src] {
    animation: none;
    background: none;
}

@keyframes shimmer-loading {
    to { background-position-x: -200%; }
}


/* ═══════════════════════════════════════════════════════
   5. STICKY BOTTOM CTA (mobile uniquement)
   La position bottom et la transition sont gérées par
   le JS auto-hide du bottom nav dans footer.php.
   Ce bloc définit uniquement le style visuel.
═══════════════════════════════════════════════════════ */
.sticky-mobile-cta {
    display: none;
}

@media (max-width: 767px) {
    .sticky-mobile-cta {
        display: flex;
        position: fixed;
        left: 0; right: 0;
        /* bottom est surchargé par footer.php */
        bottom: calc(68px + env(safe-area-inset-bottom, 0px));
        background: rgba(255,255,255,0.97);
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px);
        padding: 0.75rem 1rem;
        box-shadow: 0 -1px 0 rgba(0,0,0,0.08), 0 -4px 20px rgba(0,0,0,0.08);
        z-index: 1045; /* entre le contenu et le bottom nav */
        gap: 0.75rem;
        align-items: center;
        border-top: 1px solid rgba(0,0,0,0.07);
        min-height: 62px;
    }
    .sticky-mobile-cta .btn {
        flex-shrink: 0;
        min-height: 44px;
        font-weight: 700;
        font-size: 0.9rem;
        padding: 0.6rem 1.25rem;
        border-radius: 12px;
        white-space: nowrap;
    }
}


/* ═══════════════════════════════════════════════════════
   6. SCROLL SMOOTH + NO BOUNCE
═══════════════════════════════════════════════════════ */
html {
    scroll-behavior: smooth;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    overscroll-behavior-y: none;       /* Empêche le pull-to-refresh natif */
    -webkit-overflow-scrolling: touch;  /* Momentum scroll iOS */
}


/* ═══════════════════════════════════════════════════════
   7. IMAGES RESPONSIVE PAR DÉFAUT
═══════════════════════════════════════════════════════ */
img:not([width]):not(.no-resp) {
    max-width: 100%;
    height: auto;
}


/* ═══════════════════════════════════════════════════════
   8. NAVBAR MOBILE — éviter de cacher le contenu
═══════════════════════════════════════════════════════ */

/* Scroll offset global : compense la sticky navbar sur tous les anchors
   (solution moderne — pas besoin de [id]::before qui casse tout) */
html {
    scroll-padding-top: calc(var(--navbar-height, 66px) + 0.5rem);
}

@media (max-width: 767px) {
    .navbar {
        position: sticky;
        top: 0;
        z-index: 1100;
    }
}


/* ═══════════════════════════════════════════════════════
   9. MODAL MOBILE — full-screen sur petits écrans
═══════════════════════════════════════════════════════ */
@media (max-width: 480px) {
    .modal,
    .modal-dialog {
        margin: 0 !important;
        max-width: 100% !important;
        height: 100vh;
        height: 100dvh;
    }
    .modal-content {
        height: 100vh;
        height: 100dvh;
        border-radius: 0 !important;
    }
}


/* ═══════════════════════════════════════════════════════
   10. CARTES SERVICE/FREELANCER — meilleure UX tactile
═══════════════════════════════════════════════════════ */
@media (max-width: 767px) {
    .freelancer-search-card,
    .service-card {
        margin-bottom: 1rem;
    }

    /* Boutons CTA dans les cards : full width sur mobile */
    .card .btn-block-mobile {
        width: 100%;
        margin-top: 0.5rem;
    }

    /* Avatars dans les cards : taille adaptée */
    .freelancer-card .avatar,
    .service-card .avatar {
        width: 48px !important;
        height: 48px !important;
    }
}


/* ═══════════════════════════════════════════════════════
   11. TYPOGRAPHIE — meilleure lisibilité mobile
═══════════════════════════════════════════════════════ */
@media (max-width: 767px) {
    body {
        font-size: 15px;
        line-height: 1.55;
    }

    p {
        line-height: 1.6;
    }

    /* Liens plus visibles */
    a:not(.btn):not(.nav-link):not(.sidebar-link) {
        text-decoration: none;
        color: var(--primary, #6366f1);
    }
    a:not(.btn):not(.nav-link):not(.sidebar-link):active {
        opacity: 0.7;
    }

    /* Liens dans les paragraphes : underline pour clarté */
    p a {
        text-decoration: underline;
        text-decoration-thickness: 1px;
        text-underline-offset: 3px;
    }
}


/* ═══════════════════════════════════════════════════════
   12. DASHBOARD CARDS — espacement optimisé mobile
═══════════════════════════════════════════════════════ */
@media (max-width: 767px) {
    .dashboard-main {
        padding: 1rem 0.75rem !important;
    }

    .stat-cards-row {
        gap: 0.625rem !important;
    }

    .stat-card-mini {
        padding: 0.875rem 1rem !important;
    }

    .stat-card-mini .sval {
        font-size: 1.4rem !important;
    }

    .stat-card-mini .slbl {
        font-size: 0.7rem !important;
    }

    .stat-card-mini .sicon {
        width: 38px !important;
        height: 38px !important;
        font-size: 1rem !important;
    }

    .card {
        border-radius: 12px;
    }

    .card-body {
        padding: 1rem !important;
    }

    .card-header {
        padding: 0.875rem 1rem !important;
    }
}


/* ═══════════════════════════════════════════════════════
   13. NOTIFICATIONS BANNER — meilleure UX
═══════════════════════════════════════════════════════ */
@media (max-width: 480px) {
    .alert {
        border-radius: 10px;
        padding: 0.875rem 1rem;
        font-size: 0.875rem;
    }

    /* Toast position mobile (bottom) */
    .toast-container {
        bottom: calc(1rem + env(safe-area-inset-bottom, 0)) !important;
        left: 1rem !important;
        right: 1rem !important;
        top: auto !important;
    }
}


/* ═══════════════════════════════════════════════════════
   14. BUTTONS — meilleur contrast mobile + active states
═══════════════════════════════════════════════════════ */
@media (max-width: 767px) {
    .btn {
        font-size: 0.95rem;
        padding: 0.75rem 1.25rem;
        font-weight: 700;
        letter-spacing: 0.01em;
        border-radius: 10px;
    }

    .btn-lg {
        padding: 0.95rem 1.5rem;
        font-size: 1rem;
    }

    .btn-sm {
        padding: 0.5rem 0.875rem;
        font-size: 0.82rem;
    }

    /* Active state visible */
    .btn:active {
        transform: scale(0.97);
        filter: brightness(0.95);
    }
}


/* ═══════════════════════════════════════════════════════
   15. SIDEBAR MOBILE — animation + active state
═══════════════════════════════════════════════════════ */
@media (max-width: 767px) {
    .sidebar-link {
        padding: 0.875rem 1rem !important;
        margin: 0.15rem 0.5rem !important;
        font-size: 0.9rem;
    }

    .sidebar-link:active {
        transform: scale(0.98);
    }
}


/* ═══════════════════════════════════════════════════════
   16. PWA INSTALLED MODE
   (quand l'app est installée comme PWA)
═══════════════════════════════════════════════════════ */
@media (display-mode: standalone) {
    /* Hide install banner if app is already installed */
    #pwaInstallBanner { display: none !important; }

    /* Add extra padding for status bar on iOS PWA */
    body {
        padding-top: env(safe-area-inset-top, 20px);
    }

    .navbar {
        padding-top: 0 !important;
    }
}


/* ═══════════════════════════════════════════════════════
   17. IMAGES OPTIMIZATION (object-fit + aspect-ratio)
═══════════════════════════════════════════════════════ */
.profile-avatar,
.user-avatar,
.avatar {
    object-fit: cover;
    aspect-ratio: 1 / 1;
}

.service-card img,
.freelancer-card img {
    object-fit: cover;
    background: #f3f4f6;
}


/* ═══════════════════════════════════════════════════════
   18. ACCESSIBILITY — focus visible
═══════════════════════════════════════════════════════ */
@media (max-width: 767px) {
    /* Focus visible sur navigation tactile */
    *:focus-visible {
        outline: 3px solid rgba(99, 102, 241, 0.4);
        outline-offset: 2px;
    }
    .btn:focus-visible {
        outline-offset: 4px;
    }
}


/* ═══════════════════════════════════════════════════════
   19. MISC — petites améliorations UX
═══════════════════════════════════════════════════════ */

/* Empêche le drag-to-zoom sur les images */
img {
    -webkit-touch-callout: none;
    pointer-events: auto;
}

/* Scroll containers : indique qu'il y a du contenu */
.table-wrapper {
    position: relative;
}
.table-wrapper::after {
    content: '';
    position: sticky;
    top: 0; right: 0;
    width: 30px; height: 100%;
    background: linear-gradient(to right, transparent, rgba(255,255,255,0.9));
    pointer-events: none;
    display: none;
}

/* Disable text selection on icons */
.bi, [class^="bi-"] {
    -webkit-user-select: none;
    user-select: none;
}

/* Loading state for buttons */
.btn[disabled],
.btn.loading {
    opacity: 0.7;
    cursor: not-allowed;
    pointer-events: none;
}

/* Forms : meilleur affichage erreurs sur mobile */
@media (max-width: 480px) {
    .invalid-feedback {
        font-size: 0.78rem;
        margin-top: 0.3rem;
    }

    .form-text {
        font-size: 0.78rem;
        line-height: 1.4;
    }
}


/* ═══════════════════════════════════════════════════════
   20. DARK MODE PREP (pour plus tard)
   Détecte si l'OS de l'utilisateur est en dark mode
═══════════════════════════════════════════════════════ */
@media (prefers-color-scheme: dark) {
    /* Réservé pour future implémentation dark mode */
}


/* ═══════════════════════════════════════════════════════
   21. REDUCED MOTION (accessibilité)
═══════════════════════════════════════════════════════ */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}
