:root {
    --primary-color: #3b82f6;
    --primary-hover: #2563eb;
    --secondary-color: #10b981;
    --bg-color: #f3f4f6;
    --sidebar-bg: #ffffff;
    --text-color: #1f2937;
    --border-color: #e5e7eb;
    --paper-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Noto Sans SC', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    height: 100vh;
    overflow: hidden;
}

.app-container {
    display: flex;
    height: 100%;
}

/* Sidebar Styles */
.sidebar {
    width: 280px;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border-color);
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 20px;
    flex-shrink: 0;
}

.right-sidebar {
    border-right: none;
    border-left: 1px solid var(--border-color);
}

.sidebar-header h2 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-color);
    margin-bottom: 10px;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.control-group label {
    font-size: 0.875rem;
    font-weight: 500;
    color: #4b5563;
}

/* Buttons & Inputs */
.button-group {
    display: flex;
    gap: 0;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    overflow: hidden;
}

.button-group .btn {
    flex: 1;
    border: none;
    border-radius: 0;
    border-right: 1px solid var(--border-color);
}

.button-group .btn:last-child {
    border-right: none;
}

.grid-2-col {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.btn {
    padding: 8px 12px;
    background: #fff;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.875rem;
    transition: all 0.2s;
    color: #374151;
}

.btn:hover {
    background-color: #f9fafb;
}

.btn.active {
    background-color: #eff6ff;
    color: var(--primary-color);
    border-color: var(--primary-color);
    font-weight: 500;
}

.btn.primary {
    background-color: var(--primary-color);
    color: white;
    border: none;
}

.btn.primary:hover {
    background-color: var(--primary-hover);
}

.btn.secondary {
    background-color: #fff;
    border: 1px solid var(--primary-color);
    color: var(--primary-color);
}

.btn.success {
    background-color: var(--secondary-color);
    color: white;
    border: none;
}

.btn.warning {
    background-color: #f59e0b;
    color: white;
    border: none;
}

.btn.warning:hover {
    background-color: #d97706;
}

.full-width {
    width: 100%;
}

input[type="number"],
select {
    padding: 8px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 0.875rem;
    width: 100%;
}

.checkbox-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.option-category {
    margin-bottom: 12px;
}

.option-category:last-child {
    margin-bottom: 0;
}

.category-title {
    font-size: 13px;
    font-weight: 600;
    color: #666;
    margin-bottom: 6px;
    padding-bottom: 4px;
    border-bottom: 1px solid #eee;
}

.checkbox-group label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 400;
    cursor: pointer;
}

/* Main Content Area */
.main-content {
    flex: 1;
    background-color: #e5e7eb;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.preview-header {
    background: #fff;
    padding: 10px 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    gap: 20px;
    font-size: 0.875rem;
    color: #6b7280;
}

.paper-container {
    flex: 1;
    padding: 40px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 40px;
    /* Gap between pages */
}

/* A4 Paper Styles */
.a4-page {
    width: 210mm;
    min-height: 297mm;
    /* Ensure it's always at least A4 */
    background: white;
    box-shadow: var(--paper-shadow);
    padding: 20mm;
    position: relative;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
}

.worksheet-header {
    margin-bottom: 20px;
    text-align: center;
}

.worksheet-header h1 {
    font-family: '楷体', 'KaiTi', serif;
    font-size: 28px;
    margin-bottom: 15px;
    color: #333;
}

.worksheet-header h1:only-child {
    margin-bottom: 0;
}

.worksheet-header .meta-info:only-child {
    margin-top: 0;
    margin-bottom: 0;
    border-top: 2px solid #333;
    padding-top: 8px;
}

.meta-info {
    display: flex;
    justify-content: space-between;
    border-bottom: 2px solid #333;
    padding-bottom: 8px;
    margin-bottom: 10px;
    font-size: 16px;
    font-family: '楷体', 'KaiTi', serif;
}

.meta-info span {
    flex: 1;
    text-align: center;
}

.meta-info span:first-child {
    text-align: left;
}

.meta-info span:last-child {
    text-align: right;
}

