/* ═══════════════════════════════════════════════════════════════════
   InspiraGest — Responsive móvil FLUIDO.
   Enfoque: en lugar de saltar entre 2 breakpoints, todo escala entre
   un mínimo y un máximo según el ancho real de pantalla usando clamp().
   Esto hace que en un iPhone SE (320px), un iPhone 15 (393px), un
   Pixel Pro (411px) o un Galaxy Fold (280px/717px) se vea siempre
   proporcionado — sin descuadres.
   ═══════════════════════════════════════════════════════════════════ */


/* ═══ 0) Reset base SOLO para móvil (≤900px) — evita scroll horizontal
   y respeta notch. NO se aplica en desktop para no romper layouts
   existentes que dependen del cálculo content-box. ═══ */
@media (max-width: 900px) {
    html, body {
        max-width: 100vw;
        overflow-x: hidden;
    }
    /* box-sizing solo en contenedores del layout móvil, no globalmente */
    .content, main, .card, .container, .contenedor, .contenedor-principal,
    .main-container, .sidebar, header, .dashboard-icons, .tabla-wrap,
    .form-container, .tabs {
        box-sizing: border-box;
    }
    img, video, canvas, svg {
        max-width: 100%;
        height: auto;
    }
    body {
        overflow-wrap: break-word;
        word-wrap: break-word;
    }
}


/* ═══ Elementos que SOLO existen en móvil — ocultos por defecto ═══
   El JS los inyecta siempre en el DOM, pero en desktop no deben verse. */
.mobile-menu-btn,
.mobile-sidebar-btn,
.mobile-drawer-backdrop {
    display: none !important;
}


/* ═══════════════════════════════════════════════════════════════════
   1) FLUIDO — se aplica en TODAS las pantallas ≤900px.
      Todo va con clamp(min, preferido, max) para escalar sin saltos.
   ═══════════════════════════════════════════════════════════════════ */
