/* =======================================================
   1. GLOBAL & IMPORTS
   ======================================================= */
/* นำเข้าฟอนต์ TH Sarabun New จาก Google Fonts */
@import url('https://fonts.googleapis.com/css2?family=TH+Sarabun+New:ital,wght@0,400;0,700;1,400;1,700&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Prompt:wght@300;400;500;600&display=swap');

:root {
    --bg-pastel: #FDFBF7;
    --primary-pastel: #D6EAF8;
    --primary-dark: #3498DB;
    --success-pastel: #D5F5E3;
    --card-bg: #FFFFFF;
    --text-dark: #2C3E50;
}

/* ==========================================================
   📌 Navbar Alignment Style
   ========================================================== */

.navbar-container {
    /* 🔴 จุดสำคัญ: ปรับตัวเลขนี้ให้เท่ากับ max-width ของกรอบฟอร์มสร้างใบเบิกด้านล่าง */
    max-width: 1000px;

    /* จัดคอนเทนเนอร์ให้อยู่กึ่งกลางหน้าจอเสมอ */
    margin: 0 auto;

    /* เว้นระยะขอบซ้าย-ขวา เล็กน้อยกันชนขอบสำหรับจอขนาดเล็ก */
    padding: 0 20px;

    /* ใช้ Flexbox จัดข้อความซ้าย-ขวา */
    display: flex;
    justify-content: space-between;
    /* ผลักกล่องซ้ายไปซ้ายสุด ผลักกล่องขวาไปขวาสุด */
    align-items: center;
    /* จัดวางให้อยู่ในแนวระนาบเดียวกัน */
}

/* =======================================================
   2. MAIN LAYOUT & APP CONTAINER (Prompt Font)
   ======================================================= */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Prompt', sans-serif;
}

body {
    background-color: var(--bg-pastel);
    color: var(--text-dark);
    padding-bottom: 60px;
}

.navbar {
    background: #FFF;
    padding: 15px;
    border-bottom: 3px solid #F5B041;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.02);
}

.nav-container {
    max-width: 1100px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    font-weight: 500;
}

.app-container {
    max-width: 1100px;
    margin: 30px auto;
    padding: 0 20px;
}

.card {
    background: var(--card-bg);
    border-radius: 20px;
    padding: 35px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.04);
}

/* =======================================================
   3. LOGIN CARD & BUTTONS (Firebase Authentication)
   ======================================================= */
.login-card {
    text-align: center;
    max-width: 420px;
    margin: 50px auto;
}

.btn-google {
    background-color: #ffffff;
    color: #3c4043;
    border: 1px solid #dadce0;
    padding: 12px 24px;
    font-size: 0.95rem;
    border-radius: 10px;
    cursor: pointer;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    width: 100%;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
}

.btn-google:hover {
    background-color: #f8f9fa;
    border-color: #d2e3fc;
    box-shadow: 0 4px 10px rgba(66, 133, 244, 0.15);
}

.alert-box {
    padding: 12px;
    border-radius: 8px;
    margin-top: 15px;
    display: none;
    background-color: #f8d7da;
    color: #721c24;
    border: 1px solid #f5c6cb;
    font-size: 0.88rem;
    text-align: left;
}

/* =======================================================
   4. FORM & TABLE INPUTS
   ======================================================= */
.form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    margin-bottom: 20px;
}

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

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

.form-group label {
    font-size: 0.9rem;
    font-weight: 500;
    margin-bottom: 6px;
}

input[type="text"],
input[type="date"],
input[type="number"],
select {
    padding: 10px 14px;
    border: 2px solid #EAECEE;
    border-radius: 10px;
    outline: none;
    font-size: 0.95rem;
}

input:focus,
select:focus {
    border-color: var(--primary-dark);
}

.required {
    color: #E74C3C;
}

.input-items-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 10px;
}

.input-items-table th {
    background: #F8F9F9;
    padding: 10px;
    text-align: left;
    font-size: 0.85rem;
    border-bottom: 2px solid #BDC3C7;
}

.input-items-table td {
    padding: 8px 5px;
    border-bottom: 1px solid #E5E7E9;
}

