/* CSS Theme: Green Read Paper Style (Comfortable for eyes & Nature Lover) */

:root {
    /* Color Palette */
    --bg-green-read: #f6f3e9;     /* Cream Green Read Paper */
    --bg-sidebar: #e9e5d4;        /* Muted Warm Cream-Beige */
    --bg-card: #fdfcf9;           /* Soft Clean Paper Card */
    --primary-green: #244b35;     /* Dark Forest Green */
    --secondary-green: #3d6a50;   /* Sage/Pine Green */
    --accent-green: #7ca88a;      /* Soft Leaf Green */
    --text-dark: #27342b;         /* Charcoal Green-Slate (No pure black) */
    --text-light: #5d6f63;        /* Muted Sage Grey */
    --border-color: #ded9c3;      /* Soft beige border */
    
    /* UI Values */
    --radius-lg: 12px;
    --radius-md: 8px;
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    --shadow: 0 4px 18px rgba(36, 75, 53, 0.04), 0 2px 6px rgba(36, 75, 53, 0.02);
    --font-thai: 'Sarabun', sans-serif;
    --font-en: 'Outfit', sans-serif;
}

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

body {
    font-family: var(--font-thai);
    background-color: var(--bg-green-read);
    color: var(--text-dark);
    line-height: 1.6;
    overflow-x: hidden;
}

h1, h2, h3, h4, h5, h6 {
    font-weight: 600;
    color: var(--primary-green);
}

/* App Container Layout */
.app-container {
    display: flex;
    min-height: 100vh;
}

/* Sidebar Styling */
.sidebar {
    width: 320px;
    background-color: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    padding: 2.2rem 1.8rem;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    flex-shrink: 0;
}

.sidebar-header {
    margin-bottom: 2rem;
}

.logo-area {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    color: var(--primary-green);
    font-size: 1.3rem;
    font-weight: 700;
    font-family: var(--font-en);
    margin-bottom: 0.8rem;
}

.logo-area i {
    font-size: 1.4rem;
}

.sidebar-header h1 {
    font-size: 1.45rem;
    line-height: 1.3;
    margin-bottom: 0.4rem;
}

.sidebar-header .subtitle {
    font-size: 0.88rem;
    color: var(--text-light);
    font-weight: 400;
}

/* Control Panel Inputs */
.control-panel {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    margin-bottom: auto;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.control-group label {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--secondary-green);
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.styled-select {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background-color: var(--bg-card);
    color: var(--text-dark);
    font-family: var(--font-thai);
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    outline: none;
    transition: var(--transition);
    appearance: none;
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%233d6a50' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.75rem center;
    background-size: 1.2rem;
}

.styled-select:focus {
    border-color: var(--secondary-green);
    box-shadow: 0 0 0 3px rgba(61, 106, 80, 0.1);
}

/* System Status Info */
.system-status {
    background-color: rgba(253, 252, 249, 0.5);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 0.8rem;
    margin-bottom: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}

.status-indicator {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.88rem;
    font-weight: 600;
    color: var(--secondary-green);
}

.dot {
    width: 8px;
    height: 8px;
    background-color: #2b9348;
    border-radius: 50%;
    display: inline-block;
}

.pulse-green {
    box-shadow: 0 0 0 0 rgba(43, 147, 72, 0.5);
    animation: pulse 1.8s infinite;
}

@keyframes pulse {
    0% {
        transform: scale(0.95);
        box-shadow: 0 0 0 0 rgba(43, 147, 72, 0.7);
    }
    70% {
        transform: scale(1);
        box-shadow: 0 0 0 6px rgba(43, 147, 72, 0);
    }
    100% {
        transform: scale(0.95);
        box-shadow: 0 0 0 0 rgba(43, 147, 72, 0);
    }
}

.last-update {
    font-size: 0.78rem;
    color: var(--text-light);
}

.sidebar-footer {
    border-top: 1px solid var(--border-color);
    padding-top: 1rem;
    font-size: 0.75rem;
    color: var(--text-light);
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

/* Main Content Area */
.main-content {
    flex-grow: 1;
    padding: 2.2rem 2.5rem;
    height: 100vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1.8rem;
}

.content-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 1.2rem;
}

.title-section h2 {
    font-size: 1.7rem;
    margin-bottom: 0.2rem;
}

.title-section .date-indicator {
    font-size: 0.9rem;
    color: var(--text-light);
    font-family: var(--font-en);
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.65rem 1.2rem;
    border-radius: var(--radius-md);
    font-family: var(--font-thai);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    border: none;
    outline: none;
}

.btn-primary {
    background-color: var(--primary-green);
    color: #fff;
}

.btn-primary:hover {
    background-color: var(--secondary-green);
    transform: translateY(-1px);
}

.btn-secondary {
    background-color: var(--bg-sidebar);
    color: var(--primary-green);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background-color: var(--border-color);
    transform: translateY(-1px);
}

/* KPI Cards Grid */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 1rem;
}

