:root {
  --bg: #0f1216;
  --card: #171b21;
  --sidebar: #12151a;
  --border: #262c35;
  --text: #e6e9ef;
  --muted: #8b93a1;
  --accent: #4da3ff;
  --ok: #3ecf8e;
  --warn: #f5b942;
  --err: #f2545b;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.9rem; }

.error {
  color: var(--err);
  font-size: 0.9rem;
  min-height: 1.2em;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 0.75rem 1.25rem;
  align-items: end;
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.85rem;
  color: var(--muted);
}

input, select {
  background: #0d0f13;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 0.5rem 0.6rem;
  font-size: 0.95rem;
}

button {
  background: var(--accent);
  color: #05131f;
  border: none;
  border-radius: 6px;
  padding: 0.55rem 1rem;
  font-weight: 600;
  cursor: pointer;
}

button.danger { background: var(--err); color: #fff; }
button.secondary { background: transparent; border: 1px solid var(--border); color: var(--text); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.link-btn {
  background: transparent;
  color: var(--accent);
  border: none;
  padding: 0;
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
  text-align: left;
}

.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  background: rgba(77, 163, 255, 0.1);
  border: 1px solid rgba(77, 163, 255, 0.35);
  color: var(--text);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-size: 0.85rem;
  margin-bottom: 0.75rem;
}

/* ------------------------------------------------------------------ */
/* Login view                                                          */
/* ------------------------------------------------------------------ */

.login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2rem;
}

.login-card h1 {
  margin: 0 0 0.25rem;
  font-size: 1.4rem;
}

.login-card form {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  margin-top: 1.25rem;
}

.connection-details {
  margin-top: 1.25rem;
  border-top: 1px solid var(--border);
  padding-top: 0.75rem;
}

.connection-details summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
  list-style: none;
}

.connection-details summary::-webkit-details-marker { display: none; }
.connection-details summary::before { content: "▸ "; }
.connection-details[open] summary::before { content: "▾ "; }

.connection-details .grid {
  margin-top: 0.75rem;
  grid-template-columns: 1fr;
  gap: 0.6rem;
}

/* ------------------------------------------------------------------ */
/* App shell: sidebar + main content                                   */
/* ------------------------------------------------------------------ */

.app-shell {
  display: flex;
  min-height: 100vh;
}

.mobile-topbar {
  display: none;
  align-items: center;
  gap: 0.75rem;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 52px;
  padding: 0 0.75rem;
  background: var(--sidebar);
  border-bottom: 1px solid var(--border);
  z-index: 120;
}

.mobile-topbar-title {
  font-weight: 700;
  font-size: 1rem;
}

.nav-toggle {
  background: transparent;
  border: none;
  padding: 0.4rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  width: 36px;
  height: 36px;
}

.nav-toggle span {
  display: block;
  height: 2px;
  width: 100%;
  background: var(--text);
  border-radius: 2px;
}

.nav-close {
  display: none;
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  background: transparent;
  color: var(--muted);
  border: none;
  font-size: 1.5rem;
  line-height: 1;
  padding: 0.2rem 0.5rem;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 150;
}

.sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1.25rem 1rem;
  gap: 1.25rem;
}

.sidebar-header {
  position: relative;
}

.sidebar-header h1 {
  margin: 0;
  font-size: 1.15rem;
}

.sidebar-header .muted {
  margin-top: 0.2rem;
  font-size: 0.75rem;
  word-break: break-word;
}

.sidebar-section {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.sidebar-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  flex: 1;
  overflow-y: auto;
}

.nav-item {
  background: transparent;
  color: var(--text);
  border: none;
  border-radius: 6px;
  padding: 0.55rem 0.6rem;
  font-weight: 600;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  width: 100%;
  text-align: left;
}

.nav-item:hover { background: #1c222b; }

.nav-badge {
  background: rgba(62, 207, 142, 0.18);
  color: var(--ok);
  font-size: 0.7rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 0.1rem 0.5rem;
}

.nav-sublist {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  margin: 0.1rem 0 0.5rem 0.75rem;
}

.nav-subitem {
  background: transparent;
  color: var(--muted);
  border: none;
  padding: 0.3rem 0.5rem;
  font-size: 0.8rem;
  border-radius: 5px;
  cursor: pointer;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  width: 100%;
}

.nav-subitem:hover { background: #1c222b; color: var(--text); }

.nav-subitem .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ok);
  flex-shrink: 0;
}

.sidebar-footer {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  border-top: 1px solid var(--border);
  padding-top: 0.75rem;
}

#main-content {
  flex: 1;
  padding: 2rem 2.5rem;
  max-width: 900px;
}

