/* ==========================================================================
   รายงานการขยายผลสู่กลุ่มเป้าหมาย
   กรมวิชาการเกษตร กองยุทธศาสตร์และแผนงาน กลุ่มติดตามและประเมินผล
   ธีมสี: Natural Green
   ========================================================================== */

:root {
    --color-primary: #2E7D32;      /* เขียวเข้ม - header, ปุ่มหลัก */
    --color-primary-dark: #1B5E20; /* เขียวเข้มมาก - hover, group header */
    --color-secondary: #66BB6A;    /* เขียวอ่อน - accent */
    --color-bg-light: #F1F8E9;     /* เขียวอ่อนมาก - พื้นกลุ่ม/แถบสลับสี */
    --color-text: #212121;
    --color-border: #C8E6C9;
    --font-family: 'Sarabun', sans-serif;

    /* ค่าเสริมที่ใช้ภายใน */
    --color-muted: #6B7770;
    --color-surface: #FFFFFF;
    --color-page: #F7FAF5;
    --color-danger: #B3261E;
    --header-height: 68px;
    --radius: 8px;
    --shadow-sm: 0 1px 3px rgba(27, 94, 32, .16);
    --shadow-md: 0 4px 14px rgba(27, 94, 32, .18);
}

/* --------------------------------------------------------------- พื้นฐาน */
*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    font-family: var(--font-family);
    font-size: 15px;
    line-height: 1.5;
    color: var(--color-text);
    background: var(--color-page);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

[x-cloak] {
    display: none !important;
}

button {
    font-family: inherit;
    font-size: inherit;
    cursor: pointer;
}

input,
select {
    font-family: inherit;
    font-size: inherit;
}

