/* ==========================================
   1. VARIABLES GLOBLAS I RESET
   ========================================== */
:root {
  --primary-color: #1f8f5a; /* Verd GesGat */
  --primary-hover: #166e44;
  --secondary-color: #4b5563;
  --bg-color: #f3f4f6;
  --surface-color: #ffffff;
  --text-main: #1f2937;
  --text-muted: #6b7280;
  --border-color: #e5e7eb;
  --danger-color: #ef4444;
  --sidebar-width: 250px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Inter', sans-serif;
  background-color: var(--bg-color);
  color: var(--text-main);
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ==========================================
   2. CLASSES D'UTILITAT (Molt importants)
   ========================================== */
.d-none { display: none !important; }
.w-100 { width: 100%; }
.text-center { text-align: center; }
.text-muted { color: var(--text-muted); }
.pe-none { pointer-events: none; }

/* ==========================================
   3. PANTALLA DE LOGIN
   ========================================== */
.pantalla-completa {
  position: fixed;
  top: 0; left: 0; width: 100vw; height: 100vh;
  background-color: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(5px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.card-login {
  background: var(--surface-color);
  padding: 2.5rem;
  border-radius: 12px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.2);
  width: 100%;
  max-width: 400px;
  text-align: center;
}

.login-logo {
  max-width: 150px;
  margin-bottom: 2rem;
}

.card-login input {
  width: 100%;
  padding: 0.75rem;
  margin-bottom: 1rem;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-size: 1rem;
}

.input-icon-wrapper { position: relative; }
#btn-veure-password {
  position: absolute;
  right: 10px; top: 50%;
  transform: translateY(-50%);
  background: none; border: none; cursor: pointer;
  margin-top: -8px;
}

/* ==========================================
   4. ESTRUCTURA PRINCIPAL (SIDEBAR I MAIN)
   ========================================== */
#sidebar {
  width: var(--sidebar-width);
  background: var(--surface-color);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  padding: 1.5rem 1rem;
  z-index: 100;
}

.logo-sidebar { max-width: 120px; margin: 0 auto 2rem auto; display: block; }

.user-panel {
  background: var(--bg-color);
  padding: 1rem;
  border-radius: 8px;
  margin-bottom: 1.5rem;
  font-size: 0.85rem;
}

/* EL CONTENIDOR AMB SCROLL */
.nav-links { 
  list-style: none; 
  flex-grow: 1; 
  overflow-y: auto; /* Permet l'scroll */
  margin-bottom: 1rem;
  padding-right: 5px; 
}

/* DISSENY DE LA BARRA D'SCROLL */
.nav-links::-webkit-scrollbar {
  width: 6px;
}
.nav-links::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 4px;
}
.nav-links li a {
  display: block;
  padding: 0.75rem 1rem;
  color: var(--text-main);
  text-decoration: none;
  border-radius: 6px;
  margin-bottom: 0.25rem;
  transition: background 0.2s;
}
.nav-links li a:hover { background: var(--bg-color); color: var(--primary-color); }

/*Estil modern pel botó veure dels registre dels llistats de taules*/
.btn-veure:hover svg {
    stroke: var(--primary-color); /* S'il·lumina en verd GesGat */
}

/* Estil modern per al botó de sortir */
.btn-logout {
    background: none;
    border: 1px solid rgba(255, 255, 255, 0.2); /* Borde suau i minimalista */
    border-radius: 8px; /* Arrodonit */
    color: #ffffff !important; /* Mateix color que la resta */
    font-size: 13px; /* Mateix tamany que els links */
    font-family: inherit;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 12px;
    margin: 20px 10px;
    width: calc(100% - 20px);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px; /* Espai entre icona i text */
    transition: all 0.3s ease;
}

.btn-logout:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.5);
    transform: translateY(-1px);
}

/* Color de la icona SVG */
.btn-logout svg {
    stroke: currentColor;
    width: 18px;
    height: 18px;
}

#main-content {
  flex-grow: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow-y: auto;
}

.page-content {
  padding: 2rem;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
}

/* ==========================================
   5. COMPONENT MAPA
   ========================================== */
#sec-mapa { padding: 0; }
#map-container { width: 100%; height: 100%; min-height: 100vh; z-index: 1; }

.legend.card-mapa {
  background: white;
  padding: 10px;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  line-height: 1.8;
}

