/**
 * PWA Mobile Enhancement CSS - Frontend Portale Clienti
 * Bottom Navigation, Transizioni, Pull-to-Refresh, Safe Area, Splash Screen
 */

/* ============================================
   CSS VARIABLES
   ============================================ */
:root {
    --pwa-primary: #1a73e8;
    --pwa-primary-dark: #1557b0;
    --pwa-accent: #1a73e8;
    --pwa-bottom-nav-height: 56px;
    --pwa-safe-bottom: env(safe-area-inset-bottom, 0px);
    --pwa-safe-top: env(safe-area-inset-top, 0px);
    --pwa-safe-left: env(safe-area-inset-left, 0px);
    --pwa-safe-right: env(safe-area-inset-right, 0px);
}

/* ============================================
   SAFE AREA - iPhone Notch/Dynamic Island
   ============================================ */
@supports (padding: env(safe-area-inset-top)) {
    .main-header.navbar {
        padding-top: var(--pwa-safe-top);
    }

    .content-wrapper {
        padding-left: var(--pwa-safe-left);
        padding-right: var(--pwa-safe-right);
    }
}

/* ============================================
   SPLASH SCREEN / LOADING OVERLAY
   ============================================ */
.pwa-splash {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #1a73e8 0%, #1557b0 100%);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 99999;
    transition: opacity 0.5s ease, visibility 0.5s ease;
}

.pwa-splash.hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.pwa-splash-logo {
    max-width: 180px;
    height: auto;
    margin-bottom: 24px;
    animation: pwa-pulse 1.5s ease-in-out infinite;
}

.pwa-splash-spinner {
    width: 36px;
    height: 36px;
    border: 3px solid rgba(255, 255, 255, 0.2);
    border-top-color: #fff;
    border-radius: 50%;
    animation: pwa-spin 0.8s linear infinite;
}

.pwa-splash-text {
    color: rgba(255, 255, 255, 0.8);
    font-size: 13px;
    margin-top: 14px;
    font-weight: 500;
}

@keyframes pwa-spin {
    to { transform: rotate(360deg); }
}

@keyframes pwa-pulse {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.03); opacity: 0.9; }
}

/* ============================================
   INSTALL BANNER
   ============================================ */
.pwa-install-banner {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #fff;
    padding: 14px 16px;
    padding-bottom: calc(14px + var(--pwa-safe-bottom));
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
    z-index: 10000;
    display: flex;
    align-items: center;
    gap: 10px;
    transform: translateY(100%);
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 16px 16px 0 0;
}

.pwa-install-banner.show {
    transform: translateY(0);
}

.pwa-install-banner-icon {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    flex-shrink: 0;
}

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

.pwa-install-banner-title {
    font-weight: 700;
    font-size: 14px;
    color: #212529;
    margin-bottom: 1px;
}

.pwa-install-banner-desc {
    font-size: 11px;
    color: #6c757d;
    line-height: 1.3;
}

.pwa-install-btn {
    background: var(--pwa-accent);
    color: #fff;
    border: none;
    border-radius: 18px;
    padding: 8px 18px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.2s;
    flex-shrink: 0;
}

.pwa-install-btn:hover {
    background: var(--pwa-primary-dark);
}

.pwa-install-close {
    background: none;
    border: none;
    color: #adb5bd;
    font-size: 20px;
    cursor: pointer;
    padding: 4px;
    line-height: 1;
    flex-shrink: 0;
}

/* ============================================
   PULL TO REFRESH
   ============================================ */
