:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface2: #273549;
  --border: #334155;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-light: rgba(59,130,246,0.15);
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --success: #22c55e;
  --danger: #ef4444;
  --header-h: 56px;
}

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

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

/* Header */
header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 16px;
  flex-shrink: 0;
  z-index: 100;
}

header h1 {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.3px;
  color: var(--text);
  white-space: nowrap;
}

header h1 span { color: var(--accent); }

#stats {
  font-size: 12px;
  color: var(--text-muted);
  margin-left: 0;
}

#loader-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
}

.header-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.btn-softinfo {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-muted);
  padding: 7px 10px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color .15s, color .15s, box-shadow .15s;
}

.btn-softinfo:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-softinfo:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-light);
}

.dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--success);
}

.dot.loading { background: var(--accent); animation: pulse 1s infinite; }
.dot.error { background: var(--danger); }

@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.4} }

/* Main layout */
main {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* Sidebar (+ larghezza modificabile tramite `.sidebar-resizer` + JS) */
#sidebar {
  width: min(500px, 42vw);
  min-width: 280px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: none;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-resizer {
  flex-shrink: 0;
  width: 8px;
  margin: 0 -3px 0 -3px;
  cursor: col-resize;
  touch-action: none;
  align-self: stretch;
  position: relative;
  z-index: 6;
  background: transparent;
  outline: none;
}

.sidebar-resizer::before {
  content: '';
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 3px;
  top: 0;
  bottom: 0;
  background: var(--border);
  border-radius: 2px;
  transition: background .12s ease, box-shadow .12s ease;
}

.sidebar-resizer:hover::before,
.sidebar-resizer:focus-visible::before {
  background: var(--accent);
}

.sidebar-resizer.is-dragging::before {
  background: var(--accent);
}

@media (prefers-reduced-motion: reduce) {
  .sidebar-resizer::before {
    transition: none;
  }
}

/* Filters */
#filters {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.filter-row {
  display: flex;
  gap: 8px;
}

.filter-row-inline {
  align-items: center;
}

.filter-checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}

.filter-checkbox input {
  flex: 0 0 auto;
}

th.col-l131,
td.col-l131 {
  width: 3.25rem;
  text-align: center;
  white-space: nowrap;
}

th.col-pec,
td.col-pec {
  width: 3.75rem;
  text-align: center;
  white-space: nowrap;
}

th.col-info,
td.col-info {
  width: 3.75rem;
  text-align: center;
  white-space: nowrap;
}

select, input[type=text] {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 8px 10px;
  font-size: 13px;
  outline: none;
  transition: border-color .15s;
  min-width: 0;
}

select:focus, input[type=text]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-light);
}

select option { background: var(--surface2); }

#search-input {
  width: 100%;
}

.btn-reset {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-muted);
  padding: 8px 12px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: all .15s;
}

.btn-reset:hover { border-color: var(--accent); color: var(--accent); }

.group-panel {
  padding-top: 4px;
  border-top: 1px solid var(--border);
  margin-top: 4px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.group-panel-title {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-muted);
}

.group-detail {
  font-size: 11.5px;
  line-height: 1.45;
  color: var(--text-muted);
  margin: 0;
}

.group-detail.empty { font-style: italic; }

.group-detail strong {
  color: var(--text);
  font-weight: 600;
}

.group-names {
  display: block;
  margin-top: 6px;
  font-size: 11px;
  color: var(--text-muted);
}

/* Table area */
#table-area {
  flex: 1;
  overflow-y: auto;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

#result-bar {
  padding: 8px 16px;
  font-size: 11px;
  color: var(--text-muted);
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.export-bar {
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.sel-count {
  font-size: 11px;
  color: var(--text-muted);
}

.export-actions {
  display: flex;
  gap: 8px;
}

.btn-export {
  background: var(--accent);
  border: 1px solid transparent;
  border-radius: 8px;
  color: white;
  padding: 7px 10px;
  font-size: 12px;
  cursor: pointer;
  transition: background .15s, transform .05s;
  white-space: nowrap;
}

.btn-export:hover { background: var(--accent-hover); }
.btn-export:active { transform: translateY(1px); }
.btn-export:disabled { opacity: .4; cursor: not-allowed; }

.col-check { width: 36px; }

td.check {
  width: 36px;
  padding: 8px 10px;
}

table {
  width: 100%;
  min-width: 0;
  border-collapse: collapse;
  font-size: 12.5px;
}

thead th {
  position: sticky;
  top: 0;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
  padding: 9px 12px;
  text-align: left;
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}

thead th:hover { color: var(--text); }

thead th.num,
tbody td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  width: 1%;
  min-width: 60px;
  padding-left: 6px;
  padding-right: 10px;
}

