:root {
  --bg: #061826;
  --panel: #0a2236;
  --ink: #e8f1f7;
  --muted: #8aa6b8;
  --accent: #1e9fd0;
  --accent2: #43d6a1;
  --warn: #ffb547;
  --danger: #e85a5a;
  --border: rgba(255, 255, 255, 0.08);
  --radius: 10px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--ink); min-height: 100vh; }

.topbar {
  display: flex; align-items: center; gap: 1.5rem;
  padding: 0.75rem 1.25rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: baseline; gap: 0.4rem; }
.brand .logo { font-size: 1.4rem; color: var(--accent); }
.brand strong { font-size: 1.1rem; letter-spacing: 0.02em; }
.brand .sub { color: var(--muted); font-size: 0.85rem; }

.nav { display: flex; gap: 1rem; margin-left: auto; }
.nav a {
  color: var(--muted); text-decoration: none; font-size: 0.9rem;
  padding: 0.35rem 0.6rem; border-radius: 6px;
  transition: background 0.15s, color 0.15s;
}
.nav a:hover, .nav a:focus { background: rgba(30, 159, 208, 0.12); color: var(--ink); outline: none; }

.status {
  margin-left: 1rem; font-size: 0.8rem; padding: 0.25rem 0.6rem;
  border-radius: 999px; background: rgba(255,255,255,0.06); color: var(--muted);
}
.status.ok { color: var(--accent2); background: rgba(67, 214, 161, 0.12); }
.status.err { color: var(--danger); background: rgba(232, 90, 90, 0.14); }

.user-pill {
  display: inline-flex; align-items: center; gap: 0.5rem;
  margin-left: 0.5rem; font-size: 0.8rem;
  padding: 0.3rem 0.5rem 0.3rem 0.75rem; border-radius: 999px;
  background: rgba(30, 159, 208, 0.12); color: var(--accent);
  border: 1px solid rgba(30, 159, 208, 0.25);
}
.user-pill #user-name { font-weight: 600; }
.user-pill a {
  background: rgba(232, 90, 90, 0.15); color: var(--danger);
  border: 1px solid rgba(232, 90, 90, 0.35);
  text-decoration: none;
  padding: 0.2rem 0.55rem; border-radius: 999px;
  font-size: 0.75rem; font-weight: 500;
  cursor: pointer; transition: background 0.15s, color 0.15s;
}
.user-pill a:hover, .user-pill a:focus {
  background: var(--danger); color: #fff; outline: none;
}

main { max-width: 1280px; margin: 0 auto; padding: 1.5rem 1.25rem 4rem; }
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}
h1 { margin: 0 0 0.5rem; font-size: 1.6rem; }
h2 { margin: 0 0 1rem; font-size: 1.2rem; color: var(--accent); }
.muted { color: var(--muted); font-size: 0.85rem; }

.archmap-wrap { position: relative; }
.archmap {
  width: 100%; height: 60vh; min-height: 420px;
  border-radius: 8px; overflow: hidden;
  background: #062032;
  border: 1px solid rgba(255,255,255,0.05);
}

