@import url('https://fonts.googleapis.com/css2?family=Sarabun:wght@400;500;600;700&display=swap');

@font-face {
    font-family: "TH Sarabun New";
    src: local("TH Sarabun New"), local("TH Sarabun PSK"), local("Sarabun");
}

/* ===== CSS Variables ===== */
:root {
    --app-bg: #f4f7f4;
    --line: #cfd8cf;
    --ink: #1f2a24;
    --muted: #66736b;
    --brand: #198754;
    --sidebar-w: 260px;
    --topbar-h: 56px;

    /* Override Bootstrap font variables */
    --bs-body-font-family: "TH Sarabun New", "Sarabun", Arial, sans-serif;
    --bs-font-sans-serif: "TH Sarabun New", "Sarabun", Arial, sans-serif;
}

/* ===== Base — ครอบ font ทุก element รวม Bootstrap ===== */
*, *::before, *::after {
    font-family: "TH Sarabun New", "Sarabun", Arial, sans-serif;
}

body {
    background: var(--app-bg);
    color: var(--ink);
    font-size: 18px;
}

/* ป้องกัน iOS ซูมอัตโนมัติ + font ชัดเจน */
input, select, textarea, button {
    font-family: "TH Sarabun New", "Sarabun", Arial, sans-serif !important;
    font-size: 17px !important;
}

/* ===== Layout =====
   height: 100vh + overflow: hidden ทำให้ main-content มี scroll container จริง
   ซึ่งทำให้ position: sticky; bottom: 0 ทำงานภายใน main-content ได้ถูกต้อง
*/
.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

.main-content {
    flex-grow: 1;
    overflow-y: auto;
    overflow-x: hidden;
    background: var(--app-bg);
    min-width: 0;
    display: flex;
    flex-direction: column;
}