.kpi-card {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1.2rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    box-shadow: var(--shadow);
    transition: var(--transition);
}

.kpi-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 24px rgba(36, 75, 53, 0.06);
    border-color: var(--accent-green);
}

.kpi-icon {
    font-size: 1.8rem;
    color: var(--secondary-green);
    background-color: rgba(61, 106, 80, 0.08);
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.kpi-info {
    display: flex;
    flex-direction: column;
}

.kpi-info h3 {
    font-size: 0.82rem;
    color: var(--text-light);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 0.2rem;
}

.kpi-value {
    font-size: 1.35rem;
    font-weight: 700;
    font-family: var(--font-en);
    color: var(--primary-green);
    line-height: 1.1;
    margin-bottom: 0.2rem;
}

.kpi-value .unit {
    font-size: 0.8rem;
    font-weight: 500;
    font-family: var(--font-thai);
    color: var(--text-light);
    margin-left: 2px;
}

.kpi-subtext {
    font-size: 0.72rem;
    color: var(--text-light);
}

/* Visualization Panel Grid */
.dashboard-visuals {
    display: grid;
    grid-template-columns: 1.4fr 1.1fr;
    gap: 1.8rem;
}

.visual-panel {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1.6rem;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 1.2rem;
}

.panel-header {
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.8rem;
}

.panel-header h3 {
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.2rem;
}

.panel-sub {
    font-size: 0.8rem;
    color: var(--text-light);
}

/* Map Container */
.map-container-wrapper {
    position: relative;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    overflow: hidden;
    height: 480px;
    background-color: #f1ebd8;
}

#map {
    width: 100%;
    height: 100%;
}

.map-legend {
    display: flex;
    gap: 1.5rem;
    font-size: 0.82rem;
    color: var(--text-dark);
    margin-top: 0.2rem;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.legend-dot.green {
    width: 8px;
    height: 8px;
    background-color: #1b7343;
    border-radius: 50%;
    display: inline-block;
    box-shadow: 0 0 5px rgba(27, 115, 67, 0.6);
}

.legend-boundary {
    width: 20px;
    height: 12px;
    border: 1.5px solid #2e5a44;
    background-color: rgba(124, 168, 138, 0.15);
    border-radius: 2px;
    display: inline-block;
}

/* Charts Panel Scrollable Container */
.charts-container-scroll {
    overflow-y: auto;
    height: 480px;
    padding-right: 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

/* Scrollbar customizations (Thin, matching theme) */
.charts-container-scroll::-webkit-scrollbar,
.main-content::-webkit-scrollbar,
.sidebar::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

.charts-container-scroll::-webkit-scrollbar-track,
.main-content::-webkit-scrollbar-track,
.sidebar::-webkit-scrollbar-track {
    background: transparent;
}

.charts-container-scroll::-webkit-scrollbar-thumb,
.main-content::-webkit-scrollbar-thumb,
.sidebar::-webkit-scrollbar-thumb {
    background-color: var(--border-color);
    border-radius: 3px;
}

.charts-container-scroll::-webkit-scrollbar-thumb:hover,
.main-content::-webkit-scrollbar-thumb:hover,
.sidebar::-webkit-scrollbar-thumb:hover {
    background-color: var(--secondary-green);
}

.chart-box {
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1.2rem;
    background-color: rgba(246, 243, 233, 0.3);
}

.chart-box h4 {
    font-size: 0.9rem;
    color: var(--secondary-green);
    margin-bottom: 0.8rem;
    font-weight: 600;
}

.chart-wrapper {
    position: relative;
    height: 220px;
    width: 100%;
}

.chart-grid-two {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.chart-grid-two .chart-wrapper {
    height: 180px;
}

.chart-notes {
    font-size: 0.72rem;
    color: var(--text-light);
    margin-top: 0.4rem;
    text-align: right;
}

/* Subdistrict Table Section */
.table-section {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1.6rem;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 1.2rem;
}

.table-section .panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.8rem;
}

.search-input {
    padding: 0.55rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background-color: var(--bg-green-read);
    font-family: var(--font-thai);
    font-size: 0.88rem;
    color: var(--text-dark);
    outline: none;
    width: 220px;
    transition: var(--transition);
}

.search-input:focus {
    border-color: var(--secondary-green);
    background-color: #fff;
    width: 280px;
}

.table-wrapper {
    overflow-x: auto;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
}

.styled-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    text-align: left;
}

.styled-table th {
    background-color: var(--bg-sidebar);
    color: var(--primary-green);
    font-weight: 700;
    padding: 0.85rem 1rem;
    border-bottom: 2px solid var(--border-color);
}

.styled-table td {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border-color);
    color: var(--text-dark);
}

