<style>
  /* General styles for pages */
  @page {
    margin: 0.5in; /* Universal margin for all pages */
  }

  .page {
    width: 7.5in; /* Portrait width (8.5in - margins) */
    height: 10in; /* Portrait height (11in - margins) */
    margin: auto;
    overflow: hidden;
    position: relative;
  }

  .page-landscape {
    width: 10in; /* Landscape width (11in - margins) */
    height: 7.5in; /* Landscape height (8.5in - margins) */
    margin: auto;
    overflow: hidden;
    position: relative;
  }

  .bracket-container {
    width: 100%;
    height: auto;
    transform-origin: top left;
  }

  /* Print-specific styles */
  @media print {
    /* Set orientation for portrait pages */
    .page {
      size: 8.5in 11in; /* Portrait */
      page-break-after: always; /* Force new page */
    }

    /* Set orientation for landscape pages */
    .page-landscape {
      size: 11in 8.5in; /* Landscape */
      page-break-after: always; /* Force new page */
    }

    /* Reset body margins for print */
    body {
      margin: 0;
      padding: 0;
    }

    /* Ensure correct scaling during print */
    .bracket-container {
      transform-origin: top left;
    }

    /* Optional: Hide elements not needed in print */
    .no-print {
      display: none;
    }
  }
</style>

<script>
  function scaleContent() {
    document.querySelectorAll('.page, .page-landscape').forEach(page => {
      const container = page.querySelector('.bracket-container');
      const isLandscape = page.classList.contains('page-landscape');

      // Page dimensions (1 inch = 96px)
      const pageWidth = isLandscape ? 10 * 96 : 7.5 * 96;
      const pageHeight = isLandscape ? 7.5 * 96 : 10 * 96;

      // Subtract margins (0.5 inch margin)
      const availableWidth = pageWidth - (0.5 * 96 * 2);
      const availableHeight = pageHeight - (0.5 * 96 * 2);

      // Measure content dimensions
      const contentWidth = container.scrollWidth;
      const contentHeight = container.scrollHeight;

      // Calculate scale factors
      const scaleX = availableWidth / contentWidth;
      const scaleY = availableHeight / contentHeight;

      // Use a slightly relaxed scaling to avoid over-aggressive shrinking
      const scale = Math.min(scaleX, scaleY, 1); // Ensure scale does not exceed 100% (1)

      // Apply the scale
      container.style.transform = `scale(${scale})`;
      container.style.transformOrigin = 'top left';

      // Calculate the scaled dimensions
      const scaledWidth = contentWidth * scale;
      const scaledHeight = contentHeight * scale;

      // Center the content within the page
      const horizontalPadding = (pageWidth - scaledWidth) / 2;
      const verticalPadding = (pageHeight - scaledHeight) / 2;

      // Apply margin adjustments
      container.style.marginLeft = `${Math.max(0, horizontalPadding)}px`;
      container.style.marginTop = `${Math.max(0, verticalPadding)}px`;

      // Ensure container maintains consistent dimensions
      container.style.width = `${contentWidth}px`;
      container.style.height = `${contentHeight}px`;
    });
  }

  window.onload = scaleContent; // Ensure scaling on load
  window.onresize = scaleContent; // Recalculate on resize
  window.addEventListener('beforeprint', scaleContent); // Scale before printing
</script>

<% cache ["#{@tournament.id}_all_brackets", @tournament] do %> 
    <div class="page">
      <div class="bracket-container">
        <%= render :template => 'tournaments/team_scores' %>
      </div>
    </div>
    <% @weights.sort_by{|w| w.max}.each do |weight| %>
          <% if @tournament.tournament_type == "Pool to bracket" %>
                <!-- Need to define what the tournaments#bracket controller defines -->
                <% @matches = @tournament.matches.select{|m| m.weight_id == weight.id} %>
                <% @wrestlers = Wrestler.where(weight_id: weight.id) %>
                <% @pools = weight.pool_rounds(@matches) %>
                <% @weight = weight %>
                <%= render 'bracket_partial' %>
          <% elsif @tournament.tournament_type.include? "Modified 16 Man Double Elimination" or @tournament.tournament_type.include? "Regular Double Elimination" %>
            <!-- Need to define what the tournaments#bracket controller defines -->
            <% @matches = weight.matches %>
            <% @weight = weight %>
            <%= render 'bracket_partial' %>
          <% end %>
    <% end %>
<% end %>