@media (max-width: 900px) {

    /* Inputs 16px → iOS no hace zoom al enfocar */
    input:not([type=range]), select, textarea {
        font-size: 16px !important;
    }

    /* Padding lateral fluido que respeta safe-area (notch/home bar) */
    body {
        padding-left:  env(safe-area-inset-left, 0px);
        padding-right: env(safe-area-inset-right, 0px);
    }

    /* ── Header ── layout apilado limpio en 2 filas ──
       Fila 1: [☰] Logo + Título ................. [Cerrar sesión]
       Fila 2: ⚙ 🔔 👤 💬 ......................... [welcome truncado]
    */
    header {
        display: flex !important;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        padding: clamp(6px, 2vw, 12px) clamp(8px, 3vw, 16px);
        padding-top: calc(env(safe-area-inset-top, 0px) + clamp(6px, 2vw, 12px));
        row-gap: clamp(6px, 2vw, 10px);
        column-gap: clamp(4px, 1.5vw, 10px);
    }

    /* Bloque logo + título (queda a la izquierda) */
    .header-content {
        display: flex !important;
        flex-direction: column !important;
        align-items: flex-start !important;
        flex: 1 1 auto !important;
        min-width: 0 !important;   /* permite truncar */
        order: 2;
        margin: 0 !important;
    }
    .app-logo {
        display: flex !important;
        align-items: center !important;
        gap: clamp(4px, 1.5vw, 8px);
        min-width: 0;
        max-width: 100%;
    }
    .app-logo img {
        width: clamp(24px, 7vw, 34px) !important;
        height: auto !important;
        flex-shrink: 0;
    }
    header h1 {
        font-size: clamp(13px, 4vw, 17px) !important;
        margin: 0 !important;
        line-height: 1.2 !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        min-width: 0;
    }
    .welcome {
        font-size: clamp(10px, 2.8vw, 12px) !important;
        line-height: 1.25 !important;
        margin: 2px 0 0 !important;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        opacity: .85;
    }

    /* Logo de empresa: ocupa mucho y estorba en móvil → ocultar */
    .company-logo {
        display: none !important;
    }

    /* Botón hamburguesa a la izquierda del todo */
    .mobile-menu-btn {
        order: 1;
    }

    /* Grupo de iconos ⚙ 🔔 👤 💬 en su propia fila */
    .header-top-icons {
        display: flex !important;
        align-items: center !important;
        gap: clamp(6px, 2vw, 10px) !important;
        flex: 0 0 auto !important;
        order: 4;
        margin: 0 !important;
        padding: 0 !important;
        flex-wrap: nowrap;
    }
    .header-top-icons > * {
        margin: 0 !important;
    }
    .header-top-icons a,
    .header-top-icons .campana-notificaciones,
    .header-top-icons .icono-usuario {
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
    }
    .header-top-icons img {
        width: clamp(22px, 6.5vw, 30px) !important;
        height: auto !important;
    }
    .header-btn-mensajes {
        width: clamp(30px, 8vw, 36px) !important;
        height: clamp(30px, 8vw, 36px) !important;
    }
    .header-btn-mensajes svg {
        width: clamp(16px, 4.5vw, 20px) !important;
        height: clamp(16px, 4.5vw, 20px) !important;
    }
    /* El badge de notificaciones más pequeño para que no tape */
    .campana-notificaciones .circulo-notificaciones {
        font-size: 9px !important;
        min-width: 14px;
        height: 14px;
        padding: 0 3px !important;
        line-height: 14px !important;
    }

    /* Botón Cerrar sesión: derecha del todo, tamaño compacto */
    .logout-btn {
        order: 3;
        padding: clamp(6px, 2vw, 10px) clamp(8px, 3vw, 14px) !important;
        font-size: clamp(11px, 3.2vw, 13px) !important;
        margin: 0 !important;
        flex: 0 0 auto !important;
        position: static !important;   /* pisa el position:absolute del desktop */
    }

    /* ── Menú principal → drawer ── */
    .main-menu {
        display: none !important;
        order: 99;
        width: 100%;
        flex-direction: column;
        background: #3a5461;
        margin: 8px 0 -8px;
        padding: 4px 0 6px;
    }
    .main-menu.mobile-open {
        display: flex !important;
    }
    .main-menu a {
        margin: 0 !important;
        padding: clamp(10px, 3vw, 14px) clamp(12px, 4vw, 18px) !important;
        border-top: 1px solid rgba(255,255,255,.08);
        font-size: clamp(13px, 3.8vw, 15px) !important;
        color: #fff !important;
    }
    .main-menu a.active {
        background: rgba(255,255,255,.08);
        border-left: 3px solid #f36f21;
    }

    /* Botón hamburguesa (inyectado por responsive.js) */
    .mobile-menu-btn {
        display: inline-block !important;
        background: transparent;
        border: 1px solid rgba(255,255,255,.35);
        color: #fff;
        font-size: clamp(18px, 5.5vw, 22px);
        padding: clamp(4px, 1.5vw, 8px) clamp(8px, 3vw, 14px);
        cursor: pointer;
        line-height: 1;
        border-radius: 6px;
        order: 2;
    }

    /* ── Layout principal en columna ── */
    .main-container {
        flex-direction: column !important;
        width: 100% !important;
        max-width: 100% !important;
    }

    /* Sidebar drawer */
    .sidebar {
        width: 100% !important;
        max-width: 100% !important;
        padding: clamp(6px, 2vw, 12px) clamp(8px, 3vw, 14px) !important;
        display: none;
        box-shadow: none !important;
    }
    .sidebar.mobile-open { display: block; }
    .sidebar .submenu {
        display: flex !important;
        flex-wrap: wrap;
        gap: clamp(4px, 1.5vw, 8px);
    }
    .sidebar .submenu a,
    .sidebar .submenu .menu-btn {
        margin: 0 !important;
        flex: 1 1 clamp(120px, 45%, 200px);
        padding: clamp(8px, 2.5vw, 12px) !important;
        text-align: center;
        font-size: clamp(11.5px, 3.3vw, 13.5px) !important;
    }

    /* Botón abrir sidebar */
    .mobile-sidebar-btn {
        display: flex !important;
        align-items: center;
        gap: 8px;
        margin: clamp(6px, 2vw, 12px) clamp(8px, 3vw, 14px);
        padding: clamp(8px, 2.5vw, 12px) clamp(10px, 3.5vw, 16px);
        background: #fff;
        border: 1px solid #e5e7eb;
        border-radius: 8px;
        cursor: pointer;
        font-size: clamp(12px, 3.5vw, 14px);
        color: #4a5d6e;
        font-weight: 700;
        width: calc(100% - clamp(16px, 6vw, 28px));
    }

    /* Contenido general */
    .content,
    main {
        padding: clamp(8px, 3vw, 16px) !important;
        max-width: 100% !important;
    }

    /* Cards */
    .card {
        padding: clamp(10px, 3.5vw, 18px) !important;
        margin: clamp(6px, 2vw, 12px) auto !important;
        border-radius: 8px !important;
        max-width: 100% !important;
        width: 100% !important;
    }

    /* Grid dashboard: se auto-ajusta al ancho — sin importar el móvil */
    .dashboard-icons {
        display: grid !important;
        grid-template-columns: repeat(auto-fit, minmax(clamp(120px, 40vw, 180px), 1fr)) !important;
        gap: clamp(8px, 2.5vw, 16px) !important;
        padding: clamp(8px, 3vw, 16px) !important;
    }
    .dashboard-icons .icon-item,
    .dashboard-icons a {
        min-height: clamp(110px, 32vw, 180px);
        padding: clamp(10px, 3vw, 20px) !important;
    }
    .dashboard-icons img {
        width: clamp(48px, 14vw, 90px) !important;
        height: auto !important;
    }
    .dashboard-icons span {
        font-size: clamp(11px, 3.4vw, 15px) !important;
    }

    /* Formularios columna única */
    form.form-container,
    .form-grid {
        grid-template-columns: 1fr !important;
        gap: clamp(6px, 2vw, 12px) !important;
    }
    #change-password-form input[type="password"],
    .form-container input,
    .form-container select,
    .form-container textarea {
        width: 100% !important;
        max-width: 100% !important;
    }

    /* Botones tocables (mínimo Apple/Google: 44x44) */
    button, .btn-editar, .tab-btn, .btn-guardar-cfg, .btn-secundario, .btn {
        min-height: 42px;
        font-size: clamp(12px, 3.5vw, 14.5px);
    }

    /* Pestañas con scroll horizontal */
    .tabs {
        overflow-x: auto;
        flex-wrap: nowrap !important;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: thin;
        max-width: 100%;
    }
    .tabs > * {
        flex: 0 0 auto;
        font-size: clamp(11.5px, 3.3vw, 13.5px);
    }

    /* Modales ocupan casi todo el móvil */
    .dt-modal,
    .lt-confirm-box,
    [id$="-modal"] > div,
    [id$="-modal-box"] {
        max-width: min(96vw, 700px) !important;
        max-height: 92vh !important;
        width: 96vw !important;
        margin: 0 !important;
    }

    /* Contenedores que se salían del ancho */
    .container,
    .contenedor,
    .contenedor-principal {
        max-width: 100% !important;
        width: 100% !important;
        padding-left: clamp(8px, 3vw, 16px) !important;
        padding-right: clamp(8px, 3vw, 16px) !important;
    }

    /* Tablas grandes que aún no son "cards" → scroll horizontal en su caja */
    .tabla-wrap,
    .table-responsive {
        overflow-x: auto;
        max-width: 100%;
        -webkit-overflow-scrolling: touch;
    }
}


