/* 1. 基础布局 */
body {
    display: flex;
    height: 100vh;
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background-color: #f4f5f7;
    overflow: hidden; 
}

/* 2. 控制侧边栏 (PC) */
.controls {
    width: 300px; 
    min-width: 300px;
    padding: 20px;
    /* 修复：底部安全区 */
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
    background-color: #fff;
    box-shadow: 2px 0 10px rgba(0,0,0,0.05);
    overflow-y: auto; 
    z-index: 1000;
    transition: left 0.3s ease;
    box-sizing: border-box; /* 确保 padding 不会撑破 */
}
.control-group { margin-bottom: 15px; }
.control-group label { display: block; font-weight: 600; font-size: 14px; margin-bottom: 8px; }
.control-group input[type="text"],
.control-group input[type="number"],
.control-group select,
.control-group textarea {
    width: 100%; padding: 8px; font-size: 14px; border: 1px solid #ddd; border-radius: 4px; box-sizing: border-box;
}
.control-group textarea { height: 80px; resize: vertical; }
.control-group .checkbox-label { display: flex; align-items: center; font-weight: normal; margin-bottom: 5px; }
.action-button { width: 100%; padding: 10px 15px; font-size: 16px; font-weight: 600; border: none; border-radius: 5px; cursor: pointer; margin-top: 10px; }
#generate-pinyin-btn, #generate-math-btn { background-color: #007bff; color: white; }
#print-btn { background-color: #28a745; color: white; }

/* 新增：字体加载按钮 */
#load-fonts-btn {
    width: auto;
    background-color: #6c757d;
    padding: 4px 10px;
    font-size: 12px;
    margin-top: 0;
    float: right;
}

