/* Palantir-style: dark, dense, technical */

:root {
  --bg-deep: #0b0e11;
  --bg-panel: #13171c;
  --bg-elevated: #1a1f26;
  --border: #2d333b;
  --border-subtle: #21262d;
  --text: #e6edf3;
  --text-muted: #8b949e;
  --text-dim: #6e7681;
  --accent: #58a6ff;
  --accent-cyan: #39c5cf;
  --accent-green: #3fb950;
  --danger: #f85149;
  --font-mono: "JetBrains Mono", "SF Mono", Consolas, monospace;
  --font-ui: "Inter", system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--text);
  background: var(--bg-deep);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: grid;
  grid-template-columns: 280px 1fr;
  grid-template-rows: 48px 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ——— Top bar ——— */
.top-bar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  font-family: var(--font-mono);
}

.top-bar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.back-to-site {
  font-size: 13px;
  color: var(--text-dim);
  text-decoration: none;
  margin-right: 8px;
}
.back-to-site:hover {
  color: var(--text);
  text-decoration: underline;
}

.logo-mark {
  color: var(--accent-cyan);
  font-size: 14px;
}

.app-title {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.app-tag {
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 400;
}

.top-bar-right .data-source {
  font-size: 11px;
  color: var(--text-dim);
}

/* ——— Side panel ——— */
.side-panel {
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}

.panel-section {
  padding: 16px;
  border-bottom: 1px solid var(--border-subtle);
}

.panel-section-fill {
  flex: 1;
  min-height: 0;
}

.panel-heading {
  margin: 0 0 10px;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.map-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 8px;
}

.map-tab {
  flex: 1;
  padding: 8px 12px;
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.map-tab:hover {
  color: var(--text);
  border-color: var(--text-dim);
}

.map-tab.active {
  color: var(--accent-cyan);
  background: rgba(57, 197, 207, 0.08);
  border-color: var(--accent-cyan);
}

.panel-hint {
  margin: 0;
  font-size: 11px;
  color: var(--text-dim);
}

/* Legend */
.legend-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  font-family: var(--font-mono);
}

.legend-color {
  width: 12px;
  height: 12px;
  flex-shrink: 0;
  border-radius: 2px;
  border: 1px solid rgba(255, 255, 255, 0.12);
}

.legend-label {
  color: var(--text-muted);
}

/* Statistiche */
.stats-section {
  flex-shrink: 0;
}

.stats-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.stats-controls label {
  font-size: 11px;
  color: var(--text-muted);
}

.stats-select {
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 4px 8px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
}

.stats-table-wrap {
  max-height: 220px;
  min-height: 60px;
  overflow-y: auto;
  border: 1px solid var(--border-subtle);
  border-radius: 4px;
}

.stats-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: 11px;
  font-family: var(--font-mono);
}

.stats-table th,
.stats-table td {
  padding: 4px 8px;
  text-align: left;
  border-bottom: 1px solid var(--border-subtle);
}

.stats-table th {
  position: sticky;
  top: 0;
  background: var(--bg-elevated);
  color: var(--text-muted);
  font-weight: 600;
}

.stats-table th.stats-th-sortable {
  cursor: pointer;
  user-select: none;
}
.stats-table th.stats-th-sortable:hover {
  color: var(--text);
}

.stats-table th:first-child,
.stats-table td:first-child {
  width: 28px;
  min-width: 28px;
  color: var(--text-dim);
}

