/* ===================== Design tokens ===================== */
:root {
  --primary-950: #05263f;
  --primary-900: #0a3a5c;
  --primary-800: #0d4a75;
  --primary-700: #115a8f;
  --primary-600: #156ea8;
  --primary-500: #1d84c4;
  --primary-100: #e3f0fa;
  --primary-50: #f2f8fd;

  --accent: #d4a017;
  --accent-dark: #b3860f;

  --success: #16a34a;
  --success-bg: #e8f8ef;
  --warning: #d97706;
  --warning-bg: #fef3e2;
  --danger: #dc2626;
  --danger-bg: #fdecec;
  --neutral-bg: #eef1f5;

  --bg: #f5f7fa;
  --surface: #ffffff;
  --text: #1a2433;
  --text-muted: #5b6b7c;
  --border: #e3e8ee;

  --radius-sm: 8px;
  --radius: 14px;
  --radius-lg: 20px;
  --shadow-sm: 0 1px 2px rgba(15, 35, 60, 0.06);
  --shadow: 0 4px 16px rgba(15, 35, 60, 0.08);
  --shadow-lg: 0 12px 32px rgba(15, 35, 60, 0.14);

  --header-h: 76px;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: calc(var(--header-h) + 78px); }

body {
  margin: 0;
  font-family: 'Kanit', 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

img { max-width: 100%; display: block; }
a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }

