/* Preview Area */
.preview-area {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    padding: 20px;
    /* Allow preview area to fill the grid cell and match controls height */
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0; /* allow flex children to shrink properly */
}

.preview-header {
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 2px solid #ecf0f1;
}

.preview-header h2 {
    font-size: 1.25rem;
    color: #2c3e50;
    margin-bottom: 5px;
}

.preview-note {
    font-size: 0.85rem;
    color: #7f8c8d;
}

/* Worksheet Container */
.worksheet-container {
    background: white;
    /* Make the worksheet container take the remaining vertical space in preview-area */
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0; /* required so flex child can shrink and prevent internal scrolling */
    align-items: flex-start; /* top align */
    justify-content: center;
    position: relative;
    padding-top: 8px;
    padding-bottom: 8px;
}

.placeholder {
    color: #95a5a6;
    font-size: 1rem;
    text-align: center;
}

/* When the worksheet container only contains the placeholder, center it vertically
   and horizontally inside the preview panel */
.worksheet-container .placeholder {
    /* Center the placeholder inside the same max-width as the worksheet PDF
       so it visually aligns with generated content. */
    width: 100%;
    max-width: 800px; /* match .worksheet max-width */
    margin: 18px auto 0; /* sits below the preview header */
    text-align: center;
    color: #95a5a6;
}

.worksheet-container iframe {
    width: 100%;
    border: none;
    /* Responsive height: viewport height minus header + top padding. Adjust offset as needed */
    height: calc(100vh - 220px);
    max-height: 1200px;
    min-height: 480px;
}

/* Worksheet Styles */
.worksheet {
    width: 100%;
    max-width: 800px;
    margin: 0 auto;
    font-family: 'Open Sans', sans-serif;
    color: #000;
    background: white;
}

/* Worksheet Header */
.worksheet-header {
    border: 2px solid #000;
    padding: 15px;
    margin-bottom: 20px;
}

.worksheet-title {
    text-align: center;
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 15px;
    color: #000;
}

.worksheet-info {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    font-size: 0.95rem;
}

.info-field {
    display: flex;
    align-items: center;
}

.info-field label {
    font-weight: 600;
    margin-right: 8px;
    color: #000;
}

.info-field-line {
    flex: 1;
    border-bottom: 1px solid #000;
    min-width: 100px;
}

/* Problems Container */
.problems-container {
    display: grid;
    margin-top: 20px;
    /* Gap is set dynamically via inline styles based on number of problems and columns */
}

/* Column layouts */
.problems-container.cols-1 {
    grid-template-columns: 1fr;
}

.problems-container.cols-2 {
    grid-template-columns: 1fr 1fr;
}

.problems-container.cols-3 {
    grid-template-columns: 1fr 1fr 1fr;
}

.problems-container.cols-4 {
    grid-template-columns: 1fr 1fr 1fr 1fr;
}

.problems-container.cols-5 {
    grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
}

.problems-container.cols-6 {
    grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr;
}

/* Individual Problem Styles */
.problem {
    page-break-inside: avoid;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Vertical Format */
.problem.vertical {
    font-family: 'Open Sans', sans-serif;
    font-size: 1.1rem;
    line-height: 1;
    color: #000;
}

.problem-number {
    display: none;
}

.problem.vertical .problem-content {
    display: inline-block;
    text-align: center;
    line-height: 1;
    min-width: 80%;
}

.problem.vertical .operand-row {
    text-align: right;
    margin-bottom: 2px;
    line-height: 1;
    white-space: nowrap;
}

.problem.vertical .operator-row {
    border-bottom: 2px solid #000;
    text-align: right;
    margin-bottom: 5px;
    padding-bottom: 2px;
    display: block;
    width: 100%;
    line-height: 1;
    white-space: nowrap;
}

.problem.vertical .operator-row .operator {
    margin-right: 4px;
}

.problem.vertical .answer-row {
    text-align: right;
    /* slightly larger min-height and margin so the answer doesn't butt up against the
       operator line when the answer key is shown */
    min-height: 1.8em;
    margin-top: 8px;
    line-height: 1;
    white-space: nowrap;
}

/* Horizontal Format */
.problem.horizontal {
    font-size: 1.1rem;
    padding: 8px 0;
    color: #000;
}

.problem.horizontal .equation {
    display: flex;
    align-items: center;
    gap: 8px;
}

.problem.horizontal .answer-blank {
    display: inline-block;
    border-bottom: 1px solid #000;
    min-width: 60px;
    height: 1.2em;
}

/* Answer Key */
.answer-key {
    margin-top: 40px;
    padding-top: 20px;
    border-top: 3px solid #000;
}

.answer-key-title {
    font-size: 1.25rem;
    font-weight: 700;
    margin-bottom: 15px;
    text-align: center;
    color: #000;
}

/* Print Styles */
@media print {
    .preview-area {
        box-shadow: none;
        padding: 0;
    }

    .preview-header {
        display: none;
    }

    .worksheet {
        max-width: 100%;
    }
}

/* Responsive */
@media (max-width: 768px) {
    .worksheet-info {
        grid-template-columns: 1fr;
    }

    .problems-container.cols-3,
    .problems-container.cols-4,
    .problems-container.cols-5,
    .problems-container.cols-6 {
        grid-template-columns: 1fr 1fr;
    }
}

@media (max-width: 480px) {
    .problems-container.cols-2,
    .problems-container.cols-3,
    .problems-container.cols-4,
    .problems-container.cols-5,
    .problems-container.cols-6 {
        grid-template-columns: 1fr;
    }

    .worksheet-title {
        font-size: 1.25rem;
    }
}
