/*reset & layout*/
*{ box-sizing: border-box; }
body { margin:0; font-family:ui-sans-serif,system-ui; background:#f7f7f8; color:#1f2937; }

/* App Grid Layout */
.app { 
   display:grid; 
   grid-template-columns:320px 1fr; 
   grid-template-rows: 100vh;       
   height:100vh; 
   overflow: hidden;                
}

aside { 
   grid-column: 1;
   padding:16px; 
   background:#fff; 
   border-right:1px solid #ddd; 
   overflow-y:auto; 
}

main { 
   grid-column: 2;
   grid-row: 1;
   padding:16px; 
   overflow-y:auto; 
   position:relative; 
}

h1 { margin:0 0 12px; font-size:18px; font-weight:800; }
.field { margin-bottom:12px; display:flex; flex-direction:column; }
.field label { margin-bottom:4px; font-size:14px; color:#374151; }
input, textarea, select {
    padding:8px; border:1px solid #ddd; border-radius:6px; font-size:14px;
}
textarea { resize:vertical; }
.btn {
    display:inline-block; padding:8px 12px; background:#0d9488; color:#fff;
    border:none; border-radius:6px; cursor:pointer; font-weight:600;
    margin-right:8px;
}
.btn.secondary { background:#374151; }
.btn.danger { background:#ef4444; }
.btn.block { display: block; width: 100%; margin-right: 0; text-align: center; }
.btn:disabled { background: #e5e7eb; color: #9ca3af; border-color: #d1d5db; cursor: not-allowed; }

.pages { display:flex; flex-wrap:wrap; gap:16px; }
.page {
    width:297mm;
    height:auto;
    background:#fff;
    box-shadow:0 0 4px rgba(0,0,0,0.1); padding:12mm;
    display:flex; flex-direction:column; gap:8px;
}
.blocks {
    flex: auto;
    display:grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
    overflow:visible;
}
.block {
    border:1px dashed #ccc; border-radius:6px; padding:8px;
    background:#fafafa;
}
.block.draggable { cursor:pointer; }
.block.dragging { opacity:.5; }
.product-card {
    display: flex;
    flex-direction: column;
}
.product-card .product-header {
    text-align: center;
    margin-bottom: 2px;
    font-size: 1.1em;
}
.product-card .product-header strong {
    font-size: 90%;
}

/* 图片样式 */
.product-card img {
    width:100%; height:auto; border-radius:4px; margin:0;
    object-fit: cover;
    aspect-ratio: 1 / 1;
}

.product-params {
    margin-top: 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.param {
    display: flex;
    justify-content: space-between;
    font-size:12px;
    color:#374151;
    line-height:1.4;
}
.param-label {
    font-weight: 500;
}
.param-value {
    text-align: right;
}
.sku-small {
    font-size:10px;
    color:#666;
}
.footer, .header {
    font-size:13px;
    color:#666;
}
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 8px;
    border-bottom: 1px solid #ccc;
    margin-bottom: 8px;
}
.header-left img {
    height: 40px;
    width: auto;
}
.header-right {
    text-align: right;
}
.footer {
    margin-top:auto;
    text-align: center;
    padding-top: 8px;
    border-top: 1px solid #ccc;
    margin-top: 8px;
}
.product-card-divider {
    border-top: 1px solid #eee;
    margin: 8px 0;
}
.product-price {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    font-weight: 600;
    margin-top: 4px;
}

/* Modal Styles */
.modal {
    position:fixed; top:0; left:0; width:100%; height:100%;
    background:rgba(0,0,0,0.5); display:flex; align-items:center;
    justify-content:center; padding:16px; z-index:300;
}
.modal.hidden { display:none; }
.modal-content {
    background:#fff; border-radius:6px; padding:20px; width:100%;
    max-width:400px; max-height:90vh; overflow:auto;
    display: flex; flex-direction: column;
}
.modal-content h2 { margin-top:0; }
.modal-content .field { margin-bottom:8px; }
.modal-content .actions { text-align:right; margin-top:12px; }
.modal-content .actions .btn.danger { margin-right: auto; }

/* --- Library Modal (恢复为简单网格) --- */
#libraryModal .modal-content {
    max-width: 800px;
}
#libraryModal .library-products {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 16px;
    padding: 16px;
    border: 1px solid #ddd;
    border-radius: 6px;
    max-height: 60vh;
    overflow-y: auto;
}
#libraryModal .library-item {
    border: 1px solid #ccc;
    border-radius: 6px;
    padding: 8px;
    background: #fcfcfc;
    display: flex;
    flex-direction: column;
    text-align: center;
}
#libraryModal .library-item img {
    width: 100%;
    height: 120px;
    border-radius: 4px;
    object-fit: cover;
    aspect-ratio: 1 / 1;
    background: #eee;
    margin-bottom: 8px;
}
#libraryModal .library-item .item-title {
    font-weight: bold;
    margin: 8px 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
#libraryModal .library-item .btn {
    margin-top: auto;
    width: 100%;
    font-size: 14px;
}
#libraryModal .library-item .actions {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}

