:root {
  --green: #16A34A;
  --green-dark: #15803D;
  --green-deeper: #14532D;
  --green-soft: #DCFCE7;
  --yellow: #FACC15;
  --yellow-glow: #FDE047;
  --ink: #0F172A;
  --ink-2: #334155;
  --muted: #64748B;
  --line: #E2E8F0;
  --bg: #ffffff;
  --panel-w: 380px;
  --topbar-h: 72px;
  --shadow: 0 8px 28px rgba(15, 23, 42, 0.12);
  --radius: 16px;
  --font: "DM Sans", "Segoe UI", "Helvetica Neue", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); font-family: var(--font); }
body { display: flex; flex-direction: column; min-height: 100%; }
button, input { font-family: inherit; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* Header */
.topbar {
  height: var(--topbar-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px 0 18px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 0 0 auto; }
.logo {
  width: 28px; height: 28px; border-radius: 8px;
  background: var(--green);
  box-shadow: inset 0 0 0 3px #fff, 0 0 0 2px var(--green);
  flex: 0 0 auto;
}
.brand-text h1 {
  margin: 0; font-size: 16px; font-weight: 700; letter-spacing: -0.01em; line-height: 1.15; color: var(--ink);
}
.brand-text p {
  margin: 1px 0 0; font-size: 12px; color: var(--muted); font-weight: 500; white-space: nowrap;
}

.search {
  position: relative;
  flex: 1 1 280px;
  max-width: 460px;
  margin-left: auto;
}
.search-icon {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  color: var(--muted); pointer-events: none;
}
#search-input {
  width: 100%;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 36px 0 40px;
  font-size: 14px;
  background: #F8FAFC;
  color: var(--ink);
  outline: none;
  transition: border-color .15s, box-shadow .15s, background .15s;
}
#search-input:focus {
  background: #fff;
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.18);
}
.search-clear {
  position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  width: 26px; height: 26px; border: 0; border-radius: 50%;
  background: #E2E8F0; color: var(--ink-2); font-size: 18px; line-height: 1; cursor: pointer;
}
.search-results {
  position: absolute; left: 0; right: 0; top: calc(100% + 6px);
  margin: 0; padding: 6px;
  list-style: none;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  max-height: 280px;
  overflow: auto;
  z-index: 30;
}
.search-results li button {
  width: 100%; text-align: left; border: 0; background: transparent;
  padding: 9px 10px; border-radius: 8px; cursor: pointer; color: var(--ink);
}
.search-results li button:hover,
.search-results li button.active { background: var(--green-soft); }
.search-results .res-addr { display: block; font-size: 13.5px; font-weight: 600; }
.search-results .res-meta { display: block; font-size: 11.5px; color: var(--muted); margin-top: 1px; }
.search-results .res-empty { padding: 12px; color: var(--muted); font-size: 13px; }

.topbar-meta { flex: 0 0 auto; display: flex; align-items: center; gap: 10px; }
.mode-toggle {
  display: flex;
  border: 1px solid var(--line);
  border-radius: 999px;
  overflow: hidden;
  background: #fff;
}
.mode-toggle a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 42px;
  padding: 5px 12px;
  font-size: 12.5px;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-decoration: none;
  color: var(--ink-2);
}
.mode-toggle a.active {
  background: var(--green);
  color: #fff;
}
.mode-toggle a:not(.active):hover { background: var(--green-soft); }
.hint-3d {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 8px 10px;
  font-size: 12px;
  color: var(--muted);
  max-width: 200px;
  line-height: 1.35;
}
.hint-3d strong { color: var(--ink-2); font-weight: 600; }
.btn-reset {
  border: 0;
  background: transparent;
  color: var(--green-dark);
  font-weight: 700;
  font-size: 12.5px;
  padding: 8px 12px;
  cursor: pointer;
  width: 100%;
  text-align: left;
}
.btn-reset:hover { background: var(--green-soft); }

/* MapLibre controls */
.maplibregl-ctrl-group {
  border: 1px solid var(--line) !important;
  border-radius: 12px !important;
  box-shadow: var(--shadow) !important;
  overflow: hidden;
}
.maplibregl-ctrl-group button { width: 34px !important; height: 34px !important; }
.maplibregl-ctrl-attrib { font-size: 11px !important; }
.maplibregl-ctrl-bottom-left { margin-bottom: 4px; }

.chip {
  display: inline-flex; align-items: center;
  background: var(--green-soft); color: var(--green-deeper);
  font-size: 12.5px; font-weight: 600;
  padding: 6px 11px; border-radius: 999px;
  white-space: nowrap;
}

/* Stage / map */
.stage { position: relative; flex: 1 1 auto; min-height: 0; }
#map { position: absolute; inset: 0; background: #dbe4ea; z-index: 0; }

