/* =========================================================
   CCTV Donhuaroh — Civic Light theme (index.php)
   ========================================================= */
:root {
  --ground: #f6f7f4;
  --surface: #ffffff;
  --ink: #1b2a2f;
  --muted: #5f6b66;
  --line: #e1e5de;
  --line-strong: #cfd6cc;
  --brand: #1f7a5a;
  --brand-soft: #eaf4ef;
  --seg-bg: #eef1ec;
  --green: #2bb673;
  --yellow: #f2b233;
  --red: #e5484d;
  --offline: #8a949c;
  --topbar-h: 0px; /* หัวเว็บย้ายเข้าแถบขวาแล้ว ไม่กินความสูงหน้า */
  --side-w: 400px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--ground);
  color: var(--ink);
  font-family: "Anuphan", "Prompt", sans-serif;
  -webkit-font-smoothing: antialiased;
}

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0; padding: 0; margin: -1px;
}

/* ---------- top bar ---------- */
.topbar {
  flex: none;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.topbar img { width: 38px; height: 38px; object-fit: contain; }
.brand h1 { margin: 0; font-size: 16px; font-weight: 700; line-height: 1.2; }
.brand { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.brand span { font-size: 12px; color: var(--muted); }
.topnav { margin-left: auto; display: flex; gap: 6px; }
.topnav a {
  display: flex; align-items: center;
  min-height: 44px; padding: 0 16px; border-radius: 10px;
  color: #3e4a46; font-size: 14px; text-decoration: none;
}
.topnav a:hover { background: var(--seg-bg); }
.topnav a[aria-current="page"] { background: var(--brand-soft); color: var(--brand); font-weight: 600; }

/* ---------- layout (desktop) ---------- */
.layout {
  height: calc(100% - var(--topbar-h));
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--side-w);
  grid-template-rows: minmax(0, 1fr);
  grid-template-areas: "map side"; /* วิดีโอ+แผนที่ซ้าย, รายการกล้องขวา */
}
.sidebar { grid-area: side; }
#map { grid-area: map; height: 100%; width: 100%; z-index: 1; background: #e4e8df; }
.video-card {
  grid-area: map;
  align-self: start; justify-self: start;
  margin: 16px;
  width: min(420px, 42%);
  position: relative;
  z-index: 600; /* อยู่เหนือ legend เวลาลากไปทับ */
}
.legend {
  grid-area: map;
  align-self: end; justify-self: end;
  margin: 0 24px 28px 0;
  z-index: 500;
}
/* ปุ่มซูมแผนที่ย้ายไปขวา (ซ้ายมีการ์ดวิดีโอทับ) */
.leaflet-top.leaflet-left { left: auto; right: 0; }
.leaflet-top.leaflet-left .leaflet-control { margin-left: 0; margin-right: 12px; }

/* ---------- sidebar ---------- */
.sidebar {
  display: flex; flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border-left: 1px solid var(--line);
}
.side-top { padding: 14px 16px 10px; display: flex; flex-direction: column; gap: 10px; }

.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.stat { padding: 8px 10px; border-radius: 10px; }
.stat small { display: block; font-size: 12px; }
.stat b { font-size: 18px; font-weight: 700; }
.stat b span { font-size: 14px; font-weight: 500; color: var(--muted); }
.stat-online { background: var(--brand-soft); color: #3e5a4e; }
.stat-online b { color: var(--brand); }
.stat-yellow { background: #fbf0d6; color: #6b4e10; }
.stat-yellow b { color: #8a5a00; }
.stat-red { background: #fbe3e3; color: #7a2a2d; }
.stat-red b { color: #a12a2e; }

/* ป้ายช่องค้นหาซ่อนจากตา (placeholder บอกแล้ว) แต่โปรแกรมอ่านหน้าจอยังอ่านได้ */
.search label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.search-box { position: relative; }
.search-box svg { position: absolute; left: 12px; top: 10px; pointer-events: none; }
#searchInput {
  width: 100%; height: 38px;
  padding: 0 36px 0 38px;
  border-radius: 10px; border: 1px solid var(--line-strong);
  background: var(--ground); color: var(--ink);
  font-family: inherit; font-size: 14px;
}
#searchInput::placeholder { color: #7d8783; }
#searchInput:focus { outline: 2px solid var(--brand); outline-offset: 1px; border-color: var(--brand); }
#clearSearch {
  display: none;
  position: absolute; right: 2px; top: 1px;
  width: 36px; height: 36px;
  border: 0; background: transparent; cursor: pointer;
  font-size: 22px; line-height: 1; color: var(--muted);
}

.seg { display: flex; padding: 3px; border-radius: 10px; background: var(--seg-bg); }
.seg button {
  flex: 1; min-height: 32px;
  border: 0; border-radius: 8px;
  background: transparent; color: var(--muted);
  font-family: inherit; font-size: 13px; cursor: pointer;
}
.seg button[aria-pressed="true"] {
  background: var(--surface); color: var(--brand); font-weight: 600;
  box-shadow: 0 1px 3px rgba(27, 42, 47, 0.15);
}

.cam-list { flex: 1; min-height: 0; overflow-y: auto; padding: 0 12px; }

/* scrollbar บาง สีโทนเดียวกับหน้า (Firefox ใช้ scrollbar-*, Chrome/Edge/Safari ใช้ ::-webkit-scrollbar) */
.cam-list { scrollbar-width: thin; scrollbar-color: #c5d0c9 transparent; }
.cam-list::-webkit-scrollbar { width: 8px; }
.cam-list::-webkit-scrollbar-track { background: transparent; }
.cam-list::-webkit-scrollbar-thumb { background: #c5d0c9; border-radius: 8px; border: 2px solid var(--surface); }
.cam-list::-webkit-scrollbar-thumb:hover { background: #8fa79a; }
.cam-list::-webkit-scrollbar-button { display: none; height: 0; }
.cam {
  width: 100%; min-height: 56px;
  display: flex; align-items: center; gap: 12px;
  padding: 8px 10px; margin-bottom: 4px;
  border: 1px solid transparent; border-radius: 12px;
  background: transparent; color: var(--ink);
  font-family: inherit; text-align: left; cursor: pointer;
}
.cam:hover { background: var(--ground); }
.cam[aria-current="true"] { background: var(--brand-soft); border-color: var(--brand); }
.cam .no {
  flex: none; width: 36px; height: 36px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  background: var(--seg-bg); font-weight: 700; font-size: 13px; color: #3e4a46;
}
.cam[aria-current="true"] .no { background: var(--brand); color: #fff; }
.cam .txt { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.cam .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; font-weight: 500; }
.cam .meta { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.cam.off { color: var(--offline); cursor: not-allowed; }
.cam.off:hover { background: transparent; }
.cam-empty { padding: 24px 10px; text-align: center; color: var(--muted); font-size: 14px; }

.dot { flex: none; display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--offline); }
.dot-green { background: var(--green); }
.dot-yellow { background: var(--yellow); }
.dot-red { background: var(--red); }
.dot-off { background: transparent; border: 2px solid var(--offline); }

/* รายงานกล้องที่ไม่มีสัญญาณ */
/* แถวปุ่ม 2 คอลัมน์: ไม่มีสัญญาณ | เกี่ยวกับระบบ (ไม่มีกล้องดับ → ปุ่มเกี่ยวกับระบบเต็มแถว) */
/* ระยะบน = ระยะล่าง (ห่างรายการกล้อง = ห่างบรรทัดท้าย) */
.side-actions { position: relative; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 12px; }
.side-actions:has(.offline-btn[hidden]) { grid-template-columns: 1fr; }
.side-actions > * { min-width: 0; }
.offline-btn {
  min-height: 32px; padding: 0 12px; border: 0; border-radius: 8px;
  background: #fbe3e3; color: #7a2a2d; font-family: inherit; font-size: 12px; font-weight: 600;
  text-align: left; cursor: pointer;
}
.offline-btn:hover { background: #f7d2d2; }
.offline-btn::after { content: "›"; float: right; font-size: 18px; line-height: 1; font-weight: 400; }

/* เกี่ยวกับระบบ + คำถามที่พบบ่อย (SEO: ข้อความจริงบนหน้า) — ปุ่มเปิด modal */
.about-btn {
  min-height: 32px; padding: 0 12px;
  border: 0; border-radius: 8px; background: var(--ground);
  color: #3e4a46; font-family: inherit; font-size: 12px; font-weight: 600;
  text-align: left; cursor: pointer;
}
.about-btn:hover { background: var(--seg-bg); }
.about-btn::after { content: "›"; float: right; font-size: 18px; line-height: 1; font-weight: 400; }
.modal {
  width: min(560px, 92vw); max-height: 85vh; max-height: 85dvh;
  padding: 0; border: 0; border-radius: 16px;
  background: var(--surface); color: var(--ink);
  box-shadow: 0 24px 64px rgba(27, 42, 47, 0.3);
  font-family: inherit;
}
.modal[open] { display: flex; flex-direction: column; }
.modal::backdrop { background: rgba(27, 42, 47, 0.45); }
.modal-head {
  flex: none; display: flex; align-items: center; gap: 12px;
  padding: 14px 12px 12px 20px; border-bottom: 1px solid var(--line);
}
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-close {
  margin-left: auto; width: 40px; height: 40px; border: 0; border-radius: 10px;
  background: transparent; color: var(--muted); font-size: 24px; line-height: 1; cursor: pointer;
}
.modal-close:hover { background: var(--seg-bg); }
.modal-body { overflow-y: auto; padding: 16px 20px 20px; font-size: 14px; line-height: 1.7; color: #3e4a46; }
.modal-body p { margin: 0 0 14px; }
.modal-body h3 { margin: 0 0 4px; font-size: 15px; color: var(--ink); }
.modal-body dl { margin: 0; }
.modal-body dt { font-weight: 600; color: var(--ink); margin-top: 12px; }
.modal-body dd { margin: 2px 0 0; color: var(--muted); }
.modal-note { color: var(--muted); font-size: 13px; }
.off-list { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 8px; }
.off-list li { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; background: #fdf1f1; }
.off-list li b {
  flex: none; width: 32px; height: 32px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  background: #fbe3e3; color: #a12a2e; font-size: 13px;
}
.side-foot {
  display: flex; justify-content: space-between; gap: 8px;
  padding: 10px 20px; border-top: 1px solid var(--line);
  font-size: 12px; color: var(--muted);
}
.side-foot a { color: var(--brand); text-decoration: none; }
.side-foot a:hover { text-decoration: underline; }

/* ---------- video card ---------- */
.video-card {
  border-radius: 12px; overflow: hidden;
  background: var(--surface);
  box-shadow: 0 12px 32px rgba(27, 42, 47, 0.18);
}
#video-container { position: relative; background: #000; }
#video-container .video-js.vjs-fluid {
  padding-top: 0 !important;
  width: 100% !important;
  /* สูงตามความกว้างการ์ดจริง (สตรีม 4:3) → ไม่มีแถบดำบน/ล่าง */
  height: auto !important;
  aspect-ratio: 4 / 3;
}
#video-container .video-js .vjs-tech { object-fit: contain; }
.cam-info { display: flex; align-items: center; gap: 10px; padding: 8px 12px 8px 4px; cursor: grab; user-select: none; touch-action: none; }
.drag-grip {
  flex: none; width: 24px; height: 36px; margin-right: -4px;
  display: flex; align-items: center; justify-content: center;
  border: 0; border-radius: 8px; background: transparent; cursor: grab;
}
.drag-grip:hover { background: var(--seg-bg); }
.drag-grip:focus-visible { outline: 2px solid var(--brand); }
.video-card.dragging { box-shadow: 0 20px 48px rgba(27, 42, 47, 0.32); }
/* มุมจับย่อ/ขยายการ์ดวิดีโอ (desktop) */
.resize-grip {
  position: absolute; right: 0; bottom: 0; z-index: 2;
  width: 24px; height: 24px; padding: 0;
  display: flex; align-items: flex-end; justify-content: flex-end;
  border: 0; border-radius: 0 0 12px 0; background: transparent;
  cursor: nwse-resize; touch-action: none;
}
.resize-grip svg { margin: 0 4px 4px 0; }
.resize-grip:hover svg, .video-card.resizing .resize-grip svg { stroke: var(--brand); }
.resize-grip:focus-visible { outline: 2px solid var(--brand); outline-offset: -2px; }
.video-card.resizing { box-shadow: 0 20px 48px rgba(27, 42, 47, 0.32); user-select: none; }
.video-card.dragging .cam-info, .video-card.dragging .drag-grip { cursor: grabbing; }
.cam-info .no {
  flex: none; width: 32px; height: 32px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  background: var(--brand); color: #fff; font-weight: 700; font-size: 13px;
}
.cam-info .txt { flex: 1; min-width: 0; }
.cam-info .name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cam-info .meta { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }

/* ---------- legend ---------- */
.legend {
  display: flex; flex-wrap: wrap; gap: 14px;
  padding: 10px 16px; border-radius: 12px;
  background: var(--surface);
  box-shadow: 0 4px 12px rgba(27, 42, 47, 0.12);
  font-size: 13px; color: #3e4a46;
}
.legend span { display: flex; align-items: center; gap: 6px; }

/* ---------- video.js (ย้ายมาจาก style.css) ---------- */
.video-js { background-color: #000; }
.video-js .vjs-control-bar {
  background: none !important; opacity: 1 !important; visibility: visible !important;
  transition: none !important; justify-content: space-between;
}
.vjs-live-indicator {
  font-family: "Anuphan", sans-serif;
  display: flex; align-items: center;
  font-size: 12px; font-weight: 600; color: #fff; margin-left: 12px;
}
.vjs-live-indicator .dot { width: 10px; height: 10px; border-radius: 50%; background-color: red; margin-right: 6px; animation: blink-red 1s infinite; }
@keyframes blink-red { 0%, 50%, 100% { background-color: red; } 25%, 75% { background-color: #cf202e; } }
.offline-overlay {
  font-family: "Anuphan", sans-serif;
  position: absolute; inset: 0;
  background: rgba(0, 0, 0, 0.55); color: #fff;
  font-size: 24px; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  z-index: 9999; opacity: 0; pointer-events: none; transition: opacity 0.3s ease;
}
.offline-overlay .th-text { font-size: 16px; font-weight: 400; margin-top: 8px; }
.offline-overlay.show { opacity: 1; pointer-events: auto; }
.offline-overlay.show ~ .vjs-error-display,
.video-js.vjs-error .vjs-error-display,
.video-js.vjs-error .vjs-big-play-button { display: none !important; }
.video-js *:focus:not(:focus-visible) { outline: none; box-shadow: none; }

/* ---------- leaflet (ย้ายมาจาก style.css) ---------- */
.leaflet-popup-content-wrapper, .leaflet-popup-tip {
  font-family: "Anuphan", sans-serif; font-size: 13px; font-weight: 500;
  color: var(--ink); background: #fff; border-radius: 10px;
}
.leaflet-tooltip { font-family: "Anuphan", sans-serif; border-radius: 6px; }
.boundary-tooltip {
  background: rgba(255, 255, 255, 0.95); border-radius: 6px; color: #333;
  font-family: "Anuphan", sans-serif; font-size: 12px; padding: 4px 8px;
  border: 1px solid #aaa; box-shadow: 0 0 4px rgba(0, 0, 0, 0.25);
}

/* =========================================================
   TABLET (768–1100px): sidebar แคบลง, วิดีโอเล็กลง
   ========================================================= */
@media (max-width: 1100px) {
  :root { --side-w: 330px; }
  .video-card { width: min(360px, 48%); margin: 12px; }
  .legend { display: none; }
}

/* =========================================================
   MOBILE (≤767px): วิดีโอ → รายการกล้อง (ไม่แสดงแผนที่, พอดี 1 หน้าจอ)
   ========================================================= */
@media (max-width: 767px) {
  :root { --topbar-h: 0px; }
  html, body { height: 100%; overflow: hidden; }
  /* ไม่มีแถบหัวเว็บ: เหลือโลโก้ลอยอยู่มุมซ้ายบนของวิดีโอ, วิดีโอขึ้นไปบนสุด */
  .topbar {
    position: absolute; top: 8px; left: 8px; z-index: 700;
    height: auto; padding: 0; border: 0; background: transparent;
  }
  .topbar img {
    width: 40px; height: 40px; padding: 2px; border-radius: 50%;
    background: rgba(255, 255, 255, 0.9); box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  }
  /* ซ่อนชื่อจากตาบนมือถือ แต่ยังให้ Google/โปรแกรมอ่านหน้าจออ่านได้ */
  .brand { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .side-actions { margin: 6px 8px; gap: 6px; }
  .about-btn { min-height: 34px; font-size: 12px; }
  .modal-body { padding: 14px 16px 18px; }

  /* เต็มความสูงจอพอดี: รายการกล้องเลื่อนในตัวเอง หน้าไม่เลื่อน */
  .layout {
    height: calc(100vh - var(--topbar-h));
    height: calc(100dvh - var(--topbar-h));
    display: flex; flex-direction: column;
  }
  .video-card { order: 1; flex: none; width: 100%; margin: 0; border-radius: 0; box-shadow: none; transform: none !important; }
  #video-container .video-js.vjs-fluid { height: min(56.25vw, 30vh) !important; }
  /* ข้อมูลกล้องใต้วิดีโอซ้ำกับการ์ดที่เลือกในรายการ → ซ่อน */
  .cam-info, .resize-grip { display: none; }
  .sidebar { order: 2; flex: 1; min-height: 0; border-left: 0; }
  .side-top { padding: 12px 12px 8px; gap: 10px; }
  /* การ์ดสรุป: ป้าย + ตัวเลขในบรรทัดเดียว */
  .stats { gap: 6px; }
  .stat { display: flex; align-items: baseline; justify-content: center; gap: 6px; padding: 6px 8px; border-radius: 10px; white-space: nowrap; }
  .stat small { display: inline; font-size: 12px; }
  .stat b { font-size: 16px; }
  .stat b span { font-size: 12px; }
  .cam-list { flex: 1; min-height: 0; padding: 0 8px; }
  .legend { display: none; }
  /* ไม่แสดงแผนที่ แต่ห้าม display:none (ขนาด 0 → Leaflet คำนวณพิกัดเป็น NaN ตอนบินไปหาหมุด)
     → ย่อเหลือ 10px วางนอกจอ โหลดภาพแผนที่น้อยมาก */
  #map { position: fixed !important; left: -100px; top: 0; width: 10px !important; height: 10px !important; visibility: hidden; }

  /* ฟอร์มกระชับ: ค้นหา / ตัวกรอง / รายงานกล้องไม่มีสัญญาณ */
  .side-top { padding: 10px 12px 6px; gap: 8px; }
  .search label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .search-box svg { left: 12px; top: 12px; width: 18px; height: 18px; }
  /* font-size 16px: iPhone จะไม่ซูมหน้าจอเองตอนแตะช่องค้นหา */
  #searchInput { height: 42px; padding: 0 42px 0 38px; border-radius: 10px; font-size: 16px; }
  #clearSearch { width: 40px; height: 40px; top: 1px; font-size: 22px; }
  .seg { padding: 3px; border-radius: 10px; }
  .seg button { min-height: 36px; border-radius: 8px; font-size: 13px; }
  .offline-btn { min-height: 34px; font-size: 12px; }
  .side-foot { padding: 6px 12px; font-size: 11px; }
}