/* ==========================================
   6. MÒDULS I TAULES (Colònies, Gats...)
   ========================================== */
.header-seccio {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 1.5rem;
}

.card {
  background: var(--surface-color);
  border-radius: 8px;
  padding: 1.5rem;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  margin-bottom: 1.5rem;
}

.toolbar-filtres {
  display: flex; gap: 1rem; align-items: center; flex-wrap: wrap;
}

.toolbar-filtres input, .toolbar-filtres select {
  padding: 0.5rem; border: 1px solid var(--border-color); border-radius: 6px;
}

/* === SOLUCIÓ PER A TAULES AMPLES === */
.contenidor-taula {
    width: 100%;
    overflow-x: auto; /* Crea scroll horitzontal si sobren columnes */
    -webkit-overflow-scrolling: touch; /* Scroll suau en mòbils i tauletes */
}

.taula-dades { 
    width: 100%; 
    border-collapse: collapse; 
    text-align: left; 
    min-width: 600px; /* Força un mínim perquè no s'aixafi massa abans de fer scroll */
}

.taula-dades th { background: var(--bg-color); padding: 1rem; cursor: pointer; white-space: nowrap; }
.taula-dades td { padding: 1rem; border-bottom: 1px solid var(--border-color); }
.taula-dades tr:hover { background: #f9fafb; }
.taula-dades th.sortable:hover { background: #e5e7eb; }

/* Bloquegem la columna d'accions perquè els botons no s'apilin */
.accions-taula {
    white-space: nowrap;
    min-width: 90px;
}

/* ==========================================
   7. BOTONS
   ========================================== */
.btn-primary {
    background: var(--primary-color); color: white;
    border: none; padding: 0.75rem 1.5rem; border-radius: 6px;
    cursor: pointer; font-weight: 600; transition: background 0.2s;
    /* AFEGIT: Alineació perfecta per a les icones */
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

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

/* AFEGIT: Donem format a qualsevol icona que posem dins d'un botó principal */
.btn-primary svg {
    width: 18px;
    height: 18px;
    stroke: currentColor; /* Agafa el color blanc de la lletra automàticament */
    stroke-width: 2;
    fill: none;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.btn-icon {
  background: none; border: none; cursor: pointer; font-size: 1.2rem; margin: 0 0.2rem;
  transition: transform 0.1s;
}
.btn-icon:hover { transform: scale(1.1); }

/* ==========================================
   8. RESPONSIVITAT I MENÚ MÒBIL
   ========================================== */
/* Amaguem el botó hamburguesa per defecte (Ordinador) */
.hamburger { display: none; }

/* Preparem l'overlay fosc amb transició suau (invisible per defecte) */
#menu-overlay { 
    visibility: hidden;
    opacity: 0;
    transition: opacity 0.3s ease;
    position: fixed;
    top: 0; left: 0; width: 100vw; height: 100vh;
    background: rgba(0,0,0,0.5);
    z-index: 9998; /* Just sota el menú lateral */
}

/* Quan la pantalla és inferior a 768px (Mòbils i Tablets) */
@media (max-width: 768px) {
  /* Dibuixem el botó hamburguesa flotant a dalt a l'esquerra */
  .hamburger {
    display: block;
    position: fixed;
    top: 15px;
    left: 15px;
    z-index: 9997; /* Per sota de l'overlay fosc */
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 10px 15px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: bold;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  }

  /* Amaguem la barra lateral fora de la pantalla per l'esquerra (-100%) */
  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    height: 100vh;
    transform: translateX(-100%); 
    transition: transform 0.3s ease-in-out; /* Animació suau */
    z-index: 9999; /* Màxima prioritat: per sobre de tot */
  }
  
  /* CLAU: Ara sí s'anomena 'actiu' com al JavaScript */
  #sidebar.actiu {
    transform: translateX(0);
  }

  /* El fons fosc s'activa amb una transició elegant */
  #menu-overlay.actiu {
    visibility: visible;
    opacity: 1;
    pointer-events: auto; /* Permet fer clic per tancar */
  }
  
  /* Fem una mica d'espai a dalt perquè l'hamburguesa no tapi res important */
  .header-seccio {
    margin-top: 40px; 
  }
}

/* ==========================================
   9. MODALS I BOTTOM SHEETS (Formularis)
   ========================================== */
