* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --color-gold: #E7B85E;
    --color-brown: #4E1F00;
    --color-cream: #FFFBF3;
}

body {
    font-family: 'Sarabun', 'Segoe UI', Tahoma, sans-serif;
    background-color: var(--color-cream);
    color: var(--color-brown);
    line-height: 1.6;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
    position: relative;
}

header {
    text-align: center;
    padding: 20px 0;
    border-bottom: 3px solid var(--color-gold);
    margin-bottom: 20px;
}

header h1 {
    color: var(--color-brown);
    font-size: 2rem;
    margin-bottom: 8px;
}

/* Logos */
#logos-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 14px;
    margin-bottom: 12px;
    max-width: 100%;
}

#logos-container img {
    height: 50px;
    max-width: 120px;
    width: auto;
    object-fit: contain;
}

header .subtitle {
    color: var(--color-brown);
    opacity: 0.7;
    font-size: 0.95rem;
}

/* Mode Toggle */
.mode-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 15px;
    margin-bottom: 20px;
}

.mode-label {
    font-weight: 600;
    color: var(--color-brown);
}

.toggle-buttons {
    display: flex;
    gap: 0;
    border: 2px solid var(--color-gold);
    border-radius: 8px;
    overflow: hidden;
}

.toggle-btn {
    padding: 10px 20px;
    border: none;
    background: var(--color-cream);
    color: var(--color-brown);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.toggle-btn:not(:last-child) {
    border-right: 2px solid var(--color-gold);
}

.toggle-btn:hover {
    background: rgba(231, 184, 94, 0.3);
}

.toggle-btn.active {
    background: var(--color-gold);
    color: var(--color-brown);
}

/* Filters */
.filters {
    display: flex;
    gap: 15px;
    flex-wrap: wrap;
    margin-bottom: 25px;
    padding: 20px;
    background: var(--color-cream);
    border: 2px solid var(--color-gold);
    border-radius: 12px;
}

.filter-group {
    flex: 1;
    min-width: 180px;
}

.filter-group label {
    display: block;
    font-weight: 600;
    color: var(--color-brown);
    margin-bottom: 6px;
    font-size: 0.9rem;
}

.filter-group select {
    width: 100%;
    padding: 10px 12px;
    border: 2px solid var(--color-gold);
    border-radius: 8px;
    background: var(--color-cream);
    color: var(--color-brown);
    font-size: 0.95rem;
    cursor: pointer;
    transition: border-color 0.2s;
}

.filter-group select:focus {
    outline: none;
    border-color: var(--color-brown);
}

.filter-group select:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Summary Cards */
.summary-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 15px;
    margin-bottom: 25px;
}

/* Province ranking tables */
.province-table-section {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 20px;
    margin-bottom: 25px;
}

.province-table-card {
    background: var(--color-cream);
    border: 2px solid var(--color-gold);
    border-radius: 12px;
    padding: 16px;
    box-shadow: 0 4px 15px rgba(78, 31, 0, 0.1);
}

.province-table-header h2 {
    font-size: 1.1rem;
    color: var(--color-brown);
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 2px solid var(--color-gold);
}

.table-wrapper {
    overflow-x: auto;
}

.province-rank-table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
    font-size: 0.95rem;
}

.province-rank-table th,
.province-rank-table td {
    padding: 10px 12px;
    border-bottom: 1px solid #f0f0f0;
    text-align: left;
}

.province-rank-table th {
    background: var(--color-gold);
    color: var(--color-brown);
    font-weight: 600;
}

.province-rank-table tr:last-child td {
    border-bottom: none;
}

.province-rank-table tbody tr:nth-child(even) {
    background: #fffdf7;
}

.province-rank-table tbody td:first-child {
    font-weight: 600;
    color: var(--color-brown);
}

/* Health matrix table */
.health-matrix-section {
    margin-bottom: 25px;
}

.health-matrix-card {
    background: var(--color-cream);
    border: 2px solid var(--color-gold);
    border-radius: 12px;
    padding: 16px;
    box-shadow: 0 4px 15px rgba(78, 31, 0, 0.1);
}

.health-matrix-header {
    margin-bottom: 12px;
}

.health-matrix-header h2 {
    font-size: 1.1rem;
    color: var(--color-brown);
    margin-bottom: 4px;
}

.health-matrix-header p {
    font-size: 0.88rem;
    color: rgba(78, 31, 0, 0.75);
}

