/* ==========================================================================
   Perfex CRM PWA - Immersive Mobile App Shell & Design System
   Auteur : Ir. Roland MANTAMA
   ========================================================================== */

/* Position du creux : déclarée pour que le navigateur sache l'animer (sans support, le creux saute d'un onglet à l'autre) */
@property --pwa-notch-x {
    syntax: '<length>';
    inherits: true;
    initial-value: -100px;
}

:root {
    --pwa-primary: #3B82F6;
    --pwa-primary-hover: #1D4ED8;
    --pwa-primary-glow: rgba(37, 99, 235, 0.42);
    --pwa-dock-bg: rgba(15, 23, 42, 0.86);
    --pwa-dock-border: rgba(255, 255, 255, 0.12);
    --pwa-dock-surface: linear-gradient(180deg, #FFFFFF 0%, #EEF4FE 45%, #DFEAFB 100%);
    --pwa-dock-shadow: drop-shadow(0 -1px 0 rgba(148, 163, 184, 0.35)) drop-shadow(0 -10px 20px rgba(30, 64, 175, 0.16));
    --pwa-pill-active: rgba(37, 99, 235, 0.13);
    --pwa-pill-hover: rgba(37, 99, 235, 0.07);
    --pwa-ring: #FFFFFF;
    --pwa-text-active: #1D4ED8;
    --pwa-text-idle: #475569;
    --pwa-text-on-primary: #FFFFFF;
    --pwa-focus: #2563EB;
    --pwa-ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
    /* Géométrie de la barre : la vague garde sa taille et se déplace, les côtés s'étirent */
    --pwa-dock-overhang: 26px; /* dépassement de la bulle au-dessus du bord */
    --pwa-ease-travel: cubic-bezier(0.65, 0, 0.35, 1);
    --pwa-dock-height: 72px;
    --pwa-dock-safe: env(safe-area-inset-bottom, 0px);
    --pwa-dock-wave: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='44' viewBox='0 0 160 44'%3E%3Cpath d='M0 0C26 0 36 2 46 15C56 29 66 36 80 36C94 36 104 29 114 15C124 2 134 0 160 0V44H0Z'/%3E%3C/svg%3E");
}

/* Prise en compte de la Safe Area sur les terminaux avec encoche (iPhone / Android) */
/* ≤ 768 px uniquement : au-delà la barre est masquée, la marge laissait un vide de 74 px sous chaque page */
@media (max-width: 768px) {
    body.pwa-has-bottom-bar {
        padding-bottom: calc(var(--pwa-dock-height) + 12px + var(--pwa-dock-safe)) !important;
    }
}

/* ==========================================================================
   1. Barre de navigation mobile : le creux de la vague suit l'onglet actif
   ========================================================================== */
#pwa-bottom-bar,
.pwa-bottom-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(var(--pwa-dock-height) + var(--pwa-dock-safe));
    padding: 0 max(4px, env(safe-area-inset-right)) var(--pwa-dock-safe) max(4px, env(safe-area-inset-left));
    display: flex;
    align-items: flex-end;
    /* L'ombre suit la découpe : box-shadow resterait rectangulaire */
    filter: var(--pwa-dock-shadow);
    /* Native Bootstrap fixed layer, below modal/backdrop (1040/1050). */
    --pwa-z-dock: 1030;
    z-index: var(--pwa-z-dock);
    transition: transform 0.32s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.32s ease;
    user-select: none;
    -webkit-user-select: none;
}

/* Glissement du creux, activé après le premier placement (pwa-client.js) : pas d'animation au chargement */
#pwa-bottom-bar.pwa-dock-ready {
    transition: transform 0.32s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.32s ease, --pwa-notch-x 0.46s var(--pwa-ease-travel);
}

/* Surface découpée : deux côtés pleins, la vague à la position du creux, le bas plein.
   Raccords de 0,5 px contre les liserés. */