.modal-overlay {
    position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
    background: rgba(0,0,0,0.1); /* 1. Fons quasi transparent perquè es vegi el mapa */
    display: flex; justify-content: center; align-items: center;
    z-index: 2000; opacity: 0; 
    pointer-events: none; /* 2. CLAU: Deixa que els clics traspassin al mapa de fons */
    transition: opacity 0.3s ease;
}

.modal-overlay.obert { opacity: 1; } /* Ja no posem pointer-events:auto aquí */

.modal-content {
    background: var(--surface-color);
    width: 90%; max-width: 600px;
    max-height: 90vh; overflow-y: auto;
    border-radius: 12px; padding: 2rem;
    box-shadow: 0 -5px 25px rgba(0,0,0,0.2);
    position: relative;
    pointer-events: auto; /* 3. CLAU: El formulari SÍ que captura els clics */
    transform: translateY(50px); transition: transform 0.3s ease;
}

.modal-overlay.obert .modal-content { transform: translateY(0); }

/* Grid per organitzar els formularis a 2 columnes en PC */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-bottom: 1rem; }
.form-grup { display: flex; flex-direction: column; }
.form-grup.full-width { grid-column: 1 / -1; }
.form-grup label { font-weight: 600; font-size: 0.9rem; margin-bottom: 0.3rem; color: var(--text-muted); }
.form-grup input, .form-grup select, .form-grup textarea {
    padding: 0.75rem; border: 1px solid var(--border-color); border-radius: 6px; font-family: inherit;
}

.header-modal { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid var(--border-color); padding-bottom: 1rem; margin-bottom: 1.5rem; }
.btn-tancar-modal { background: none; border: none; font-size: 1.5rem; cursor: pointer; color: var(--text-muted); }

/* Secció d'Auditoria (Lletra petita a baix) */
.info-auditoria {
    margin-top: 2rem; padding: 1rem; background: var(--bg-color);
    border-radius: 8px; font-size: 0.8rem; color: var(--text-muted);
}

/* COMPORTAMENT MÒBIL (Bottom Sheet) */
@media (max-width: 768px) {
    .modal-overlay { align-items: flex-end; /* Ho empeny cap abaix */ }
    .modal-content {
        width: 100%; max-width: 100%; border-radius: 20px 20px 0 0;
        padding: 1.5rem 1rem; max-height: 75vh; /* Deixa un 25% de pantalla a dalt pel mapa */
    }
    .form-grid { grid-template-columns: 1fr; /* 1 sola columna en mòbil */ }
}

/* ==========================================
   10. ICONES DEL MAPA (Leaflet)
   ========================================== */
/* Traiem el fons blanc per defecte de les icones div */
.div-icon-transparent { background: transparent; border: none; }

