:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --primary-light: #818cf8;
    --accent: #f43f5e;
    --bg-main: #f8fafc;
    --sidebar-bg: #ffffff;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --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);
    --radius: 12px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Outfit', sans-serif;
}

body {
    background-color: var(--bg-main);
    color: var(--text-main);
    overflow: auto;
    /* Allow scrolling by default, or handle in media queries */
    height: 100vh;
}

#app {
    display: flex;
    height: 100vh;
}

@media (max-width: 1024px) {
    body {
        height: auto;
        overflow-y: auto;
    }

    #app {
        flex-direction: column;
        height: auto;
        overflow-y: visible;
    }
}

/* Sidebar Styling */
.sidebar {
    width: 320px;
    background: var(--sidebar-bg);
    border-right: 1px solid #e2e8f0;
    padding: 2rem;
    display: flex;
    flex-direction: column;
    z-index: 10;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
}

@media (max-width: 1024px) {
    .sidebar {
        width: 100%;
        border-right: none;
        border-bottom: 2px solid #e2e8f0;
        padding: 1.5rem;
        height: auto;
        overflow-y: visible;
    }
}

.sidebar-header {
    margin-bottom: 2rem;
    text-align: center;
}

.logo {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    font-family: 'Fredoka', cursive;
    font-size: 1.75rem;
    color: var(--primary);
    font-weight: 600;
}

.sidebar-header p {
    font-size: 0.875rem;
    color: var(--text-muted);
    letter-spacing: 0.1em;
    text-transform: uppercase;
    margin-top: 0.25rem;
}

.control-group {
    margin-bottom: 2rem;
}

.control-group h3 {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.input-wrapper {
    margin-bottom: 1rem;
}

.input-wrapper label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 0.5rem;
    color: var(--text-main);
}

input[type="text"],
select {
    width: 100%;
    padding: 0.75rem;
    border: 2px solid #f1f5f9;
    border-radius: var(--radius);
    font-size: 0.9rem;
    transition: all 0.2s;
    background: #f8fafc;
}

input:focus,
select:focus {
    outline: none;
    border-color: var(--primary-light);
    background: #fff;
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
}

.operation-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.5rem;
}

.op-btn {
    aspect-ratio: 1;
    border: 2px solid #f1f5f9;
    border-radius: 10px;
    background: #f8fafc;
    cursor: pointer;
    font-size: 1rem;
    color: var(--text-muted);
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.op-btn:hover {
    transform: translateY(-2px);
    background: #fff;
}

.op-btn.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

.digit-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.5rem;
}

.digit-btn {
    padding: 0.75rem 0;
    border: 2px solid #f1f5f9;
    border-radius: 10px;
    background: #f8fafc;
    cursor: pointer;
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-muted);
    transition: all 0.2s;
}

.digit-btn:hover {
    background: #fff;
    border-color: var(--primary-light);
}

.digit-btn.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

.range-value {
    display: block;
    text-align: right;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--primary);
    margin-top: 0.25rem;
}

input[type="range"] {
    width: 100%;
    accent-color: var(--primary);
}

.checkbox-wrapper {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 1rem;
}

.primary-btn {
    width: 100%;
    padding: 1rem;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    font-size: 1rem;
    margin-bottom: 1rem;
}

.primary-btn:hover {
    background: var(--primary-dark);
    box-shadow: 0 8px 20px rgba(99, 102, 241, 0.3);
    transform: translateY(-2px);
}

.export-options {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
}

.secondary-btn {
    padding: 0.75rem;
    background: #f1f5f9;
    color: var(--text-main);
    border: none;
    border-radius: var(--radius);
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    text-align: left;
}

.btn-text {
    display: flex;
    flex-direction: column;
    font-size: 0.85rem;
}

.btn-text span {
    font-size: 0.65rem;
    color: var(--text-muted);
}

.secondary-btn:hover {
    background: #e2e8f0;
}

