/* =============================================================================
   Visor de Medi Ambient — Infraestructura verda municipal
   Estil: plataforma GIS municipal moderna (desktop → mòbil)
   ============================================================================= */

:root {
  --primary: #16875a;
  --primary-dark: #0f6b47;
  --primary-soft: #e8f5ef;
  --accent: #2d9f6f;
  --background: #f5f7f8;
  --surface: #ffffff;
  --surface-glass: rgba(255, 255, 255, 0.92);
  --text: #17212b;
  --text-secondary: #667085;
  --border: #e4e7ec;
  --danger: #c62828;
  --warn: #e65100;
  --ok: #16875a;
  --muted: #98a2b3;
  --shadow: 0 4px 24px rgba(23, 33, 43, 0.08);
  --shadow-sm: 0 1px 4px rgba(23, 33, 43, 0.06);
  --radius: 12px;
  --radius-sm: 8px;
  --header-h: 56px;
  --modes-h: 48px;
  --kpi-h: 64px;
  --left-w: 300px;
  --right-w: 340px;
  --font: "Manrope", "Source Sans 3", system-ui, -apple-system, sans-serif;
  --touch: 44px;
  --z-header: 40;
  --z-panel: 30;
  --z-drawer: 50;
  --z-overlay: 45;
  --z-toast: 60;
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font);
  color: var(--text);
  background: var(--background);
  overflow: hidden;
}

button, input, select, textarea {
  font-family: inherit;
  font-size: 13px;
}

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  min-height: 36px;
  transition: background .15s, border-color .15s, color .15s;
}
button:hover { background: var(--primary-soft); border-color: #c5e6d4; }
button:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
button.is-active, button[aria-pressed="true"] {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
button.is-active:hover { background: var(--primary-dark); }
button:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
  font-weight: 600;
}
.btn-primary:hover { background: var(--primary-dark); color: #fff; }
.btn-ghost { background: transparent; }

a { color: var(--primary-dark); }

/* ---- App shell ---- */
.app {
  display: grid;
  grid-template-rows: var(--header-h) var(--modes-h) 1fr auto;
  height: 100%;
  width: 100%;
  max-width: 100vw;
}

/* ---- Header ---- */
.app-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: var(--z-header);
  box-shadow: var(--shadow-sm);
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  min-width: 0;
}
.brand__icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: linear-gradient(145deg, var(--primary), var(--primary-dark));
  display: grid;
  place-items: center;
  color: #fff;
  flex-shrink: 0;
}
.brand__icon svg { width: 20px; height: 20px; }
.brand__text { min-width: 0; }
.brand__title {
  font-size: 15px;
  font-weight: 700;
  line-height: 1.15;
  margin: 0;
  white-space: nowrap;
}
.brand__sub {
  font-size: 11px;
  color: var(--text-secondary);
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand__muni {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary-dark);
  font-size: 10px;
  font-weight: 600;
}

.header-search {
  flex: 1;
  max-width: 480px;
  margin: 0 auto;
  position: relative;
}
.header-search input {
  width: 100%;
  padding: 10px 14px 10px 38px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--background);
  color: var(--text);
}
.header-search input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(22, 135, 90, 0.15);
}
.header-search__icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  pointer-events: none;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.header-actions .icon-btn {
  width: var(--touch);
  height: var(--touch);
  min-height: var(--touch);
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: var(--text-secondary);
}
.header-actions .icon-btn span.badge {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warn);
}
.icon-btn-wrap { position: relative; }

/* ---- Modes ---- */
.work-modes {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  z-index: var(--z-header);
}
.work-modes::-webkit-scrollbar { display: none; }
.mode-seg {
  display: inline-flex;
  background: var(--background);
  border-radius: 999px;
  padding: 4px;
  gap: 2px;
  margin: 6px 0;
}
.mode-seg button {
  border: none;
  background: transparent;
  border-radius: 999px;
  padding: 8px 14px;
  font-weight: 600;
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  min-height: 36px;
}
.mode-seg button .mode-ico { margin-right: 4px; }
.mode-seg button.is-active {
  background: var(--surface);
  color: var(--primary-dark);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
}

/* ---- Main workspace ---- */
.workspace {
  display: grid;
  grid-template-columns: var(--left-w) minmax(0, 1fr) var(--right-w);
  min-height: 0;
  position: relative;
  background: var(--background);
}

