:root {
    --primary-color: #2563eb;
    --primary-dark: #1e40af;
    --bg-color: #f3f4f6;
    --sidebar-bg: #ffffff;
    --text-color: #111827;
    --text-muted: #6b7280;
    --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);
    --font-family: 'Noto Sans KR', sans-serif;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-family);
    background-color: var(--bg-color);
    color: var(--text-color);
    height: 100vh;
    overflow: hidden;
    /* App-like feel */
}

/* Layout */
.app-container {
    display: flex;
    height: 100%;
}

/* Sidebar */
.sidebar {
    width: 360px;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border-color);
    padding: 1.5rem;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    z-index: 10;
    box-shadow: 4px 0 10px rgba(0, 0, 0, 0.02);
}

.sidebar-header h1 {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-color);
}

.sidebar-header p {
    font-size: 0.875rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

.form-section {
    border-top: 1px solid var(--border-color);
    padding-top: 1rem;
}

.form-section h3 {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-color);
    margin-bottom: 0.75rem;
}

.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: 0.375rem;
    font-size: 0.875rem;
    font-family: inherit;
    transition: border-color 0.2s;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
}

/* Buttons */
.btn-primary {
    display: block;
    width: 100%;
    padding: 0.75rem;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 0.375rem;
    font-weight: 600;
    font-size: 0.95rem;
    cursor: pointer;
    transition: background-color 0.2s;
    margin-top: auto;
    /* Push to bottom */
}

.btn-primary:hover {
    background-color: var(--primary-dark);
}

.btn-secondary {
    display: block;
    width: 100%;
    padding: 0.5rem;
    background-color: #fff;
    border: 1px solid var(--border-color);
    color: var(--text-color);
    border-radius: 0.375rem;
    font-size: 0.875rem;
    cursor: pointer;
    transition: background-color 0.2s;
}

.btn-secondary:hover {
    background-color: #f9fafb;
    border-color: #d1d5db;
}

/* Preview Area */
.preview-area {
    flex: 1;
    background-color: #525659;
    /* Acrobat Reader grey */
    padding: 2rem;
    padding-top: 1rem;
    /* Reduced from 2rem */
    overflow-y: auto;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    /* Ensure items start at top */
}

/* Paper (A4 size) */
.paper {
    background: white;
    width: 210mm;
    min-width: 210mm;
    /* Prevent crushing on mobile */
    height: 297mm;
    /* Strict A4 height */
    overflow: hidden;
    /* visual feedback if content overflows */
    padding: 20mm;
    padding-top: 10mm;
    flex-shrink: 0;
    /* Important: Don't let flex container squeeze it */
    /* Lift content by 10mm */
    box-shadow: var(--paper-shadow);
    position: relative;
    box-sizing: border-box;
    /* Includes padding in width */
    display: flex;
    flex-direction: column;
}

/* Invoice Design */
.invoice-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    border-bottom: 2px solid var(--text-color);
    padding-bottom: 1rem;
    margin-bottom: 2rem;
}

.invoice-header h1 {
    font-size: 2.5rem;
    font-weight: 800;
    letter-spacing: -1px;
}

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

.meta-row {
    margin-bottom: 0.25rem;
}

.meta-row .label {
    font-weight: 600;
    margin-right: 0.5rem;
}

.parties-container {
    display: flex;
    gap: 2rem;
    margin-bottom: 1rem;
}

.party-box {
    flex: 1;
}

.party-box h3 {
    font-size: 0.9rem;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.5rem;
    margin-bottom: 0.5rem;
}

.party-box table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
}

.party-box th,
.party-box td {
    padding: 0.3rem 0;
    text-align: left;
}

.party-box th {
    width: 60px;
    color: var(--text-muted);
    font-weight: 500;
}

.summary-bar {
    background-color: var(--bg-color);
    padding: 1rem;
    border-radius: 0.5rem;
    margin-bottom: 0.5rem;
    text-align: right;
}

.amount-group .label {
    font-size: 1rem;
    font-weight: 600;
    margin-right: 1rem;
}

.amount-group .value {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary-color);
}

/* Items Table */
.items-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 2rem;
    font-size: 0.9rem;
}

.items-table th {
    background-color: var(--bg-color);
    padding: 0.75rem;
    border-top: 2px solid var(--text-color);
    border-bottom: 1px solid var(--border-color);
    font-weight: 600;
    text-align: center;
}

.items-table td {
    padding: 0.75rem;
    border-bottom: 1px solid var(--border-color);
}