/* Tab 切换样式 */
.tab-nav { display: flex; border-bottom: 1px solid #ddd; margin-bottom: 20px; }
.tab-btn { flex: 1; padding: 10px; border: none; background: #f0f0f0; cursor: pointer; font-size: 16px; font-weight: 600; color: #555; border-top-left-radius: 5px; border-top-right-radius: 5px; }
.tab-btn.active { background: #fff; color: #007bff; border-top: 2px solid #007bff; border-left: 1px solid #ddd; border-right: 1px solid #ddd; position: relative; bottom: -1px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* 3. 页面预览区 */
.page-container {
    flex: 1;
    overflow: auto; 
    padding: 20px;
    display: block; 
}
.page-a4 {
    width: 210mm;
    min-height: 297mm; /* 保证至少有一页高，但允许内容溢出时扩展 */
    overflow: visible;   /* 允许内容溢出可见，以便父容器滚动 */
    padding: 15mm;
    margin: 20px auto; /* PC上居中 */
    background: white;
    box-shadow: 0 0 15px rgba(0,0,0,0.1);
    box-sizing: border-box;
}

/* 页头 */
header.page-header { text-align: center; margin-bottom: 6mm; }
header.page-header h1 { font-size: 20pt; margin: 0 0 6mm 0; font-family: 'SimHei', '黑体', sans-serif; }
.info-fields { display: flex; justify-content: space-around; font-size: 11pt; font-family: 'KaiTi', '楷体', serif; }

/* 4. 语文样式 (没变) */
.grid-container { display: grid; gap: 10px; }
.grid-box { border: 1px solid #000; display: flex; flex-direction: column; background: #fff; break-inside: avoid; box-sizing: border-box; }
.grid-pinyin { height: 39px; border-bottom: 1px solid #000; display: flex; justify-content: center; align-items: center; font-size: 20px; font-family: "Arial", sans-serif; box-sizing: border-box; position: relative; }
.grid-pinyin span { position: relative; z-index: 1; } /* Bring text to the foreground */
.grid-pinyin::before, .grid-pinyin::after { content: ''; position: absolute; left: 0; right: 0; border-top: 1px dashed #999; z-index: 0; } /* Send lines to the background */
.grid-pinyin::before { top: 33.3%; } 
.grid-pinyin::after { top: 66.6%; } 
.grid-tianzige { position: relative; display: flex; justify-content: center; align-items: center; aspect-ratio: 1 / 1; box-sizing: border-box; }
.grid-tianzige.show-lines::before, .grid-tianzige.show-lines::after { content: ''; position: absolute; z-index: 0; }
.grid-tianzige.show-lines::before { left: 50%; top: 0; bottom: 0; border-left: 1px dashed #999; }
.grid-tianzige.show-lines::after { top: 50%; left: 0; right: 0; border-top: 1px dashed #999; }
.grid-hanzi { position: relative; z-index: 1; font-family: 'KaiTi', '楷体', serif; }

/* !! 5. 数学样式 (已修改) !! */
.math-container {
    display: block; /* 不再是 grid */
    padding-top: 5px;
}
.math-container pre {
    /* 关键：让它 100% 还原你的输入 */
    white-space: pre-wrap; 
    word-wrap: break-word;
    margin: 0; /* 覆盖 pre 标签的默认边距 */
    font-family: 'KaiTi', '楷体', serif; /* 默认字体 */
    line-height: 2.8; /* 留出答题行高 */
}

/* * =============================================
 * !! 5. 打印样式 (你12:24 AM的那个正确版本) !!
 * =============================================
*/
@media print {
    
    /* 修复1：“归零”大法，解决“0,0点”问题 */
    * {
        margin: 0 !important;
        padding: 0 !important;
        box-sizing: border-box !important;
    }

    html, body { 
        background: none !important; 
    }
    body { 
        display: block !important; 
    }
    
    .controls { display: none !important; }
    .mobile-fab-container { display: none !important; } 
    .page-container { display: block !important; overflow: visible !important; }
    
    /* 修复2：A4页面 *必须* 0 内边距，解决“左右不全” */
    .page-a4 {
        width: 100% !important; 
        height: auto !important; 
        min-height: 0 !important;
        overflow: visible !important; 
        box-shadow: none !important;
        border: none !important;
        padding: 0 !important; /* 必须是 0！*/
    }
    
    /* 修复3：压缩页头，解决“7行溢出”问题 */
    header.page-header {
        text-align: center !important;
        margin-bottom: 3mm !important; 
        padding: 15mm 15mm 0 15mm !important; /* 关键：把页面的内边距还给页头 */
    }
    header.page-header h1 {
        font-size: 16pt !important; 
        margin-bottom: 3mm !important; 
    }
    .info-fields {
        display: flex !important;
        justify-content: space-around !important;
        font-size: 10pt !important; 
    }
    
    /* 把 main 也加上内边距 */
    .page-a4 main {
        padding: 0 15mm 15mm 15mm !important;
    }
    
    * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
}

@page {
    size: A4;
    margin: 15mm; /* 打印机边距 */
}

/* 7. 移动端弹窗 */
.mobile-fab-container {
    display: none; 
    position: fixed; 
    /* 修复：底部安全区 */
    bottom: calc(20px + env(safe-area-inset-bottom));
    right: 20px;
    z-index: 999; 
    flex-direction: column; gap: 10px;
}
.mobile-fab {
    width: 56px; height: 56px; background-color: #007bff; color: white;
    border: none; border-radius: 50%; font-size: 24px; line-height: 56px;
    text-align: center; box-shadow: 0 4px 10px rgba(0,0,0,0.2); cursor: pointer;
}
#mobile-print-btn { background-color: #28a745; }
.modal-overlay {
    display: none; position: fixed; top: 0; left: 0;
    width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 998;
}

/* 移动端样式 (当屏幕小于 768px) */
@media (max-width: 768px) {
    body { display: block; height: auto; overflow: auto; }
    .controls {
        position: fixed; top: 0; left: -100%;
        width: 85%; max-width: 320px; height: 100%; /* <-- 修复：vh 换成 % */
        box-shadow: 0 0 15px rgba(0,0,0,0.2);
    }
    .controls.modal-open { left: 0; }
    .modal-overlay.modal-open { display: block; }
    #print-btn { display: none; } 
    .mobile-fab-container { display: flex; } 
    .page-container { padding: 10px; overflow-y: auto; overflow-x: auto; }
    .page-a4 {
        width: 210mm; height: 297mm;
        overflow: hidden; margin: 0;
        box-shadow: 0 0 10px rgba(0,0,0,0.1);
        padding: 15mm; 
    }
}

/* 8. 数学占位符样式 */
.math-circle-blank {
    display: inline-block;
    width: 1em;
    height: 1em;
    border: 1.5px solid black;
    border-radius: 50%;
    vertical-align: middle;
    margin: 0 0.1em;
    position: relative;
    top: -0.1em;
}