/* ── ตัวแปรธีม ─────────────────────────────────────────── */
:root {
  --bg:        #f1f5f9;
  --surface:   #ffffff;
  --surface-2: #f8fafc;
  --border:    #e2e8f0;
  --text:      #0f172a;
  --text-2:    #475569;
  --text-3:    #94a3b8;
  --accent:    #2563eb;
  --accent-2:  #1d4ed8;
  --danger:    #dc2626;
  --shadow:    0 1px 3px rgba(15,23,42,.08), 0 8px 24px rgba(15,23,42,.06);
  --radius:    12px;
  --sidebar-w: 288px;
  --detail-w:  380px;
  --topbar-h:  56px;
}
:root[data-theme="dark"] {
  --bg:        #0b1220;
  --surface:   #111a2b;
  --surface-2: #16203385;
  --border:    #1e293b;
  --text:      #e2e8f0;
  --text-2:    #94a3b8;
  --text-3:    #64748b;
  --accent:    #3b82f6;
  --accent-2:  #60a5fa;
  --shadow:    0 1px 3px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.3);
}

* { box-sizing: border-box; }
/* ต้องมาก่อนกฎที่ตั้ง display — ไม่งั้น .modal-backdrop{display:grid} จะชนะ attribute hidden */
[hidden] { display: none !important; }

/* iOS Safari เปลี่ยนความสูงจริงตอนแถบที่อยู่ยุบ/กาง ใช้ 100% แล้วจอจะล้น
   dvh คือความสูงที่มองเห็นจริง ณ ขณะนั้น */
html, body { height: 100%; }
@supports (height: 100dvh) { html, body { height: 100dvh; } }

body {
  margin: 0;
  font-family: "Noto Sans Thai", "Sarabun", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  overscroll-behavior: none;          /* กันหน้าเด้งตอนลากสุดขอบบน iOS */
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
h2, h3 { margin: 0; font-weight: 700; }
select, input, button { font-family: inherit; font-size: 13px; color: inherit; }

/* ── Top bar ───────────────────────────────────────────── */
.topbar {
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 12px;
  padding: 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: relative; z-index: 40;
}
.brand { font-weight: 800; font-size: 15px; white-space: nowrap; display: flex; align-items: center; gap: 7px; }
.brand-dot { font-size: 18px; }
.topbar-search { flex: 1; max-width: 420px; }
.topbar-search input {
  width: 100%; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface-2); outline: none;
}
.topbar-search input:focus { border-color: var(--accent); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.stat-pill {
  font-size: 12px; color: var(--text-2);
  background: var(--surface-2); border: 1px solid var(--border);
  padding: 5px 11px; border-radius: 999px; white-space: nowrap;
}
.icon-btn {
  width: 34px; height: 34px; flex-shrink: 0;
  display: grid; place-items: center;
  border: 1px solid var(--border); border-radius: 9px;
  background: var(--surface); cursor: pointer; font-size: 15px;
  transition: background .15s;
}
.icon-btn:hover { background: var(--surface-2); }
.btn-primary {
  padding: 8px 15px; border: 0; border-radius: 9px;
  background: var(--accent); color: #fff; font-weight: 700; cursor: pointer;
  white-space: nowrap; transition: background .15s;
}
.btn-primary:hover { background: var(--accent-2); }
.btn-ghost {
  width: 100%; padding: 9px; margin-top: 4px;
  border: 1px solid var(--border); border-radius: 9px;
  background: transparent; color: var(--text-2); cursor: pointer;
}
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

/* ── Layout ────────────────────────────────────────────── */
.layout { display: flex; height: calc(100% - var(--topbar-h)); min-height: 0; }

.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto; overscroll-behavior: contain;
  padding: 14px; display: flex; flex-direction: column; gap: 14px;
}
.panel h3 {
  font-size: 11px; letter-spacing: .06em; text-transform: uppercase;
  color: var(--text-3); margin-bottom: 9px;
}
.lbl { display: block; font-size: 11px; color: var(--text-3); margin: 8px 0 4px; }
.panel select, .range-row input[type="number"] {
  width: 100%; padding: 7px 9px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); outline: none;
}
.panel select:focus, .range-row input:focus { border-color: var(--accent); }

