:root {
  --bg: #1f2530;
  --panel: #272e3a;
  --panel-2: #2f3744;
  --text: #e7ebf0;
  --muted: #9aa6b6;
  --accent: #4f8cff;
  --border: #3a4351;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}

#app { display: flex; height: 100vh; }

/* ---- LOGIN ---- */
.login-overlay {
  position: fixed; inset: 0; z-index: 4000;
  display: flex; align-items: center; justify-content: center;
  background: #1a1f28;
}
.login-overlay.hidden { display: none; }
.login-card {
  width: 320px; max-width: 90vw;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
  box-shadow: 0 18px 50px rgba(0,0,0,.5);
}
.login-card h2 { margin: 0; font-size: 24px; }
.login-sub { margin: 2px 0 16px; color: var(--muted); font-size: 13px; }
.login-card button { width: 100%; margin-top: 6px; }
.login-err {
  background: rgba(230,25,75,.15); color: #ff8aa0;
  border: 1px solid rgba(230,25,75,.4);
  padding: 7px 10px; border-radius: 8px; font-size: 13px; margin-bottom: 10px;
}
.login-err.hidden { display: none; }

/* ---- BARRA UTENTE ---- */
.userbar {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  margin-bottom: 10px;
}
.userbar.hidden { display: none; }
.user-name { font-size: 12px; color: var(--muted); margin-right: auto; }
.user-name::before { content: "👤 "; }

/* ---- PANNELLO UTENTI ---- */
.users-list { display: flex; flex-direction: column; gap: 8px; }
.user-row {
  border: 1px solid var(--border); border-radius: 10px; padding: 10px;
  background: var(--panel-2);
}
.user-row .ur-head { display: flex; align-items: center; gap: 8px; }
.user-row .ur-name { font-weight: 600; flex: 1; }
.user-row .ur-role { font-size: 11px; color: var(--muted); background: rgba(255,255,255,.07); padding: 1px 7px; border-radius: 10px; }
.user-row .ur-projects { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.ur-projects label { font-size: 12px; display: flex; align-items: center; gap: 4px; color: var(--text); }
.user-row .ur-actions { display: flex; gap: 6px; margin-top: 8px; }
.user-row .ur-actions button { font-size: 12px; }
.nu-projects { display: flex; flex-wrap: wrap; gap: 8px; }
.nu-projects label { font-size: 13px; display: flex; align-items: center; gap: 4px; }

/* overlay durante il trascinamento di una foto */
body.dragging::after {
  content: "Rilascia qui la foto per aggiungerla";
  position: fixed;
  inset: 16px;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  font-weight: 600;
  color: #fff;
  background: rgba(31, 37, 48, .82);
  border: 3px dashed var(--accent);
  border-radius: 16px;
  pointer-events: none;
}

/* ---- SIDEBAR ---- */
#sidebar {
  width: 320px;
  min-width: 320px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.side-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.side-header h1 {
  margin: 0 0 10px;
  font-size: 20px;
  letter-spacing: .3px;
}

.search-box { padding: 12px 16px 6px; }
.search-box input {
  width: 100%;
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 14px;
}

.view-controls {
  display: flex;
  gap: 6px;
  padding: 4px 16px 10px;
}

.projects { flex: 1; overflow-y: auto; padding: 4px 8px 12px; }

.project {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin: 8px 4px;
  overflow: hidden;
}
.project-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  cursor: pointer;
}
.project-head input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; }
.project-head .pname { flex: 1; font-weight: 600; font-size: 14px; }
.project-head .count {
  font-size: 12px; color: var(--muted);
  background: rgba(255,255,255,.06);
  padding: 1px 7px; border-radius: 10px;
}
.project-head .twisty { color: var(--muted); transition: transform .15s; }
.project.collapsed .twisty { transform: rotate(-90deg); }
.project.collapsed .poi-list { display: none; }