/* ===================== Header ===================== */
.site-header {
  position: sticky; top: 0; z-index: 100;
  height: var(--header-h);
  background: linear-gradient(180deg, var(--primary-900), var(--primary-800));
  box-shadow: 0 2px 12px rgba(5, 38, 63, 0.25);
}
.header-inner {
  max-width: 1280px; margin: 0 auto; height: 100%;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 24px;
}
.brand { display: flex; align-items: center; gap: 12px; color: #fff; }
.brand-icon {
  width: 44px; height: 44px; border-radius: 50%; flex-shrink: 0;
  background: #fff;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}
.brand-icon img { width: 100%; height: 100%; object-fit: contain; }
.brand-text { display: flex; flex-direction: column; line-height: 1.25; }
.brand-title { font-weight: 600; font-size: 17px; letter-spacing: .2px; }
.brand-sub { font-size: 12.5px; color: rgba(255,255,255,0.72); }

.main-nav { display: flex; align-items: center; gap: 28px; }
.main-nav a { color: rgba(255,255,255,0.85); font-size: 14.5px; font-weight: 400; transition: color .15s; }
.main-nav a:hover { color: #fff; }
.nav-admin {
  padding: 9px 18px; border-radius: 999px;
  background: var(--accent); color: #221a05 !important; font-weight: 500;
}
.nav-admin:hover { background: var(--accent-dark); color: #fff !important; }

.nav-toggle { display: none; flex-direction: column; gap: 5px; background: none; border: none; padding: 8px; }
.nav-toggle span { width: 24px; height: 2px; background: #fff; border-radius: 2px; }

/* ===================== Hero ===================== */
.hero {
  background:
    radial-gradient(ellipse at top right, rgba(212,160,23,0.18), transparent 55%),
    linear-gradient(160deg, var(--primary-950), var(--primary-800) 65%, var(--primary-700));
  color: #fff;
  padding: 56px 24px 48px;
}
.hero-inner { max-width: 900px; margin: 0 auto; text-align: center; }
.hero h1 { font-size: clamp(26px, 4vw, 40px); font-weight: 600; margin: 0 0 16px; letter-spacing: .2px; }
.hero p { font-size: 16px; color: rgba(255,255,255,0.82); max-width: 640px; margin: 0 auto; font-weight: 300; }

/* ===================== Filter bar ===================== */
.filter-bar {
  position: sticky; top: var(--header-h); z-index: 90;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.filter-inner {
  max-width: 1280px; margin: 0 auto; padding: 18px 24px;
  display: flex; flex-wrap: wrap; align-items: end; gap: 14px;
}
.filter-group { display: flex; flex-direction: column; gap: 6px; min-width: 150px; }
.filter-search { flex: 1; min-width: 200px; }
.filter-group label { font-size: 12.5px; color: var(--text-muted); font-weight: 500; }
.filter-group select, .filter-group input {
  font-family: inherit; font-size: 14px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--bg); color: var(--text); outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.filter-group select:focus, .filter-group input:focus {
  border-color: var(--primary-500); box-shadow: 0 0 0 3px var(--primary-100);
}
.btn-ghost {
  padding: 10px 18px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text-muted); font-size: 14px; white-space: nowrap;
  transition: all .15s;
}
.btn-ghost:hover { background: var(--neutral-bg); color: var(--text); }
.btn-ghost svg { width: 16px; height: 16px; vertical-align: -3px; margin-right: 4px; }

/* ===================== Sections ===================== */
.section { padding: 8px 24px 64px; }
.section-alt { background: var(--surface); border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); }
.section-inner { max-width: 1280px; margin: 0 auto; padding-top: 40px; }
.section-head { margin-bottom: 24px; }
.section-head h2 { font-size: 24px; font-weight: 600; margin: 0 0 6px; }
.section-head p { color: var(--text-muted); font-size: 14.5px; margin: 0; }

/* ===================== KPI cards ===================== */
.kpi-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 18px;
  margin-bottom: 28px;
}
.kpi-card {
  background: var(--surface); border-radius: var(--radius); padding: 22px;
  display: flex; align-items: center; gap: 16px; min-width: 0;
  box-shadow: var(--shadow-sm); border: 1px solid var(--border);
  transition: transform .15s, box-shadow .15s;
}
.kpi-card:hover { transform: translateY(-3px); box-shadow: var(--shadow); }
.kpi-icon {
  width: 52px; height: 52px; border-radius: 14px; flex-shrink: 0;
  background-size: 24px; background-repeat: no-repeat; background-position: center;
}
.icon-projects { background-color: var(--primary-100); background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23115a8f' stroke-width='2'%3E%3Crect x='3' y='3' width='7' height='7' rx='1.5'/%3E%3Crect x='14' y='3' width='7' height='7' rx='1.5'/%3E%3Crect x='3' y='14' width='7' height='7' rx='1.5'/%3E%3Crect x='14' y='14' width='7' height='7' rx='1.5'/%3E%3C/svg%3E"); }
.icon-budget { background-color: #fbf3de; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23b3860f' stroke-width='2'%3E%3Ccircle cx='12' cy='12' r='9'/%3E%3Cpath d='M12 7v10M9 9.5c0-1.4 1.3-2.5 3-2.5s3 1 3 2.3c0 3-6 1.7-6 4.7 0 1.3 1.3 2.5 3 2.5s3-1.1 3-2.5'/%3E%3C/svg%3E"); }
.icon-disbursed { background-color: var(--success-bg); background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2316a34a' stroke-width='2'%3E%3Cpath d='M3 12l6 6 12-12'/%3E%3C/svg%3E"); }
.icon-done { background-color: #ede9fe; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%237c3aed' stroke-width='2'%3E%3Cpath d='M9 12l2 2 4-4'/%3E%3Ccircle cx='12' cy='12' r='9'/%3E%3C/svg%3E"); }
.kpi-body { display: flex; flex-direction: column; min-width: 0; }
.kpi-value { font-size: 25px; font-weight: 600; color: var(--primary-900); line-height: 1.3; overflow-wrap: anywhere; }
.kpi-label { font-size: 13px; color: var(--text-muted); }

/* ===================== Charts ===================== */
.charts-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 18px;
}
.chart-card {
  background: var(--surface); border-radius: var(--radius); padding: 22px; min-width: 0;
  box-shadow: var(--shadow-sm); border: 1px solid var(--border);
}
.chart-wide { grid-column: span 2; }
.chart-card h3 { margin: 0 0 16px; font-size: 15.5px; font-weight: 500; color: var(--text); }
.chart-canvas-wrap { position: relative; height: 260px; min-width: 0; }
.chart-canvas-wrap canvas { max-width: 100%; }
.chart-tall { height: 320px; }

/* ===================== Map ===================== */
/* position+z-index ที่นี่บังคับให้ Leaflet สร้าง stacking context ของตัวเอง
   ป้องกันปุ่มซูม/คอนโทรลภายใน (z-index สูงถึง 1000 ในตัว) หลุดไปทับ header/filter-bar ที่ sticky อยู่ */
.leaflet-map, .picker-map, .detail-mini-map { position: relative; z-index: 0; }
.leaflet-map { height: 520px; border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); border: 1px solid var(--border); }
.map-legend { display: flex; gap: 22px; margin-top: 14px; font-size: 13.5px; color: var(--text-muted); flex-wrap: wrap; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; }
.dot-done { background: var(--success); }
.dot-progress { background: var(--warning); }
.dot-other { background: #94a3b8; }

.leaflet-popup-content-wrapper { border-radius: 12px; }
.map-popup { font-family: 'Kanit', sans-serif; max-width: 240px; }
.map-popup img { width: 100%; height: 120px; object-fit: cover; border-radius: 8px; margin-bottom: 8px; }
.map-popup h4 { font-size: 13.5px; margin: 0 0 6px; line-height: 1.4; font-weight: 500; }
.map-popup .popup-meta { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.map-popup .popup-link { font-size: 12.5px; color: var(--primary-600); font-weight: 500; display: inline-flex; align-items: center; gap: 4px; }
.map-popup .popup-detail { background: none; border: none; padding: 0; font-family: inherit; cursor: pointer; }
.map-popup .popup-actions { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.map-popup .popup-nav { white-space: nowrap; }
.map-popup .icon-chevron, .map-popup .icon-directions { width: 13px; height: 13px; flex-shrink: 0; color: currentColor; }

/* ===================== Project grid ===================== */
.project-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 20px;
}
.project-card {
  background: var(--surface); border-radius: var(--radius); overflow: hidden;
  box-shadow: var(--shadow-sm); border: 1px solid var(--border);
  cursor: pointer; transition: transform .18s, box-shadow .18s;
  display: flex; flex-direction: column;
}
.project-card:hover { transform: translateY(-4px); box-shadow: var(--shadow-lg); }
.card-thumb { position: relative; height: 168px; background: var(--neutral-bg); overflow: hidden; }
.card-thumb img { width: 100%; height: 100%; object-fit: cover; }
.card-thumb-placeholder {
  width: 100%; height: 100%; display: flex; align-items: center; justify-content: center;
  color: #b7c2cf; background: linear-gradient(135deg, #eef2f6, #e4eaf0);
}
.card-thumb-placeholder svg { width: 40px; height: 40px; }
.card-badge {
  position: absolute; top: 10px; left: 10px;
  font-size: 11.5px; font-weight: 500; padding: 4px 10px; border-radius: 999px;
  background: rgba(10,58,92,0.85); color: #fff;
}
.card-status {
  position: absolute; top: 10px; right: 10px;
  font-size: 11px; font-weight: 500; padding: 4px 10px; border-radius: 999px;
}
.status-done { background: var(--success-bg); color: var(--success); }
.status-progress { background: var(--warning-bg); color: var(--warning); }
.status-other { background: var(--neutral-bg); color: var(--text-muted); }

.card-body { padding: 16px; display: flex; flex-direction: column; gap: 8px; flex: 1; }
.card-title { font-size: 14.5px; font-weight: 500; line-height: 1.45; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; min-height: 42px; }
.card-meta { font-size: 12.5px; color: var(--text-muted); display: flex; align-items: center; gap: 6px; }
.icon-pin { width: 13px; height: 13px; flex-shrink: 0; vertical-align: -2px; color: var(--primary-800, #115a8f); }
.icon-category { width: 13px; height: 13px; flex-shrink: 0; vertical-align: -2px; color: var(--text-muted); }
.card-footer { margin-top: auto; display: flex; justify-content: space-between; align-items: center; padding-top: 8px; border-top: 1px dashed var(--border); }
.card-budget { font-size: 15px; font-weight: 600; color: var(--primary-800); }
.card-budget span { font-size: 11.5px; font-weight: 400; color: var(--text-muted); }

.load-more-wrap { text-align: center; margin-top: 32px; }
.btn-primary {
  padding: 12px 32px; border-radius: 999px; border: none;
  background: var(--primary-700); color: #fff; font-size: 14.5px; font-weight: 500;
  box-shadow: var(--shadow-sm); transition: background .15s, transform .15s;
}
.btn-primary:hover { background: var(--primary-600); transform: translateY(-1px); }
.empty-state { color: var(--text-muted); font-size: 14.5px; padding: 40px 0; }

/* ===================== Footer ===================== */
.site-footer { background: var(--primary-950); color: rgba(255,255,255,0.75); padding: 26px 24px; }
.footer-inner { max-width: 1280px; margin: 0 auto; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; font-size: 13px; }
.footer-inner a { color: var(--accent); font-weight: 500; }

/* ===================== Modal ===================== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(10,20,35,0.55);
  display: flex; align-items: center; justify-content: center; z-index: 1000; padding: 20px;
  backdrop-filter: blur(2px);
}
.modal-overlay[hidden] { display: none; }
.modal-panel {
  background: var(--surface); border-radius: var(--radius-lg); max-width: 880px; width: 100%;
  max-height: 88vh; overflow-y: auto; position: relative; box-shadow: var(--shadow-lg);
}
.modal-close {
  position: sticky; top: 14px; float: right; margin-right: 14px; z-index: 5;
  width: 36px; height: 36px; border-radius: 50%; border: none; background: var(--surface);
  box-shadow: var(--shadow); font-size: 20px; color: var(--text-muted); line-height: 1;
}
.modal-close:hover { color: var(--danger); }
.modal-body { padding: 8px 32px 32px; }
.modal-loading { padding: 80px; text-align: center; color: var(--text-muted); }

.detail-gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; margin: 18px 0; }
.detail-gallery img { width: 100%; height: 110px; object-fit: cover; border-radius: 10px; cursor: zoom-in; }
.detail-title { font-size: 20px; font-weight: 600; line-height: 1.45; margin: 4px 0 12px; padding-right: 40px; }
/* 44px tap target; negative top margin keeps the visual gap to the title unchanged */
.detail-title-toggle { display: none; width: fit-content; align-items: center; min-height: 44px; margin: -17px 0 0; padding: 0 12px 0 0; border: 0; background: none; color: var(--primary-700); font: inherit; font-size: 13px; font-weight: 500; cursor: pointer; }
.detail-title-toggle[hidden] { display: none; }
.detail-badges { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.detail-badge { font-size: 12px; padding: 5px 12px; border-radius: 999px; background: var(--primary-100); color: var(--primary-800); font-weight: 500; }
.detail-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; margin: 18px 0; }
.detail-item { background: var(--bg); border-radius: 10px; padding: 12px 14px; }
.detail-item .dl-label { font-size: 12px; color: var(--text-muted); display: block; margin-bottom: 3px; }
.detail-item .dl-value { font-size: 14.5px; font-weight: 500; }
.detail-section-title { font-size: 14.5px; font-weight: 600; margin: 20px 0 8px; color: var(--primary-800); }
.detail-section-title-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.detail-nav-link {
  font-size: 13px; font-weight: 500; color: var(--primary-800); background: var(--primary-100);
  display: inline-flex; align-items: center; gap: 6px; padding: 7px 14px; border-radius: 999px;
  transition: background .15s, transform .15s;
}
.detail-nav-link:hover { background: var(--primary-700); color: #fff; transform: translateY(-1px); }
.detail-nav-link .icon-directions { width: 13px; height: 13px; flex-shrink: 0; }
.detail-text { font-size: 13.5px; color: var(--text-muted); white-space: pre-line; line-height: 1.7; }
.detail-mini-map { height: 260px; border-radius: 12px; margin-top: 12px; overflow: hidden; }

.lightbox-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 3000;
  display: flex; align-items: center; justify-content: center; padding: 32px;
}
.lightbox-overlay[hidden] { display: none; }
.lightbox-overlay img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 6px; }
.lightbox-close {
  position: fixed; top: 18px; right: 24px; width: 40px; height: 40px; border-radius: 50%;
  border: none; background: rgba(255,255,255,.12); color: #fff; font-size: 22px; line-height: 1;
}
.lightbox-close:hover { background: rgba(255,255,255,.22); }

/* ===================== Responsive ===================== */
@media (max-width: 980px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .charts-grid { grid-template-columns: 1fr; }
  .chart-wide { grid-column: span 1; }
  .detail-grid { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .main-nav { position: fixed; top: var(--header-h); left: 0; right: 0; background: var(--primary-900);
    flex-direction: column; align-items: stretch; padding: 12px 20px 20px; gap: 4px;
    transform: translateY(-140%); transition: transform .2s; box-shadow: var(--shadow-lg); }
  .main-nav.open { transform: translateY(0); }
  .main-nav a { padding: 12px 4px; border-bottom: 1px solid rgba(255,255,255,0.08); }
  .nav-admin { text-align: center; margin-top: 6px; }
  .nav-toggle { display: flex; }
  /* บนมือถือฟิลด์ตัวกรองเรียงซ้อนแนวตั้งจนสูงมาก ถ้ายัง sticky จะค้างบังจอครึ่งนึงตอนเลื่อน
     จึงปิด sticky ไว้เฉพาะช่วงจอแคบ ให้เลื่อนผ่านไปกับเนื้อหาปกติแทน */
  .filter-bar { position: static; }
  .filter-inner { padding: 14px 16px; }
  .kpi-grid { grid-template-columns: 1fr 1fr; }
  .hero { padding: 40px 20px 70px; }
}
@media (max-width: 480px) {
  .kpi-grid { grid-template-columns: 1fr; }
  .header-inner { padding: 0 16px; }
  .brand-sub { display: none; }
  .detail-title {
    font-size: 17px; padding-right: 44px;
    display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden;
  }
  .detail-title.expanded { display: block; -webkit-line-clamp: unset; overflow: visible; }
  .detail-title-toggle { display: flex; }
}
