.elementor-27013 .elementor-element.elementor-element-5ddf9f0{--display:flex;}/* Start custom CSS */<!-- SD Leaflet Distribution & Printing Quote Builder (On-page, updated pricing) -->
<div id="sdq-app">
  <h2 class="sdq-title">Leaflet Quote Builder</h2>
  <p class="sdq-intro">
    Use this tool to calculate recommended retail prices (RRP) for leaflet printing and shared blanket distribution in GBP (£).
  </p>

  <div class="sdq-step-indicator">
    <span class="sdq-step-dot sdq-step-dot-active" data-step="1"></span>
    <span class="sdq-step-dot" data-step="2"></span>
    <span class="sdq-step-dot" data-step="3"></span>
    <span class="sdq-step-dot" data-step="4"></span>
  </div>

  <div class="sdq-card">

    <!-- STEP 1: Service type -->
    <section class="sdq-step sdq-step-active" data-step="1">
      <h3>Step 1 of 4: Choose service type</h3>
      <p>Please select what this quote is for:</p>
      <div class="sdq-field-group sdq-inline-options">
        <label>
          <input type="radio" name="sdq-service-type" value="distribution" checked>
          Distribution only
        </label>
        <label>
          <input type="radio" name="sdq-service-type" value="printing">
          Printing only
        </label>
        <label>
          <input type="radio" name="sdq-service-type" value="bundle">
          Print &amp; distribution bundle
        </label>
      </div>
    </section>

    <!-- STEP 2: Distribution -->
    <section class="sdq-step" data-step="2">
      <h3>Step 2 of 4: Distribution details</h3>
      <p>All distribution is shared, blanket delivery at £80 per 1,000 leaflets (ex. VAT).</p>

      <div class="sdq-grid-3">
        <div class="sdq-field-group">
          <label for="sdq-dist-size">Leaflet size</label>
          <select id="sdq-dist-size">
            <option value="">-- Select size --</option>
            <option value="A6">A6</option>
            <option value="A5">A5</option>
            <option value="A4">A4</option>
            <option value="DL">DL</option>
            <option value="A4 folded to A5">A4 folded to A5</option>
          </select>
        </div>

        <div class="sdq-field-group">
          <label for="sdq-dist-gsm">Paper weight</label>
          <select id="sdq-dist-gsm">
            <option value="">-- Select paper weight --</option>
            <option value="130">130gsm gloss</option>
            <option value="170">170gsm gloss</option>
            <option value="250">250gsm gloss</option>
          </select>
        </div>

        <div class="sdq-field-group">
          <label for="sdq-dist-quantity">Distribution quantity</label>
          <select id="sdq-dist-quantity">
            <option value="">-- Select size &amp; paper weight first --</option>
          </select>
          <small>Options mirror your print price list for the selected size and weight.</small>
        </div>
      </div>

      <div class="sdq-alert sdq-alert-warning" id="sdq-dist-pricing-warning" style="display:none;">
        No quantity options are configured for this size and weight yet. Please check the print price list.
      </div>

      <div class="sdq-grid-2">
        <div class="sdq-field-group">
          <label for="sdq-dist-postcode">Target postcode sector(s)</label>
          <input type="text" id="sdq-dist-postcode" placeholder="e.g. EX1 1, EX2 4, TQ12 3">
          <small>Use UK postcode sectors as free text.</small>
        </div>

        <div class="sdq-field-group">
          <label for="sdq-dist-notes">Distribution notes (optional)</label>
          <textarea id="sdq-dist-notes" rows="2" placeholder="Any special information about areas, timings, access, etc."></textarea>
        </div>
      </div>
    </section>

    <!-- STEP 3: Printing -->
    <section class="sdq-step" data-step="3">
      <h3>Step 3 of 4: Printing details</h3>
      <p>All printing is full colour, double-sided, gloss finish.</p>

      <div class="sdq-grid-3">
        <div class="sdq-field-group">
          <label for="sdq-print-size">Leaflet size</label>
          <select id="sdq-print-size">
            <option value="">-- Select size --</option>
            <option value="A6">A6</option>
            <option value="A5">A5</option>
            <option value="A4">A4</option>
            <option value="DL">DL</option>
            <option value="A4 folded to A5">A4 folded to A5</option>
          </select>
        </div>

        <div class="sdq-field-group">
          <label for="sdq-print-gsm">Paper weight</label>
          <select id="sdq-print-gsm">
            <option value="">-- Select paper weight --</option>
            <option value="130">130gsm gloss</option>
            <option value="170">170gsm gloss</option>
            <option value="250">250gsm gloss</option>
          </select>
        </div>

        <div class="sdq-field-group">
          <label for="sdq-print-quantity">Print quantity</label>
          <select id="sdq-print-quantity">
            <option value="">-- Select size &amp; paper weight first --</option>
          </select>
          <small>For 250gsm A4 folded to A5, this tool allows a maximum of 2,000.</small>
        </div>
      </div>

      <div class="sdq-alert sdq-alert-warning" id="sdq-print-pricing-warning" style="display:none;">
        No pricing has been configured for this size and weight yet. Please check the print price list.
      </div>
    </section>

    <!-- STEP 4: Summary -->
    <section class="sdq-step" data-step="4">
      <h3>Step 4 of 4: Quote summary</h3>

      <div class="sdq-summary-section">
        <h4>Service details</h4>
        <p><strong>Service type:</strong> <span id="sdq-summary-service-type"></span></p>
      </div>

      <div class="sdq-summary-section" id="sdq-summary-print-section" style="display:none;">
        <h4>Printing</h4>
        <p><span id="sdq-summary-print-description"></span></p>

        <div class="sdq-summary-grid">
          <div>
            <strong>Print ex. VAT</strong><br>
            £<span id="sdq-print-exvat">0.00</span>
          </div>
          <div>
            <label for="sdq-print-vat-rate"><strong>Print VAT rate</strong></label><br>
            <select id="sdq-print-vat-rate">
              <option value="20">20% (standard)</option>
              <option value="0">0% (zero-rated)</option>
            </select>
          </div>
          <div>
            <strong>Print VAT</strong><br>
            £<span id="sdq-print-vat">0.00</span>
          </div>
          <div>
            <strong>Print inc. VAT (before discount)</strong><br>
            £<span id="sdq-print-incvat">0.00</span>
          </div>
          <div>
            <label for="sdq-print-discount-rate"><strong>Print discount</strong></label><br>
            <select id="sdq-print-discount-rate">
              <option value="0">No discount</option>
              <option value="5">5% discount</option>
              <option value="10">10% discount</option>
              <option value="15">15% discount</option>
              <option value="20">20% discount</option>
            </select>
          </div>
          <div>
            <strong>Print total after discount</strong><br>
            £<span id="sdq-print-afterdisc">0.00</span>
          </div>
        </div>
      </div>

      <div class="sdq-summary-section" id="sdq-summary-dist-section" style="display:none;">
        <h4>Distribution</h4>
        <p>
          Shared blanket distribution at £80 per 1,000 leaflets (ex. VAT).<br>
          <strong>Size &amp; weight:</strong>
          <span id="sdq-summary-dist-size"></span>,
          <span id="sdq-summary-dist-gsm"></span>gsm gloss<br>
          <strong>Quantity:</strong> <span id="sdq-summary-dist-qty"></span> leaflets<br>
          <strong>Postcode sectors:</strong> <span id="sdq-summary-dist-postcode"></span>
        </p>

        <div class="sdq-summary-grid">
          <div>
            <strong>Distribution ex. VAT</strong><br>
            £<span id="sdq-dist-exvat">0.00</span>
          </div>
          <div>
            <label for="sdq-dist-vat-rate"><strong>Distribution VAT rate</strong></label><br>
            <select id="sdq-dist-vat-rate">
              <option value="20">20% (standard)</option>
              <option value="0">0% (zero-rated)</option>
            </select>
          </div>
          <div>
            <strong>Distribution VAT</strong><br>
            £<span id="sdq-dist-vat">0.00</span>
          </div>
          <div>
            <strong>Distribution inc. VAT (before discount)</strong><br>
            £<span id="sdq-dist-incvat">0.00</span>
          </div>
          <div>
            <label for="sdq-dist-discount-rate"><strong>Distribution discount</strong></label><br>
            <!-- labels rewritten in JS to include £xx.xx/1,000 -->
            <select id="sdq-dist-discount-rate">
              <option value="0">No discount</option>
              <option value="5">5% discount</option>
              <option value="10">10% discount</option>
              <option value="15">15% discount</option>
              <option value="20">20% discount</option>
              <option value="25">25% discount</option>
              <option value="30">30% discount</option>
              <option value="35">35% discount</option>
              <option value="40">40% discount</option>
              <option value="45">45% discount</option>
              <option value="50">50% discount</option>
            </select>
          </div>
          <div>
            <strong>Distribution total after discount</strong><br>
            £<span id="sdq-dist-afterdisc">0.00</span>
          </div>

          <!-- Profit & cost indicators -->
          <div>
            <strong>Outside Torbay cost</strong><br>
            £<span id="sdq-dist-cost-out">0.00</span><br>
            <span id="sdq-dist-profit-out">Profit: £0.00</span><br>
            <span id="sdq-dist-margin-out" class="sdq-margin-indicator">Margin: 0.0%</span>
          </div>
          <div>
            <strong>Torbay cost</strong><br>
            £<span id="sdq-dist-cost-tor">0.00</span><br>
            <span id="sdq-dist-profit-tor">Profit: £0.00</span><br>
            <span id="sdq-dist-margin-tor" class="sdq-margin-indicator">Margin: 0.0%</span>
          </div>
        </div>
        <small>Margin colour guide: <span class="sdq-margin-good">green = 40%+ margin</span>, <span class="sdq-margin-bad">red = under 40%.</span></small>
      </div>

      <div class="sdq-summary-section">
        <h4>Overall totals</h4>
        <div class="sdq-summary-grid">
          <div>
            <strong>Subtotal ex. VAT</strong><br>
            £<span id="sdq-overall-exvat">0.00</span>
          </div>
          <div>
            <strong>Total VAT</strong><br>
            £<span id="sdq-overall-vat">0.00</span>
          </div>
          <div>
            <strong>Subtotal inc. VAT (after section discounts)</strong><br>
            £<span id="sdq-overall-incvat">0.00</span>
          </div>
          <div>
            <label for="sdq-discount-rate"><strong>Overall discount</strong></label><br>
            <select id="sdq-discount-rate">
              <option value="0">No discount</option>
              <option value="5">5% discount</option>
              <option value="10">10% discount</option>
              <option value="15">15% discount</option>
              <option value="20">20% discount</option>
              <option value="25">25% discount</option>
              <option value="30">30% discount</option>
              <option value="35">35% discount</option>
              <option value="40">40% discount</option>
              <option value="45">45% discount</option>
              <option value="50">50% discount</option>
            </select>
          </div>
          <div>
            <strong>Final total (after all discounts)</strong><br>
            £<span id="sdq-final-total">0.00</span>
          </div>
        </div>
      </div>

      <div class="sdq-summary-section">
        <h4>Internal notes</h4>
        <textarea id="sdq-notes" rows="3" placeholder="Any extra information to include with this quote (visible only to staff)."></textarea>
      </div>
    </section>

    <div class="sdq-footer">
      <div class="sdq-footer-left">
        <button type="button" class="sdq-btn sdq-btn-secondary" id="sdq-reset-btn">Reset quote</button>
      </div>
      <div class="sdq-footer-right">
        <button type="button" class="sdq-btn sdq-btn-secondary" id="sdq-back-btn">Back</button>
        <button type="button" class="sdq-btn sdq-btn-primary" id="sdq-next-btn">Next</button>
      </div>
    </div>

    <div class="sdq-footer-actions">
      <button type="button" class="sdq-btn sdq-btn-ghost" id="sdq-copy-btn">Copy quote to clipboard</button>
      <button type="button" class="sdq-btn sdq-btn-ghost" id="sdq-email-btn">Send quote by email</button>
      <button type="button" class="sdq-btn sdq-btn-ghost" id="sdq-print-btn">Print quote</button>
    </div>

  </div>
