:root {
  --paper: #f7f6f2;
  --ink: #17181a;
  --muted: #6d6e73;
  --card: rgba(255, 255, 255, 0.88);
  --solid-card: #fff;
  --line: rgba(24, 25, 27, 0.09);
  --shadow: 0 16px 50px rgba(27, 31, 38, 0.14);
  --fire: #ff5a36;
  --fire-soft: #ff8a4c;
  --violet: #7357ff;
  --lime: #b9ee62;
  --blue: #2878ff;
  --safe-top: max(12px, env(safe-area-inset-top));
  --safe-bottom: max(12px, env(safe-area-inset-bottom));
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; width: 100%; height: 100%; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.45 Inter, ui-rounded, "SF Pro Rounded", -apple-system,
    BlinkMacSystemFont, "Segoe UI", sans-serif;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }
#map { position: absolute; inset: 0; }
.hidden { display: none !important; }

/* Chrome della mappa: marchio e Territorio, con GPS incorporato. */
#topbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 8;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: var(--safe-top) 12px 16px;
  pointer-events: none;
  background: linear-gradient(180deg, rgba(247, 246, 242, .9), rgba(247, 246, 242, 0));
}
#topbar > * { pointer-events: auto; }
.tool-dock,
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 7px;
}
.map-tools {
  display: flex;
  align-items: center;
  gap: 7px;
  animation: tools-in .18s ease-out;
}
.brand-lockup {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 5px 11px 5px 6px;
  border: 1px solid rgba(255, 255, 255, .7);
  border-radius: 15px;
  background: rgba(255, 255, 255, .8);
  box-shadow: 0 8px 24px rgba(28, 31, 36, .1);
  backdrop-filter: blur(18px) saturate(1.25);
  flex: 0 0 auto;
  color: var(--ink);
  cursor: pointer;
  transition: transform .18s ease, border-color .18s ease,
              box-shadow .18s ease;
}
.brand-lockup:active { transform: scale(.96); }
.brand-lockup.is-open {
  border-color: rgba(255, 90, 54, .3);
  box-shadow: 0 9px 26px rgba(255, 90, 54, .18);
}
.brand-mark, .auth-mark {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  color: #fff;
  background: linear-gradient(145deg, var(--fire-soft), var(--fire));
  box-shadow: 0 7px 16px rgba(255, 90, 54, .28);
}
.brand-mark {
  width: 32px;
  height: 32px;
  border-radius: 11px;
}
.brand-mark svg, .auth-mark svg { width: 21px; height: 21px; fill: currentColor; }
.brand-name { font-size: 15px; font-weight: 800; letter-spacing: -.02em; }

.territory-button,
.map-tool-button {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 8px 6px 10px;
  border: 0;
  border-radius: 15px;
  color: #fff;
  background: var(--ink);
  box-shadow: 0 9px 24px rgba(20, 21, 23, .22);
  cursor: pointer;
  transition: transform .18s ease, box-shadow .18s ease;
}
.territory-button:active,
.map-tool-button:active { transform: scale(.96); }
.territory-button:focus-visible,
.map-tool-button:focus-visible,
.brand-lockup:focus-visible,
.close-button:focus-visible,
.logout-button:focus-visible,
.card button:focus-visible,
.card input:focus-visible {
  outline: 3px solid rgba(40, 120, 255, .35);
  outline-offset: 2px;
}
.territory-button svg,
.map-tool-button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.territory-label { font-weight: 700; }
.territory-button strong {
  min-width: 47px;
  padding: 4px 7px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 800;
  text-align: center;
  color: var(--ink);
  background: var(--lime);
}
.map-tool-button {
  width: 44px;
  justify-content: center;
  overflow: hidden;
  padding: 6px;
  border: 1px solid rgba(255, 255, 255, .72);
  color: var(--ink);
  background: rgba(255, 255, 255, .82);
  box-shadow: 0 8px 24px rgba(28, 31, 36, .1);
  backdrop-filter: blur(18px) saturate(1.25);
}
.map-tool-button span { display: none; font-size: 12px; font-weight: 800; }
.map-tool-button.is-active {
  width: auto;
  gap: 7px;
  padding-inline: 11px;
  border-color: rgba(255, 90, 54, .22);
  color: #fff;
  background: var(--fire);
  box-shadow: 0 9px 24px rgba(255, 90, 54, .26);
}
.map-tool-button.is-active span { display: inline; }
.draw-tool-button.is-active {
  border-color: rgba(115, 87, 255, .24);
  background: var(--violet);
  box-shadow: 0 9px 24px rgba(115, 87, 255, .25);
}
.edit-history {
  display: flex;
  gap: 5px;
}
.edit-history button {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, .72);
  border-radius: 14px;
  color: var(--ink);
  background: rgba(255, 255, 255, .86);
  box-shadow: 0 8px 24px rgba(28, 31, 36, .1);
  backdrop-filter: blur(18px) saturate(1.25);
  cursor: pointer;
}
.edit-history button:disabled { opacity: .35; cursor: default; }
.edit-history button:not(:disabled):active { transform: scale(.94); }
.edit-history svg { width: 18px; height: 18px; }
body.segment-editing #territoryToggle { display: none; }
body.segment-editing .map-tool-button:not(.is-active) { display: none; }