.chip-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.chip-grid.sm { grid-template-columns: 1fr 1fr; }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: flex; align-items: center; gap: 5px;
  padding: 7px 9px; font-size: 12px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); color: var(--text-2);
  cursor: pointer; user-select: none;
  transition: all .12s; text-align: left;
}
.chip:hover { border-color: var(--text-3); }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.chip .swatch { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.chip.on .swatch { box-shadow: 0 0 0 2px rgba(255,255,255,.55); }

.range-row { display: flex; align-items: center; gap: 7px; }
.range-row .dash { color: var(--text-3); }
.range-row input[type="range"] { flex: 1; accent-color: var(--accent); }
.radius-val { font-size: 12px; color: var(--text-2); white-space: nowrap; min-width: 56px; }
.quick-row { display: flex; gap: 5px; margin-top: 6px; }
.quick-row button {
  flex: 1; padding: 5px 0; font-size: 11px;
  border: 1px solid var(--border); border-radius: 7px;
  background: var(--surface-2); color: var(--text-2); cursor: pointer;
}
.quick-row button:hover { border-color: var(--accent); color: var(--accent); }

.switch { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--text-2); cursor: pointer; margin-bottom: 6px; }
.switch input { accent-color: var(--accent); width: 15px; height: 15px; cursor: pointer; }

/* ── Map ───────────────────────────────────────────────── */
.map-wrap { flex: 1; position: relative; min-width: 0; }
#map { position: absolute; inset: 0; }
.maplibregl-popup-content {
  background: var(--surface); color: var(--text);
  border-radius: 10px; box-shadow: var(--shadow); padding: 0; overflow: hidden;
}
.maplibregl-popup-tip { border-top-color: var(--surface) !important; border-bottom-color: var(--surface) !important; }
.maplibregl-popup-close-button { color: var(--text-2); font-size: 17px; padding: 2px 7px; }

.map-legend {
  position: absolute; left: 12px; bottom: 12px; z-index: 5;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: var(--shadow);
  padding: 9px 11px; font-size: 11.5px; color: var(--text-2);
  display: flex; flex-direction: column; gap: 5px; max-width: 190px;
}
.legend-item { display: flex; align-items: center; gap: 7px; }
.legend-item .swatch { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }

/* หมุด landmark (HTML marker) */
.lm-marker {
  width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center; font-size: 15px;
  background: var(--surface); border: 2px solid;
  box-shadow: 0 2px 8px rgba(0,0,0,.25); cursor: pointer;
  transition: transform .12s;
}
.lm-marker:hover { transform: scale(1.18); }

/* ── List panel ────────────────────────────────────────── */
.list-toggle {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: 12px; z-index: 6;
  padding: 9px 18px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); box-shadow: var(--shadow);
  font-weight: 700; font-size: 13px; cursor: pointer;
  display: flex; align-items: center; gap: 8px;
}
.list-toggle .caret { color: var(--text-3); font-size: 11px; }

.listpanel {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 7;
  height: 42%; min-height: 220px;
  background: var(--surface); border-top: 1px solid var(--border);
  box-shadow: 0 -8px 24px rgba(15,23,42,.10);
  display: flex; flex-direction: column;
  transform: translateY(100%); transition: transform .22s ease;
}
.listpanel.open { transform: translateY(0); }
.listpanel-head {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-bottom: 1px solid var(--border);
}
.sortrow { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--text-3); }
.sortrow select { padding: 5px 8px; border: 1px solid var(--border); border-radius: 7px; background: var(--surface-2); }
.listpanel-head .icon-btn { margin-left: auto; }
.listpanel-body {
  flex: 1; overflow-y: auto; padding: 10px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 9px;
  align-content: start;
}

.card {
  border: 1px solid var(--border); border-left-width: 4px; border-radius: 10px;
  background: var(--surface-2); padding: 10px; cursor: pointer;
  display: flex; flex-direction: column; gap: 5px;
  transition: box-shadow .14s, transform .14s;
  overflow: hidden;
}