</div>

<style>
  #sdq-app, #sdq-app * {
    box-sizing: border-box;
    font-family: Arial, sans-serif;
    font-size: 14px;
  }

  #sdq-app {
    max-width: 900px;
    margin: 0 auto;
    color: #00133d;
  }

  .sdq-title {
    text-align: left;
    margin-bottom: 4px;
  }

  .sdq-intro {
    margin-top: 0;
    margin-bottom: 16px;
  }

  .sdq-card {
    background: #ffffff;
    border-radius: 16px;
    border: 1px solid #a7a9ac;
    padding: 16px 18px 12px;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.08);
  }

  .sdq-step {
    display: none;
  }

  .sdq-step-active {
    display: block;
  }

  .sdq-step h3 {
    margin-top: 0;
  }

  .sdq-field-group {
    margin-bottom: 12px;
    text-align: left;
  }

  .sdq-field-group label {
    font-weight: 600;
    display: block;
    margin-bottom: 4px;
  }

  .sdq-field-group input[type="text"],
  .sdq-field-group select,
  .sdq-field-group textarea {
    width: 100%;
    padding: 6px 8px;
    border-radius: 6px;
    border: 1px solid #a7a9ac;
  }

  .sdq-field-group small {
    display: block;
    margin-top: 3px;
    color: #555555;
    font-size: 12px;
  }

  .sdq-inline-options label {
    display: inline-flex;
    align-items: center;
    margin-right: 16px;
    font-weight: 500;
  }

  .sdq-inline-options input[type="radio"] {
    margin-right: 6px;
  }

  .sdq-grid-3 {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 12px;
  }

  .sdq-grid-2 {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 12px;
  }

  .sdq-step-indicator {
    display: flex;
    justify-content: center;
    margin-bottom: 10px;
    gap: 6px;
  }

  .sdq-step-dot {
    width: 10px;
    height: 10px;
    border-radius: 999px;
    border: 2px solid #a7a9ac;
    background: #ffffff;
  }

  .sdq-step-dot-active {
    background: #00133d;
    border-color: #00133d;
  }

  .sdq-btn {
    font-size: 14px;
    padding: 8px 16px;
    border-radius: 999px;
    border: none;
    cursor: pointer;
    margin: 4px;
    white-space: nowrap;
  }

  .sdq-btn-primary {
    background-color: #00133d;
    color: #ffffff;
  }

  .sdq-btn-secondary {
    background-color: #f28c6f;
    color: #00133d;
  }

  .sdq-btn-ghost {
    background: transparent;
    border: 1px solid #a7a9ac;
    color: #00133d;
  }

  .sdq-btn:hover {
    opacity: 0.9;
  }

  .sdq-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-top: 1px solid #a7a9ac;
    margin-top: 8px;
    padding-top: 8px;
    flex-wrap: wrap;
    gap: 4px 8px;
  }

  .sdq-footer-right {
    display: flex;
    align-items: center;
    gap: 4px;
  }

  .sdq-footer-actions {
    margin-top: 4px;
    display: flex;
    justify-content: flex-end;
    flex-wrap: wrap;
  }

  .sdq-summary-section {
    border: 1px solid #a7a9ac;
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 10px;
    text-align: left;
    background: #f7f7f8;
  }

  .sdq-summary-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 8px;
    margin-top: 8px;
  }

  .sdq-summary-grid > div {
    background: #ffffff;
    border-radius: 8px;
    padding: 6px 8px;
    border: 1px solid #e0e0e0;
  }

  .sdq-alert {
    padding: 8px 10px;
    border-radius: 8px;
    font-size: 13px;
    margin: 8px 0;
  }

  .sdq-alert-warning {
    background: #fff3cd;
    border: 1px solid #ffeeba;
    color: #856404;
  }

  .sdq-margin-indicator {
    display: inline-block;
    margin-top: 3px;
  }

  .sdq-margin-good {
    color: #008000;
    font-weight: 700;
  }

  .sdq-margin-bad {
    color: #cc0000;
    font-weight: 700;
  }

  @media (max-width: 600px) {
    #sdq-app {
      padding: 0 4px;
    }
  }
