:root {
  --ai4ch-green: #2d6a4f;
  --ai4ch-green-light: #40916c;
  --ai4ch-green-dark: #1b4332;
  --ai4ch-accent: #95d5b2;
  --ai4ch-bg: #f8faf9;
  --ai4ch-surface: #ffffff;
  --ai4ch-text: #1a1a2e;
  --ai4ch-muted: #6c757d;
  --ai4ch-border: #e9ecef;
  --ai4ch-shadow: 0 4px 24px rgba(45, 106, 79, 0.08);
  --ai4ch-radius: 12px;
  --sidebar-width: 260px;
  --sidebar-width-narrow: 76px;
}

[data-theme="dark"] {
  --ai4ch-bg: #0f1419;
  --ai4ch-surface: #1a2332;
  --ai4ch-text: #e8eef4;
  --ai4ch-muted: #94a3b8;
  --ai4ch-border: #2d3748;
  --ai4ch-shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
}

body.ai4ch-app {
  background: var(--ai4ch-bg);
  color: var(--ai4ch-text);
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
}

.ai4ch-layout {
  display: flex;
  min-height: 100vh;
}

.ai4ch-sidebar {
  width: var(--sidebar-width);
  background: var(--ai4ch-surface);
  border-right: 1px solid var(--ai4ch-border);
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  z-index: 1040;
  transition: width 0.25s ease, transform 0.25s ease;
  display: flex;
  flex-direction: column;
  overflow: visible;
}

.ai4ch-sidebar.is-narrow {
  width: var(--sidebar-width-narrow);
}

.ai4ch-sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 1rem 0.85rem;
  border-bottom: 1px solid var(--ai4ch-border);
  flex-shrink: 0;
}

.ai4ch-sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  text-decoration: none;
  color: inherit;
  min-width: 0;
  flex: 1;
}

.ai4ch-sidebar-brand-icon {
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 10px;
  background: rgba(45, 106, 79, 0.1);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.ai4ch-sidebar-brand-mark {
  display: none;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--ai4ch-green-dark), var(--ai4ch-green-light));
  color: #fff;
  font-size: 0.95rem;
  font-weight: 700;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.ai4ch-sidebar-brand-icon img {
  width: 1.5rem;
  height: 1.5rem;
  object-fit: contain;
}

.ai4ch-sidebar-brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
  overflow: hidden;
  transition: opacity 0.2s ease, width 0.2s ease;
}

.ai4ch-sidebar-brand-text strong {
  color: var(--ai4ch-green);
  font-size: 1rem;
  white-space: nowrap;
}

.ai4ch-sidebar-brand-text small {
  color: var(--ai4ch-muted);
  font-size: 0.68rem;
  white-space: nowrap;
}

