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

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

html, body {
    font-family: 'Sarabun', sans-serif;
    height: 100%;
    overflow: hidden;
}

/* =========================
   HEADER & NAVIGATION
========================= */
.app {
    height: 100vh;
    display: grid;
    grid-template-rows: minmax(90px, 10vh) 1fr;
}

.header {
    display: flex;
    align-items: center;
    padding: 0 30px;
    background: linear-gradient(135deg, #a8e063 0%, #56ab2f 50%, #2d6a4f 100%);
    border-bottom: 1px solid rgba(255, 255, 255, 0.25);
    box-shadow: 0 8px 24px rgba(45, 106, 79, 0.2);
    position: relative;
    gap: 40px;
    z-index: 1000;
}

.logo {
    height: 60px;
    cursor: pointer;
}

/* =========================
   MENUS
========================= */
.hazard-menu {
    display: flex;
    gap: 15px;
    padding: 0;
    background: transparent;
    flex: 1;
}

.hazard-item {
    position: relative;
    display: flex;
    align-items: center;
    gap: 8px;
    flex: none; 
    min-width: auto; 
}

.hazard-item.disabled {
    cursor: pointer;
    opacity: 0.7;
}

.hazard-item.disabled::after {
    content: "อยู่ระหว่างพัฒนา";
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(-10deg);
    font-size: 10px;
    font-weight: 800;
    color: rgba(239, 68, 68, 0.4);
    white-space: nowrap;
    pointer-events: none;
    z-index: 10;
    text-transform: uppercase;
    border: 1px solid rgba(239, 68, 68, 0.3);
    padding: 1px 4px;
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.6);
}

.hazard {
    flex: 1;
    text-align: center;
    padding: 8px 15px;
    border-radius: 12px;
    cursor: pointer;
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(4px);
    transition: .2s;
    font-size: 12px;
    white-space: nowrap;
    min-width: 45px;
}

.hazard:hover {
    background: #e9f5db;
}

.hazard.active {
    background: #ffffff;
    box-shadow: 0 2px 6px rgba(0, 0, 0, .15);
    font-weight: 800;
    color: #1b4332;
}

.hazard-icon {
    font-size: 18px;
    vertical-align: middle;
}

.hazard-text {
    vertical-align: middle;
    margin-left: 5px;
}

/* Tooltip & Compact style */
.hazard-item:not(.active-container) .hazard-text {
    display: none;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 20px;
    margin-left: auto;
    flex-shrink: 0; /* ป้องกันไม่ให้ถูกเบียดจนหายไป */
}

#user_display {
    font-weight: 800;
    color: #ffffff;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
    font-size: 14px;
}

.btn-manual {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: none;
    background: #ffffff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    transition: all 0.3s ease;
    flex-shrink: 0;
    color: #2d6a4f;
}

.btn-manual:hover {
    background: #e9f5db;
    transform: scale(1.1) rotate(5deg);
    color: #1b4332;
}

.btn-header-contact {
    background: white;
    border: none;
    border-radius: 999px;
    padding: 8px 18px;
    font-weight: 600;
    font-size: 13px;
    color: #1b4332;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    gap: 6px;
}

