void drawKreslingPattern() {
  noFill();
  float totalY = numSides * panelWidth;

  float polygonRadius = panelWidth / (2 * sin(PI / numSides));
  float capScoreDist = polygonRadius * 0.635;
  float capHeight = polygonRadius * 1.604;

  // --- MATHEMATICAL SOLVER FOR KRESLING OFFSET ---
  float a = panelWidth;
  float h = foldHeight;
  float n = numSides;
  float eta = PI / n;
  
  float A = 1.0 / pow(sin(eta), 2);
  float B = -(pow(a, 2) + (2 * a * h) / tan(eta));
  float C = pow(a, 2) * pow(h, 2);
  
  float sSquared = (-B - sqrt(pow(B, 2) - 4 * A * C)) / (2 * A);
  
  if (sSquared < 0 || Float.isNaN(sSquared)) {
     println("Not Possible");
    sSquared = 0.001; 
  } else{
    println("Possible");
  }
  
  float s = sqrt(sSquared);
  
  float gamma = asin(s / a);
  float beta = PI - gamma - eta;
  
  float foldOffset = -h / tan(beta);

  // --- 1. SCORE LINES ---
  strokeWeight(scoreWeight / globalScale);

  // Left Cap 
  stroke(0, 0, 255); // Mountain (Blue)
  float leftScoreX = capHeight - capScoreDist;
  drawDashedLine(leftScoreX, 0, leftScoreX, totalY + glueTabWidth, 4, 8);
  
  for (int i = 0; i <= numSides; i++) {
    float y = i * panelWidth;
    if (i > 0 && i < numSides) {
        stroke(0, 0, 255); // Mountain
        drawDashedLine(0, y, capHeight, y, 4, 8);
    }
    if (i < numSides) {
        stroke(255, 0, 0); // Valley (Red)
        drawDashedLine(0, y + panelWidth/2, leftScoreX, y + panelWidth, 4, 8);
    }
  }

  // Main Kresling Tiers
  float currentX = capHeight;
  float currentYOffset = 0;
  for (int t = 0; t < numTiers; t++) {
    float nextX = currentX + foldHeight;
    float nextYOffset = currentYOffset + foldOffset;

    stroke(0, 0, 255); // Mountain
    drawDashedLine(currentX, currentYOffset, currentX, currentYOffset + totalY + glueTabWidth, 4, 8);
    
    for (int i = 0; i < numSides; i++) {
      float yStart = currentYOffset + (i * panelWidth);
      float yEnd = nextYOffset + (i * panelWidth);
      float yEndNext = nextYOffset + ((i + 1) * panelWidth);
      
      // Mountain fold (parallelogram edge)
      if (yStart <= currentYOffset + totalY && yEnd <= nextYOffset + totalY) {
          stroke(0, 0, 255);
          drawDashedLine(currentX, yStart, nextX, yEnd, 4, 8);      
      }
          
      // Valley fold (diagonal)
      if (yStart <= currentYOffset + totalY && yEndNext <= nextYOffset + totalY) {
          stroke(255, 0, 0);
          drawDashedLine(currentX, yStart, nextX, yEndNext, 4, 8);  
      }
    }
    currentX = nextX;
    currentYOffset = nextYOffset;
  }

  // Right Cap
  stroke(0, 0, 255); // Mountain
  drawDashedLine(currentX, currentYOffset, currentX, currentYOffset + totalY + glueTabWidth, 4, 8);
  float rightScoreX = currentX + capScoreDist;
  drawDashedLine(rightScoreX, currentYOffset, rightScoreX, currentYOffset + totalY + glueTabWidth, 4, 8);
  
  for (int i = 0; i <= numSides; i++) {
    float y = currentYOffset + (i * panelWidth);
    if (i > 0 && i < numSides) {
        stroke(0, 0, 255); // Mountain
        drawDashedLine(currentX, y, currentX + capHeight, y, 4, 8);
    }
    if (i < numSides) {
        stroke(255, 0, 0); // Valley
        drawDashedLine(rightScoreX, y, currentX + capHeight, y + panelWidth/2, 4, 8);
    }
  }

  // Glue Tab horizontal lines (Mountain folds)
  float tabX = capHeight;
  float tabYOff = 0;
  stroke(0, 0, 255); 
  drawDashedLine(0, totalY, capHeight, totalY, 4, 8); 
  for (int t = 0; t < numTiers; t++) {
    drawDashedLine(tabX, tabYOff + totalY, tabX + foldHeight, tabYOff + foldOffset + totalY, 4, 8); 
    tabX += foldHeight; tabYOff += foldOffset;
  }
  drawDashedLine(tabX, tabYOff + totalY, tabX + capHeight, tabYOff + totalY, 4, 8); 

  // --- 2. CUT LINES ---
  stroke(0); // Reset cut lines to Black
  strokeWeight(cutWeight / globalScale);
  beginShape();
  vertex(0, 0); vertex(capHeight, 0);
  float edgeX = capHeight; float edgeY = 0;
  for(int t = 0; t < numTiers; t++){ edgeX += foldHeight; edgeY += foldOffset; vertex(edgeX, edgeY); }
  vertex(edgeX + capHeight, edgeY);
  vertex(edgeX + capHeight, edgeY + totalY + glueTabWidth);
  vertex(edgeX, edgeY + totalY + glueTabWidth);
  for(int t = 0; t < numTiers; t++){ edgeX -= foldHeight; edgeY -= foldOffset; vertex(edgeX, edgeY + totalY + glueTabWidth); }
  vertex(capHeight, totalY + glueTabWidth); vertex(0, totalY + glueTabWidth);
  endShape(CLOSE);

  float margin = panelWidth * 0.1;
  // Pass the calculated offset to the metadata function so it gets engraved!
  drawMetadata("Kresling", capHeight - margin, margin, capScoreDist, panelWidth, foldOffset);
}