.ai4ch-sidebar-collapse-btn {
  display: none;
  width: 2rem;
  height: 2rem;
  border: 1px solid var(--ai4ch-border);
  border-radius: 8px;
  background: transparent;
  color: var(--ai4ch-muted);
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.ai4ch-sidebar-collapse-btn:hover {
  background: rgba(45, 106, 79, 0.08);
  color: var(--ai4ch-green);
  border-color: rgba(45, 106, 79, 0.25);
}

.ai4ch-sidebar-nav {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0.65rem 0.5rem 1rem;
  scrollbar-width: thin;
}

.ai4ch-nav-group {
  margin-bottom: 0.15rem;
}

.ai4ch-nav-group-toggle {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  width: calc(100% - 0.5rem);
  margin: 0 0.25rem;
  padding: 0.5rem 0.65rem;
  border: none;
  outline: none;
  box-shadow: none;
  appearance: none;
  -webkit-appearance: none;
  border-radius: 8px;
  background: transparent;
  color: var(--ai4ch-text);
  font-size: 0.8125rem;
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.ai4ch-nav-group-toggle:focus,
.ai4ch-nav-group-toggle:focus-visible {
  outline: none;
  box-shadow: inset 0 0 0 2px rgba(45, 106, 79, 0.25);
}

.ai4ch-nav-group-toggle:hover {
  background: rgba(45, 106, 79, 0.06);
  color: var(--ai4ch-green);
}

.ai4ch-nav-group.is-open > .ai4ch-nav-group-toggle {
  color: var(--ai4ch-green);
}

.ai4ch-nav-chevron {
  margin-left: auto;
  font-size: 0.75rem;
  transition: transform 0.2s ease;
}

.ai4ch-nav-group.is-open .ai4ch-nav-chevron {
  transform: rotate(180deg);
}

.ai4ch-nav-group-body {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows 0.25s ease;
}

.ai4ch-nav-group.is-open .ai4ch-nav-group-body {
  grid-template-rows: 1fr;
}

.ai4ch-nav-group-inner {
  overflow: hidden;
  padding-top: 0.15rem;
}

.ai4ch-nav-link {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.55rem 0.75rem 0.55rem 2.35rem;
  color: var(--ai4ch-text);
  text-decoration: none;
  border-radius: 10px;
  margin: 0.1rem 0.25rem;
  font-size: 0.875rem;
  font-weight: 500;
  transition: background 0.15s ease, color 0.15s ease;
}

.ai4ch-nav-link-standalone {
  padding-left: 0.75rem;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.ai4ch-nav-link-muted {
  color: var(--ai4ch-muted);
}

.ai4ch-nav-icon {
  width: 1.35rem;
  height: 1.35rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 1rem;
}

.ai4ch-nav-label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: opacity 0.2s ease;
}

.ai4ch-nav-badge {
  margin-left: auto;
  min-width: 1.25rem;
  height: 1.25rem;
  padding: 0 0.35rem;
  border-radius: 999px;
  background: #dc3545;
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.ai4ch-nav-link:hover,
.ai4ch-nav-link.active {
  background: rgba(45, 106, 79, 0.12);
  color: var(--ai4ch-green);
}

.ai4ch-nav-link.active {
  font-weight: 600;
  box-shadow: inset 3px 0 0 var(--ai4ch-green);
}

.ai4ch-nav-group-toggle .ai4ch-nav-icon {
  color: var(--ai4ch-green);
  background: rgba(45, 106, 79, 0.08);
  border-radius: 8px;
  width: 1.75rem;
  height: 1.75rem;
  font-size: 0.9rem;
}

.ai4ch-nav-group + .ai4ch-nav-group {
  border-top: 1px solid var(--ai4ch-border);
  padding-top: 0.35rem;
  margin-top: 0.35rem;
}

.ai4ch-sidebar-foot {
  border-top: 1px solid var(--ai4ch-border);
  padding: 0.85rem;
  flex-shrink: 0;
}

.ai4ch-sidebar-user {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
}

.ai4ch-sidebar-user-avatar {
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--ai4ch-green-dark), var(--ai4ch-green-light));
  color: #fff;
  font-size: 0.85rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.ai4ch-sidebar-user-meta {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
  overflow: hidden;
  min-width: 0;
}

.ai4ch-sidebar-user-meta strong,
.ai4ch-sidebar-user-meta small {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ai4ch-sidebar-user-meta small {
  color: var(--ai4ch-muted);
  font-size: 0.72rem;
}

/* Narrow (icon) sidebar */
.ai4ch-sidebar.is-narrow .ai4ch-sidebar-brand-text,
.ai4ch-sidebar.is-narrow .ai4ch-nav-label,
.ai4ch-sidebar.is-narrow .ai4ch-nav-chevron,
.ai4ch-sidebar.is-narrow .ai4ch-nav-badge,
.ai4ch-sidebar.is-narrow .ai4ch-sidebar-user-meta {
  display: none;
}

.ai4ch-sidebar.is-narrow .ai4ch-sidebar-brand-mark {
  display: inline-flex;
}

.ai4ch-sidebar-expand-edge {
  display: none;
  position: fixed;
  top: 50%;
  left: calc(var(--sidebar-width-narrow) - 1px);
  transform: translateY(-50%);
  width: 1.5rem;
  height: 2.75rem;
  border: 1px solid var(--ai4ch-border);
  border-left: none;
  border-radius: 0 10px 10px 0;
  background: var(--ai4ch-surface);
  color: var(--ai4ch-muted);
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 1055;
  box-shadow: 4px 0 16px rgba(0, 0, 0, 0.08);
  padding: 0;
  pointer-events: auto;
}

.ai4ch-sidebar-expand-edge:hover {
  color: var(--ai4ch-green);
  border-color: rgba(45, 106, 79, 0.25);
}

.ai4ch-sidebar.is-narrow .ai4ch-sidebar-head {
  justify-content: center;
  padding-inline: 0.5rem;
  flex-direction: column;
  gap: 0.35rem;
}

.ai4ch-sidebar.is-narrow .ai4ch-sidebar-brand {
  justify-content: center;
}

.ai4ch-sidebar.is-narrow .ai4ch-nav-group-toggle {
  display: none;
}

.ai4ch-sidebar.is-narrow .ai4ch-nav-group {
  margin-bottom: 0;
  padding-top: 0.35rem;
  border-top: 1px solid var(--ai4ch-border);
}

.ai4ch-sidebar.is-narrow .ai4ch-nav-group:first-of-type {
  border-top: none;
  padding-top: 0;
}

.ai4ch-sidebar.is-narrow .ai4ch-nav-group-body,
.ai4ch-sidebar.is-narrow .ai4ch-nav-group.is-open .ai4ch-nav-group-body {
  grid-template-rows: 1fr;
}

.ai4ch-sidebar.is-narrow .ai4ch-nav-link {
  justify-content: center;
  padding: 0.55rem;
  margin-inline: 0.15rem;
}

.ai4ch-sidebar.is-narrow .ai4ch-nav-link.active {
  box-shadow: none;
  background: rgba(45, 106, 79, 0.16);
}

.ai4ch-sidebar.is-narrow .ai4ch-nav-link-standalone {
  margin-bottom: 0.35rem;
  border-bottom: none;
}

.ai4ch-sidebar.is-narrow .ai4ch-sidebar-foot {
  display: flex;
  justify-content: center;
}

.ai4ch-main {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-height: 100vh;
  min-width: 0;
  width: calc(100% - var(--sidebar-width));
  transition: margin 0.25s ease, width 0.25s ease;
  display: flex;
  flex-direction: column;
}

.ai4ch-sidebar.is-narrow + .ai4ch-main {
  margin-left: var(--sidebar-width-narrow);
  width: calc(100% - var(--sidebar-width-narrow));
}

.ai4ch-topbar {
  background: var(--ai4ch-surface);
  border-bottom: 1px solid var(--ai4ch-border);
  padding: 0.75rem 1.5rem;
  position: sticky;
  top: 0;
  z-index: 1030;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 1rem;
}

.ai4ch-content {
  padding: 1.5rem;
  flex: 1;
  width: 100%;
}

/* Legacy nav section label — kept for any older templates */
.ai4ch-nav-section {
  display: none;
}

.stat-card {
  background: var(--ai4ch-surface);
  border: 1px solid var(--ai4ch-border);
  border-radius: var(--ai4ch-radius);
  padding: 1.25rem;
  box-shadow: var(--ai4ch-shadow);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 32px rgba(45, 106, 79, 0.12);
}

.stat-card-link {
  display: block;
  color: inherit;
}

.stat-card-link:hover {
  color: inherit;
}

.stat-card-clickable {
  position: relative;
  cursor: pointer;
}

.stat-card-go {
  position: absolute;
  right: 1rem;
  bottom: 1rem;
  font-size: 0.85rem;
  color: var(--ai4ch-green);
  opacity: 0;
  transform: translateX(-4px);
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.stat-card-link:hover .stat-card-go {
  opacity: 1;
  transform: translateX(0);
}

.stat-card .stat-value {
  font-size: 2rem;
  font-weight: 700;
  color: var(--ai4ch-green);
}

.sensor-card {
  border-left: 4px solid var(--ai4ch-green-light);
}

.sensor-card.status-warning { border-left-color: #f59e0b; }
.sensor-card.status-critical { border-left-color: #ef4444; }
.sensor-card.status-offline { border-left-color: #94a3b8; }

.gauge-value {
  font-size: 1.75rem;
  font-weight: 700;
}

.skeleton {
  background: linear-gradient(90deg, var(--ai4ch-border) 25%, rgba(255,255,255,0.4) 50%, var(--ai4ch-border) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: 8px;
  min-height: 1rem;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.htmx-request .htmx-indicator { opacity: 1; }
.htmx-indicator { opacity: 0; transition: opacity 0.2s; }

.confidence-bar {
  height: 8px;
  border-radius: 4px;
  background: var(--ai4ch-border);
  overflow: hidden;
}

.confidence-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--ai4ch-green-light), var(--ai4ch-green));
  transition: width 0.6s ease;
}

.upload-dropzone {
  border: 2px dashed var(--ai4ch-green-light);
  border-radius: var(--ai4ch-radius);
  padding: 2.5rem;
  text-align: center;
  background: rgba(149, 213, 178, 0.08);
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.upload-dropzone.dragover {
  border-color: var(--ai4ch-green);
  background: rgba(45, 106, 79, 0.12);
}

.mobile-bottom-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--ai4ch-surface);
  border-top: 1px solid var(--ai4ch-border);
  z-index: 1050;
  padding: 0.35rem 0;
}

.mobile-bottom-nav a {
  flex: 1;
  text-align: center;
  font-size: 0.7rem;
  color: var(--ai4ch-muted);
  text-decoration: none;
  padding: 0.35rem;
}

.mobile-bottom-nav a.active { color: var(--ai4ch-green); }

@media (max-width: 991px) {
  .ai4ch-sidebar {
    transform: translateX(-100%);
    width: min(var(--sidebar-width), 88vw);
  }

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

  .ai4ch-sidebar.is-narrow {
    width: min(var(--sidebar-width), 88vw);
  }

  .ai4ch-main,
  .ai4ch-sidebar.is-narrow + .ai4ch-main {
    margin-left: 0;
    width: 100%;
  }

  .mobile-bottom-nav { display: flex; }
  .ai4ch-content { padding-bottom: 4.5rem; }

  .ai4ch-menu-btn {
    display: inline-flex !important;
  }
}

@media (min-width: 992px) {
  .ai4ch-sidebar {
    transform: none;
  }

  .ai4ch-sidebar.show {
    transform: none;
  }

  .ai4ch-sidebar-collapse-btn {
    display: inline-flex;
  }

  .ai4ch-sidebar.is-narrow .ai4ch-sidebar-collapse-btn {
    display: inline-flex;
  }

  .ai4ch-sidebar.is-narrow .ai4ch-sidebar-brand {
    cursor: pointer;
  }

  .ai4ch-desktop-expand-btn {
    display: none !important;
  }

  .ai4ch-desktop-expand-btn.is-visible {
    display: inline-flex !important;
  }

  .ai4ch-sidebar.is-narrow .ai4ch-sidebar-expand-edge {
    display: inline-flex;
  }

  .ai4ch-menu-btn {
    display: none !important;
  }
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 1035;
}

.sidebar-backdrop.show { display: block; }

.chart-container {
  position: relative;
  min-height: 260px;
}

#field-map {
  height: calc(100vh - 11rem);
  min-height: 520px;
  max-height: 840px;
  width: 100%;
  border-radius: var(--ai4ch-radius);
  overflow: hidden;
  position: relative;
  z-index: 0;
}

#field-map.leaflet-container {
  background: #e9ecef;
}

.field-map-popup-wrapper {
  max-width: min(320px, calc(100vw - 2rem));
}

.field-map-popup-wrapper .leaflet-popup-content-wrapper {
  max-height: min(70vh, 520px);
  overflow-y: auto;
}

.field-map-popup-wrapper .leaflet-popup-content {
  margin: 0;
  min-width: 240px;
}

.skip-link {
  position: absolute;
  top: -40px;
  left: 0;
  background: var(--ai4ch-green);
  color: white;
  padding: 8px 16px;
  z-index: 2000;
}

.skip-link:focus { top: 0; }