/* ===== Sidebar ===== */
.app-sidebar {
    width: var(--sidebar-w);
    background: linear-gradient(180deg, #0f2d1e 0%, #1a3d2b 40%, #163324 100%);
    box-shadow: 4px 0 24px rgba(0,0,0,0.35);
    color: #ecf0f1;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    position: sticky;
    top: 0;
    height: 100vh;
    padding: 20px 0;
    transition: transform 0.28s cubic-bezier(.4,0,.2,1);
    z-index: 300;
    /* พื้นผิวลายจุดละเอียด */
    background-image:
        linear-gradient(180deg, #0f2d1e 0%, #1a3d2b 40%, #163324 100%),
        radial-gradient(circle, rgba(255,255,255,0.018) 1px, transparent 1px);
    background-size: auto, 22px 22px;
}

/* เส้นแสงบนขอบขวา sidebar */
.app-sidebar::after {
    content: '';
    position: absolute;
    top: 0; right: 0; bottom: 0;
    width: 1px;
    background: linear-gradient(180deg,
        transparent 0%,
        rgba(255,255,255,0.08) 20%,
        rgba(255,255,255,0.12) 50%,
        rgba(255,255,255,0.08) 80%,
        transparent 100%);
    pointer-events: none;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 4px 20px 20px 20px;
    border-bottom: 1px solid rgba(255,255,255,0.08);
    margin-bottom: 16px;
    position: relative;
}

.brand-logo {
    width: 44px;
    height: 44px;
    object-fit: contain;
    border-radius: 10px;
    background: #fff;
    padding: 3px;
    flex-shrink: 0;
    box-shadow: 0 2px 10px rgba(0,0,0,0.3), 0 0 0 1px rgba(255,255,255,0.15);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.brand-logo:hover {
    transform: scale(1.06);
    box-shadow: 0 4px 16px rgba(0,0,0,0.4), 0 0 0 2px rgba(255,255,255,0.25);
}

.brand-text {
    display: flex;
    flex-direction: column;
}

.brand-title {
    font-size: 15px;
    font-weight: 700;
    color: #fff;
    line-height: 1.2;
    text-shadow: 0 1px 3px rgba(0,0,0,0.4);
}

.brand-subtitle {
    font-size: 11px;
    color: #7fb88a;
    margin-top: 2px;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 0 10px;
}

/* nav item — มี indicator แถบซ้าย + ripple effect */
.sidebar-nav .nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 11px 15px;
    color: #a8c4b0;
    text-decoration: none;
    border-radius: 10px;
    font-weight: 500;
    font-size: 15px;
    transition: background 0.18s ease, color 0.18s ease,
                transform 0.12s ease, box-shadow 0.18s ease;
    min-height: 48px;
    border: none;
    position: relative;
    overflow: hidden;
    cursor: pointer;
    background: transparent;
}

/* แถบไฟซ้ายซ่อนไว้ */
.sidebar-nav .nav-item::before {
    content: '';
    position: absolute;
    left: 0; top: 20%; bottom: 20%;
    width: 3px;
    border-radius: 0 3px 3px 0;
    background: #4caf50;
    opacity: 0;
    transform: scaleY(0);
    transition: opacity 0.2s, transform 0.2s cubic-bezier(.4,0,.2,1);
}

.sidebar-nav .nav-item:hover {
    background: rgba(255,255,255,0.07);
    color: #e8f5e9;
    transform: translateX(3px);
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.05);
}

.sidebar-nav .nav-item:hover::before {
    opacity: 0.6;
    transform: scaleY(1);
}

.sidebar-nav .nav-item:active {
    transform: translateX(1px) scale(0.98);
    background: rgba(255,255,255,0.04);
}

/* active state — เขียวสว่างมี glow */
.sidebar-nav .nav-item.active {
    background: linear-gradient(135deg, #1f6b3a 0%, #198754 100%);
    color: #fff;
    font-weight: 700;
    box-shadow:
        0 4px 15px rgba(25,135,84,0.4),
        inset 0 1px 0 rgba(255,255,255,0.15),
        inset 0 -1px 0 rgba(0,0,0,0.1);
    transform: none;
}

.sidebar-nav .nav-item.active::before {
    opacity: 1;
    transform: scaleY(1);
    background: #a5d6a7;
    width: 4px;
}

.sidebar-nav .nav-item i {
    font-size: 19px;
    flex-shrink: 0;
    transition: transform 0.2s ease;
}

.sidebar-nav .nav-item:hover i {
    transform: scale(1.15);
}

.sidebar-nav .nav-item.active i {
    filter: drop-shadow(0 0 4px rgba(165,214,167,0.6));
}

/* footer ด้านล่าง sidebar */
.sidebar-footer {
    margin-top: auto;
    padding: 14px 12px 16px;
    border-top: 1px solid rgba(255,255,255,0.08);
}

/* PWA install button — ดูเหมือนปุ่มจริง ไม่ใช่แค่รายการเมนู */
.pwa-install-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 11px 16px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    color: #1b5e20;
    text-align: center;
    background: linear-gradient(135deg, #a5d6a7 0%, #66bb6a 50%, #43a047 100%) !important;
    border: none !important;
    box-shadow:
        0 4px 12px rgba(67,160,71,0.45),
        inset 0 1px 0 rgba(255,255,255,0.35),
        inset 0 -2px 0 rgba(0,0,0,0.15);
    transition: all 0.18s ease;
    letter-spacing: 0.3px;
}

.pwa-install-btn i {
    font-size: 17px;
    filter: drop-shadow(0 1px 1px rgba(0,0,0,0.2));
}

.pwa-install-btn:hover {
    background: linear-gradient(135deg, #b9f6ca 0%, #69f0ae 40%, #00c853 100%) !important;
    color: #1b5e20;
    box-shadow:
        0 6px 18px rgba(0,200,83,0.5),
        inset 0 1px 0 rgba(255,255,255,0.45),
        inset 0 -2px 0 rgba(0,0,0,0.12);
    transform: translateY(-1px);
}

.pwa-install-btn:active {
    transform: translateY(1px);
    box-shadow:
        0 2px 6px rgba(67,160,71,0.35),
        inset 0 1px 0 rgba(255,255,255,0.2),
        inset 0 2px 4px rgba(0,0,0,0.15);
}

/* sidebar-save-area ซ่อนทุก breakpoint */
.sidebar-save-area {
    display: none;
}

/* ===== Save Bar — sticky ติดด้านล่าง main-content scroll container ===== */
.form-save-bar {
    background: #fff;
    border-top: 2px solid var(--brand);
    bottom: 0;
    box-shadow: 0 -3px 12px rgba(0,0,0,0.10);
    display: flex;
    gap: 12px;
    justify-content: center;
    align-items: center;
    padding: 14px 20px;
    position: sticky;
    z-index: 100;
    flex-shrink: 0;
}

/* ปุ่ม 3D มีมิติ */
.form-save-bar .btn {
    min-width: 140px;
    min-height: 48px;
    font-size: 15px;
    font-weight: 700;
    border-radius: 10px;
    letter-spacing: 0.3px;
    position: relative;
    transition: transform 0.08s ease, box-shadow 0.08s ease;
}

/* ปุ่ม บันทึก — เขียว 3D */
.form-save-bar .btn-success {
    background: linear-gradient(180deg, #2e9e5b 0%, #1a7a40 100%);
    border: none;
    border-bottom: 4px solid #0f5228;
    box-shadow: 0 4px 12px rgba(25, 135, 84, 0.45), inset 0 1px 0 rgba(255,255,255,0.18);
    color: #fff;
    text-shadow: 0 1px 2px rgba(0,0,0,0.25);
}

.form-save-bar .btn-success:hover {
    background: linear-gradient(180deg, #34b364 0%, #1e8c49 100%);
    box-shadow: 0 6px 16px rgba(25, 135, 84, 0.5), inset 0 1px 0 rgba(255,255,255,0.2);
    transform: translateY(-1px);
}

.form-save-bar .btn-success:active {
    background: linear-gradient(180deg, #1a7a40 0%, #145e32 100%);
    border-bottom-width: 1px;
    box-shadow: 0 1px 4px rgba(25, 135, 84, 0.3);
    transform: translateY(3px);
}

/* ปุ่ม กลับรายการ — ขาว/เทา 3D */
.form-save-bar .btn-outline-secondary {
    background: linear-gradient(180deg, #ffffff 0%, #e8ede8 100%);
    border: 1px solid #b0bdb0;
    border-bottom: 4px solid #8a9a8a;
    box-shadow: 0 4px 10px rgba(0,0,0,0.12), inset 0 1px 0 rgba(255,255,255,0.9);
    color: #2d4a35;
    font-weight: 700;
}

.form-save-bar .btn-outline-secondary:hover {
    background: linear-gradient(180deg, #fff 0%, #dce8dc 100%);
    box-shadow: 0 6px 14px rgba(0,0,0,0.15), inset 0 1px 0 rgba(255,255,255,0.9);
    transform: translateY(-1px);
    color: #1a3325;
}

.form-save-bar .btn-outline-secondary:active {
    background: linear-gradient(180deg, #dce8dc 0%, #c8d8c8 100%);
    border-bottom-width: 1px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.10);
    transform: translateY(3px);
}

/* ===== Mobile Top Bar (ซ่อนบน desktop) ===== */
.mobile-topbar {
    display: none;
}

/* Overlay เมื่อ sidebar เปิดบน mobile */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 299;
}

.sidebar-overlay.active {
    display: block;
}

/* Sticky save bar บน mobile (ซ่อนบน desktop) */
.mobile-save-bar {
    display: none;
}

/* ===== Toolbar — sticky ติดด้านบน content area ตลอดเวลา ===== */
.toolbar {
    align-items: center;
    background: var(--app-bg);
    border-bottom: 1px solid var(--line);
    display: flex;
    gap: 12px;
    justify-content: space-between;
    margin-bottom: 14px;
    flex-wrap: wrap;
    padding: 10px 0;
    position: sticky;
    top: 0;
    z-index: 100;
}

.toolbar h1 {
    font-size: 22px;
    font-weight: 700;
    margin: 0;
}

/* ===== Form Panels ===== */
.slip-entry-form {
    margin: 0 auto;
    max-width: 1280px;
}

.form-panel,
.search-panel,
.list-panel {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 8px;
    margin-bottom: 14px;
    padding: 16px;
}

.section-title {
    border-left: 4px solid var(--brand);
    font-size: 17px;
    font-weight: 700;
    margin-bottom: 12px;
    padding-left: 10px;
}

.form-label {
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 4px;
}

/* ===== Entry Tables ===== */
.entry-table th {
    background: #eef6ee;
    color: #26362c;
    white-space: nowrap;
    font-size: 13px;
}

.entry-table td:first-child {
    font-weight: 600;
    min-width: 180px;
}

.entry-table input {
    min-width: 80px;
}

/* ===== Preview Toolbar ===== */
.preview-toolbar {
    background: #fff;
    border-bottom: 1px solid #ddd;
    display: flex;
    gap: 8px;
    justify-content: center;
    padding: 10px;
    position: sticky;
    top: 0;
    z-index: 30;
    flex-wrap: wrap;
}

/* ===== List Table ===== */
.action-cell {
    white-space: nowrap;
}

.list-panel th:not(:last-child) {
    cursor: pointer;
    user-select: none;
    transition: background-color 0.2s;
}

.list-panel th:not(:last-child):hover {
    background-color: #e2efe2 !important;
}

/* ===== Dotted Input (ส่วน other) ===== */
.dotted-input {
    border-top: 0 !important;
    border-left: 0 !important;
    border-right: 0 !important;
    border-bottom: 1px dotted #999 !important;
    background: transparent !important;
    border-radius: 0 !important;
    padding: 0 4px !important;
    box-shadow: none !important;
    outline: none !important;
}

.dotted-input:focus {
    border-bottom: 1px dotted #198754 !important;
    background: rgba(25, 135, 84, 0.05) !important;
}

/* ===== Thai Date Widget ===== */
.thai-date-widget {
    flex-wrap: nowrap;
}

.thai-date-widget input[type="number"]::-webkit-inner-spin-button,
.thai-date-widget input[type="number"]::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.thai-date-widget input[type="number"] {
    -moz-appearance: textfield;
    appearance: textfield;
}

/* ============================================================
   RESPONSIVE — Tablet & Mobile (≤ 991px)
   Sidebar กลายเป็น drawer ซ่อนด้านซ้าย (ครอบคลุม PDA / tablet / มือถือ)
   ============================================================ */
@media (max-width: 991.98px) {

    /* Top bar แสดงบน mobile/tablet */
    .mobile-topbar {
        display: flex;
        align-items: center;
        gap: 10px;
        background: #1e3d2f;
        color: #fff;
        padding: 0 14px;
        height: var(--topbar-h);
        position: sticky;
        top: 0;
        z-index: 400;
        flex-shrink: 0;
    }

    .mobile-topbar-title {
        font-size: 15px;
        font-weight: 700;
        flex-grow: 1;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .hamburger-btn {
        background: none;
        border: none;
        color: #fff;
        cursor: pointer;
        padding: 8px;
        border-radius: 6px;
        display: flex;
        align-items: center;
        justify-content: center;
        min-width: 40px;
        min-height: 40px;
        flex-shrink: 0;
    }

    .hamburger-btn:hover {
        background: rgba(255,255,255,0.1);
    }

    /* เปลี่ยน layout เป็น column ให้ topbar อยู่บนสุด */
    .app-container {
        display: flex;
        flex-direction: column;
        height: 100dvh;
        overflow: visible;
    }

    /* Sidebar กลายเป็น fixed drawer ซ่อนด้านซ้าย */
    .app-sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        width: 280px;
        transform: translateX(-100%);
        z-index: 500;
        padding-top: 16px;
    }

    /* เมื่อเปิด drawer */
    .app-sidebar.open {
        transform: translateX(0);
    }

    /* ปรับ padding sidebar brand */
    .app-sidebar .sidebar-brand {
        padding: 12px 20px 16px 20px;
    }

    .main-content {
        width: 100%;
        flex-grow: 1;
        overflow-y: auto;
    }

    /* form-save-bar ให้ปุ่มใหญ่ขึ้นสำหรับนิ้ว */
    .form-save-bar .btn {
        min-height: 48px;
        font-size: 15px;
        flex: 1;
    }

    /* toolbar — เลื่อน top ตาม topbar */
    .toolbar {
        top: var(--topbar-h);
        padding: 8px 0;
    }

    .toolbar h1 {
        font-size: 17px;
    }

    /* Panel เต็มความกว้าง ไม่มี border radius ด้านข้าง */
    .form-panel,
    .search-panel,
    .list-panel {
        border-radius: 0;
        margin-left: -12px;
        margin-right: -12px;
        padding: 12px;
    }

    /* ขนาด input ใหญ่ขึ้นสำหรับนิ้ว */
    .form-control,
    .form-select {
        min-height: 44px;
    }

    .form-control-sm,
    .input-group-sm .form-control,
    .input-group-sm .form-select {
        min-height: 40px;
    }

    /* Thai date widget — อนุญาตให้ wrap ได้ */
    .thai-date-widget {
        flex-wrap: wrap;
        gap: 4px !important;
    }

    /* ตารางให้ scroll แนวนอนได้ */
    .table-responsive {
        -webkit-overflow-scrolling: touch;
    }

    /* รายการในตาราง part1/2/3 ย่อ min-width */
    .entry-table td:first-child {
        min-width: 140px;
        font-size: 13px;
    }

    .entry-table input {
        min-width: 64px;
    }

    /* หน้ารายการ — ซ่อนคอลัมน์ที่ไม่จำเป็น */
    .list-panel table th:nth-child(4),
    .list-panel table td:nth-child(4),
    .list-panel table th:nth-child(5),
    .list-panel table td:nth-child(5) {
        display: none;
    }

    /* action buttons เป็น column */
    .action-cell {
        display: flex;
        flex-direction: column;
        gap: 4px;
        align-items: flex-end;
    }

    .action-cell .btn {
        min-width: 72px;
        min-height: 36px;
    }
}

/* ============================================================
   RESPONSIVE — Small Mobile (≤ 575px)
   ============================================================ */
@media (max-width: 575px) {
    .app-sidebar {
        width: 100vw; /* drawer เต็มจอบนโทรศัพท์เล็ก */
    }

    .thai-date-widget input[type="number"] {
        width: 56px !important;
    }
}
