/**
 * Responsive CSS - Innovalabs 4D
 * Mobile First — cargado al final para máxima prioridad
 * Breakpoints:
 *   xs: 0–479px     (celular pequeño)
 *   sm: 480–767px   (celular grande)
 *   md: 768–1023px  (tablet)
 *   lg: 1024px+     (desktop)
 */

/* ============================================================
   1. VIEWPORT Y BASE
   ============================================================ */
html {
    -webkit-text-size-adjust: 100%;
    touch-action: manipulation;
}

/* Evita scroll horizontal global */
body {
    overflow-x: hidden;
}

/* Imágenes responsivas por defecto */
img {
    max-width: 100%;
    height: auto;
}

/* ============================================================
   2. LAYOUT PRINCIPAL (dashboard-wrapper)
   ============================================================ */

/* Mobile: sidebar oculto, contenido ocupa todo */
@media (max-width: 768px) {
    .dashboard-wrapper {
        display: block;
    }

    .page-content {
        padding: var(--spacing-sm) !important;
    }

    .main-content {
        width: 100%;
        min-width: 0;
    }
}

/* Tablet */
@media (min-width: 769px) and (max-width: 1023px) {
    .page-content {
        padding: var(--spacing-md) !important;
    }

    .sidebar {
        width: 220px;
    }
}

/* ============================================================
   3. TOP HEADER
   ============================================================ */