.map-notice {
  position: fixed;
  left: 50%;
  bottom: var(--safe-bottom);
  z-index: 20;
  max-width: calc(100vw - 28px);
  min-height: 44px;
  display: flex;
  align-items: center;
  gap: 13px;
  padding: 9px 10px 9px 15px;
  border: 1px solid rgba(255, 255, 255, .14);
  border-radius: 15px;
  color: #fff;
  background: rgba(23, 24, 26, .92);
  box-shadow: 0 12px 36px rgba(20, 21, 23, .26);
  backdrop-filter: blur(18px);
  transform: translateX(-50%);
  font-size: 12px;
  font-weight: 700;
}
.map-notice span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.map-notice button {
  min-height: 30px;
  flex: 0 0 auto;
  padding: 5px 10px;
  border: 0;
  border-radius: 9px;
  color: var(--ink);
  background: var(--lime);
  font-size: 11px;
  font-weight: 900;
  cursor: pointer;
}

/* Superficie di disegno/cancellazione: intercetta un solo puntatore sopra la
   mappa; il piccolo mirino segue esattamente dito o mouse. */
.segment-eraser-surface {
  position: fixed;
  inset: 0;
  z-index: 7;
  overflow: hidden;
  cursor: crosshair;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.segment-eraser-cursor {
  position: absolute;
  left: 0;
  top: 0;
  width: 34px;
  height: 34px;
  opacity: 0;
  border-radius: 50%;
  border: 2px solid var(--fire);
  background: rgba(255, 255, 255, .12);
  box-shadow: 0 5px 18px rgba(23, 24, 26, .2),
              inset 0 0 0 1px rgba(255, 255, 255, .9);
  transform: translate(-50%, -50%);
  pointer-events: none;
  transition: opacity .1s ease;
}
.segment-eraser-cursor.is-visible { opacity: 1; }
.segment-eraser-cursor.is-drawing { border-color: var(--violet); }
.segment-eraser-cursor::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 5px;
  height: 5px;
  border: 1px solid #fff;
  border-radius: 50%;
  background: var(--fire);
  box-shadow: 0 0 0 2px rgba(255, 90, 54, .2);
  transform: translate(-50%, -50%);
}
.segment-eraser-cursor.is-drawing::after {
  background: var(--violet);
  box-shadow: 0 0 0 2px rgba(115, 87, 255, .2);
}

/* Stato GPS: solo un punto a destra dei chilometri, dentro Territorio. */
.live-status {
  position: relative;
  width: 8px;
  height: 8px;
  flex: 0 0 8px;
  display: grid;
  place-items: center;
  margin-left: 1px;
  pointer-events: none;
}
.live-pulse {
  width: 6px;
  height: 6px;
  flex: 0 0 auto;
  border: 1.5px solid rgba(255, 255, 255, .92);
  border-radius: 50%;
  background: var(--blue);
  box-shadow: 0 0 0 2px rgba(40, 120, 255, .2);
}
.live-status-copy {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}
.live-status.is-live .live-pulse {
  animation: live-pulse 2.1s ease-out infinite;
}
.live-status.is-error .live-pulse {
  background: var(--fire);
  box-shadow: 0 0 0 3px rgba(255, 90, 54, .18);
  animation: none;
}
@keyframes live-pulse {
  0%, 35% { box-shadow: 0 0 0 2px rgba(40, 120, 255, .2); }
  70%, 100% { box-shadow: 0 0 0 5px rgba(40, 120, 255, 0); }
}

