/* ============================================================
   AP & Telangana Village Finder — UI styles
   ============================================================ */
:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #0f172a;
  --ink-2: #475569;
  --ink-3: #94a3b8;
  --line: #e6e8ec;
  --line-2: #eef1f4;
  --brand: #1f6feb;
  --brand-dk: #1858c2;
  --brand-soft: #eaf2ff;
  --ap: #1f6feb;     /* Andhra Pradesh accent */
  --tg: #0f9d58;     /* Telangana accent */
  --ka: #d97706;     /* Karnataka accent */
  --tn: #dc2626;     /* Tamil Nadu accent */
  --shadow: 0 1px 2px rgba(15,23,42,.06), 0 8px 24px rgba(15,23,42,.08);
  --shadow-sm: 0 1px 2px rgba(15,23,42,.08);
  --radius: 14px;
  --sidebar-w: 400px;
  font-synthesis: none;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button { font-family: inherit; cursor: pointer; }
a { color: var(--brand); }

#app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  transition: grid-template-columns .28s cubic-bezier(.4,0,.2,1);
}
#app.collapsed { grid-template-columns: 0 1fr; }

/* ---------------- Sidebar ---------------- */
#sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.side-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--line-2);
}
.brand {
  display: flex; align-items: center; gap: 11px; min-width: 0;
  text-decoration: none; color: inherit; cursor: pointer; border-radius: 12px;
}
.brand:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.brand-mark {
  font-size: 26px; line-height: 1; color: var(--brand);
  background: var(--brand-soft); border-radius: 11px;
  width: 42px; height: 42px; display: grid; place-items: center;
  transform: rotate(0deg); transition: box-shadow .15s, transform .15s;
}
/* the brand doubles as a "home" link back to the all-states landing page */
.brand:hover .brand-mark { box-shadow: 0 0 0 3px var(--brand-soft); transform: scale(1.05); }
.brand:hover h1 { color: var(--brand); }
.brand h1 { transition: color .15s; }
.brand h1 { font-size: 1.08rem; margin: 0; letter-spacing: -0.01em; }
.brand-sub { margin: 1px 0 0; font-size: .8rem; color: var(--ink-3); }

.icon-btn {
  border: 1px solid var(--line); background: #fff; color: var(--ink-2);
  width: 30px; height: 30px; border-radius: 9px; font-size: 18px; line-height: 1;
  display: grid; place-items: center; transition: .15s;
}
.icon-btn:hover { background: var(--bg); color: var(--ink); }

/* Header tools (language selector + collapse) */
.head-tools { display: flex; align-items: center; gap: 8px; }
.lang-wrap {
  display: flex; align-items: center; gap: 5px;
  border: 1px solid var(--line); border-radius: 9px; background: #fff;
  padding: 0 7px 0 8px; height: 30px; color: var(--ink-2); transition: .15s;
}
.lang-wrap:hover { background: var(--bg); color: var(--ink); }
.lang-ico { flex-shrink: 0; opacity: .8; }
#lang-select {
  border: none; background: transparent; color: inherit; cursor: pointer;
  font-family: inherit; font-size: .82rem; font-weight: 600;
  padding: 0 2px; outline: none; max-width: 92px;
}
#lang-select:focus-visible { outline: 2px solid var(--brand); border-radius: 4px; }

/* Search */
.search-wrap {
  position: relative; margin: 14px 16px 10px;
  display: flex; align-items: center;
}
.search-ico { position: absolute; left: 12px; color: var(--ink-3); pointer-events: none; }
#search {
  width: 100%; padding: 11px 36px 11px 38px;
  border: 1px solid var(--line); border-radius: 11px;
  font-size: .95rem; background: #fff; color: var(--ink); outline: none;
  transition: border-color .15s, box-shadow .15s;
}
#search::placeholder { color: var(--ink-3); }
#search:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
#clear-search { position: absolute; right: 6px; border: none; background: transparent; font-size: 20px; }