/* ═══════════════════════════════════════════════════════════════════
   2) < 640px — refuerzos para móvil pequeño: tablas → tarjetas
   ═══════════════════════════════════════════════════════════════════ */
@media (max-width: 640px) {

    /* Tablas de listado se convierten en tarjetas apiladas */
    .tabla-control,
    .tabla {
        border: 0 !important;
        width: 100% !important;
        display: block;
    }
    .tabla-control thead,
    .tabla thead {
        display: none !important;
    }
    .tabla-control tbody,
    .tabla tbody {
        display: block;
    }
    .tabla-control tbody tr,
    .tabla tbody tr {
        display: block;
        margin: 0 0 clamp(8px, 2.5vw, 14px) !important;
        padding: clamp(10px, 3vw, 14px) !important;
        border: 1px solid #e5e7eb !important;
        border-radius: 8px !important;
        background: #fff !important;
        box-shadow: 0 1px 3px rgba(0,0,0,.04);
    }
    .tabla-control tbody td,
    .tabla tbody td {
        display: block !important;
        padding: clamp(3px, 1vw, 5px) 0 !important;
        border: 0 !important;
        font-size: clamp(12px, 3.6vw, 14px) !important;
        text-align: left !important;
        white-space: normal !important;
    }
    .tabla-control tbody td:last-child,
    .tabla tbody td:last-child {
        margin-top: clamp(6px, 2vw, 12px) !important;
        padding-top: clamp(6px, 2vw, 12px) !important;
        border-top: 1px solid #f3f4f6 !important;
        display: flex !important;
        gap: 6px;
        flex-wrap: wrap;
    }
    .tabla-control tbody td:last-child > * {
        min-height: 36px;
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
    }

    /* Titulares fluidos */
    h1 { font-size: clamp(15px, 4.8vw, 20px) !important; }
    h2 { font-size: clamp(14px, 4.4vw, 18px) !important; }
    h3 { font-size: clamp(13px, 4vw, 16px) !important; }
    .titulo-centro { font-size: clamp(14px, 4.4vw, 18px) !important; }

    /* Banner PWA */
    #pwa-install-banner {
        padding: clamp(8px, 2.5vw, 14px) !important;
        padding-bottom: calc(env(safe-area-inset-bottom, 0px) + clamp(8px, 2.5vw, 14px)) !important;
    }
}