/* --- Toast --- */
#toast-container {
    position: fixed; top: 20px; left: 50%; transform: translateX(-50%);
    z-index: 9999; display: flex; flex-direction: column; gap: 10px; pointer-events: none;
}
.toast {
    background: rgba(31, 41, 55, 0.95); color: #fff; padding: 10px 20px;
    border-radius: 8px; font-size: 14px; font-weight: 500;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    opacity: 0; transform: translateY(-20px); transition: all 0.3s ease;
    backdrop-filter: blur(4px); display: flex; align-items: center; gap: 8px;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.success { border-left: 4px solid #10b981; }
.toast.error { border-left: 4px solid #ef4444; }
.toast.info { border-left: 4px solid #3b82f6; }

.disabled-overlay {
   position: absolute; top: 0; left: 0; width: 100%; height: 100%;
   background-color: rgba(255, 255, 255, 0.7); z-index: 5; display: none; cursor: not-allowed;
}

#privacyShield {
   grid-column: 2; grid-row: 1; width: 100%; height: 100%; z-index: 200;
   background: rgba(255, 255, 255, 0.98); backdrop-filter: blur(5px);
   display: flex; flex-direction: column; align-items: center; justify-content: center;
   text-align: center; color: #374151; transition: opacity 0.3s ease, visibility 0.3s ease;
}
#privacyShield.hidden { opacity: 0; visibility: hidden; pointer-events: none; }
#privacyShield h3 { font-size: 20px; margin-bottom: 12px; color: #111; }
#privacyShield p { margin-bottom: 24px; color: #666; }
#privacyShield .lock-icon { font-size: 48px; margin-bottom: 16px; }

#pasteModal .modal-content { max-width: 500px; }
#pasteModal .paste-area { width: 100%; height: 150px; margin-bottom: 12px; }
#pasteModal .preview-area .field { margin-bottom: 4px; align-items: center; }
#pasteModal .preview-area .field label { width: 100px; }

#statusMessage {
    position: fixed; top: 16px; left: 50%; transform: translateX(-50%); z-index: 100;
    text-align: center; padding: 10px 20px; background: #fff; border: 1px solid #ddd;
    border-radius: 6px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    font-size: 14px; font-weight: 500; color: #374151;
}
#statusMessage.hidden { display: none; }

@media print {
    /* 1. 全局隐藏：隐藏无关元素 */
    .no-print, 
    .footer, 
    #privacyShield, 
    aside, 
    #sidebar-top-controls, 
    #sidebar-bottom-controls { 
        display: none !important; 
    }

    /* 2. 页面纸张设置 */
    @page {
        size: A4 landscape; /* 横向 */
        margin: 10mm;       /* 页边距 */
    }

    /* 3. 解除容器限制 (核心修复点) */
    /* 必须设置为 visible 和 auto，否则浏览器会认为内容只有一页高 */
    html, body, .app, main, .pages { 
        margin: 0 !important; 
        padding: 0 !important; 
        width: 100% !important; 
        height: auto !important; /* 关键：允许高度自动增长 */
        overflow: visible !important; /* 关键：允许内容溢出到下一页 */
        display: block !important;
        background: #fff !important; 
    }

    /* 4. 单页容器设置 */
    .page { 
        /* 强制每个 .page 元素视为一个独立的打印页 */
        page-break-after: always !important; 
        break-after: page !important;
        page-break-inside: avoid !important;
        
        /* 布局设置 */
        width: 100% !important;
        /* 这里设为固定高度是为了保证 header/grid 排版稳定，
           但因为父级 overflow: visible，它不会阻止下一页的渲染 */
        height: 190mm !important; /* A4横向高度约210mm，预留一点余量防止被挤下去 */
        
        display: flex !important;
        flex-direction: column; 
        
        margin: 0 !important;
        padding: 0 !important;
        box-shadow: none !important;
        border: none !important;
    }

    /* 最后一页不需要强制分页，避免多出一个空白页 */
    .page:last-child {
        page-break-after: auto !important;
        break-after: auto !important;
    }
    
    .page:empty { display: none; }

    /* 5. 页眉调整 */
    .header {
        flex-shrink: 0; 
        margin-bottom: 10px !important;
        padding-bottom: 5px !important;
        border-bottom: 2px solid #eee !important;
    }
    .header img {
        height: 40px !important;
    }

    /* 6. 产品网格区域 */
    .blocks { 
        flex-grow: 1; 
        display: grid !important; 
        grid-template-columns: repeat(3, 1fr) !important; 
        gap: 15px; 
        align-content: start; 
        overflow: visible !important; /* 确保网格内也不截断 */
    }

    /* 7. 产品卡片样式 */
    .block { 
        page-break-inside: avoid; 
        break-inside: avoid;
        border: 1px solid #ddd !important; 
        padding: 10px !important; 
        background: #fff;
        display: flex;
        flex-direction: column;
    }
    
    .product-card img, #libraryModal .library-item img { 
        width: 100% !important;
        aspect-ratio: 1 / 1 !important; 
        object-fit: cover !important;
        max-height: none !important; 
        margin-bottom: 8px !important;
    }

    /* 字体微调 */
    .product-header { font-size: 13px !important; margin-bottom: 4px; }
    .product-params { gap: 1px; }
    .param { font-size: 11px !important; line-height: 1.2; } 
    .product-price { 
        margin-top: auto; 
        padding-top: 6px;
        font-size: 14px !important; 
        border-top: 1px dashed #eee;
    }
}