.btn-header-contact:hover {
    background: #e9f5db;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

/* =========================
   DASHBOARD MAIN (3/20/77 Layout)
========================= */
.dashboard-main {
    display: flex;
    flex: 1;
    min-height: 0;
    position: relative;
    overflow: hidden;
}

/* 1. Trigger Bar (3%) */
.sidebar-trigger-bar {
    width: 3%;
    min-width: 40px; 
    background: linear-gradient(180deg, #a8e063 0%, #56ab2f 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: white;
    font-size: 20px;
    transition: all 0.3s ease;
    z-index: 1005;
    box-shadow: 2px 0 10px rgba(45, 106, 79, 0.2);
    border-top-right-radius: 20px;
    border-bottom-right-radius: 20px;
    margin: 10px 0;
    height: calc(100% - 20px);
}

.sidebar-trigger-bar:hover {
    background: #ffffff;
    color: #40916c;
}

/* 2. Side Panel (47%) */
.info-sidebar {
    width: 47%;
    min-width: 400px; 
    display: flex;
    flex-direction: column;
    gap: 15px;
    overflow-y: auto;
    padding: 15px;
    background: #f1f8f1;
    border-right: 1px solid #d8e3d8;
    transition: all 0.4s ease;
    z-index: 900;
}

/* 3. Map Area (50%) */
.map-container {
    width: 50%;
    flex: 0 0 50%;
    position: relative;
    background: white;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transition: all 0.4s ease; /* เพิ่ม transition ให้แผนที่ขยายแบบนุ่มนวล */
}

/* =========================
   SIDEBAR COLLAPSED STATE
========================= */
.sidebar-collapsed .info-sidebar {
    width: 0;
    min-width: 0;
    padding: 0;
    margin: 0;
    border: none;
    opacity: 0;
    pointer-events: none;
}

.sidebar-collapsed .map-container {
    width: 97%;
    flex: 0 0 97%;
}

.sidebar-collapsed .sidebar-trigger-bar i {
    transform: rotate(180deg); /* หมุนลูกศรเมื่อปิด */
}

.map-title-overlay {
    width: 100%;
    text-align: center;
    padding: 15px 0;
    font-weight: 800;
    font-size: 28px;
    color: #1b4332;
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(6px);
    z-index: 800;
}

#map {
    width: 100%;
    flex: 1;
}

/* =========================
   SIDEBAR COMPONENTS
========================= */
.info-card {
    background: white;
    border-radius: 18px;
    padding: 15px;
    border: 1px solid rgba(82, 183, 136, 0.2);
    box-shadow: 0 4px 12px rgba(27, 67, 50, 0.03);
    height: auto; /* 🔹 ปรับความสูงให้พอดีข้อมูล */
    min-height: fit-content;
}

.card-title {
    font-weight: 800;
    color: #2d6a4f;
    font-size: 15px;
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.choices-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.choice-item {
    padding: 8px 12px;
    background: #f7fbf7;
    border-radius: 10px;
    font-size: 13.5px;
    color: #40916c;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: all 0.2s;
    cursor: default;
}

.choice-item:hover {
    background: #d8f3dc;
    transform: translateX(5px);
}

.choice-item i {
    width: 20px;
    color: #52b788;
}

/* Stats Card Enhanced */
.stats-summary {
    border-radius: 14px;
    margin-bottom: 15px;
}

.summary-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.summary-box {
    background: #f8f9fa;
    border-radius: 12px;
    padding: 12px;
    text-align: center;
    border: 1px solid #e9ecef;
}

.summary-box.highlight-box {
    grid-column: span 2;
    background: linear-gradient(135deg, #1b4332 0%, #2d6a4f 100%);
    color: white;
    border: none;
    box-shadow: 0 4px 15px rgba(27, 67, 50, 0.15);
}

.summary-label {
    font-size: 12px;
    margin-bottom: 4px;
    color: #6b7280;
    font-weight: 600;
}

.summary-box.highlight-box .summary-label {
    color: rgba(255, 255, 255, 0.8);
}

.summary-value {
    font-size: 18px;
    font-weight: 800;
    color: #1f2937;
}

.summary-box.highlight-box .summary-value {
    font-size: 24px;
    color: white;
}

.summary-value small {
    font-size: 13px;
    font-weight: 400;
    opacity: 0.8;
}

.residue-list-wrapper {
    display: flex;
    flex-direction: column;
    gap: 12px;
    height: auto; /* 🔹 ปล่อยให้สูงตามรายการข้อมูล */
    padding-right: 5px;
}

.residue-item {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding-bottom: 10px;
    border-bottom: 1px solid #f0f4f0;
}

.residue-item:last-child {
    border-bottom: none;
}

.residue-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.residue-name {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13.5px;
    font-weight: 600;
    color: #1b4332;
}

.residue-name i {
    width: 20px;
    text-align: center;
    color: #52b788;
}

.residue-val {
    font-size: 13px;
    font-weight: 700;
    color: #2d6a4f;
}

.residue-bar-bg {
    width: 100%;
    height: 6px;
    background: #e9f5db;
    border-radius: 3px;
    overflow: hidden;
}

.residue-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, #74c69d, #40916c);
    border-radius: 3px;
    transition: width 0.8s cubic-bezier(0.4, 0, 0.2, 1);
}

.no-data-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 30px 0;
    color: #95d5b2;
    gap: 10px;
}

.no-data-placeholder i {
    font-size: 32px;
}

.no-data-placeholder p {
    font-size: 13px;
    font-weight: 500;
}

.side-logo {
    width: 100%;
    max-height: 140px;
    object-fit: contain;
}

/* Sidebar Collapsed State */
.sidebar-collapsed .info-sidebar {
    width: 0 !important;
    min-width: 0 !important;
    padding: 0;
    opacity: 0;
    pointer-events: none;
}

.sidebar-collapsed .sidebar-trigger-bar i {
    transform: rotate(180deg);
}

/* =========================
   MODALS & OVERLAYS
========================= */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(8px);
    z-index: 10000;
    align-items: center;
    justify-content: center;
}