/* Segmented control */
.seg {
  display: flex; gap: 4px; margin: 0 16px 12px;
  background: var(--bg); padding: 4px; border-radius: 11px; border: 1px solid var(--line-2);
}
.seg button {
  flex: 1; border: none; background: transparent; color: var(--ink-2);
  padding: 7px 6px; border-radius: 8px; font-size: .82rem; font-weight: 600; transition: .15s;
  white-space: nowrap;
}
.seg button.active { background: #fff; color: var(--ink); box-shadow: var(--shadow-sm); cursor: default; }
.seg .seg-link {
  flex: 1; text-align: center; text-decoration: none; color: var(--ink-2);
  padding: 7px 6px; border-radius: 8px; font-size: .82rem; font-weight: 600;
  transition: .15s; white-space: nowrap; display: flex; align-items: center; justify-content: center; gap: 4px;
}
.seg .seg-link::after { content: "→"; opacity: .5; font-size: .85em; }
.seg .seg-link:hover { background: #fff; color: var(--brand); }

/* Breadcrumb */
.crumbs {
  display: flex; align-items: center; gap: 4px; flex-wrap: wrap;
  margin: 0 16px 8px; font-size: .82rem;
}
.crumb {
  border: none; background: transparent; color: var(--brand); font-weight: 600;
  padding: 3px 6px; border-radius: 7px; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.crumb:hover { background: var(--brand-soft); }
.crumb.active { color: var(--ink-2); font-weight: 700; pointer-events: none; }
.crumb-sep { color: var(--ink-3); font-size: .8rem; }

/* Panel (scrollable content area) */
.panel { flex: 1; overflow-y: auto; padding: 4px 8px 12px; min-height: 0; scrollbar-width: thin; }
.panel::-webkit-scrollbar { width: 8px; }
.panel::-webkit-scrollbar-thumb { background: #dfe3e8; border-radius: 8px; }

/* Stat cards */
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 6px 8px 10px; }
.stat {
  background: var(--bg); border: 1px solid var(--line-2); border-radius: 11px; padding: 11px 12px;
}
.stat .num { font-size: 1.32rem; font-weight: 750; letter-spacing: -0.02em; }
.stat .lab { font-size: .74rem; color: var(--ink-3); margin-top: 1px; text-transform: uppercase; letter-spacing: .04em; }
.stat.ap .num { color: var(--ap); }
.stat.tg .num { color: var(--tg); }
.stat.ka .num { color: var(--ka); }
.stat.tn .num { color: var(--tn); }

.section-label {
  font-size: .72rem; font-weight: 700; color: var(--ink-3); text-transform: uppercase; letter-spacing: .06em;
  padding: 10px 10px 6px; display: flex; justify-content: space-between; align-items: center;
}
.section-label .hint { font-weight: 600; text-transform: none; letter-spacing: 0; }

/* List rows (districts / mandals / villages / search results) */
.list { display: flex; flex-direction: column; gap: 2px; }
.row {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 10px; border: 1px solid transparent;
  background: #fff; text-align: left; width: 100%; transition: .12s;
}
.row:hover { background: var(--bg); border-color: var(--line-2); }
.row.clickable { cursor: pointer; }
.row .dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.row .main { min-width: 0; flex: 1; }
.row .name { font-size: .92rem; font-weight: 600; color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .meta { font-size: .78rem; color: var(--ink-3); margin-top: 1px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .count {
  font-size: .78rem; font-weight: 700; color: var(--ink-2);
  background: var(--bg); border: 1px solid var(--line-2); border-radius: 999px; padding: 2px 9px; flex-shrink: 0;
}
.row .chev { color: var(--ink-3); flex-shrink: 0; }
.row mark { background: #fde68a; color: inherit; border-radius: 3px; padding: 0 1px; }

.badge { font-size: .68rem; font-weight: 700; padding: 1px 6px; border-radius: 6px; }
.badge.ap { background: #e7f0ff; color: var(--ap); }
.badge.tg { background: #e3f6ec; color: var(--tg); }
.badge.rural { background: #eef2f7; color: var(--ink-2); }
.badge.urban { background: #fff1e6; color: #c2570f; }

.empty { padding: 26px 16px; text-align: center; color: var(--ink-3); font-size: .88rem; line-height: 1.5; }

.back-row {
  display: flex; align-items: center; gap: 8px; margin: 4px 8px 8px; padding: 8px 10px;
  border-radius: 10px; border: 1px solid var(--line-2); background: var(--bg); color: var(--ink-2);
  font-weight: 600; font-size: .86rem; width: calc(100% - 16px);
}
.back-row:hover { color: var(--ink); }

.detail-head { padding: 8px 10px 4px; }
.detail-head .title { font-size: 1.18rem; font-weight: 750; letter-spacing: -0.02em; line-height: 1.2; }
.detail-head .sub { font-size: .82rem; color: var(--ink-3); margin-top: 3px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }

.side-foot {
  border-top: 1px solid var(--line-2); padding: 10px 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.fresh { font-size: .74rem; color: var(--ink-3); }
.fresh b { color: var(--ink-2); font-weight: 700; }
.foot-links { display: flex; gap: 12px; font-size: .76rem; }
.foot-links a { color: var(--ink-3); text-decoration: none; }
.foot-links a:hover { color: var(--brand); text-decoration: underline; }

/* ---------------- Map ---------------- */
#map-wrap { position: relative; min-width: 0; }
#map { position: absolute; inset: 0; background: #eef1f4; }
.leaflet-container { font: inherit; background: #eef1f4; }

.floating {
  position: absolute; top: 14px; left: 14px; z-index: 500;
  box-shadow: var(--shadow); width: 34px; height: 34px;
}

.legend {
  position: absolute; left: 14px; bottom: 18px; z-index: 500;
  padding: 11px 13px; min-width: 180px;
}
.card { background: rgba(255,255,255,.96); backdrop-filter: blur(6px); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); }
.legend-title { font-size: .74rem; font-weight: 700; color: var(--ink-2); margin-bottom: 7px; text-transform: uppercase; letter-spacing: .04em; }
.legend-scale { display: flex; flex-direction: column; gap: 3px; }
.legend-row { display: flex; align-items: center; gap: 8px; font-size: .76rem; color: var(--ink-2); }
.legend-swatch { width: 16px; height: 12px; border-radius: 3px; border: 1px solid rgba(0,0,0,.08); }

.map-loading {
  position: absolute; inset: 0; display: grid; place-items: center; z-index: 600;
  background: #eef1f4; color: var(--ink-3); font-size: .9rem; transition: opacity .4s; pointer-events: none;
}
.map-loading.hidden { opacity: 0; }

.toast {
  position: absolute; top: 14px; left: 50%; transform: translateX(-50%); z-index: 700;
  background: var(--ink); color: #fff; padding: 9px 16px; border-radius: 999px; font-size: .84rem;
  box-shadow: var(--shadow); transition: opacity .25s, transform .25s;
}
.toast.hidden { opacity: 0; transform: translateX(-50%) translateY(-8px); pointer-events: none; }

/* Leaflet polygon hover tooltip */
.region-tip {
  background: var(--ink); color: #fff; border: none; border-radius: 8px;
  padding: 6px 10px; font-size: .8rem; font-weight: 600; box-shadow: var(--shadow);
}
.region-tip::before { border-top-color: var(--ink); }
.region-tip .tip-sub { font-weight: 500; opacity: .75; font-size: .74rem; }

/* Map markers (fallback when no mandal polygons) */
.bubble {
  background: var(--brand); color: #fff; border: 2px solid #fff;
  border-radius: 999px; box-shadow: var(--shadow-sm);
  display: grid; place-items: center; font-size: .68rem; font-weight: 700;
}

/* Selected village row */
.row.selected { border-color: var(--brand); background: var(--brand-soft); }
.row.selected .name { color: var(--brand-dk); }

/* Village map pin + popup */
.village-pin {
  display: block; width: 16px; height: 16px;
  background: var(--brand); border: 2px solid #fff;
  border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
  box-shadow: 0 2px 6px rgba(15,23,42,.35);
}
.village-popup .leaflet-popup-content-wrapper { border-radius: 12px; box-shadow: var(--shadow); }
.village-popup .leaflet-popup-content { margin: 11px 13px; }
.vpop-name { font-weight: 750; font-size: 1rem; letter-spacing: -0.01em; }
.vpop-meta { color: var(--ink-3); font-size: .8rem; margin-top: 2px; }
.vpop-tags { display: flex; gap: 6px; align-items: center; margin-top: 9px; }
.vpop-code { font-size: .73rem; color: var(--ink-2); background: var(--bg);
             border: 1px solid var(--line-2); border-radius: 999px; padding: 1px 8px; }
.vpop-note { margin-top: 9px; font-size: .72rem; color: var(--ink-3); line-height: 1.4; }

/* Nearby services (live OpenStreetMap lookup) inside the village popup */
.vpop-nb-btn {
  margin-top: 11px; width: 100%;
  border: 1px solid var(--brand); background: var(--brand-soft); color: var(--brand-dk);
  font-weight: 700; font-size: .82rem; padding: 8px 10px; border-radius: 9px; transition: .15s;
}
.vpop-nb-btn:hover { background: var(--brand); color: #fff; }
.vpop-nb-btn.loading { opacity: .65; cursor: default; }
.vpop-nb { margin-top: 8px; max-height: 232px; overflow-y: auto; scrollbar-width: thin; }
.nb-status { font-size: .8rem; color: var(--ink-3); padding: 8px 2px; text-align: center; line-height: 1.4; }
button.nb-retry {
  width: 100%; border: 1px dashed var(--line); background: #fff; color: var(--ink-2); border-radius: 9px;
}
button.nb-retry:hover { color: var(--brand); border-color: var(--brand); }
.nb-group { margin-top: 8px; }
.nb-group-head {
  font-size: .7rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
  color: var(--ink-3); padding: 2px 2px 4px;
}
.nb-item {
  display: block; text-decoration: none; color: inherit;
  padding: 6px 8px; border-radius: 8px; border: 1px solid var(--line-2);
  background: #fff; margin-bottom: 4px; transition: .12s;
}
.nb-item:hover { background: var(--bg); border-color: var(--line); }
.nb-item-name { display: block; font-size: .85rem; font-weight: 600; color: var(--ink); }
.nb-item-meta { display: block; font-size: .74rem; color: var(--ink-3); margin-top: 1px; }
.nb-src { display: inline-block; margin-top: 8px; font-size: .7rem; color: var(--ink-3); text-decoration: none; }
.nb-src:hover { text-decoration: underline; color: var(--brand); }

.hidden { display: none !important; }

/* ---------------- RTL (Urdu) ----------------
   We flip text direction inside the sidebar only, so the overall
   sidebar↔map layout and the map controls stay put. */
#sidebar[dir="rtl"] .row,
#sidebar[dir="rtl"] .detail-head,
#sidebar[dir="rtl"] .stat,
#sidebar[dir="rtl"] .empty { text-align: right; }
#sidebar[dir="rtl"] .search-wrap { direction: rtl; }
#sidebar[dir="rtl"] #search { padding: 11px 38px 11px 36px; }
#sidebar[dir="rtl"] .search-ico { left: auto; right: 12px; }
#sidebar[dir="rtl"] #clear-search { right: auto; left: 6px; }
#sidebar[dir="rtl"] .row .chev,
#sidebar[dir="rtl"] .back-row { transform: scaleX(-1); }
#sidebar[dir="rtl"] .back-row > * { transform: scaleX(-1); }
.village-popup .vpop[dir="rtl"] { text-align: right; }
.village-popup .vpop[dir="rtl"] .vpop-tags { flex-direction: row-reverse; justify-content: flex-end; }
.village-popup .vpop[dir="rtl"] .nb-group-head,
.village-popup .vpop[dir="rtl"] .nb-item,
.village-popup .vpop[dir="rtl"] .nb-src { text-align: right; }

/* ---------------- Responsive ---------------- */
@media (max-width: 820px) {
  :root { --sidebar-w: 100%; }
  #app { grid-template-columns: 1fr; grid-template-rows: 1fr; }
  #sidebar {
    position: absolute; inset: 0; z-index: 1000; border-right: none;
    transition: transform .28s cubic-bezier(.4,0,.2,1);
  }
  #app.collapsed #sidebar { transform: translateY(calc(100% - 132px)); border-top: 1px solid var(--line); border-radius: 18px 18px 0 0; box-shadow: 0 -8px 30px rgba(0,0,0,.12); }
  #app.collapsed { grid-template-columns: 1fr; }
  #map-wrap { z-index: 1; }
  .floating { display: none !important; }
}