.health-matrix-table-wrap {
    max-height: 460px;
    overflow: auto;
    border: 1px solid rgba(231, 184, 94, 0.7);
    border-radius: 10px;
    background: #fff;
}

.health-matrix-table {
    width: max-content;
    min-width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 0.88rem;
}

.health-matrix-table th,
.health-matrix-table td {
    padding: 9px 10px;
    border-bottom: 1px solid #f0e6d2;
    border-right: 1px solid #f8f0e0;
    white-space: nowrap;
}

.health-matrix-table thead th {
    position: sticky;
    top: 0;
    z-index: 3;
    background: #f7e2b7;
    color: var(--color-brown);
    font-weight: 700;
    text-align: center;
}

.matrix-sort-btn {
    width: 100%;
    border: none;
    background: transparent;
    color: inherit;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 0;
    font: inherit;
    font-weight: 700;
    cursor: pointer;
}

.health-matrix-table thead th:first-child .matrix-sort-btn {
    justify-content: flex-start;
}

.matrix-sort-label {
    line-height: 1.2;
}

.matrix-sort-icons {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    line-height: 0.9;
    opacity: 0.5;
    margin-top: -1px;
}

.matrix-sort-icon {
    font-size: 0.58rem;
    color: #7a6248;
}

.matrix-sort-icon.active {
    color: #4E1F00;
    opacity: 1;
}

.health-matrix-table tbody td {
    text-align: right;
    color: #4a2a11;
}

.health-matrix-table th:first-child,
.health-matrix-table td:first-child {
    position: sticky;
    left: 0;
    z-index: 2;
    min-width: 170px;
    text-align: left;
    font-weight: 600;
    background: #fffdf7;
}

.health-matrix-table thead th:first-child {
    z-index: 4;
    background: #f2d699;
}

.health-matrix-table tbody tr:nth-child(even) td {
    background: #fffdf9;
}

.health-matrix-table tbody tr:nth-child(even) td:first-child {
    background: #fff8ea;
}

.card {
    background: var(--color-cream);
    border: 2px solid var(--color-gold);
    border-radius: 12px;
    padding: 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    transition: transform 0.2s, box-shadow 0.2s;
}

.card:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 20px rgba(78, 31, 0, 0.12);
}

.card-icon {
    width: 65px;
    height: 65px;  
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
 

.card-content h3 {
    font-size: 0.75rem;
    color: var(--color-brown);
    opacity: 0.8;
    margin-bottom: 2px;
}

.card-value {
    font-size: 1.4rem;
    font-weight: 700;
    color: var(--color-brown);
    line-height: 1.2;
}

.card-label {
    font-size: 0.75rem;
    color: var(--color-brown);
    opacity: 0.6;
}

/* Overview Chart Section */
.overview-chart-section {
    margin-bottom: 25px;
}

.overview-chart-container {
    background: var(--color-cream);
    border: 2px solid var(--color-gold);
    border-radius: 12px;
    padding: 20px;
    margin: 0 auto;
    box-shadow: 0 4px 15px rgba(78, 31, 0, 0.1);
}

.overview-chart-container h2 {
    font-size: 1.1rem;
    color: var(--color-brown);
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 2px solid var(--color-gold);
    text-align: center;
}

.overview-chart-wrapper {
    position: relative;
    height: 300px;
}

/* Charts Section */
.charts-section {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
    gap: 20px;
    margin-bottom: 25px;
}

.chart-container {
    background: var(--color-cream);
    border: 2px solid var(--color-gold);
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 4px 15px rgba(78, 31, 0, 0.1);
}

.chart-container h2 {
    font-size: 1.1rem;
    color: var(--color-brown);
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 2px solid var(--color-gold);
}

.chart-wrapper {
    position: relative;
    height: 250px;
    margin-bottom: 15px;
}

/* Detail Breakdown */
.detail-breakdown {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.breakdown-item {
    background: rgba(231, 184, 94, 0.15);
    border-radius: 8px;
    padding: 12px;
    border-left: 4px solid var(--color-gold);
}

.breakdown-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.breakdown-title {
    font-weight: 600;
    color: var(--color-brown);
    font-size: 0.95rem;
}

.breakdown-total {
    background: var(--color-gold);
    color: var(--color-brown);
    padding: 3px 10px;
    border-radius: 15px;
    font-weight: 600;
    font-size: 0.85rem;
}

.breakdown-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.breakdown-tag {
    background: var(--color-cream);
    border: 1px solid var(--color-gold);
    padding: 4px 10px;
    border-radius: 5px;
    font-size: 0.8rem;
    color: var(--color-brown);
}

.breakdown-tag strong {
    color: var(--color-brown);
}

/* Status Section */
.status-section {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
    gap: 20px;
    margin-bottom: 25px;
}

.status-container {
    background: var(--color-cream);
    border: 2px solid var(--color-gold);
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 4px 15px rgba(78, 31, 0, 0.1);
}

.status-container h2 {
    font-size: 1.1rem;
    color: var(--color-brown);
    margin-bottom: 8px;
}

.status-total {
    color: var(--color-brown);
    opacity: 0.8;
    font-size: 0.9rem;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 2px solid var(--color-gold);
}

.status-total span {
    font-weight: 700;
    color: var(--color-brown);
}

.status-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 10px;
}

.status-item {
    background: rgba(231, 184, 94, 0.15);
    border-radius: 8px;
    padding: 12px;
    text-align: center;
    border: 1px solid var(--color-gold);
}

.status-name {
    font-size: 0.75rem;
    color: var(--color-brown);
    opacity: 0.8;
    margin-bottom: 4px;
}

.status-value {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--color-brown);
}

