:root {
    --primary: #FF6600;
    /* สีหลัก (จะถูกเปลี่ยนตามประเภทเอกสาร) */
    --primary-dark: #E65100;
    --text-dark: #333333;
    --text-gray: #666666;
    --border: #dddddd;
    --bg-light: #f9f9f9;
}

body {
    font-family: 'Sarabun', sans-serif;
    background-color: #f0f2f5;
    color: var(--text-dark);
    line-height: 1.6;
    margin: 0;
}

.container {
    max-width: 1000px;
    margin: 20px auto;
    padding: 0 15px;
}

/* Header */
.header {
display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    /* [เพิ่ม] คำสั่งล็อก Header ไว้ด้านบน */
    position: sticky;
    top: 0;             /* สั่งให้ติดขอบบนสุด */
    z-index: 1000;      /* สั่งให้ลอยอยู่ชั้นบนสุด (ทับเนื้อหาอื่น) */
    
    /* [เพิ่ม] พื้นหลังและระยะห่าง */
    background-color: #f0f2f5; /* สีเดียวกับพื้นหลังเว็บ (เพื่อบังเนื้อหาที่เลื่อนผ่าน) */
    padding-top: 20px;  /* เพิ่มขอบบนหน่อยเวลาติดขอบจะได้ไม่ชิดเกินไป */
    padding-bottom: 15px; /* เพิ่มขอบล่างให้ดูไม่อึดอัด */
    border-bottom: 1px solid rgba(0,0,0,0.05); /* เพิ่มเส้นขอบจางๆ ด้านล่างให้ดูมีมิติ */
}

.header-logo {
    height: 45px;
    width: auto;
    object-fit: contain;
}

/* Doc Selector */
.doc-selector {
    height: 40px;
    padding-left: 10px;
    padding-right: 30px;
    font-family: 'Sarabun';
    font-weight: bold;
    cursor: pointer;
    border: 2px solid var(--primary);
    color: var(--primary);
    background: white;
    border-radius: 4px;
    appearance: none;
    -webkit-appearance: none;
    transition: 0.3s;
}

.doc-selector:focus {
    outline: none;
    box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.1);
}

.doc-selector-arrow {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--primary);
    pointer-events: none;
}

/* Main Content */
.main-content {
    background: white;
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

section {
    margin-bottom: 30px;
    padding-bottom: 20px;
    border-bottom: 1px dashed var(--border);
}

section h2 {
    font-size: 1.1rem;
    color: var(--text-dark);
    margin-bottom: 15px;
    display: flex;
    align-items: center;
    gap: 8px;
}

section h2 i {
    color: var(--primary);
}

/* Forms & Grid */
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 15px;
}

.grid-2-col {
    display: grid;
    grid-template-columns: 220px 1fr;
    gap: 30px;
}

.span-2 {
    grid-column: span 2;
}

.form-group {
    display: flex;
    flex-direction: column;
}

.form-group label {
    font-size: 0.85rem;
    color: var(--text-gray);
    margin-bottom: 5px;
}

input,
textarea,
select {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-family: inherit;
    font-size: 0.95rem;
    width: 100%;
    box-sizing: border-box;
}

input:focus,
textarea:focus {
    outline: none;
    border-color: var(--primary);
}

/* Upload Boxes */
.images-column {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.logo-container {
    width: 100%;
    height: 100px;
    border: 2px dashed var(--border);
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    background: var(--bg-light);
    overflow: hidden;
}

.logo-container img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.logo-placeholder {
    text-align: center;
    color: #999;
    font-size: 0.8rem;
}

.info-box {
    margin-top: 10px;
    background-color: #fff8e1;
    border: 1px solid #ffe0b2;
    padding: 15px;
    border-radius: 6px;
    font-size: 0.85rem;
    color: #5d4037;
    line-height: 1.6;
}

.info-box h4 {
    margin: 0 0 10px 0;
    color: #f57c00;
    font-size: 0.95rem;
}

.info-box ul {
    margin: 0;
    padding-left: 20px;
}

.info-box li {
    margin-bottom: 5px;
}

/* Approver Box */
.approver-box {
    background: #f9f9f9;
    padding: 15px;
    border-radius: 6px;
    border: 1px dashed #ddd;
    margin-top: 5px;
}

/* Items Table */
.items-table-wrapper {
    overflow-x: auto;
    margin-bottom: 20px;
}

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 600px;
}

