/* --- System Fonts & CSS Reset --- */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: #f4f6f8;
    color: #333;
    line-height: 1.4;
    padding: 20px;
}

.app-container {
    max-width: 1400px;
    margin: 0 auto;
}

header {
    margin-bottom: 24px;
}

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #1e293b;
}

.logo i {
    font-size: 2rem;
    color: #d97706; /* Industrial Orange Accent */
}

/* --- Layout Grid --- */
.main-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    align-items: start;
}

@media (max-width: 1150px) {
    .main-content {
        grid-template-columns: 1fr;
    }
}

/* --- Form Styles --- */
.form-section {
    background: #ffffff;
    padding: 24px;
    border-radius: 8px;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 20px;
}

.form-group.full-width {
    grid-column: span 2;
    margin-top: 10px;
    border-bottom: 1px solid #e2e8f0;
    padding-bottom: 5px;
}

.form-group label {
    display: block;
    font-size: 0.875rem;
    font-weight: 600;
    color: #475569;
    margin-bottom: 6px;
}

.form-group input {
    width: 100%;
    padding: 10px;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    font-size: 0.95rem;
    font-family: inherit;
}

.form-group input:focus {
    outline: none;
    border-color: #d97706;
    box-shadow: 0 0 0 3px rgba(217, 119, 6, 0.1);
}

.items-form-section {
    margin-top: 24px;
}

table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 15px;
}

th, td {
    padding: 10px;
    text-align: left;
    border-bottom: 1px solid #e2e8f0;
}

th {
    background-color: #f8fafc;
    font-weight: 600;
    color: #64748b;
}

.table-responsive {
    overflow-x: auto;
}

.item-desc, .item-qty, .item-price {
    width: 100%;
    padding: 6px;
    border: 1px solid #cbd5e1;
    border-radius: 4px;
    font-family: inherit;
    font-size: 0.9rem;
}

/* --- Buttons --- */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    border: none;
    font-family: inherit;
    transition: background 0.2s;
}

.btn-primary {
    background-color: #d97706;
    color: white;
    width: 100%;
    justify-content: center;
    font-size: 1rem;
    margin-top: 20px;
}

.btn-primary:hover {
    background-color: #b45309;
}

.btn-secondary {
    background-color: #f1f5f9;
    color: #334155;
}

/* --- Preview / Layout Container --- */
.preview-section {
    display: flex;
    flex-direction: column;
}

.single-sheet-layout {
    display: flex;
    flex-direction: column;
    background: #ffffff;
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    border: 1px solid #e2e8f0;
    gap: 15px;
}

.receipt-card {
    background: #ffffff;
    padding: 24px;
    border: 1px dashed #cbd5e1;
    border-radius: 6px;
}

/* --- Safe Layout-Driven Header Block --- */
.receipt-header {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 20px;
    margin-top: 5px;
    margin-bottom: 15px;
    align-items: start;
}