/* Loading */
.loading {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 251, 243, 0.95);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.loading.hidden {
    display: none;
}

.spinner {
    width: 50px;
    height: 50px;
    border: 4px solid var(--color-gold);
    border-top-color: var(--color-brown);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

.support-container {
    display: flex;
    position: absolute;
    top: 0;
    right: 10px;
    text-align: left;
    align-items: center;
    font-size: 13px;
    gap: 5px;
}

.associate-title {
    margin-top: 40px;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.loading p {
    margin-top: 15px;
    color: var(--color-brown);
    font-size: 1.1rem;
}

/* Responsive */
@media (max-width: 768px) {
    .container {
        padding: 15px;
    }

    header h1 {
        font-size: 1.5rem;
    }

    .mode-toggle {
        flex-direction: column;
        gap: 10px;
    }

    .filters {
        flex-direction: column;
        gap: 12px;
    }

    .filter-group {
        min-width: 100%;
    }

    .summary-cards {
        grid-template-columns: repeat(2, 1fr);
    }

    .card {
        padding: 12px;
    }

    .card-value {
        font-size: 1.2rem;
    }

    .charts-section,
    .status-section {
        grid-template-columns: 1fr;
    }

    .chart-wrapper {
        height: 220px;
    }
}

@media (max-width: 480px) {
    header {
        padding-top: 0;
    }
    .support-container {
        position: relative;
        width: 100%;
        right: 0;
    }
    .summary-cards {
        grid-template-columns: 1fr;
    }

    .toggle-btn {
        padding: 8px 15px;
        font-size: 0.9rem;
    }

    .associate-title {
        margin-top: 0;
    }

    #logos-container img {
        height: 40px;
    }
}

/* ── Map grid (heatmap + bubble side-by-side) ─────────────────────── */
.map-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 25px;
}

.map-grid .heatmap-section,
.map-grid .bubble-section {
    margin-bottom: 0;
}

@media (max-width: 900px) {
    .map-grid {
        grid-template-columns: 1fr;
    }
}

/* ── Map card: shared header + controls ───────────────────────────── */
.map-card-header {
    padding-bottom: 10px;
    margin-bottom: 15px;
    border-bottom: 2px solid var(--color-gold);
}

.map-card-header h2 {
    font-size: 1.1rem;
    color: var(--color-brown);
    margin: 0 0 .2rem;
}

.map-year-badge {
    display: inline-block;
    font-size: 0.8rem;
    color: var(--color-brown);
    opacity: 0.65;
}

.map-controls {
    border-top: 2px solid var(--color-gold);
    padding-top: 12px;
    margin-top: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.map-ctrl-row {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.map-ctrl-label {
    font-size: 0.85rem;
    color: var(--color-brown);
    opacity: 0.55;
    white-space: nowrap;
    flex-shrink: 0;
}

/* Sub-filter rows: prepend a muted label via ::before (survives innerHTML = '') */
#heatmap-health-btns.map-ctrl-sub::before,
#bubble-health-btns.map-ctrl-sub::before {
    content: 'โรค';
    font-size: 0.8rem;
    color: var(--color-brown);
    opacity: 0.5;
    white-space: nowrap;
    align-self: center;
    flex-shrink: 0;
}

#heatmap-activity-btns.map-ctrl-sub::before,
#bubble-activity-btns.map-ctrl-sub::before {
    content: 'กิจกรรม';
    font-size: 0.8rem;
    color: var(--color-brown);
    opacity: 0.5;
    white-space: nowrap;
    align-self: center;
    flex-shrink: 0;
}