th {
/* ลบ background: #e9ecef; ออก */
    /* ลบ color: #333333 !important; ออก */
    text-align: left;
    padding: 10px;
    font-size: 0.9rem;
    font-weight: 700;
    border-bottom: 2px solid #ccc;
}

td {
    padding: 8px 10px;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}

/* Summary */
.summary-section {
    display: flex;
    gap: 30px;
    margin-top: 20px;
}

.remarks-box {
    flex: 1;
}

.totals-box {
    width: 300px;
}

.summary-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 0.95rem;
}

.summary-row.total {
    font-weight: bold;
    color: var(--primary);
    font-size: 1.2rem;
    border-top: 2px solid var(--primary);
    padding-top: 10px;
    margin-top: 10px;
}

/* Payment & PromptPay */
.promptpay-box {
    background-color: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 8px;
    padding: 15px;
}

.qr-display {
    width: 130px;
    flex-shrink: 0;
    text-align: center;
    border-left: 1px solid #ddd;
    padding-left: 30px;
}

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

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

.btn-primary:hover {
    opacity: 0.9;
}

.btn-success {
    background: #28a745;
    color: white;
}

.btn-secondary {
    background: #6c757d;
    color: white;
}

.btn-outline {
    border: 1px solid var(--primary);
    color: var(--primary);
    background: white;
}

.btn-outline:hover {
    background: var(--bg-light);
}

.btn-text-danger {
    background: none;
    border: none;
    color: #dc3545;
    cursor: pointer;
    font-size: 0.85rem;
    margin-top: 5px;
}

.circle-btn {
    width: 40px;
    height: 40px;
    padding: 0;
    justify-content: center;
    border-radius: 50%;
}

/* =========================================
   [ปรับปรุงใหม่] Action Bar & Buttons (Pro Style)
   ========================================= */

/* กรอบรวมปุ่ม (Toolbar) */
.actions-bar {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 15px;                  /* ระยะห่างระหว่างปุ่ม */
    margin-top: 30px;
    padding: 20px;              /* พื้นที่ภายในกรอบ */
    background: #ffffff;        /* พื้นหลังสีขาว */
    border-radius: 12px;        /* มุมโค้งมน */
    border: 1px solid #eaeaea;  /* เส้นขอบบางๆ */
    box-shadow: 0 4px 20px rgba(0,0,0,0.03); /* เงาฟุ้งๆ ให้ดูลอยมีมิติ */
    flex-wrap: wrap;            /* ให้ปัดลงบรรทัดใหม่ได้ถ้าหน้าจอเล็ก */
}

/* ปรับแต่งปุ่มกดทั่วไป */
.btn {
    border: none;
    border-radius: 8px;         /* มุมโค้งมนกว่าเดิม (เดิม 4px) */
    cursor: pointer;
    font-weight: 600;
    font-family: 'Sarabun', sans-serif;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); /* Animation นุ่มๆ */
    position: relative;
    overflow: hidden;
    white-space: nowrap;        /* [สำคัญ] ห้ามตัดบรรทัดเด็ดขาด! */
}

/* ปุ่มขนาดใหญ่ใน Actions Bar */
.actions-bar .btn.large {
    padding: 0 24px;            /* ระยะห่างซ้ายขวา */
    height: 48px;               /* [สำคัญ] บังคับความสูงเท่ากันทุกปุ่ม */
    font-size: 0.95rem;         /* ขนาดตัวอักษรกำลังดี */
    min-width: 140px;           /* ความกว้างขั้นต่ำ (ปุ่มจะไม่ลีบ) */
    box-shadow: 0 2px 4px rgba(0,0,0,0.05); /* เงาปุ่มเล็กน้อย */
}

/* Effect ตอนเอาเมาส์ชี้ (Hover) */
.actions-bar .btn.large:hover {
    transform: translateY(-2px); /* ลอยขึ้นนิดนึง */
    box-shadow: 0 6px 12px rgba(0,0,0,0.1); /* เงาเข้มขึ้น */
}