.panel {
  background: var(--surface-glass);
  backdrop-filter: blur(12px);
  display: flex;
  flex-direction: column;
  min-height: 0;
  border: none;
  z-index: var(--z-panel);
}
.panel--left { border-right: 1px solid var(--border); }
.panel--right { border-left: 1px solid var(--border); }
.panel.is-collapsed { display: none; }

.panel__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.panel__head h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
}
.panel__body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px;
  -webkit-overflow-scrolling: touch;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 10px;
  box-shadow: var(--shadow-sm);
}
.card__title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-secondary);
  margin: 0 0 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.card__title button.collapse-btn {
  border: none;
  background: transparent;
  padding: 4px;
  min-height: auto;
  color: var(--muted);
}

.field { margin-bottom: 10px; }
.field label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 4px;
}
.field select,
.field input[type="text"] {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
  color: var(--text);
}
.field select:focus,
.field input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(22, 135, 90, 0.12);
}
.field-hint {
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
}

/* Layer list */
.layer-group { margin-bottom: 8px; }
.layer-group__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  margin: 4px 0 6px;
  background: none;
  border: none;
  width: 100%;
  padding: 4px 0;
  text-align: left;
}
.layer-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 6px;
  border-radius: var(--radius-sm);
  position: relative;
}
.layer-item:hover { background: var(--primary-soft); }
.layer-item.is-disabled {
  opacity: .55;
}
.layer-item.is-disabled .layer-item__name::after {
  content: " · properament";
  font-size: 10px;
  color: var(--muted);
  font-weight: 500;
}
.layer-item input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--primary);
}
.layer-item__ico {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  font-size: 12px;
  flex-shrink: 0;
}
.layer-item__name {
  flex: 1;
  font-size: 13px;
  font-weight: 500;
  min-width: 0;
}
.layer-item__menu-btn {
  border: none;
  background: transparent;
  padding: 4px 6px;
  min-height: auto;
  color: var(--muted);
  border-radius: 6px;
}
.layer-menu {
  position: absolute;
  right: 4px;
  top: 100%;
  z-index: 5;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  min-width: 160px;
  padding: 6px;
  display: none;
}
.layer-menu.is-open { display: block; }
.layer-menu button {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  padding: 8px 10px;
  border-radius: 6px;
}
.layer-menu .opacity-row {
  padding: 6px 10px;
  font-size: 12px;
  color: var(--text-secondary);
}
.layer-menu input[type="range"] { width: 100%; }

/* Pill segmented (transport) */
.pill-seg {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 6px;
}
.pill-seg button {
  border-radius: 999px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text-secondary);
  font-weight: 600;
}
.pill-seg button.is-active {
  background: var(--primary-soft);
  color: var(--primary-dark);
  border-color: #b7dfc9;
}

.btn-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 10px;
}
.btn-row button { flex: 1 1 auto; }

.small { font-size: 12px; color: var(--text-secondary); }
.status-line { min-height: 1.2em; margin-top: 6px; font-size: 12px; color: var(--text-secondary); }

/* ---- Map stage ---- */
.map-stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  background: #dce6e0;
}
#map {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.map-float {
  position: absolute;
  z-index: 5;
  pointer-events: none;
}
.map-float > * { pointer-events: auto; }

.map-view-toggle {
  top: 12px;
  left: 12px;
  display: flex;
  gap: 4px;
  background: var(--surface);
  border-radius: 999px;
  padding: 4px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}
.map-view-toggle button {
  border: none;
  border-radius: 999px;
  min-width: 48px;
  font-weight: 700;
  font-size: 12px;
  padding: 6px 10px;
  background: transparent;
  color: var(--text-secondary);
}
.map-view-toggle button.is-active {
  background: var(--primary);
  color: #fff;
}