</style>

<script>
  (function () {
    // Base distribution rate per 1,000 (used for dropdown hints & calculations)
    const DIST_BASE_RATE_PER_1000 = 80;

    // PRINT PRICING DATA (from latest "Print Pricing Dec 2025" PDF, Cost column, ex VAT)
    const pricingData = {
      "A6": {
        "130": {
          "25": 28.32,
          "50": 29.18,
          "100": 30.08,
          "150": 30.34,
          "200": 30.61,
          "250": 30.86,
          "300": 31.58,
          "400": 31.92,
          "500": 32.26,
          "1000": 38.91,
          "1500": 44.26,
          "2000": 49.62,
          "2500": 60.37,
          "5000": 71.10,
          "7500": 95.20,
          "10000": 119.30,
          "12500": 156.69,
          "15000": 187.82,
          "20000": 250.46,
          "25000": 310.05,
          "30000": 368.43,
          "35000": 425.65,
          "40000": 481.68,
          "45000": 536.56,
          "50000": 590.24,
          "55000": 639.09,
          "60000": 686.10,
          "65000": 731.26,
          "70000": 774.58,
          "75000": 816.05,
          "80000": 855.68,
          "85000": 893.44,
          "90000": 929.36,
          "95000": 963.44,
          "100000": 995.66
        },
        "170": {
          "25": 25.57,
          "50": 28.85,
          "100": 30.61,
          "150": 31.49,
          "200": 32.34,
          "250": 33.23,
          "300": 35.25,
          "400": 36.14,
          "500": 37.36,
          "1000": 47.14,
          "1500": 50.43,
          "2000": 53.71,
          "2500": 64.18,
          "5000": 87.47,
          "7500": 119.71,
          "10000": 157.52,
          "12500": 196.10,
          "15000": 226.37,
          "20000": 295.78,
          "25000": 365.18,
          "30000": 433.87,
          "35000": 502.53,
          "40000": 568.30,
          "45000": 634.08,
          "50000": 696.99,
          "55000": 756.22,
          "60000": 813.57,
          "65000": 868.98,
          "70000": 922.50,
          "75000": 974.13,
          "80000": 1023.82,
          "85000": 1071.65,
          "90000": 1117.55,
          "95000": 1161.55,
          "100000": 1203.66
        },
        "250": {
          "25": 28.32,
          "50": 30.08,
          "100": 32.05,
          "150": 36.11,
          "200": 40.16,
          "250": 44.22,
          "300": 46.98,
          "400": 49.49,
          "500": 51.28,
          "1000": 63.92,
          "2000": 79.09,
          "2500": 92.05,
          "5000": 123.86,
          "7500": 176.19,
          "10000": 228.48,
          "12500": 287.04,
          "15000": 333.54,
          "20000": 444.72,
          "25000": 529.54,
          "30000": 629.06,
          "35000": 726.51,
          "40000": 821.89,
          "45000": 915.18,
          "50000": 1006.42,
          "55000": 1096.90,
          "60000": 1185.54,
          "65000": 1272.30,
          "70000": 1357.25,
          "75000": 1440.34,
          "80000": 1521.57,
          "85000": 1600.96,
          "90000": 1678.50,
          "95000": 1754.19,
          "100000": 1828.05
        }
      },
      "A5": {
        "130": {
          "25": 30.10,
          "50": 32.40,
          "100": 34.72,
          "150": 36.94,
          "200": 39.18,
          "250": 41.39,
          "300": 42.13,
          "400": 49.15,
          "500": 52.51,
          "1000": 68.86,
          "1500": 78.27,
          "2000": 87.70,
          "2500": 106.50,
          "5000": 125.34,
          "7500": 161.36,
          "10000": 197.44,
          "12500": 234.34,
          "15000": 270.42,
          "20000": 342.86,
          "25000": 414.59,
          "30000": 485.56,
          "35000": 555.75,
          "40000": 625.16,
          "45000": 693.82,
          "50000": 761.71,
          "55000": 827.83,
          "60000": 892.22,
          "65000": 954.83,
          "70000": 1015.70,
          "75000": 1074.80,
          "80000": 1132.18,
          "85000": 1187.79,
          "90000": 1241.65,
          "95000": 1293.77,
          "100000": 1344.13
        },
        "170": {
          "25": 27.42,
          "50": 31.82,
          "100": 35.84,
          "150": 40.24,
          "200": 44.64,
          "250": 49.06,
          "300": 51.54,
          "400": 54.22,
          "500": 54.72,
          "1000": 73.57,
          "1500": 84.94,
          "2000": 96.30,
          "2500": 109.26,
          "5000": 163.73,
          "7500": 241.52,
          "10000": 317.58,
          "12500": 396.67,
          "15000": 474.40,
          "20000": 629.92,
          "25000": 785.34,
          "30000": 939.54,
          "35000": 1092.55,
          "40000": 1244.28,
          "45000": 1394.70,
          "50000": 1543.84,
          "55000": 1691.50,
          "60000": 1837.72,
          "65000": 1982.43,
          "70000": 2125.62,
          "75000": 2267.29,
          "80000": 2407.34,
          "85000": 2545.77,
          "90000": 2682.58,
          "95000": 2817.76,
          "100000": 2951.32
        },
        "250": {
          "25": 31.84,
          "50": 33.60,
          "100": 38.83,
          "150": 44.74,
          "200": 50.67,
          "250": 56.61,
          "300": 59.26,
          "400": 63.17,
          "500": 65.01,
          "1000": 86.69,
          "2000": 122.21,
          "2500": 145.74,
          "5000": 206.99,
          "7500": 291.94,
          "10000": 376.88,
          "12500": 459.15,
          "15000": 540.53,
          "20000": 703.85,
          "25000": 866.17,
          "30000": 1027.25,
          "35000": 1187.16,
          "40000": 1345.74,
          "45000": 1502.96,
          "50000": 1658.84,
          "55000": 1813.26,
          "60000": 1966.24,
          "65000": 2117.76,
          "70000": 2267.81,
          "75000": 2416.33,
          "80000": 2563.27,
          "85000": 2708.66,
          "90000": 2852.48,
          "95000": 2994.67,
          "100000": 3135.21
        }
      },
      "A4": {
        "130": {
          "25": 35.39,
          "50": 38.30,
          "100": 41.22,
          "150": 45.81,
          "200": 50.40,
          "250": 54.96,
          "300": 57.92,
          "400": 70.22,
          "500": 77.86,
          "1000": 102.53,
          "1500": 130.06,
          "2000": 157.58,
          "2500": 185.79,
          "5000": 214.02,
          "7500": 276.79,
          "10000": 339.58,
          "12500": 409.20,
          "15000": 476.98,
          "20000": 613.90,
          "25000": 739.76,
          "30000": 865.71,
          "35000": 989.41,
          "40000": 1111.88,
          "45000": 1233.09,
          "50000": 1353.06,
          "55000": 1471.79,
          "60000": 1589.27,
          "65000": 1705.48,
          "70000": 1820.47,
          "75000": 1934.20,
          "80000": 2046.68,
          "85000": 2157.87,
          "90000": 2267.84,
          "95000": 2376.51,
          "100000": 2483.90
        },
        "170": {
          "25": 31.15,
          "50": 35.14,
          "100": 40.64,
          "150": 49.14,
          "200": 57.57,
          "250": 66.06,
          "300": 74.58,
          "400": 79.57,
          "500": 85.58,
          "1000": 114.16,
          "1500": 143.32,
          "2000": 172.49,
          "2500": 205.61,
          "5000": 238.73,
          "7500": 310.00,
          "10000": 381.29,
          "12500": 458.58,
          "15000": 535.86,
          "20000": 687.70,
          "25000": 826.74,
          "30000": 966.87,
          "35000": 1105.10,
          "40000": 1241.41,
          "45000": 1376.81,
          "50000": 1510.31,
          "55000": 1642.88,
          "60000": 1774.55,
          "65000": 1905.29,
          "70000": 2035.12,
          "75000": 2164.02,
          "80000": 2292.00,
          "85000": 2419.05,
          "90000": 2545.18
        },
        "250": {
          "25": 33.62,
          "50": 38.96,
          "100": 49.25,
          "150": 57.78,
          "200": 66.29,
          "250": 74.80,
          "300": 83.10,
          "400": 92.03,
          "500": 98.32,
          "1000": 125.81,
          "2000": 240.59,
          "2500": 284.26,
          "5000": 413.60,
          "7500": 592.35,
          "10000": 797.28,
          "12500": 991.90,
          "15000": 1184.29,
          "20000": 1508.06,
          "25000": 1830.08,
          "30000": 2149.85,
          "35000": 2467.35,
          "40000": 2782.59,
          "45000": 3095.46,
          "50000": 3405.97,
          "55000": 3714.16,
          "60000": 4020.02,
          "65000": 4323.55,
          "70000": 4624.73
        }
      },
      "DL": {
        "130": {
          "25": 28.32,
          "50": 30.34,
          "100": 31.71,
          "150": 32.42,
          "200": 33.10,
          "250": 33.73,
          "300": 34.24,
          "400": 35.10,
          "500": 37.78,
          "1000": 46.98,
          "1500": 52.92,
          "2000": 58.83,
          "2500": 70.80,
          "5000": 82.78,
          "7500": 105.32,
          "10000": 127.87,
          "12500": 149.95,
          "15000": 171.73,
          "20000": 214.61,
          "25000": 256.90,
          "30000": 298.63,
          "35000": 339.90,
          "40000": 380.49,
          "45000": 420.51,
          "50000": 459.92,
          "55000": 498.70,
          "60000": 536.86,
          "65000": 574.42,
          "70000": 611.36,
          "75000": 647.70,
          "80000": 683.43,
          "85000": 718.57,
          "90000": 753.09,
          "95000": 787.00,
          "100000": 820.32
        },
        "170": {
          "25": 25.57,
          "50": 30.08,
          "100": 32.40,
          "150": 33.22,
          "200": 35.10,
          "250": 36.43,
          "300": 37.97,
          "400": 41.57,
          "500": 43.15,
          "1000": 55.55,
          "1500": 62.38,
          "2000": 69.20,
          "2500": 81.84,
          "5000": 94.49,
          "7500": 120.19,
          "10000": 145.90,
          "12500": 171.13,
          "15000": 196.14,
          "20000": 245.31,
          "25000": 293.75,
          "30000": 341.45,
          "35000": 388.41,
          "40000": 434.64,
          "45000": 480.13,
          "50000": 524.89,
          "55000": 568.89,
          "60000": 612.16,
          "65000": 654.70,
          "70000": 696.48,
          "75000": 737.54,
          "80000": 777.86,
          "85000": 817.42,
          "90000": 856.25,
          "95000": 894.34,
          "100000": 931.72
        },
        "250": {
          "25": 28.32,
          "50": 31.49,
          "100": 34.13,
          "150": 38.26,
          "200": 43.95,
          "250": 48.86,
          "300": 51.36,
          "400": 57.18,
          "500": 61.07,
          "1000": 78.51,
          "2000": 101.68,
          "2500": 122.72,
          "5000": 180.22,
          "7500": 260.62,
          "10000": 341.02,
          "12500": 420.10,
          "15000": 498.21,
          "20000": 652.79,
          "25000": 806.09,
          "30000": 958.02,
          "35000": 1108.60,
          "40000": 1257.74,
          "45000": 1405.43,
          "50000": 1551.68,
          "55000": 1696.46,
          "60000": 1839.82,
          "65000": 1981.73,
          "70000": 2122.18,
          "75000": 2261.21,
          "80000": 2398.77,
          "85000": 2534.89,
          "90000": 2669.54,
          "95000": 2802.73,
          "100000": 2934.46
        }
      },
      "A4 folded to A5": {
        "130": {
          "25": 38.72,
          "50": 42.83,
          "100": 51.65,
          "150": 59.90,
          "200": 68.19,
          "250": 73.58,
          "300": 79.01,
          "400": 84.67,
          "500": 90.32,
          "1000": 110.40,
          "1500": 128.91,
          "2000": 147.41,
          "2500": 172.84,
          "5000": 198.26,
          "7500": 254.24,
          "10000": 310.18,
          "12500": 371.48,
          "15000": 429.98,
          "20000": 544.21,
          "25000": 657.31,
          "30000": 769.11,
          "35000": 879.59,
          "40000": 988.80,
          "45000": 1096.69,
          "50000": 1203.31,
          "55000": 1308.61,
          "60000": 1412.57,
          "65000": 1515.22,
          "70000": 1616.51,
          "75000": 1716.49,
          "80000": 1815.13,
          "85000": 1912.47,
          "90000": 2008.46,
          "95000": 2103.13,
          "100000": 2196.46
        },
        "170": {
          "25": 39.38,
          "50": 44.29,
          "100": 54.96,
          "150": 60.69,
          "200": 74.94,
          "250": 84.99,
          "300": 89.02,
          "400": 93.31,
          "500": 97.62,
          "1000": 122.94,
          "1500": 144.41,
          "2000": 165.88,
          "2500": 195.07,
          "5000": 224.26,
          "7500": 298.73,
          "10000": 373.15,
          "12500": 449.48,
          "15000": 524.02,
          "20000": 673.06,
          "25000": 821.01,
          "30000": 967.68,
          "35000": 1113.11,
          "40000": 1257.23,
          "45000": 1400.07,
          "50000": 1541.57,
          "55000": 1681.74,
          "60000": 1820.55,
          "65000": 1958.02,
          "70000": 2094.12,
          "75000": 2228.81,
          "80000": 2362.16
        },
        "250": {
          "25": 38.56,
          "50": 44.02,
          "100": 55.66,
          "150": 66.59,
          "200": 77.62,
          "250": 88.64,
          "300": 93.63,
          "400": 98.86,
          "500": 104.08,
          "1000": 132.66,
          "2000": 242.91
        }
      }
    };

    function toMoney(value) {
      return (Math.round(value * 100) / 100).toFixed(2);
    }

    function calculatePrintExVat(size, gsm, qty) {
      if (!size || !gsm || !qty) return 0;
      const sizeData = pricingData[size] || {};
      const gsmData = sizeData[gsm] || {};
      const price = gsmData[qty];
      return price ? price : 0;
    }

    // Update distribution discount dropdown labels to show £xx.xx/1,000
    function updateDistDiscountLabels() {
      const select = document.getElementById("sdq-dist-discount-rate");
      if (!select) return;
      for (let i = 0; i < select.options.length; i++) {
        const opt = select.options[i];
        const val = Number(opt.value || 0);
        if (val === 0) {
          opt.textContent = "No discount (£" + toMoney(DIST_BASE_RATE_PER_1000) + "/1,000)";
        } else {
          const ratePerThousand = DIST_BASE_RATE_PER_1000 * (100 - val) / 100;
          opt.textContent = val + "% discount (£" + toMoney(ratePerThousand) + "/1,000)";
        }
      }
    }

    function populateQuantities(sizeId, gsmId, qtyId, warningId, enforceA4FoldLimit) {
      const size = document.getElementById(sizeId).value;
      const gsm = document.getElementById(gsmId).value;
      const qtySelect = document.getElementById(qtyId);
      const warning = document.getElementById(warningId);

      qtySelect.innerHTML = "";
      warning.style.display = "none";

      if (!size || !gsm) {
        qtySelect.innerHTML = '<option value="">-- Select size &amp; paper weight first --</option>';
        return;
      }

      const sizeData = pricingData[size] || {};
      const gsmData = sizeData[gsm] || {};
      const quantities = Object.keys(gsmData)
        .map(Number)
        .sort((a, b) => a - b);

      if (!quantities.length) {
        qtySelect.innerHTML = '<option value="">No quantities configured for this size &amp; weight</option>';
        warning.style.display = "block";
        return;
      }

      quantities.forEach(q => {
        if (enforceA4FoldLimit && size === "A4 folded to A5" && gsm === "250" && q > 2000) {
          return; // enforce max 2,000 on 250gsm A4 folded to A5
        }
        const opt = document.createElement("option");
        opt.value = q;
        opt.textContent = q.toLocaleString("en-GB");
        qtySelect.appendChild(opt);
      });

      if (!qtySelect.options.length) {
        qtySelect.innerHTML = '<option value="">No allowed quantities for this combination</option>';
        warning.style.display = "block";
      }
    }

    function getSelectedServiceType() {
      const radios = document.querySelectorAll('input[name="sdq-service-type"]');
      let val = "distribution";
      radios.forEach(r => {
        if (r.checked) val = r.value;
      });
      return val;
    }

    function isStepRelevant(step, serviceType) {
      if (step === 2 && serviceType === "printing") return false;
      if (step === 3 && serviceType === "distribution") return false;
      return true;
    }

    function updateStepVisibility(step) {
      const steps = document.querySelectorAll(".sdq-step");
      steps.forEach(s => {
        s.classList.remove("sdq-step-active");
        if (Number(s.getAttribute("data-step")) === step) {
          s.classList.add("sdq-step-active");
        }
      });

      const dots = document.querySelectorAll(".sdq-step-dot");
      dots.forEach(d => {
        d.classList.remove("sdq-step-dot-active");
        if (Number(d.getAttribute("data-step")) === step) {
          d.classList.add("sdq-step-dot-active");
        }
      });

      const backBtn = document.getElementById("sdq-back-btn");
      backBtn.style.visibility = (step === 1) ? "hidden" : "visible";

      const nextBtn = document.getElementById("sdq-next-btn");
      nextBtn.textContent = (step === 4) ? "Finish" : "Next";
    }

    function resetForm() {
      document.getElementById("sdq-dist-size").value = "";
      document.getElementById("sdq-dist-gsm").value = "";
      document.getElementById("sdq-dist-quantity").innerHTML = '<option value="">-- Select size &amp; paper weight first --</option>';
      document.getElementById("sdq-dist-postcode").value = "";
      document.getElementById("sdq-dist-notes").value = "";

      document.getElementById("sdq-print-size").value = "";
      document.getElementById("sdq-print-gsm").value = "";
      document.getElementById("sdq-print-quantity").innerHTML = '<option value="">-- Select size &amp; paper weight first --</option>';

      document.getElementById("sdq-print-vat-rate").value = "20";
      document.getElementById("sdq-dist-vat-rate").value = "20";
      document.getElementById("sdq-print-discount-rate").value = "0";
      document.getElementById("sdq-dist-discount-rate").value = "0";
      document.getElementById("sdq-discount-rate").value = "0";
      document.getElementById("sdq-notes").value = "";

      const serviceRadios = document.querySelectorAll('input[name="sdq-service-type"]');
      serviceRadios.forEach(r => { r.checked = (r.value === "distribution"); });

      currentStep = 1;
      updateStepVisibility(currentStep);
      calculateAndDisplayTotals();
    }

    function validateStep(step) {
      const serviceType = getSelectedServiceType();

      if (step === 1) return true;

      if (step === 2) {
        if (serviceType === "printing") return true;

        const size = document.getElementById("sdq-dist-size").value;
        const gsm = document.getElementById("sdq-dist-gsm").value;
        const qty = document.getElementById("sdq-dist-quantity").value;
        if (!size || !gsm || !qty) {
          alert("Please complete the size, paper weight and quantity for distribution.");
          return false;
        }
        return true;
      }

      if (step === 3) {
        if (serviceType === "distribution") return true;

        const size = document.getElementById("sdq-print-size").value;
        const gsm = document.getElementById("sdq-print-gsm").value;
        const qty = document.getElementById("sdq-print-quantity").value;

        if (!size || !gsm || !qty) {
          alert("Please complete the size, paper weight and quantity for printing.");
          return false;
        }

        const exVat = calculatePrintExVat(size, gsm, Number(qty));
        if (!exVat) {
          alert("No pricing is set for this size and weight yet. Please check the print price list.");
          return false;
        }

        return true;
      }

      return true;
    }

    function prepareSummary() {
      const serviceType = getSelectedServiceType();
      const summaryServiceType = document.getElementById("sdq-summary-service-type");

      if (serviceType === "distribution") summaryServiceType.textContent = "Distribution only";
      else if (serviceType === "printing") summaryServiceType.textContent = "Printing only";
      else summaryServiceType.textContent = "Print & distribution bundle";

      const printSection = document.getElementById("sdq-summary-print-section");
      const distSection = document.getElementById("sdq-summary-dist-section");

      printSection.style.display = (serviceType === "printing" || serviceType === "bundle") ? "block" : "none";
      distSection.style.display = (serviceType === "distribution" || serviceType === "bundle") ? "block" : "none";

      if (printSection.style.display === "block") {
        const size = document.getElementById("sdq-print-size").value;
        const gsm = document.getElementById("sdq-print-gsm").value;
        const qty = document.getElementById("sdq-print-quantity").value;
        if (size && gsm && qty) {
          document.getElementById("sdq-summary-print-description").textContent =
            qty.toLocaleString("en-GB") + " × " + size +
            ", " + gsm + "gsm, full colour, double-sided, gloss.";
        } else {
          document.getElementById("sdq-summary-print-description").textContent = "Not specified.";
        }
      }

      if (distSection.style.display === "block") {
        const distSize = document.getElementById("sdq-dist-size").value || "Not specified";
        const distGsm = document.getElementById("sdq-dist-gsm").value || "Not specified";
        const distQty = document.getElementById("sdq-dist-quantity").value || "0";
        const postcode = document.getElementById("sdq-dist-postcode").value || "Not specified";

        document.getElementById("sdq-summary-dist-size").textContent = distSize;
        document.getElementById("sdq-summary-dist-gsm").textContent = distGsm;
        document.getElementById("sdq-summary-dist-qty").textContent =
          Number(distQty).toLocaleString("en-GB");
        document.getElementById("sdq-summary-dist-postcode").textContent = postcode;
      }

      calculateAndDisplayTotals();
    }

    function calculateAndDisplayTotals() {
      const serviceType = getSelectedServiceType();

      // -------- PRINT SECTION --------
      let printExVatRRP = 0;
      let printVatRRP = 0;
      let printIncVatRRP = 0;
      let printExVatAfterDisc = 0;
      let printVatAfterDisc = 0;
      let printIncVatAfterDisc = 0;

      const printVatRate = Number(document.getElementById("sdq-print-vat-rate").value || 0);
      const printDiscRate = Number(document.getElementById("sdq-print-discount-rate").value || 0);

      if (serviceType === "printing" || serviceType === "bundle") {
        const size = document.getElementById("sdq-print-size").value;
        const gsm = document.getElementById("sdq-print-gsm").value;
        const qty = Number(document.getElementById("sdq-print-quantity").value || 0);
        printExVatRRP = calculatePrintExVat(size, gsm, qty) || 0;
      }

      if (printExVatRRP > 0) {
        printVatRRP = printExVatRRP * (printVatRate / 100);
        printIncVatRRP = printExVatRRP + printVatRRP;

        printExVatAfterDisc = printExVatRRP * (100 - printDiscRate) / 100;
        printVatAfterDisc = printExVatAfterDisc * (printVatRate / 100);
        printIncVatAfterDisc = printExVatAfterDisc + printVatAfterDisc;
      }

      document.getElementById("sdq-print-exvat").textContent = toMoney(printExVatRRP);
      document.getElementById("sdq-print-vat").textContent = toMoney(printVatRRP);
      document.getElementById("sdq-print-incvat").textContent = toMoney(printIncVatRRP);
      document.getElementById("sdq-print-afterdisc").textContent = toMoney(printIncVatAfterDisc);

      // -------- DISTRIBUTION SECTION --------
      let distExVatRRP = 0;
      let distVatRRP = 0;
      let distIncVatRRP = 0;
      let distExVatAfterDisc = 0;
      let distVatAfterDisc = 0;
      let distIncVatAfterDisc = 0;

      const distVatRate = Number(document.getElementById("sdq-dist-vat-rate").value || 0);
      const distDiscRate = Number(document.getElementById("sdq-dist-discount-rate").value || 0);
      const distQty = Number(document.getElementById("sdq-dist-quantity").value || 0);

      if ((serviceType === "distribution" || serviceType === "bundle") && distQty > 0) {
        const thousands = Math.ceil(distQty / 1000);
        distExVatRRP = thousands * DIST_BASE_RATE_PER_1000;
      }

      if (distExVatRRP > 0) {
        distVatRRP = distExVatRRP * (distVatRate / 100);
        distIncVatRRP = distExVatRRP + distVatRRP;

        distExVatAfterDisc = distExVatRRP * (100 - distDiscRate) / 100;
        distVatAfterDisc = distExVatAfterDisc * (distVatRate / 100);
        distIncVatAfterDisc = distExVatAfterDisc + distVatAfterDisc;
      }

      document.getElementById("sdq-dist-exvat").textContent = toMoney(distExVatRRP);
      document.getElementById("sdq-dist-vat").textContent = toMoney(distVatRRP);
      document.getElementById("sdq-dist-incvat").textContent = toMoney(distIncVatRRP);
      document.getElementById("sdq-dist-afterdisc").textContent = toMoney(distIncVatAfterDisc);

      // -------- DISTRIBUTION COSTS & MARGINS (using ex VAT AFTER distribution discount) --------
      let distCostOut = 0, distProfitOut = 0, distMarginOut = 0;
      let distCostTor = 0, distProfitTor = 0, distMarginTor = 0;

      if (distQty > 0 && distExVatAfterDisc > 0) {
        distCostOut = distQty * 0.055;
        distProfitOut = distExVatAfterDisc - distCostOut;
        distMarginOut = (distProfitOut / distExVatAfterDisc) * 100;

        distCostTor = distQty / 3 / 120 * 13.50;
        distProfitTor = distExVatAfterDisc - distCostTor;
        distMarginTor = (distProfitTor / distExVatAfterDisc) * 100;
      }

      document.getElementById("sdq-dist-cost-out").textContent = toMoney(distCostOut);
      document.getElementById("sdq-dist-profit-out").textContent = "Profit: £" + toMoney(distProfitOut);
      document.getElementById("sdq-dist-cost-tor").textContent = toMoney(distCostTor);
      document.getElementById("sdq-dist-profit-tor").textContent = "Profit: £" + toMoney(distProfitTor);

      const marginOutSpan = document.getElementById("sdq-dist-margin-out");
      const marginTorSpan = document.getElementById("sdq-dist-margin-tor");

      const marginOutText = distMarginOut > 0 ? distMarginOut.toFixed(1) : "0.0";
      const marginTorText = distMarginTor > 0 ? distMarginTor.toFixed(1) : "0.0";

      marginOutSpan.textContent = "Margin: " + marginOutText + "%";
      marginTorSpan.textContent = "Margin: " + marginTorText + "%";

      marginOutSpan.classList.remove("sdq-margin-good", "sdq-margin-bad");
      marginTorSpan.classList.remove("sdq-margin-good", "sdq-margin-bad");

      if (distMarginOut >= 40) {
        marginOutSpan.classList.add("sdq-margin-good");
      } else {
        marginOutSpan.classList.add("sdq-margin-bad");
      }

      if (distMarginTor >= 40) {
        marginTorSpan.classList.add("sdq-margin-good");
      } else {
        marginTorSpan.classList.add("sdq-margin-bad");
      }

      // -------- OVERALL TOTALS (AFTER section discounts) --------
      const overallExVat = printExVatAfterDisc + distExVatAfterDisc;
      const overallVat = printVatAfterDisc + distVatAfterDisc;
      const overallIncVat = printIncVatAfterDisc + distIncVatAfterDisc;

      document.getElementById("sdq-overall-exvat").textContent = toMoney(overallExVat);
      document.getElementById("sdq-overall-vat").textContent = toMoney(overallVat);
      document.getElementById("sdq-overall-incvat").textContent = toMoney(overallIncVat);

      const overallDiscRate = Number(document.getElementById("sdq-discount-rate").value || 0);
      const finalTotal = overallIncVat * (100 - overallDiscRate) / 100;
      document.getElementById("sdq-final-total").textContent = toMoney(finalTotal);
    }

    function buildQuoteText() {
      const notes = document.getElementById("sdq-notes").value || "";

      const lines = [];
      lines.push("Leaflet Quote");
      lines.push("==============");
      lines.push("Service type: " + document.getElementById("sdq-summary-service-type").textContent);
      lines.push("");

      const printVisible = document.getElementById("sdq-summary-print-section").style.display !== "none";
      const distVisible = document.getElementById("sdq-summary-dist-section").style.display !== "none";

      if (printVisible) {
        lines.push("Printing:");
        lines.push("  " + document.getElementById("sdq-summary-print-description").textContent);
        lines.push("  Ex. VAT: £" + document.getElementById("sdq-print-exvat").textContent);
        lines.push("  VAT: £" + document.getElementById("sdq-print-vat").textContent);
        lines.push("  Inc. VAT (before discount): £" + document.getElementById("sdq-print-incvat").textContent);
        lines.push("  Discount: " + document.getElementById("sdq-print-discount-rate").value + "%");
        lines.push("  Total after discount: £" + document.getElementById("sdq-print-afterdisc").textContent);
        lines.push("");
      }

      if (distVisible) {
        lines.push("Distribution:");
        lines.push("  Size & weight: " +
          document.getElementById("sdq-summary-dist-size").textContent + ", " +
          document.getElementById("sdq-summary-dist-gsm").textContent + "gsm gloss");
        lines.push("  Quantity: " + document.getElementById("sdq-summary-dist-qty").textContent + " leaflets");
        lines.push("  Postcode sectors: " + document.getElementById("sdq-summary-dist-postcode").textContent);
        lines.push("  Ex. VAT: £" + document.getElementById("sdq-dist-exvat").textContent);
        lines.push("  VAT: £" + document.getElementById("sdq-dist-vat").textContent);
        lines.push("  Inc. VAT (before discount): £" + document.getElementById("sdq-dist-incvat").textContent);
        lines.push("  Discount: " + document.getElementById("sdq-dist-discount-rate").value + "%");
        lines.push("  Total after discount: £" + document.getElementById("sdq-dist-afterdisc").textContent);
        lines.push("  Outside Torbay cost: £" + document.getElementById("sdq-dist-cost-out").textContent +
          " | " + document.getElementById("sdq-dist-profit-out").textContent +
          " | " + document.getElementById("sdq-dist-margin-out").textContent);
        lines.push("  Torbay cost: £" + document.getElementById("sdq-dist-cost-tor").textContent +
          " | " + document.getElementById("sdq-dist-profit-tor").textContent +
          " | " + document.getElementById("sdq-dist-margin-tor").textContent);
        lines.push("");
      }

      lines.push("Overall:");
      lines.push("  Subtotal ex. VAT: £" + document.getElementById("sdq-overall-exvat").textContent);
      lines.push("  VAT: £" + document.getElementById("sdq-overall-vat").textContent);
      lines.push("  Subtotal inc. VAT (after section discounts): £" + document.getElementById("sdq-overall-incvat").textContent);
      lines.push("  Overall discount: " + document.getElementById("sdq-discount-rate").value + "%");
      lines.push("  Final total (after all discounts): £" + document.getElementById("sdq-final-total").textContent);
      lines.push("");

      if (notes.trim()) {
        lines.push("Internal notes:");
        lines.push(notes);
        lines.push("");
      }

      lines.push("All prices in GBP (£).");

      return lines.join("\n");
    }

    function copyQuoteToClipboard() {
      const text = buildQuoteText();
      if (navigator.clipboard && navigator.clipboard.writeText) {
        navigator.clipboard.writeText(text).then(() => {
          alert("Quote copied to clipboard.");
        }).catch(() => {
          alert("Unable to copy automatically. You can paste using Ctrl+V / Cmd+V.");
        });
      } else {
        alert("Clipboard copying is not supported in this browser.");
      }
    }

    function emailQuote() {
      const text = buildQuoteText();
      const email = "bookings@sdleafletdistribution.co.uk"; // change if needed
      const subject = "Leaflet quote";
      const mailtoLink = "mailto:" + encodeURIComponent(email) +
        "?subject=" + encodeURIComponent(subject) +
        "&body=" + encodeURIComponent(text);
      window.location.href = mailtoLink;
    }

    function printQuote() {
      window.print();
    }

    // Event bindings & step logic
    let currentStep = 1;

    document.getElementById("sdq-back-btn").addEventListener("click", () => {
      if (currentStep > 1) {
        const serviceType = getSelectedServiceType();
        let prevStep = currentStep - 1;
        while (prevStep > 1 && !isStepRelevant(prevStep, serviceType)) {
          prevStep--;
        }
        currentStep = prevStep;
        updateStepVisibility(currentStep);
      }
    });

    document.getElementById("sdq-next-btn").addEventListener("click", () => {
      if (!validateStep(currentStep)) return;

      if (currentStep < 4) {
        const serviceType = getSelectedServiceType();
        let nextStep = currentStep + 1;
        while (nextStep < 4 && !isStepRelevant(nextStep, serviceType)) {
          nextStep++;
        }
        currentStep = nextStep;
        updateStepVisibility(currentStep);
        if (currentStep === 4) {
          prepareSummary();
        }
      } else {
        prepareSummary();
        alert("Quote summary updated. You can now copy, email or print it.");
      }
    });

    document.getElementById("sdq-reset-btn").addEventListener("click", resetForm);

    // Quantity population
    document.getElementById("sdq-print-size").addEventListener("change", () => {
      populateQuantities("sdq-print-size", "sdq-print-gsm", "sdq-print-quantity", "sdq-print-pricing-warning", true);
      calculateAndDisplayTotals();
    });
    document.getElementById("sdq-print-gsm").addEventListener("change", () => {
      populateQuantities("sdq-print-size", "sdq-print-gsm", "sdq-print-quantity", "sdq-print-pricing-warning", true);
      calculateAndDisplayTotals();
    });
    document.getElementById("sdq-print-quantity").addEventListener("change", calculateAndDisplayTotals);

    document.getElementById("sdq-dist-size").addEventListener("change", () => {
      populateQuantities("sdq-dist-size", "sdq-dist-gsm", "sdq-dist-quantity", "sdq-dist-pricing-warning", true);
      calculateAndDisplayTotals();
    });
    document.getElementById("sdq-dist-gsm").addEventListener("change", () => {
      populateQuantities("sdq-dist-size", "sdq-dist-gsm", "sdq-dist-quantity", "sdq-dist-pricing-warning", true);
      calculateAndDisplayTotals();
    });
    document.getElementById("sdq-dist-quantity").addEventListener("change", calculateAndDisplayTotals);

    document.getElementById("sdq-print-vat-rate").addEventListener("change", calculateAndDisplayTotals);
    document.getElementById("sdq-dist-vat-rate").addEventListener("change", calculateAndDisplayTotals);
    document.getElementById("sdq-print-discount-rate").addEventListener("change", calculateAndDisplayTotals);
    document.getElementById("sdq-dist-discount-rate").addEventListener("change", calculateAndDisplayTotals);
    document.getElementById("sdq-discount-rate").addEventListener("change", calculateAndDisplayTotals);

    const serviceRadios = document.querySelectorAll('input[name="sdq-service-type"]');
    serviceRadios.forEach(r => {
      r.addEventListener("change", () => {
        const serviceType = getSelectedServiceType();

        if (currentStep === 2 && serviceType === "printing") {
          currentStep = 3;
          updateStepVisibility(currentStep);
        } else if (currentStep === 3 && serviceType === "distribution") {
          currentStep = 2;
          updateStepVisibility(currentStep);
        }

        calculateAndDisplayTotals();
      });
    });

    document.getElementById("sdq-copy-btn").addEventListener("click", copyQuoteToClipboard);
    document.getElementById("sdq-email-btn").addEventListener("click", emailQuote);
    document.getElementById("sdq-print-btn").addEventListener("click", printQuote);

    // Initialise
    updateStepVisibility(currentStep);
    updateDistDiscountLabels();
    calculateAndDisplayTotals();
  })();
</script>/* End custom CSS */