/**
 * PWA y estilos móviles - Heifer Agave
 * Banner de instalación y diseño responsivo para pantallas pequeñas
 */

/* Altura del header: en móvil se ajusta con JS al abrir filtros (filters-mobile.js) para que no sobreponga ni deje hueco */
@media (max-width: 768px) {
    :root {
        --filters-header-offset: 140px; /* fallback hasta que JS mida el .header real */
    }
}

/* ===== FILTROS COMO MENÚ LATERAL (móvil) ===== */
.filters-toggle-btn {
    display: none;
    position: fixed;
    bottom: 1rem;
    left: 1rem;
    z-index: 900;
    padding: 0.75rem 1.25rem;
    background: #2B4053;
    color: white;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.95rem;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(43, 64, 83, 0.4);
    align-items: center;
    gap: 0.5rem;
}

.filters-toggle-btn i {
    font-size: 1.1rem;
}

.filters-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    min-width: 100vw;
    min-height: 100vh;
    min-height: 100dvh;
    background: rgba(0, 0, 0, 0.55);
    z-index: 9998;
    -webkit-tap-highlight-color: transparent;
}

/* En móvil: overlay y panel solo en el área de contenido (debajo del header) */
@media (max-width: 768px) {
    .filters-overlay {
        top: var(--filters-header-offset, 120px);
        height: calc(100vh - var(--filters-header-offset, 120px));
        height: calc(100dvh - var(--filters-header-offset, 120px));
        min-height: calc(100vh - var(--filters-header-offset, 120px));
        min-height: calc(100dvh - var(--filters-header-offset, 120px));
    }
}

.sidebar-close-btn {
    width: 100%;
    margin-top: 1rem;
    padding: 0.75rem;
    background: #D84315;
    color: white;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

@media (max-width: 768px) {
    .filters-toggle-btn {
        display: flex;
        bottom: calc(1rem + env(safe-area-inset-bottom));
    }

    .main-container {
        flex-direction: column;
        padding: 0 1rem;
        margin: 1rem auto;
    }

    .sidebar {
        position: fixed;
        top: var(--filters-header-offset, 120px);
        left: 0;
        width: 300px;
        max-width: 85vw;
        height: calc(100vh - var(--filters-header-offset, 120px));
        height: calc(100dvh - var(--filters-header-offset, 120px));
        z-index: 9999;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        overflow-y: auto;
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.25);
        -webkit-overflow-scrolling: touch;
    }

    .sidebar.sidebar-open {
        transform: translateX(0);
    }

    .sidebar .sidebar-close-btn {
        display: flex !important;
    }

    .sidebar-title {
        font-size: 1.4rem;
    }

    .search-box {
        min-width: 0;
    }
}

@media (min-width: 769px) {
    .sidebar .sidebar-close-btn {
        display: none !important;
    }
}

/* ===== ESTILOS GENERALES MÓVIL (index, etc) ===== */
@media (max-width: 768px) {
    .products-section {
        padding-top: 0.5rem;
    }
}

