/* --- Global Design System --- */
:root {
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --accent: #f59e0b;
    --background: #f8fafc;
    --surface: #ffffff;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 24px;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background-color: var(--background);
    color: var(--text-main);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

.container {
    max-width: 900px;
    margin: 0 auto;
    padding: 2rem 1rem;
}

/* --- Header & Branding --- */
header {
    text-align: center;
    margin-bottom: 3rem;
}

.brand-title {
    font-size: 3rem;
    font-weight: 800;
    background: linear-gradient(135deg, var(--primary), #818cf8);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: -0.02em;
    margin-bottom: 0.5rem;
}

.brand-subtitle {
    color: var(--text-muted);
    font-size: 1.125rem;
    font-weight: 500;
}

/* --- Configuration Panel --- */
.config-panel {
    background: var(--surface);
    padding: 2.5rem;
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
    margin-bottom: 3rem;
}

.config-section {
    margin-bottom: 2.5rem;
}

.section-title {
    font-size: 1.25rem;
    font-weight: 700;
    margin-bottom: 1.5rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    color: var(--text-main);
}

.section-title span {
    font-size: 1.5rem;
}

/* Grid Layouts for options */
.operations-grid,
.digits-grid,
.layout-grid {
    display: grid;
    gap: 1rem;
}

.operations-grid {
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
}

.digits-grid {
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
}

@media (max-width: 600px) {
    .digits-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 0.75rem;
    }

    .diff-card {
        padding: 0.75rem 0.5rem;
        display: flex;
        flex-direction: column;
        justify-content: center;
        aspect-ratio: 1;
        /* Make them square-ish */
    }

    .diff-title {
        font-size: 0.8rem;
        margin-bottom: 0;
        line-height: 1.1;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 0;
    }

    .d-num {
        font-size: 1.75rem;
        font-weight: 800;
        color: var(--primary);
        line-height: 1;
        margin-bottom: 0.1rem;
    }

    .diff-example {
        display: none;
        /* Hide example on mobile for cleaner look */
    }
}

.layout-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 1rem;
}

/* Option Cards */
.op-card,
.diff-card,
.layout-card {
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: var(--radius-md);
    padding: 1.25rem;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: hidden;
    text-align: center;
}