.pwa-bottom-bar::before {
    content: '';
    position: absolute;
    inset: 0;
    z-index: -1;
    border-radius: 26px 26px 0 0;
    background: var(--pwa-dock-surface);
    --pwa-mask:
        linear-gradient(#000 0 0) left top / max(0px, calc(var(--pwa-notch-x) - 79.5px)) 100% no-repeat,
        linear-gradient(#000 0 0) right top / max(0px, calc(100% - var(--pwa-notch-x) - 79.5px)) 100% no-repeat,
        var(--pwa-dock-wave) calc(var(--pwa-notch-x) - 80px) 0 / 160px 44px no-repeat,
        linear-gradient(#000 0 0) center bottom / 100% calc(100% - 43.5px) no-repeat;
    -webkit-mask: var(--pwa-mask);
    mask: var(--pwa-mask);
}

/* Masquage intelligent au défilement vers le bas */
/* Synchronous lock, independent of scroll direction and animation timing. */
body.modal-open #pwa-bottom-bar,
body.modal-open .pwa-bottom-bar {
    visibility: hidden;
    pointer-events: none;
}

.pwa-bottom-bar.pwa-bar-hidden {
    transform: translateY(calc(100% + var(--pwa-dock-overhang) + 8px));
    opacity: 0;
    pointer-events: none;
}

@media (max-width: 768px) {
    /* Bulle de discussion : au-dessus de la bulle de l'onglet actif. translate et non bottom :
       le module de discussion pose bottom en style inline et le modifie quand on déplace sa bulle */
    body.pwa-has-bottom-bar .chat-fab {
        translate: 0 -34px;
    }

    /* Menu latéral ouvert : la barre couvrait ses dernières entrées */
    body.show-sidebar #pwa-bottom-bar {
        visibility: hidden;
        pointer-events: none;
    }

    /* Formulaires : leur barre d'enregistrement occupe le bas de l'écran et passait sous la barre.
       Barre fixe du cœur (hors modales) ou pied de formulaire collant du thème Nova UI. */
    html.nui-form-page #pwa-bottom-bar {
        display: none;
    }

    html.nui-form-page body.pwa-has-bottom-bar {
        padding-bottom: 0 !important;
    }

    /* Règles séparées : un navigateur sans :has() ignorerait aussi le sélecteur voisin */
    body:has(.btn-bottom-toolbar:not(.modal *)) #pwa-bottom-bar {
        display: none;
    }

    body.pwa-has-bottom-bar:has(.btn-bottom-toolbar:not(.modal *)) {
        padding-bottom: 0 !important;
    }
}

/* Onglets */
.pwa-nav-item {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    gap: 2px;
    flex: 1 1 0;
    min-width: 0;
    height: 60px;
    padding-bottom: 6px;
    text-decoration: none !important;
    color: var(--pwa-text-idle) !important;
    transition: color 0.2s ease;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.pwa-nav-item.active {
    color: var(--pwa-text-active) !important;
}

/* Icône : l'onglet quitté redescend aussitôt ; le nouvel onglet attend que le creux arrive, puis sa bulle sort */
.pwa-nav-icon {
    position: relative;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.18s ease-in, color 0.1s ease;
}

.pwa-nav-icon::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 54px;
    height: 54px;
    margin: -27px 0 0 -27px;
    border-radius: 50%;
    background: linear-gradient(145deg, var(--pwa-primary), var(--pwa-primary-hover));
    box-shadow: 0 0 0 4px var(--pwa-ring), 0 10px 20px -4px var(--pwa-primary-glow), inset 0 1px 0 rgba(255, 255, 255, 0.3);
    opacity: 0;
    transform: scale(0.3);
    transition: transform 0.16s ease-in, opacity 0.12s ease;
}

.pwa-nav-item.active .pwa-nav-icon,
.pwa-nav-item--rest .pwa-nav-icon {
    color: var(--pwa-text-on-primary);
    transform: translateY(-33px);
    transition: transform 0.34s var(--pwa-ease-spring) 0.26s, color 0.1s ease 0.26s;
}

.pwa-nav-item.active .pwa-nav-icon::before,
.pwa-nav-item--rest .pwa-nav-icon::before {
    opacity: 1;
    transform: scale(1);
    transition: transform 0.34s var(--pwa-ease-spring) 0.26s, opacity 0.12s ease 0.26s;
}

/* Premier affichage : l'onglet de la page est déjà en place, sans attente */
.pwa-bottom-bar:not(.pwa-dock-ready) .pwa-nav-icon,
.pwa-bottom-bar:not(.pwa-dock-ready) .pwa-nav-icon::before {
    transition: none;
}

