:root {
  --bg: #0f1420;
  --bg-card: #171d2e;
  --bg-input: #0f1420;
  --border: #2a3350;
  --text: #e6e9f2;
  --text-dim: #9aa4c0;
  --accent: #5b8def;
  --accent-hover: #4574d6;
  --danger: #e0546b;
  --danger-hover: #c73f56;
  --success: #4caf7d;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  min-height: 100vh;
}

a:not(.btn) { color: var(--accent); }
a:not(.btn):visited { color: var(--accent); }
a:not(.btn):hover { color: var(--accent-hover); }

nav.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  padding: 24px 0;
  display: flex;
  flex-direction: column;
}

nav.sidebar h1 {
  font-size: 1.1rem;
  padding: 0 20px 16px;
  margin: 0;
}

.sidebar-summary {
  padding: 0 20px 16px;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--border);
  font-size: 0.82rem;
}
.sidebar-summary .ss-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 2px 0;
  color: var(--text-dim);
}
.sidebar-summary .ss-row span:last-child {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

nav.sidebar a {
  display: block;
  padding: 12px 20px;
  color: var(--text-dim);
  text-decoration: none;
  font-size: 0.95rem;
  border-left: 3px solid transparent;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease, padding-left 0.15s ease;
}

nav.sidebar a:hover { color: var(--text); background: rgba(255,255,255,0.03); padding-left: 24px; }
nav.sidebar a.active { color: var(--text); border-left-color: var(--accent); background: rgba(91,141,239,0.08); }

.logout-top { padding: 0 20px 16px; margin-bottom: 8px; border-bottom: 1px solid var(--border); }

main {
  flex: 1;
  padding: 32px 40px;
  max-width: 1600px;
  width: 100%;
  min-width: 0;
}

h2 { margin-top: 0; }

.flash {
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 20px;
  font-size: 0.92rem;
  border: 1px solid transparent;
}
.flash-success { background: rgba(76,175,125,0.15); color: var(--success); border-color: rgba(76,175,125,0.3); }
.flash-error { background: rgba(224,84,107,0.15); color: var(--danger); border-color: rgba(224,84,107,0.3); }

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 24px;
  margin-bottom: 20px;
  max-width: 100%;
  overflow-x: auto;
}

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); font-size: 0.92rem; }
th { color: var(--text-dim); font-weight: 600; }
tr:last-child td { border-bottom: none; }

