:root {
  --bg: #0e1116;
  --bg-card: #171c24;
  --bg-raised: #1f2630;
  --line: #2a3340;
  --text: #e8edf4;
  --text-dim: #8b98a9;
  --accent: #4da3ff;
  --accent-soft: rgba(77, 163, 255, 0.15);
  --green: #3ddc84;
  --red: #ff6b6b;
  --radius: 14px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Pretendard", -apple-system, BlinkMacSystemFont, "Malgun Gothic", sans-serif;
  overscroll-behavior-y: none;
}

/* ---------- 상단바 / 하단탭 ---------- */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: calc(52px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 16px 0;
  display: flex; align-items: center; gap: 8px;
  background: rgba(14, 17, 22, 0.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
  z-index: 50;
}
.topbar h1 { font-size: 17px; font-weight: 700; }
.topbar-sub { font-size: 12px; color: var(--text-dim); }

.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  display: flex;
  background: rgba(14, 17, 22, 0.95);
  backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 50;
}
.nav-btn {
  flex: 1;
  padding: 8px 0 10px;
  background: none; border: none;
  color: var(--text-dim);
  font-size: 11px; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  cursor: pointer;
}
.nav-btn .nav-ico { font-size: 20px; }
.nav-btn.active { color: var(--accent); }

/* ---------- 탭 공통 ---------- */
.tab {
  display: none;
  position: fixed;
  top: calc(52px + env(safe-area-inset-top));
  bottom: calc(56px + env(safe-area-inset-bottom));
  left: 0; right: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.tab.active { display: block; }

/* ---------- 피드 ---------- */
#tab-feed { scroll-snap-type: y proximity; padding: 12px 12px 24px; }

.group-section { margin-bottom: 20px; }
.group-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex; align-items: baseline; gap: 8px;
  padding: 8px 4px;
  background: linear-gradient(var(--bg) 70%, transparent);
}
.group-header h2 { font-size: 18px; font-weight: 800; }
.group-header .cnt { font-size: 12px; color: var(--text-dim); }

.cctv-card {
  scroll-snap-align: start;
  scroll-margin-top: 40px;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 12px;
}
.cctv-video-wrap {
  position: relative;
  aspect-ratio: 16 / 10;
  background: #000;
}
.cctv-video-wrap video {
  width: 100%; height: 100%;
  object-fit: contain;
  display: block;
}
.cctv-status {
  position: absolute;
  inset: 0;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-dim);
  font-size: 13px;
  text-align: center;
  padding: 12px;
  pointer-events: none;
}
.cctv-status.hidden { display: none; }
.live-dot {
  position: absolute;
  top: 8px; left: 8px;
  font-size: 10px; font-weight: 700;
  color: #fff;
  background: rgba(220, 40, 60, 0.9);
  padding: 2px 7px;
  border-radius: 20px;
  letter-spacing: 0.5px;
  display: none;
}
.cctv-card.playing .live-dot { display: block; }
.cctv-meta {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px;
}
.cctv-meta .name { font-size: 14px; font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  font-size: 10px; font-weight: 700;
  padding: 2px 7px; border-radius: 20px;
  flex-shrink: 0;
}
.badge.ex { background: rgba(77, 163, 255, 0.18); color: var(--accent); }
.badge.its { background: rgba(61, 220, 132, 0.15); color: var(--green); }

/* ---------- 빈 상태 / 온보딩 ---------- */
.empty {
  margin: 40px 16px;
  padding: 28px 20px;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  text-align: center;
}
.empty .big { font-size: 40px; margin-bottom: 12px; }
.empty h3 { font-size: 16px; margin-bottom: 8px; }
.empty p { font-size: 13px; color: var(--text-dim); line-height: 1.6; margin-bottom: 16px; }
.empty ol { text-align: left; font-size: 13px; color: var(--text-dim); line-height: 1.9; margin: 0 auto 16px; max-width: 300px; padding-left: 20px; }

