--- includes/common.inc.original	2008-04-09 23:11:44.000000000 +0200
+++ includes/common.inc	2008-07-05 21:42:42.000000000 +0200
@@ -1688,6 +1688,9 @@ function drupal_get_css($css = NULL) {
   $no_theme_preprocess = '';
 
   $preprocess_css = (variable_get('preprocess_css', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update'));
+  $group_css_enable = variable_get('group_css_enable', FALSE);
+  $group_css_limit = variable_get('group_css_limit', 0);
+
   $directory = file_directory_path();
   $is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC);
 
@@ -1700,6 +1703,11 @@ function drupal_get_css($css = NULL) {
   foreach ($css as $media => $types) {
     // If CSS preprocessing is off, we still need to output the styles.
     // Additionally, go through any remaining styles if CSS preprocessing is on and output the non-cached ones.
+    if ($group_css_enable) {
+      $no_module_preprocess_media = array();
+      $no_theme_preprocess_media = array();
+      $output_media = array();
+    }
     foreach ($types as $type => $files) {
       if ($type == 'module') {
         // Setup theme overrides for module styles.
@@ -1718,15 +1726,27 @@ function drupal_get_css($css = NULL) {
           // If a CSS file is not to be preprocessed and it's a module CSS file, it needs to *always* appear at the *top*,
           // regardless of whether preprocessing is on or off.
           if (!$preprocess && $type == 'module') {
-            $no_module_preprocess .= '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
+            if ($group_css_enable) {
+              $no_module_preprocess_media[] = '@import "'. base_path() . $file . $query_string .'";';
+            } else {
+              $no_module_preprocess .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $file . $query_string .'";</style>' ."\n";
+            }
           }
           // If a CSS file is not to be preprocessed and it's a theme CSS file, it needs to *always* appear at the *bottom*,
           // regardless of whether preprocessing is on or off.
           else if (!$preprocess && $type == 'theme') {
-            $no_theme_preprocess .= '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
+            if ($group_css_enable) {
+              $no_theme_preprocess_media[] = '@import "'. base_path() . $file . $query_string .'";';
+            } else {
+              $no_theme_preprocess .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $file . $query_string .'";</style>' ."\n";
+            }
           }
           else {
-            $output .= '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
+            if ($group_css_enable) {
+              $output_media[] = '@import "'. base_path() . $file . $query_string .'";';
+            } else {
+              $output .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $file . $query_string .'";</style>' ."\n";
+            }
           }
         }
       }
@@ -1735,7 +1755,78 @@ function drupal_get_css($css = NULL) {
     if ($is_writable && $preprocess_css) {
       $filename = md5(serialize($types) . $query_string) .'.css';
       $preprocess_file = drupal_build_css_cache($types, $filename);
-      $output .= '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $preprocess_file .'" />'."\n";
+      if ($group_css_enable) {
+        $output_media[] = '@import "'. base_path() . $preprocess_file .'";';
+      } else {
+        $output .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $preprocess_file .'";</style>'. "\n";
+      }
+    }
+
+    if ($group_css_enable && $no_module_preprocess_media) {
+      if ($group_css_limit > 0 ) {
+        $i = 1;
+        $importgroup = '';
+        foreach ($no_module_preprocess_media as $importfile) {
+          $importgroup .= $importfile ."\n";
+          if ($i < $group_css_limit) {
+            $i++;
+          } else {
+            $no_module_preprocess .= '<style type="text/css" media="'. $media .'">'. $importgroup .'</style>'. "\n";
+            $importgroup = '';
+            $i = 1;
+          }
+        }
+        if (!empty($importgroup)) {
+          $no_module_preprocess .= '<style type="text/css" media="'. $media .'">'. $importgroup .'</style>'. "\n";
+        }
+      } else {
+        $no_module_preprocess .= '<style type="text/css" media="'. $media .'">'. join("\n",$no_module_preprocess_media) .'</style>'. "\n";
+      }
+    }
+
+    if ($group_css_enable && $no_theme_preprocess_media) {
+      if ($group_css_limit > 0 ) {
+        $i = 1;
+        $importgroup = '';
+        foreach ($no_theme_preprocess_media as $importfile) {
+          $importgroup .= $importfile ."\n";
+          if ($i < $group_css_limit) {
+            $i++;
+          } else {
+            $no_theme_preprocess .= '<style type="text/css" media="'. $media .'">'. $importgroup .'</style>'. "\n";
+            $importgroup = '';
+            $i = 1;
+          }
+        }
+        if (!empty($importgroup)) {
+          $no_theme_preprocess .= '<style type="text/css" media="'. $media .'">'. $importgroup .'</style>'. "\n";
+        }
+      } else {
+        $no_theme_preprocess .= '<style type="text/css" media="'. $media .'">'. join("\n",$no_theme_preprocess_media) .'</style>'. "\n";
+      }
+    }
+
+    if ($group_css_enable && $output_media) {
+      if ($group_css_limit > 0 ) {
+        $i = 1;
+        $importgroup = '';
+        foreach ($output_media as $importfile) {
+          $importgroup .= $importfile ."\n";
+          if ($i < $group_css_limit) {
+            $i++;
+          } else {
+            $output .= '<style type="text/css" media="'. $media .'">'. $importgroup .'</style>'. "\n";
+            $importgroup = '';
+            $i = 1;
+          }
+        }
+        if (!empty($importgroup)) {
+          $output .= '<style type="text/css" media="'. $media .'">'. $importgroup .'</style>'. "\n";
+        }
+      } else {
+        $output .= '<style type="text/css" media="'. $media .'">'. join("\n",$output_media) .'</style>'. "\n";
+      }
+
     }
   }
 
--- modules/system/system.admin.inc.original	2008-03-25 11:58:16.000000000 +0100
+++ modules/system/system.admin.inc	2008-07-05 21:43:40.000000000 +0200
@@ -1334,6 +1334,29 @@ function system_performance_settings() {
     '#description' => t('This option can interfere with module development and should only be enabled in a production environment.'),
   );
 
+  $form['group_css'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Group CSS files'),
+    '#description' => '<p>' . t('Internet Explorer 6 and 7 ignore css stylesheets after 30 &lt;style&gt; or &lt;link&gt; tags (see <a href="@url_kb" title="@title_kb" rel=\"nofollow\">Microsoft Knowledge Base 262161</a> and this <a href="@url_issue" title="@title_issue">issue</a> on drupal.org.', array('@url_kb' => 'http://support.microsoft.com/kb/262161/en-us', '@title_kb' => 'All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer', '@url_issue' => 'http://drupal.org/node/228818', '@title_issue' => 'IE: Stylesheets ignored after 30 link/style tags')) . '</p>' . '<p>' . t('The following options provide the possibility to group multiple CSS files per media type into single &lt;style&gt; tags.  Because Internet Explorer 6 and 7 also do not process more than 30 CSS files inside one &lt;style&gt; tag, the second option allows to limit the number of CSS files per &lt;style&gt; tag.') . '</p>'
+  );
+
+  $form['group_css']['group_css_enable'] = array(
+    '#type' => 'radios',
+    '#title' => t('Group CSS files per media type'),
+    '#default_value' => intval(variable_get('group_css_enable', FALSE)),
+    '#options' => array(t('Disabled'), t('Enabled')),
+    '#description' => t("It is recommended to enable this option if more than 30 CSS stylesheets have to be loaded."),
+  );
+  $form['group_css']['group_css_limit'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Limit for the number of CSS files inside each style tag'),
+    '#default_value' => intval(variable_get('group_css_limit', 0)),
+    '#size' => 2,
+    '#maxlength' => 2,
+    '#description' => t("It is recommended to limit the number of CSS files to 30 if grouping CSS files per media type is enabled. The value 0 means \"no limit\""),
+    '#field_suffix' => t('CSS stylesheets')
+  );
+
   $form['clear_cache'] = array(
     '#type' => 'fieldset',
     '#title' => t('Clear cached data'),
@@ -1353,6 +1376,21 @@ function system_performance_settings() {
 }
 
 /**
+ * Validate the submitted performance form.
+ */
+function system_performance_settings_validate($form, &$form_state) {
+  // Validate the combine css settings
+  if ($form_state['values']['group_css_enable'] == '1') {
+    if (!is_numeric($form_state['values']['group_css_limit'])) {
+      form_set_error('group_css_limit', t('The limit for the number of CSS files inside each style tag is invalid. It must be a number between 0 and 99.'));
+    }
+    elseif ($form_state['values']['group_css_limit'] < 0 || $form_state['values']['group_css_limit'] > 99) {
+      form_set_error('group_css_limit', t('The limit for the number of CSS files inside each style tag is invalid. It must be a number between 0 and 99.'));
+    }
+  }
+}
+
+/**
  * Submit callback; clear system caches.
  *
  * @ingroup forms
