/*
 * Tema unico, scuro.
 *
 * La mappa occupa tutto: barra in alto, schede in basso, pannelli sopra. Nessun
 * layout a colonne, perché la pagina si guarda quasi sempre da un telefono con
 * una mano sola — e ciò che serve davvero (chi dov'è, fai suonare) deve stare
 * sotto il pollice.
 */

:root {
  --bg: #0b0f17;
  --surface: #121826;
  --surface-2: #1a2233;
  --line: #1f2937;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #2563eb;
  --ok: #22c55e;
  --warn: #f59e0b;
  --error: #ef4444;
  --radius: 16px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior: none;
}

#map { position: fixed; inset: 0; background: #0b0f17; }

/* Le tessere chiare stonerebbero con l'interfaccia scura: si smorzano un po'. */
.leaflet-tile { filter: brightness(0.82) saturate(0.85) contrast(1.05); }
.leaflet-container { background: #0b0f17; font: inherit; }
.leaflet-control-attribution { background: rgba(11, 15, 23, .7); color: var(--muted); }
.leaflet-control-attribution a { color: var(--accent); }
.leaflet-bar a { background: var(--surface); color: var(--text); border-color: var(--line); }

/* ── barra in alto ── */

.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 500;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 12px 10px;
  background: linear-gradient(180deg, rgba(11, 15, 23, .96), rgba(11, 15, 23, 0));
}

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: .2px; }
.brand .pin { font-size: 20px; }
.top-actions { display: flex; align-items: center; gap: 6px; }

.icon-btn {
  border: 1px solid var(--line);
  background: rgba(18, 24, 38, .92);
  color: var(--text);
  width: 40px; height: 40px;
  border-radius: 12px;
  font-size: 17px;
  cursor: pointer;
}
.icon-btn:hover { border-color: var(--accent); }
.icon-btn.active { border-color: var(--accent); background: rgba(56, 189, 248, .18); }

.avatar-btn {
  border: 1px solid var(--line); background: var(--surface);
  width: 40px; height: 40px; border-radius: 50%; padding: 0; overflow: hidden; cursor: pointer;
}
.avatar-btn img { width: 100%; height: 100%; display: block; }

/* ── avvisi ── */

.banner {
  position: fixed;
  top: calc(64px + env(safe-area-inset-top, 0px));
  left: 12px; right: 12px;
  z-index: 700;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  font-size: 14px;
}
.banner.ok { border-color: rgba(34, 197, 94, .5); }
.banner.warn { border-color: rgba(245, 158, 11, .5); }
.banner.error { border-color: rgba(239, 68, 68, .6); }

/* ── segnaposto ── */

