/* Sidebar Navigation Styles */

/* CSS Variables */
:root {
  --sidebar-width: 260px;
  --sidebar-collapsed-width: 60px;
  --sidebar-bg: #f8f9fa;
  --sidebar-border: #dee2e6;
  --sidebar-hover-bg: #e9ecef;
  --sidebar-active-bg: #0d6efd;
  --sidebar-active-color: #fff;
  --topbar-height: 56px;
  --transition-speed: 0.3s;
}

/* Top Navigation Bar */
.top-navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--topbar-height);
  background: #fff;
  border-bottom: 1px solid var(--sidebar-border);
  z-index: 1030;
  display: flex;
  align-items: center;
  padding: 0 1rem;
}

.sidebar-toggle {
  background: none;
  border: none;
  font-size: 1.25rem;
  padding: 0.5rem;
  cursor: pointer;
  color: #333;
  margin-right: 1rem;
}

.sidebar-toggle:hover {
  background: var(--sidebar-hover-bg);
  border-radius: 4px;
}

/* Body Padding */
body {
  padding-top: var(--topbar-height);
}

/* Layout Container */
.layout-container {
  display: flex;
  min-height: calc(100vh - var(--topbar-height));
}

.main-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.main-content {
  flex: 1;
}

/* Sidebar Base Styles */
.sidebar {
  position: fixed;
  top: var(--topbar-height);
  left: 0;
  width: var(--sidebar-width);
  height: calc(100vh - var(--topbar-height));
  background: var(--sidebar-bg);
  border-right: 1px solid var(--sidebar-border);
  overflow-y: auto;
  overflow-x: hidden;
  transition: all var(--transition-speed) ease;
  z-index: 1000;
}

.sidebar-collapsed .sidebar {
  width: var(--sidebar-collapsed-width);
}

/* Sidebar Header */
.sidebar-header {
  padding: 1rem;
  border-bottom: 1px solid var(--sidebar-border);
  text-align: center;
}

.sidebar-header img {
  max-width: 100%;
  height: auto;
  max-height: 40px;
}

.sidebar-collapsed .sidebar-header img {
  max-height: 30px;
}

/* Sidebar Navigation */
.sidebar-nav {
  list-style: none;
  padding: 0;
  margin: 0;
}

.sidebar-section {
  margin: 0;
}

.sidebar-section > a {
  display: flex;
  align-items: center;
  padding: 12px 20px;
  color: #333;
  text-decoration: none;
  transition: background 0.2s;
  cursor: pointer;
  font-weight: 500;
}

.sidebar-section > a:hover {
  background: var(--sidebar-hover-bg);
}

.sidebar-section > a i {
  margin-right: 10px;
  width: 20px;
  text-align: center;
  font-size: 1.1rem;
}

.sidebar-section > a .section-text {
  flex: 1;
}

.sidebar-section > a .toggle-icon {
  font-size: 0.8rem;
  transition: transform 0.2s;
}

.sidebar-section > a[aria-expanded="true"] .toggle-icon {
  transform: rotate(90deg);
}

/* Submenu Styles */
.sidebar-submenu {
  list-style: none;
  padding-left: 0;
  background: rgba(0,0,0,0.02);
}

.sidebar-submenu li a {
  display: block;
  padding: 8px 20px 8px 50px;
  color: #666;
  text-decoration: none;
  transition: all 0.2s;
  font-size: 0.95rem;
}

.sidebar-submenu li a:hover {
  background: var(--sidebar-hover-bg);
  color: #333;
}

.sidebar-submenu li a.active {
  background: var(--sidebar-active-bg);
  color: var(--sidebar-active-color);
  font-weight: 500;
}

/* Collapsed State - Hide Text, Show Icons Only */
.sidebar-collapsed .sidebar-section > a .section-text,
.sidebar-collapsed .sidebar-section > a .toggle-icon,
.sidebar-collapsed .sidebar-submenu {
  display: none;
}

.sidebar-collapsed .sidebar-section > a {
  justify-content: center;
  padding: 12px;
}

.sidebar-collapsed .sidebar-section > a i {
  margin-right: 0;
}