@media (max-width: 768px) {
    .top-header {
        height: auto;
        min-height: 56px;
        padding: 0.5rem var(--spacing-sm);
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .page-title {
        font-size: 1rem !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 160px;
    }

    /* Ocultar texto en botones del header, solo íconos */
    .btn-header-action .btn-text {
        display: none !important;
    }

    .btn-header-action {
        padding: 0.5rem !important;
        min-width: 36px;
        min-height: 36px;
    }

    .btn-header-action .btn-icon {
        font-size: 1.1rem;
    }

    /* Ocultar info usuario en header */
    .user-info-horizontal {
        display: none !important;
    }

    .header-separator {
        display: none !important;
    }

    .header-actions {
        gap: 0.35rem;
    }

    .theme-toggle {
        width: 36px !important;
        height: 36px !important;
        font-size: 1rem !important;
    }

    /* Notificaciones */
    .notifications-btn {
        font-size: 1.1rem;
        padding: 0.4rem;
    }
}

@media (min-width: 769px) and (max-width: 1023px) {
    .top-header {
        padding: 0 var(--spacing-md);
    }

    .page-title {
        font-size: 1.2rem;
    }

    .user-info-horizontal {
        gap: 0.75rem;
        padding: 0;
    }

    .info-label {
        display: none;
    }
}

/* ============================================================
   4. SIDEBAR MÓVIL
   ============================================================ */

/* Fix crítico: sidebar siempre fixed en móvil independiente del wrapper */
@media (max-width: 768px) {
    .sidebar {
        position: fixed !important;
        left: 0 !important;
        top: 0 !important;
        height: 100vh !important;
        z-index: 1000 !important;
        transform: translateX(-100%) !important;
        transition: transform 0.3s ease !important;
        width: 260px !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
    }

    .sidebar.active {
        transform: translateX(0) !important;
        box-shadow: 4px 0 20px rgba(0,0,0,0.3) !important;
    }

    .btn-menu-toggle {
        display: block !important;
    }
}

@media (max-width: 768px) {
    .sidebar {
        position: fixed !important;
        left: 0;
        top: 0;
        height: 100vh;
        z-index: 1000;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        width: 260px !important;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .sidebar.active {
        transform: translateX(0) !important;
        box-shadow: 4px 0 20px rgba(0,0,0,0.3);
    }

    .btn-menu-toggle {
        display: block !important;
        padding: 0.4rem 0.6rem;
        font-size: 1.3rem;
    }

    /* Links del sidebar más grandes para touch */
    .nav-link {
        padding: 0.85rem var(--spacing-lg);
        min-height: 48px;
        display: flex;
        align-items: center;
    }

    .nav-icon {
        font-size: 1.3rem;
        min-width: 28px;
    }
}

/* ============================================================
   5. TABLAS
   ============================================================ */

/* Todas las tablas con scroll horizontal en móvil */
@media (max-width: 768px) {
    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        border-radius: var(--radius-sm);
        border: 1px solid var(--border-color);
    }

    /* Reducir padding de celdas */
    .table th,
    .table td {
        padding: 0.5rem 0.6rem !important;
        font-size: 0.82rem;
        white-space: nowrap;
    }

    /* Celdas de acciones no tan anchas */
    .table .actions {
        gap: 0.25rem;
        flex-wrap: nowrap;
    }

    .btn-icon {
        font-size: 1.1rem;
        padding: 2px;
    }
}

@media (min-width: 769px) and (max-width: 1023px) {
    .table th,
    .table td {
        padding: 0.6rem 0.75rem !important;
        font-size: 0.9rem;
    }
}

/* ============================================================
   6. SISTEMA DE TABS
   ============================================================ */

@media (max-width: 768px) {
    .tabs-navigation {
        display: flex;
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        gap: 0.3rem;
        padding-bottom: 0;
        scrollbar-width: none; /* Firefox */
        -ms-overflow-style: none;
        margin-bottom: 1rem;
    }

    .tabs-navigation::-webkit-scrollbar {
        display: none; /* Chrome/Safari */
    }

    .tab-button {
        white-space: nowrap;
        font-size: 0.78rem !important;
        padding: 0.5rem 0.65rem !important;
        flex-shrink: 0;
        min-height: 44px; /* touch target */
    }

    /* Badge dentro del tab */
    .tab-button .badge {
        font-size: 10px;
        padding: 1px 5px;
        margin-left: 3px;
    }
}

@media (min-width: 769px) and (max-width: 1023px) {
    .tab-button {
        font-size: 0.85rem;
        padding: 0.65rem 0.9rem;
    }
}

/* ============================================================
   7. CARDS Y FORMULARIOS
   ============================================================ */

@media (max-width: 768px) {
    .card {
        padding: var(--spacing-sm) !important;
        border-radius: var(--radius-sm);
    }

    /* Formularios: inputs más grandes para touch */
    .form-control,
    select.form-control,
    textarea.form-control {
        font-size: 16px !important; /* evita zoom en iOS */
        min-height: 44px;
        padding: 0.6rem 0.75rem;
    }

    .form-label {
        font-size: 0.9rem;
    }

    .form-group {
        margin-bottom: var(--spacing-sm);
    }

    /* Botones más grandes para touch */
    .btn {
        min-height: 44px;
        padding: 0.6rem var(--spacing-sm);
        font-size: 0.9rem;
    }

    .btn-sm {
        min-height: 36px;
        font-size: 0.8rem;
        padding: 0.4rem 0.6rem;
    }
}

/* ============================================================
   8. GRIDS Y LAYOUTS INTERNOS
   ============================================================ */

@media (max-width: 768px) {
    /* Grid de permisos */
    .permisos-grid {
        grid-template-columns: 1fr !important;
        gap: 1rem;
    }

    /* Acciones en columna en móvil */
    .actions {
        flex-wrap: wrap;
        gap: 0.3rem;
    }

    /* Page headers con título + acciones */
    .page-header {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 0.75rem;
    }

    .page-header .header-actions,
    .page-header > a,
    .page-header > button {
        width: 100%;
        justify-content: center;
    }
}

@media (min-width: 769px) and (max-width: 1023px) {
    .permisos-grid {
        grid-template-columns: 1fr !important;
    }
}

/* ============================================================
   9. BADGES Y ESTADOS
   ============================================================ */

@media (max-width: 768px) {
    .badge {
        font-size: 10px;
        padding: 3px 6px;
    }

    .badge-secondary,
    .badge-warning,
    .badge-success,
    .badge-danger,
    .badge-info {
        font-size: 10px;
        padding: 3px 6px;
    }
}

/* ============================================================
   10. ALERTAS
   ============================================================ */

@media (max-width: 768px) {
    .alert {
        padding: 0.75rem;
        font-size: 0.875rem;
    }
}

/* ============================================================
   11. MODALES Y OVERLAYS
   ============================================================ */

/* El modal del STL viewer necesita ser full-screen en móvil */
@media (max-width: 768px) {
    /* Modales genéricos */
    [id$="Modal"],
    [id$="-modal"],
    .modal-overlay {
        padding: 0.5rem !important;
    }

    .modal-content,
    .modal-box {
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        border-radius: var(--radius-sm);
        max-height: 90vh;
        overflow-y: auto;
    }
}

/* ============================================================
   12. NOTIFICACIONES PANEL
   ============================================================ */

@media (max-width: 768px) {
    .notifications-panel {
        position: fixed !important;
        top: 56px !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        max-height: 70vh;
        overflow-y: auto;
        border-radius: 0 0 var(--radius-md) var(--radius-md);
        z-index: 1500;
    }
}

/* ============================================================
   13. UTILIDADES TOUCH
   ============================================================ */

/* Áreas clickeables mínimas 44x44px para touch */
@media (max-width: 1023px) {
    button,
    [role="button"],
    .btn,
    .nav-link,
    .tab-button,
    input[type="submit"],
    input[type="button"],
    input[type="reset"] {
        -webkit-tap-highlight-color: rgba(46, 184, 186, 0.2);
    }

    /* Checkboxes y radios más fáciles de tocar */
    input[type="checkbox"],
    input[type="radio"] {
        min-width: 20px;
        min-height: 20px;
    }
}

/* ============================================================
   14. SCROLLBAR ESTILIZADA (desktop)
   ============================================================ */

@media (min-width: 1024px) {
    ::-webkit-scrollbar {
        width: 6px;
        height: 6px;
    }

    ::-webkit-scrollbar-track {
        background: var(--bg-secondary);
    }

    ::-webkit-scrollbar-thumb {
        background: var(--color-gray);
        border-radius: 3px;
    }

    ::-webkit-scrollbar-thumb:hover {
        background: var(--color-primary);
    }
}

/* ============================================================
   15. DASHBOARD INDEX — STAT CARDS
   ============================================================ */

@media (max-width: 768px) {
    /* Stat cards en 2 columnas en mobile, no 1 */
    .stats-cards {
        grid-template-columns: 1fr 1fr !important;
        gap: var(--spacing-sm) !important;
    }

    .stat-card {
        padding: var(--spacing-sm) !important;
        gap: var(--spacing-sm) !important;
        flex-direction: column;
        text-align: center;
        align-items: center;
    }

    .stat-icon {
        font-size: 1.8rem !important;
    }

    .stat-info h3 {
        font-size: 1.4rem !important;
        margin-bottom: 2px !important;
    }

    .stat-info p {
        font-size: 0.75rem !important;
    }

    /* Buscador en columna */
    .buscador-row {
        flex-direction: column !important;
    }

    .buscador-field,
    .buscador-field-wide {
        min-width: 100% !important;
    }

    /* Cards del dashboard menos padding */
    .card {
        padding: var(--spacing-sm) !important;
    }

    .card-title {
        font-size: 1rem !important;
        margin-bottom: var(--spacing-sm) !important;
    }

    /* Estado bars mas compactas */
    .estado-bar-item {
        padding: 6px var(--spacing-sm) !important;
        font-size: 0.82rem;
    }

    .estado-bar-count {
        font-size: 1rem !important;
    }

    .estado-bar-item.es-subestado {
        margin-left: 10px !important;
        font-size: 0.78rem !important;
    }
}

@media (min-width: 769px) and (max-width: 1023px) {
    .stats-cards {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .stat-card {
        padding: var(--spacing-md) !important;
    }

    .stat-icon {
        font-size: 2.2rem !important;
    }

    .stat-info h3 {
        font-size: 1.6rem !important;
    }
}

/* ============================================================
   16. PRINT
   ============================================================ */

@media print {
    .sidebar,
    .top-header,
    .btn-menu-toggle,
    .theme-switcher,
    .notifications-wrapper,
    .header-actions {
        display: none !important;
    }

    .dashboard-wrapper {
        display: block;
    }

    .main-content {
        width: 100%;
    }

    .page-content {
        padding: 0 !important;
    }

    .table th,
    .table td {
        padding: 0.4rem !important;
        font-size: 11px;
    }
}