/* Dashboard territorio. */
.territory-overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-top: env(safe-area-inset-top);
  background: rgba(16, 17, 19, .24);
  backdrop-filter: blur(5px);
  animation: overlay-in .2s ease both;
}
.territory-panel {
  width: 100%;
  max-height: min(90dvh, 880px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, .75);
  border-radius: 30px 30px 0 0;
  background:
    radial-gradient(circle at 90% -5%, rgba(185, 238, 98, .4), transparent 30%),
    var(--paper);
  box-shadow: 0 -20px 70px rgba(18, 20, 24, .22);
  animation: sheet-in .3s cubic-bezier(.2, .75, .2, 1) both;
  will-change: transform;
}
.territory-panel.is-dragging,
.territory-panel.is-snapping,
.territory-panel.is-dismissing { animation: none; }
.territory-panel.is-dragging { transition: none; }
.territory-panel.is-snapping { transition: transform .22s cubic-bezier(.2, .75, .2, 1); }
.territory-panel.is-dismissing { transition: transform .18s ease-in; }
.sheet-handle {
  width: 42px;
  height: 5px;
  flex: 0 0 auto;
  margin: 10px auto 2px;
  border-radius: 999px;
  background: rgba(23, 24, 26, .15);
}
.territory-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 15px 20px 8px;
}
.eyebrow {
  margin: 0 0 3px;
  color: var(--fire);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: .12em;
  text-transform: uppercase;
}
.territory-head h2 {
  margin: 0;
  font-size: clamp(22px, 5.5vw, 30px);
  line-height: 1.08;
  letter-spacing: -.045em;
}
.close-button {
  width: 40px;
  height: 40px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 14px;
  color: var(--ink);
  background: rgba(255, 255, 255, .78);
  cursor: pointer;
}
.close-button svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}
.auto-update {
  display: flex;
  align-items: center;
  gap: 7px;
  margin: 0 20px 15px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}