.store-info-block {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.store-name {
    font-size: 1.25rem;
    color: #1e293b;
    font-weight: 700;
    max-width: 450px;
    word-wrap: break-word;
    line-height: 1.2;
}

.store-email {
    font-size: 0.85rem;
    color: #64748b;
}

.receipt-title-block {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 5px;
    text-align: right;
}

.receipt-title-block h2 {
    color: #1e293b;
    font-size: 1.3rem;
    font-weight: 800;
    letter-spacing: 0.02em;
    line-height: 1.1;
}

.receipt-title-block p {
    font-size: 0.85rem;
    color: #334155;
}

/* Inline badges replacing absolute layers */
.copy-indicator {
    display: inline-block;
    font-size: 0.7rem;
    font-weight: 800;
    padding: 4px 10px;
    border-radius: 4px;
    letter-spacing: 0.05em;
    width: fit-content;
    white-space: nowrap;
    margin-bottom: 2px;
}

.text-customer { background: #fee2e2; color: #991b1b; }
.text-hardware { background: #e0f2fe; color: #075985; }

hr {
    border: 0;
    border-top: 1px solid #e2e8f0;
    margin-bottom: 15px;
}

.receipt-details {
    margin-bottom: 15px;
    font-size: 0.9rem;
}

.receipt-details h4 {
    color: #64748b;
    text-transform: uppercase;
    font-size: 0.75rem;
    margin-bottom: 3px;
}

.receipt-table {
    margin-top: 10px;
    font-size: 0.85rem;
}

.receipt-table th, .receipt-table td {
    padding: 6px 8px;
}

.text-right {
    text-align: right;
}

.receipt-total-container {
    display: flex;
    justify-content: flex-end;
    margin-top: 10px;
}

.total-table {
    width: 220px;
    font-size: 0.9rem;
}

.total-table td {
    padding: 4px 0;
    border: none;
}

.receipt-footer {
    margin-top: 35px;
    display: flex;
    justify-content: space-between;
}

.signature-box {
    width: 42%;
    text-align: center;
}

.signature-box .line {
    border-top: 1px solid #94a3b8;
    margin-bottom: 6px;
}

.signature-box p {
    font-size: 0.75rem;
    color: #64748b;
}

.paper-cut-line {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    color: #94a3b8;
    font-size: 0.8rem;
    font-style: italic;
    margin: 10px 0;
}

.paper-cut-line::before, .paper-cut-line::after {
    content: "";
    flex: 1;
    border-bottom: 1px dashed #cbd5e1;
}

/* Screen Responsiveness for App Previews */
@media (max-width: 650px) {
    .receipt-header {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    .receipt-title-block {
        align-items: flex-start;
        text-align: left;
    }
}

/* --- CRITICAL PRINT ENGINE AUTO-CALIBRATION --- */
@page {
    size: A4 portrait;
    margin: 5mm; 
}

@media print {
    body {
        background-color: #fff;
        padding: 0;
        margin: 0;
        font-size: 11px;
    }

    .no-print {
        display: none !important;
    }

    .main-content, .preview-section {
        display: block;
    }

    .single-sheet-layout {
        border: none;
        box-shadow: none;
        padding: 0;
        gap: 0;
    }

    .receipt-header {
        display: grid !important;
        grid-template-columns: 1fr auto !important;
        gap: 20px !important;
    }

    .receipt-title-block {
        align-items: flex-end !important;
        text-align: right !important;
    }

    /* --- CONDITION A: 10 ITEMS OR LESS (STRICT SINGLE SHEET FIT) --- */
    .single-sheet-layout.compact-view {
        display: flex !important;
        flex-direction: column !important;
        height: 282mm; 
        justify-content: space-between;
        page-break-inside: avoid !important;
    }

    .single-sheet-layout.compact-view .receipt-card {
        border: 1px solid #cbd5e1;
        border-radius: 0;
        padding: 6mm 6mm !important; 
        font-size: 9.5px !important;   
        line-height: 1.2 !important;
        margin: 0 !important;
        page-break-inside: avoid !important;
    }

    .single-sheet-layout.compact-view .store-name { font-size: 1.05rem; max-width: 380px; }
    .single-sheet-layout.compact-view .receipt-title-block h2 { font-size: 1.1rem; }
    .single-sheet-layout.compact-view .receipt-header { margin-bottom: 4px; margin-top: 0; }
    .single-sheet-layout.compact-view hr { margin-bottom: 8px; }
    .single-sheet-layout.compact-view .receipt-details { margin-bottom: 6px; }
    .single-sheet-layout.compact-view .receipt-table { font-size: 9px !important; margin-top: 4px; }
    .single-sheet-layout.compact-view .receipt-table th, 
    .single-sheet-layout.compact-view .receipt-table td { padding: 3px 5px !important; }
    .single-sheet-layout.compact-view .receipt-total-container { margin-top: 4px; }
    .single-sheet-layout.compact-view .receipt-footer { margin-top: 15px; }
    
    .single-sheet-layout.compact-view .paper-cut-line {
        display: flex !important; 
        margin: 4mm 0 !important;
        page-break-inside: avoid !important;
    }

    /* --- CONDITION B: MORE THAN 10 ITEMS (CONTINUOUS GROW MODE) --- */
    .single-sheet-layout.continuous-view {
        display: block !important;
    }

    .single-sheet-layout.continuous-view .receipt-card {
        border: 1px solid #cbd5e1;
        border-radius: 0;
        padding: 12mm 8mm;
        page-break-inside: auto !important; 
        font-size: 12px;
    }
    
    /* Dynamically hide cut guides and push second copy block down onto a fresh page */
    .single-sheet-layout.continuous-view .paper-cut-line {
        display: none !important; 
    }

    .single-sheet-layout.continuous-view .receipt-card:last-child {
        page-break-before: always; 
    }

    .receipt-table tr {
        page-break-inside: avoid !important;
    }
    
    .receipt-table thead {
        display: table-header-group !important;
    }
}