.pwa-pull-indicator {
    position: fixed;
    top: 0;
    left: 50%;
    transform: translateX(-50%) translateY(-60px);
    z-index: 9998;
    width: 36px;
    height: 36px;
    background: #fff;
    border-radius: 50%;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.pwa-pull-indicator.pulling {
    transform: translateX(-50%) translateY(20px);
}

.pwa-pull-indicator.refreshing {
    transform: translateX(-50%) translateY(20px);
}

.pwa-pull-indicator i {
    font-size: 16px;
    color: var(--pwa-accent);
    transition: transform 0.3s;
}

.pwa-pull-indicator.pulling i {
    transform: rotate(180deg);
}

.pwa-pull-indicator.refreshing i {
    animation: pwa-spin 0.8s linear infinite;
}

/* ============================================
   PAGE TRANSITIONS
   ============================================ */
.pwa-page-transition {
    animation: pwa-fadeSlideIn 0.3s ease-out;
}

@keyframes pwa-fadeSlideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.pwa-page-exit {
    animation: pwa-fadeSlideOut 0.2s ease-in forwards;
}

@keyframes pwa-fadeSlideOut {
    from {
        opacity: 1;
        transform: translateY(0);
    }
    to {
        opacity: 0;
        transform: translateY(-6px);
    }
}

/* Stagger animation for cards */
.pwa-page-transition .card {
    animation: pwa-cardSlideIn 0.35s ease-out both;
}
.pwa-page-transition .card:nth-child(1) { animation-delay: 0s; }
.pwa-page-transition .card:nth-child(2) { animation-delay: 0.04s; }
.pwa-page-transition .card:nth-child(3) { animation-delay: 0.08s; }
.pwa-page-transition .card:nth-child(4) { animation-delay: 0.12s; }

@keyframes pwa-cardSlideIn {
    from {
        opacity: 0;
        transform: translateY(16px) scale(0.98);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* ============================================
   BOTTOM NAVIGATION BAR (Mobile only)
   ============================================ */
@media (max-width: 768px) {
    .pwa-bottom-nav {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: calc(var(--pwa-bottom-nav-height) + var(--pwa-safe-bottom));
        background: #fff;
        display: flex;
        align-items: flex-start;
        justify-content: space-around;
        padding-top: 5px;
        padding-bottom: var(--pwa-safe-bottom);
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
        z-index: 1040;
        border-top: 1px solid #e9ecef;
    }

    .pwa-bottom-nav-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        text-decoration: none;
        color: #868e96;
        font-size: 10px;
        font-weight: 500;
        padding: 3px 6px;
        min-width: 52px;
        position: relative;
        transition: color 0.2s;
        -webkit-tap-highlight-color: transparent;
    }

    .pwa-bottom-nav-item i {
        font-size: 19px;
        margin-bottom: 2px;
        transition: transform 0.2s;
    }

    .pwa-bottom-nav-item.active {
        color: var(--pwa-accent);
    }

    .pwa-bottom-nav-item.active i {
        transform: scale(1.1);
    }

    .pwa-bottom-nav-item:active i {
        transform: scale(0.9);
    }

    /* Ripple effect on tap */
    .pwa-bottom-nav-item::after {
        content: '';
        position: absolute;
        top: 50%;
        left: 50%;
        width: 0;
        height: 0;
        background: rgba(26, 115, 232, 0.1);
        border-radius: 50%;
        transform: translate(-50%, -50%);
        transition: width 0.3s, height 0.3s;
    }

    .pwa-bottom-nav-item:active::after {
        width: 44px;
        height: 44px;
    }

    /* Badge for notifications */
    .pwa-bottom-nav-badge {
        position: absolute;
        top: -1px;
        right: 2px;
        background: #dc3545;
        color: #fff;
        font-size: 9px;
        font-weight: 700;
        min-width: 15px;
        height: 15px;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0 3px;
    }

    .pwa-bottom-nav-badge:empty {
        display: none;
    }

    /* Adjust content for bottom nav */
    .content-wrapper {
        padding-bottom: calc(var(--pwa-bottom-nav-height) + var(--pwa-safe-bottom) + 8px) !important;
    }

    /* Hide footer on mobile - bottom nav replaces it */
    .main-footer {
        display: none !important;
    }

    /* Hide navbar and sidebar on mobile PWA */
    body.pwa-mobile-nav .main-header.navbar {
        display: none !important;
    }

    body.pwa-mobile-nav .main-sidebar {
        display: none !important;
    }

    body.pwa-mobile-nav .content-wrapper {
        margin-left: 0 !important;
        margin-top: 0 !important;
        padding-top: 8px;
    }
}

/* Hide bottom nav on desktop */
@media (min-width: 769px) {
    .pwa-bottom-nav {
        display: none !important;
    }
}

/* ============================================
   OFFLINE INDICATOR
   ============================================ */
.pwa-offline-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background: #dc3545;
    color: #fff;
    text-align: center;
    padding: 5px 12px;
    padding-top: calc(5px + var(--pwa-safe-top));
    font-size: 12px;
    font-weight: 600;
    z-index: 10001;
    transform: translateY(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.pwa-offline-bar.show {
    transform: translateY(0);
}

.pwa-offline-bar i {
    margin-right: 6px;
}

/* ============================================
   UPDATE AVAILABLE TOAST
   ============================================ */
.pwa-update-toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: #212529;
    color: #fff;
    padding: 10px 16px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    gap: 10px;
    z-index: 10002;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    font-size: 13px;
    max-width: 90%;
    white-space: nowrap;
}

@media (max-width: 768px) {
    .pwa-update-toast {
        bottom: calc(var(--pwa-bottom-nav-height) + var(--pwa-safe-bottom) + 12px);
    }
}

.pwa-update-toast.show {
    transform: translateX(-50%) translateY(0);
}

.pwa-update-toast button {
    background: var(--pwa-accent);
    color: #fff;
    border: none;
    border-radius: 6px;
    padding: 5px 12px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
}

/* ============================================
   ENHANCED TOUCH INTERACTIONS (mobile)
   ============================================ */
@media (max-width: 768px) {
    /* Better tap targets */
    .btn {
        min-height: 42px;
    }

    /* Smooth scrolling */
    .content-wrapper,
    .table-responsive {
        -webkit-overflow-scrolling: touch;
        scroll-behavior: smooth;
    }

    /* Card press feedback */
    .card {
        transition: transform 0.15s ease;
    }

    .card:active {
        transform: scale(0.985);
    }

    /* Small box tap effect */
    .small-box {
        transition: transform 0.15s ease;
    }

    .small-box:active {
        transform: scale(0.97);
    }

    /* List item touch feedback */
    .list-group-item {
        transition: background-color 0.15s ease;
    }

    .list-group-item:active {
        background-color: #f0f0f0;
    }
}

/* ============================================
   STANDALONE MODE ADJUSTMENTS
   ============================================ */
@media (display-mode: standalone) {
    .main-header.navbar {
        padding-top: var(--pwa-safe-top);
    }

    .breadcrumb {
        display: none;
    }
}

/* ============================================
   iOS SPECIFIC PWA FIXES
   ============================================ */
@supports (-webkit-touch-callout: none) {
    body {
        overscroll-behavior-y: none;
    }

    .pwa-splash {
        height: -webkit-fill-available;
    }

    .pwa-bottom-nav-item {
        -webkit-user-select: none;
        user-select: none;
    }
}