.pin-person {
  width: 46px; height: 46px; border-radius: 50%;
  border: 3px solid var(--accent);
  background: var(--surface);
  box-shadow: 0 6px 18px rgba(0, 0, 0, .5);
  overflow: hidden; position: relative;
}
.pin-person img { width: 100%; height: 100%; display: block; }
.pin-person.stale { border-color: #64748b; filter: grayscale(.6); }
.pin-person.selected { border-color: #fbbf24; transform: scale(1.08); }
.pin-battery { position: absolute; right: -2px; bottom: -2px; font-size: 14px; }

.pin-place {
  width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--surface); border: 2px solid var(--line); font-size: 15px;
}

.accuracy { stroke: #38bdf8; stroke-opacity: .35; fill: #38bdf8; fill-opacity: .10; stroke-width: 1; }
.place-area { stroke: #22c55e; stroke-opacity: .5; fill: #22c55e; fill-opacity: .08; stroke-dasharray: 4 4; }
.trail { stroke: #fbbf24; stroke-width: 4; stroke-opacity: .85; fill: none; }
.trail-start { stroke: #fbbf24; fill: #0b0f17; }

/* ── schede in basso ── */

.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 500;
  background: linear-gradient(180deg, rgba(11, 15, 23, 0), rgba(11, 15, 23, .96) 22%);
  padding: 4px 10px calc(10px + var(--safe-bottom));
  max-height: 42vh; overflow: hidden;
}
.sheet.expanded { max-height: 76vh; overflow-y: auto; }
.sheet-handle {
  width: 44px; height: 4px; border-radius: 999px;
  background: #334155; margin: 6px auto 10px; cursor: pointer;
}

.cards { display: flex; flex-direction: column; gap: 8px; }
.sheet:not(.expanded) .cards { max-height: 32vh; overflow-y: auto; }

.card {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: rgba(18, 24, 38, .94);
  color: inherit; text-align: left; font: inherit; cursor: pointer;
}
.card.selected { border-color: #fbbf24; }
.card-avatar { width: 42px; height: 42px; border-radius: 50%; flex: 0 0 auto; }
.card-main { flex: 1; min-width: 0; }
.card-name { font-weight: 600; }
.card-detail { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-side { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }

.dot { width: 9px; height: 9px; border-radius: 50%; background: #475569; display: inline-block; }
.dot.on { background: var(--ok); box-shadow: 0 0 8px rgba(34, 197, 94, .8); }
.dot.off { background: #475569; }

/* ── pannelli ── */

.scrim { position: fixed; inset: 0; background: rgba(2, 6, 12, .6); z-index: 800; }

.panel {
  position: fixed;
  z-index: 900;
  left: 0; right: 0; bottom: 0;
  max-height: 88vh; overflow-y: auto;
  background: var(--surface);
  border-top: 1px solid var(--line);
  border-radius: 20px 20px 0 0;
  padding: 16px 16px calc(24px + var(--safe-bottom));
}
@media (min-width: 760px) {
  .panel { left: auto; top: 0; width: 420px; max-height: 100vh; border-radius: 0; border-left: 1px solid var(--line); }
}

.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.panel-head h2 { margin: 0; font-size: 19px; }
.panel-section { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--line); }
.panel-section h3 { margin: 0 0 10px; font-size: 15px; color: var(--muted); font-weight: 600; }

.muted { color: var(--muted); font-size: 14px; line-height: 1.5; }
.muted.small { font-size: 12px; }
.empty { color: var(--muted); font-size: 14px; text-align: center; padding: 18px 6px; }

.grid-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 14px; }
.action {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 14px 6px; border-radius: 14px;
  border: 1px solid var(--line); background: var(--surface-2); color: inherit;
  font-size: 22px; cursor: pointer;
}
.action span { font-size: 11px; color: var(--muted); text-align: center; line-height: 1.3; }
.action:hover { border-color: var(--accent); }

.list { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.list-row {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px; border-radius: 14px;
  border: 1px solid var(--line); background: var(--surface-2);
}
.list-icon { font-size: 20px; }
.list-avatar { width: 36px; height: 36px; border-radius: 50%; }
.list-main { flex: 1; min-width: 0; }
.list-title { font-weight: 600; font-size: 15px; }
.list-sub { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tag { font-size: 10px; text-transform: uppercase; letter-spacing: 1px; color: var(--accent); border: 1px solid var(--line); border-radius: 6px; padding: 1px 5px; }

.field { display: block; margin-bottom: 12px; }
.field span { display: block; color: var(--muted); font-size: 12.5px; margin-bottom: 6px; }
.field input {
  width: 100%; padding: 11px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit;
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.check { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13.5px; margin-bottom: 12px; }
.row { display: flex; gap: 8px; }

.btn {
  padding: 11px 16px; border-radius: 12px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface-2); color: var(--text);
  font: inherit; font-weight: 600; text-decoration: none; display: inline-block; text-align: center;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: linear-gradient(135deg, var(--accent-2), var(--accent)); border-color: transparent; color: #04121f; }
.btn.danger { border-color: rgba(239, 68, 68, .5); color: #fecaca; }
.btn.wide { display: block; width: 100%; margin-bottom: 10px; }

.steps { color: var(--muted); font-size: 14px; line-height: 1.7; padding-left: 20px; }
.code { display: flex; gap: 8px; justify-content: center; margin: 18px 0 10px; }
.code span {
  width: 46px; height: 60px; display: grid; place-items: center;
  border-radius: 12px; border: 1px solid var(--line); background: var(--bg);
  font-size: 30px; font-weight: 800; color: var(--accent);
  font-family: "SF Mono", Consolas, monospace;
}

/* ── accesso ── */

.login-page { display: grid; place-items: center; min-height: 100dvh; padding: 24px; }
.login-card {
  width: 100%; max-width: 380px; text-align: center;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 20px; padding: 34px 26px 28px;
}
.login-pin { font-size: 48px; }
.login-card h1 { margin: 8px 0 4px; font-size: 26px; }
.login-sub { margin: 0 0 22px; color: var(--muted); font-size: 14px; }
.login-button {
  display: block; padding: 14px 20px; border-radius: 12px;
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
  color: #04121f; font-weight: 700; text-decoration: none;
}
.login-note { margin: 18px 0 0; color: #64748b; font-size: 12px; line-height: 1.6; }
.login-error {
  margin: 0 0 16px; padding: 10px 12px; border-radius: 12px;
  border: 1px solid rgba(239, 68, 68, .5); background: rgba(239, 68, 68, .12);
  color: #fecaca; font-size: 13px;
}

/* La pagina di download è la stessa scheda dell'accesso, ma con dentro
   istruzioni da leggere: più larga, testo allineato a sinistra. */
.download-card { max-width: 460px; }
.download-card .login-button { margin-bottom: 6px; }
.download-link { margin-top: 14px; }
.download-steps { text-align: left; margin-top: 22px; }
.download-steps li { margin-bottom: 8px; }

/* Diagnostica: chiusa di default, una riga per errore, traccia a richiesta. */
.log { border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); padding: 8px 12px; margin-bottom: 8px; }
.log summary { display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: 13.5px; }
.log-message { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.log-when { color: var(--muted); font-size: 11.5px; white-space: nowrap; }
.log-detail { color: var(--muted); font-size: 12px; margin-top: 8px; line-height: 1.6; }
.log-detail pre {
  margin: 8px 0 0; padding: 10px; border-radius: 10px; background: var(--bg);
  overflow-x: auto; font-size: 11px; line-height: 1.5; color: #cbd5e1;
}