tbody tr {
  border-bottom: 1px solid rgba(51,65,85,.5);
  cursor: pointer;
  transition: background .1s;
}

tbody tr:hover { background: var(--surface2); }
tbody tr.selected { background: var(--accent-light); }
tbody tr.selected td { color: var(--text); }

tbody td {
  padding: 9px 10px;
  color: var(--text);
  white-space: nowrap;
}

/* Solo il nome comune viene troncato: le altre colonne restano leggibili */
tbody td.comune {
  font-weight: 500;
  max-width: 9rem;
  min-width: 4rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

tbody td.code {
  font-family: monospace;
  color: var(--text-muted);
  font-size: 11px;
  min-width: 72px;
}

/* Pagination */
#pagination {
  padding: 10px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  flex-shrink: 0;
}

.btn-page {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--text);
  padding: 5px 10px;
  font-size: 12px;
  cursor: pointer;
  transition: all .15s;
}

.btn-page:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.btn-page:disabled { opacity: .4; cursor: default; }
.btn-page.active { background: var(--accent); border-color: var(--accent); color: #fff; }

#page-info { font-size: 12px; color: var(--text-muted); }

/* Map */
#map {
  flex: 1;
  z-index: 0;
}

/* Leaflet overrides */
.leaflet-container { background: #1a2540; }
.leaflet-popup-content-wrapper {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0,0,0,.4);
}
.leaflet-popup-tip { background: var(--surface); }
.leaflet-popup-content { font-size: 13px; line-height: 1.6; }
.leaflet-popup-content strong { color: var(--accent); font-size: 14px; }
.leaflet-popup-content .meta { color: var(--text-muted); font-size: 11px; margin-top: 4px; }
.leaflet-control-zoom a {
  background: var(--surface) !important;
  color: var(--text) !important;
  border-color: var(--border) !important;
}
.leaflet-bar { border: 1px solid var(--border) !important; }

/* Loading overlay */
#loading {
  position: fixed;
  inset: 0;
  background: rgba(15,23,42,.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  flex-direction: column;
  gap: 16px;
}

#loading.hidden { display: none; }

.spinner {
  width: 40px; height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

#loading p { color: var(--text-muted); font-size: 13px; }

/* Modal (software info) */
.modal.hidden { display: none; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 10000;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(15,23,42,.78);
}

.modal-dialog {
  position: relative;
  width: min(640px, calc(100vw - 32px));
  margin: 72px auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 18px 45px rgba(0,0,0,.45);
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  font-size: 14px;
  font-weight: 700;
  letter-spacing: -0.2px;
  margin: 0;
}

.modal-close {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-muted);
  padding: 6px 10px;
  cursor: pointer;
  transition: border-color .15s, color .15s, box-shadow .15s;
}

.modal-close:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.modal-close:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-light);
}

.modal-body {
  padding: 14px 16px 16px;
}

.modal-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.modal-field {
  background: var(--surface2);
  border: 1px solid rgba(51,65,85,.8);
  border-radius: 10px;
  padding: 10px 12px;
}

.modal-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .6px;
  color: var(--text-muted);
}

.modal-value {
  margin-top: 6px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.modal-links {
  margin-top: 14px;
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.modal-about {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid rgba(51,65,85,.65);
}

.modal-about-title {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .6px;
  color: var(--text-muted);
}

.modal-about-text {
  margin-top: 6px;
  font-size: 12px;
  line-height: 1.45;
  color: var(--text);
}

.modal-about-desc {
  color: var(--text-muted);
  margin-bottom: 8px;
}

.modal-about-text a {
  color: var(--text);
  text-decoration: underline;
  text-decoration-color: rgba(148,163,184,.55);
}

.modal-about-text a:hover {
  text-decoration-color: var(--accent);
}

.modal-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 10px;
  border-radius: 10px;
  border: 1px solid rgba(51,65,85,.85);
  background: rgba(39,53,73,.35);
  color: var(--text);
  text-decoration: none;
  font-size: 12px;
  transition: border-color .15s, box-shadow .15s, transform .05s;
}

.modal-link:hover {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(59,130,246,.12);
}

.modal-link:active { transform: translateY(1px); }

.modal-error {
  margin-top: 12px;
  font-size: 12px;
  color: #fecaca;
}

.modal-error.hidden { display: none; }

@media (max-width: 640px) {
  .modal-dialog { margin: 56px auto; }
  .modal-grid { grid-template-columns: 1fr; }
}

/* Empty state */
.empty-state {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* Responsive: tablet */
@media (max-width: 900px) {
  main { flex-direction: column; }
  #sidebar {
    width: 100%;
    height: 50vh;
    min-height: 0;
    border-bottom: 1px solid var(--border);
  }
  .sidebar-resizer { display: none; }
  #map { min-height: 50vh; }
}
