:root {
    --green-900: #1f4a1c;
    --green-700: #2f6b2a;
    --green-600: #3b8034;
    --green-100: #e8f3e5;
    --text: #1d2420;
    --muted: #5d6a61;
    --border: #dde5dc;
    --bg: #f6f8f5;
    --surface: #ffffff;
    --radius: 12px;
    --shadow: 0 6px 24px rgba(20, 40, 20, .12);
    --topbar-h: 64px;
    --sidebar-w: 340px;
    color-scheme: light;
}

*,
*::before,
*::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}

svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }

.visually-hidden {
    position: absolute !important;
    width: 1px; height: 1px;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

.skip-link {
    position: absolute; left: 8px; top: -60px; z-index: 2000;
    background: var(--green-700); color: #fff; padding: 8px 12px; border-radius: 8px;
}
.skip-link:focus { top: 8px; }

:focus-visible { outline: 3px solid #f2b705; outline-offset: 2px; }

/* ---------- Layout ---------- */
.app {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    grid-template-rows: var(--topbar-h) 1fr;
    grid-template-areas: "top top" "side map";
    height: 100vh;
    height: 100dvh;
}

.topbar {
    grid-area: top;
    display: flex; align-items: center; gap: 12px;
    padding: 0 16px;
    padding-left: max(16px, env(safe-area-inset-left));
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    z-index: 1100;
}
.brand-logo { height: 44px; width: auto; }
.brand-text h1 { margin: 0; font-size: 1.125rem; line-height: 1.2; color: var(--green-900); }
.brand-text p { margin: 0; font-size: .8rem; color: var(--muted); }

.icon-btn {
    display: none;
    align-items: center; justify-content: center;
    width: 42px; height: 42px; flex: none;
    border: 1px solid var(--border); border-radius: 10px;
    background: var(--surface); color: var(--text); cursor: pointer;
}

.sidebar {
    grid-area: side;
    display: flex; flex-direction: column; gap: 12px;
    padding: 16px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    overflow: hidden;
    z-index: 1050;
}

.map-wrap { grid-area: map; position: relative; min-height: 0; }
#map { position: absolute; inset: 0; background: #e6ebe4; }

/* ---------- Sidebar contents ---------- */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat {
    background: var(--green-100); border-radius: var(--radius);
    padding: 12px; display: flex; flex-direction: column;
}
.stat-value { font-size: 1.4rem; font-weight: 700; color: var(--green-900); font-variant-numeric: tabular-nums; }
.stat-label { font-size: .78rem; color: var(--muted); }

.search {
    display: flex; align-items: center; gap: 8px;
    border: 1px solid var(--border); border-radius: 10px;
    padding: 0 12px; background: var(--bg); color: var(--muted);
}
.search:focus-within { border-color: var(--green-600); background: #fff; }
.search input {
    flex: 1; border: 0; background: transparent; outline: none;
    padding: 11px 0; font: inherit; font-size: 16px; color: var(--text); min-width: 0;
}

.sort-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: .85rem; color: var(--muted); }
.sort-row select {
    font: inherit; font-size: .85rem; padding: 6px 8px;
    border: 1px solid var(--border); border-radius: 8px; background: #fff; color: var(--text);
}

.desa-list {
    list-style: none; margin: 0; padding: 0 2px 0 0;
    overflow-y: auto; flex: 1; min-height: 0;
    overscroll-behavior: contain;
}
.desa-list li + li { margin-top: 6px; }
.list-info { padding: 16px 4px; color: var(--muted); font-size: .9rem; text-align: center; }

.desa-item {
    width: 100%; text-align: left; cursor: pointer;
    display: flex; align-items: center; justify-content: space-between; gap: 10px;
    padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px;
    background: #fff; font: inherit; color: inherit;
    transition: background-color .15s, border-color .15s;
}
.desa-item:hover { background: var(--green-100); border-color: #c4dcbf; }
.desa-item[aria-current="true"] { background: var(--green-100); border-color: var(--green-600); }
.desa-item .nama { font-weight: 600; display: block; }
.desa-item .kades { font-size: .8rem; color: var(--muted); display: block; }
.desa-item .jiwa {
    flex: none; font-size: .78rem; font-weight: 600; color: var(--green-900);
    background: var(--green-100); padding: 3px 8px; border-radius: 999px; font-variant-numeric: tabular-nums;
}

/* ---------- Popup & tooltip ---------- */
.leaflet-popup-content-wrapper { border-radius: var(--radius); box-shadow: var(--shadow); }
.leaflet-popup-content { margin: 14px 16px; }
.popup-custom { min-width: 220px; max-width: 280px; font-family: inherit; }
.popup-custom h3 {
    margin: 0 0 8px; padding-bottom: 6px;
    color: var(--green-900); font-size: 1.05rem;
    border-bottom: 1px solid var(--border);
}
.popup-custom dl { margin: 0; }
.popup-custom dt { font-size: .75rem; color: var(--muted); margin-top: 8px; }
.popup-custom dd { margin: 2px 0 0; font-size: .9rem; font-weight: 600; overflow-wrap: anywhere; }

.btn, .btn-web {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 9px 14px; border-radius: 8px; border: 0; cursor: pointer;
    background: var(--green-700); color: #fff !important; font: inherit; font-weight: 600;
    text-decoration: none; transition: background-color .2s;
}
.btn:hover, .btn-web:hover { background: var(--green-900); }
.btn-web { display: flex; width: 100%; margin-top: 12px; }

.desa-tooltip {
    background: rgba(255, 255, 255, .96);
    border: 1px solid var(--border); border-radius: 6px;
    color: var(--text); font-size: 12px; font-weight: 600;
    padding: 4px 8px; box-shadow: 0 2px 6px rgba(0, 0, 0, .15);
}

.leaflet-control-reset a { display: flex !important; align-items: center; justify-content: center; color: var(--text); }
.leaflet-control-reset svg { width: 16px; height: 16px; }

/* ---------- Notice ---------- */
.notice {
    position: absolute; left: 50%; top: 16px; transform: translateX(-50%);
    z-index: 1000; width: min(92%, 420px);
    background: #fff; border-left: 4px solid #c0392b; border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 14px 16px;
    display: flex; align-items: center; gap: 12px; justify-content: space-between;
}
.notice[hidden] { display: none; }
.notice p { margin: 0; font-size: .9rem; }

.noscript { position: fixed; inset: auto 0 0 0; margin: 0; padding: 12px; background: #c0392b; color: #fff; text-align: center; z-index: 3000; }

.backdrop { display: none; }

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
    :root { --topbar-h: 58px; }

    .app { grid-template-columns: 1fr; grid-template-areas: "top" "map"; }

    .icon-btn { display: inline-flex; }
    .brand-logo { height: 38px; }
    .brand-text h1 { font-size: 1rem; }

    .sidebar {
        position: fixed;
        top: var(--topbar-h); bottom: 0; left: 0;
        width: min(88vw, var(--sidebar-w));
        padding-bottom: max(16px, env(safe-area-inset-bottom));
        transform: translateX(-105%);
        transition: transform .25s ease;
        box-shadow: var(--shadow);
        visibility: hidden;
    }
    .sidebar.is-open { transform: translateX(0); visibility: visible; }

    .backdrop {
        display: block; position: fixed; inset: var(--topbar-h) 0 0 0;
        background: rgba(0, 0, 0, .35); z-index: 1040;
    }
    .backdrop[hidden] { display: none; }
}

@media (max-width: 420px) {
    .brand-text p { display: none; }
    .popup-custom { min-width: 0; max-width: 230px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}