.poi-list { padding: 2px 6px 8px; }
.poi {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 13px;
}
.poi:hover { background: rgba(255,255,255,.05); }
.poi.selected { background: rgba(79,140,255,.18); box-shadow: inset 3px 0 0 var(--accent); }
.poi.selected .ptitle { font-weight: 600; color: #fff; }
.poi .dot { width: 11px; height: 11px; border-radius: 50%; flex: none; box-shadow: 0 0 0 2px rgba(0,0,0,.25); }
.poi .ptitle { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.poi .del { color: var(--muted); opacity: 0; font-size: 15px; padding: 0 4px; }
.poi:hover .del { opacity: 1; }
.proj-actions { padding: 0 10px 8px; }
.proj-actions button { font-size: 12px; }

.empty { color: var(--muted); font-size: 13px; padding: 20px 16px; text-align: center; }

.side-footer {
  border-top: 1px solid var(--border);
  padding: 8px 16px;
  font-size: 12px;
  color: var(--muted);
}

/* ---- MAPPA ---- */
#map { flex: 1; height: 100%; }

/* barra per il disegno di una tratta */
.draw-toolbar {
  position: fixed;
  bottom: 22px; left: 50%; transform: translateX(-50%);
  z-index: 1600;
  display: flex; align-items: center; gap: 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 8px 12px;
  border-radius: 12px;
  box-shadow: 0 8px 26px rgba(0,0,0,.45);
}
.draw-toolbar.hidden { display: none; }
.draw-toolbar #draw-info { font-size: 13px; color: var(--muted); margin-right: 4px; }
body.drawing #map { cursor: crosshair; }

/* selezione multipla sulla mappa */
body.selecting #map { cursor: crosshair; }
.marker-sel { filter: drop-shadow(0 0 0 #4f8cff) drop-shadow(0 0 5px #4f8cff) drop-shadow(0 0 5px #4f8cff); }
#btn-select.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.poi-check { width: 15px; height: 15px; accent-color: var(--accent); flex: none; cursor: pointer; }

/* durante la creazione di un collegamento */
body.linking #map { cursor: crosshair; }
body.linking::before {
  content: "Collegamento: clicca il punto di arrivo (Esc per annullare)";
  position: fixed;
  top: 12px; left: 50%; transform: translateX(-50%);
  z-index: 1500;
  background: var(--accent); color: #fff;
  padding: 7px 14px; border-radius: 20px;
  font-size: 13px; font-weight: 600;
  box-shadow: 0 4px 14px rgba(0,0,0,.35);
  pointer-events: none;
}

/* ---- BOTTONI ---- */
button {
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 14px;
  padding: 8px 12px;
  background: var(--panel-2);
  color: var(--text);
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.ghost { background: transparent; font-size: 12px; padding: 6px 9px; }

/* ---- MODALE ---- */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
}
.modal.hidden { display: none; }
.modal-card {
  width: 440px; max-width: 92vw; max-height: 92vh; overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px 22px;
  box-shadow: 0 18px 50px rgba(0,0,0,.5);
}
.modal-card h2 { margin: 0 0 14px; font-size: 18px; }

.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }
.field input, .field textarea, .field select {
  width: 100%;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
}
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }
.hint { font-size: 11px; color: var(--muted); margin: -4px 0 12px; }

.photo-preview { margin-bottom: 12px; text-align: center; }
.photo-preview.hidden { display: none; }
.photo-preview img { max-width: 100%; max-height: 180px; border-radius: 8px; border: 1px solid var(--border); }
.gps-info { display: block; font-size: 12px; margin-top: 6px; color: var(--muted); }
.gps-info.ok { color: #6fcf7f; }
.gps-info.warn { color: #f2c94c; }

.color-picker { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch {
  width: 26px; height: 26px; border-radius: 50%;
  cursor: pointer; border: 2px solid transparent;
}
.swatch.selected { border-color: #fff; box-shadow: 0 0 0 2px var(--accent); }

.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }

/* ---- MARKER PERSONALIZZATO ---- */
.pin {
  width: 26px; height: 26px;
  position: relative;
}
.pin svg { width: 26px; height: 36px; display: block; filter: drop-shadow(0 2px 2px rgba(0,0,0,.4)); }

/* ---- POPUP ---- */
.leaflet-popup-content { margin: 0; width: 240px !important; }
.popup img { width: 100%; border-radius: 8px 8px 0 0; display: block; max-height: 170px; object-fit: cover; }
.popup .pbody { padding: 8px 10px 10px; }
.popup h3 { margin: 0 0 4px; font-size: 15px; color: #1a1a1a; }
.popup p { margin: 0 0 6px; font-size: 13px; color: #333; white-space: pre-wrap; }
.popup .meta { font-size: 11px; color: #777; }
.popup .actions { margin-top: 8px; display: flex; gap: 6px; }
.popup .actions button { font-size: 12px; padding: 5px 9px; background: #eef; color: #224; border: 1px solid #ccd; }
.popup .actions button.danger { background: #fee; color: #a22; border-color: #ecc; }
.popup img { cursor: zoom-in; }
.lcolors { display: flex; flex-wrap: wrap; gap: 5px; margin: 5px 0 8px; }
.lswatch {
  width: 18px; height: 18px; border-radius: 50%;
  cursor: pointer; border: 2px solid transparent;
}
.lswatch:hover { transform: scale(1.12); }
.lswatch.sel { border-color: #333; box-shadow: 0 0 0 2px #fff inset; }

/* ---- VISUALIZZATORE FOTO A TUTTO SCHERMO ---- */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 3000;
  background: rgba(0, 0, 0, .92);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}
.lightbox.hidden { display: none; }
.lightbox img {
  max-width: 96vw;
  max-height: 96vh;
  object-fit: contain;
  border-radius: 6px;
  box-shadow: 0 10px 50px rgba(0, 0, 0, .6);
}
.lightbox-close {
  position: fixed;
  top: 16px;
  right: 20px;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  font-size: 20px;
  color: #fff;
  background: rgba(255, 255, 255, .12);
  border: 1px solid rgba(255, 255, 255, .3);
}
.lightbox-close:hover { background: rgba(255, 255, 255, .25); }