/* Effect ตอนกด (Active) */
.actions-bar .btn.large:active {
    transform: translateY(0);
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

/* --- สีของปุ่มแต่ละประเภท (ปรับให้ดูแพงขึ้น) --- */

/* 1. ปุ่มเปิดไฟล์ (สีเทา) */
.btn-secondary { 
    background-color: #f1f3f5; 
    color: #495057; 
    border: 1px solid #dee2e6;
}
.btn-secondary:hover { background-color: #e9ecef; color: #212529; }

/* 2. ปุ่มบันทึก (สีส้มแบบ Outline) */
.btn-outline { 
    background-color: white; 
    border: 1px solid var(--primary); 
    color: var(--primary); 
}
.btn-outline:hover { 
    background-color: var(--primary); 
    color: white; 
}

/* 3. ปุ่มดูตัวอย่าง (สีส้มหลัก) */
.btn-primary { 
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%); 
    color: white; 
    box-shadow: 0 4px 10px rgba(255, 102, 0, 0.2); /* เงาสีส้ม */
}
.btn-primary:hover { opacity: 0.95; }

/* 4. ปุ่ม PDF (สีแดง) */
#pdfBtn {
    background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
    color: white;
    border: none;
    box-shadow: 0 4px 10px rgba(231, 76, 60, 0.2); /* เงาสีแดง */
}

/* 5. ปุ่มพิมพ์ (สีเขียว) */
.btn-success { 
    background: linear-gradient(135deg, #2ecc71 0%, #27ae60 100%); 
    color: white; 
    box-shadow: 0 4px 10px rgba(46, 204, 113, 0.2); /* เงาสีเขียว */
}

/* จัดไอคอนในปุ่มให้สวยงาม */
.btn i {
    font-size: 1.1em;
}

/* Modal & Preview */
.modal {
    display: none;
    position: fixed;
    
    /* [แก้ไข] เปลี่ยนจาก 100 เป็น 2000 (เพื่อให้ลอยทับ Header หลักที่ set ไว้ 1000) */
    z-index: 2000;
    
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(2px);
}

.modal-content {
    background-color: #ffffff; /* [แก้] เปลี่ยนพื้นหลังเป็นสีขาว สบายตา */
    margin: 40px auto;         /* จัดระยะห่างจากขอบบน */
    padding: 30px;
    width: 90%;
    max-width: 900px;
    height: 85vh;              /* ความสูง */
    overflow-y: auto;          /* ให้เลื่อน Scroll ได้ */
    position: relative;
    border-radius: 12px;       /* มุมโค้งมน */
    box-shadow: 0 10px 30px rgba(0,0,0,0.3); /* เงานุ่มๆ ให้ดูลอยขึ้น */
    color: #333;               /* ตัวหนังสือสีดำ */
}

/* ปุ่มกากบาท (Close) */
.close {
    color: #aaaaaa;            /* สีเทาอ่อน */
    font-size: 32px;           /* ใหญ่ขึ้นนิดนึง */
    font-weight: bold;
    line-height: 1;
    
    /* [สำคัญ] บังคับลบพื้นหลังทุกกรณี */
    background: transparent !important; 
    border: none;
    
    transition: all 0.2s;
    cursor: pointer;
    
    /* จัดตำแหน่งให้อยู่สวยๆ */
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 50%; /* เวลา Hover จะเป็นวงกลม */
}

/* Effect ตอนเอาเมาส์ชี้ */
.close:hover,
.close:focus {
    color: #ff4d4d;             /* เปลี่ยนเป็นสีแดง */
    background-color: #ffe6e6 !important; /* พื้นหลังชมพูจางๆ เฉพาะตอนชี้ */
    text-decoration: none;
    transform: rotate(90deg);   /* หมุนนิดนึงให้ดูมีลูกเล่น */
}

/* A4 Paper */
.preview-paper {
    background: white;
    width: 210mm;
    min-height: 297mm;
    margin: 0 auto;
    padding: 15mm;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
    box-sizing: border-box;
    font-size: 14px;
    position: relative;
}

/* Language Switch */

.lang-switch-container {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 84px;  /* ปรับความกว้างให้กระชับขึ้น (เดิม 100px กว้างไป) */
    height: 40px; /* ความสูงเท่าปุ่มอื่นๆ */
    background-color: #f0f2f5;
    border-radius: 6px; /* ความมนเท่ากับปุ่มอื่นในเว็บ */
    cursor: pointer;
    padding: 4px; /* ช่องว่างขอบ */
    box-sizing: border-box;
    margin-left: 10px;
    user-select: none;
    border: 1px solid #ddd;
}

/* ตัวเลื่อนสีขาว (Slider) */
.lang-slider {
    position: absolute;
    top: 4px;
    left: 4px;
    width: 37px; /* คำนวณ: (84px - 2px(border) - 8px(padding)) / 2 = 37px เป๊ะ */
    height: 30px; /* คำนวณ: 40px - 2px - 8px = 30px เป๊ะ */
    background-color: white;
    border-radius: 4px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.15); /* เงาเข้มขึ้นนิดนึงให้ดูนูน */
    transition: transform 0.25s cubic-bezier(0.4, 0.0, 0.2, 1);
    z-index: 1;
}

