/* =========================================================================
   CCTV Bandar Lampung — Design System & Layout
   Mobile-first, dark-by-default, support TV wall / ultrawide
   ========================================================================= */

:root {
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;

  /* Theme tokens — dark */
  --bg-page: #0b1020;
  --bg-surface: #12182c;
  --bg-surface-elevated: #1a233f;
  --bg-input: #1a2238;

  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.16);

  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;

  --accent: #38bdf8;
  --accent-hover: #0284c7;
  --accent-contrast: #082f49;

  --macet-color: #f97316;
  --strategis-color: #38bdf8;
  --ptz-color: #a855f7;

  --status-online: #22c55e;
  --status-offline: #ef4444;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-full: 9999px;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.6);

  --header-h: 56px;
  --drawer-w: 420px;
}

[data-theme="light"] {
  --bg-page: #f8fafc;
  --bg-surface: #ffffff;
  --bg-surface-elevated: #f1f5f9;
  --bg-input: #e2e8f0;

  --border: rgba(0, 0, 0, 0.08);
  --border-strong: rgba(0, 0, 0, 0.16);

  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.12);
}

/* Base reset */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  font-family: var(--font-sans);
  background: var(--bg-page);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* App container */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
  position: relative;
  overflow: hidden;
}

/* Topbar */
.topbar {
  /* min-height, not height: wrapping to a second row on phones must not clip
     the tabs, and #app is a flex column so .layout still absorbs the rest. */
  min-height: var(--header-h);
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  z-index: 30;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  white-space: nowrap;
}

.brand-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--status-online);
  box-shadow: 0 0 10px var(--status-online);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.searchbar {
  position: relative;
  flex: 1;
  max-width: 320px;
}

.searchbar svg {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  color: var(--text-muted);
  pointer-events: none;
}

.searchbar input {
  width: 100%;
  height: 34px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0 12px 0 34px;
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s;
}

.searchbar input:focus {
  border-color: var(--accent);
}