.page h2 {
  margin-top: 0;
  margin-bottom: 0;
  font-size: 1.1rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.page-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.page-toolbar h2 { margin: 0; }

.page-divider {
  height: 1px;
  background: var(--border);
  margin: 1.25rem 0 1.5rem;
}

.media-rows {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  max-height: 60vh;
  overflow-y: auto;
}

.media-row {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.7rem 0.85rem;
  background: #0d0f13;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.media-row-main {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.media-row-main .title-line {
  font-weight: 600;
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.media-row-main .meta {
  font-size: 0.75rem;
  color: var(--muted);
}

/* ------------------------------------------------------------------ */
/* Drones modal                                                        */
/* ------------------------------------------------------------------ */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  /* Must beat the mobile hamburger topbar (120) and off-canvas sidebar
   * (200) - a modal is always the top-most layer, full-screen ones on
   * mobile especially (otherwise the fixed topbar visually covers the
   * modal's own header/close button - a real bug caught live in a mobile
   * viewport screenshot: "Add drone" title and its close button were
   * entirely hidden behind the topbar). */
  z-index: 300;
  padding: 1.5rem;
}

.modal {
  width: 100%;
  max-width: 720px;
  max-height: 85vh;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal.modal-wide {
  max-width: 1100px;
}

.modal-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0;
  font-size: 1.05rem;
  flex: 1;
}

.modal-close {
  background: transparent;
  color: var(--muted);
  border: none;
  font-size: 1.4rem;
  line-height: 1;
  padding: 0.1rem 0.4rem;
  cursor: pointer;
}

.modal-close:hover { color: var(--text); }

.modal-body {
  padding: 1.25rem;
  overflow-y: auto;
}

.modal-toolbar {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 0.75rem;
}

.form-actions {
  display: flex;
  gap: 0.6rem;
  align-items: center;
}

.drone-rows {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.drone-row {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.7rem 0.85rem;
  background: #0d0f13;
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  align-items: center;
  gap: 0.75rem;
  cursor: pointer;
}

.drone-row:hover { border-color: #384154; }

.drone-row-main {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.drone-row-main .title-line {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 600;
  font-size: 0.9rem;
}

.drone-row-main .title-line span:first-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.drone-row-main .meta {
  font-size: 0.75rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.drone-row-main .detail.error-text { color: var(--err); font-size: 0.75rem; }

.badge {
  font-size: 0.7rem;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: 0.03em;
  flex-shrink: 0;
}

.badge.registering, .badge.connecting, .badge.requesting-sensor, .badge.joining-janus, .badge.stopping {
  background: rgba(245, 185, 66, 0.15);
  color: var(--warn);
}
.badge.publishing { background: rgba(62, 207, 142, 0.15); color: var(--ok); }
.badge.error { background: rgba(242, 84, 91, 0.15); color: var(--err); }
.badge.stopped { background: rgba(139, 147, 161, 0.15); color: var(--muted); }
.badge.saved { background: rgba(77, 163, 255, 0.15); color: var(--accent); }

.drone-menu { position: relative; flex-shrink: 0; }

.drone-menu-btn {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.1rem 0.5rem;
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.4;
}

.drone-menu-btn:hover, .drone-menu.open .drone-menu-btn {
  background: #1c222b;
  color: var(--text);
}

.drone-menu-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  min-width: 140px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.3rem;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  z-index: 10;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.drone-menu-dropdown button {
  background: transparent;
  color: var(--text);
  border: none;
  border-radius: 5px;
  padding: 0.4rem 0.6rem;
  font-weight: 500;
  font-size: 0.85rem;
  text-align: left;
  cursor: pointer;
  width: 100%;
}

.drone-menu-dropdown button:hover { background: #1c222b; }
.drone-menu-dropdown button.danger { color: var(--err); background: transparent; }
.drone-menu-dropdown button.danger:hover { background: rgba(242, 84, 91, 0.12); }

.drone-menu-divider {
  height: 1px;
  margin: 0.25rem 0.1rem;
  background: var(--border);
}

/* ------------------------------------------------------------------ */
/* Activity log drawer                                                  */
/* ------------------------------------------------------------------ */

.activity-drawer {
  position: fixed;
  bottom: 0;
  right: 1.5rem;
  width: min(480px, calc(100vw - 3rem));
  max-height: 40vh;
  background: var(--card);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 10px 10px 0 0;
  display: flex;
  flex-direction: column;
  z-index: 90;
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.4);
}

.activity-drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 0.9rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

#event-log {
  margin: 0;
  padding: 0.75rem 0.9rem;
  font-size: 0.78rem;
  white-space: pre-wrap;
  overflow-y: auto;
  color: var(--muted);
}

/* ------------------------------------------------------------------ */
/* Responsive / mobile - hamburger nav, off-canvas sidebar             */
/* ------------------------------------------------------------------ */

@media (max-width: 860px) {
  .mobile-topbar {
    display: flex;
  }

  .app-shell {
    padding-top: 52px;
  }

  .nav-close {
    display: block;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: min(80vw, 300px);
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.4);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  #main-content {
    padding: 1.25rem 1rem;
    max-width: 100%;
  }

  .page-toolbar {
    flex-wrap: wrap;
    gap: 0.6rem;
  }

  /* Drone/media rows: let the main info block force a wrap onto its own
   * line, so the action button(s) end up together on the next line
   * instead of getting squeezed into a cramped multi-column grid - no JS
   * changes needed, the DOM order (main, then buttons) already supports
   * this with a plain flex-wrap. */
  .drone-row {
    display: flex;
    flex-wrap: wrap;
  }

  .drone-row-main,
  .media-row-main {
    flex: 1 1 100%;
  }

  .media-row {
    flex-wrap: wrap;
  }

  /* Full-screen modals - much friendlier than a floating centered card
   * once a virtual keyboard is involved. */
  .modal-overlay {
    padding: 0;
  }

  .modal {
    max-width: 100%;
    width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
  }

  .login-view {
    padding: 1rem;
  }

  .login-card {
    padding: 1.5rem;
  }

  .activity-drawer {
    left: 0;
    right: 0;
    width: 100%;
    max-height: 50vh;
    border-radius: 0;
  }
}

@media (min-width: 861px) {
  /* Belt-and-suspenders: never let a stale mobile-only open/backdrop
   * state (e.g. from resizing a window that started narrow) linger once
   * the layout is wide enough for the sidebar to be a normal, always-
   * visible flex column again. */
  .sidebar-backdrop {
    display: none !important;
  }
}

/* ------------------------------------------------------------------ */
/* Flight paths                                                        */
/* ------------------------------------------------------------------ */

.flight-path-rows {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.flight-path-row {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.7rem 0.85rem;
  background: #0d0f13;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.flight-path-row-main {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  flex: 1 1 auto;
}

.flight-path-row-main .title-line {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 600;
  font-size: 0.9rem;
}

.flight-path-row-main .meta {
  font-size: 0.75rem;
  color: var(--muted);
}

.flight-path-editor {
  display: flex;
  gap: 1.25rem;
  align-items: flex-start;
}

.flight-path-map-col {
  flex: 1 1 55%;
  min-width: 0;
  position: sticky;
  top: 0;
}

.flight-path-map {
  width: 100%;
  height: 420px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #0d0f13;
}

.flight-path-locate-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.flight-path-locate-row input[type="text"] {
  flex: 1 1 auto;
  min-width: 0;
}

.flight-path-locate-row button {
  flex: 0 0 auto;
  white-space: nowrap;
}

.flight-path-address-results {
  position: relative;
  z-index: 5;
  margin: -0.35rem 0 0.5rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  max-height: 180px;
  overflow-y: auto;
}

.flight-path-address-result {
  padding: 0.5rem 0.75rem;
  cursor: pointer;
  font-size: 0.85rem;
  border-bottom: 1px solid var(--border);
}

.flight-path-address-result:last-child {
  border-bottom: none;
}

.flight-path-address-result:not(.muted):hover {
  background: rgba(255, 255, 255, 0.06);
}

.flight-path-address-result.muted {
  cursor: default;
}

#flight-path-locate-error {
  margin: -0.25rem 0 0.5rem;
  font-size: 0.82rem;
}

#flight-path-locate-error:empty {
  display: none;
}

.flight-path-map-hint {
  margin: 0.5rem 0;
}

.flight-path-map-modes {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.flight-path-map-modes button.active,
.flight-path-action-set-on-map.active {
  background: var(--accent);
  color: #05131f;
  border-color: var(--accent);
}

.flight-path-form-col {
  flex: 1 1 45%;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.flight-path-form-col h3 {
  margin: 0.25rem 0 0;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.flight-path-point-fields {
  grid-template-columns: 1fr 1fr;
}

.checkbox-label {
  flex-direction: row !important;
  align-items: center;
  gap: 0.5rem !important;
}

.flight-path-actions-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 240px;
  overflow-y: auto;
}

.flight-path-action-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.7rem;
  background: #0d0f13;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.flight-path-action-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.flight-path-action-card-header .badge {
  background: rgba(77, 163, 255, 0.15);
  color: var(--accent);
}

.flight-path-action-card-buttons {
  display: flex;
  gap: 0.25rem;
}

.flight-path-action-card-buttons button {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 0.15rem 0.45rem;
  font-size: 0.85rem;
}

.flight-path-action-card-buttons button:hover {
  color: var(--text);
  background: #1c222b;
}

.flight-path-action-fields {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 0.5rem;
}

.flight-path-action-fields label {
  font-size: 0.75rem;
}

.flight-path-action-fields input,
.flight-path-action-fields select {
  padding: 0.35rem 0.5rem;
  font-size: 0.85rem;
}

.flight-path-add-action {
  display: flex;
  gap: 0.5rem;
}

.flight-path-add-action select {
  flex: 1;
}

@media (max-width: 960px) {
  .flight-path-editor {
    flex-direction: column;
  }

  .flight-path-map-col {
    position: static;
    width: 100%;
  }
}

/* Leaflet's own div gets a solid background + our border-radius applied
 * via the wrapper above - just make sure leaflet's internal panes don't
 * poke outside that rounded corner. */
.flight-path-map .leaflet-container {
  background: #0d0f13;
  border-radius: 8px;
  font-family: inherit;
}