/* Desktop: ≥992px - Sidebar Pushes Content */
@media (min-width: 992px) {
  .main-wrapper {
    margin-left: var(--sidebar-width);
    transition: margin-left var(--transition-speed) ease;
  }

  .sidebar-collapsed .main-wrapper {
    margin-left: var(--sidebar-collapsed-width);
  }

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

/* Tablet: 768-991px - Hybrid Behavior */
@media (min-width: 768px) and (max-width: 991px) {
  .sidebar {
    transform: translateX(-100%);
    z-index: 1045; /* Higher than Leaflet map controls (1000) */
  }

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

  .sidebar-open .main-wrapper {
    margin-left: var(--sidebar-width);
  }

  .main-wrapper {
    margin-left: 0;
    transition: margin-left var(--transition-speed) ease;
  }
}

/* Mobile: <768px - Overlay Drawer */
@media (max-width: 767px) {
  .sidebar {
    transform: translateX(-100%);
    box-shadow: 2px 0 10px rgba(0,0,0,0.1);
    z-index: 1045; /* Higher than Leaflet map controls (1000) and Bootstrap offcanvas */
  }

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

  /* Backdrop Overlay */
  body.sidebar-open::before {
    content: '';
    position: fixed;
    top: var(--topbar-height);
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 1044; /* Just below sidebar */
  }

  .main-wrapper {
    margin-left: 0;
  }
}

/* Scrollbar Styling for Sidebar */
.sidebar::-webkit-scrollbar {
  width: 6px;
}

.sidebar::-webkit-scrollbar-track {
  background: var(--sidebar-bg);
}

.sidebar::-webkit-scrollbar-thumb {
  background: var(--sidebar-border);
  border-radius: 3px;
}

.sidebar::-webkit-scrollbar-thumb:hover {
  background: #adb5bd;
}

/* Organization Selector Styles */
.sidebar-org-selector {
  margin: 8px;
  list-style: none;
}

.sidebar-org-selector .dropdown {
  width: 100%;
}

.org-selector-toggle {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  background: rgba(13, 110, 253, 0.1);
  border: 1px solid rgba(13, 110, 253, 0.2);
  border-radius: 8px;
  color: #333;
  text-decoration: none;
  transition: all 0.2s;
  cursor: pointer;
  font-weight: 500;
}

.org-selector-toggle:hover {
  background: rgba(13, 110, 253, 0.15);
  border-color: rgba(13, 110, 253, 0.3);
  color: #333;
}

.org-selector-toggle i.bi-building {
  font-size: 1.1rem;
  color: var(--bs-primary, #0d6efd);
  flex-shrink: 0;
}

.org-selector-toggle .org-selector-text {
  flex: 1;
  margin: 0 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.org-selector-toggle .toggle-chevron {
  font-size: 0.75rem;
  opacity: 0.6;
  flex-shrink: 0;
  transition: transform 0.2s;
}

.org-selector-toggle[aria-expanded="true"] .toggle-chevron {
  transform: rotate(180deg);
}

.org-dropdown-menu {
  width: calc(var(--sidebar-width) - 16px);
  max-height: 300px;
  overflow-y: auto;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.org-dropdown-menu .dropdown-item {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.org-dropdown-menu .dropdown-item.active {
  background-color: var(--bs-primary, #0d6efd);
  color: #fff;
}

.org-dropdown-menu .dropdown-item.active .badge {
  background-color: rgba(255, 255, 255, 0.2) !important;
}

/* Collapsed state - show only icon */
.sidebar-collapsed .sidebar-org-selector .org-selector-text,
.sidebar-collapsed .sidebar-org-selector .toggle-chevron {
  display: none;
}

.sidebar-collapsed .sidebar-org-selector .org-selector-toggle {
  justify-content: center;
  padding: 10px;
}

.sidebar-collapsed .sidebar-org-selector .org-selector-toggle i.bi-building {
  margin: 0;
}

/* Organization dropdown - allow overflow to the right */
.sidebar:has(.org-selector-toggle[aria-expanded="true"]) {
  overflow: visible;
}

.org-dropdown-menu {
  min-width: 280px;
  z-index: 1050;
}

/* In collapsed state, use dropend behavior */
.sidebar-collapsed .sidebar-org-selector .dropdown {
  position: static;
}

.sidebar-collapsed .sidebar-org-selector .dropdown .org-dropdown-menu {
  position: absolute;
  left: var(--sidebar-collapsed-width);
  top: 0;
  margin-top: 0;
}

/* Quick Create Panel Styles */
.quick-create-section {
  background: linear-gradient(135deg, #667eea 0%, #0d6efd 100%);
  margin: 0 8px 8px 8px;
  border-radius: 8px;
}

.quick-create-section > a {
  color: #fff;
  font-weight: 600;
  padding: 12px 16px;
}

.quick-create-section > a:hover {
  background: rgba(255, 255, 255, 0.1);
}

.quick-create-section > a i.bi-plus-circle-fill {
  color: #ffd700;
}

.quick-create-form {
  background: #f8f9fa;
}

.quick-create-form .form-label {
  font-weight: 600;
  margin-bottom: 4px;
}

.sidebar-divider {
  border-top: 1px solid var(--sidebar-border);
  margin: 8px 16px;
  list-style: none;
}

/* Alert styles for feedback */
.alert-sm {
  font-size: 0.875rem;
}

/* Collapsed state - hide quick create */
.sidebar-collapsed .quick-create-section {
  display: none;
}

/* ========================================
   Dark Mode Styles
   ======================================== */

[data-bs-theme="dark"] {
  --sidebar-bg: #212529;
  --sidebar-border: #495057;
  --sidebar-hover-bg: #343a40;
  --sidebar-active-bg: #0d6efd;
  --sidebar-active-color: #fff;
}

[data-bs-theme="dark"] .top-navbar {
  background: #212529;
  border-bottom-color: #495057;
}

[data-bs-theme="dark"] .sidebar-toggle {
  color: #e9ecef;
}

[data-bs-theme="dark"] .sidebar-toggle:hover {
  background: var(--sidebar-hover-bg);
}

[data-bs-theme="dark"] .sidebar {
  background: var(--sidebar-bg);
  border-right-color: var(--sidebar-border);
}

[data-bs-theme="dark"] .sidebar-header {
  border-bottom-color: var(--sidebar-border);
}

[data-bs-theme="dark"] .sidebar-section > a {
  color: #e9ecef;
}

[data-bs-theme="dark"] .sidebar-section > a:hover {
  background: var(--sidebar-hover-bg);
}

[data-bs-theme="dark"] .sidebar-submenu {
  background: rgba(255, 255, 255, 0.02);
}

[data-bs-theme="dark"] .sidebar-submenu li a {
  color: #adb5bd;
}

[data-bs-theme="dark"] .sidebar-submenu li a:hover {
  background: var(--sidebar-hover-bg);
  color: #e9ecef;
}

[data-bs-theme="dark"] .sidebar-submenu li a.active {
  background: var(--sidebar-active-bg);
  color: var(--sidebar-active-color);
}

[data-bs-theme="dark"] .sidebar-divider {
  border-top-color: var(--sidebar-border);
}

[data-bs-theme="dark"] .quick-create-section {
  background: linear-gradient(135deg, #4a5568 0%, #2d3748 100%);
}

[data-bs-theme="dark"] .quick-create-form {
  background: #2d3748;
}

/* Organization Selector Dark Mode */
[data-bs-theme="dark"] .org-selector-toggle {
  background: rgba(99, 179, 237, 0.15);
  border-color: rgba(99, 179, 237, 0.25);
  color: #e9ecef;
}

[data-bs-theme="dark"] .org-selector-toggle:hover {
  background: rgba(99, 179, 237, 0.2);
  border-color: rgba(99, 179, 237, 0.35);
  color: #fff;
}

[data-bs-theme="dark"] .org-selector-toggle i.bi-building {
  color: #63b3ed;
}

[data-bs-theme="dark"] .org-dropdown-menu {
  background-color: #2d3748;
  border-color: #4a5568;
}

[data-bs-theme="dark"] .org-dropdown-menu .dropdown-header {
  color: #a0aec0;
}

[data-bs-theme="dark"] .org-dropdown-menu .dropdown-item {
  color: #e2e8f0;
}

[data-bs-theme="dark"] .org-dropdown-menu .dropdown-item:hover {
  background-color: #4a5568;
  color: #fff;
}

[data-bs-theme="dark"] .org-dropdown-menu .dropdown-divider {
  border-color: #4a5568;
}

/* Mobile backdrop in dark mode */
@media (max-width: 767px) {
  [data-bs-theme="dark"] body.sidebar-open::before {
    background: rgba(0, 0, 0, 0.7);
  }
}

/* Scrollbar in dark mode */
[data-bs-theme="dark"] .sidebar::-webkit-scrollbar-track {
  background: var(--sidebar-bg);
}

[data-bs-theme="dark"] .sidebar::-webkit-scrollbar-thumb {
  background: #495057;
}

[data-bs-theme="dark"] .sidebar::-webkit-scrollbar-thumb:hover {
  background: #6c757d;
}