.pwa-nav-icon svg {
    position: relative;
    width: 22px;
    height: 22px;
}

.pwa-nav-item:not(.active):not(.pwa-nav-item--rest):active .pwa-nav-icon {
    transform: scale(0.88);
}

.pwa-nav-label {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 0;
    line-height: 14px;
}

.pwa-nav-item.active .pwa-nav-label {
    font-weight: 700;
}

@media (hover: hover) {
    .pwa-nav-item:not(.active):hover {
        color: var(--pwa-text-active) !important;
    }
}

/* ==========================================================================
   2. Pull to Refresh Gesture Indicator
   ========================================================================== */
.pwa-pull-indicator {
    position: fixed;
    top: -50px;
    left: 50%;
    transform: translateX(-50%);
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: var(--pwa-dock-bg);
    border: 1px solid var(--pwa-dock-border);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--pwa-primary);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.35);
    z-index: 100000;
    transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.2s ease;
    opacity: 0;
    pointer-events: none;
}

.pwa-pull-indicator.visible {
    opacity: 1;
}

.pwa-pull-indicator.refreshing svg {
    animation: pwaSpin 0.8s linear infinite;
}

@keyframes pwaSpin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* ==========================================================================
   3. Bannière d'Installation Intelligente A2HS
   ========================================================================== */
.pwa-install-banner {
    position: fixed;
    bottom: calc(var(--pwa-dock-height) + 24px + var(--pwa-dock-safe));
    left: 16px;
    right: 16px;
    max-width: 460px;
    margin: 0 auto;
    background: linear-gradient(135deg, rgba(30, 41, 59, 0.95), rgba(15, 23, 42, 0.98));
    border: 1px solid rgba(255, 255, 255, 0.12);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-radius: 18px;
    padding: 16px;
    display: flex;
    align-items: center;
    gap: 14px;
    box-shadow: 0 16px 36px rgba(0, 0, 0, 0.45);
    z-index: 99998;
    animation: slideUpBanner 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

.pwa-banner-icon {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    flex-shrink: 0;
    object-fit: cover;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.pwa-banner-content {
    flex: 1;
    min-width: 0;
}

.pwa-banner-title {
    font-size: 14px;
    font-weight: 700;
    color: #F8FAFC;
    margin-bottom: 2px;
}

.pwa-banner-desc {
    font-size: 12px;
    color: #94A3B8;
    line-height: 1.3;
}

.pwa-banner-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.pwa-btn-install {
    padding: 8px 14px;
    background: linear-gradient(135deg, #3B82F6, #2563EB);
    color: #FFFFFF;
    border: none;
    border-radius: 10px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
}

.pwa-btn-dismiss {
    background: transparent;
    border: none;
    color: #64748B;
    cursor: pointer;
    padding: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
}

@keyframes slideUpBanner {
    from { transform: translateY(30px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

/* Masquer la Bottom Bar sur Desktop (> 768px) */
@media (min-width: 769px) {
    #pwa-bottom-bar,
    .pwa-bottom-bar,
    .pwa-pull-indicator {
        display: none !important;
    }

    /* Pas de barre sur ordinateur : la bannière n'a pas à la contourner */
    .pwa-install-banner {
        bottom: 24px;
    }
}

/* v1.1.3 : focus clavier visible (barre, bannière), mouvement réduit respecté */
.pwa-nav-item:focus-visible,
.pwa-btn-install:focus-visible,
.pwa-btn-dismiss:focus-visible { outline: 2px solid var(--pwa-focus); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) {
    .pwa-bottom-bar, #pwa-bottom-bar, #pwa-bottom-bar.pwa-dock-ready, .pwa-nav-item.active .pwa-nav-icon, .pwa-nav-item.active .pwa-nav-icon::before, .pwa-nav-item--rest .pwa-nav-icon, .pwa-nav-item--rest .pwa-nav-icon::before, .pwa-nav-item, .pwa-nav-icon, .pwa-nav-icon::before, .pwa-install-banner, .pwa-pull-indicator, .pwa-pull-indicator svg {
        animation: none !important;
        transition: none !important;
    }
}
