/* =====================================================================
 * tamroypao_map/css/map.css — หน้าแผนที่ v2
 * โทน/ฟอนต์เดียวกับ dashboard (Prompt, เขียว #2f7d32) · ตัวอักษรขั้นต่ำ 15px บนมือถือ · ปุ่มแตะ ≥ 44px
 * โครง: desktop grid 2 คอลัมน์ · ≤768px แผนที่เต็มจอ + แผงเป็น bottom sheet (body[data-sheet=peek|half|full])
 * ===================================================================== */
:root{--ink:#1d1d1b;--ink2:#5c5c57;--muted:#8a8a84;--line:#e4e4df;--bg:#f2f2ef;--card:#fff;--g:#2f7d32;--r:#d63b2f;--gold:#ffe066;--panel-w:400px;--top-h:56px}
*{box-sizing:border-box}
html,body{height:100%;margin:0}
body{font-family:Prompt,'Sarabun',system-ui,sans-serif;font-weight:300;font-size:14px;color:var(--ink);background:var(--bg);overflow:hidden}
b,strong,th,.title{font-weight:500}

/* ---------- แถบบน ---------- */
.topbar{height:var(--top-h);display:flex;align-items:center;gap:12px;padding:0 14px;background:#fff;border-bottom:1px solid var(--line);position:relative;z-index:1050}
.topbar .brand img{height:32px;width:auto;display:block}
.topbar .title{font-size:18px;font-weight:600;white-space:nowrap}
.topbar .title .bi{color:var(--r)}
.topbar .chip{display:inline-flex;align-items:center;gap:6px;border:1px solid var(--line);border-radius:999px;padding:4px 12px;font-size:13px;white-space:nowrap}
.topbar .chip.period{background:#eaf5ea;border-color:#b9dcb9;color:#1f5a22;font-weight:500;margin-left:auto}
.topbar .chip.visitors{color:var(--ink2)}
.topbar .actions{display:flex;gap:6px}
.topbar .btn-sm{font-family:inherit;font-weight:400;border-radius:8px;min-height:36px}

/* ---------- โครง ---------- */
.layout{height:calc(100% - var(--top-h));display:grid;grid-template-columns:1fr var(--panel-w)}
.mapwrap{position:relative;min-height:0}
#map{position:absolute;inset:0;background:#242424}
.panel{background:var(--bg);border-left:1px solid var(--line);overflow-y:auto;padding:12px;display:flex;flex-direction:column;gap:12px}
.sheet-handle,.peek{display:none}

/* ---------- การ์ดในแผง ---------- */
.card{background:var(--card);border:1px solid var(--line);border-radius:14px;padding:14px;box-shadow:0 1px 3px rgba(0,0,0,.04)}
.card h4{margin:0 0 10px;font-size:15px;font-weight:600;display:flex;align-items:center;gap:8px;flex-wrap:wrap}
.card h4 .sub{font-weight:300;color:var(--ink2);font-size:13px}
.card h4 .btn-xs{margin-left:auto;font-size:12px;padding:2px 8px;font-family:inherit}
.seg{display:flex;border:1px solid var(--line);border-radius:10px;overflow:hidden}
.seg button{flex:1;border:0;background:#fff;padding:9px 6px;font:inherit;font-size:14px;color:var(--ink2);cursor:pointer;min-height:40px}
.seg button.on{background:var(--g);color:#fff;font-weight:500}
.row-sel{display:flex;align-items:center;gap:10px;margin-top:10px}
.row-sel label{font-size:13px;color:var(--ink2);min-width:44px}
.row-sel .form-select{font-family:inherit;font-size:15px;border-radius:10px;min-height:42px}
.chips{display:flex;flex-wrap:wrap;gap:6px;margin-top:10px}
.chips button{border:1px solid var(--line);background:#fff;border-radius:8px;padding:6px 12px;font:inherit;font-size:14px;color:var(--ink2);cursor:pointer;min-height:36px}
.chips button.on{background:var(--ink);color:#fff;border-color:var(--ink);font-weight:500}

/* KPI */
.kstack{display:flex;flex-direction:column;gap:8px;min-width:0}        /* กล่อง KPI เรียงลงล่าง: กลุ่มเกษตร → กลุ่มป่า → รวม (เดิมใช้ grid 2 คอลัมน์ ทำให้กลุ่มล้นขอบแผงบน PC) */
.kgroup{border:1px solid var(--line);border-radius:12px;padding:8px;background:#fafaf8;min-width:0}
.kgroup .kg-head{font-size:13px;color:var(--ink2);margin-bottom:6px;display:flex;align-items:center;gap:6px}
.kgroup .kg-head b{font-weight:600;color:var(--ink)}
.kgroup.forest{background:#fff3f2;border-color:#f3c6c2}.kgroup.forest .kg-head{color:#8f1f18}
#areaTable td.f,#areaTable th.f{color:#b3261e}
.kpis{display:grid;grid-template-columns:1fr 1fr;gap:8px}.kpis>*{min-width:0}
.kpi{border:1px solid var(--line);border-left:5px solid #ccc;border-radius:10px;padding:8px 10px;background:#fff}
.kpi small{display:block;color:var(--muted);font-size:12px}
.kpi .v{font-size:20px;font-weight:600;line-height:1.2;font-variant-numeric:tabular-nums;overflow-wrap:anywhere}
.kpi .d{font-size:12px;color:var(--ink2)}
.kpi.tot{grid-column:span 2;background:var(--ink);color:#fff;border-color:var(--ink)}
.kpi.tot small{color:#bbb}.kpi.tot .v{font-size:24px}.kpi.tot .u{font-size:13px;font-weight:300}
.chartbox{position:relative;height:150px;margin-top:10px}

/* ตาราง */
.tabs{display:flex;gap:2px;border-bottom:1px solid var(--line);margin-bottom:8px}
.tabs b{padding:6px 12px;font-weight:400;font-size:14px;color:var(--ink2)}
.tabs b.on{color:var(--g);font-weight:500;border-bottom:2px solid var(--g);margin-bottom:-1px}
#tableSearch{font-family:inherit;font-size:15px;border-radius:10px;margin-bottom:8px;min-height:40px}
.tablewrap{max-height:360px;overflow:auto;border:1px solid var(--line);border-radius:10px}
#areaTable{width:100%;border-collapse:collapse;font-size:14px}
#areaTable th{position:sticky;top:0;background:#fafaf8;color:var(--ink2);font-weight:500;padding:8px 8px;border-bottom:1px solid var(--line);text-align:left;cursor:pointer;white-space:nowrap;user-select:none}
#areaTable th.asc::after{content:' ▲';font-size:10px}#areaTable th.desc::after{content:' ▼';font-size:10px}
#areaTable td{padding:8px 8px;border-bottom:1px solid #f0f0ec}
#areaTable .i{width:36px;color:var(--muted);text-align:center}
#areaTable .n{text-align:right;font-variant-numeric:tabular-nums;white-space:nowrap}
#areaTable tbody tr{cursor:pointer}#areaTable tbody tr:hover{background:#f5f9f5}
#areaTable tbody tr.sel{background:#fff6cc}
#areaTable .bar{display:inline-block;height:6px;background:var(--gold);border-radius:3px;vertical-align:middle;margin-right:6px}
.card .hint{color:var(--muted);font-size:12px;margin-top:6px}

/* ---------- บนแผนที่ ---------- */
.mapchip{position:absolute;z-index:800;background:#fff;border-radius:10px;box-shadow:0 2px 8px rgba(0,0,0,.3);padding:6px 12px;font-size:14px}
.crumbs{top:12px;left:56px;display:flex;align-items:center;gap:6px;max-width:calc(100% - 130px);overflow:hidden;white-space:nowrap}
.crumbs .crumb{cursor:pointer;color:var(--g);font-weight:500}
.crumbs .crumb.on{color:var(--ink);cursor:default}
.crumbs .bi{color:var(--muted);font-size:11px}
.province-label{font-size:16px;font-weight:600;color:#fff;text-shadow:0 0 4px #000,0 0 8px #000;background:none;text-align:center}
.district-label{font-size:13px;color:#fff;text-shadow:0 0 3px #000,0 0 6px #000;background:none;text-align:center}
.leaflet-tooltip.factory-tooltip{background:#fff;color:#000;font-weight:500;border:1px solid #444;padding:3px 6px;border-radius:4px}
.factory-icon{color:#4dabf7;font-size:26px;text-align:center;text-shadow:0 0 3px #000}
.legend{background:#fff;padding:8px 10px;line-height:1.7;color:#333;font-size:12px;box-shadow:0 0 12px rgba(0,0,0,.25);border-radius:8px}
.legend b{font-weight:500}.legend i{width:14px;height:14px;float:left;margin:3px 8px 0 0;border-radius:3px;opacity:.9}
.vector-status{padding:6px 10px;font-size:12px;line-height:1.3;background:#fff;border-radius:8px !important;color:var(--ink2)}
.vector-status .hint{color:var(--muted)}
.leaflet-control-locate{z-index:10000 !important}
.leaflet-bar a,.leaflet-bar button{width:34px;height:34px;line-height:34px}
.leaflet-container{font-family:Prompt,sans-serif}
.leaflet-container:fullscreen,.leaflet-container:-webkit-full-screen{width:100% !important;height:100% !important}
.spinner-backdrop{position:fixed;inset:0;background:rgba(0,0,0,.7);z-index:9999;display:flex;align-items:center;justify-content:center;font-size:16px}

/* legend พับได้ (รอยเผา + LULC) — วางกองกันมุมล่างขวา */
.legend-fold .lg-head{cursor:pointer;display:flex;align-items:center;gap:6px;user-select:none;white-space:nowrap}
.legend-fold .lg-head .bi{margin-left:auto;font-size:11px;transition:transform .2s}
.legend-fold .lg-body{margin-top:4px}
.legend-lulc .lg-body{display:grid;grid-template-columns:1fr 1fr;column-gap:12px}
.legend-fold.collapsed .lg-body{display:none}.legend-fold.collapsed .lg-head .bi{transform:rotate(-90deg)}
.legend-fold.collapsed{padding:6px 10px}
@media (max-width:768px){.legend-lulc .lg-body{grid-template-columns:1fr}.legend-fold{max-width:210px;font-size:12px}}

/* ---------- เมนูชั้นข้อมูล ---------- */
.layer-menu{--bs-offcanvas-width:340px;font-family:Prompt,sans-serif}
.layer-menu .offcanvas-title{font-size:17px;font-weight:600}
.layer-menu .offcanvas-header{border-bottom:1px solid var(--line);padding:12px 16px}
.layer-menu .lm-close{font-family:inherit;border-radius:8px;min-height:36px;margin-left:auto}
.layer-menu .offcanvas-footer{padding:10px 16px calc(10px + env(safe-area-inset-bottom));border-top:1px solid var(--line);background:#fff}
.layer-menu .offcanvas-footer .btn{font-family:inherit;min-height:46px;border-radius:10px;font-size:15px}
.layer-menu .lg-title{font-size:13px;color:var(--muted);text-transform:none;margin:14px 0 4px;font-weight:500}
.layer-menu .lrow{display:flex;align-items:center;justify-content:space-between;gap:10px;min-height:46px;padding:0 6px;border-bottom:1px solid #f0f0ec;font-size:15px;cursor:pointer;margin:0}
.layer-menu .lrow:hover{background:#f7f7f4}
.layer-menu .lrow input[type=radio]{width:20px;height:20px;accent-color:var(--g);margin-right:10px;flex:none}
.layer-menu .lrow:has(input[type=radio]){justify-content:flex-start}   /* แผนที่ฐาน: ชื่ออยู่ติดปุ่มวงกลมด้านซ้าย อ่านง่ายกว่าชิดขวา */
.layer-menu .lrow input[type=checkbox]{width:44px;height:24px;appearance:none;-webkit-appearance:none;background:#ccc;border-radius:12px;position:relative;cursor:pointer;flex:none;margin:0;border:0}
.layer-menu .lrow input[type=checkbox]::after{content:'';position:absolute;top:2px;left:2px;width:20px;height:20px;border-radius:50%;background:#fff;transition:left .15s}
.layer-menu .lrow input[type=checkbox]:checked{background:var(--g)}.layer-menu .lrow input[type=checkbox]:checked::after{left:22px}
.layer-btn .bi{font-size:16px}
@media (max-width:768px){
  .layer-menu.offcanvas-end{top:auto;right:0;left:0;width:100%;height:auto;max-height:78vh;border-radius:18px 18px 0 0;transform:translateY(100%);border-left:0;z-index:1060}   /* 1060 = เหนือ topbar (1050) ไม่งั้นหัวเมนู "แผนที่ฐาน/ปิด" ถูก topbar บังเมื่อเมนูสูงถึงด้านบน */
  .offcanvas-backdrop{z-index:1055}
  .layer-menu.offcanvas-end.show{transform:none}
  .layer-menu .lrow{font-size:16px;min-height:50px}
}

/* ---------- มือถือ: แผนที่เต็มจอ + bottom sheet ---------- */
@media (max-width:768px){
  body{font-size:15px}
  .topbar{height:auto;min-height:52px;flex-wrap:wrap;gap:8px;padding:8px 10px}
  .topbar .title{font-size:16px}
  .topbar .chip.period{order:5;flex-basis:100%;margin-left:0;justify-content:center;font-size:14px}
  body{display:flex;flex-direction:column;height:100dvh}       /* topbar สูงเท่าไรก็ได้ ส่วนที่เหลือเป็นแผนที่ */
  .topbar{flex:0 0 auto}
  .layout{display:block;position:relative;flex:1 1 auto;min-height:0;height:auto}
  .mapwrap{position:absolute;inset:0}
  .panel{position:absolute;left:0;right:0;bottom:0;height:auto;border-left:0;border-top:1px solid var(--line);border-radius:18px 18px 0 0;box-shadow:0 -6px 24px rgba(0,0,0,.25);transition:top .3s ease;padding-top:6px;z-index:1001;background:var(--bg)}   /* 1001 = เหนือปุ่ม Leaflet (1000) แต่ใต้ offcanvas (1045) */
  .sheet-handle{display:flex;justify-content:center;padding:6px 0 4px;cursor:grab;touch-action:none}
  .sheet-handle span{width:44px;height:5px;border-radius:3px;background:#c9c9c4}
  .peek{display:block;text-align:center;font-size:15px;color:var(--ink2);padding:2px 0 8px}
  .peek b{color:var(--ink);font-weight:600;font-size:17px}
  body[data-sheet="peek"] .panel{top:calc(100% - 78px);overflow:hidden}
  body[data-sheet="peek"] .panel .card{display:none}
  body[data-sheet="half"] .panel{top:48%}
  body[data-sheet="full"] .panel{top:8%}
  body[data-sheet="peek"] .card{visibility:hidden}
  .leaflet-bottom{bottom:84px}                         /* พ้นแถบ peek ของ bottom sheet */
  body[data-sheet="half"] .leaflet-bottom,body[data-sheet="full"] .leaflet-bottom{display:none}   /* แผงกางอยู่ = ซ่อน legend/สถานะ/attribution */
  .vector-status{font-size:11px;max-width:150px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
  .kpi .v{font-size:19px}.kpi.tot .v{font-size:22px}
  .chartbox{height:120px}
  .tablewrap{max-height:none}
  .crumbs{left:52px;font-size:14px}
  #areaTable{font-size:15px}
  .row-sel .form-select,#tableSearch{font-size:16px}   /* ≥16px กัน iOS ซูมช่องกรอกเอง */
  .leaflet-bar a,.leaflet-bar button{width:40px;height:40px;line-height:40px}
}
@media (min-width:769px) and (max-width:1100px){:root{--panel-w:340px}}
