:root {
  --navy-950: #070d1f;
  --navy-900: #0b1530;
  --navy-800: #13214a;
  --navy-700: #1c2f63;
  --gold: #f5c542;
  --gold-2: #ffdf7e;
  --gold-deep: #c8961c;
  --red: #c8102e;
  --ink: #f4f6fb;
  --ink-2: #c3cbe0;
  --ink-3: #8d98b8;
  --line: rgba(245, 197, 66, .28);
  --glass: rgba(9, 18, 42, .78);
  --glass-strong: rgba(9, 18, 42, .94);
  --radius: 16px;
  --sheet-peek: 236px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --font: "IBM Plex Sans Thai", "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--navy-950); color: var(--ink); font-family: var(--font); -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; }
svg { display: block; }

#map { position: fixed; inset: 0; }
#map .cesium-widget-credits, .cesium-viewer-bottom { display: none !important; }
.cesium-widget canvas { outline: none; }

/* ---------- top bar ---------- */
.topbar {
  position: fixed; z-index: 20; top: calc(10px + var(--safe-t)); left: 10px; right: 10px;
  display: flex; align-items: center; gap: 8px; pointer-events: none;
}
.brand {
  pointer-events: auto; flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px;
  padding: 7px 12px 7px 7px; border-radius: 999px; background: var(--glass);
  border: 1px solid var(--line); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 6px 24px rgba(0,0,0,.35);
}
.brand-zone {
  flex: none; font-weight: 700; font-size: 13px; letter-spacing: .04em; color: var(--navy-900);
  background: linear-gradient(180deg, var(--gold-2), var(--gold) 55%, var(--gold-deep));
  padding: 6px 10px; border-radius: 999px; box-shadow: inset 0 -1px 0 rgba(0,0,0,.2);
}
.brand-text { min-width: 0; display: flex; flex-direction: column; line-height: 1.2; }
.brand-text strong { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-text span { font-size: 11.5px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.icon-btn {
  pointer-events: auto; flex: none; width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--glass); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  display: grid; place-items: center; cursor: pointer; box-shadow: 0 6px 24px rgba(0,0,0,.35);
}
.icon-btn svg { width: 22px; height: 22px; fill: var(--gold); }
.icon-btn[aria-expanded="true"] { background: var(--gold); }
.icon-btn[aria-expanded="true"] svg { fill: var(--navy-900); }

/* ---------- legend chips ---------- */
.legend {
  position: fixed; z-index: 19; top: calc(66px + var(--safe-t)); left: 0; right: 0;
  display: flex; gap: 6px; overflow-x: auto; padding: 0 10px 6px; scrollbar-width: none;
}
.legend::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 11px 0 8px;
  border-radius: 999px; border: 1px solid rgba(255,255,255,.14); background: var(--glass);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  font-size: 12.5px; color: var(--ink); cursor: pointer; white-space: nowrap;
}
.chip[aria-pressed="true"] { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold) inset; }
.sw { width: 14px; height: 14px; border-radius: 4px; flex: none; border: 2px solid var(--sw-o, transparent); background: var(--sw); }
.sw.dash { border-style: dashed; }
.sw.line { height: 4px; border-radius: 2px; border: 0; }

/* ---------- layers panel ---------- */
.layers-panel {
  position: fixed; z-index: 30; top: calc(62px + var(--safe-t)); right: 10px; width: 230px;
  padding: 12px 14px; border-radius: var(--radius); background: var(--glass-strong); border: 1px solid var(--line);
  box-shadow: 0 12px 40px rgba(0,0,0,.45);
}
.layers-panel h3 { margin: 0 0 8px; font-size: 13px; color: var(--gold); font-weight: 600; }
.layers-panel label { display: flex; align-items: center; gap: 10px; font-size: 14px; padding: 7px 0; cursor: pointer; }
.layers-panel input { width: 18px; height: 18px; accent-color: var(--gold); }
.layers-panel .mode-line { margin: 8px 0 0; padding-top: 8px; border-top: 1px solid rgba(255,255,255,.1); font-size: 12px; color: var(--ink-3); }
.layers-panel .mode-line span { color: var(--ink); }
body:not(.mode-osm) .osm-only { display: none; }