.workspace {
    flex: 1;
    overflow: auto;
    padding: 3rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    background: #e2e8f0;
    background-image:
        radial-gradient(#cbd5e1 1.5px, transparent 1.5px),
        radial-gradient(#cbd5e1 1.5px, transparent 1.5px);
    background-size: 30px 30px;
    background-position: 0 0, 15px 15px;
    position: relative;
    min-height: 400px;
}

@media (max-width: 768px) {
    .workspace {
        padding: 1rem;
    }
}

.preview-header {
    width: 100%;
    max-width: 800px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}

@media (max-width: 600px) {
    .preview-header {
        flex-direction: column;
        gap: 1rem;
        text-align: center;
    }

    .zoom-controls {
        transform: scale(0.9);
    }
}

.preview-header h2 {
    font-size: 1.25rem;
    color: var(--text-muted);
    font-weight: 500;
}

.zoom-controls {
    display: flex;
    align-items: center;
    gap: 1rem;
    background: white;
    padding: 0.5rem 1rem;
    border-radius: 99px;
    box-shadow: var(--shadow-md);
}

.zoom-controls button {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    padding: 0.25rem;
}

/* A4 Container & Page */
.a4-container {
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    transform-origin: top center;
    display: flex;
    flex-direction: column;
    gap: 30mm;
    padding: 20px 20px 100px 20px;
    width: fit-content;
    margin: 0 auto;
    /* Ensure centering */
}

.a4-page {
    width: 210mm;
    height: 297mm;
    min-width: 210mm;
    /* Strict width */
    min-height: 297mm;
    background: white;
    padding: 15mm 15mm;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.15);
    display: flex;
    flex-direction: column;
    position: relative;
    border: 1px solid #e2e8f0;
    page-break-after: always;
    flex-shrink: 0;
    /* Never squash the page */
    margin: 0 auto;
    box-sizing: border-box;
    font-size: 14px;
}

.ws-header {
    border-bottom: 3px solid #333;
    padding-bottom: 1.5rem;
    margin-bottom: 2rem;
}

.ws-header h1 {
    font-family: 'Fredoka', cursive;
    font-size: 1.75rem;
    /* Smaller header */
    margin-bottom: 1rem;
    color: #000;
}

.ws-meta {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.meta-field {
    font-size: 1.1rem;
    color: #444;
}

.meta-field span {
    font-weight: 600;
    width: 80px;
    display: inline-block;
}

/* Question Grid Implementation */
.problems-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: repeat(5, 1fr);
    gap: 0.5rem 2rem;
    flex: 1;
    margin-top: 0.5rem;
}

@media (max-width: 210mm) {

    /* Handle sub-A4 viewports specifically for the preview scaling */
    .a4-container {
        width: 100%;
        overflow-x: auto;
        display: flex;
        justify-content: center;
    }
}

@media (max-width: 1024px) {
    .workspace {
        padding: 2rem 1rem;
        width: 100%;
        /* Use full width */
        height: auto;
        /* Allow height to expand */
        min-height: unset;
        overflow-x: hidden;
        overflow-y: visible;
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    .a4-container {
        margin: 0 auto;
        width: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
    }
}

.problem-item {
    font-size: 1.4rem;
    /* Smaller problem font */
    font-family: 'Fredoka', cursive;
    display: flex;
    flex-direction: row;
    /* Horizontal layout for num + calculation + answer */
    align-items: flex-start;
    padding: 0.75rem;
    border-radius: 8px;
    position: relative;
}

.problem-num {
    font-size: 0.8rem;
    font-family: 'Outfit', sans-serif;
    color: #888;
    margin-right: 1.5rem;
    padding-top: 0.5rem;
}

.calculation {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    width: 80px;
    /* Thinner columns */
}

.row {
    height: 1.8rem;
    /* Tighter rows */
    display: flex;
    align-items: center;
}

.answer-space {
    margin-left: auto;
    /* Push to far right */
    display: flex;
    align-items: center;
    justify-content: flex-end;
    min-width: 100px;
}

.answer-key {
    color: var(--accent);
    font-weight: 700;
    opacity: 0;
    font-size: 1.1rem;
    border-left: 2px dashed #cbd5e1;
    padding-left: 1rem;
    transition: opacity 0.2s;
}

.show-answers .answer-key {
    opacity: 1;
}

/* Long Division Styling */
.long-division {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-left: 1rem;
    font-family: 'Fredoka', cursive;
    font-size: 1.4rem;
}

.divisor {
    padding-right: 0.5rem;
    border-right: 2px solid #000;
    height: 1.8rem;
    display: flex;
    align-items: center;
}

.dividend {
    padding: 0 0.5rem;
    border-top: 2px solid #000;
    position: relative;
    top: 4px;
    /* Align top bar with vertical line */
    height: 1.8rem;
    display: flex;
    align-items: center;
}

.ws-footer {
    margin-top: auto;
    text-align: center;
    font-size: 0.875rem;
    color: #999;
    padding-top: 2rem;
    border-top: 1px dashed #eee;
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}

/* Print Specifics */
@media print {
    body * {
        visibility: hidden;
    }

    .a4-container,
    .a4-container * {
        visibility: visible;
    }

    .a4-container {
        position: absolute;
        left: 0;
        top: 0;
        transform: scale(1) !important;
    }

    .a4-page {
        box-shadow: none;
        padding: 0;
    }
}