.styled-table tbody tr:nth-of-type(even) {
    background-color: rgba(246, 243, 233, 0.2);
}

.styled-table tbody tr:last-of-type {
    border-bottom: none;
}

.styled-table tbody tr:hover {
    background-color: rgba(124, 168, 138, 0.08);
}

.styled-table th:nth-child(n+2),
.styled-table td:nth-child(n+2) {
    text-align: right;
    font-family: var(--font-en);
}

.styled-table th:last-child,
.styled-table td:last-child {
    font-family: var(--font-en);
}

/* Trend View Section */
.full-width {
    grid-column: span 2;
}

.trend-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1.5rem;
}

.trend-chart-wrapper {
    height: 300px;
    width: 100%;
}

/* Leaflet Custom Styles to Match Green Read Theme */
.leaflet-container {
    background: #f1ebd8 !important;
}

.leaflet-control-zoom a {
    background-color: var(--bg-card) !important;
    color: var(--primary-green) !important;
    border-color: var(--border-color) !important;
}

.leaflet-control-zoom a:hover {
    background-color: var(--bg-sidebar) !important;
}

.leaflet-bar {
    border: 1px solid var(--border-color) !important;
    box-shadow: var(--shadow) !important;
}

.leaflet-popup-content-wrapper {
    background-color: var(--bg-card) !important;
    color: var(--text-dark) !important;
    border-radius: var(--radius-md) !important;
    border: 1px solid var(--border-color) !important;
    font-family: var(--font-thai) !important;
    box-shadow: var(--shadow) !important;
}

.leaflet-popup-tip {
    background-color: var(--bg-card) !important;
    border: 1px solid var(--border-color) !important;
}

.map-popup-title {
    font-weight: 700;
    color: var(--primary-green);
    margin-bottom: 0.3rem;
    font-size: 0.95rem;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.2rem;
}

.map-popup-detail {
    font-size: 0.85rem;
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

.map-popup-detail span {
    font-family: var(--font-en);
    font-weight: 600;
}

/* General Utilities */
.hidden {
    display: none !important;
}

/* Responsiveness overrides */
@media (max-width: 1200px) {
    .kpi-grid {
        grid-template-columns: repeat(3, 1fr);
    }
    .dashboard-visuals {
        grid-template-columns: 1fr;
    }
    .full-width {
        grid-column: span 1;
    }
    .trend-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 900px) {
    .app-container {
        flex-direction: column;
    }
    .sidebar {
        width: 100%;
        height: auto;
        position: relative;
    }
    .main-content {
        height: auto;
        overflow-y: visible;
    }
}

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

/* ============================
   Subdistrict Detail Modal
   ============================ */

/* Clickable table row */
#subdistrict-table-body tr {
    cursor: pointer;
    transition: background 0.15s, transform 0.1s;
}
#subdistrict-table-body tr:hover {
    background: rgba(36, 75, 53, 0.07) !important;
    transform: translateX(3px);
}
#subdistrict-table-body tr:hover td:first-child strong {
    color: var(--primary);
    text-decoration: underline;
}