.map-controls {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.control-card {
  background: #fff;
  border-radius: 12px;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  overflow: hidden;
  display: flex;
}
.control-card.layers { flex-direction: column; padding: 6px 8px; gap: 2px; }
.seg {
  border: 0; background: transparent;
  padding: 8px 14px; font-size: 13px; font-weight: 600;
  color: var(--ink-2); cursor: pointer; min-width: 96px;
}
.seg + .seg { border-left: 1px solid var(--line); }
.seg.active { background: var(--green); color: #fff; }
.toggle {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; font-weight: 500; color: var(--ink-2);
  padding: 5px 6px; cursor: pointer; user-select: none;
}
.toggle input { accent-color: var(--green); width: 15px; height: 15px; }

/* Leaflet tweaks */
.leaflet-container { font-family: var(--font); }
.leaflet-control-zoom {
  border: 1px solid var(--line) !important;
  border-radius: 12px !important;
  overflow: hidden;
  box-shadow: var(--shadow) !important;
}
.leaflet-control-zoom a {
  width: 34px !important; height: 34px !important;
  line-height: 34px !important;
  color: var(--ink) !important;
  border-bottom-color: var(--line) !important;
}
.leaflet-bottom.leaflet-right { margin-bottom: 8px; }

/* Side panel */
.panel {
  position: absolute;
  top: 12px;
  right: 12px;
  bottom: 28px;
  width: min(var(--panel-w), calc(100% - 24px));
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  z-index: 30;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.panel[hidden] { display: none; }
.panel-close {
  position: absolute; top: 10px; right: 10px;
  width: 32px; height: 32px; border: 0; border-radius: 50%;
  background: #F1F5F9; color: var(--ink-2);
  font-size: 22px; line-height: 1; cursor: pointer;
  z-index: 2;
}
.panel-close:hover { background: #E2E8F0; }
.panel-scroll { padding: 20px 22px 24px; overflow: auto; }
.badge {
  display: inline-block;
  margin: 0 0 8px;
  background: var(--green-soft); color: var(--green-deeper);
  font-size: 11px; font-weight: 700; letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 3px 8px; border-radius: 999px;
}
#p-indirizzo {
  margin: 0 28px 6px 0;
  font-size: 22px; font-weight: 700; letter-spacing: -0.03em;
  line-height: 1.25; color: var(--ink);
}
.panel-nome { margin: 0 0 4px; font-size: 14px; font-weight: 600; color: var(--green-dark); }
.panel-angolo { margin: 0 0 8px; font-size: 13px; color: var(--muted); }
.facts {
  display: grid; grid-template-columns: 1fr; gap: 10px;
  margin: 16px 0 0; padding: 14px 0 0;
  border-top: 1px solid var(--line);
}
.facts div { margin: 0; }
.facts dt { margin: 0; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.facts dd { margin: 2px 0 0; font-size: 14px; font-weight: 600; color: var(--ink); word-break: break-word; }
.facts dd.muted { font-weight: 500; color: var(--muted); font-style: italic; }

.owners { margin-top: 18px; }
.owners h3 {
  margin: 0 0 10px; font-size: 13px; font-weight: 700;
  letter-spacing: 0.02em; text-transform: uppercase; color: var(--green-deeper);
}
.empty {
  background: #F8FAFC;
  border: 1px dashed #CBD5E1;
  border-radius: 12px;
  padding: 16px 14px;
}
.empty-title { margin: 0 0 4px; font-size: 14px; font-weight: 700; color: var(--ink-2); }
.empty-hint { margin: 0; font-size: 13px; line-height: 1.45; color: var(--muted); }

.attrib {
  flex: 0 0 auto;
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px;
  padding: 5px 14px;
  font-size: 11px; color: var(--muted);
  background: #fff;
  border-top: 1px solid var(--line);
  z-index: 20;
}
.attrib .dot { opacity: 0.5; }

/* Mobile */
@media (max-width: 720px) {
  :root { --topbar-h: auto; }
  .topbar {
    height: auto;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas:
      "brand meta"
      "search search";
    align-items: center;
    padding: 10px 12px;
    gap: 8px 10px;
  }
  .brand { grid-area: brand; min-width: 0; width: auto; }
  .brand-text p { overflow: hidden; text-overflow: ellipsis; }
  .search { grid-area: search; width: 100%; flex: none; max-width: none; margin-left: 0; order: unset; }
  .topbar-meta { grid-area: meta; margin-left: 0; flex-shrink: 0; }
  .hint-3d { display: none; }
  .map-controls { top: 8px; left: 8px; }
  .seg { min-width: 78px; padding: 7px 10px; font-size: 12px; }
  .panel {
    top: auto;
    right: 0; left: 0; bottom: 0;
    width: 100%;
    max-height: 54vh;
    border-radius: 18px 18px 0 0;
    border-bottom: 0;
  }
  #p-indirizzo { font-size: 19px; }
  .attrib { font-size: 10px; padding: 4px 10px 6px; }
  .stage:has(#panel:not([hidden])) .map-controls { display: none; }
}

@media (max-width: 400px) {
  .brand-text h1 { font-size: 15px; }
  .brand-text p { font-size: 11px; white-space: nowrap; }
}