/* ---------- camera controls ---------- */
.controls {
  position: fixed; z-index: 18; right: 10px; top: calc(112px + var(--safe-t));
  display: flex; flex-direction: column; gap: 8px;
}
.ctrl {
  width: 44px; height: 44px; border-radius: 12px; border: 1px solid rgba(255,255,255,.14);
  background: var(--glass); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  display: grid; place-items: center; cursor: pointer; box-shadow: 0 4px 16px rgba(0,0,0,.3);
}
.ctrl svg { width: 22px; height: 22px; fill: var(--ink); }
.ctrl:active { transform: scale(.96); }
.ctrl[aria-pressed="true"] { background: var(--gold); border-color: var(--gold); }
.ctrl[aria-pressed="true"] svg { fill: var(--navy-900); }
.ctrl-txt { font-weight: 700; font-size: 13px; letter-spacing: .02em; }
#compassIcon { transition: transform .1s linear; }
#compassIcon .n { fill: #ff4d4d; }
#compassIcon .s { fill: var(--ink-2); }
.ctrl.desk { display: none; }
:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

.credits {
  position: fixed; z-index: 15; left: 8px; right: 64px; bottom: calc(var(--sheet-peek) + var(--safe-b) + 6px);
  font-size: 10px; line-height: 1.3; color: rgba(255,255,255,.85); text-shadow: 0 1px 2px rgba(0,0,0,.8);
  pointer-events: auto; transition: bottom .25s ease;
}
.credits img { max-height: 16px; vertical-align: middle; }
.credits a { color: inherit; }
.credits .cesium-credit-expand-link, .credits .cesium-credit-textContainer { display: inline; }
.credits .cesium-credit-logoContainer { display: inline-block; margin-right: 6px; }

/* ---------- bottom sheet ---------- */
.sheet {
  position: fixed; z-index: 25; left: 0; right: 0; bottom: 0;
  height: calc(var(--sheet-h, var(--sheet-peek)) + var(--safe-b));
  max-height: calc(100% - 70px - var(--safe-t));
  background: linear-gradient(180deg, rgba(14,26,60,.97), rgba(8,15,36,.985));
  border-top: 1px solid var(--line); border-radius: 22px 22px 0 0;
  box-shadow: 0 -10px 40px rgba(0,0,0,.45);
  display: flex; flex-direction: column; transition: height .28s cubic-bezier(.2,.8,.2,1);
  touch-action: none;
}
.sheet.dragging { transition: none; }
.sheet-grab { flex: none; height: 26px; display: grid; place-items: center; cursor: grab; touch-action: none; }
.sheet-grab span { width: 44px; height: 5px; border-radius: 3px; background: rgba(255,255,255,.28); }
.sheet-body {
  flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 0 16px calc(18px + var(--safe-b));
  -webkit-overflow-scrolling: touch; touch-action: pan-y;
}
.sheet[data-state="peek"] .sheet-body { overflow: hidden; }

/* sheet content */
.sh-head { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 10px; }
.sh-head h2 { margin: 0; font-size: 19px; font-weight: 700; line-height: 1.25; }
.sh-head p { margin: 2px 0 0; font-size: 13px; color: var(--ink-2); }
.sh-head .grow { flex: 1; min-width: 0; }
.back {
  flex: none; height: 34px; padding: 0 12px 0 8px; border-radius: 999px; border: 1px solid rgba(255,255,255,.16);
  background: rgba(255,255,255,.06); font-size: 13px; cursor: pointer; display: inline-flex; align-items: center; gap: 4px;
}
.back svg { width: 16px; height: 16px; fill: currentColor; }