/* ตัวหนังสือ TH/EN */
.lang-option {
    position: relative;
    z-index: 2;
    flex: 1; /* แบ่งพื้นที่กันคนละครึ่ง */
    display: flex;          /* ใช้ Flexbox จัดกึ่งกลาง */
    justify-content: center; /* กึ่งกลางแนวนอน */
    align-items: center;     /* กึ่งกลางแนวตั้ง */
    height: 100%;
    font-size: 13px; /* ลดขนาดนิดนึงให้ดูแพง */
    font-weight: 700;
    color: #999;
    transition: color 0.25s;
}

/* สีส้มเมื่อถูกเลือก */
.lang-option.active {
    color: var(--primary);
}

/* สถานะเมื่อเป็นภาษาอังกฤษ (เลื่อน Slider ไปทางขวา) */
.lang-switch-container.en-active .lang-slider {
    transform: translateX(37px); /* เลื่อนไปเท่ากับความกว้างตัวเองเป๊ะ */
}
/* Autocomplete */
.autocomplete-wrapper {
    position: relative;
}

.autocomplete-list {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: white;
    border: 1px solid #ddd;
    max-height: 150px;
    overflow-y: auto;
    z-index: 10;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.autocomplete-item {
    padding: 8px;
    cursor: pointer;
    border-bottom: 1px solid #eee;
    font-size: 0.9rem;
}

.autocomplete-item:hover {
    background-color: #f0f0f0;
}

/* Footer */
.app-footer {
    text-align: center;
    margin-top: 20px;
    padding-bottom: 30px;
    font-size: 0.85rem;
    color: #999;
}

@media (max-width: 768px) {
    .grid-2-col {
        grid-template-columns: 1fr;
    }

    .summary-section {
        flex-direction: column;
    }

    .totals-box {
        width: 100%;
    }

    .actions-bar {
        flex-direction: column;
    }

    .header {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }

    .header-actions {
        flex-wrap: wrap;
    }
}

/* =========================================
   Print Layout & Pagination (ฉบับแก้ไข: แก้ภาพซ้อน)
   ========================================= */

/* ตั้งค่าหน้ากระดาษ A4 ในหน้าจอ Preview */
.preview-paper {
    background: gray; /* พื้นหลังสีเทา เพื่อให้เห็นขอบกระดาษชัดเจน */
    width: 100%;
    height: auto;
    padding: 20px 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px; /* ระยะห่างระหว่างกระดาษแต่ละแผ่น */
}

/* หน้ากระดาษแต่ละแผ่น (ทั้งในจอและตอนพิมพ์) */
.print-page {
    position: relative;
    width: 210mm;
    height: 296mm; /* ลดลง 1mm กัน Browser แถมขอบ */
    padding: 15mm 15mm; /* ขอบกระดาษ: บน-ล่าง-ซ้าย-ขวา */
    background: white;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); /* เงาให้ดูมีมิติ */
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    overflow: hidden; /* สำคัญ! ถ้าเนื้อหาเกิน ให้ตัดทิ้งเลย จะได้ไม่ไปซ้อนหน้าอื่น */
}

/* ส่วนหัวกระดาษหน้า 2 เป็นต้นไป */
.page-header-spacer {
    height: 30px; /* ดันหัวตารางลงมา */
}