/* 1. Colònies: Cercles de colors segons estat CER */
/* Aquests estils es mantenen per si es requereixen en llistats HTML purs, al mapa es renderitzen via icones.js */
.marker-cer-verd { background-color: #28a745; border: 2px solid white; border-radius: 50%; box-shadow: 0 3px 6px rgba(0,0,0,0.5); width: 100%; height: 100%; display: block; }
.marker-cer-groc { background-color: #ffc107; border: 2px solid white; border-radius: 50%; box-shadow: 0 3px 6px rgba(0,0,0,0.5); width: 100%; height: 100%; display: block; }
.marker-cer-vermell { background-color: #dc3545; border: 2px solid white; border-radius: 50%; box-shadow: 0 3px 6px rgba(0,0,0,0.5); width: 100%; height: 100%; display: block; }
.marker-cer-gris { background-color: #6c757d; border: 2px solid white; border-radius: 50%; box-shadow: 0 3px 6px rgba(0,0,0,0.5); width: 100%; height: 100%; display: block; }

/* 2. Emojis grans per a Punts i Incidències */
.marker-gran { 
    font-size: 28px; /* Mida molt més gran */
    line-height: 28px; 
    text-shadow: 0 3px 6px rgba(0,0,0,0.5); /* Ombra per fer-los ressaltar */
    display: flex; justify-content: center; align-items: center; 
}

/* ==========================================
   11. FINESTRES FLOTANTS I ARROSSEGABLES EN ORDINADOR
   ========================================== */
@media (min-width: 769px) {
    #modal-colonia.modal-overlay {
        justify-content: flex-end; 
        align-items: flex-start;   
        padding: 20px;             
    }
    
    #modal-colonia .modal-content {
        margin: 0;
        transform: none; 
        max-height: calc(100vh - 40px); 
        box-shadow: -5px 5px 25px rgba(0,0,0,0.3);
    }
}

/* ==========================================
   12. AJUSTOS DE LA BARRA DE FILTRES I TAULA
   ========================================== */
/* Fem que la caixa de cerca ocupi tot l'espai lliure */
.toolbar-filtres {
    display: flex;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap; /* En mòbils baixarà a la línia següent si no hi cap */
}

.toolbar-filtres .grup-input {
    flex-grow: 1; /* La màgia: estira aquesta caixa */
}

.toolbar-filtres .grup-input input {
    width: 100%; /* L'input s'estira al màxim del seu contenidor */
}

/* Efecte visual en passar el ratolí per les capçaleres de la taula */
th.sortable:hover {
    background-color: rgba(0, 0, 0, 0.05);
}

/* ==========================================
   13. ESTILS DEL MENÚ LATERAL (DARK MODE)
   ========================================== */
#sidebar { 
    background: #2f3e4d; /* El teu color fosc original */
    box-shadow: 2px 0 10px rgba(0,0,0,0.3);
}

/* El panell on surt l'ajuntament i l'usuari */
.user-panel {
    background: rgba(0, 0, 0, 0.25);
    padding: 12px;
    border-radius: 6px;
    margin-bottom: 20px;
    text-align: center;
    border: 1px solid rgba(255,255,255,0.05);
}

.user-role-label {
    font-size: 0.65rem;
    text-transform: uppercase;
    color: #8aa4af;
    letter-spacing: 0.5px;
    margin-bottom: 4px;
}

.user-entity-name {
    font-size: 0.9rem;
    font-weight: bold;
    color: #ffffff;
    margin-bottom: 12px;
    text-shadow: 0 1px 2px rgba(0,0,0,0.5);
}

.user-active-info {
    font-size: 0.75rem;
    color: #b8c7ce;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 6px;
}

.user-active-info span:first-child { color: #fff; }

.badge-rol {
    background: #1f8f5a; /* El teu verd corporatiu */
    color: white;
    padding: 2px 6px;
    border-radius: 10px;
    font-size: 0.65rem;
    font-weight: bold;
}

.badge-rol.clickable {
    cursor: pointer;
    transition: transform 0.2s, background 0.2s, box-shadow 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.badge-rol.clickable:hover {
    transform: scale(1.05);
    background: var(--primary-hover);
    box-shadow: 0 2px 5px rgba(0,0,0,0.3);
}

/* Forcem que els enllaços del menú lateral siguin sempre blancs */
#sidebar a, #sidebar .nav-links a {
    display: flex; /* Alinea la icona i el text */
    align-items: center; /* Ho centra verticalment */
    gap: 12px; /* L'espai perfecte entre icona i text */
    color: #ffffff !important;
    text-decoration: none;
    margin: 8px 0;
    cursor: pointer;
    font-size: 13px;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    padding: 10px 12px;
    border-radius: 6px;
    transition: 0.2s;
}

#sidebar a:hover, #sidebar .nav-links a:hover {
    color: #ffffff !important;
    background: #3d566e !important;
    padding-left: 16px; /* Efecte de desplaçament suau */
}

/* Bombolla per notificacions noves al menú */
.badge-notificacio {
    background-color: var(--danger-color);
    color: white;
    border-radius: 12px;
    padding: 2px 6px;
    font-size: 0.65rem;
    font-weight: bold;
    margin-left: auto; /* L'empeny cap a la dreta del tot */
    min-width: 20px;
    text-align: center;
}

/* Nova regla per a les icones del menú */
.icon-menu {
    width: 18px;
    height: 18px;
    stroke: currentColor; /* Agafa el color blanc del text automàticament */
    stroke-width: 2;
    fill: none;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex-shrink: 0;
    opacity: 0.8; /* Una mica translúcides per donar tot el pes al text */
}

#sidebar a:hover .icon-menu {
    opacity: 1; /* S'il·luminen al passar el ratolí */
}

/* Reduïm la tipografia de l'entorn de treball per fer-ho més elegant */
.user-entity-name {
    font-size: 0.8rem !important; 
}

.user-active-info {
    font-size: 0.7rem !important; 
}