.badge {
  flex: none; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center;
  font-weight: 700; font-size: 19px; color: #fff;
  background: radial-gradient(circle at 35% 30%, #e8344f, var(--red) 60%, #7d0718);
  box-shadow: 0 0 0 2px var(--gold), 0 4px 12px rgba(0,0,0,.4);
}
.badge.sm { width: 30px; height: 30px; font-size: 14px; }
.swatch-lg { flex: none; width: 40px; height: 40px; border-radius: 10px; background: var(--sw); border: 3px solid var(--sw-o, rgba(255,255,255,.3)); }
.swatch-lg.dash { border-style: dashed; }

.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin: 4px 0 12px; }
.kpi { background: rgba(255,255,255,.05); border: 1px solid rgba(255,255,255,.08); border-radius: 12px; padding: 8px 6px; text-align: center; }
.kpi b { display: block; font-size: 18px; font-weight: 700; color: var(--gold); font-variant-numeric: tabular-nums; line-height: 1.2; }
.kpi span { display: block; font-size: 11px; color: var(--ink-2); line-height: 1.25; }

.cards { display: flex; gap: 8px; overflow-x: auto; margin: 0 -16px; padding: 2px 16px 10px; scroll-snap-type: x mandatory; scrollbar-width: none; }
.cards::-webkit-scrollbar { display: none; }
.pcard {
  flex: none; width: 168px; scroll-snap-align: start; text-align: left; cursor: pointer;
  padding: 10px; border-radius: 14px; border: 1px solid rgba(245,197,66,.25);
  background: linear-gradient(160deg, rgba(28,47,99,.9), rgba(13,24,54,.9));
  display: flex; flex-direction: column; gap: 6px;
}
.pcard:hover { border-color: var(--gold); }
.pcard .row { display: flex; align-items: center; gap: 8px; }
.pcard .nm { font-weight: 600; font-size: 14px; line-height: 1.25; }
.pcard .cap { font-size: 12px; color: var(--ink-2); }
.pcard .cap b { color: var(--gold); font-weight: 700; font-size: 15px; }
.tag { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 999px; background: rgba(245,197,66,.14); color: var(--gold-2); border: 1px solid rgba(245,197,66,.3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

.sec { margin-top: 16px; }
.sec h3 { margin: 0 0 8px; font-size: 13px; font-weight: 600; color: var(--gold); letter-spacing: .02em; display: flex; align-items: center; gap: 6px; }
.sec h3 small { color: var(--ink-3); font-weight: 400; }

.list { list-style: none; margin: 0; padding: 0; border-radius: 12px; overflow: hidden; border: 1px solid rgba(255,255,255,.08); }
.list li { display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: rgba(255,255,255,.035); font-size: 14px; }
.list li + li { border-top: 1px solid rgba(255,255,255,.06); }
.list li .grow { flex: 1; min-width: 0; }
.list li .sub { display: block; font-size: 12px; color: var(--ink-3); }
.list li .val { font-weight: 600; font-variant-numeric: tabular-nums; }
.list li.click { cursor: pointer; }
.list li.click:hover { background: rgba(245,197,66,.08); }

.masked { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; color: var(--ink-3); }
.masked svg { width: 13px; height: 13px; fill: currentColor; }
.redact { display: inline-block; width: 74px; height: 10px; border-radius: 3px; background: repeating-linear-gradient(90deg, rgba(255,255,255,.22) 0 6px, rgba(255,255,255,.1) 6px 9px); vertical-align: middle; }

.bar { display: flex; height: 12px; border-radius: 6px; overflow: hidden; margin: 2px 0 8px; }
.bar i { display: block; height: 100%; }
.bar-legend { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; font-size: 13px; }
.bar-legend .dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.bar-legend b { font-variant-numeric: tabular-nums; }

.block { border: 1px solid rgba(255,255,255,.1); border-radius: 14px; padding: 12px; background: rgba(255,255,255,.035); }
.block + .block { margin-top: 8px; }
.block-h { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.block-h .bid { font-weight: 700; font-size: 13px; color: var(--navy-900); background: var(--gold); border-radius: 8px; padding: 2px 8px; }
.block-h .bp { font-weight: 600; font-size: 14px; flex: 1; min-width: 0; }
.block-h .bn { font-size: 13px; color: var(--ink-2); white-space: nowrap; }
.block-h .bn b { color: var(--gold); font-size: 15px; }
.mini { display: grid; grid-template-columns: repeat(5, minmax(0,1fr)); gap: 4px; margin-top: 8px; }
.mini div { text-align: center; background: rgba(0,0,0,.2); border-radius: 8px; padding: 5px 2px; }
.mini b { display: block; font-size: 14px; font-variant-numeric: tabular-nums; }
.mini span { display: block; font-size: 10px; color: var(--ink-3); }
.mini div.tot { background: rgba(245,197,66,.15); }
.mini div.tot b { color: var(--gold); }
.roles-line { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 13px; color: var(--ink-2); }
.roles-line b { color: var(--ink); }

.notes { margin: 0; padding-left: 18px; font-size: 13px; color: var(--ink-2); }
.notes li + li { margin-top: 3px; }

.actions { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.btn {
  flex: 1; min-width: 140px; height: 42px; border-radius: 12px; border: 1px solid var(--line); background: rgba(245,197,66,.1);
  color: var(--gold-2); font-weight: 600; font-size: 14px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none;
}
.btn.primary { background: linear-gradient(180deg, var(--gold-2), var(--gold)); color: var(--navy-900); border-color: var(--gold); }
.btn svg { width: 18px; height: 18px; fill: currentColor; }

.privacy {
  margin-top: 16px; display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border-radius: 12px;
  background: rgba(127,179,255,.08); border: 1px solid rgba(127,179,255,.25); font-size: 12.5px; color: var(--ink-2);
}
.privacy svg { flex: none; width: 18px; height: 18px; fill: #7fb3ff; margin-top: 1px; }
.foot { margin-top: 14px; font-size: 11px; color: var(--ink-3); line-height: 1.5; }

/* ---------- hint / loader ---------- */
.hint {
  position: fixed; z-index: 26; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--sheet-peek) + var(--safe-b) + 30px); max-width: calc(100% - 32px);
  padding: 8px 14px; border-radius: 999px; background: rgba(0,0,0,.72); font-size: 12.5px; color: #fff; text-align: center;
  animation: fadeout 6s forwards; pointer-events: none;
}
@keyframes fadeout { 0%, 80% { opacity: 1; } 100% { opacity: 0; } }
.loader { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; background: radial-gradient(circle at 50% 40%, #13214a, #070d1f 70%); transition: opacity .5s; }
.loader.done { opacity: 0; pointer-events: none; }
.loader-card { text-align: center; padding: 24px; }
.loader-card p { margin: 14px 0 0; color: var(--ink-2); font-size: 14px; }
.loader-card p.err { color: #ffb4b4; }
.spinner { width: 44px; height: 44px; margin: 0 auto; border-radius: 50%; border: 3px solid rgba(245,197,66,.2); border-top-color: var(--gold); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .sheet, .credits { transition: none; }
  .spinner { animation-duration: 3s; }
}

/* ---------- desktop / tablet ---------- */
@media (min-width: 900px) {
  :root { --sheet-peek: 0px; }
  .topbar { right: auto; width: 400px; left: 16px; top: 16px; }
  .icon-btn { position: fixed; right: 16px; top: 16px; }
  .legend { top: 16px; left: 432px; right: 72px; flex-wrap: wrap; overflow: visible; padding: 0; }
  .layers-panel { top: 68px; right: 16px; }
  .controls { top: 72px; right: 16px; }
  .ctrl.desk { display: grid; }
  .sheet {
    left: 16px; right: auto; top: 76px; bottom: 16px; width: 400px; height: auto !important; max-height: none;
    border: 1px solid var(--line); border-radius: 20px;
  }
  .sheet-grab { display: none; }
  .sheet .sheet-body, .sheet[data-state="peek"] .sheet-body { overflow-y: auto; padding-top: 16px; }
  .cards { flex-wrap: wrap; overflow: visible; margin: 0; padding: 2px 0 10px; }
  .pcard { width: calc(50% - 4px); }
  .credits { left: 432px; bottom: 10px; right: 16px; }
  .hint { bottom: 24px; left: calc(50% + 208px); }
}

/* capture mode (?clean) */
body.clean .topbar, body.clean .legend, body.clean .controls, body.clean .sheet,
body.clean .credits, body.clean .hint, body.clean .layers-panel { display: none !important; }