.map-legend {
  bottom: 52px;
  left: 12px;
  width: min(220px, calc(100% - 24px));
  background: var(--surface-glass);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.map-legend__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
  background: transparent;
  width: 100%;
  border-left: none;
  border-right: none;
  border-top: none;
  border-radius: 0;
  min-height: auto;
}
.map-legend__body { padding: 8px 10px 10px; }
.map-legend.is-collapsed .map-legend__body { display: none; }
.legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  margin: 4px 0;
  color: var(--text);
}
.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex-shrink: 0;
  border: 1px solid rgba(0,0,0,.12);
}
.legend-swatch--point { border-radius: 50%; background: var(--primary); }
.legend-swatch--park { background: #7bc67e; }
.legend-swatch--green { background: #a8d5a2; }
.legend-swatch--garden { background: #c5e1a5; }

.map-footer-info {
  bottom: 8px;
  left: 12px;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-wrap: wrap;
  max-width: calc(100% - 24px);
}
.coord-chip, .scale-chip {
  background: var(--surface-glass);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  font: 11px/1.3 ui-monospace, monospace;
  color: var(--text);
  box-shadow: var(--shadow-sm);
}
.coord-chip { pointer-events: none; }
.scale-chip .mapboxgl-ctrl-scale {
  position: relative !important;
  background: transparent !important;
  border-color: #333 !important;
  box-shadow: none !important;
  margin: 0 !important;
}

.map-panel-toggles {
  top: 12px;
  right: 12px;
  display: none; /* desktop: panells amb botó ✕ propi; mòbil: barra inferior */
  flex-direction: column;
  gap: 6px;
}
.map-panel-toggles button {
  min-width: 108px;
  box-shadow: var(--shadow);
  background: var(--surface);
  font-weight: 600;
}

/* Mapbox controls */
.mapboxgl-ctrl-top-right { top: 56px; right: 12px; }
.mapboxgl-ctrl-bottom-right { bottom: 8px; right: 8px; }
.mapboxgl-ctrl-attrib { font-size: 10px; max-width: min(360px, 50vw); }
.mapboxgl-popup-content {
  padding: 12px 14px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-family: var(--font);
  font-size: 13px;
  max-width: min(360px, 90vw);
  max-height: 70vh;
  overflow-y: auto;
}
.mapboxgl-popup-close-button { font-size: 18px; padding: 4px 8px; }
.popup-title { font-weight: 700; margin-bottom: 8px; color: var(--primary-dark); }
.popup-kv { margin: 4px 0; font-size: 12px; }
.popup-kv b { display: inline-block; min-width: 120px; color: var(--text-secondary); font-weight: 600; }

/* ---- Right panel: tabs / agenda / fitxa ---- */
.tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--background);
  border-radius: 999px;
  margin-bottom: 10px;
}
.tabs button {
  flex: 1;
  border: none;
  border-radius: 999px;
  background: transparent;
  font-weight: 600;
  color: var(--text-secondary);
  padding: 8px;
}
.tabs button.is-active {
  background: #fff;
  color: var(--primary-dark);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
}

.agenda-section { margin-bottom: 8px; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.agenda-section__head {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  text-align: left;
  border: none;
  border-radius: 0;
  background: #fafbfc;
  padding: 10px 12px;
  font-weight: 600;
  font-size: 13px;
}
.agenda-dot {
  width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
}
.agenda-dot--late { background: var(--danger); }
.agenda-dot--soon { background: var(--warn); }
.agenda-dot--mid { background: var(--muted); }
.agenda-dot--far { background: #cfd4dc; }
.agenda-section__body { padding: 0 8px 8px; display: none; }
.agenda-section.is-open .agenda-section__body { display: block; }
.agenda-empty {
  padding: 16px 8px;
  text-align: center;
  color: var(--text-secondary);
  font-size: 13px;
  line-height: 1.4;
}
.agenda-item {
  padding: 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  margin-top: 6px;
  cursor: pointer;
  background: #fff;
}
.agenda-item:hover { border-color: #b7dfc9; background: var(--primary-soft); }
.agenda-item__sp { font-weight: 700; font-size: 13px; }
.agenda-item__meta { font-size: 11px; color: var(--text-secondary); margin-top: 2px; }
.agenda-badge {
  display: inline-block;
  margin-top: 6px;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
}
.agenda-badge--late { background: #ffebee; color: var(--danger); }
.agenda-badge--soon { background: #fff3e0; color: var(--warn); }
.agenda-badge--done { background: var(--primary-soft); color: var(--ok); }

.ficha { }
.ficha__back {
  border: none;
  background: transparent;
  color: var(--primary-dark);
  font-weight: 600;
  padding: 0 0 8px;
  min-height: auto;
}
.ficha dl { margin: 0; }
.ficha dt {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-top: 8px;
}
.ficha dd { margin: 2px 0 0; font-size: 13px; }

.history-timeline { margin-top: 12px; padding-left: 12px; border-left: 2px solid var(--border); }
.history-timeline li {
  list-style: none;
  margin: 0 0 10px;
  font-size: 12px;
  position: relative;
}
.history-timeline li::before {
  content: "";
  position: absolute;
  left: -17px;
  top: 4px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--primary);
}

.gv-list { margin-top: 8px; }
.gv-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-top: 6px;
  background: #fff;
}
.gv-item__name { font-weight: 700; font-size: 13px; }
.gv-item__meta { font-size: 12px; color: var(--text-secondary); }

/* ---- KPI bar ---- */
.kpi-bar {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  min-height: var(--kpi-h);
  align-items: stretch;
  scrollbar-width: thin;
}
.kpi-card {
  flex: 0 0 auto;
  min-width: 120px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--primary-soft);
  cursor: default;
  text-align: left;
}
.kpi-card.is-clickable { cursor: pointer; }
.kpi-card.is-clickable:hover { border-color: var(--primary); }
.kpi-card.is-hidden { display: none; }
.kpi-card__val {
  font-size: 18px;
  font-weight: 800;
  color: var(--primary-dark);
  line-height: 1.1;
}
.kpi-card__lbl {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* ---- Overlay / drawers (tablet+mobile) ---- */
.drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(23, 33, 43, 0.35);
  z-index: var(--z-overlay);
}
.drawer-backdrop.is-visible { display: block; }

.mobile-bar {
  display: none;
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 8;
  gap: 6px;
  padding: 6px;
  background: var(--surface);
  border-radius: 999px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}
.mobile-bar button {
  border-radius: 999px;
  font-weight: 600;
  font-size: 12px;
  padding: 8px 12px;
  min-height: 40px;
}

.toast {
  position: fixed;
  bottom: calc(var(--kpi-h) + 16px);
  left: 50%;
  transform: translateX(-50%);
  z-index: var(--z-toast);
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(23, 33, 43, 0.92);
  color: #fff;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
  max-width: min(90vw, 400px);
  text-align: center;
}
.toast.is-visible { opacity: 1; }

.help-popover {
  display: none;
  position: absolute;
  top: calc(var(--header-h) + 8px);
  right: 12px;
  width: min(320px, calc(100vw - 24px));
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  z-index: var(--z-drawer);
  font-size: 13px;
  line-height: 1.45;
  color: var(--text-secondary);
}
.help-popover.is-open { display: block; }
.help-popover h3 { margin: 0 0 8px; font-size: 14px; color: var(--text); }

.mode-panel { display: none; }
.mode-panel.is-active { display: block; }

/* ---- Responsive ---- */
@media (max-width: 1439px) {
  :root { --left-w: 280px; --right-w: 300px; }
}

@media (max-width: 1023px) {
  .workspace {
    grid-template-columns: 1fr;
  }
  .panel--left,
  .panel--right {
    position: fixed;
    top: calc(var(--header-h) + var(--modes-h));
    bottom: var(--kpi-h);
    width: min(360px, 92vw);
    z-index: var(--z-drawer);
    box-shadow: var(--shadow);
    display: none;
  }
  .panel--left { left: 0; border-right: 1px solid var(--border); }
  .panel--right { right: 0; border-left: 1px solid var(--border); }
  .panel--left.is-open,
  .panel--right.is-open { display: flex; }
  .panel--left.is-collapsed,
  .panel--right.is-collapsed { display: none; }
  .panel--left.is-open.is-collapsed { display: none; }
  .mobile-bar { display: flex; }
  .map-panel-toggles { display: none; }
  .mapboxgl-ctrl-top-right { top: 12px; right: 12px; }
  .brand__sub { max-width: 140px; }
}

@media (max-width: 767px) {
  :root {
    --header-h: 52px;
    --modes-h: 44px;
    --kpi-h: 56px;
  }
  .app-header { padding: 0 10px; gap: 8px; }
  .brand__sub { display: none; }
  .brand__title { font-size: 13px; }
  .header-search { max-width: none; }
  .header-search input { padding: 8px 12px 8px 34px; font-size: 12px; }
  .header-actions .icon-btn:nth-child(n+3) { display: none; }
  .mode-seg button { padding: 6px 10px; font-size: 12px; }
  .mode-seg button .mode-label { /* keep icon+short */ }
  .kpi-card { min-width: 100px; padding: 6px 10px; }
  .kpi-card__val { font-size: 15px; }
  .panel--left,
  .panel--right {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    max-height: min(70vh, 520px);
    border-radius: 16px 16px 0 0;
    border: none;
    border-top: 1px solid var(--border);
  }
  .map-legend { bottom: 72px; }
  .map-footer-info { bottom: 64px; }
  .mobile-bar { bottom: calc(var(--kpi-h) + 8px); }
  .toast { bottom: calc(var(--kpi-h) + 64px); }
}

@media (min-width: 1440px) {
  :root { --left-w: 300px; --right-w: 340px; }
}
