/* 
   CORE PRINT SYSTEM (統一列印核心)
   -----------------------------------------
   解決問題：
   1. 每次新增模板都要重寫 @media print
   2. 修改 A 模板不小心弄壞 B 模板
   3. A4 尺寸不精準
*/

/* 1. 定義標準 A4 紙張 (螢幕預覽用) */
.sheet {
    display: block;
    background: white;
    width: 210mm;
    height: 297mm;
    margin: 0 auto;
    margin-bottom: 2rem;
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
    position: relative;
    overflow: hidden;
    /* 防止內容爆版 */

    /* 讓內容層可以絕對定位 */
    position: relative;
}

/* 2. 內容安全區域 (Padding 統一管理) */
.sheet-content {
    width: 100%;
    height: 100%;
    padding: 15mm;
    /* 預設邊距，模板可覆寫 */
    box-sizing: border-box;
}

/* 3. 列印專用邏輯 (Print Only) */
@media print {

    /* 隱藏所有非紙張元素 */
    body * {
        visibility: hidden;
    }

    /* 只顯示紙張 */
    .sheet,
    .sheet * {
        visibility: visible;
    }

    /* 確保紙張位置正確 */
    .sheet {
        position: relative !important;
        left: auto !important;
        top: auto !important;
        margin: 0 !important;
        padding: 0 !important;
        box-shadow: none !important;
        width: 210mm !important;
        height: 297mm !important;
        page-break-after: auto;
        /* 由 wrapper 或兄弟節點規則決定換頁，避免尾頁多空白 */
        overflow: hidden !important;
    }

    /* 若每頁包在 .sheet-wrapper，改以 wrapper 控制換頁，才能正確處理最後一頁 */
    .sheet-wrapper {
        page-break-after: always !important;
        break-after: page !important;
        break-inside: avoid;
    }

    .sheet-wrapper:last-child {
        page-break-after: auto !important;
        break-after: auto !important;
    }

    /* 相容：沒有 wrapper 時，使用 sheet 兄弟節點換頁 */
    .sheet:not(:last-child) {
        page-break-after: always;
    }

    /* 重置 Body，防止 Flexbox 或是 Grid 影響列印 */
    body,
    html {
        background: white;
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        overflow: visible !important;
    }

    /* 隱藏導覽列、Sidebar等 (多重保險) */
    nav,
    header,
    aside,
    .zoom-controls,
    button {
        display: none !important;
    }
}