/* =============================================================
   Population Intelligence — dashboard.css
   (ฐานดีไซน์ใช้ร่วมกับ Hotel dashboard + ส่วนเสริมประชากรท้ายไฟล์)
   ============================================================= */
:root {
    --bg: #f1f5f9;
    --card: #ffffff;
    --ink: #0f172a;
    --muted: #64748b;
    --line: #e2e8f0;
    --brand: #6366f1;
    --brand-2: #22d3ee;
    --shadow: 0 1px 3px rgba(15, 23, 42, .06), 0 8px 24px rgba(15, 23, 42, .04);
    --radius: 18px;
}
* { box-sizing: border-box; font-family: 'Sarabun', system-ui, sans-serif; }
html, body { margin: 0; padding: 0; }
body {
    background: radial-gradient(1200px 600px at 100% -10%, #e0e7ff 0%, transparent 55%),
                radial-gradient(900px 500px at -10% 0%, #cffafe 0%, transparent 45%), var(--bg);
    color: var(--ink);
    -webkit-font-smoothing: antialiased;
}

/* ── Topbar ── */
.topbar {
    display: flex; align-items: center; justify-content: space-between;
    gap: 16px; flex-wrap: wrap;
    padding: 18px 26px;
    background: linear-gradient(120deg, #4f46e5, #6366f1 45%, #06b6d4);
    color: #fff;
    box-shadow: var(--shadow);
}
.topbar .tb-title { display: flex; align-items: center; gap: 14px; }
.topbar .tb-logo {
    width: 46px; height: 46px; border-radius: 14px;
    background: rgba(255, 255, 255, .18);
    display: flex; align-items: center; justify-content: center; font-size: 24px;
}
.topbar h1 { font-size: 1.35rem; font-weight: 800; margin: 0; letter-spacing: -.01em; }
.topbar .tb-sub { font-size: .82rem; opacity: .85; margin-top: 2px; }
.topbar .tb-meta { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; font-size: .8rem; }
.tb-pill { background: rgba(255, 255, 255, .16); padding: 7px 14px; border-radius: 999px; font-weight: 600; }
.tb-pill a { color: #fff; text-decoration: none; }
.tb-pill.warn { background: rgba(251, 191, 36, .9); color: #422006; }

.page { max-width: 1480px; margin: 0 auto; padding: 22px 18px 60px; }

/* ── KPI cards ── */
.kpi-grid {
    display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 22px;
}
.kpi {
    background: var(--card); border-radius: var(--radius); padding: 20px 22px;
    box-shadow: var(--shadow); border: 1px solid var(--line);
    position: relative; overflow: hidden;
}
.kpi::before {
    content: ''; position: absolute; right: -20px; top: -20px;
    width: 90px; height: 90px; border-radius: 50%; opacity: .12;
    background: var(--accent, var(--brand));
}
.kpi .kpi-icon { font-size: 1.5rem; }
.kpi .kpi-label { color: var(--muted); font-size: .85rem; margin: 10px 0 4px; }
.kpi .kpi-value { font-size: 1.9rem; font-weight: 800; letter-spacing: -.02em; }
.kpi .kpi-foot { color: var(--muted); font-size: .76rem; margin-top: 4px; }

/* ── Sections / cards ── */
.grid { display: grid; gap: 16px; }
.cols-2 { grid-template-columns: repeat(2, 1fr); }
.cols-3 { grid-template-columns: repeat(3, 1fr); }
.span-2 { grid-column: span 2; }

.card {
    background: var(--card); border-radius: var(--radius); padding: 18px 20px;
    box-shadow: var(--shadow); border: 1px solid var(--line); margin-bottom: 16px;
}
.card h2 { font-size: 1.02rem; font-weight: 700; margin: 0 0 2px; display: flex; align-items: center; gap: 8px; }
.card .card-sub { color: var(--muted); font-size: .8rem; margin: 0 0 14px; }

/* ── Card head + help tooltip ── */
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.card-head h2 { flex: 1; }
.help-icon {
    position: relative; flex: none; cursor: help;
    width: 22px; height: 22px; border-radius: 50%;
    border: none; background: var(--brand); color: #fff;
    font-weight: 800; font-size: .82rem; line-height: 1; font-family: inherit;
    display: inline-flex; align-items: center; justify-content: center;
    transition: transform .12s, background .12s;
}
.help-icon:hover, .help-icon:focus-visible { background: #4f46e5; transform: scale(1.12); outline: none; }
/* hover ทำให้ transform สร้าง stacking context บนไอคอน → ต้องยก z-index
   ที่ "ไอคอน" (ไม่ใช่ที่ .help-pop) ให้สูงกว่า Leaflet (panes 200–700, controls ~1000)
   มิฉะนั้น tooltip จะถูกขังอยู่ใต้แผนที่ */
.help-icon:hover, .help-icon:focus-visible, .help-icon:focus-within { z-index: 1100; }
.help-pop {
    position: absolute; top: 130%; right: 0; z-index: 1100;
    width: 268px; padding: 12px 14px; border-radius: 12px;
    background: #0f172a; color: #e2e8f0; text-align: left;
    font-weight: 400; font-size: .8rem; line-height: 1.5;
    box-shadow: 0 10px 30px rgba(15,23,42,.28);
    opacity: 0; visibility: hidden; transform: translateY(-4px);
    transition: opacity .15s, transform .15s, visibility .15s;
    display: flex; flex-direction: column; gap: 7px;
}
.help-pop::after {
    content: ''; position: absolute; top: -6px; right: 8px;
    border: 6px solid transparent; border-top: 0; border-bottom-color: #0f172a;
}
.help-pop b { color: #fff; }
.help-pop .help-goal { color: #a5b4fc; }
.help-icon:hover .help-pop, .help-icon:focus-visible .help-pop { opacity: 1; visibility: visible; transform: translateY(0); }

/* การ์ดแผนที่: ยกทั้งการ์ดเหนือ Leaflet + การ์ดอื่นตอน tooltip ทำงาน */
.map-card:has(.help-icon:hover), .map-card:has(.help-icon:focus-within) {
    position: relative; z-index: 1100;
}
/* การ์ดแผนที่สูง 560px — เปิด tooltip "ขึ้นบน" เพื่อไม่ให้ทับตัวแผนที่เลย
   (กันปัญหาตั้งแต่ geometry ไม่ใช่แค่ stacking) + พลิกลูกศรชี้ลงหาไอคอน */
.map-card .help-pop { top: auto; bottom: 130%; transform: translateY(4px); }
.map-card .help-icon:hover .help-pop, .map-card .help-icon:focus-visible .help-pop { transform: translateY(0); }
.map-card .help-pop::after {
    top: auto; bottom: -6px;
    border-top: 6px solid #0f172a; border-bottom: 0; border-bottom-color: transparent;
}
.card .chart-box { position: relative; height: 300px; }
.card .chart-box.tall { height: 360px; }
.card .zone-tall .chart-box { height: 360px; }

/* ── Table ── */
table.tbl { width: 100%; border-collapse: collapse; font-size: .86rem; }
table.tbl th, table.tbl td { padding: 9px 10px; text-align: left; border-bottom: 1px solid var(--line); }
table.tbl th { color: var(--muted); font-weight: 600; font-size: .76rem; text-transform: uppercase; letter-spacing: .03em; }
table.tbl td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.tbl tr:hover td { background: #f8fafc; }
.star-badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: .74rem; font-weight: 700; color: #fff; }

/* ── Placeholder (รอข้อมูล) ── */
.placeholder {
    height: 280px; border: 2px dashed var(--line); border-radius: 14px;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    text-align: center; color: var(--muted); gap: 8px; background: #f8fafc;
}
.placeholder .ph-icon { font-size: 2.4rem; opacity: .6; }
.placeholder .ph-title { font-weight: 700; color: #475569; }
.placeholder .ph-note { font-size: .8rem; max-width: 360px; }

.empty-note { color: var(--muted); font-size: .85rem; padding: 30px; text-align: center; }

/* ── Filter bar ── */
.filterbar {
    background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 14px 18px; margin-bottom: 20px;
    display: flex; flex-wrap: wrap; align-items: flex-end; gap: 18px 24px;
}
.fb-group { display: flex; flex-direction: column; gap: 6px; }
.fb-label { font-size: .72rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.fb-dates { display: flex; align-items: center; gap: 8px; }
.fb-dash { color: var(--muted); }
.filterbar input[type=date], .filterbar select {
    font-family: inherit; font-size: .86rem; padding: 8px 10px;
    border: 1px solid var(--line); border-radius: 10px; background: #f8fafc; color: var(--ink);
}
.filterbar input[type=date]:focus, .filterbar select:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
.fb-toggle { display: flex; align-items: center; gap: 7px; font-size: .84rem; color: #334155; cursor: pointer; }
.fb-toggle input { width: 16px; height: 16px; accent-color: var(--brand); cursor: pointer; }
.fb-actions { display: flex; align-items: center; gap: 10px; margin-left: auto; }
.fb-btn {
    font-family: inherit; font-size: .86rem; font-weight: 700; cursor: pointer;
    padding: 10px 20px; border-radius: 10px; border: 1px solid transparent; text-decoration: none;
}
.fb-btn.primary { background: var(--brand); color: #fff; }
.fb-btn.primary:hover { background: #4f46e5; }
.fb-btn.ghost { background: transparent; color: var(--muted); border-color: var(--line); }
.fb-btn.ghost:hover { background: #f1f5f9; }

/* ── Data badge (เตือนความครอบคลุมของข้อมูล) ── */
.data-badge {
    display: inline-flex; align-items: center; gap: 3px;
    font-size: .68rem; font-weight: 600; padding: 3px 9px; border-radius: 999px;
    margin-left: 8px; vertical-align: middle; white-space: nowrap;
}
.data-badge.warn { background: #fef3c7; color: #92400e; }
.data-badge.info { background: #e0f2fe; color: #075985; }

/* ── Section header (คั่นกลุ่มกราฟ เช่น forecast) ── */
.section-head { margin: 30px 0 14px; }
.section-title { font-size: 1.25rem; font-weight: 800; margin: 0; letter-spacing: -.01em; }
.section-sub { color: var(--muted); font-size: .85rem; margin: 4px 0 0; }

/* ── Map (full-width, ใหญ่) ── */
.map-card { margin-bottom: 22px; }
#popMap { height: 520px; border-radius: 14px; overflow: hidden; }
@media (max-width: 980px) { #popMap { height: 420px; } }

/* ── Search box ── */
.fb-search input[type=search] {
    font-family: inherit; font-size: .86rem; padding: 8px 12px; min-width: 200px;
    border: 1px solid var(--line); border-radius: 10px; background: #f8fafc; color: var(--ink);
}
.fb-search input[type=search]:focus { outline: 2px solid var(--brand); outline-offset: 1px; }

/* ── Click hint on chart subtitles ── */
.click-hint {
    color: var(--brand); font-weight: 600; font-size: .76rem;
    background: #eef2ff; padding: 1px 7px; border-radius: 999px; white-space: nowrap;
}

/* ── Active filter chips ── */
.filter-chips { display: none; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 18px; }
.filter-chips.show { display: flex; }
.chips-label { font-size: .8rem; font-weight: 700; color: var(--muted); }
.fchip {
    font-family: inherit; font-size: .8rem; font-weight: 600; cursor: pointer;
    background: var(--brand); color: #fff; border: none;
    padding: 6px 12px; border-radius: 999px; transition: background .12s;
}
.fchip:hover { background: #4f46e5; }
.chip-clear {
    font-family: inherit; font-size: .8rem; font-weight: 600; cursor: pointer;
    background: transparent; color: #ef4444; border: 1px solid #fecaca;
    padding: 6px 12px; border-radius: 999px;
}
.chip-clear:hover { background: #fef2f2; }

/* ── Clickable charts: pointer cursor on canvas ── */
.chart-box canvas, #zoneBox canvas { cursor: pointer; }

/* ── Map toggle ── */
.map-toggle { display: inline-flex; gap: 4px; margin-bottom: 10px; background: #f1f5f9; padding: 4px; border-radius: 10px; }
.mt-btn {
    font-family: inherit; font-size: .8rem; font-weight: 600; cursor: pointer;
    padding: 6px 14px; border: none; border-radius: 8px; background: transparent; color: var(--muted);
}
.mt-btn.active { background: var(--card); color: var(--ink); box-shadow: 0 1px 2px rgba(15,23,42,.08); }

@media (max-width: 980px) {
    .kpi-grid { grid-template-columns: repeat(2, 1fr); }
    .cols-2, .cols-3 { grid-template-columns: 1fr; }
    .span-2 { grid-column: auto; }
    .fb-actions { margin-left: 0; width: 100%; }
}

/* =============================================================
   ── ส่วนเสริมเฉพาะ Population dashboard ──
   ============================================================= */

/* KPI 5 ใบ (ประชากร/ไทย/ต่างชาติ/%ต่างชาติ/ครัวเรือน) */
.kpi-grid.cols-5 { grid-template-columns: repeat(5, 1fr); }
@media (max-width: 1180px) { .kpi-grid.cols-5 { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 720px)  { .kpi-grid.cols-5 { grid-template-columns: repeat(2, 1fr); } }

/* legend อำเภอ (คลิกกรองได้) — ใต้กราฟที่กรองตามอำเภอ */
.dist-legend { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 12px; }
.dist-chip {
    font-family: inherit; font-size: .8rem; font-weight: 600; cursor: pointer;
    display: inline-flex; align-items: center; gap: 6px;
    background: #f1f5f9; color: #334155; border: 1px solid var(--line);
    padding: 5px 12px; border-radius: 999px; transition: all .12s;
}
.dist-chip .dot { width: 10px; height: 10px; border-radius: 50%; }
.dist-chip.active { background: #fff; box-shadow: 0 1px 3px rgba(15,23,42,.12); border-color: currentColor; }
.dist-chip.dim { opacity: .45; }

/* breadcrumb drill-down อำเภอ → ตำบล → หมู่บ้าน */
.drill-crumb { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; font-size: .86rem; }
.drill-crumb .crumb {
    font-family: inherit; font-size: .84rem; font-weight: 600; cursor: pointer;
    background: #eef2ff; color: var(--brand); border: none;
    padding: 5px 12px; border-radius: 8px;
}
.drill-crumb .crumb:hover { background: #e0e7ff; }
.drill-crumb .crumb.current { background: var(--brand); color: #fff; cursor: default; }
.drill-crumb .sep { color: var(--muted); }

/* treemap / nested cells (drill-down view) */
.treemap { display: flex; flex-wrap: wrap; gap: 6px; align-content: flex-start; min-height: 300px; }
.tm-cell {
    border-radius: 10px; padding: 10px 12px; cursor: pointer; color: #fff;
    display: flex; flex-direction: column; justify-content: space-between;
    min-width: 90px; overflow: hidden; transition: transform .1s, filter .1s;
}
.tm-cell:hover { transform: translateY(-2px); filter: brightness(1.06); }
.tm-cell .tm-name { font-weight: 700; font-size: .86rem; line-height: 1.2; }
.tm-cell .tm-val { font-size: .78rem; opacity: .92; margin-top: 4px; }
.tm-cell.leaf { cursor: default; }
.tm-cell.leaf:hover { transform: none; filter: none; }

/* density / MoM color cell (heatmap-ish) */
.heat-grid { display: grid; gap: 4px; }
.heat-cell { padding: 8px 6px; border-radius: 6px; text-align: center; font-size: .74rem; font-weight: 600; color: #fff; }

/* แถวสรุปใต้ปิรามิด */
.pyramid-note { display: flex; justify-content: space-between; font-size: .8rem; color: var(--muted); margin-top: 8px; }
.pyramid-note b { color: var(--ink); }
