:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --ink: #1f2a37;
  --muted: #6b7280;
  --brand: #1d5fa8;
  --brand-dark: #154880;
  --line: #e3e8ef;
  --ok: #1f9d55;
  --warn: #d97706;
  --danger: #dc2626;
  --shadow: 0 1px 3px rgba(16,24,40,.08), 0 1px 2px rgba(16,24,40,.06);
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
}
a { color: var(--brand); }
button { font-family: inherit; cursor: pointer; }
.hidden { display: none !important; }

/* LOGIN */
#login {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #1d5fa8, #154880);
}
.login-card {
  background: var(--panel); padding: 34px; border-radius: 14px; width: 360px;
  box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; color: var(--brand-dark); }
.login-card .sub { color: var(--muted); margin: 0 0 22px; font-size: 13px; }
.login-card input { width: 100%; }
.login-err { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 8px; }

/* LAYOUT */
#app { display: flex; min-height: 100vh; }
.sidebar {
  width: 230px; background: var(--brand-dark); color: #dbe7f5; display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.sidebar .brand { padding: 20px 18px; font-weight: 700; font-size: 17px; color: #fff; letter-spacing:.3px; }
.sidebar .brand span { color: #9ec3ec; font-weight: 400; display:block; font-size: 11px; }
.nav { flex: 1; padding: 6px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 11px 14px; border-radius: 8px;
  color: #cfe0f2; text-decoration: none; font-weight: 500; margin-bottom: 2px;
}
.nav a:hover { background: rgba(255,255,255,.08); }
.nav a.active { background: var(--brand); color: #fff; }
.nav .ico { width: 18px; text-align: center; }
.sidebar .who { padding: 14px 18px; border-top: 1px solid rgba(255,255,255,.12); font-size: 12px; color:#9ec3ec; }
.sidebar .who button { margin-top: 8px; background: transparent; border: 1px solid rgba(255,255,255,.3); color: #dbe7f5; padding: 6px 10px; border-radius: 6px; width: 100%; }
.sidebar .who button:hover { background: rgba(255,255,255,.1); }

.main { flex: 1; min-width: 0; }
.topbar { background: var(--panel); border-bottom: 1px solid var(--line); padding: 16px 26px; display:flex; align-items:center; justify-content: space-between; position: sticky; top:0; z-index: 5; }
.topbar h2 { margin: 0; font-size: 19px; }
.content { padding: 24px 26px; }

/* CARDS / STATS */
.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 22px; }
.stat { background: var(--panel); border:1px solid var(--line); border-radius: 12px; padding: 18px; box-shadow: var(--shadow); }
.stat .n { font-size: 30px; font-weight: 700; color: var(--brand-dark); }
.stat .l { color: var(--muted); font-size: 13px; margin-top: 2px; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); margin-bottom: 20px; }
.panel-h { padding: 14px 18px; border-bottom: 1px solid var(--line); display:flex; justify-content: space-between; align-items:center; }
.panel-h h3 { margin: 0; font-size: 15px; }
.panel-b { padding: 4px 0; }

/* TABLE */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 18px; border-bottom: 1px solid var(--line); }
th { font-size: 12px; text-transform: uppercase; letter-spacing:.4px; color: var(--muted); background: #fafbfc; }
tbody tr:hover { background: #f8fafc; }
td.actions { text-align: right; white-space: nowrap; }
.empty { padding: 30px; text-align: center; color: var(--muted); }

/* BADGES */
.badge { display: inline-block; padding: 3px 9px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.badge.censito { background: #e5e7eb; color:#374151; }
.badge.attivato { background: #dbeafe; color:#1e40af; }
.badge.in_servizio { background: #dcfce7; color:#166534; }
.badge.fuori_servizio { background: #fef3c7; color:#92400e; }
.badge.disattivato { background: #fee2e2; color:#991b1b; }
.badge.dismesso { background: #f3e8ff; color:#6b21a8; }
.pill { padding:2px 8px; border-radius:6px; font-size:11px; font-weight:600; }
.pill.scaduto { background:#fee2e2; color:#991b1b; }
.pill.urgente { background:#fef3c7; color:#92400e; }
.pill.ok { background:#dcfce7; color:#166534; }

/* BUTTONS / FORMS */
.btn { background: var(--brand); color:#fff; border:none; padding: 9px 16px; border-radius: 8px; font-weight: 600; }
.btn:hover { background: var(--brand-dark); }
.btn.sm { padding: 6px 11px; font-size: 12px; }
.btn.ghost { background: transparent; color: var(--brand); border:1px solid var(--line); }
.btn.ghost:hover { background: #f1f5f9; }
.btn.danger { background: var(--danger); }
.btn.danger:hover { background:#b91c1c; }
.linkbtn { background:none; border:none; color: var(--brand); padding:4px 6px; font-weight:600; font-size:12px; }
.linkbtn:hover { text-decoration: underline; }
.linkbtn.danger { color: var(--danger); }

input, select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 14px;
  margin-top: 4px; background:#fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid #bfdbfe; border-color: var(--brand); }
label { font-size: 12px; font-weight: 600; color: #475569; display:block; margin-bottom: 2px; }
.field { margin-bottom: 13px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; }

.toolbar { display:flex; gap: 10px; margin-bottom: 16px; align-items:center; }
.toolbar input, .toolbar select { width: auto; flex: 0 0 auto; margin: 0; }
.toolbar .search { flex: 1; }
.toolbar .spacer { flex: 1; }

/* MODAL */
.modal-bg { position: fixed; inset: 0; background: rgba(15,23,42,.5); display:flex; align-items:flex-start; justify-content:center; padding: 40px 16px; z-index: 50; overflow:auto; }
.modal { background:#fff; border-radius: 14px; width: 640px; max-width: 100%; box-shadow: 0 25px 60px rgba(0,0,0,.3); }
.modal-h { padding: 18px 22px; border-bottom: 1px solid var(--line); display:flex; justify-content: space-between; align-items:center; }
.modal-h h3 { margin: 0; font-size: 17px; }
.modal-h .x { background:none; border:none; font-size: 22px; color: var(--muted); line-height: 1; }
.modal-b { padding: 22px; max-height: 65vh; overflow:auto; }
.modal-f { padding: 14px 22px; border-top: 1px solid var(--line); display:flex; justify-content:flex-end; gap: 10px; }

.note-box { background:#eff6ff; border:1px solid #bfdbfe; border-radius:10px; padding:14px 16px; color:#1e3a5f; font-size:13px; line-height:1.5; margin-bottom:16px; }
.note-box.warn { background:#fffbeb; border-color:#fde68a; color:#78350f; }
.muted { color: var(--muted); }
.timeline { list-style:none; margin:0; padding: 4px 0; }
.timeline li { padding: 14px 18px; border-bottom: 1px solid var(--line); }
.timeline .t-top { display:flex; justify-content: space-between; align-items:center; }
.timeline .t-type { font-weight: 700; }
.timeline .t-date { color: var(--muted); font-size: 12px; }
.timeline .t-desc { margin-top: 4px; color:#374151; }
.timeline .t-meta { margin-top: 4px; font-size: 12px; color: var(--muted); }
/* QR PANEL */
.qr-panel { background:#eff6ff; border:1px solid #bfdbfe; border-radius:10px; padding:14px 16px; margin-bottom:18px; }
.qr-head { font-size:13px; color:#1e3a5f; margin-bottom:10px; }
.qr-tools { display:flex; flex-wrap:wrap; gap:8px; align-items:center; }
.qr-tools .qr-link { flex:1; min-width:180px; margin:0; }
.filebtn { position:relative; overflow:hidden; }
.filebtn input[type=file] { position:absolute; inset:0; opacity:0; cursor:pointer; }
#qr-stage { margin-top:10px; }
.qr-status { margin-top:8px; font-size:12.5px; min-height:16px; color:#475569; }
.qr-status.ok { color:#166534; font-weight:600; }
.qr-status.err { color:#b91c1c; font-weight:600; }
.qr-status.warn { color:#92400e; font-weight:600; }

@media (max-width: 800px){ .stat-grid{grid-template-columns:repeat(2,1fr);} .sidebar{width:64px;} .sidebar .brand span,.nav a span.txt,.sidebar .who{display:none;} }