/* Nou estil corporatiu pel botó de la llicència */
.btn-llicencia {
    display: flex;
    width: 100%;
    justify-content: center;
    align-items: center;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: white;
    padding: 8px;
    border-radius: 6px;
    font-size: 0.8rem;
    cursor: pointer;
    margin-bottom: 15px;
    transition: 0.2s;
}

.btn-llicencia:hover {
    background: rgba(255, 255, 255, 0.25);
    transform: translateY(-1px);
}

/* ==========================================
   14. GARANTIA DE VISIBILITAT DELS MODALS
   ========================================== */
#modal-llicencia, #pantalla-bloqueig-llicencia {
    z-index: 999999 !important; /* Per sobre del menú i del mapa */
}

#modal-llicencia .modal-content {
    background-color: white !important;
    opacity: 1 !important;
    visibility: visible !important;
}

/* ==========================================
   15 i 16. ICONES D'ACCIÓ A LES TAULES
   ========================================== */
.btn-icon-minimal {
    background: none;
    border: none;
    padding: 6px;
    cursor: pointer;
    transition: all 0.2s ease;
    border-radius: 6px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.btn-icon-minimal:hover {
    background-color: rgba(0, 0, 0, 0.05);
    transform: translateY(-1px);
}

.btn-icon-minimal svg {
    stroke: #546e7a; /* Un color fosc corporatiu i suau */
    width: 18px;
    height: 18px;
    stroke-width: 2;
    fill: none;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* Colors en passar el ratolí */
.btn-editar:hover svg {
    stroke: #3498db; /* Blau per editar */
}

.btn-borrar:hover svg {
    stroke: #e74c3c; /* Vermell per eliminar */
}

/* ==========================================
   17. LOGOS SUPERIORS (GESGAT + AJUNTAMENT)
   ========================================== */
.sidebar-logos {
    display: flex;
    align-items: center; /* Alinea els centres de les imatges verticalment */
    justify-content: center; /* Els centra al mig del menú */
    gap: 40px; /* L'espai de separació entre els dos logos */
    margin-bottom: 20px;
    padding: 0 10px;
}

/* Fem que les dues imatges tinguin una alçada màxima perquè no desquadrin */
.sidebar-logos .logo-meitat {
    max-height: 85px; /* Pots pujar o baixar aquest valor per fer-los més grans o petits */
    width: auto;
    max-width: 50%; /* Evita que un logo molt ample es mengi l'altre */
    object-fit: contain;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2)); /* Una ombra subtil perquè ressaltin */
}

/* ==========================================
   19. AJUSTOS DE LLETRA I TAULES EN MÒBIL
   ========================================== */
/* Regles per a la Llegenda Col·lapsable (Funcionen en PC i mòbil) */
.llegenda-header {
    display: flex; 
    justify-content: space-between; 
    align-items: center;
    cursor: pointer; 
    margin-bottom: 0;
    gap: 15px; /* Espai entre el títol i la fletxa */
}

.llegenda-body {
    display: none; /* Amagat per defecte */
    margin-top: 15px;
}

.llegenda-body.obert {
    display: block; /* Es mostra quan cliquem */
}

.icona-fletxa {
    transition: transform 0.3s ease; 
    font-size: 10px;
    color: var(--text-muted);
}

.icona-fletxa.obert {
    transform: rotate(180deg); /* La fletxa gira cap amunt */
}

/* Regles d'encongiment exclusivament per a Mòbil */
@media (max-width: 768px) {
    /* Taules i marges */
    .taula-dades { font-size: 0.8rem; }
    .taula-dades th, .taula-dades td { padding: 0.6rem 0.4rem; }
    .page-content, .card { padding: 1rem; }
    .btn-icon-minimal { padding: 4px; }
    .btn-icon-minimal svg { width: 16px; height: 16px; }

    /* AFEGIT: Capçalera - Títol i icones més petits */
    .header-seccio h2 { 
        font-size: 1.3rem; 
        gap: 6px !important; 
    }
    .header-seccio h2 > div { 
        width: 22px !important; 
        height: 22px !important; 
    }

    /* AFEGIT: Botons principals més compactes */
    .btn-primary { 
        padding: 0.6rem 1rem; 
        font-size: 0.85rem; 
    }
    .btn-primary > div { 
        width: 16px !important; 
        height: 16px !important; 
    }
}