/* เลขหน้า */
/* =========================================
   [แก้ไข] จัดเลขหน้าให้อยู่ กึ่งกลาง-ล่างสุด
   ========================================= */
.page-number {
    position: absolute;
    bottom: 15mm;      /* ระยะห่างจากขอบกระดาษด้านล่าง */
    left: 0;
    width: 100%;      /* กว้างเต็มแผ่นเพื่อให้จัดกลางได้ */
    text-align: center; /* จัดกึ่งกลาง */
    font-size: 12px;
    color: #999;
    font-weight: normal;
    pointer-events: none; /* กันไม่ให้บังเนื้อหาอื่น */
}

/* =========================================
   [แก้ปัญหา] ซ่อนกล่อง Print ในหน้าจอปกติ
   ========================================= */
#print-mount {
    display: none; /* ซ่อนไว้ ไม่ให้รกหน้าจอ */
}

/* =========================================
   SETTINGS FOR PRINTER ONLY (สำคัญมาก)
   ========================================= */
@media print {

    /* สั่งให้กลับมาโชว์ เฉพาะตอนสั่งพิมพ์ */
    #print-mount {
        display: block !important;
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        z-index: 9999;
    }

    /* ซ่อนส่วนอื่นๆ ของเว็บ */
    body > *:not(#print-mount) {
        display: none !important;
    }

    /* 1. รีเซ็ตค่าหน้ากระดาษ ตัดขอบขาวทิ้ง */
    @page {
        size: A4;
        margin: 0 !important;
    }

    /* 2. ซ่อนทุกอย่างในเว็บตามปกติ */
    body {
        visibility: hidden;
        background: white;
        margin: 0;
        padding: 0;
    }
    
    .container, .header, .modal, .actions-bar {
        display: none !important;
    }

    /* 3. เปิดแสดงผลเฉพาะกล่อง print-mount */
    #print-mount {
        display: block !important;
        visibility: visible !important;
        position: absolute;
        left: 0;
        top: 0;
        width: 210mm;
        margin: 0;
        padding: 0;
        z-index: 9999;
    }

    #print-mount * {
        visibility: visible !important;
    }

    /* 4. การจัดหน้ากระดาษ */
    .preview-paper {
        background: none;
        padding: 0;
        width: 100%;
        display: block;
    }

    .print-page {
        width: 210mm;
        height: 296mm;
        margin: 0;
        box-shadow: none;
        page-break-after: always;
        break-after: page;
        overflow: hidden;
        position: relative;
    }
    
    /* =======================================================
       [ไฮไลท์] สูตรประหยัดหมึก: บังคับขาว-ดำ เฉพาะตอนพิมพ์
       ======================================================= */
    
    /* 1. หัวตาราง (Table Header) */
    th, .bg-primary-print {
        background-color: white !important; /* ลบสีพื้นหลังออก */
        color: black !important;            /* บังคับตัวหนังสือสีดำ */
        border-top: 2px solid black !important;    /* เพิ่มเส้นขอบบนแทนสีที่หายไป */
        border-bottom: 2px solid black !important; /* เพิ่มเส้นขอบล่างแทนสีที่หายไป */
    }

    /* 2. ยอดรวมสุทธิ (Grand Total Box) */
    .grand-total-box {
        background: white !important;       /* ลบสีพื้นหลัง */
        color: black !important;            /* ตัวหนังสือสีดำ */
        border: 2px solid black !important; /* ตีกรอบสีดำเข้มแทน */
    }
    
    /* 3. ลบสีข้อความอื่นๆ ที่อาจจะเป็นสีส้ม ให้เป็นดำให้หมด */
    h1, strong, span {
        color: black !important;
    }
}

/* =========================================
   [NEW] PDF Generation Helper
   ========================================= */
/* คลาสสำหรับกล่องที่จะถูกจับภาพเป็น PDF */
.pdf-capture-container {
    /* [แก้] เปลี่ยนจาก fixed เป็น absolute เพื่อให้ html2canvas จับภาพได้ครบทั้งหน้า */
    position: absolute !important;
    top: 0;
    left: 0;
    width: 210mm;
    min-height: 297mm;
    z-index: 9999;
    background: white;
    padding: 0;
    margin: 0;
    -webkit-font-smoothing: antialiased;
}
