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

  .page {
    width: 7.8in; /* 8.5in - 2 * 0.35in */
    height: 10.3in; /* 11in - 2 * 0.35in */
    margin: auto;
    overflow: hidden;
    position: relative;
  }

  .page-landscape {
    width: 10.3in; /* 11in - 2 * 0.35in */
    height: 7.8in; /* 8.5in - 2 * 0.35in */
    margin: auto;
    overflow: hidden;
    position: relative;
  }

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

  .bracket-container h4 {
    margin-top: 0.15rem;
    margin-bottom: 0.45rem;
  }

  /* 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;
    }

    .bracket {
      page-break-inside: avoid;
    }

    /* 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');

      // Use the actual page box size (already accounts for @page margins)
      const availableWidth = page.clientWidth;
      const availableHeight = page.clientHeight;

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

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

      // Keep a tiny buffer so borders/text don't clip at print edges
      const scale = Math.min(scaleX, scaleY, 1) * 0.99;

      // 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 available page box
      const horizontalPadding = (availableWidth - scaledWidth) / 2;
      const verticalPadding = (availableHeight - 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 = @matches_by_weight_id[weight.id] || [] %>
                <% @wrestlers = @wrestlers_by_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 = @matches_by_weight_id[weight.id] || [] %>
            <% @wrestlers = @wrestlers_by_weight_id[weight.id] || [] %>
            <% @weight = weight %>
            <%= render 'bracket_partial' %>
          <% end %>
    <% end %>
<% end %>