/* รูปตัวอย่างบนการ์ด — ล็อกสัดส่วนไว้ล่วงหน้า หน้าจะได้ไม่กระโดดตอนรูปโหลดเสร็จ */
.card-thumb {
  position: relative; margin: -10px -10px 4px; aspect-ratio: 16 / 10;
  background: var(--bg); overflow: hidden;
}
.card-thumb img {
  width: 100%; height: 100%; object-fit: cover; display: block;
  opacity: 0; transition: opacity .3s;
}
.card-thumb img.loaded { opacity: 1; }
.card-thumb .count {
  position: absolute; right: 7px; bottom: 7px;
  background: rgba(15,23,42,.78); color: #fff;
  font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px;
  backdrop-filter: blur(4px);
}
.card-thumb .ph {                       /* โครงร่างระหว่างรอรูป */
  position: absolute; inset: 0; display: grid; place-items: center;
  color: var(--text-3); font-size: 22px;
  background: linear-gradient(100deg, var(--bg) 30%, var(--surface) 50%, var(--bg) 70%);
  background-size: 220% 100%; animation: shimmer 1.4s linear infinite;
}
@keyframes shimmer { to { background-position: -220% 0; } }

/* ── แกลเลอรีในแผงรายละเอียด ── */
.gal { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; }
.gal > button {
  padding: 0; border: 1px solid var(--border); border-radius: 8px;
  overflow: hidden; aspect-ratio: 1; background: var(--bg); cursor: zoom-in;
}
.gal img { width: 100%; height: 100%; object-fit: cover; display: block; opacity: 0; transition: opacity .3s; }
.gal img.loaded { opacity: 1; }
.gal > button:first-child { grid-column: span 3; aspect-ratio: 16 / 10; }

/* ── ดูรูปเต็มจอ ── */
.lightbox {
  position: fixed; inset: 0; z-index: 300;
  background: rgba(2, 6, 23, .95);
  display: grid; place-items: center;
  touch-action: pan-y;
}
.lightbox img {
  max-width: 100%; max-height: 100%;
  width: auto; height: auto; object-fit: contain;
  padding: env(safe-area-inset-top) 8px calc(56px + env(safe-area-inset-bottom));
}
.lb-btn {
  position: absolute; width: 44px; height: 44px; border: 0; border-radius: 50%;
  background: rgba(255,255,255,.14); color: #fff; font-size: 19px; cursor: pointer;
  display: grid; place-items: center; backdrop-filter: blur(6px);
}
.lb-btn:hover { background: rgba(255,255,255,.26); }
.lb-close { top: calc(14px + env(safe-area-inset-top)); right: 14px; }
.lb-prev { left: 14px; top: 50%; transform: translateY(-50%); }
.lb-next { right: 14px; top: 50%; transform: translateY(-50%); }
.lb-count {
  position: absolute; bottom: calc(18px + env(safe-area-inset-bottom)); left: 50%;
  transform: translateX(-50%); color: #cbd5e1; font-size: 13px; font-weight: 600;
  background: rgba(15,23,42,.7); padding: 6px 14px; border-radius: 999px;
}
.lb-open {
  position: absolute; bottom: calc(18px + env(safe-area-inset-bottom)); right: 16px;
  color: #cbd5e1; font-size: 12px; text-decoration: none;
  background: rgba(15,23,42,.7); padding: 6px 12px; border-radius: 999px;
}
.card:hover { box-shadow: var(--shadow); transform: translateY(-1px); }
.card.is-inactive { opacity: .58; }
.card-title { font-weight: 700; font-size: 13.5px; line-height: 1.35; }
.card-meta { font-size: 11.5px; color: var(--text-3); }
.card-price { font-weight: 800; font-size: 14px; color: var(--accent); }
.card-price .sold { color: var(--text-3); text-decoration: line-through; font-weight: 600; }
.card-tags { display: flex; flex-wrap: wrap; gap: 4px; }
.tag {
  font-size: 10.5px; padding: 2px 7px; border-radius: 999px;
  background: var(--bg); color: var(--text-2); border: 1px solid var(--border);
}
.empty { grid-column: 1/-1; text-align: center; padding: 40px 16px; color: var(--text-3); font-size: 13px; }