/* ── Heatmap section ──────────────────────────────────────────────── */
.heatmap-section {
    background: var(--color-cream);
    border: 2px solid var(--color-gold);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 0;
    box-shadow: 0 4px 15px rgba(78, 31, 0, 0.1);
}

.heatmap-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 12px;
}

.heatmap-header h2 {
    margin: 0;
    font-size: 1.1rem;
    color: var(--color-brown);
}

.heatmap-layer-btns {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.heatmap-sub-btns {
    margin-top: 8px;
}

.heatmap-sub-btns .heatmap-btn {
    padding: 4px 10px;
    font-size: 0.8rem;
    border-style: dashed;
    opacity: 0.95;
}

.heatmap-btn {
    padding: 5px 14px;
    border: 1.5px solid var(--color-gold);
    border-radius: 20px;
    background: var(--color-cream);
    color: var(--color-brown);
    font-size: 0.85rem;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
    font-family: inherit;
}

.heatmap-btn:hover { background: rgba(231, 184, 94, 0.3); }
.heatmap-btn.active { background: var(--color-gold); border-color: var(--color-gold); color: var(--color-brown); font-weight: 600; }

.heatmap-note {
    font-size: 0.78rem;
    color: #888;
    margin-bottom: 8px;
    min-height: 1.1em;
}

#heatmap-container {
    width: 100%;
    height: 520px;
    border-radius: 8px;
    overflow: hidden;
    background: rgba(78, 31, 0, 0.04);
    border: 1px solid rgba(231, 184, 94, 0.4);
}

/* Leaflet popup overrides */
.heatmap-info {
    background: rgba(255,255,255,0.95);
    padding: 10px 14px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.18);
    font-size: 0.82rem;
    line-height: 1.6;
    min-width: 160px;
    pointer-events: none;
}
.heatmap-info b { color: var(--color-brown); }
.heatmap-info p { margin: 0; color: #888; }

.heatmap-legend {
    background: rgba(255,255,255,0.95);
    padding: 10px 12px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    font-size: 0.78rem;
    line-height: 1.8;
}
.legend-title { font-weight: 600; margin-bottom: 4px; color: #333; }
.legend-row { display: flex; align-items: center; gap: 8px; }
.legend-row span {
    display: inline-block;
    width: 14px;
    height: 14px;
    border-radius: 2px;
    flex-shrink: 0;
}

@media (max-width: 768px) {
    #heatmap-container { height: 380px; }
    .heatmap-header { flex-direction: column; align-items: flex-start; }
}

/* ── Bubble Map section ───────────────────────────────────────────── */
.bubble-section {
    background: var(--color-cream);
    border: 2px solid var(--color-gold);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 25px;
    box-shadow: 0 4px 15px rgba(78, 31, 0, 0.1);
}

.bubble-subtitle {
    font-size: 0.78rem;
    color: #888;
    margin: 4px 0 0;
}

#bubble-container {
    width: 100%;
    height: 520px;
    border-radius: 8px;
    overflow: hidden;
    background: rgba(78, 31, 0, 0.04);
    border: 1px solid rgba(231, 184, 94, 0.4);
    margin-top: 12px;
}

.bubble-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 12px 24px;
    margin: 8px 0 4px;
    font-size: 0.82rem;
    color: var(--color-brown);
}

.bubble-legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
}

.bubble-legend-item svg {
    flex-shrink: 0;
}

.scatter-insight {
    font-size: 0.82rem;
    color: var(--color-brown);
    background: var(--color-cream);
    border-left: 3px solid var(--color-gold);
    padding: 7px 12px;
    border-radius: 0 6px 6px 0;
    margin: 8px 0 12px;
    min-height: 1.5rem;
}

/* toggle btn with dot */
.bubble-toggle-btn {
    display: flex;
    align-items: center;
    gap: 6px;
}

.bubble-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

@media (max-width: 768px) {
    #bubble-container { height: 380px; }
}