.op-card:hover,
.diff-card:hover,
.layout-card:hover {
    border-color: var(--primary);
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.op-card.active,
.diff-card.active,
.layout-card.active {
    border-color: var(--primary);
    background: rgba(79, 70, 229, 0.04);
}

.op-card.active::after,
.diff-card.active::after,
.layout-card.active::after {
    content: "✓";
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    background: var(--primary);
    color: white;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    font-size: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
}

/* Operation Specifics */
.op-icon {
    font-size: 2rem;
    margin-bottom: 0.75rem;
    display: block;
}

.op-name {
    font-weight: 700;
    font-size: 1rem;
    margin-bottom: 0.25rem;
}

.op-example,
.diff-example {
    font-size: 0.85rem;
    color: var(--text-muted);
}

/* Difficulty Specifics */
.diff-title {
    font-weight: 700;
    margin-bottom: 0.5rem;
}

/* Layout Specifics & Mini Previews */
.mini-preview {
    min-height: 90px;
    background: #f8fafc;
    border-radius: var(--radius-sm);
    margin-bottom: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.75rem 0.5rem;
    font-family: 'Courier New', Courier, monospace;
    font-weight: 700;
    font-size: 0.7rem;
    color: #475569;
    border: 1px solid #e2e8f0;
}

.p-num {
    font-size: 0.65rem;
    opacity: 0.6;
}

.mini-preview.vertical {
    flex-direction: row;
    gap: 0.5rem;
}

.p-math {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.p-math span {
    line-height: 1.3;
    font-size: 0.7rem;
}

.p-line {
    width: 25px;
    height: 1.5px;
    background: #475569;
    margin-top: 1px;
}

.mini-preview.horizontal {
    white-space: normal;
    word-wrap: break-word;
    text-align: center;
    padding: 0.75rem;
    line-height: 1.4;
}

.layout-name {
    font-weight: 700;
    font-size: 0.85rem;
    color: var(--text-main);
}

.settings-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 2rem;
    margin-bottom: 2.5rem;
    padding-top: 2rem;
    border-top: 1px solid var(--border);
}

@media (max-width: 768px) {
    .settings-grid {
        grid-template-columns: 1fr;
        gap: 1.5rem;
    }

    .setting-group {
        align-items: center;
    }

    .setting-label {
        width: 100%;
        justify-content: space-between;
    }

    .config-panel {
        padding: 1.5rem;
    }

    .brand-title {
        font-size: 2rem;
    }
}

.setting-group {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    align-items: stretch;
}

.setting-group:last-child {
    justify-content: center;
}

.setting-label {
    font-weight: 600;
    color: var(--text-main);
    display: flex;
    justify-content: space-between;
}

.range-value {
    color: var(--primary);
}

.stepper-control {
    display: flex;
    align-items: center;
    gap: 0;
    border: 2px solid var(--border);
    border-radius: var(--radius-md);
    overflow: hidden;
    background: white;
    width: fit-content;
}

.stepper-btn {
    background: white;
    border: none;
    padding: 0.75rem 1.25rem;
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text-main);
    cursor: pointer;
    transition: all 0.2s;
    line-height: 1;
    min-width: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.stepper-btn:hover {
    background: #f8fafc;
    color: var(--primary);
}

.stepper-btn:active {
    background: #e2e8f0;
}

.stepper-value {
    border: none;
    border-left: 2px solid var(--border);
    border-right: 2px solid var(--border);
    padding: 0.75rem 1.5rem;
    font-size: 1.1rem;
    font-weight: 600;
    text-align: center;
    min-width: 60px;
    background: white;
    color: var(--text-main);
    -moz-appearance: textfield;
    appearance: textfield;
}

.stepper-value::-webkit-outer-spin-button,
.stepper-value::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.stepper-value:focus {
    outline: none;
}

.example-preview {
    background: #f1f5f9;
    padding: 0.75rem;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    margin-top: 0.5rem;
    color: var(--text-muted);
    font-style: italic;
    text-align: center;
}

/* Switch Toggle */
.switch {
    width: 50px;
    height: 26px;
    position: relative;
    display: inline-block;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #cbd5e1;
    transition: 0.4s;
    border-radius: 34px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 18px;
    width: 18px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    transition: 0.4s;
    border-radius: 50%;
}

input:checked+.slider {
    background-color: var(--primary);
}

input:checked+.slider:before {
    transform: translateX(24px);
}

/* Buttons */
.btn-primary {
    width: 100%;
    padding: 1.25rem;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius-lg);
    font-size: 1.25rem;
    font-weight: 800;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 10px 15px -3px rgba(79, 70, 229, 0.3);
}

.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-2px);
    box-shadow: 0 15px 20px -3px rgba(79, 70, 229, 0.4);
}

.generating {
    cursor: wait;
    opacity: 0.8;
}

/* --- Preview Section --- */
.preview-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
    flex-wrap: wrap;
    gap: 1rem;
}

.preview-actions {
    display: flex;
    gap: 0.75rem;
}

.btn-action {
    padding: 0.6rem 1.2rem;
    border-radius: var(--radius-md);
    border: none;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.btn-action.shuffle {
    background: #f1f5f9;
    color: #475569;
}

.btn-action.download {
    background: var(--accent);
    color: white;
}

.btn-action.print {
    background: #1e293b;
    color: white;
}

.btn-action:hover {
    transform: translateY(-2px);
    filter: brightness(1.1);
}

#worksheet-preview {
    background: #edf2f7;
    /* Background page to make the paper pop */
    padding: 3rem 1rem;
    min-height: 400px;
    margin: 2rem auto;
    border-radius: var(--radius-md);
    display: flex;
    flex-direction: column;
    align-items: center;
}

.worksheet-container {
    background: white;
    padding: 2.5rem;
    /* Reduced slightly to account for frame */
    width: 210mm;
    min-height: 297mm;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
    border: 1px solid #e2e8f0;
    margin-bottom: 3rem;
    position: relative;
    display: flex;
    flex-direction: column;
}

/* Inner Frame from Reference */
.worksheet-container::before {
    content: '';
    position: absolute;
    top: 10mm;
    left: 10mm;
    right: 10mm;
    bottom: 10mm;
    border: 4px solid #000;
    pointer-events: none;
    z-index: 10;
}

.worksheet-inner-content {
    padding: 10mm;
    flex: 1;
    z-index: 5;
}