.input-items-table input,
.input-items-table select {
    width: 100%;
    padding: 8px;
    border: 1px solid #BDC3C7;
    border-radius: 6px;
    font-size: 0.9rem;
}

.btn-delete-row {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1.1rem;
}

/* ==========================================
   📌 CSS สไตล์ปุ่มมาตรฐาน (เพิ่มเติม .btn-danger)
   ========================================== */
.form-actions,
.preview-actions {
    display: flex;
    justify-content: center;
    gap: 15px;
    margin-top: 25px;
}

.btn {
    padding: 10px 24px;
    border-radius: 10px;
    border: none;
    font-weight: 500;
    cursor: pointer;
    font-size: 0.95rem;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: all 0.2s ease;
}

.btn-primary {
    background: var(--primary-pastel, #dbeafe);
    color: #21618C;
}

.btn-primary:hover {
    background: var(--primary-dark, #2980b9);
    color: #FFF;
}

.btn-secondary {
    background: #EAEDED;
    color: #5D6D7E;
}

.btn-secondary:hover {
    background: #D5DBDB;
}

.btn-success {
    background: var(--success-pastel, #d4edda);
    color: #1E8449;
}

/* 🔴 เพิ่ม Class สำหรับปุ่ม PDF (สีแดงอ่อนพาสเทล นุ่มนวล) */
.btn-danger {
    background: #FADBD8;
    color: #78281F;
}

.btn-danger:hover {
    background: #da554c;
    color: #e9b0aa;
}

.view-section {
    display: none;
}

.active-view {
    display: block;
}

.hidden {
    display: none !important;
}

.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.85);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 999;
}

.spinner {
    width: 45px;
    height: 45px;
    border: 4px solid #F3F3F3;
    border-top: 4px solid var(--primary-dark);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 12px;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

.popup {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.25);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 99;
}

.popup-content {
    background: #FFF;
    padding: 35px;
    border-radius: 20px;
    text-align: center;
    max-width: 400px;
    width: 90%;
}

/* =======================================================
   📌 BUTTON & MODAL STYLE FOR HISTORY
   ======================================================= */
/* ปุ่มประวัติสีม่วงพาสเทล */
.btn-history {
    background-color: #E8DAEF;
    color: #6C3483;
    border: none;
}

.btn-history:hover {
    background-color: #D7BDE2;
    color: #512E5F;
}

/* พื้นหลังฉากหลัง Modal */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.4);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
}

/* การ์ด Modal */
.modal-card {
    background: #ffffff;
    width: 90%;
    max-width: 650px;
    border-radius: 16px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
    overflow: hidden;
    animation: modalFadeIn 0.25s ease-out;
}

@keyframes modalFadeIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

.modal-header {
    padding: 16px 24px;
    background-color: #F8F9F9;
    border-bottom: 1px solid #EAECEE;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h3 {
    font-size: 1.1rem;
    color: #2C3E50;
    font-weight: 500;
}

.modal-close-btn {
    background: none;
    border: none;
    font-size: 1.6rem;
    color: #7F8C8D;
    cursor: pointer;
}

.modal-close-btn:hover {
    color: #C0392B;
}

.modal-body {
    padding: 20px 24px;
    max-height: 400px;
    overflow-y: auto;
}

/* ตารางแสดงประวัติ */
.history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.92rem;
}

.history-table th {
    background-color: #F2F4F4;
    padding: 10px;
    text-align: center;
    color: #34495E;
    border-bottom: 2px solid #BDC3C7;
}

.history-table td {
    padding: 10px;
    border-bottom: 1px solid #ECF0F1;
    text-align: center;
    color: #2C3E50;
}

/* =======================================================
   5. A4 PRINT & PREVIEW (TH Sarabun New)
   ======================================================= */
.a4-page,
.a4-page * {
    font-family: 'TH Sarabun New', 'Sarabun', sans-serif !important;
    font-weight: normal !important;
}

.a4-page {
    background: #FFFFFF;
    width: 210mm;
    min-height: 297mm;
    padding: 20mm 15mm 15mm 15mm;
    margin: 20px auto;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
    position: relative;
    color: #000000;
    font-size: 16pt;
    line-height: 1.15;
}