/* ── Detail drawer ─────────────────────────────────────── */
.detail {
  width: var(--detail-w); flex-shrink: 0;
  background: var(--surface); border-left: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden;
}
.detail-head { display: flex; padding: 9px 12px; border-bottom: 1px solid var(--border); }
.detail-head .icon-btn { margin-left: auto; }
.detail-body { flex: 1; overflow-y: auto; padding: 14px; }

.d-title { font-size: 17px; font-weight: 800; line-height: 1.35; margin-bottom: 4px; }
.d-project { font-size: 13px; color: var(--text-2); margin-bottom: 12px; }
.d-price { font-size: 21px; font-weight: 800; color: var(--accent); margin-bottom: 12px; }
.d-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 14px;
}
.d-cell { background: var(--surface-2); border: 1px solid var(--border); border-radius: 9px; padding: 8px 10px; }
.d-cell .k { font-size: 10.5px; color: var(--text-3); text-transform: uppercase; letter-spacing: .04em; }
.d-cell .v { font-size: 14px; font-weight: 700; margin-top: 2px; }
.d-section { margin-bottom: 14px; }
.d-section > h4 {
  font-size: 11px; letter-spacing: .06em; text-transform: uppercase;
  color: var(--text-3); margin: 0 0 7px;
}
.d-near { display: flex; flex-direction: column; gap: 5px; }
.d-near-row {
  display: flex; align-items: center; gap: 8px; font-size: 12.5px;
  padding: 6px 9px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
}
.d-near-row .dist { margin-left: auto; font-weight: 700; color: var(--text-2); font-size: 12px; }
.d-raw {
  font-size: 12px; color: var(--text-2); line-height: 1.6;
  white-space: pre-wrap; word-break: break-word;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 9px; padding: 10px; max-height: 220px; overflow-y: auto;
}
.d-imgs { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.d-imgs img { width: 100%; aspect-ratio: 4/3; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.d-actions { display: flex; flex-wrap: wrap; gap: 7px; margin-bottom: 14px; }
.d-actions a, .d-actions button {
  flex: 1; min-width: 110px; text-align: center; text-decoration: none;
  padding: 8px 10px; border-radius: 9px; font-size: 12.5px; font-weight: 600;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text-2); cursor: pointer;
}
.d-actions a:hover, .d-actions button:hover { border-color: var(--accent); color: var(--accent); }
.d-actions .danger:hover { border-color: var(--danger); color: var(--danger); }

.badge {
  display: inline-block; font-size: 10.5px; font-weight: 700;
  padding: 3px 9px; border-radius: 999px; margin-right: 5px;
}

/* ── Modal ─────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(15,23,42,.55); backdrop-filter: blur(3px);
  display: grid; place-items: center; padding: 18px;
}
.modal {
  width: 100%; max-width: 620px; max-height: 88vh;
  background: var(--surface); border-radius: 14px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; overflow: hidden;
}
.modal-head {
  display: flex; align-items: center; gap: 10px;
  padding: 13px 16px; border-bottom: 1px solid var(--border);
}
.modal-head h2 { font-size: 15px; }
.modal-head .icon-btn { margin-left: auto; }
.modal-body { padding: 16px; overflow-y: auto; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form-grid .full { grid-column: 1/-1; }
.field label { display: block; font-size: 11px; color: var(--text-3); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); outline: none;
}
.field textarea { resize: vertical; min-height: 92px; line-height: 1.55; }
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.hint { font-size: 11.5px; color: var(--text-3); line-height: 1.55; margin-top: 6px; }

/* ── Toast ─────────────────────────────────────────────── */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  z-index: 200; padding: 10px 18px; border-radius: 999px;
  background: var(--text); color: var(--bg);
  font-size: 13px; font-weight: 600; box-shadow: var(--shadow);
}