/* Response Scaling for Preview */
@media (max-width: 900px) {
    #worksheet-preview {
        padding: 1rem;
        min-height: auto;
        overflow-x: hidden;
        /* Hide scrollbars if scale is perfect */
    }

    .worksheet-container {
        transform-origin: top center;
        /* Center the paper */
        transform: scale(0.6);
        margin-bottom: -120mm;
        margin-left: auto;
        margin-right: auto;
    }
}

@media (max-width: 600px) {
    .worksheet-container {
        transform: scale(0.45);
        /* Slightly bigger for readability */
        margin-bottom: -160mm;
    }
}

/* Rendered Worksheet Styles */
.worksheet-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    margin-bottom: 1.5rem;
    padding-bottom: 1rem;
    border-bottom: 2px solid #000;
}

.worksheet-title {
    font-size: 2.8rem;
    font-weight: 800;
    text-transform: uppercase;
    color: black;
    margin: 0;
    line-height: 1;
}

.worksheet-meta {
    text-align: right;
    font-size: 1.1rem;
    font-weight: 500;
    color: #333;
    line-height: 1.6;
}

.worksheet-meta div {
    white-space: nowrap;
}

.worksheet-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 3.5rem 2rem;
    width: 100%;
}

.worksheet-grid.horizontal-layout {
    grid-template-columns: repeat(3, 1fr);
    gap: 3rem 1rem;
}

.problem {
    font-family: 'Courier New', Courier, monospace;
    font-weight: 700;
    display: flex;
    align-items: flex-start;
    gap: 1.5rem;
}

.problem-number {
    font-family: 'Outfit', sans-serif;
    font-size: 1.1rem;
    font-weight: 400;
    color: #000;
    width: 30px;
    text-align: right;
}

.problem-inner {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    min-width: 80px;
}

.problem-row {
    font-size: 1.8rem;
    display: flex;
    justify-content: flex-end;
    width: 100%;
    line-height: 1.2;
}

.operator {
    margin-right: 0.75rem;
}

.line {
    width: 100%;
    height: 3px;
    background: #000;
    margin-top: 0.25rem;
    margin-bottom: 0.5rem;
}

.answer-value {
    font-size: 1.8rem;
    color: #444;
}

/* Utilities */
.hidden {
    display: none;
}


/* Placeholder for custom print overrides */

.ad-slot {
    background: #f1f5f9;
    border: 2px dashed #cbd5e1;
    padding: 1rem;
    text-align: center;
    color: #94a3b8;
    border-radius: var(--radius-md);
    margin-bottom: 2rem;
    font-weight: 600;
}

/* Info Cards */
.info-section {
    padding: 4rem 0;
}

.features-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    margin-bottom: 4rem;
}

.feature-card {
    background: white;
    padding: 1.5rem;
    border-radius: var(--radius-lg);
    border: 1px solid #e2e8f0;
}

.feature-card h3 {
    margin-bottom: 0.75rem;
    color: var(--primary);
}

.how-to-use,
.seo-text {
    background: white;
    padding: 2.5rem;
    border-radius: var(--radius-xl);
    border: 1px solid #e2e8f0;
    margin-bottom: 2.5rem;
}

@media (max-width: 640px) {
    .brand-title {
        font-size: 2.25rem;
    }

    .config-panel {
        padding: 1.5rem;
    }

    .layout-grid {
        grid-template-columns: 1fr;
    }

    .preview-header {
        flex-direction: column;
        align-items: flex-start;
    }
}

@media print {

    .no-print,
    header,
    .config-panel,
    footer,
    .ad-slot,
    .preview-header,
    .info-section,
    .preview-actions {
        display: none !important;
    }

    body {
        background: white;
        margin: 0;
        padding: 0;
    }

    .container {
        max-width: none;
        width: 100%;
        margin: 0;
        padding: 0;
    }

    #worksheet-preview {
        background: none;
        padding: 0;
        margin: 0;
        display: block !important;
    }

    .worksheet-container {
        box-shadow: none;
        border: none;
        padding: 0;
        margin: 0 !important;
        width: 100%;
        page-break-after: always;
        transform: none !important;
        /* Ensure it's not scaled */
    }

    .answer-key-section {
        display: none;
        /* Already handled by page-break or PDF logic */
    }
}