.a4-doc-id {
    position: absolute;
    top: 20mm;
    right: 15mm;
    font-size: 16pt;
}

.a4-header-center {
    text-align: center;
    margin-top: 10px;
    margin-bottom: 25px;
}

.buu-logo {
    width: 65px;
    height: auto;
    margin-bottom: 5px;
}

.uni-title {
    font-size: 16pt;
    margin-bottom: 2px;
}

.doc-title {
    font-size: 16pt;
}

.metadata-layout-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 20px;
    font-size: 16pt;
}

.metadata-layout-table td {
    padding: 4px 0;
    vertical-align: bottom;
}

.a4-materials-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 10px;
    font-size: 15pt;
}

.a4-materials-table th,
.a4-materials-table td {
    border: 1px solid #000000;
    padding: 5px 4px;
    vertical-align: middle;
}

.a4-materials-table th {
    text-align: center;
    background-color: #FFFFFF;
}

.section-supply-title {
    text-align: center;
}

.static-code-row td {
    height: 32px;
}

.total-row td {
    height: 32px;
}

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

.signatures-layout-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 30px;
    text-align: center;
    font-size: 15pt;
}

.signatures-layout-table td {
    width: 33.33%;
    padding: 3px 0;
}

.sig-line-row td {
    vertical-align: bottom;
    padding-top: 50px;
}

.sig-name-row td {
    vertical-align: middle;
    padding-top: 2px;
}

.sig-title-row td {
    vertical-align: top;
    padding-top: 2px;
}

/* =======================================================
   6. PRINT MEDIA STYLES (ปรับปรุงแก้ไขให้พอดี 1 หน้า A4 เสมอ)
   ======================================================= */
/* =======================================================
   PRINT MEDIA STYLES (ยึดโครงสร้างเดิม 100% - แก้ตำแหน่งเลขที่ + ดึงลายเซ็นเข้าหน้าแรก)
   ======================================================= */
@media print {
    @page {
        size: A4 portrait;
        margin: 8mm 12mm 8mm 12mm !important;
        /* ปรับขอบ บน-ล่าง ลงเล็กน้อยเป็น 8mm */
    }

    html,
    body {
        background: #FFFFFF !important;
        padding: 0 !important;
        margin: 0 !important;
        width: 100% !important;
        height: auto !important;
        overflow: visible !important;
    }

    .no-print,
    .view-section,
    header,
    footer,
    nav,
    .navbar,
    .form-actions,
    #postSaveActions {
        display: none !important;
    }

    #previewSection {
        display: block !important;
        width: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    .a4-page {
        position: relative !important;
        /* 🟢 ปักหมุดให้ .a4-doc-id อิงตำแหน่งขวาบน */
        width: 100% !important;
        min-height: auto !important;
        max-height: 100% !important;
        border: none !important;
        box-shadow: none !important;
        margin: 0 !important;
        padding: 0 !important;
        box-sizing: border-box !important;
        font-size: 14pt !important;
        line-height: 1.2 !important;
    }

    /* 🟢 ดึง เลขที่... ขึ้นไปด้านบนสุด (ขยับ top เข้าหาขอบบน) */
    .a4-doc-id {
        position: absolute !important;
        top: 0mm !important;
        /* 🟢 ดึงขึ้นไปขอบบนสุด */
        right: 0mm !important;
        /* ชิดขวา */
        font-size: 14pt !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    .a4-materials-table {
        margin-top: 6px !important;
        font-size: 14pt !important;
    }

    .a4-materials-table th,
    .a4-materials-table td {
        padding: 3px 4px !important;
    }

    /* 🟢 ปรับระยะตารางลายเซ็นดึงกลับขึ้นมาในหน้าแรก */
    .signatures-layout-table {
        margin-top: 15px !important;
        /* 🟢 ลดระยะห่างด้านบนเพื่อดึงลายเซ็นกลับเข้าหน้า 1 */
        font-size: 14pt !important;
        page-break-inside: avoid !important;
        break-inside: avoid !important;
    }

    .sig-line-row td {
        padding-top: 18px !important;
        /* ปรับระยะบรรทัดเซ็นชื่อให้กระชับพอดี */
    }
}