/* ===== BANNER INSTALAR PWA ===== */
.pwa-install-banner {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(135deg, #2B4053 0%, #1a2d3d 100%);
    color: white;
    padding: 1rem 1.25rem;
    padding-bottom: calc(1rem + env(safe-area-inset-bottom));
    box-shadow: 0 -4px 20px rgba(0,0,0,0.2);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    flex-wrap: wrap;
}

.pwa-install-content {
    display: flex;
    align-items: center;
    gap: 1rem;
    flex: 1;
    min-width: 0;
}

.pwa-install-icon {
    width: 48px;
    height: 48px;
    object-fit: contain;
    flex-shrink: 0;
}

.pwa-install-content strong {
    display: block;
    font-size: 1rem;
    margin-bottom: 0.25rem;
}

.pwa-install-content p {
    margin: 0;
    font-size: 0.85rem;
    opacity: 0.9;
}

.pwa-install-actions {
    display: flex;
    gap: 0.5rem;
    flex-shrink: 0;
}

.pwa-install-btn {
    background: #73C3D5;
    color: white;
    border: none;
    padding: 0.6rem 1.25rem;
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.95rem;
    cursor: pointer;
}

.pwa-dismiss-btn {
    background: transparent;
    color: rgba(255,255,255,0.8);
    border: 1px solid rgba(255,255,255,0.4);
    padding: 0.6rem 1rem;
    border-radius: 8px;
    font-size: 0.9rem;
    cursor: pointer;
}

/* ===== RESPONSIVE MÓVIL - PÁGINA PRODUCTOR ===== */

/* Header móvil - más espacio, iconos y búsqueda más anchos para que no se vean pegados */
@media (max-width: 768px) {
    .header {
        padding: 1rem 0 0.65rem;
        padding-top: max(1rem, calc(env(safe-area-inset-top) + 0.35rem));
        padding-right: max(0.5rem, env(safe-area-inset-right));
        width: 100%;
        box-sizing: border-box;
        overflow: hidden;
        background-color: #fff;
    }

    .header-container {
        padding: 0 0.75rem 0 1rem;
        padding-right: max(0.75rem, env(safe-area-inset-right));
        gap: 0.6rem;
        flex-wrap: wrap;
        overflow-x: hidden;
        min-width: 0;
        max-width: 100%;
        width: 100%;
        box-sizing: border-box;
        align-items: center;
    }

    /* Primera fila: logo a la izquierda, búsqueda e iconos a la derecha, en una sola fila */
    .logo-container {
        flex-shrink: 0;
        order: 1;
    }

    .logo-img {
        max-width: 100px;
        height: auto;
    }

    .nav-menu {
        order: 3;
        width: 100%;
        flex-basis: 100%;
        justify-content: center;
        gap: 0.5rem;
        margin-top: 0.6rem;
        padding-top: 0.6rem;
        border-top: 1px solid #eee;
    }

    .nav-link {
        font-size: 0.85rem;
        padding: 0.4rem 0.5rem;
    }

    .header-actions {
        order: 2;
        gap: 0.5rem;
        min-width: 0;
        flex: 1 1 auto;
        justify-content: flex-end;
        max-width: calc(100% - 10px);
        align-items: center;
        min-height: 44px;
    }

    /* Página inicio (index): solo búsqueda + Acceder → barra más ancha para "Buscar productos..." */
    .page-index .header-actions .search-box {
        flex: 1 1 140px;
        max-width: 220px;
        padding: 0.5rem 0.85rem;
    }

    .page-index .header-actions .search-input {
        font-size: 0.95rem;
    }

    .page-index .header-actions .login-btn {
        padding: 0.6rem 1.2rem;
        font-size: 0.95rem;
        flex-shrink: 0;
    }

    .header-actions .search-box {
        min-width: 0;
        flex: 1 1 100px;
        max-width: 160px;
        padding: 0.5rem 0.75rem;
    }

    .header-actions .search-input {
        font-size: 0.9rem;
    }

    .header-actions .search-input::placeholder {
        font-size: 0.85rem;
    }

    .header-icon-btn {
        width: 44px;
        height: 44px;
        font-size: 1.2rem;
        padding: 0.5rem;
        flex-shrink: 0;
    }

    /* Botón cerrar sesión mismo tamaño que iconos */
    .header-actions .logout-btn {
        width: 44px;
        height: 44px;
        padding: 0;
        min-width: 44px;
        flex-shrink: 0;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .header-actions .logout-btn span {
        display: none;
    }

    .header-actions .logout-btn i {
        margin: 0;
        font-size: 1.2rem;
    }

    /* Panel de notificaciones: fijo y centrado en móvil para que se vea completo */
    .notifications-dropdown {
        position: static;
    }

    .notifications-panel {
        position: fixed;
        top: max(70px, calc(env(safe-area-inset-top) + 55px));
        left: max(0.75rem, env(safe-area-inset-left));
        right: max(0.75rem, env(safe-area-inset-right));
        width: auto;
        max-width: 400px;
        margin: 0 auto;
        max-height: min(70vh, 70dvh);
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
        z-index: 10001;
    }

    .notifications-header {
        padding: 1rem 1rem 1rem 1.25rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .notifications-header h3 {
        font-size: 1.1rem;
        word-break: break-word;
    }

    .mark-all-read-btn {
        font-size: 0.8rem;
        white-space: nowrap;
    }

    .notifications-list {
        max-height: 50vh;
        max-height: 50dvh;
    }
}

/* ===== AJUSTES ESPECÍFICOS PARA INICIO CLIENTE (PWA) ===== */
@media (max-width: 768px) {
    .page-cliente .header-container {
        flex-direction: column;
        align-items: stretch;
        gap: 0.4rem;
    }

    .page-cliente .logo-container {
        order: 1;
        justify-content: center;
        width: 100%;
    }

    .page-cliente .logo-img {
        max-width: 140px;
    }

    .page-cliente .header-actions {
        order: 2;
        width: 100%;
        flex-wrap: wrap;
        row-gap: 0.45rem;
        justify-content: flex-end;
    }

    /* Barra de búsqueda en su propia fila y a todo el ancho */
    .page-cliente .header-actions .search-box {
        order: 1;
        flex: 1 1 100%;
        max-width: 100%;
        min-width: 0;
    }

    /* Iconos y botón salir en una segunda fila, alineados a la derecha */
    .page-cliente .header-actions .header-icon-btn,
    .page-cliente .header-actions .logout-btn {
        order: 2;
    }

    /* Menú de pestañas debajo del bloque de acciones ocupando todo el ancho */
    .page-cliente .nav-menu {
        order: 3;
        width: 100%;
        justify-content: center;
    }
}

@media (max-width: 480px) {
    .page-cliente .header-container {
        gap: 0.3rem;
    }

    .page-cliente .header-actions {
        row-gap: 0.35rem;
    }

    .page-cliente .header-actions .search-box {
        padding-inline: 0.6rem;
    }
}

@media (max-width: 768px) {
    .producer-sidebar {
        width: 100%;
        padding: 1rem 1.25rem;
        position: relative;
        top: 0;
        height: auto;
        max-height: none;
    }

    .profile-section {
        margin-bottom: 1rem;
        padding-bottom: 1rem;
    }

    .profile-image {
        width: 70px;
        height: 70px;
    }

    .profile-name {
        font-size: 1rem;
    }

    .sidebar-menu-item {
        padding: 0.85rem 1rem;
        font-size: 0.95rem;
    }

    .producer-main-content {
        padding: 1rem 1.25rem;
        padding-bottom: calc(1rem + env(safe-area-inset-bottom) + 80px);
    }

    .section-title {
        font-size: 1.4rem;
        margin-bottom: 1rem;
    }

    /* Stats cards - 1 columna en móvil */
    .stats-cards-grid {
        grid-template-columns: 1fr !important;
        gap: 1rem;
        margin-bottom: 1.5rem;
    }

    .stat-card-modern {
        padding: 1.25rem;
    }

    .stat-number-modern {
        font-size: 1.75rem;
    }

    /* Dual section - apilar */
    .dual-section {
        grid-template-columns: 1fr;
        gap: 1rem;
        margin-bottom: 1.5rem;
    }

    .productos-recientes,
    .mensajes-recientes {
        padding: 1rem 1.25rem;
    }

    .producto-reciente-item {
        padding: 0.85rem;
    }

    .producto-img {
        width: 50px;
        height: 50px;
    }

    .product-type-filters {
        flex-direction: column;
        gap: 0.75rem;
    }

    .filter-type-btn {
        width: 100%;
        justify-content: center;
        padding: 0.85rem;
    }

    .products-grid {
        grid-template-columns: 1fr;
        gap: 1rem;
    }

    .product-card {
        max-width: 100%;
    }

    .product-actions {
        flex-direction: column;
    }

    .action-btn {
        width: 100%;
        padding: 0.6rem;
    }

    /* Mensajes - layout móvil */
    .messages-container-full {
        flex-direction: column;
        height: auto;
        min-height: 400px;
        padding: 1rem;
    }

    .messages-sidebar-full {
        width: 100%;
        border-right: none;
        border-bottom: 1px solid #e0e0e0;
        max-height: 200px;
    }

    .messages-content-full {
        flex: 1;
        min-height: 300px;
    }

    #chatMessagesFull .message-bubble {
        max-width: 85%;
    }

    .chat-input {
        padding: 0.5rem;
    }

    #messageInputFull {
        font-size: 16px; /* Evita zoom en iOS */
    }

    /* Modal */
    .modal-content {
        width: 95%;
        max-height: 85vh;
        margin: 1rem;
    }

    /* Modal de Mensajes - layout apilado en móvil para que salga completo */
    .modal-messages {
        width: 100%;
        max-width: 100%;
        height: 100vh;
        height: 100dvh;
        max-height: 100vh;
        max-height: 100dvh;
        margin: 0;
        border-radius: 0;
        box-sizing: border-box;
    }

    .modal-messages-header {
        padding-top: max(1.5rem, env(safe-area-inset-top));
    }

    .modal-messages-body {
        flex-direction: column;
        min-height: 0;
    }

    .messages-sidebar {
        width: 100%;
        max-width: 100%;
        max-height: 35vh;
        max-height: 35dvh;
        min-height: 120px;
        flex-shrink: 0;
        border-right: none;
        border-bottom: 1px solid #e0e0e0;
    }

    .messages-content {
        flex: 1;
        min-height: 0;
        overflow: hidden;
    }

    .messages-chat {
        display: flex;
        flex-direction: column;
        min-height: 0;
    }

    .chat-messages {
        flex: 1;
        min-height: 0;
        overflow-y: auto;
    }

    .modal-messages .chat-input {
        padding-bottom: max(1rem, env(safe-area-inset-bottom));
    }

    .modal-header,
    .profile-form {
        padding: 1rem 1.25rem;
    }

    /* Perfil */
    .profile-banner {
        padding: 1.5rem;
    }

    .profile-banner-content {
        flex-direction: column;
        text-align: center;
    }

    .profile-avatar-large {
        width: 90px;
        height: 90px;
        font-size: 2rem;
    }

    .profile-banner-name {
        font-size: 1.4rem;
    }

    .profile-info-grid {
        grid-template-columns: 1fr;
    }
}

/* Pantallas muy pequeñas - header más compacto */
@media (max-width: 480px) {
    .header-container {
        padding: 0 0.5rem 0 0.75rem;
        padding-right: max(0.5rem, env(safe-area-inset-right));
        gap: 0.4rem;
    }

    .logo-img {
        max-width: 100px;
    }

    .header-actions .search-box {
        max-width: 100px;
    }

    .page-index .header-actions .search-box {
        max-width: 160px;
    }

    .header-icon-btn {
        width: 40px;
        height: 40px;
        font-size: 1.1rem;
    }

    .header-actions .logout-btn {
        width: 40px;
        height: 40px;
        min-width: 40px;
    }

    .header-actions .logout-btn i {
        font-size: 1.1rem;
    }

    .pwa-install-banner {
        flex-direction: column;
        align-items: stretch;
        text-align: center;
    }

    .pwa-install-content {
        justify-content: center;
    }

    .pwa-install-actions {
        justify-content: center;
    }

    .stat-card-modern {
        padding: 1rem;
    }

    .add-product-btn,
    .stat-add-product {
        padding: 1rem;
        font-size: 0.95rem;
    }
}

/* ===== PÁGINA DETALLE PRODUCTO (cliente) ===== */
@media (max-width: 768px) {
    .product-detail-container {
        padding: 1rem 1.25rem;
    }

    .product-detail-content {
        grid-template-columns: 1fr;
        gap: 1.5rem;
    }

    .product-images-panel {
        order: 1;
    }

    .product-details-panel {
        order: 2;
    }

    .main-image-container {
        max-height: 300px;
    }

    .product-title {
        font-size: 1.4rem;
    }

    .detail-item-buenas-practicas .buenas-practicas-list {
        font-size: 0.9rem;
    }
}

/* ===== PÁGINA INICIO CLIENTE ===== */
@media (max-width: 768px) {
    .products-grid .product-card {
        min-width: 0;
    }
}

/* ===== FORMULARIOS (subir producto, login, registro) ===== */
@media (max-width: 768px) {
    .login-wrapper {
        max-width: 100%;
        padding: 0 1rem;
    }

    .login-box {
        padding: 1.5rem 1.25rem;
    }

    .buenas-practicas-panel {
        max-height: 220px;
    }
}

/* Safe area para iPhone con notch */
@supports (padding: env(safe-area-inset-top)) {
    body {
        padding-top: env(safe-area-inset-top);
    }

    .header {
        padding-top: calc(0.5rem + env(safe-area-inset-top));
    }

    @media (max-width: 768px) {
        .producer-main-content {
            padding-bottom: calc(1rem + env(safe-area-inset-bottom) + 100px);
        }
    }
}
