/* ==========================================================================
   tracktags - stile webapp (mobile-first, responsive, dark)
   ========================================================================== */

:root {
  --bg: #0f172a;
  --bg-elev: #16223a;
  --panel: #1e293b;
  --text: #e8eef7;
  --muted: #9fb0c6;
  --accent: #38bdf8;
  --accent-ink: #04263a;
  --border: #33415c;
  --ok: #22c55e;
  --error: #f87171;
  --radius: 12px;
  --gap: 1rem;
  --topbar-h: 56px;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.28);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 16px;               /* evita lo zoom automatico su iOS */
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  min-height: 100svh;
}

a { color: var(--accent); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  background: var(--accent);
  color: var(--accent-ink);
  padding: 0.6rem 0.9rem;
  border-radius: 0 0 var(--radius) 0;
  z-index: 1200;
}
.skip-link:focus { left: 0; }

/* ------------------------------------------------------------------ Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 1100;                 /* sopra i pane/controlli di Leaflet (max 1000) */
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-height: var(--topbar-h);
  padding: 0.5rem 0.9rem;
  padding-top: max(0.5rem, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.brand {
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.4px;
  font-size: 1.05rem;
}

.main-nav {
  display: none;                 /* mobile: nascosto, mostrato dal toggle */
  flex: 1;
}
.main-nav a {
  color: var(--text);
  text-decoration: none;
  padding: 0.6rem 0.75rem;
  border-radius: 8px;
}
.main-nav a:hover { background: var(--bg-elev); color: var(--accent); }

.me {
  display: none;                 /* mobile: logout nel menu */
  align-items: center;
  gap: 0.5rem;
  color: var(--muted);
}
.me-name { display: none; }

.menu-toggle {
  order: 3;
  margin-left: auto;
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
}
.menu-icon,
.menu-icon::before,
.menu-icon::after {
  content: "";
  display: block;
  width: 20px;
  height: 2px;
  background: var(--text);
  position: relative;
}
.menu-icon::before { position: absolute; top: -6px; }
.menu-icon::after { position: absolute; top: 6px; }

/* Menu aperto (mobile) */
.topbar.open .main-nav {
  display: flex;
  flex-direction: column;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  padding: 0.5rem;
  gap: 0.25rem;
}
.topbar.open .main-nav a { padding: 0.85rem 0.9rem; }

/* ------------------------------------------------------------------- Main */
main {
  max-width: 1000px;
  margin: 0 auto;
  padding: var(--gap);
  padding-left: max(var(--gap), env(safe-area-inset-left));
  padding-right: max(var(--gap), env(safe-area-inset-right));
  padding-bottom: max(var(--gap), env(safe-area-inset-bottom));
}

h1 { font-size: clamp(1.35rem, 1.1rem + 2vw, 1.75rem); margin: 0 0 0.75rem; }
h2 { font-size: 1.1rem; margin: 1.75rem 0 0.5rem; }
.muted { color: var(--muted); }
.error { color: var(--error); }
code {
  background: var(--bg-elev);
  padding: 0.1rem 0.35rem;
  border-radius: 6px;
  font-size: 0.9em;
  word-break: break-all;
}

/* --------------------------------------------------------------- Feedback */
.flash { padding: 0.75rem 0.9rem; border-radius: 10px; margin-bottom: 1rem; }
.flash-ok { background: #10331d; border: 1px solid var(--ok); }
.flash-error { background: #3a1a1a; border: 1px solid var(--error); }

/* --------------------------------------------------------------- Controlli */
.btn, .btn-small, button {
  font: inherit;
  cursor: pointer;
}
.btn, .btn-small {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;              /* touch target */
  padding: 0.55rem 1rem;
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: 10px;
  font-weight: 600;
  text-decoration: none;
}
.btn-small { min-height: 40px; padding: 0.4rem 0.8rem; font-size: 0.9rem; }
.btn.danger, .btn-small.danger { background: var(--error); color: #1a0606; }
.btn:active, .btn-small:active { transform: translateY(1px); }

input[type=text], input[type=password], input[type=number], input[type=search], select, .select {
  width: 100%;
  min-height: 44px;
  padding: 0.55rem 0.7rem;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font: inherit;
}
.input-num { width: 100%; }
::placeholder { color: var(--muted); }

label { display: block; font-size: 0.9rem; color: var(--muted); }
label > input, label > select { margin-top: 0.3rem; }
label.inline {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text);
  min-height: 44px;
}

.toolbar {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin: 1rem 0;
}

/* ------------------------------------------------------------------- Card */
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem;
  box-shadow: var(--shadow);
}
.login-card { max-width: 380px; margin: 1.5rem auto; }
.login-card form { display: flex; flex-direction: column; gap: 0.9rem; }

/* ------------------------------------------------------------------- Mappe */
#map, #dashboard-map {
  height: min(60vh, 480px);
  min-height: 300px;
  border-radius: var(--radius);
  margin: 1rem 0;
  border: 1px solid var(--border);
}

/* ------------------------------------------------------- Tabelle -> card  */
table.grid {
  width: 100%;
  border-collapse: collapse;
  margin-top: 0.75rem;
}
table.grid th, table.grid td {
  text-align: left;
  padding: 0.55rem 0.6rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.92rem;
}
table.grid th { color: var(--muted); font-weight: 600; }

/* Mobile: ogni riga diventa una card */
table.grid thead { display: none; }
table.grid tr {
  display: block;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.35rem 0.75rem;
  margin-bottom: 0.75rem;
}
table.grid td {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  border: none;
  border-bottom: 1px solid color-mix(in srgb, var(--border) 50%, transparent);
  padding: 0.6rem 0;
}
table.grid tr td:last-child { border-bottom: none; }
table.grid td::before {
  content: attr(data-label);
  color: var(--muted);
  font-weight: 600;
  font-size: 0.85rem;
}
table.grid td.td-actions { justify-content: flex-end; }
table.grid td.td-actions::before { content: ""; }

/* --------------------------------------------------- Dispositivi (admin)  */
.device-list { display: flex; flex-direction: column; gap: 0.75rem; }
.device-row {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem;
}
.device-col.name { display: flex; flex-direction: column; gap: 0.15rem; }
.inline-form { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }

/* ----------------------------------------------------------------- Switch */
.switch { position: relative; display: inline-block; width: 52px; height: 30px; flex: none; }
.switch input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
}
.switch .slider {
  position: absolute;
  inset: 0;
  background: #4b5b74;
  border-radius: 30px;
  transition: background 0.2s;
  pointer-events: none;
}
.switch .slider::before {
  content: "";
  position: absolute;
  height: 22px;
  width: 22px;
  left: 4px;
  top: 4px;
  background: #e8eef7;
  border-radius: 50%;
  transition: transform 0.2s;
}
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(22px); }
.switch input:focus-visible + .slider { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ============================ Tablet / desktop =========================== */
@media (min-width: 640px) {
  :root { --gap: 1.5rem; }
  .toolbar { flex-direction: row; align-items: flex-end; flex-wrap: wrap; }
  .input-num { width: 6rem; }
  .device-row { flex-direction: row; align-items: center; }
  .device-row .device-col { flex: 1; }
  .device-row .device-col.monitored,
  .device-row .device-col:last-child { flex: none; }
  label > input, label > select { width: auto; }
  label > input[type=text], label > input[type=password] { width: 100%; }
}

@media (min-width: 900px) {
  /* Navigazione inline */
  .main-nav {
    display: flex;
    flex: 1;
    gap: 0.25rem;
    margin-left: 1rem;
  }
  .menu-toggle { display: none; }
  .me { display: flex; margin-left: auto; }
  .me-name { display: inline; }

  /* Tabelle classiche */
  table.grid thead { display: table-header-group; }
  table.grid tr {
    display: table-row;
    background: transparent;
    border: none;
    border-radius: 0;
    padding: 0;
    margin: 0;
  }
  table.grid td {
    display: table-cell;
    border-bottom: 1px solid var(--border);
    padding: 0.55rem 0.6rem;
  }
  table.grid td::before { content: none; }
  .input-num { width: 6rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
