*{box-sizing:border-box;margin:0;padding:0}
html{height:100%;height:-webkit-fill-available}
body{
  height:100%;height:-webkit-fill-available;
  background:#f0f2f5;font-family:'Sarabun',sans-serif;
  display:flex;flex-direction:column;overflow:hidden;
}
header{
  background:#fff;border-bottom:2px solid #e5e7eb;padding:10px 20px;
  display:flex;align-items:center;gap:12px;
  box-shadow:0 2px 8px rgba(0,0,0,0.06);flex-shrink:0;
}
header h1{font-family:'Kanit',sans-serif;font-size:1rem;font-weight:800;color:#1a2332;}
header p{color:#6b7280;font-size:0.78rem;margin-top:1px;}

.layout {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  height: 0; /* ✅ เพิ่มบรรทัดนี้ */
}

.map-area{
  flex:1;position:relative;overflow:hidden;
  background:#ede8f2;
  background-image:linear-gradient(rgba(255,255,255,0.5) 1px,transparent 1px),linear-gradient(90deg,rgba(255,255,255,0.5) 1px,transparent 1px);
  background-size:30px 30px;min-width:0;min-height:0;
}
#map-svg{display:block;width:100%;height:100%;cursor:grab;touch-action:none;}
#map-svg:active{cursor:grabbing;}

.zoom-controls{position:absolute;bottom:16px;right:16px;display:flex;flex-direction:column;gap:2px;z-index:20;}
.zoom-btn{
  width:40px;height:40px;background:#fff;border:1px solid #e5e7eb;border-radius:10px;
  font-size:1rem;cursor:pointer;display:flex;align-items:center;justify-content:center;
  box-shadow:0 2px 8px rgba(0,0,0,0.12);transition:background 0.15s,transform 0.1s;
  color:#374151;user-select:none;-webkit-user-select:none;
}
.zoom-btn:hover{background:#f3f4f6;transform:scale(1.08);}
.zoom-btn:active{transform:scale(0.92);}
.zoom-hint{
  position:absolute;bottom:16px;left:16px;background:rgba(255,255,255,0.88);
  border:1px solid #e5e7eb;border-radius:8px;padding:4px 6px;
  font-size:0.72rem;color:#6b7280;backdrop-filter:blur(4px);pointer-events:none;z-index:20;
}

.province{stroke:#fff;stroke-width:0.8;stroke-linejoin:round;cursor:pointer;transition:filter 0.18s;}
.province:hover{stroke-width:1.8;filter:brightness(1.2) drop-shadow(0 1px 4px rgba(46, 45, 45, 0.2));}
.province.selected{stroke:#1a2332;stroke-width:2.2;filter:brightness(1.25) drop-shadow(0 0 6px rgba(0,0,0,0.3));}

.prov-label{
  font-family:'Sarabun',sans-serif;fill:#fff;text-anchor:middle;
  pointer-events:none;font-weight:700;paint-order:stroke fill;stroke:rgba(0,0,0,0.7);stroke-width:2px;
}
@media(max-width:640px){
  .prov-label{ stroke-width:2.5px; }
}
.sidebar {
  width: 280px;
  flex-shrink: 0;
  background: #fff;
  border-left: 1px solid #e5e7eb;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* ✅ คำนวณความสูงจาก viewport โดยตรง ไม่พึ่ง parent */
  height: 100vh;
  max-height: 100vh;
}

.sidebar-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.sidebar-section { padding: 16px 18px; border-bottom: 1px solid #f3f4f6; }
.sidebar-section h2 {
  font-family: 'Kanit', sans-serif; font-size: 0.7rem; font-weight: 600;
  letter-spacing: 0.12em; text-transform: uppercase; color: #9ca3af; margin-bottom: 12px;
}
.prov-placeholder{color:#9ca3af;font-size:0.85rem;text-align:center;padding:12px 0;}
.prov-th{font-family:'Kanit',sans-serif;font-size:1.6rem;font-weight:800;line-height:1;color:#1a2332;}
.prov-en{font-size:0.82rem;color:#6b7280;margin:3px 0 12px;}
.info-row{display:flex;justify-content:space-between;align-items:center;padding:8px 0;border-bottom:1px solid #f3f4f6;font-size:0.85rem;}
.info-row:last-child{border-bottom:none;}
.info-lbl{color:#6b7280;}.info-val{font-weight:600;color:#1a2332;}
.region-tag{display:inline-block;margin-top:10px;padding:3px 12px;border-radius:99px;font-size:0.76rem;font-weight:600;color:#fff;}

.legend-row{display:flex;align-items:center;gap:10px;padding:6px 0;cursor:pointer;font-size:0.85rem;color:#374151;transition:opacity 0.2s;}
.legend-row:hover{opacity:0.7;}
.legend-swatch{width:16px;height:16px;border-radius:4px;flex-shrink:0;box-shadow:0 1px 3px rgba(0,0,0,0.15);}
.legend-count{margin-left:auto;background:#f3f4f6;color:#6b7280;font-size:0.73rem;padding:2px 7px;border-radius:99px;}

#tooltip{
  position:fixed;background:rgba(26,35,50,0.94);color:#fff;border-radius:8px;
  padding:7px 12px;font-size:0.6rem;pointer-events:none;opacity:0;transition:opacity 0.12s;
  z-index:9999;backdrop-filter:blur(4px);border:1px solid rgba(255,255,255,0.1);white-space:nowrap;
}
#tooltip.on{opacity:1;}

/* ── DISTRICT POPUP CARD ── */
#dist-popup {
  display: none;
  position: fixed;
  z-index: 5000;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.22);
  width: 300px;
  max-height: 80vh;
  overflow: hidden;
  flex-direction: column;
  font-family: 'Sarabun', sans-serif;
  animation: popIn 0.18s ease;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  cursor: default;
  user-select: none;
}

@keyframes popIn { from{opacity:0;transform:scale(0.92)} to{opacity:1;transform:scale(1)} }

#dist-popup.on { display: flex; } /* ✅ เปลี่ยนจาก block เป็น flex */

#dist-popup.dragging {
  box-shadow: 0 12px 48px rgba(0,0,0,0.32);
  opacity: 0.95;
}

#dist-popup .pop-img-wrap {
  width: 100%; height: 160px; background: #e8edf2;
  overflow: hidden; position: relative;
  flex-shrink: 0;
  cursor: grab;
}

#dist-popup .pop-img-wrap:active {
  cursor: grabbing;
}
#dist-popup .pop-img-wrap img {
  width: 100%; height: 100%; object-fit: cover; display: block;
}
#dist-popup .pop-img-wrap .pop-no-img {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  flex-direction: column; gap: 6px; color: #9ca3af; font-size: 0.8rem;
}

#dist-popup .pop-body {
  padding: 14px 16px 16px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}
#dist-popup .pop-amp-th { font-family: 'Kanit',sans-serif; font-size: 0.8rem; font-weight: 800; color: #1a2332; line-height: 1.2; }
#dist-popup .pop-amp-en { font-size: 0.78rem; color: #6b7280; margin: 2px 0 10px; }
#dist-popup .pop-row { display: flex; justify-content: space-between; font-size: 0.82rem; padding: 5px 0; border-bottom: 1px solid #f3f4f6; }
#dist-popup .pop-row:last-child { border-bottom: none; }
#dist-popup .pop-lbl { color: #9ca3af; }
#dist-popup .pop-val { font-weight: 600; color: #374151; }

#dist-popup .pop-close {
  position: absolute; top: 10px; right: 10px; width: 28px; height: 28px;
  background: rgba(0,0,0,0.45); border: none; border-radius: 50%;
  color: #fff; font-size: 1rem; cursor: pointer; display: flex; align-items: center; justify-content: center;
  z-index: 10; line-height: 1;
}
#dist-popup .pop-close:hover { background: rgba(0,0,0,0.65); }

#dist-popup .pop-img-nav {
  position: absolute; bottom: 8px; right: 8px; display: flex; gap: 4px;
}
#dist-popup .pop-img-nav button {
  width: 24px; height: 24px; border-radius: 50%; border: none;
  background: rgba(0,0,0,0.5); color: #fff; font-size: 0.8rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
#dist-popup .pop-img-nav button:hover { background: rgba(0,0,0,0.75); }

#dist-popup .pop-img-counter {
  position: absolute; bottom: 10px; left: 10px;
  background: rgba(0,0,0,0.5); color: #fff;
  font-size: 0.7rem; padding: 2px 7px; border-radius: 99px;
}

/* district info card */
.dist-name-th{font-family:'Kanit',sans-serif;font-size:1.4rem;font-weight:800;line-height:1;color:#1a2332;}
.dist-name-en{font-size:0.8rem;color:#6b7280;margin:3px 0 12px;}
.dist-placeholder{color:#9ca3af;font-size:0.82rem;text-align:center;padding:10px 0;}
#tooltip .tt-th{font-family:'Kanit',sans-serif;font-size:0.95rem;font-weight:700;}
#tooltip .tt-sub{font-size:0.73rem;opacity:0.7;margin-top:1px;}

/* ── FORM CONTROLS ── */
.form-label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  color: #6b7280;
  margin-bottom: 5px;
}
.form-control {
  width: 100%;
  padding: 8px 10px;
  border: 1.5px solid #e5e7eb;
  border-radius: 8px;
  font-family: 'Sarabun', sans-serif;
  font-size: 0.85rem;
  color: #1a2332;
  background: #f9fafb;
  outline: none;
  transition: border-color 0.2s;
  appearance: auto;
}
.form-control:focus { border-color: #6366f1; background: #fff; }

.search-wrap{position:relative;margin-bottom:4px;}
.search-wrap input{
  width:100%;padding:8px 10px 8px 32px;border:1.5px solid #e5e7eb;border-radius:8px;
  font-family:'Sarabun',sans-serif;font-size:0.85rem;color:#1a2332;
  outline:none;transition:border-color 0.2s;background:#f9fafb;
}
.search-wrap input:focus{border-color:#6366f1;background:#fff;}
.search-icon{position:absolute;left:9px;top:50%;transform:translateY(-50%);color:#9ca3af;font-size:0.85rem;pointer-events:none;}
.search-drop{
  position:absolute;top:calc(100% + 4px);left:0;right:0;background:#fff;
  border:1px solid #e5e7eb;border-radius:8px;max-height:200px;overflow-y:auto;
  z-index:200;display:none;box-shadow:0 8px 24px rgba(0,0,0,0.1);
}
.search-drop.open{display:block;}
.search-item{padding:8px 12px;font-size:0.85rem;cursor:pointer;border-bottom:1px solid #f3f4f6;transition:background 0.12s;display:flex;align-items:center;gap:8px;}
.search-item:last-child{border-bottom:none;}
.search-item:hover{background:#f5f3ff;}
.si-dot{width:10px;height:10px;border-radius:2px;flex-shrink:0;}

/* ── DISTRICT MODE ── keep sidebar, just fill map-area */
body.district-mode .zoom-hint{ display:none; }

/* loading overlay */
#district-loading{
  display:none;position:fixed;inset:0;z-index:9000;
  background:rgba(15,23,42,0.72);backdrop-filter:blur(4px);
  flex-direction:column;align-items:center;justify-content:center;gap:18px;
  color:#fff;font-family:'Kanit',sans-serif;
}
#district-loading.on{ display:flex; }
.loader-ring{
  width:56px;height:56px;border-radius:50%;
  border:5px solid rgba(255,255,255,0.15);
  border-top-color:#fff;
  animation:spin 0.9s linear infinite;
}
@keyframes spin{ to{transform:rotate(360deg)} }
#district-loading p{ font-size:1.1rem;font-weight:600;opacity:0.9; }
#district-loading small{ font-family:'Sarabun',sans-serif;font-size:0.8rem;opacity:0.55; }

/* ── TABLET LARGE (769–1024px) ── */
@media(max-width:1024px){
  .sidebar{ width:280px; }
  .prov-th{ font-size:1.35rem; }
  #dist-popup{ width:280px; }
}

/* ── TABLET SMALL (641–768px) ── */
@media(max-width:768px){
  .sidebar{ width:220px; }
  .sidebar-section{ padding:12px 14px; }
  .prov-th{ font-size:1.2rem; }
  .info-row{ font-size:0.8rem; }
  #dist-popup{ width:260px; }
  #dist-popup .pop-img-wrap{ height:130px; }
  .zoom-controls{ right:10px; bottom:10px; }
  .zoom-btn{ width:34px; height:34px; font-size:0.9rem; }
}

/* ── MOBILE (≤640px) ── bottom sheet layout ── */
@media(max-width:640px){
  header{ padding:8px 14px; }
  header h1{ font-size:0.95rem; }
  header p{ display:none; }

  .layout{ flex-direction:column; position:relative; }
  .map-area{ flex:1; min-height:0; }

  /* sidebar เป็น bottom sheet */
  .sidebar{
    position:fixed; bottom:0; left:0; right:0; z-index:300;
    width:100%; height:60vh;
    border-left:none; border-top:none;
    border-radius:20px 20px 0 0;
    box-shadow:0 -4px 24px rgba(0,0,0,0.15);
    flex-direction:column; overflow:hidden;
    transform:translateY(calc(100% - 52px));
    transition:transform 0.32s cubic-bezier(0.4,0,0.2,1);
    background:#fff;
  }
  .sidebar.open{ transform:translateY(0); }

  /* drag handle */
  .sidebar::before{
    content:'';display:block;width:40px;height:4px;
    background:#d1d5db;border-radius:2px;
    margin:10px auto 4px;flex-shrink:0;
  }

  /* peek bar */
  .sidebar-peek{
    display:flex;align-items:center;justify-content:space-between;
    padding:0 18px 10px;font-family:'Kanit',sans-serif;
    font-size:0.85rem;font-weight:700;color:#374151;flex-shrink:0;cursor:pointer;
  }
  .sidebar-peek-arrow{ transition:transform 0.28s; font-size:0.75rem; color:#9ca3af; }
  .sidebar.open .sidebar-peek-arrow{ transform:rotate(180deg); }

  .sidebar-scroll{ overflow-y:auto; flex:1; min-height:0; }

  .sidebar-section{ padding:12px 16px; }
  .sidebar-section h2{ font-size:0.3rem; margin-bottom:8px; }
  .prov-th{ font-size:1.15rem; }
  .info-row{ font-size:0.82rem; padding:6px 0; }
  .zoom-hint{ display:none; }
  .zoom-controls{ bottom:64px; right:12px; }
  .zoom-btn{ width:20px; height:20px; }

  /* popup → full-width bottom sheet on mobile */
  #dist-popup{
    width:100% !important;
    left:0 !important; right:0 !important;
    bottom:0 !important; top:auto !important;
    transform:none !important;
    border-radius:20px 20px 0 0;
    max-height:75vh;
    animation:slideUp 0.22s ease;
  }
  @keyframes slideUp{ from{transform:translateY(100%) !important} to{transform:translateY(0) !important} }
  #dist-popup .pop-img-wrap{ height:180px; cursor:default; }

  #btn-back{
    top:auto !important; bottom:calc(52px + 12px);
    left:12px !important; font-size:0.8rem; padding:6px 12px;
  }
  #district-count{
    top:auto !important; bottom:calc(52px + 12px);
    left:50% !important; transform:translateX(-50%) !important;
  }

  .form-control{ font-size:1rem; padding:10px 12px; }
}

/* ── SMALL MOBILE (≤380px) ── */
@media(max-width:380px){
  .sidebar{ height:65vh; }
  #dist-popup{ max-height:80vh; }
  #dist-popup .pop-img-wrap{ height:140px; }
  .prov-th{ font-size:1rem; }
  .dist-name-th{ font-size:1.1rem; }
}