/* ---------------------------------------------------------------- Header */
.app-header {
    position: sticky;
    top: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    gap: 14px;
    min-height: var(--header-height);
    padding: 8px 18px;
    background: linear-gradient(180deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
    color: #fff;
    box-shadow: var(--shadow-md);
}

.app-header__logo {
    height: 48px;
    width: auto;
    flex: none;
    background: #fff;
    border-radius: 50%;
    padding: 3px;
}

.app-header__titles {
    min-width: 0;
}

.app-header__title {
    margin: 0;
    font-size: 1.4rem;
    font-weight: 700;
    line-height: 1.25;
    letter-spacing: .01em;
}

.app-header__subtitle {
    margin: 0;
    font-size: .82rem;
    font-weight: 300;
    opacity: .92;
}

/* ---------------------------------------------------------------- Footer */
.app-footer {
    margin-top: auto;
    padding: 12px 16px;
    text-align: center;
    font-size: .82rem;
    color: #fff;
    background: var(--color-primary-dark);
}

/* ------------------------------------------------------------ โครงหน้าจอ */
.app-main {
    flex: 1 1 auto;
    display: grid;
    grid-template-columns: 340px minmax(0, 1fr);
    gap: 16px;
    align-items: start;
    padding: 16px;
    width: 100%;
}

/* -------------------------------------------------------- Filter Panel */
.panel {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
}

.filter-panel {
    position: sticky;
    top: calc(var(--header-height) + 16px);
    max-height: calc(100vh - var(--header-height) - 32px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.filter-panel__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 10px 14px;
    background: var(--color-bg-light);
    border-bottom: 1px solid var(--color-border);
    border-radius: var(--radius) var(--radius) 0 0;
    font-weight: 600;
    color: var(--color-primary-dark);
}

.filter-panel__toggle {
    display: none;
    border: 1px solid var(--color-border);
    background: #fff;
    color: var(--color-primary-dark);
    border-radius: 6px;
    padding: 4px 10px;
}

.filter-panel__body {
    padding: 12px 14px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.filter-panel__actions {
    display: flex;
    gap: 8px;
    padding: 12px 14px;
    border-top: 1px solid var(--color-border);
    background: #FBFDF9;
    border-radius: 0 0 var(--radius) var(--radius);
}

.filter-panel__actions .btn {
    flex: 1 1 auto;
    justify-content: center;
}

/* ------------------------------------------------------------ ปุ่มต่าง ๆ */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 8px 14px;
    border-radius: 6px;
    border: 1px solid transparent;
    font-weight: 500;
    line-height: 1.2;
    transition: background-color .15s ease, box-shadow .15s ease, color .15s ease;
    white-space: nowrap;
}

.btn:focus-visible {
    outline: 3px solid var(--color-secondary);
    outline-offset: 2px;
}

.btn:disabled {
    opacity: .55;
    cursor: not-allowed;
}

.btn--primary {
    background: var(--color-primary);
    color: #fff;
}

.btn--primary:hover:not(:disabled) {
    background: var(--color-primary-dark);
}

.btn--outline {
    background: #fff;
    color: var(--color-primary-dark);
    border-color: var(--color-primary);
}

.btn--outline:hover:not(:disabled) {
    background: var(--color-bg-light);
}

.btn--ghost {
    background: transparent;
    color: var(--color-primary-dark);
    border-color: var(--color-border);
}

.btn--ghost:hover:not(:disabled) {
    background: var(--color-bg-light);
}

.btn--sm {
    padding: 4px 9px;
    font-size: .8rem;
}

/* ------------------------------------------------- Multi-select dropdown */
.ms {
    position: relative;
}

.ms__label {
    display: block;
    margin-bottom: 4px;
    font-size: .84rem;
    font-weight: 600;
    color: var(--color-primary-dark);
}

.ms__control {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    min-height: 38px;
    padding: 6px 10px;
    text-align: left;
    background: #fff;
    border: 1px solid var(--color-border);
    border-radius: 6px;
    color: var(--color-text);
}

.ms__control:hover {
    border-color: var(--color-secondary);
}

.ms__control[aria-expanded="true"] {
    border-color: var(--color-primary);
    box-shadow: 0 0 0 2px rgba(102, 187, 106, .35);
}

.ms__value {
    flex: 1 1 auto;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: .9rem;
}

.ms__value--empty {
    color: var(--color-muted);
}

.ms__count {
    flex: none;
    background: var(--color-primary);
    color: #fff;
    border-radius: 999px;
    padding: 1px 8px;
    font-size: .75rem;
}

.ms__caret {
    flex: none;
    color: var(--color-primary);
    font-size: .8rem;
}

/* เมนูตัวเลือกแสดงในลำดับปกติของเอกสาร (ไม่ใช้ position:absolute)
   เพื่อไม่ให้ถูกตัดขอบเมื่อแผงตัวกรองมีแถบเลื่อนของตัวเอง */
.ms__menu {
    margin-top: 4px;
    background: #fff;
    border: 1px solid var(--color-primary);
    border-radius: 6px;
    box-shadow: var(--shadow-md);
    display: flex;
    flex-direction: column;
    max-height: 320px;
    overflow: hidden;
}

.ms__search {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px;
    border-bottom: 1px solid var(--color-border);
}

.ms__search i {
    color: var(--color-muted);
}

.ms__search input {
    flex: 1 1 auto;
    min-width: 0;
    border: 1px solid var(--color-border);
    border-radius: 5px;
    padding: 5px 8px;
}

.ms__search input:focus {
    outline: none;
    border-color: var(--color-primary);
}

.ms__tools {
    display: flex;
    gap: 6px;
    padding: 6px 8px;
    border-bottom: 1px solid var(--color-border);
    background: var(--color-bg-light);
}

.ms__options {
    overflow-y: auto;
    padding: 4px 0;
}

.ms__option {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 5px 10px;
    font-size: .88rem;
    cursor: pointer;
}

.ms__option:hover {
    background: var(--color-bg-light);
}

.ms__option input {
    margin: 3px 0 0;
    accent-color: var(--color-primary);
    flex: none;
}

.ms__empty {
    padding: 10px;
    text-align: center;
    color: var(--color-muted);
    font-size: .85rem;
}

.ms__footnote {
    padding: 5px 8px;
    border-top: 1px solid var(--color-border);
    font-size: .75rem;
    color: var(--color-muted);
    text-align: center;
}

/* ------------------------------------------------------- ส่วนแสดงผลตาราง */
.result {
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
}

.toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--color-border);
    background: var(--color-bg-light);
    border-radius: var(--radius) var(--radius) 0 0;
}