/* =======================================================
   PDF EXPORT TEMPORARY STYLES (สไตล์กระชับพื้นที่เฉพาะตอนจับภาพ PDF)
   ======================================================= */

/* 1. คุมขนาดคอนเทนเนอร์หลักให้อยู่ใน 1 หน้า A4 พอดี ไม่ให้เกิดหน้า 2 */
#previewSection.pdf-print-fit {
    box-sizing: border-box !important;
    padding: 0 !important;
    margin: 0 !important;
    height: auto !important;
    max-height: 295mm !important;
    overflow: hidden !important;
}

#previewSection.pdf-print-fit .a4-page {
    box-shadow: none !important;
    border: none !important;
    margin: 0 !important;
    padding: 8mm 12mm 6mm 12mm !important;
    /* กำหนดระยะขอบกระดาษใน PDF */
    box-sizing: border-box !important;
}

/* 2. กระชับส่วนหัวเอกสาร (ลดขนาดโลโก้และระยะห่างบรรทัด) */
#previewSection.pdf-print-fit .a4-header-center {
    margin-top: 0 !important;
    margin-bottom: 4px !important;
}

/* 2.1 ดึงเลขที่เอกสารขึ้นไปเท่ากับโลโก้มหาลัย */
#previewSection.pdf-print-fit .a4-doc-id {
    position: absolute !important;
    top: 5mm !important;
    right: 12mm !important;
    font-size: 14pt !important;
    margin: 0 !important;
    padding: 0 !important;
}

#previewSection.pdf-print-fit .buu-logo {
    width: 85px !important;
    /* ย่อขนาดโลโก้ลงเล็กน้อยให้สมดุล */
    height: auto !important;
    margin-bottom: 2px !important;
}

#previewSection.pdf-print-fit .uni-title {
    font-size: 15pt !important;
    line-height: 1.1 !important;
    margin-bottom: 0 !important;
}

#previewSection.pdf-print-fit .doc-title {
    font-size: 16pt !important;
    line-height: 1.1 !important;
}

/* 3. กระชับตารางข้อมูลส่วนหัว (แหล่งเงิน/หน่วยงาน/วันที่) */
#previewSection.pdf-print-fit .metadata-layout-table {
    margin-top: 2px !important;
    margin-bottom: 4px !important;
}

#previewSection.pdf-print-fit .metadata-layout-table td {
    padding: 1px 0 !important;
    line-height: 1.15 !important;
}

/* 4. กระชับตารางรายการพัสดุ */
#previewSection.pdf-print-fit .a4-materials-table {
    margin-top: 4px !important;
}

#previewSection.pdf-print-fit .a4-materials-table th {
    padding: 4px 3px !important;
}

#previewSection.pdf-print-fit .a4-materials-table td {
    padding: 2px 3px !important;
}

#previewSection.pdf-print-fit .static-code-row td,
#previewSection.pdf-print-fit .total-row td {
    height: 24px !important;
}

/* 5. ดึงตารางลายเซ็นขึ้นด้านบน เพื่อป้องกันการโดนตัดขอบล่าง */
#previewSection.pdf-print-fit .signatures-layout-table {
    margin-top: -5px !important;
    margin-bottom: 0 !important;
    page-break-inside: avoid !important;
    break-inside: avoid !important;
}

#previewSection.pdf-print-fit .sig-line-row td {
    padding-top: 80px !important;
}

#previewSection.pdf-print-fit .sig-name-row td,
#previewSection.pdf-print-fit .sig-title-row td {
    padding-top: 1px !important;
}

/* ==========================================
   📌 POPUP & MODAL STYLES (จัดให้อยู่กลางจอ)
   ========================================== */
.popup-hidden {
    display: none !important;
}

#successPopup {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.5);
    /* พื้นหลังสีดำโปร่งแสง */
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    /* ให้อยู่ชั้นบนสุด */
}

.popup-card {
    background: white;
    padding: 30px;
    border-radius: 12px;
    text-align: center;
    max-width: 400px;
    width: 90%;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
    animation: popupFadeIn 0.3s ease-out;
}

@keyframes popupFadeIn {
    from {
        transform: scale(0.8);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

.hidden {
    display: none !important;
}