#login_overlay {
    position: fixed;
    inset: 0;
    z-index: 11000;
    background: rgba(0, 0, 0, 0.6);
    display: none;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(10px);
}

.login-box, .processing-card {
    background: white;
    border-radius: 24px;
    padding: 30px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.4);
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.processing-card {
    width: 400px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 25px;
    text-align: center;
    animation: swal-show 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.spinner-box {
    padding: 10px;
}

.modern-spinner {
    width: 60px;
    height: 60px;
    border: 5px solid #f0f7f4;
    border-top: 5px solid #40916c;
    border-radius: 50%;
    animation: spin 1s cubic-bezier(0.4, 0, 0.2, 1) infinite;
}

.processing-info {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.proc-title {
    font-size: 22px;
    font-weight: 800;
    color: #1b4332;
    letter-spacing: -0.5px;
}

.proc-status {
    font-size: 14.5px;
    color: #52b788;
    line-height: 1.5;
    font-weight: 500;
}

.btn-cancel-modal {
    width: 100%;
    padding: 12px;
    border: 1px solid #d8e2dc;
    background: #f8f9fa;
    color: #6c757d;
    border-radius: 14px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 13.5px;
    margin-top: 10px;
}

.btn-cancel-modal:hover {
    background: #fff0f0;
    color: #e63946;
    border-color: #ffb3b3;
}

.login-title {
    font-size: 18px;
    font-weight: 700;
    text-align: center;
    margin-bottom: 18px;
    color: #2d6a4f;
}

.login-actions {
    display: flex;
    gap: 10px;
    margin-top: 18px;
}

.login-actions button {
    flex: 1;
    border: none;
    border-radius: 999px;
    padding: 10px;
    cursor: pointer;
    font-weight: 600;
}

.btn-cancel { background: #ccc; }

/* Contact Card */
.contact-card {
    background: white;
    width: 800px;
    border-radius: 24px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.contact-header {
    background: linear-gradient(135deg, #a8e063 0%, #2d6a4f 100%);
    padding: 20px 25px;
    color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.contact-title { font-size: 20px; font-weight: 700; }

.contact-close {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.contact-body {
    padding: 30px;
    display: grid;
    grid-template-columns: 1fr 1.2fr;
    gap: 30px;
    background: #f0f7f7;
}

.dept-name {
    color: #1b4332;
    font-size: 18px;
    font-weight: 800;
    margin-bottom: 25px;
}

.info-item {
    display: flex;
    gap: 15px;
    margin-bottom: 20px;
    font-size: 15px;
    color: #444;
}

.contact-map {
    border-radius: 16px;
    overflow: hidden;
    border: 1px solid #e0e0e0;
    height: 350px;
}

/* Spinner */
.modern-spinner {
    width: 50px;
    height: 50px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #52b788;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
@keyframes swal-show { 0% { transform: scale(0.5); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }

.shadow-soft {
    box-shadow: 0 8px 18px rgba(0, 0, 0, .08), 0 2px 6px rgba(0, 0, 0, .05);
}

/* =========================
   FILTER BAR (Standardized with HNB)
========================= */
.filter-bar {
    display: flex;
    flex-direction: column;
    padding: 15px 20px;
    background: #ffffff;
    border-radius: 20px;
    box-shadow: 0 4px 15px rgba(27, 67, 50, 0.04);
    margin-bottom: 15px;
    border: 1px solid rgba(82, 183, 136, 0.15);
    height: auto; /* 🔹 สูงตามเนื้อหา */
}

.filter-bar .card-title {
    margin-bottom: 0; /* เอา margin ออกเพื่อให้เวลาหดแล้วไม่เหลือที่ว่าง */
    padding-bottom: 0;
    border-bottom: none;
    color: #2d6a4f;
    font-size: 15px;
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 24px;
}

/* เมื่อส่วนเนื้อหาแสดงผล ค่อยเพิ่มระยะห่าง */
.filter-group[style*="display: block"] {
    margin-top: 15px;
    padding-top: 15px;
    border-top: 1px dashed rgba(82, 183, 136, 0.2);
}

.filter-group {
    display: flex;
    flex-direction: column;
    gap: 18px; /* เพิ่มระยะห่างระหว่างกลุ่มตัวเลือก */
}

.filter-item {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.filter-item label {
    font-size: 12.5px;
    font-weight: 700;
    color: #1b4332;
    margin-left: 2px;
}

.filter-item select {
    border: 1px solid #e0e7e0;
    border-radius: 12px;
    padding: 0 14px;
    background: #f9fbf9;
    font-size: 14px;
    color: #2d6a4f;
    height: 44px;
    outline: none;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.filter-item select:focus {
    border-color: #52b788;
    background: white;
    box-shadow: 0 0 0 4px rgba(82, 183, 136, 0.1);
}

.filter-item .date-box {
    display: flex;
    align-items: center;
    gap: 0;
    background: #f9fbf9;
    border-radius: 12px;
    border: 1px solid #e0e7e0;
    height: 44px;
    overflow: hidden;
    transition: all 0.2s ease;
}

.filter-item .date-box:focus-within {
    border-color: #52b788;
    background: white;
    box-shadow: 0 0 0 4px rgba(82, 183, 136, 0.1);
}

.filter-item .date-box input {
    flex: 1;
    min-width: 0;
    border: none;
    background: transparent;
    padding: 0 10px;
    font-size: 13px;
    color: #2d6a4f;
    outline: none;
    height: 100%;
    text-align: center;
    cursor: pointer;
}

.filter-item .date-box span {
    color: #74a48e;
    font-weight: bold;
    flex-shrink: 0;
    padding: 0 5px;
}

.btn-filter-process {
    width: 100%;
    background: linear-gradient(135deg, #74c69d 0%, #2d6a4f 100%);
    color: white;
    border: none;
    border-radius: 12px;
    padding: 14px;
    font-weight: 700;
    font-size: 14.5px;
    cursor: pointer;
    transition: all 0.3s;
    margin-top: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.btn-filter-process:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(45, 106, 79, 0.25);
    filter: brightness(1.1);
}

.btn-filter-process:active {
    transform: translateY(0);
}


/* =========================
   MAP LABELS (Standardized with HNB)
========================= */
.label {
    display: flex;
    align-items: center;
    justify-content: center;
    color: black;
    font-weight: 800;
    text-shadow:
        -1px -1px 0 white,
        1px -1px 0 white,
        -1px 1px 0 white,
        1px 1px 0 white;
    pointer-events: none; /* ป้องกันการคลิกทับพื้นที่ด้านหลัง */
}

/* =========================
   MAP LABELS (Enhanced Shadow for HNB Look)
========================= */
.label {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #000000;
    font-weight: 900;
    white-space: nowrap;
    line-height: 1;
    text-shadow: 
        2px 2px 0 #FFFFFF, 
        -2px 2px 0 #FFFFFF, 
        2px -2px 0 #FFFFFF, 
        -2px -2px 0 #FFFFFF, 
        0 0 5px #FFFFFF;
    pointer-events: none;
}

/* =========================
   FORMULA MODAL STYLES
========================= */
.formula-card {
    background: white;
    width: 500px;
    border-radius: 24px;
    overflow: hidden;
    animation: swal-show 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.formula-header {
    background: linear-gradient(135deg, #40916c 0%, #1b4332 100%);
    padding: 18px 25px;
    color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.formula-title { font-size: 18px; font-weight: 700; }

.formula-close {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.formula-close:hover { background: rgba(255, 255, 255, 0.3); }

.formula-body { padding: 25px; }

.formula-intro {
    font-size: 13px;
    color: #6c757d;
    margin-bottom: 20px;
    line-height: 1.5;
}

.formula-main-box {
    background: #f8f9fa;
    border-left: 4px solid #40916c;
    padding: 15px;
    border-radius: 4px 12px 12px 4px;
    margin-bottom: 20px;
}

.formula-text {
    display: inline-block;
    margin-top: 8px;
    font-size: 14px;
    font-weight: 700;
    color: #2d6a4f;
}

.crop-formula-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 15px;
    margin-bottom: 20px;
}

.crop-formula-item {
    background: #f1f8f1;
    padding: 12px;
    border-radius: 12px;
    border: 1px solid rgba(64, 145, 108, 0.1);
}

.crop-formula-name {
    font-weight: 800;
    font-size: 14px;
    color: #1b4332;
    margin-bottom: 8px;
}

.crop-formula-detail {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 12px;
    color: #40916c;
}

.formula-note {
    font-size: 11px;
    color: #adb5bd;
    line-height: 1.6;
    border-top: 1px dashed #dee2e6;
    padding-top: 15px;
}

.btn-info-formula {
    background: #e9f5db;
    color: #40916c;
    border: none;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    font-size: 12px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    margin-left: auto;
}

.btn-info-formula:hover {
    background: #40916c;
    color: white;
    transform: scale(1.1);
}

.ref-link {
    color: #2d6a4f;
    text-decoration: underline;
    font-weight: 600;
    transition: color 0.2s;
}

.ref-link:hover {
    color: #40916c;
}

/* =========================
   ENHANCED FORMULA MODAL STYLES
========================= */
.formula-section {
    margin-bottom: 25px;
}

.section-subtitle {
    color: #1b4332;
    font-size: 15px;
    font-weight: 800;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.section-subtitle i {
    color: #40916c;
}

.section-desc {
    font-size: 13.5px;
    color: #4b5563;
    margin-bottom: 10px;
}

.reference-table {
    width: 100%;
    border-collapse: collapse;
    margin: 10px 0;
    font-size: 13px;
    background: white;
    border-radius: 12px;
    overflow: hidden;
    border: 1px solid #e5e7eb;
}

.reference-table th {
    background: #f1f8f1;
    color: #1b4332;
    padding: 12px;
    text-align: left;
    font-weight: 700;
}

.reference-table td {
    padding: 12px;
    border-top: 1px solid #f3f4f6;
    color: #374151;
}

.badge-check {
    background: #dcfce7;
    color: #166534;
    padding: 4px 10px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 700;
}

.ref-list {
    padding-left: 20px;
    font-size: 13px;
    color: #4b5563;
}

.ref-list li {
    margin-bottom: 5px;
}

.academic-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.academic-item {
    background: #f9fafb;
    padding: 12px;
    border-radius: 10px;
    font-size: 12px;
    color: #4b5563;
    border-left: 3px solid #9ca3af;
}

.formula-footer-note {
    background: #1b4332;
    color: white;
    padding: 20px;
    border-radius: 12px;
    font-style: italic;
    font-size: 13px;
    line-height: 1.6;
    text-align: center;
    margin-top: 20px;
}

/* =========================
   MOBILE PORTRAIT (Responsive)
========================= */
@media (max-width: 768px) {
    html, body {
        height: auto !important;
        overflow: auto !important;
    }

    .app {
        min-width: 0 !important;
        width: 100% !important;
        height: auto !important;
        display: block !important;
        overflow-x: hidden;
    }

    .header {
        padding: 15px !important;
        height: auto !important;
        min-height: 80px;
        flex-direction: column;
        gap: 15px;
    }

    .logo {
        height: 45px;
        margin-bottom: 5px;
    }

    .hazard-menu {
        max-width: 100%;
        overflow-x: auto;
        padding-bottom: 10px;
        width: 100%;
        justify-content: flex-start;
        display: flex;
        -webkit-overflow-scrolling: touch;
    }

    .hazard-item {
        flex: 0 0 auto;
    }

    .header-right {
        width: 100%;
        justify-content: center;
        margin-left: 0;
        gap: 15px;
        padding-top: 10px;
        border-top: 1px solid rgba(255,255,255,0.2);
    }

    .dashboard-main {
        flex-direction: column !important;
        height: auto !important;
        padding: 0 !important;
        gap: 0 !important;
        display: flex !important;
        overflow: visible !important;
    }

    .sidebar-trigger-bar {
        display: none !important;
    }

    .map-container {
        width: 100% !important;
        height: 450px !important;
        flex: none !important;
        order: -1; /* แผนที่อยู่บนสุด */
    }

    .map-title-overlay {
        font-size: 18px !important;
        padding: 10px 15px;
    }

    .info-sidebar {
        width: 100% !important;
        min-width: 0 !important;
        padding: 15px !important;
        overflow: visible !important;
        background: #f1f8f1 !important;
        border-right: none !important;
    }

    .info-card {
        width: 100% !important;
        margin-bottom: 15px;
    }

    /* Stats Summary Adjustments for Mobile */
    .stats-summary {
        flex-direction: column !important;
        gap: 20px !important;
    }

    .stats-summary > div {
        width: 100% !important;
    }

    #summary_donut_chart {
        height: 250px !important;
    }

    /* Modals */
    .contact-card, .processing-card, .formula-card {
        width: 95% !important;
        max-width: 100% !important;
        margin: 10px;
    }

    .contact-body {
        grid-template-columns: 1fr;
        padding: 20px;
    }

    .contact-map {
        height: 200px;
    }
}

/* =========================
   MOBILE LANDSCAPE (Responsive)
========================= */
@media (orientation: landscape) and (max-height: 600px) {
    .app {
        height: auto !important;
        display: block !important;
    }

    .header {
        /* ปล่อยว่างไว้ หรือเอาออกหากใช้ตัวล่างแทน */
    }

    .dashboard-main {
        display: flex !important;
        flex-direction: row !important;
        height: calc(100vh - 60px) !important;
        overflow: hidden !important;
    }

    .sidebar-trigger-bar {
        display: none !important;
    }

    .info-sidebar {
        width: 45% !important;
        height: 100% !important;
        padding: 10px !important;
        border-right: 1px solid #d8e3d8 !important;
    }

    .map-container {
        width: 55% !important;
        height: 100% !important;
        flex: none !important;
        order: 1 !important;
    }

    .map-title-overlay {
        font-size: 16px !important;
        padding: 8px !important;
    }

    .info-card {
        padding: 10px !important;
        margin-bottom: 10px !important;
    }

    .summary-grid {
        grid-template-columns: 1fr 1fr 1fr !important; /* กางออก 3 คอลัมน์ในแนวนอน */
    }

    .summary-box.highlight-box {
        grid-column: span 1 !important;
    }

    #summary_donut_chart {
        height: 180px !important;
    }

    canvas#crop_bar_chart, canvas#area_bar_chart {
        height: 180px !important;
    }
}


/* =========================
   MOBILE LANDSCAPE (Standardized)
========================= */
@media (orientation: landscape) and (max-height: 600px) {
    .app {
        height: 100vh !important;
        width: 100vw !important;
        display: flex !important;
        flex-direction: column !important;
        background: #f4f6f9 !important;
        overflow: hidden !important;
        position: fixed !important;
        top: 0;
        left: 0;
    }

    .header {
        flex: none !important;
        height: 70px !important;
        min-height: 70px !important;
        padding: 0 12px !important;         /* ลด padding ขอบลงเหลือ 12px */
        display: flex !important;
        flex-direction: row !important;
        align-items: center !important;
        gap: 12px !important;               /* ลด gap ระหว่าง Logo/Menu/Right เหลือ 12px */
        background: linear-gradient(135deg, #a8e063 0%, #56ab2f 50%, #2d6a4f 100%) !important;
        border-bottom: 1px solid rgba(255, 255, 255, 0.25) !important;
        box-shadow: 0 8px 24px rgba(45, 106, 79, 0.2) !important;
        z-index: 1000 !important;
        position: relative !important;
        width: 100% !important;
        overflow: hidden !important;
        box-sizing: border-box !important;
    }

    .logo {
        height: 32px !important;            /* ลดความสูงโลโก้ลงอีก */
        width: auto !important;
        max-width: 150px !important;        /* จำกัดความกว้างไม่ให้เบียด */
        flex-shrink: 0 !important;
        margin: 0 !important;
        cursor: pointer !important;
    }

    .hazard-menu {
        display: flex !important;
        gap: 6px !important;                /* ลด gap ระหว่างปุ่มเมนูเหลือ 6px */
        padding: 0 5px !important;
        margin: 0 !important;
        background: transparent !important;
        flex: 1 1 0 !important;             /* กินพื้นที่ที่เหลือ */
        align-items: center !important;
        overflow-x: auto !important;        /* ให้เลื่อนแนวนอนได้ถ้าเมนูเยอะเกิน */
        overflow-y: hidden !important;
        min-width: 0 !important;            /* สำคัญ: ให้ flex item หดได้ */
        -webkit-overflow-scrolling: touch !important;
    }

    /* ซ่อน scrollbar สำหรับ hazard-menu */
    .hazard-menu::-webkit-scrollbar {
        display: none !important;
    }

    .hazard {
        flex: 0 0 auto !important;
        text-align: center !important;
        padding: 4px 10px !important;       /* ลด padding ลงเหลือ 4px 10px */
        border-radius: 8px !important;
        cursor: pointer !important;
        background: rgba(255, 255, 255, 0.7) !important;
        backdrop-filter: blur(4px) !important;
        transition: .2s !important;
        font-size: 10px !important;         /* ลดฟอนต์เหลือ 10px */
        white-space: nowrap !important;
        color: #333 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        border: none !important;
        min-width: 32px !important;         /* ขนาดขั้นต่ำเล็กลง */
    }

    .hazard.active {
        background: #ffffff !important;
        box-shadow: 0 2px 6px rgba(0, 0, 0, .15) !important;
        font-weight: 800 !important;
        color: #2d6a4f !important;
    }

    .hazard-icon {
        font-size: 16px !important;         /* ลดขนาดไอคอนเหลือ 16px */
    }

    .hazard-text {
        margin-left: 5px !important; 
        font-weight: inherit !important;
    }

    /* ตรรกะแสดงเฉพาะ Active Text */
    .hazard:not(.active) .hazard-text {
        display: none !important;
    }

    .hazard.active .hazard-text {
        display: inline-block !important;
    }

    .header-right {
        display: flex !important;
        align-items: center !important;
        gap: 6px !important;
        flex-shrink: 0 !important;          /* ห้ามหด */
        margin-left: auto !important;
        min-width: fit-content !important;
    }

    #user_display {
        display: none !important;           /* ซ่อนชื่อผู้ใช้ใน landscape เพื่อเพิ่มพื้นที่ */
    }

    .btn-header-contact {
        padding: 5px 10px !important;       /* ลด padding ปุ่มให้เล็กลง */
        font-size: 11px !important;         /* ลดขนาดฟอนต์ปุ่ม */
        white-space: nowrap !important;
        height: 32px !important;            /* กำหนดความสูงให้พอดีกับโลโก้ */
        display: flex !important;
        align-items: center !important;
    }

    .dashboard-main {
        flex: 1 !important;
        display: flex !important;
        flex-direction: row !important;
        min-height: 0 !important;
        overflow: hidden !important;
    }

    /* ปรับ Modal ติดต่อเราให้พอดีกับจอแนวตั้งในโหมดแนวนอน */
    .contact-card {
        max-height: 95vh !important;
        width: 90% !important;
        display: flex !important;
        flex-direction: column !important;
        overflow: hidden !important;
        margin: auto !important;
    }
    .contact-header {
        padding: 10px 20px !important;
    }
    .contact-body {
        overflow-y: auto !important;
        padding: 15px !important;
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: 15px !important;
        flex: 1 !important;
    }
    .contact-map {
        height: 180px !important;
    }
    .dept-name {
        font-size: 14px !important;
        margin-bottom: 10px !important;
    }
    .info-item {
        font-size: 12px !important;
        margin-bottom: 8px !important;
        gap: 8px !important;
    }
}