/* ---------- 버튼 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: none; cursor: pointer;
  border-radius: 10px;
  font-size: 14px; font-weight: 700;
  padding: 11px 18px;
  background: var(--accent);
  color: #06131f;
}
.btn.ghost { background: var(--bg-raised); color: var(--text); border: 1px solid var(--line); }
.btn.small { padding: 7px 12px; font-size: 12px; border-radius: 8px; }
.btn.danger { background: rgba(255, 107, 107, 0.15); color: var(--red); }
.icon-btn {
  background: var(--bg-raised);
  border: 1px solid var(--line);
  color: var(--text-dim);
  width: 30px; height: 30px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  flex-shrink: 0;
}
.icon-btn:disabled { opacity: 0.3; cursor: default; }

/* ---------- 탐색(지도) ---------- */
#tab-explore { display: none; flex-direction: column; overflow: hidden; }
#tab-explore.active { display: flex; }
.explore-toolbar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  z-index: 20;
}
.explore-toolbar select {
  background: var(--bg-raised);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 13px;
}
.chk { font-size: 13px; color: var(--text-dim); display: flex; align-items: center; gap: 4px; }
.chk input { accent-color: var(--accent); }
#map { flex: 1; background: #1a1e26; }
.map-hint {
  position: absolute;
  bottom: 16px; left: 50%;
  transform: translateX(-50%);
  background: rgba(14, 17, 22, 0.85);
  border: 1px solid var(--line);
  color: var(--text-dim);
  font-size: 12px;
  padding: 7px 14px;
  border-radius: 20px;
  z-index: 500;
  pointer-events: none;
  transition: opacity 0.3s;
}
.map-hint.hidden { opacity: 0; }
.leaflet-container { font: inherit; }

/* ---------- 그룹 관리 ---------- */
#tab-groups, #tab-settings { padding: 14px 14px 24px; }
.section-title {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px;
}
.section-title h2 { font-size: 17px; }

.group-card {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 12px;
  overflow: hidden;
}
.group-card-head {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 14px;
}
.group-card-head .g-name { font-size: 15px; font-weight: 700; flex: 1; min-width: 0; }
.group-card-head .g-cnt { font-size: 12px; color: var(--text-dim); }
.group-items { border-top: 1px solid var(--line); }
.group-item {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 14px;
  border-bottom: 1px solid rgba(42, 51, 64, 0.5);
  font-size: 13px;
}
.group-item:last-child { border-bottom: none; }
.group-item .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- 설정 ---------- */
.settings-card {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}
.settings-card h3 { font-size: 15px; margin-bottom: 10px; }
.settings-card p { font-size: 13px; color: var(--text-dim); line-height: 1.7; margin-bottom: 10px; }
.settings-card ol { font-size: 13px; color: var(--text-dim); line-height: 1.9; padding-left: 20px; margin-bottom: 12px; }
.settings-card a { color: var(--accent); }
.key-row { display: flex; gap: 8px; }
.key-row input {
  flex: 1; min-width: 0;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 13px;
}
.key-row input:focus { outline: none; border-color: var(--accent); }
.key-status { font-size: 12px; margin-top: 8px; min-height: 16px; }
.key-status.ok { color: var(--green); }
.key-status.bad { color: var(--red); }

/* ---------- 바텀 시트 ---------- */
.sheet-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 90;
}
.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  background: var(--bg-card);
  border-top: 1px solid var(--line);
  border-radius: 18px 18px 0 0;
  padding: 14px 16px calc(20px + env(safe-area-inset-bottom));
  z-index: 100;
  max-height: 80vh;
  overflow-y: auto;
}
.sheet.hidden, .sheet-backdrop.hidden { display: none; }
.sheet-grab { width: 40px; height: 4px; border-radius: 4px; background: var(--line); margin: 0 auto 12px; }
.sheet h3 { font-size: 15px; margin-bottom: 4px; }
.sheet .sheet-sub { font-size: 12px; color: var(--text-dim); margin-bottom: 12px; }
.sheet-video {
  width: 100%;
  aspect-ratio: 16 / 10;
  background: #000;
  border-radius: 10px;
  margin-bottom: 14px;
  object-fit: contain;
}
.sheet-groups { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.sheet-group-btn {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 14px; font-weight: 600;
  cursor: pointer;
}
.sheet-group-btn.added { border-color: var(--green); color: var(--green); }
.new-group-row { display: flex; gap: 8px; }
.new-group-row input {
  flex: 1; min-width: 0;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 14px;
}

/* ---------- 토스트 ---------- */
.toast {
  position: fixed;
  bottom: calc(76px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-raised);
  border: 1px solid var(--line);
  color: var(--text);
  font-size: 13px; font-weight: 600;
  padding: 10px 18px;
  border-radius: 24px;
  z-index: 200;
  white-space: nowrap;
  transition: opacity 0.25s;
}
.toast.hidden { opacity: 0; pointer-events: none; }

@media (min-width: 700px) {
  #tab-feed, #tab-groups, #tab-settings { max-width: 560px; margin: 0 auto; }
}