.items-table td:nth-child(1) {
    text-align: center;
}

/* No */
.items-table td:nth-child(4),
/* Qty */
.items-table td:nth-child(5),
/* Price */
.items-table td:nth-child(6) {
    text-align: right;
}

/* Amount */

.total-row td,
.vat-row td {
    text-align: right;
    font-weight: 600;
    border: none;
    padding-top: 1rem;
}

.grand-total-row td {
    text-align: right;
    font-weight: 800;
    font-size: 1.1rem;
    color: var(--primary-color);
    border-top: 2px solid var(--text-color);
    padding-top: 1rem;
}

.invoice-footer {
    text-align: center;
    margin-top: 4rem;
    color: var(--text-muted);
    font-size: 0.9rem;
}

/* Print Overrides */
@media print {
    body {
        background: white;
        height: auto;
        overflow: visible;
    }

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

    .preview-area {
        background: white;
        padding: 0;
        display: block;
    }

    .paper {
        box-shadow: none;
        margin: 0;
        padding: 0;
        width: 100%;
        min-height: auto;
        /* Allow auto height to prevent forcing extra pages if content is short */
        height: auto;
    }

    /* Hide interactive cues during print/PDF */
    #dispProviderCeo,
    #dispDate {
        cursor: default !important;
        box-shadow: none !important;
        background-color: transparent !important;
        transition: none !important;
    }
}

/* Interactive Stamp Area */
#dispProviderCeo {
    cursor: pointer;
    transition: background-color 0.2s;
}

/* Mobile Responsiveness */
@media (max-width: 768px) {
    body {
        height: auto;
        overflow: auto;
    }

    .app-container {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        height: auto;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
        padding: 1rem;
        box-shadow: none;
    }

    .preview-area {
        padding: 1rem;
        background-color: #525659;
        /* Fix: Start from left so scaling from 'top left' works correctly */
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: flex-start;
        /* IMPORTANT: Align left to prevent cutoff */
        /* Let it grow to fit content if scaled */
        overflow: hidden;
    }

    /* Scale the A4 paper to fit screen width */
    .paper {
        transform-origin: top center;
        /* Calculate scale: (Viewport width - padding) / Paper Width */
        /* But CSS calc can't access viewport in scale easily. 
           We'll use a container query or a simple hack. 
           Mobile usually < 400px. A4 ~ 794px. Scale ~ 0.5 or less.
           Using container-type on preview-area or just media queries with fixed scales.
        */
    }
}

/* Print Optimization */
@media print {
    @page {
        margin: 0.5cm;
    }

    body,
    .app-container {
        height: auto;
        overflow: visible;
        background: white;
    }

    .sidebar,
    .download-btn-container {
        display: none !important;
    }

    .preview-area {
        display: block;
        padding: 0;
        background: white;
        overflow: visible;
    }

    .paper {
        margin: 0 !important;
        box-shadow: none !important;
        page-break-after: always;
        page-break-inside: avoid;
        border: none;
        width: 100%;
        /* Ensure fits within print margins */
    }

    .items-table tr {
        page-break-inside: avoid;
    }

    /* Force page breaks */
    .page-break {
        page-break-before: always;
    }

    .page-break-start-here {
        page-break-after: always;
    }

    .item-row,
    .items-table tr {
        page-break-inside: avoid;
    }
}

/* =========================================
   PDF Export Specific Styles (Clean V2)
   ========================================= */
.pdf-export-mode {
    /* Container Reset */
    position: absolute;
    top: 0;
    left: 0;
    width: 210mm !important;
    /* A4 Width */
    background: white;
    margin: 0;
    padding: 0;
    box-shadow: none;
    z-index: -9999;
    /* Hide from view */
    height: fit-content !important;
    /* No extra whitespace */
}

.pdf-export-mode .paper {
    /* Paper Reset */
    width: 100% !important;
    height: auto !important;
    min-height: auto !important;
    box-shadow: none !important;
    margin: 0 !important;
    padding: 20px !important;
    /* Internal padding */
    border: none !important;
    page-break-after: always;
}

.pdf-export-mode .party-info-container {
    /* Layout Safety: Force block to avoid 2-col clipping */
    display: block !important;
}

.pdf-export-mode .party-box {
    width: 100% !important;
    margin-bottom: 20px;
}

.pdf-export-mode .item-row {
    page-break-inside: avoid;
}

.pdf-export-mode .subtotal-footer {
    page-break-inside: avoid;
}