/* Zona: default minimo così €/m² si vede bene; trascinando il bordo si allarga per leggere il nome intero */
.stats-table th.stats-th-zona,
.stats-table td:nth-child(2) {
  min-width: 44px;
  width: var(--stats-zona-width, 44px);
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stats-th-zona {
  position: relative;
  padding-right: 14px;
}

.stats-col-resize {
  position: absolute;
  top: 0;
  right: 0;
  width: 8px;
  height: 100%;
  cursor: col-resize;
}
.stats-col-resize:hover {
  background: rgba(57, 197, 207, 0.15);
}

.stats-table th:last-child,
.stats-table td:last-child {
  width: 64px;
  min-width: 64px;
  text-align: right;
  white-space: nowrap;
}

.stats-table .stats-row {
  cursor: pointer;
}

.stats-table .stats-row:hover {
  background: rgba(57, 197, 207, 0.08);
}

/* Detail panel */
.detail-placeholder {
  min-height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 12px;
}

.detail-muted {
  font-style: italic;
}

.detail-content {
  font-family: var(--font-mono);
  font-size: 12px;
}

.detail-content h3 {
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 600;
  color: var(--accent-cyan);
}

.detail-content .detail-pinned {
  margin: 0 0 10px;
  font-size: 11px;
  color: var(--accent-cyan);
  font-style: italic;
}

.detail-content .row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 4px 0;
  border-bottom: 1px solid var(--border-subtle);
}

.detail-content .row:last-child {
  border-bottom: none;
}

.detail-content .detail-list {
  margin: 0.5rem 0 0 1rem;
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.5;
}


.detail-content .label {
  color: var(--text-muted);
}

.detail-content .value {
  color: var(--text);
}

.detail-content .value.var-up {
  color: var(--accent-green);
}

.detail-content .value.var-down {
  color: #f87171;
}

/* ——— Workspace / Map ——— */
.workspace {
  position: relative;
  background: var(--bg-deep);
  min-height: 0;
  height: 100%;
  min-height: 400px;
}

.map-wrapper {
  position: absolute;
  inset: 0;
  min-height: 400px;
}

.map-main,
#map {
  width: 100%;
  height: 100%;
  min-height: 400px;
  background: #1a1a1a;
}

.map-overlay {
  position: absolute;
  bottom: 8px;
  left: 8px;
  padding: 4px 8px;
  font-size: 10px;
  font-family: var(--font-mono);
  color: var(--text-dim);
  background: rgba(0, 0, 0, 0.6);
  border-radius: 4px;
}

.map-attribution {
  left: auto;
  right: 8px;
}

/* Leaflet overrides for dark theme */
.leaflet-container {
  background: #1a1a1a;
  font-family: var(--font-ui);
}

.leaflet-popup-content-wrapper {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
}

.leaflet-popup-tip {
  background: var(--bg-elevated);
}

.leaflet-popup-content {
  margin: 12px 16px;
  font-size: 12px;
}

.popup-content h3 {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--accent-cyan);
}

.popup-content p {
  margin: 4px 0;
  color: var(--text-muted);
}

.popup-content strong {
  color: var(--text);
}

.popup-content .popup-codice {
  margin-bottom: 6px;
  font-size: 11px;
}
.popup-content .popup-muted {
  font-size: 10px;
  color: var(--text-dim);
  margin-top: 0;
}

.popup-content .var-up {
  color: var(--accent-green);
}

.popup-content .var-down {
  color: #f87171;
}

@media (max-width: 900px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: 48px auto 1fr;
  }

  .side-panel {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto auto minmax(0, 140px);
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 45vh;
    overflow-y: auto;
  }

  .side-panel .panel-section:first-of-type {
    grid-column: 1 / -1;
  }

  .side-panel .panel-section:nth-of-type(2) {
    grid-row: 2;
    grid-column: 1;
  }

  .side-panel .panel-section:nth-of-type(3) {
    grid-row: 3;
    grid-column: 1 / -1;
  }

  .side-panel .panel-section:nth-of-type(4) {
    grid-row: 2;
    grid-column: 2;
    min-height: 0;
  }

  .side-panel .panel-section-fill {
    flex: none;
    min-height: 0;
    overflow-y: auto;
  }

  .side-panel .stats-table-wrap {
    max-height: 200px;
    -webkit-overflow-scrolling: touch;
  }
}