/* ═══════════════════════════════════════════════════════════════════
   3) Móvil muy pequeño (≤380px: iPhone SE, plegables cerrados) —
      apretar aún más para que quepa todo.
   ═══════════════════════════════════════════════════════════════════ */
@media (max-width: 380px) {
    header {
        padding-left: 6px !important;
        padding-right: 6px !important;
    }
    .header-top-icons {
        gap: 5px !important;
    }
    .header-top-icons img {
        width: 20px !important;
    }
    .header-btn-mensajes {
        width: 28px !important;
        height: 28px !important;
    }
    .welcome {
        display: none !important;   /* no cabe — quitar saludo largo */
    }
    header h1 {
        font-size: 12px !important;
    }
    .app-logo img {
        width: 22px !important;
    }
    .logout-btn {
        padding: 5px 8px !important;
        font-size: 10.5px !important;
    }
    .dashboard-icons {
        grid-template-columns: 1fr 1fr !important;
        gap: 6px !important;
    }
}


/* ═══════════════════════════════════════════════════════════════════
   4) Backdrop del sidebar drawer
   ═══════════════════════════════════════════════════════════════════ */
.mobile-drawer-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.4);
    z-index: 998;
    display: none;
}
.mobile-drawer-backdrop.open { display: block; }


/* ═══════════════════════════════════════════════════════════════════
   5) Modo PWA instalado (standalone) — sin barra del navegador,
      añadir padding-top para respetar el status bar del móvil.
   ═══════════════════════════════════════════════════════════════════ */
@media (display-mode: standalone) {
    header {
        padding-top: calc(env(safe-area-inset-top, 0px) + 8px) !important;
    }
    body {
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }
}