label { display: block; margin: 14px 0 6px; font-size: 0.88rem; color: var(--text-dim); }
input, select, textarea {
  width: 100%;
  padding: 9px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.95rem;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
textarea { min-height: 38px; line-height: 1.3; font-family: inherit; }

.row { display: flex; gap: 16px; }
.row > * { flex: 1; }

button, .btn {
  display: inline-block;
  padding: 8px 16px;
  border-radius: 6px;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 0.9rem;
  cursor: pointer;
  text-decoration: none;
}
button:hover, .btn:hover { background: var(--accent-hover); }
button.danger, .btn.danger { background: var(--danger); }
button.danger:hover, .btn.danger:hover { background: var(--danger-hover); }
button.ghost, .btn.ghost { background: transparent; border: 1px solid var(--border); color: var(--text); }
button.small, .btn.small { padding: 4px 10px; font-size: 0.82rem; }
button:disabled, .btn:disabled { opacity: 0.4; cursor: default; }
button:disabled:hover, .btn:disabled:hover { background: transparent; }

.pager { display: flex; align-items: center; gap: 14px; margin-top: 14px; }

.barwrap { background: var(--bg-input); border-radius: 4px; height: 8px; margin-top: 4px; overflow:hidden; }
.bar { background: var(--accent); height: 100%; }
.bar.warn { background: #d9a441; }
.bar.danger { background: var(--danger); }

/* Reusable "Top X" ranked-list widget (see static/top-widget.js) - usable
   on any page, not just Risorse in tempo reale. */
.top-widget-head { display:flex; justify-content:space-between; align-items:center; gap:12px; flex-wrap:wrap; margin-bottom:12px; }
.top-widget-controls { display:flex; align-items:center; gap:10px; }
.top-widget-limit { width:auto; padding:5px 8px; font-size:0.82rem; }
.seg { display:flex; border:1px solid var(--border); border-radius:6px; overflow:hidden; }
.seg-btn { background:transparent; color:var(--text-dim); border:none; border-radius:0; padding:5px 12px; font-size:0.8rem; cursor:pointer; }
.seg-btn:hover { background:var(--bg-input); }
.seg-btn.active { background:var(--accent); color:white; }
.top-widget-row { margin-bottom:10px; }
.top-widget-row:last-child { margin-bottom:0; }
.top-widget-label { display:flex; justify-content:space-between; font-size:0.85rem; margin-bottom:3px; }
.top-widget-empty { padding:8px 0; }
.pager.pager-top { margin-top: 0; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.actions form { display: inline; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 0.78rem; }
.badge.up { background: rgba(76,175,125,0.15); color: var(--success); }
.badge.down { background: rgba(224,84,107,0.15); color: var(--danger); }
.badge.warn { background: rgba(224,170,60,0.18); color: #e0aa3c; }
.badge.off { background: rgba(140,150,170,0.18); color: var(--text-dim, #9aa3b5); }

.hero { text-align: center; max-width: 640px; margin: 24px auto 40px; }
.hero-title { font-size: 2.4rem; margin: 0 0 8px; }
.hero-tagline { color: var(--text-dim); margin: 0 0 24px; }
.hero-actions { display: flex; gap: 12px; justify-content: center; }

.stat-grid { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 24px; }
.stat-tile {
  flex: 1;
  min-width: 140px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  transition: border-color 0.15s ease, transform 0.15s ease;
}
.stat-tile:hover { border-color: var(--accent); transform: translateY(-2px); }
.stat-tile .stat-value { font-size: 1.8rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat-tile .stat-label { color: var(--text-dim); font-size: 0.82rem; margin-top: 4px; }

.muted { color: var(--text-dim); font-size: 0.85rem; }
.hint { color: var(--text-dim); font-size: 0.82rem; margin-top: 4px; }

pre.log {
  background: #060910;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 16px;
  overflow-x: auto;
  font-size: 0.82rem;
  white-space: pre-wrap;
  max-height: 70vh;
  overflow-y: auto;
}

code { background: rgba(255,255,255,0.06); padding: 1px 6px; border-radius: 4px; font-size: 0.9em; }

.login-wrap { display: flex; align-items: center; justify-content: center; width: 100%; }
.login-card { width: 340px; }
.error { color: var(--danger); font-size: 0.88rem; margin-top: 10px; }

/* Mobile: sidebar becomes a horizontal, scrollable top bar instead of a
   fixed-width column, form rows stack instead of squeezing side by side,
   and spacing shrinks to fit small screens. Wide tables/forms inside
   .card already scroll horizontally on their own (see .card overflow-x)
   rather than breaking the page layout. */
@media (max-width: 768px) {
  body { flex-direction: column; min-height: auto; }

  nav.sidebar {
    width: 100%;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    padding: 8px 12px 4px;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  nav.sidebar h1 {
    width: 100%;
    padding: 0 0 8px;
    margin: 0 0 4px;
    border-bottom: none;
  }
  .sidebar-summary {
    width: 100%;
    padding: 0 0 8px;
    margin: 0 0 4px;
    display: flex;
    flex-wrap: wrap;
    gap: 2px 16px;
  }
  .sidebar-summary .ss-row { padding: 0; }
  nav.sidebar a {
    padding: 8px 12px;
    border-left: none;
    border-bottom: 3px solid transparent;
  }
  nav.sidebar a.active { border-left-color: transparent; border-bottom-color: var(--accent); }
  .logout-top { width: 100%; padding: 0 0 8px; margin: 0 0 4px; border-bottom: 1px solid var(--border); }

  main { padding: 16px; }

  .row { flex-direction: column; gap: 0; }
  .stat-grid { gap: 10px; }
  .stat-tile { min-width: calc(50% - 5px); }
  .hero { margin: 8px auto 28px; }
  .hero-title { font-size: 1.8rem; }
  .hero-actions { flex-direction: column; }

  .card { padding: 16px; }

  table { font-size: 0.85rem; }
  th, td { padding: 8px 6px; }
}

/* Pagine pubbliche (senza menu laterale): contenuto a tutta larghezza. */
body.public main { max-width: none; }
body.public .hero { max-width: none; }

/* Mappa della griglia (static/mappa_griglia.js): home pubblica, Sistema, dashboard utente. */
.gridview {
  position: relative;
  overflow: hidden;
  height: 420px;
  background: #1c4a66;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: default;
}
.gridview.zoomed { cursor: grab; }
.gridview.zoomed.dragging { cursor: grabbing; }
.gv-world { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
.gv-tile { position: absolute; width: 256px; height: 256px; display: block; pointer-events: none; }
.gv-overlay { position: absolute; inset: 0; pointer-events: none; }
.gv-region { position: absolute; border: 1px solid rgba(255,255,255,0.45); box-sizing: border-box; }
.gv-region.off { background: rgba(10,14,24,0.55); border-color: rgba(224,84,107,0.7); }
.gv-label {
  position: absolute; left: 4px; top: 4px; max-width: calc(100% - 8px);
  background: rgba(15,20,32,0.78); color: var(--text);
  padding: 3px 7px; border-radius: 6px; font-size: 0.78rem; line-height: 1.25;
  display: flex; flex-direction: column; overflow: hidden;
}
.gv-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gv-meta { color: var(--text-dim); font-size: 0.7rem; white-space: nowrap; }
.gv-region.off .gv-meta { color: var(--danger); }
.gv-region.small .gv-label { font-size: 0.66rem; padding: 2px 4px; }
.gv-region.small .gv-meta { display: none; }
.gv-controls { position: absolute; right: 10px; top: 10px; display: flex; flex-direction: column; gap: 6px; z-index: 2; }
.gv-btn {
  width: 36px; height: 36px; padding: 0; border-radius: 8px;
  background: rgba(23,29,46,0.88); color: var(--text); border: 1px solid var(--border);
  font-size: 1.25rem; line-height: 1; cursor: pointer;
}
.gv-btn:hover:not(:disabled) { background: var(--accent); border-color: var(--accent); }
.gv-btn:disabled { opacity: 0.4; cursor: default; }
.gv-msg { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--text-dim); }
.gridview-card h3 { margin-top: 0; }
.gridview-card { overflow: visible; }
@media (max-width: 768px) {
  .gv-btn { width: 42px; height: 42px; }
  .gv-label { font-size: 0.7rem; }
}
.gv-btn:disabled:hover { background: rgba(23,29,46,0.88); }

/* ---------------------------------------- pagine pubbliche: menu, info, aiuto */
.pubnav { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; margin: 0 0 18px; }
.pubnav a { padding: 6px 14px; border-radius: 999px; color: var(--text-dim); text-decoration: none; border: 1px solid transparent; }
.pubnav a:hover { color: var(--text); border-color: var(--border); }
.pubnav a.active { color: var(--text); background: var(--bg-card); border-color: var(--border); }
.info-two { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.info-two > .card { min-width: 0; }
.ichart svg { width: 100%; height: auto; display: block; }
.ich-bar { fill: var(--accent); }
.ich-bar:hover { fill: #7ea6f5; }
.ich-grid { stroke: var(--border); stroke-dasharray: 3 4; }
.ich-ax { fill: var(--text-dim); font-size: 11px; }
.itable td, .itable th { padding: 6px 8px; }
.itable td.num, .itable th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.itable tr.itot td { font-weight: 600; border-top: 2px solid var(--border); }
.help ol { line-height: 1.8; padding-left: 20px; }
.help details { border-top: 1px solid var(--border); padding: 10px 0; }
.help details summary { cursor: pointer; font-weight: 600; }
.help details p { margin: 8px 0 0; color: var(--text-dim); }
.copyline { margin: 6px 0 10px; }
.copyline code { font-size: 1rem; padding: 4px 10px; user-select: all; }
@media (max-width: 900px) { .info-two { grid-template-columns: 1fr; } }