/* Search overlay top-center on the map */
.map-search {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  z-index: 20; width: min(440px, calc(100% - 24px));
  pointer-events: auto;
}
.map-search-input-wrap {
  position: relative; display: flex; align-items: center;
  background: rgba(6, 24, 38, 0.96);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 999px;
  box-shadow: 0 6px 22px rgba(0,0,0,0.55);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.map-search-input-wrap:focus-within {
  border-color: var(--accent);
  box-shadow: 0 6px 22px rgba(0,0,0,0.55), 0 0 0 3px rgba(30,159,208,0.2);
}
.map-search-icon {
  font-size: 1rem; padding: 0 0.2rem 0 0.85rem; user-select: none;
}
.map-search input[type=search] {
  flex: 1; min-width: 0;
  background: transparent; border: none; outline: none;
  color: var(--ink);
  padding: 0.65rem 0.6rem;
  font-size: 0.92rem; font-family: inherit;
}
.map-search input[type=search]::placeholder { color: var(--muted); }
.map-search input[type=search]::-webkit-search-cancel-button { display: none; }
.map-search-clear {
  display: none; align-items: center; justify-content: center;
  width: 22px; height: 22px; margin-right: 0.4rem;
  background: rgba(255,255,255,0.08); border: none; border-radius: 50%;
  color: var(--muted); font-size: 1rem; cursor: pointer; line-height: 1;
  flex-shrink: 0;
}
.map-search-clear:hover { background: rgba(255,255,255,0.15); color: var(--ink); }

.map-search-results {
  display: none;
  margin-top: 6px;
  background: rgba(6, 24, 38, 0.98);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 14px;
  max-height: 340px; overflow-y: auto;
  box-shadow: 0 10px 28px rgba(0,0,0,0.6);
}
.map-search-results.visible { display: block; }
.map-search-result {
  display: flex; align-items: center; gap: 0.7rem;
  width: 100%; padding: 0.65rem 0.85rem;
  background: transparent; border: none;
  border-bottom: 1px solid rgba(255,255,255,0.06);
  color: var(--ink); cursor: pointer; text-align: left;
  font-family: inherit; font-size: 0.88rem;
}
.map-search-result:last-child { border-bottom: none; }
.map-search-result:hover, .map-search-result:focus {
  background: rgba(30,159,208,0.14); outline: none;
}
.map-search-result .result-icon { flex-shrink: 0; font-size: 1.1rem; }
.map-search-result .result-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 0.1rem; overflow: hidden; }
.map-search-result .result-text strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.map-search-result .result-text .muted { font-size: 0.74rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.map-search-result .result-type {
  flex-shrink: 0; font-size: 0.66rem; color: var(--muted);
  background: rgba(255,255,255,0.05); padding: 0.15rem 0.5rem; border-radius: 999px;
  text-transform: lowercase;
}
.map-search-empty {
  padding: 0.85rem; color: var(--muted); font-size: 0.86rem; text-align: center;
}

@media (max-width: 640px) {
  .map-search { top: 8px; width: calc(100% - 16px); }
  .map-search input[type=search] { font-size: 0.85rem; padding: 0.55rem 0.45rem; }
}
/* MapLibre attribution: blend into dark theme */
.maplibregl-ctrl-attrib {
  background: rgba(6, 24, 38, 0.7) !important;
  color: var(--muted) !important;
}
.maplibregl-ctrl-attrib a { color: var(--muted) !important; }
.maplibregl-ctrl-scale {
  background: rgba(6, 24, 38, 0.7);
  color: var(--muted);
  border-color: var(--border);
}

/* MapLibre popups: dark theme, leggibile */
.maplibregl-popup-content {
  background: rgba(6, 24, 38, 0.97) !important;
  color: var(--ink) !important;
  border: 1px solid rgba(30, 159, 208, 0.45);
  border-radius: 12px;
  padding: 0.9rem 1rem 0.85rem !important;
  max-width: 320px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.55);
  font-family: inherit;
}
.maplibregl-popup-anchor-top .maplibregl-popup-tip,
.maplibregl-popup-anchor-top-left .maplibregl-popup-tip,
.maplibregl-popup-anchor-top-right .maplibregl-popup-tip {
  border-bottom-color: rgba(6, 24, 38, 0.97) !important;
}
.maplibregl-popup-anchor-bottom .maplibregl-popup-tip,
.maplibregl-popup-anchor-bottom-left .maplibregl-popup-tip,
.maplibregl-popup-anchor-bottom-right .maplibregl-popup-tip {
  border-top-color: rgba(6, 24, 38, 0.97) !important;
}
.maplibregl-popup-anchor-left .maplibregl-popup-tip {
  border-right-color: rgba(6, 24, 38, 0.97) !important;
}
.maplibregl-popup-anchor-right .maplibregl-popup-tip {
  border-left-color: rgba(6, 24, 38, 0.97) !important;
}
.maplibregl-popup-close-button {
  color: var(--muted) !important;
  background: transparent !important;
  font-size: 1.3rem !important;
  padding: 0 0.55rem !important;
  top: 0.2rem !important;
  right: 0.2rem !important;
}
.maplibregl-popup-close-button:hover {
  color: var(--ink) !important;
}