.tabs {
  display: flex;
  gap: 4px;
  background: var(--bg-input);
  padding: 3px;
  border-radius: var(--radius-md);
  min-width: 0;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  border: none;
  background: none;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  padding: 5px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.tab:hover {
  color: var(--text-primary);
}

.tab.is-active {
  background: var(--bg-surface-elevated);
  color: var(--text-primary);
  box-shadow: var(--shadow-sm);
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover {
  background: var(--bg-surface-elevated);
  color: var(--text-primary);
}

.icon-btn svg {
  width: 18px;
  height: 18px;
}

/* Layout */
.layout {
  flex: 1;
  display: flex;
  position: relative;
  overflow: hidden;
}

/* Split view: Map + List */
.split {
  display: flex;
  flex: 1;
  width: 100%;
  height: 100%;
  position: relative;
}

.map {
  flex: 1;
  height: 100%;
  background: #111;
}

.list {
  width: 380px;
  height: 100%;
  background: var(--bg-surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 20;
}

.list-head {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--bg-surface);
}

.list-count {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
}

.list-actions {
  display: flex;
  gap: 6px;
}

.list-actions select {
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 4px 8px;
  font-size: 11px;
  outline: none;
}

.cards {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Camera Card */
.card {
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 8px;
  display: grid;
  grid-template-columns: 88px 1fr auto;
  gap: 10px;
  align-items: center;
  cursor: pointer;
  transition: all 0.15s;
}

.card:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

.card.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.card-thumb {
  position: relative;
  width: 88px;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.card-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.card-status {
  position: absolute;
  top: 4px;
  left: 4px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--status-online);
}

.card-status[data-status="offline"] {
  background: var(--status-offline);
}

.card-source {
  position: absolute;
  bottom: 3px;
  right: 3px;
  font-size: 8px;
  padding: 1px 4px;
  border-radius: 3px;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  text-transform: uppercase;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.card-body {
  min-width: 0;
}

.card-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-addr {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-tags {
  display: flex;
  gap: 4px;
  margin-top: 3px;
}

.tag-badge {
  font-size: 9px;
  padding: 1px 5px;
  border-radius: var(--radius-full);
  font-weight: 600;
  text-transform: uppercase;
}

.tag-badge.tag-macet {
  background: rgba(249, 115, 22, 0.2);
  color: var(--macet-color);
}

.tag-badge.tag-strategis {
  background: rgba(56, 189, 248, 0.2);
  color: var(--strategis-color);
}

.tag-badge.tag-ptz {
  background: rgba(168, 85, 247, 0.2);
  color: var(--ptz-color);
}

.card-actions {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* Buttons */
.btn {
  background: var(--accent);
  color: var(--accent-contrast);
  font-weight: 600;
  border: none;
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s;
}

.btn:hover {
  background: var(--accent-hover);
}

.btn-sm {
  padding: 4px 8px;
  font-size: 11px;
}

.btn-ghost {
  background: var(--bg-input);
  color: var(--text-primary);
}

.btn-ghost:hover {
  background: var(--bg-surface-elevated);
}

/* Drawer */
.drawer {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: var(--drawer-w);
  max-width: 100%;
  background: var(--bg-surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  z-index: 40;
  display: flex;
  flex-direction: column;
  transform: translateX(0);
  transition: transform 0.25s ease-out;
}

.drawer[hidden] {
  display: none !important;
}

.drawer-head {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.drawer-head h2 {
  font-size: 14px;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.drawer-player {
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: var(--radius-md);
  overflow: hidden;
  position: relative;
}

.drawer-player video,
.drawer-player iframe {
  width: 100%;
  height: 100%;
  border: none;
  display: block;
}

.meta {
  display: grid;
  grid-template-columns: 110px 1fr;
  gap: 6px 12px;
  font-size: 12px;
}

.meta dt {
  color: var(--text-muted);
}

.meta dd {
  color: var(--text-primary);
  font-weight: 500;
}

.drawer-actions {
  display: flex;
  gap: 8px;
}

/* Multi-Stream View */
.multistream {
  position: absolute;
  inset: 0;
  background: var(--bg-page);
  z-index: 50;
  display: flex;
  flex-direction: column;
}

.multistream[hidden] {
  display: none !important;
}

.ms-header {
  height: 48px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
}

.ms-presets {
  display: flex;
  gap: 6px;
}

.ms-preset {
  background: var(--bg-input);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 4px 10px;
  font-size: 11px;
  cursor: pointer;
}

.ms-preset.is-active {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
  font-weight: 600;
}

.ms-actions {
  display: flex;
  gap: 6px;
}

.ms-grid {
  flex: 1;
  padding: 8px;
  gap: 8px;
  display: grid;
  background: #000;
  overflow: hidden;
}

/* Multi-stream tile */
.ms-tile {
  background: #0b1020;
  border-radius: var(--radius-sm);
  overflow: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.ms-tile-header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: 6px 8px;
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0.8), transparent);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
  z-index: 10;
  pointer-events: none;
}

.ms-tile-content {
  flex: 1;
  width: 100%;
  height: 100%;
  position: relative;
  background: #000;
}

.ms-tile-content video,
.ms-tile-content iframe {
  width: 100%;
  height: 100%;
  border: none;
  object-fit: cover;
  display: block;
}

/* Responsive Viewports */
@media (max-width: 900px) {
  .list {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 45vh;
    border-left: none;
    border-top: 1px solid var(--border);
  }

  .tabs {
    overflow-x: auto;
    max-width: 50vw;
  }
}

@media (max-width: 600px) {
  .topbar {
    padding: 8px 10px;
    gap: 8px;
    flex-wrap: wrap;
    min-height: auto;
  }
  .brand-text strong {
    display: none;
  }
  .searchbar {
    max-width: none;
    flex: 1;
    min-width: 140px;
  }
  /* On phones, tabs break into a full-width horizontally scrollable row so
     "Pantau Macet", "Lokasi Strategis", and "PTZ" stay directly reachable. */
  .tabs {
    order: 10;
    width: 100%;
    max-width: 100%;
    display: flex;
    overflow-x: auto;
    gap: 4px;
    padding: 3px;
  }
  .tab {
    flex: 1 0 auto;
    text-align: center;
    white-space: nowrap;
    padding: 5px 8px;
  }
  .card {
    grid-template-columns: 88px 1fr;
  }
  .card-actions {
    grid-column: 1 / -1;
    flex-direction: row;
  }

  /* Intersection multi-stream layout on phones: stack main camera on top (2/3 height)
     and 3 PiPs side-by-side underneath (1/3 height) so PiP tiles aren't crushed to 84px. */
  .ms-grid[data-preset="intersection"] {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    grid-template-rows: 2fr 1fr !important;
    grid-template-areas:
      "m m m"
      "p1 p2 p3" !important;
  }
}

/* MapLibre popup & marker styling */
.maplibregl-popup-content {
  background: var(--bg-surface) !important;
  color: var(--text-primary) !important;
  border: 1px solid var(--border) !important;
  border-radius: var(--radius-md) !important;
  padding: 10px !important;
  box-shadow: var(--shadow-md) !important;
}

.maplibregl-popup-anchor-top .maplibregl-popup-tip {
  border-bottom-color: var(--bg-surface) !important;
}
.maplibregl-popup-anchor-bottom .maplibregl-popup-tip {
  border-top-color: var(--bg-surface) !important;
}

.cctv-marker {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid #fff;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
  transition: transform 0.15s;
}

.cctv-marker:hover {
  transform: scale(1.3);
  z-index: 999;
}

.cctv-marker.macet {
  background: var(--macet-color);
}

.cctv-marker.strategis {
  background: var(--strategis-color);
}

.cctv-marker.ptz {
  background: var(--ptz-color);
}