/* Overlay backdrop */
.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: rgba(20, 35, 25, 0.55);
    backdrop-filter: blur(4px);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    animation: fadeInOverlay 0.2s ease;
}
.modal-overlay.hidden {
    display: none;
}
@keyframes fadeInOverlay {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Modal box */
.modal-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 18px;
    box-shadow: 0 24px 60px rgba(20,35,25,0.3), 0 2px 8px rgba(20,35,25,0.1);
    max-width: 860px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    padding: 0;
    animation: slideUpModal 0.25s cubic-bezier(0.34,1.56,0.64,1);
}
@keyframes slideUpModal {
    from { transform: translateY(40px) scale(0.97); opacity: 0; }
    to   { transform: translateY(0) scale(1); opacity: 1; }
}

/* Modal header */
.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.2rem 1.5rem 1rem;
    border-bottom: 1px solid var(--border);
    background: linear-gradient(135deg, rgba(36,75,53,0.06), rgba(124,168,138,0.04));
    border-radius: 18px 18px 0 0;
}
.modal-title-area {
    display: flex;
    align-items: center;
    gap: 0.9rem;
}
.modal-title-area > i {
    font-size: 1.6rem;
    color: var(--primary);
    background: var(--accent-light);
    width: 44px; height: 44px;
    display: flex; align-items: center; justify-content: center;
    border-radius: 12px;
}
.modal-title-area h2 {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--primary);
    margin: 0;
}
.modal-subtitle {
    font-size: 0.82rem;
    color: var(--text-light);
    margin: 0;
    margin-top: 2px;
}
.modal-close-btn {
    background: none;
    border: 1.5px solid var(--border);
    color: var(--text-light);
    width: 36px; height: 36px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 1rem;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}
.modal-close-btn:hover {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
    transform: rotate(90deg);
}

/* KPI Strip */
.modal-kpi-strip {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 0;
    border-bottom: 1px solid var(--border);
}
.modal-kpi-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.2rem;
    padding: 1rem 0.5rem;
    text-align: center;
    border-right: 1px solid var(--border);
    transition: background 0.15s;
}
.modal-kpi-item:last-child { border-right: none; }
.modal-kpi-item i {
    font-size: 1rem;
    color: var(--secondary);
    margin-bottom: 2px;
}
.modal-kpi-val {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--primary);
    font-family: var(--font-en);
}
.modal-kpi-label {
    font-size: 0.68rem;
    color: var(--text-light);
    line-height: 1.2;
}

/* Derived metric cards */
.modal-derived-row {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.75rem;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
    background: rgba(36,75,53,0.02);
}
.modal-derived-card {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.75rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}
.modal-derived-label {
    font-size: 0.72rem;
    color: var(--text-light);
    font-weight: 500;
}
.modal-derived-val {
    font-size: 1rem;
    font-weight: 700;
    color: var(--secondary);
    font-family: var(--font-en);
}

/* Charts row */
.modal-charts-row {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.75rem;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
}
.modal-chart-card {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 0.75rem;
}
.modal-chart-title {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 0.5rem;
    display: flex;
    align-items: center;
    gap: 0.4rem;
}
.modal-chart-wrap {
    position: relative;
    height: 130px;
}

/* Documents bars */
.modal-docs-section {
    padding: 1rem 1.25rem 1.25rem;
}
.modal-doc-bar-row {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin-bottom: 0.45rem;
}
.modal-doc-bar-label {
    font-size: 0.75rem;
    color: var(--text);
    min-width: 120px;
    flex-shrink: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.modal-doc-bar-track {
    flex: 1;
    background: var(--border);
    border-radius: 99px;
    height: 8px;
    overflow: hidden;
}
.modal-doc-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary), var(--secondary));
    border-radius: 99px;
    transition: width 0.5s cubic-bezier(0.34,1.2,0.64,1);
}
.modal-doc-bar-count {
    font-size: 0.72rem;
    color: var(--text-light);
    font-family: var(--font-en);
    min-width: 40px;
    text-align: right;
}

@media (max-width: 700px) {
    .modal-kpi-strip { grid-template-columns: repeat(3, 1fr); }
    .modal-kpi-item { border-bottom: 1px solid var(--border); }
    .modal-charts-row { grid-template-columns: 1fr; }
    .modal-derived-row { grid-template-columns: 1fr; }
}