.popup-card { font-family: inherit; line-height: 1.4; }
.popup-title {
  font-size: 1.05rem; font-weight: 700; color: var(--accent);
  margin: 0 1.6rem 0.65rem 0; letter-spacing: 0.01em;
}
.popup-title .popup-rank {
  color: var(--muted); font-weight: 400; font-size: 0.8rem; margin-left: 0.3rem;
}
.popup-data {
  display: grid; grid-template-columns: auto 1fr;
  gap: 0.25rem 0.85rem; margin: 0 0 0.7rem; font-size: 0.85rem;
}
.popup-data dt { color: var(--muted); font-weight: 400; }
.popup-data dd { margin: 0; color: var(--ink); font-weight: 500; }
.popup-data dd code {
  color: var(--accent); font-family: ui-monospace, Menlo, monospace;
  font-size: 0.8rem;
}
.popup-badge {
  display: inline-block; padding: 0.1rem 0.55rem; border-radius: 999px;
  font-weight: 600; font-size: 0.78rem;
}
.popup-badge.low  { color: #43d6a1; background: rgba(67,214,161,0.18); }
.popup-badge.mid  { color: #ffb547; background: rgba(255,181,71,0.18); }
.popup-badge.high { color: #e85a5a; background: rgba(232,90,90,0.22); }
.popup-foot {
  font-size: 0.7rem; color: var(--muted); font-style: italic; line-height: 1.4;
  border-top: 1px solid rgba(255,255,255,0.08); padding-top: 0.55rem; margin-top: 0.2rem;
}

/* Map layer control panel */
.archwater-layers {
  background: rgba(6, 24, 38, 0.92) !important;
  padding: 0.5rem 0.75rem;
  font-size: 0.8rem;
  color: var(--ink);
  min-width: 220px;
  max-width: 280px;
  border-radius: 8px !important;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4) !important;
}
.archwater-layers .layer-group { padding: 0.35rem 0; }
.archwater-layers .layer-group + .layer-group { border-top: 1px solid var(--border); }
.archwater-layers .layer-title {
  color: var(--muted); font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.08em;
  margin-bottom: 0.3rem;
}
.archwater-layers label {
  display: flex; align-items: center; gap: 0.45rem;
  padding: 0.2rem 0; cursor: pointer; line-height: 1.3;
}
.archwater-layers label.disabled { color: var(--muted); cursor: not-allowed; }
.archwater-layers .muted { color: var(--muted); font-size: 0.7rem; }
.archwater-layers input[type=radio],
.archwater-layers input[type=checkbox] { accent-color: var(--accent); }
.archwater-layers .layer-roadmap label { opacity: 0.65; }

/* (Old .archwater-search styles deprecati — sostituiti da .map-search overlay top-center sopra) */

kbd {
  display: inline-block; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.7rem; padding: 1px 5px; margin-right: 4px;
  background: rgba(255,255,255,0.06); border: 1px solid var(--border);
  border-radius: 4px; color: var(--muted);
}
.nav a:hover kbd, .nav a:focus kbd { color: var(--accent); border-color: rgba(30,159,208,0.4); }

.kpi-grid {
  display: grid; gap: 1rem; margin: 1.25rem 0 0.5rem;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
}
.kpi {
  display: flex; flex-direction: column; gap: 0.3rem;
  background: rgba(255,255,255,0.03); border: 1px solid var(--border);
  border-radius: 8px; padding: 0.9rem 1rem;
}
.kpi-label { color: var(--muted); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em; }
.kpi-value { color: var(--accent); font-size: 1.6rem; font-weight: 600; font-variant-numeric: tabular-nums; }

.data-table { padding: 0.6rem 0.9rem; background: rgba(0,0,0,0.18); border-radius: 6px; }
.bullet-list { padding-left: 1.25rem; line-height: 1.7; color: #cfdbe3; }
.bullet-list li { margin-bottom: 0.25rem; }

table.endpoints { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
table.endpoints th, table.endpoints td {
  text-align: left; padding: 0.5rem 0.6rem;
  border-bottom: 1px solid var(--border);
}
table.endpoints th { color: var(--muted); font-weight: 500; text-transform: uppercase; letter-spacing: 0.04em; font-size: 0.72rem; }
table.endpoints code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--accent); }
table.endpoints .badge { display: inline-block; font-size: 0.7rem; padding: 1px 8px; border-radius: 999px; }
table.endpoints .badge.pub { background: rgba(67,214,161,0.14); color: var(--accent2); }
table.endpoints .badge.jwt { background: rgba(30,159,208,0.15); color: var(--accent); }
table.endpoints .badge.svc { background: rgba(255,181,71,0.15); color: var(--warn); }
table.endpoints .badge.ok  { background: rgba(67,214,161,0.14); color: var(--accent2); }
table.endpoints .badge.todo { background: rgba(255,255,255,0.06); color: var(--muted); }

.bigbang-chart {
  width: 100%; height: 320px;
  background: rgba(0,0,0,0.18); border-radius: 8px;
  margin-top: 1.25rem; padding: 0.5rem;
  overflow: hidden;
}
.bigbang-chart svg { display: block; width: 100%; height: 100%; }
.bigbang-chart .axis text { fill: var(--muted); font-size: 10px; }
.bigbang-chart .axis line, .bigbang-chart .axis path { stroke: rgba(255,255,255,0.12); }
.bigbang-chart .grid line { stroke: rgba(255,255,255,0.06); stroke-dasharray: 2 3; }
.bigbang-chart .bar-normal { fill: rgba(30,159,208,0.18); }
.bigbang-chart .bar-pos { fill: #43d6a1; }
.bigbang-chart .bar-neg { fill: #e85a5a; }
.bigbang-chart .trend-line { stroke: #ffb547; stroke-width: 1.8; fill: none; stroke-dasharray: 4 3; }
.bigbang-chart .normal-line { stroke: rgba(30,159,208,0.6); stroke-width: 1; stroke-dasharray: 2 2; fill: none; }
.bigbang-chart .label { fill: var(--muted); font-size: 10px; }
.bigbang-chart .latest-marker { fill: var(--accent); }
.bigbang-chart .latest-marker.dry { fill: #e85a5a; }
.bigbang-chart .latest-marker.wet { fill: #43d6a1; }

/* GWW detail modal */
.gww-modal {
  display: none;
  position: fixed; inset: 0;
  z-index: 1000;
  align-items: center; justify-content: center;
  padding: 1.5rem;
}
.gww-modal[aria-hidden="false"] { display: flex; }
.gww-modal-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(4px);
}
.gww-modal-card {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 100%;
  max-width: 1080px;
  max-height: calc(100vh - 3rem);
  display: flex; flex-direction: column;
  box-shadow: 0 24px 80px rgba(0,0,0,0.7);
  overflow: hidden;
}
.gww-modal-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  padding: 1.25rem 1.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
}
.gww-modal-head h2 { margin: 0; font-size: 1.25rem; color: var(--accent); }
.gww-modal-head .muted { font-size: 0.85rem; margin-top: 0.2rem; }
.gww-modal-close {
  background: rgba(255,255,255,0.06); border: 1px solid var(--border);
  color: var(--muted); width: 32px; height: 32px;
  border-radius: 8px; cursor: pointer; font-size: 1.2rem; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s, color 0.15s;
}
.gww-modal-close:hover { background: var(--danger); color: #fff; }
.gww-modal-kpis {
  display: grid; gap: 0.75rem;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  padding: 1rem 1.5rem;
}
.gww-modal-kpis .kpi { padding: 0.7rem 0.85rem; }
.gww-modal-kpis .kpi-value { font-size: 1.3rem; }
.gww-modal-kpis .kpi-sub { color: var(--muted); font-size: 0.72rem; margin-top: 0.15rem; }
.gww-modal-controls {
  display: flex; align-items: center; gap: 0.6rem;
  padding: 0.25rem 1.5rem 0.75rem; flex-wrap: wrap;
}
.gww-range { display: inline-flex; gap: 0.3rem; background: rgba(0,0,0,0.2); padding: 0.25rem; border-radius: 999px; }
.gww-range button {
  background: transparent; border: none; color: var(--muted);
  padding: 0.3rem 0.85rem; border-radius: 999px;
  font-size: 0.82rem; cursor: pointer; font-family: inherit;
  transition: background 0.15s, color 0.15s;
}
.gww-range button:hover { color: var(--ink); }
.gww-range button.active {
  background: var(--accent); color: #fff;
}
.gww-modal-chart {
  flex: 1; min-height: 380px;
  margin: 0 1.5rem; padding: 0.75rem;
  background: rgba(0,0,0,0.22); border-radius: 8px; overflow: hidden;
}
.gww-modal-chart svg { width: 100%; height: 100%; display: block; }
.gww-modal-chart .axis text { fill: var(--muted); font-size: 11px; }
.gww-modal-chart .axis line { stroke: rgba(255,255,255,0.12); }
.gww-modal-chart .grid line { stroke: rgba(255,255,255,0.06); }
.gww-modal-chart .ts-line { stroke: #1e9fd0; stroke-width: 1.7; fill: none; }
.gww-modal-chart .ts-area { fill: rgba(30,159,208,0.15); }
.gww-modal-chart .latest-dot { fill: var(--accent2); }
.gww-modal-chart .latest-label { fill: var(--accent2); font-size: 11px; font-weight: 600; }
.gww-modal-chart .mean-line { stroke: rgba(255,181,71,0.7); stroke-dasharray: 4 3; stroke-width: 1.2; fill: none; }
.gww-modal-chart .hover-line { stroke: rgba(255,255,255,0.3); stroke-dasharray: 2 2; }
.gww-modal-chart .hover-dot { fill: var(--ink); }
.gww-modal-chart .hover-tip {
  fill: rgba(6,24,38,0.96); stroke: var(--accent); stroke-width: 1;
}
.gww-modal-chart .hover-tip-text { fill: var(--ink); font-size: 11px; font-weight: 500; }
.gww-modal-foot {
  padding: 0.75rem 1.5rem 1.25rem;
  border-top: 1px solid var(--border);
}

.panel.flash { animation: flashPanel 1.2s ease-out; }
@keyframes flashPanel {
  0%   { box-shadow: 0 0 0 0 rgba(30,159,208,0); }
  20%  { box-shadow: 0 0 0 3px rgba(30,159,208,0.55); }
  100% { box-shadow: 0 0 0 0 rgba(30,159,208,0); }
}

footer {
  text-align: center; color: var(--muted); padding: 1rem; border-top: 1px solid var(--border);
}
footer a { color: var(--muted); }

@media (max-width: 720px) {
  .nav { flex-wrap: wrap; gap: 0.5rem; }
  .archmap { height: 50vh; }
}