.problems-grid {
    display: grid;
    /* Columns and rows set by JS */
    width: 100%;
    flex: 1;
    /* Fill remaining space */
    align-content: start;
    justify-content: center;
    /* Center the entire grid horizontally */
    justify-items: center;
    /* Center items within their cells */
    margin: 0 auto;
    /* Ensure grid is centered */
}

.problem-item {
    font-family: Arial, sans-serif;
    padding: 2px 5px 2px 28px;
    /* Reduced top/bottom padding, Increased left for index */
    display: flex;
    align-items: center;
    justify-content: center;
    /* Center content */
    position: relative;
    white-space: nowrap;
    /* Prevent wrapping */
    font-weight: normal;
    /* Bold text removed */
    /* Font size is set inline */
    width: 100%;
    /* Take full width of grid cell */
    max-width: 100%;
    /* Prevent overflow */
}

/* Horizontal Layout Spacing */
.problem-item.horizontal span:not(.problem-index) {
    margin: 0 5px;
}

/* 4-Column Specific Adjustments */
.problems-grid.cols-4 .problem-item {
    padding: 2px 2px 2px 20px;
    /* Reduced padding */
}

.problems-grid.cols-4 .problem-item.horizontal span:not(.problem-index) {
    margin: 0 2px;
    /* Tighter spacing */
}

.problems-grid.cols-4 .problem-index {
    left: 2px;
    font-size: 0.35em;
    /* Smaller index */
}

/* 5-Column Specific Adjustments */
.problems-grid.cols-5 .problem-item {
    padding: 2px 1px 2px 16px;
    /* Even more reduced padding */
}

.problems-grid.cols-5 .problem-item.horizontal span:not(.problem-index) {
    margin: 0 1px;
    /* Tighter spacing */
}

.problems-grid.cols-5 .problem-index {
    left: 1px;
    font-size: 0.3em;
    /* Even smaller index */
}

.problem-item.bordered {
    border: 1px solid #ccc;
    border-radius: 8px;
    /* Softer border */
}

.problem-index {
    position: absolute;
    top: 2px;
    left: 4px;
    transform: none;
    font-size: 0.4em;
    /* Smaller relative to parent font size */
    color: #999;
    font-family: sans-serif;
    font-weight: normal;
    /* Keep index normal weight */
}

/* Vertical Calculation Style */
.vertical-box {
    display: inline-flex;
    flex-direction: column;
    align-items: flex-end;
    /* Align numbers to right */
    line-height: 1.2;
    white-space: nowrap;
}

.vertical-box .num-row {
    display: flex;
    align-items: center;
    gap: 4px;
    /* Space between op and num */
}

.vertical-box .op {
    margin-right: 4px;
}

.vertical-box .divider {
    width: 100%;
    height: 2px;
    background: #000;
    margin: 4px 0;
}

.vertical-box .res {
    min-height: 1em;
    /* Placeholder for result */
}


.worksheet-footer {
    position: absolute;
    bottom: 10mm;
    left: 0;
    width: 100%;
    text-align: center;
    font-size: 12px;
    color: #999;
}

/* Print Styles */
@page {
    size: A4;
    margin: 0;
}

@media print {

    body,
    html {
        margin: 0;
        padding: 0;
        visibility: hidden;
        background: white;
    }

    .paper-container {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        margin: 0;
        padding: 0;
        visibility: visible;
        background: white;
    }

    .paper-container * {
        visibility: visible;
    }

    .sidebar,
    .preview-header,
    .worksheet-header h1 {
        /* Keep header h1 visible? Usually yes. */
    }

    .sidebar,
    .preview-header {
        display: none !important;
    }

    .a4-page {
        width: 210mm;
        height: 297mm;
        /* Force A4 height */
        min-height: 297mm;
        margin: 0 auto;
        padding: 20mm;
        box-shadow: none;
        border: none;
        page-break-after: always;
        break-after: page;
        overflow: hidden;
        /* Hide anything that spills */
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
        position: relative;
        /* For footer positioning */
    }

    .a4-page:last-child {
        page-break-after: auto;
    }

    /* Adjust footer for print */
    .worksheet-footer {
        bottom: 10mm;
        position: absolute;
    }
}

.hidden {
    display: none !important;
}