/* ── Responsive ────────────────────────────────────────── */
.only-mobile { display: none; }

@media (max-width: 900px) {
  :root { --topbar-h: 52px; }

  /* ⚠️ สำคัญที่สุดสำหรับ iOS: ช่องกรอกที่ฟอนต์เล็กกว่า 16px
     จะทำให้ Safari ซูมหน้าเข้าอัตโนมัติตอนแตะ แล้วหน้าค้างอยู่สภาพซูม
     ตั้ง 16px ทุกช่องเพื่อปิดพฤติกรรมนี้ (ไม่ใช่ห้ามซูมด้วย maximum-scale
     เพราะจะทำให้คนที่ต้องการซูมอ่านใช้ไม่ได้) */
  input, select, textarea, .topbar-search input { font-size: 16px !important; }

  .only-mobile { display: grid; }
  .topbar-search { display: none; }
  .stat-pill { display: none; }
  .topbar {
    gap: 7px; padding: 0 10px;
    padding-top: env(safe-area-inset-top);
    height: calc(var(--topbar-h) + env(safe-area-inset-top));
  }
  .layout { height: calc(100% - var(--topbar-h) - env(safe-area-inset-top)); }
  .brand { font-size: 13px; }
  .brand-dot { font-size: 16px; }
  .icon-btn { width: 34px; height: 34px; font-size: 15px; }
  .btn-primary { padding: 8px 12px; font-size: 13px; }

  /* แผงตัวกรองเลื่อนเข้ามาทับ พร้อมพื้นที่เผื่อขอบจอโค้ง */
  .sidebar {
    position: fixed; top: calc(var(--topbar-h) + env(safe-area-inset-top));
    bottom: 0; left: 0; z-index: 30;
    width: min(88vw, 330px);
    transform: translateX(-100%); transition: transform .22s ease;
    box-shadow: var(--shadow);
    padding-bottom: calc(24px + env(safe-area-inset-bottom));
    -webkit-overflow-scrolling: touch;
  }
  .sidebar.open { transform: translateX(0); }

  .detail {
    position: fixed;
    top: calc(var(--topbar-h) + env(safe-area-inset-top));
    inset-inline: 0; bottom: 0; z-index: 35;
    width: 100%; border-left: 0;
  }
  .detail-body { padding-bottom: calc(28px + env(safe-area-inset-bottom)); }
  .d-grid { grid-template-columns: 1fr 1fr; }
  .d-actions a, .d-actions button { min-width: 0; flex: 1 1 46%; padding: 11px 8px; }

  /* แผงรายการเป็น bottom sheet */
  .listpanel { height: 62%; border-radius: 16px 16px 0 0; }
  .listpanel-body {
    grid-template-columns: 1fr;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    -webkit-overflow-scrolling: touch;
  }
  .list-toggle { bottom: calc(14px + env(safe-area-inset-bottom)); padding: 11px 22px; }
  .map-legend { display: none; }

  /* ปุ่มควบคุมแผนที่ให้นิ้วกดง่ายขึ้น */
  .maplibregl-ctrl-group button { width: 36px !important; height: 36px !important; }

  .modal { max-height: 92vh; border-radius: 16px; }
  .modal-body { padding: 14px; padding-bottom: calc(14px + env(safe-area-inset-bottom)); }
  .form-grid { grid-template-columns: 1fr; }
  .modal-actions button { flex: 1; padding: 12px; }
  .toast { bottom: calc(26px + env(safe-area-inset-bottom)); max-width: 90vw; }
}

/* จอแคบมาก (iPhone SE / mini) */
@media (max-width: 380px) {
  .brand { font-size: 0; gap: 0; }        /* เหลือแต่ไอคอน กันแถบบนล้น */
  .brand-dot { font-size: 19px; }
  .icon-btn { width: 32px; height: 32px; }
}

/* เปิดจากหน้าจอโฮม (โหมดแอป) — ไม่มีแถบเบราว์เซอร์ ต้องเผื่อขอบเอง */
@media (display-mode: standalone) {
  .topbar { padding-top: max(env(safe-area-inset-top), 8px); }
}