.toolbar__title {
    font-weight: 600;
    color: var(--color-primary-dark);
    margin-right: auto;
    display: flex;
    align-items: center;
    gap: 8px;
}

.toolbar__field {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: .85rem;
    color: var(--color-primary-dark);
}

.toolbar__field select {
    border: 1px solid var(--color-border);
    border-radius: 6px;
    padding: 5px 8px;
    background: #fff;
}

.notice {
    margin: 0;
    padding: 9px 14px;
    font-size: .86rem;
    background: #FFF8E1;
    color: #7A5A00;
    border-bottom: 1px solid #FFE0A3;
    display: flex;
    align-items: center;
    gap: 8px;
}

.notice--error {
    background: #FDECEA;
    color: var(--color-danger);
    border-bottom-color: #F5C2BE;
}

/* แถบแสดงเส้นทางกลุ่มปัจจุบัน (sticky group header ระหว่างเลื่อนดูข้อมูล) */
.group-path {
    position: sticky;
    top: var(--header-height);
    z-index: 12;
    display: none;
    flex-direction: column;
    background: rgba(241, 248, 233, .97);
    border-bottom: 2px solid var(--color-primary);
    backdrop-filter: blur(2px);
}

.group-path.is-visible {
    display: flex;
}

.group-path__item {
    display: flex;
    gap: 6px;
    padding: 2px 10px;
    font-size: .8rem;
    line-height: 1.35;
    color: var(--color-primary-dark);
    border-top: 1px dotted var(--color-border);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.group-path__item:first-child {
    border-top: none;
}

.group-path__label {
    font-weight: 600;
    flex: none;
}

.grid-wrap {
    position: relative;
    min-height: 380px;
}

.grid {
    min-height: 380px;
}

/* สถานะว่าง / กำลังโหลด */
.state {
    position: absolute;
    inset: 0;
    z-index: 20;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    text-align: center;
    padding: 24px;
    background: rgba(255, 255, 255, .96);
    color: var(--color-muted);
}

.state__icon {
    font-size: 3rem;
    color: var(--color-secondary);
}

.state__text {
    font-size: 1.05rem;
    font-weight: 600;
    color: var(--color-primary-dark);
}

.state__hint {
    font-size: .88rem;
}

.spinner {
    width: 46px;
    height: 46px;
    border: 5px solid var(--color-border);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: spin .9s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.grid-footer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 10px 14px;
    border-top: 2px solid var(--color-primary);
    background: var(--color-bg-light);
    border-radius: 0 0 var(--radius) var(--radius);
    font-weight: 600;
    color: var(--color-primary-dark);
}

.grid-footer__hint {
    font-weight: 400;
    font-size: .82rem;
    color: var(--color-muted);
}

/* ------------------------------------------------- ปรับแต่ง Tabulator.js */
.tabulator {
    border: none;
    border-radius: 0;
    background: #fff;
    font-size: .88rem;
}

.tabulator .tabulator-header {
    background: var(--color-primary);
    border-bottom: 2px solid var(--color-primary-dark);
    color: #fff;
    font-weight: 600;
}

.tabulator .tabulator-header .tabulator-col {
    background: var(--color-primary);
    border-right: 1px solid rgba(255, 255, 255, .28);
    color: #fff;
}

.tabulator .tabulator-header .tabulator-col.tabulator-sortable:hover {
    background: var(--color-primary-dark) !important;
}

.tabulator .tabulator-header .tabulator-col .tabulator-col-content {
    padding: 8px 8px;
}

.tabulator-row {
    border-bottom: 1px solid #ECF3E7;
}

.tabulator-row.tabulator-row-even {
    background: #FDFEFB;
}

.tabulator-row:hover {
    background: var(--color-bg-light) !important;
}

.tabulator-row .tabulator-cell {
    border-right: 1px solid #EEF4EA;
    padding: 6px 8px;
}

/* หัวกลุ่ม 7 ระดับ */
.tabulator-row.tabulator-group {
    background: var(--color-bg-light);
    border-bottom: 1px solid var(--color-border);
    border-top: 1px solid var(--color-border);
    color: var(--color-primary-dark);
    font-weight: 600;
    padding: 5px 8px;
}

.tabulator-row.tabulator-group:hover {
    background: #E4F0D6 !important;
}

.tabulator-row.tabulator-group .tabulator-arrow {
    border-left-color: var(--color-primary-dark);
    border-top-color: var(--color-primary-dark);
    margin-right: 8px;
}

.tabulator-row.tabulator-group .group-label {
    color: var(--color-primary);
    font-weight: 400;
}

.tabulator-row.tabulator-group .group-count {
    color: var(--color-muted);
    font-weight: 400;
    font-size: .82rem;
}

.tabulator-row.tabulator-group-level-0 {
    background: #DCEDC8;
    border-left: 6px solid var(--color-primary-dark);
    font-size: 1rem;
}

.tabulator-row.tabulator-group-level-1 {
    background: #E3F1CE;
    border-left: 6px solid var(--color-primary);
}

.tabulator-row.tabulator-group-level-2 {
    background: #E9F4D9;
    border-left: 6px solid #43A047;
}

.tabulator-row.tabulator-group-level-3 {
    background: #EDF6E1;
    border-left: 6px solid #66BB6A;
}

.tabulator-row.tabulator-group-level-4 {
    background: #F1F8E9;
    border-left: 6px solid #81C784;
}

.tabulator-row.tabulator-group-level-5 {
    background: #F5FAEE;
    border-left: 6px solid #A5D6A7;
}

.tabulator-row.tabulator-group-level-6 {
    background: #F9FCF4;
    border-left: 6px solid #C5E1A5;
    font-weight: 500;
}

.tabulator .tabulator-footer {
    background: #fff;
    border-top: 1px solid var(--color-border);
}

.tabulator .tabulator-footer .tabulator-page.active {
    background: var(--color-primary);
    color: #fff;
    border-color: var(--color-primary-dark);
}

.tabulator .tabulator-footer .tabulator-page {
    border-radius: 5px;
}

.cell-number {
    display: block;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* -------------------------------------------------------- Responsive */

/* Tablet: 768px - 1199px  => Filter Panel วางซ้อนด้านบน, ตารางเลื่อนแนวนอนได้ */
@media (max-width: 1199px) {
    .app-main {
        grid-template-columns: minmax(0, 1fr);
    }

    .filter-panel {
        position: static;
        max-height: none;
    }

    .filter-panel__toggle {
        display: inline-flex;
        align-items: center;
        gap: 6px;
    }

    .filter-panel__body {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
        gap: 10px 14px;
        max-height: 52vh;
    }

    .filter-panel--collapsed .filter-panel__body,
    .filter-panel--collapsed .filter-panel__actions {
        display: none;
    }
}

/* Mobile (< 768px): ยังคงให้ตารางเลื่อนแนวนอนได้เป็นอย่างน้อย */
@media (max-width: 767px) {
    body {
        font-size: 14px;
    }

    .app-main {
        padding: 10px;
        gap: 10px;
    }

    .app-header {
        min-height: auto;
    }

    .app-header__logo {
        height: 38px;
    }

    .app-header__title {
        font-size: 1.05rem;
    }

    .app-header__subtitle {
        font-size: .72rem;
    }

    .filter-panel__body {
        grid-template-columns: minmax(0, 1fr);
    }

    .filter-panel__actions {
        flex-direction: column;
    }

    .toolbar {
        gap: 6px;
    }

    .toolbar__title {
        width: 100%;
        margin-right: 0;
    }
}