.auto-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #38bd68;
  box-shadow: 0 0 0 3px rgba(56, 189, 104, .13);
}
.territory-content {
  display: grid;
  gap: 24px;
  overflow: auto;
  padding: 0 20px 28px;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.hero-stat {
  position: relative;
  min-height: 176px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  overflow: hidden;
  padding: 22px;
  border-radius: 25px;
  color: #fff;
  background:
    radial-gradient(circle at 88% 18%, rgba(255, 162, 92, .95) 0 5%, transparent 6%),
    radial-gradient(circle at 85% 20%, rgba(255, 90, 54, .34), transparent 31%),
    linear-gradient(145deg, #1d1e22, #2c2730);
  box-shadow: 0 14px 34px rgba(27, 25, 30, .18);
}
.hero-stat::after {
  content: "";
  position: absolute;
  width: 150px;
  height: 150px;
  top: -76px;
  right: -45px;
  border: 28px solid rgba(185, 238, 98, .92);
  border-radius: 50%;
  transform: rotate(-18deg);
}
.hero-kicker {
  position: relative;
  z-index: 1;
  margin-bottom: 2px;
  color: rgba(255, 255, 255, .68);
  font-size: 12px;
  font-weight: 750;
}
.hero-stat strong {
  position: relative;
  z-index: 1;
  font-size: clamp(42px, 13vw, 62px);
  line-height: 1;
  letter-spacing: -.07em;
}
.hero-stat > span:not(.hero-kicker) {
  position: relative;
  z-index: 1;
  margin-top: 8px;
  color: rgba(255, 255, 255, .72);
  font-size: 12px;
}
.progress-track {
  position: relative;
  z-index: 1;
  height: 6px;
  overflow: hidden;
  margin-top: 14px;
  border-radius: 999px;
  background: rgba(255, 255, 255, .15);
}
.progress-track i {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--fire), #ffb05e, var(--lime));
  transition: width .45s ease;
}
.section-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 11px;
}
.section-heading h3 { margin: 0; font-size: 17px; letter-spacing: -.025em; }
.section-heading span { color: var(--muted); font-size: 11px; font-weight: 700; text-transform: uppercase; }
.category-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 9px;
}
.category-card {
  min-width: 0;
  padding: 14px 12px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: rgba(255, 255, 255, .8);
  box-shadow: 0 6px 20px rgba(30, 32, 36, .05);
}
.category-icon {
  width: 29px;
  height: 29px;
  display: grid;
  place-items: center;
  margin-bottom: 15px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 900;
}
.category-card.road .category-icon { color: #b93618; background: #ffd4c8; }
.category-card.path .category-icon { color: #416d0b; background: #dff7b5; }
.category-card.rail .category-icon { color: #4f37c4; background: #ddd7ff; }
.category-card span {
  display: block;
  overflow: hidden;
  color: var(--muted);
  font-size: 11px;
  font-weight: 650;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.category-card strong {
  display: block;
  overflow: hidden;
  margin: 2px 0 8px;
  font-size: 17px;
  letter-spacing: -.035em;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.mini-progress {
  height: 4px;
  overflow: hidden;
  border-radius: 999px;
  background: rgba(23, 24, 26, .08);
}
.mini-progress i { display: block; height: 100%; border-radius: inherit; }
.road .mini-progress i { background: var(--fire); }
.path .mini-progress i { background: #83bd2a; }
.rail .mini-progress i { background: var(--violet); }
.area-list { display: grid; gap: 10px; }
.area-card {
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: rgba(255, 255, 255, .8);
}
.area-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 13px;
}
.area-top strong { font-size: 15px; letter-spacing: -.02em; }
.area-top span {
  padding: 5px 8px;
  border-radius: 9px;
  color: #4a6e0d;
  background: rgba(185, 238, 98, .45);
  font-size: 11px;
  font-weight: 850;
}
.area-breakdown { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; }
.area-value {
  min-width: 0;
  padding: 9px;
  border-radius: 13px;
  background: rgba(23, 24, 26, .035);
}
.area-value small { display: block; color: var(--muted); font-size: 9px; text-transform: uppercase; }
.area-value b { display: block; margin-top: 2px; font-size: 12px; }
.empty-state {
  padding: 26px 18px;
  border: 1px dashed rgba(23, 24, 26, .16);
  border-radius: 20px;
  color: var(--muted);
  text-align: center;
  background: rgba(255, 255, 255, .45);
}
.territory-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex: 0 0 auto;
  padding: 13px 20px calc(var(--safe-bottom) + 2px);
  border-top: 1px solid var(--line);
  background: rgba(247, 246, 242, .94);
}
.territory-footer span { max-width: 260px; color: var(--muted); font-size: 10px; }
.logout-button {
  border: 0;
  padding: 8px 10px;
  color: #c63c23;
  background: transparent;
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
}

/* Autenticazione. */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  padding: max(22px, env(safe-area-inset-top)) 18px max(22px, env(safe-area-inset-bottom));
  background:
    radial-gradient(circle at 12% 15%, rgba(185, 238, 98, .58), transparent 27%),
    radial-gradient(circle at 92% 86%, rgba(255, 90, 54, .22), transparent 29%),
    var(--paper);
}
.card {
  width: 100%;
  max-width: 390px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 28px;
  border: 1px solid rgba(255, 255, 255, .75);
  border-radius: 30px;
  background: rgba(255, 255, 255, .78);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}
.auth-brand { display: flex; align-items: center; gap: 10px; font-weight: 900; }
.auth-mark { width: 40px; height: 40px; border-radius: 14px; }
.card h1 { margin: 10px 0 -6px; font-size: clamp(30px, 9vw, 42px); line-height: 1; letter-spacing: -.06em; }
.muted { margin: 0 0 7px; color: var(--muted); font-size: 13px; }
.card label { display: grid; gap: 6px; color: #45464a; font-size: 11px; font-weight: 800; }
.card input {
  width: 100%;
  min-height: 49px;
  border: 1px solid var(--line);
  border-radius: 15px;
  padding: 12px 14px;
  color: var(--ink);
  background: rgba(255, 255, 255, .92);
  font-size: 15px;
}
.card input::placeholder { color: #a1a1a6; }
.card .row { display: grid; grid-template-columns: 1.05fr .95fr; gap: 9px; margin-top: 4px; }
.card button {
  min-height: 48px;
  border-radius: 15px;
  padding: 11px;
  cursor: pointer;
  font-weight: 800;
}
.primary { border: 0; color: #fff; background: var(--ink); }
.secondary { border: 1px solid var(--line); color: var(--ink); background: rgba(255, 255, 255, .8); }
.err { min-height: 18px; color: #cf3f24; font-size: 12px; }

.maplibregl-ctrl-attrib {
  margin-right: 4px !important;
  margin-bottom: 4px !important;
  border-radius: 8px 0 0;
  font-size: 9px;
}

@keyframes overlay-in { from { opacity: 0; } }
@keyframes sheet-in { from { transform: translateY(28px); opacity: .6; } }
@keyframes tools-in {
  from { opacity: 0; transform: translateX(-7px) scale(.96); }
}

@media (min-width: 760px) {
  :root { --safe-top: max(9px, env(safe-area-inset-top)); }
  #topbar { padding-inline: 14px; }
  .brand-lockup, .territory-button, .map-tool-button { min-height: 40px; }
  .brand-lockup { padding: 4px 9px 4px 5px; }
  .brand-mark { width: 30px; height: 30px; }
  .territory-button { padding: 5px 7px 5px 9px; }
  .territory-button strong { min-width: 44px; padding-block: 3px; }
  .territory-overlay { justify-content: flex-end; padding: 12px; }
  .territory-panel {
    width: min(470px, 42vw);
    height: calc(100dvh - 24px);
    max-height: none;
    border-radius: 30px;
  }
  .sheet-handle { display: none; }
  .territory-head { padding-top: 24px; }
  .territory-footer { padding-bottom: 17px; }
}

@media (max-width: 410px) {
  .brand-name { display: none; }
  .brand-lockup { padding-right: 7px; }
  .territory-label { display: none; }
  .territory-head, .territory-content { padding-left: 16px; padding-right: 16px; }
  .auto-update { margin-left: 16px; margin-right: 16px; }
  .territory-footer { padding-left: 16px; padding-right: 16px; }
  .category-card { padding-inline: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}
