:root {
    --bg: #0f172a;
    --panel: #111827;
    --panel-2: #ffffff;
    --text: #0f172a;
    --muted: #64748b;
    --line: #e5e7eb;
    --brand: #2563eb;
    --brand-dark: #1d4ed8;
    --danger: #dc2626;
    --success: #16a34a;
    --warning: #f59e0b;
    --shadow: 0 20px 45px rgba(15, 23, 42, .18);
}
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; color: var(--text); background: #f8fafc; }
a { color: var(--brand); text-decoration: none; }
.auth-body { min-height: 100vh; display: grid; place-items: center; background: radial-gradient(circle at top left, #dbeafe, #f8fafc 45%, #eff6ff); padding: 24px; }
.auth-card { width: 100%; max-width: 420px; background: white; border-radius: 22px; box-shadow: var(--shadow); padding: 34px; }
.brand { font-weight: 800; font-size: 1.1rem; margin-bottom: 22px; }
h1 { margin: 0 0 8px; font-size: 1.6rem; }
.muted { color: var(--muted); line-height: 1.5; }
.form-stack { display: grid; gap: 16px; margin-top: 20px; }
label { display: grid; gap: 7px; font-weight: 650; font-size: .92rem; }
input, select { width: 100%; border: 1px solid var(--line); border-radius: 12px; padding: 12px 13px; font-size: 1rem; background: white; }
input:focus, select:focus { outline: 3px solid #dbeafe; border-color: var(--brand); }
.btn { border: 0; border-radius: 12px; padding: 11px 15px; background: #e2e8f0; color: #0f172a; font-weight: 750; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.btn.primary { background: var(--brand); color: white; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.full { width: 100%; }
.btn.small { padding: 7px 11px; font-size: .85rem; }
.alert { border-radius: 12px; padding: 11px 13px; margin: 16px 0; }
.alert.error { background: #fee2e2; color: #991b1b; }
.alert.success { background: #dcfce7; color: #166534; }
.center { text-align: center; }
.app-body { height: 100vh; overflow: hidden; }
.topbar { height: 62px; background: #0f172a; color: white; display: flex; align-items: center; justify-content: space-between; padding: 0 18px; }
.topbar-brand { font-weight: 850; }
.topbar-nav { display: flex; gap: 10px; align-items: center; }
.topbar-nav a { color: #cbd5e1; padding: 9px 12px; border-radius: 10px; font-weight: 700; }
.topbar-nav a.active, .topbar-nav a:hover { background: rgba(255,255,255,.11); color: white; }
.layout { display: grid; grid-template-columns: 340px minmax(0, 1fr); height: calc(100vh - 62px); }
.sidebar { background: white; border-right: 1px solid var(--line); padding: 16px; overflow: auto; }
.panel-title { font-size: 1.1rem; font-weight: 850; margin-bottom: 12px; }
.search { margin-bottom: 14px; }
.vehicle-list { display: grid; gap: 10px; }
.vehicle-item { border: 1px solid var(--line); border-radius: 16px; padding: 12px; cursor: pointer; background: #fff; transition: .15s ease; }
.vehicle-item:hover, .vehicle-item.active { border-color: var(--brand); box-shadow: 0 8px 25px rgba(37,99,235,.12); }
.vehicle-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.plate { font-weight: 900; letter-spacing: .4px; }
.badge { border-radius: 999px; padding: 4px 8px; font-size: .75rem; font-weight: 800; background: #e2e8f0; }
.badge.online { background: #dcfce7; color: #166534; }
.badge.offline { background: #fee2e2; color: #991b1b; }
.vehicle-meta { color: var(--muted); font-size: .86rem; margin-top: 7px; line-height: 1.45; }
.mini-help { color: var(--muted); font-size: .82rem; margin-top: 16px; line-height: 1.5; }
.map-wrap { position: relative; }
#map { width: 100%; height: 100%; }
.floating-card { position: absolute; z-index: 700; left: 18px; bottom: 18px; width: min(440px, calc(100% - 36px)); }
.vehicle-card, .panel { background: white; border: 1px solid var(--line); border-radius: 18px; padding: 16px; box-shadow: var(--shadow); }
.vehicle-card p { margin: 8px 0 0; color: var(--muted); }
.vehicle-card-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; margin-top: 12px; }
.metric { background: #f8fafc; border-radius: 12px; padding: 10px; }
.metric span { display: block; color: var(--muted); font-size: .75rem; font-weight: 800; text-transform: uppercase; }
.metric strong { display: block; margin-top: 4px; }
.marker-arrow { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; background: #2563eb; color: white; border: 3px solid white; box-shadow: 0 6px 18px rgba(0,0,0,.25); font-size: 16px; }
.marker-arrow.stopped { background: #16a34a; }
.marker-arrow.offline { background: #64748b; }
.marker-arrow.alarm { background: #dc2626; }
.commands { margin-top: 12px; display: flex; gap: 8px; flex-wrap: wrap; }
.reports-body { overflow: auto; height: auto; min-height: 100vh; }
.reports-page { padding: 20px; display: grid; gap: 18px; }
.report-filters h1 { margin-bottom: 14px; }
.report-grid { display: grid; grid-template-columns: 1fr 1fr 1fr auto; gap: 14px; align-items: end; }
.report-summary { display: grid; grid-template-columns: repeat(5, minmax(0,1fr)); gap: 12px; }
.summary-box { background: white; border: 1px solid var(--line); border-radius: 16px; padding: 14px; box-shadow: 0 12px 30px rgba(15,23,42,.08); }
.summary-box span { color: var(--muted); font-size: .78rem; font-weight: 850; text-transform: uppercase; }
.summary-box strong { display:block; font-size: 1.25rem; margin-top: 5px; }
.report-layout { display: grid; grid-template-columns: minmax(0, .95fr) minmax(0, 1.05fr); gap: 18px; align-items: start; }
#reportMap { height: 560px; border-radius: 14px; overflow: hidden; }
.table-toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.table-scroll { overflow: auto; max-height: 560px; border: 1px solid var(--line); border-radius: 14px; }
table { border-collapse: collapse; width: 100%; font-size: .86rem; background: white; }
th, td { border-bottom: 1px solid var(--line); padding: 9px 10px; text-align: left; white-space: nowrap; }
th { background: #f1f5f9; position: sticky; top: 0; z-index: 1; }
@media (max-width: 900px) {
    .layout { grid-template-columns: 1fr; }
    .sidebar { height: 280px; border-right: 0; border-bottom: 1px solid var(--line); }
    .report-grid, .report-layout, .report-summary { grid-template-columns: 1fr; }
    .app-body { overflow: auto; }
    .map-wrap { height: calc(100vh - 342px); }
}

/* v9: veicoli attivi, stato online/offline e responsive migliorato */
.menu-toggle,
.sidebar-close {
    display: none;
    border: 0;
    background: rgba(255,255,255,.12);
    color: white;
    border-radius: 10px;
    font-size: 1.2rem;
    width: 42px;
    height: 42px;
    cursor: pointer;
}
.sidebar-header { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.muted-small { color: var(--muted); font-size: .78rem; }
.status-text.moving { color: var(--brand); }
.status-text.stopped { color: var(--success); }
.status-text.alarm { color: var(--danger); }
.status-text.offline { color: var(--muted); }
.selected-badge { margin-left: 8px; vertical-align: middle; }
.btn.danger { background: #fee2e2; color: #991b1b; }
.metric small { display: block; margin-top: 4px; color: var(--muted); font-size: .78rem; }

@media (max-width: 900px) {
    html, body { min-height: 100%; }
    .app-body { height: 100dvh; overflow: hidden; }
    .topbar {
        height: auto;
        min-height: 58px;
        padding: 8px 10px;
        gap: 8px;
    }
    .menu-toggle { display: inline-grid; place-items: center; flex: 0 0 auto; }
    .topbar-brand { font-size: .98rem; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .topbar-nav { gap: 4px; }
    .topbar-nav a { padding: 8px 9px; font-size: .88rem; }
    .layout {
        display: block;
        height: calc(100dvh - 58px);
        position: relative;
    }
    .sidebar {
        position: fixed;
        z-index: 1200;
        inset: 0 auto 0 0;
        width: min(88vw, 360px);
        height: 100dvh;
        border-right: 1px solid var(--line);
        border-bottom: 0;
        transform: translateX(-105%);
        transition: transform .22s ease;
        box-shadow: var(--shadow);
    }
    body.sidebar-open .sidebar { transform: translateX(0); }
    body.sidebar-open::after {
        content: '';
        position: fixed;
        inset: 0;
        background: rgba(15, 23, 42, .42);
        z-index: 1100;
    }
    .sidebar-close {
        display: inline-grid;
        place-items: center;
        background: #e2e8f0;
        color: #0f172a;
    }
    .map-wrap { height: 100%; min-height: 0; }
    #map { height: 100%; }
    .floating-card {
        left: 10px;
        right: 10px;
        bottom: 10px;
        width: auto;
        max-height: 42dvh;
        overflow: auto;
        border-radius: 16px;
    }
    .vehicle-card-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .commands .btn { flex: 1 1 auto; }
    .leaflet-control-layers { font-size: .85rem; }
}

@media (max-width: 520px) {
    .topbar-nav a { padding: 7px 7px; font-size: .82rem; }
    .vehicle-card-grid { grid-template-columns: 1fr; }
    .floating-card { max-height: 46dvh; }
    .auth-card { padding: 24px; border-radius: 18px; }
    .reports-page { padding: 12px; }
    .report-filters h1 { font-size: 1.25rem; }
    #reportMap { height: 360px; }
    .table-toolbar { gap: 8px; align-items: flex-start; flex-direction: column; }
}

/* v12: pulsante aggiornamento veicolo e feedback comando */
.vehicle-card-actions { margin-top: 12px; display: flex; gap: 8px; flex-wrap: wrap; }
.vehicle-card-actions .btn { flex: 0 1 auto; }
.btn:disabled { opacity: .65; cursor: wait; }
.command-status { margin-top: 10px; font-size: .84rem; line-height: 1.45; border-radius: 12px; padding: 9px 10px; display: none; }
.command-status.pending,
.command-status.success,
.command-status.error { display: block; }
.command-status.pending { background: #eff6ff; color: #1d4ed8; }
.command-status.success { background: #dcfce7; color: #166534; }
.command-status.error { background: #fee2e2; color: #991b1b; }
@media (max-width: 520px) {
    .vehicle-card-actions .btn { flex: 1 1 100%; }
}


/* v13: aggiornamento globale veicoli e report con colonne dinamiche */
.sidebar-action { margin: -2px 0 12px; }
.bulk-refresh-status { display: none; margin: 0 0 12px; border-radius: 12px; padding: 9px 10px; font-size: .84rem; line-height: 1.45; }
.bulk-refresh-status.pending,
.bulk-refresh-status.success,
.bulk-refresh-status.warning,
.bulk-refresh-status.error { display: block; }
.bulk-refresh-status.pending { background: #eff6ff; color: #1d4ed8; }
.bulk-refresh-status.success { background: #dcfce7; color: #166534; }
.bulk-refresh-status.warning { background: #fef3c7; color: #92400e; }
.bulk-refresh-status.error { background: #fee2e2; color: #991b1b; }
.column-panel { border: 1px solid var(--line); border-radius: 14px; padding: 12px; margin-bottom: 12px; background: #f8fafc; }
.column-panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.column-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.column-picker { display: flex; flex-wrap: wrap; gap: 8px; max-height: 170px; overflow: auto; padding-right: 4px; }
.column-chip { display: inline-flex; align-items: center; gap: 7px; width: auto; padding: 7px 10px; border: 1px solid var(--line); border-radius: 999px; background: white; font-size: .84rem; font-weight: 750; cursor: pointer; }
.column-chip input { width: auto; margin: 0; }
@media (max-width: 700px) {
    .column-panel-head { align-items: flex-start; flex-direction: column; }
    .column-actions .btn { flex: 1 1 auto; }
    .column-picker { max-height: 230px; }
    .column-chip { border-radius: 12px; width: 100%; justify-content: flex-start; }
}


/* Scheda veicolo selezionato */
.vehicle-card.has-selection { padding-right: 46px; }
.vehicle-card-title-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.vehicle-card-close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 32px;
    height: 32px;
    border-radius: 999px;
    border: 1px solid var(--line);
    background: #fff;
    color: #334155;
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 6px 16px rgba(15, 23, 42, .12);
}
.vehicle-card-close:hover { background: #f8fafc; color: #0f172a; }

/* Automazioni */
.automations-page { padding: 18px; }
.automations-shell { max-width: 1280px; margin: 0 auto; }
.automations-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 14px 0; flex-wrap: wrap; }
.automations-grid { display: grid; gap: 14px; }
.automation-card { background: #fff; border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow); padding: 16px; }
.automation-card-header { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; margin-bottom: 12px; }
.automation-card .plate { font-size: 1.05rem; }
.automation-form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.automation-form-grid label { display: grid; gap: 6px; font-size: .86rem; color: var(--muted); font-weight: 700; }
.automation-form-grid input, .automation-form-grid select { width: 100%; padding: 10px 11px; border: 1px solid var(--line); border-radius: 12px; background: #fff; }
.automation-actions { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 12px; flex-wrap: wrap; }
.automation-status { min-height: 20px; font-size: .9rem; color: var(--muted); }
.automation-status.success { color: #047857; font-weight: 800; }
.automation-status.error { color: #b91c1c; font-weight: 800; }
.automation-empty { padding: 18px; border: 1px dashed var(--line); border-radius: 18px; background: #fff; color: var(--muted); }
@media (max-width: 760px) {
  .automations-page { padding: 12px; }
  .automation-form-grid { grid-template-columns: 1fr; }
  .automation-card-header { flex-direction: column; }
  .automation-actions .btn { flex: 1 1 100%; }
}

/* v15: motore automazioni configurabile da pannello */
.hidden { display: none !important; }
textarea { width: 100%; border: 1px solid var(--line); border-radius: 12px; padding: 12px 13px; font-size: .95rem; background: white; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace; }
textarea:focus { outline: 3px solid #dbeafe; border-color: var(--brand); }
.automation-intro { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 14px; align-items: start; margin-bottom: 16px; }
.automations-toolbar.compact { margin: 0; justify-content: flex-end; }
.automation-editor { margin-bottom: 16px; }
.automation-editor-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.vehicle-card-close.inline { position: static; flex: 0 0 auto; }
.automation-rule-form .automation-form-grid.wide { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.automation-rule-form .span-2 { grid-column: span 2; }
.automation-rule-form .span-4 { grid-column: span 4; }
.automation-rule-details { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin-top: 8px; }
.automation-rule-details > div { background: #f8fafc; border: 1px solid var(--line); border-radius: 13px; padding: 10px; min-width: 0; }
.automation-rule-details strong { display: block; color: var(--muted); text-transform: uppercase; font-size: .72rem; letter-spacing: .02em; margin-bottom: 5px; }
.automation-rule-details span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; }
.automation-description { margin: 10px 0 0; }
.automation-card .automation-actions { justify-content: flex-end; border-top: 1px solid var(--line); padding-top: 12px; }
@media (max-width: 980px) {
  .automation-intro { grid-template-columns: 1fr; }
  .automations-toolbar.compact { justify-content: flex-start; }
  .automation-rule-form .automation-form-grid.wide { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .automation-rule-details { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 640px) {
  .automation-rule-form .automation-form-grid.wide { grid-template-columns: 1fr; }
  .automation-rule-form .span-2, .automation-rule-form .span-4 { grid-column: auto; }
  .automation-rule-details { grid-template-columns: 1fr; }
  .automations-toolbar.compact .btn { flex: 1 1 100%; }
}

/* v17 - Automazioni dinamiche: condizioni e placeholder da ultimo pacchetto */
.automation-fields-panel,
.automation-conditions-section {
  border: 1px solid var(--border, #d9dee8);
  border-radius: 14px;
  padding: 14px;
  background: rgba(255,255,255,.65);
}

.automation-fields-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.automation-field-preview {
  display: grid;
  grid-template-columns: minmax(140px, 260px) 1fr;
  gap: 6px 10px;
  max-height: 220px;
  overflow: auto;
  padding: 8px;
  border-radius: 10px;
  background: rgba(0,0,0,.035);
  font-size: 12px;
}

.automation-field-preview code {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.automation-field-preview span {
  overflow-wrap: anywhere;
  color: var(--muted, #667085);
}

.conditions-builder {
  display: grid;
  gap: 8px;
}

.condition-row {
  display: grid;
  grid-template-columns: minmax(220px, 1.3fr) minmax(150px, .7fr) minmax(160px, 1fr) auto;
  gap: 8px;
  align-items: center;
}

.condition-row select,
.condition-row input {
  min-width: 0;
}

.automation-empty.compact {
  padding: 10px;
  border-radius: 10px;
  font-size: 13px;
}

@media (max-width: 760px) {
  .automation-fields-head {
    display: grid;
  }
  .automation-field-preview {
    grid-template-columns: 1fr;
  }
  .condition-row {
    grid-template-columns: 1fr;
  }
  .condition-remove {
    width: 100%;
  }
}

/* v18 - Event text + gestione utenti/permessi */
.event-text-panel,
.users-page .panel {
  margin-bottom: 1rem;
}
.event-text-form,
.user-form {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: .85rem;
  align-items: end;
}
.event-text-form label,
.user-form label {
  display: grid;
  gap: .35rem;
  font-size: .9rem;
}
.event-text-list,
.users-list {
  display: grid;
  gap: .65rem;
  margin-top: 1rem;
}
.event-text-card,
.user-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  border: 1px solid rgba(148, 163, 184, .25);
  border-radius: 14px;
  padding: .8rem;
  background: rgba(255,255,255,.75);
}
.event-text-card > div,
.user-card > div:first-child {
  display: grid;
  gap: .2rem;
}
.event-text-card span:not(.badge) {
  color: #475569;
  font-size: .9rem;
}
.users-page {
  padding: 1rem;
}
.users-layout {
  display: grid;
  grid-template-columns: minmax(280px, .7fr) minmax(0, 1.3fr);
  gap: 1rem;
}
.user-card.selected {
  outline: 2px solid rgba(37, 99, 235, .45);
  background: rgba(239, 246, 255, .9);
}
.user-card-actions {
  display: flex;
  gap: .4rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.permissions-head,
.permissions-tools {
  display: flex;
  justify-content: space-between;
  gap: .75rem;
  align-items: center;
  flex-wrap: wrap;
}
.permissions-tools {
  justify-content: flex-start;
  margin: .75rem 0;
}
.permissions-matrix {
  overflow-x: auto;
}
.permissions-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 520px;
}
.permissions-table th,
.permissions-table td {
  border-bottom: 1px solid rgba(148, 163, 184, .25);
  padding: .65rem;
  text-align: left;
}
.permissions-table th:nth-child(2),
.permissions-table th:nth-child(3),
.permissions-table td:nth-child(2),
.permissions-table td:nth-child(3) {
  text-align: center;
}
.permissions-table input[type="checkbox"] {
  width: 1.15rem;
  height: 1.15rem;
}
@media (max-width: 900px) {
  .event-text-form,
  .user-form,
  .users-layout {
    grid-template-columns: 1fr;
  }
  .event-text-card,
  .user-card {
    align-items: flex-start;
    flex-direction: column;
  }
  .user-card-actions {
    justify-content: flex-start;
  }
}


/* v22: log comandi dashboard */
.command-log-panel {
    margin-top: 12px;
    border-top: 1px solid var(--line);
    padding-top: 12px;
}
.command-log-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}
.command-log-list {
    display: grid;
    gap: 8px;
    max-height: 260px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    padding-right: 3px;
}
.command-log-row {
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 9px;
    background: #f8fafc;
}
.command-log-row.ok {
    border-color: rgba(22, 163, 74, .28);
    background: #f0fdf4;
}
.command-log-row.ko {
    border-color: rgba(220, 38, 38, .28);
    background: #fff7f7;
}
.command-log-main {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    align-items: center;
}
.command-log-badge {
    border-radius: 999px;
    padding: 2px 7px;
    font-size: .72rem;
    font-weight: 900;
}
.command-log-badge.ok { background: #dcfce7; color: #166534; }
.command-log-badge.ko { background: #fee2e2; color: #991b1b; }
.command-log-meta {
    margin-top: 4px;
    color: var(--muted);
    font-size: .78rem;
    line-height: 1.45;
    overflow-wrap: anywhere;
}
.command-log-error {
    margin-top: 6px;
    color: #991b1b;
    font-weight: 700;
    font-size: .8rem;
    overflow-wrap: anywhere;
}
.command-log-row details {
    margin-top: 6px;
}
.command-log-row summary {
    cursor: pointer;
    color: var(--brand);
    font-weight: 800;
    font-size: .78rem;
}
.command-log-row pre {
    margin: 6px 0 0;
    padding: 8px;
    border-radius: 8px;
    background: #0f172a;
    color: #dbeafe;
    overflow: auto;
    white-space: pre-wrap;
    font-size: .72rem;
}

/* v22: fix scrolling mobile per Report / Automazioni / Utenti */
body.reports-body {
    height: auto !important;
    min-height: 100dvh !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
}
body.reports-body .topbar {
    position: sticky;
    top: 0;
    z-index: 1300;
}
body.reports-body .topbar-nav {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
}
body.reports-body .reports-page,
body.reports-body .automations-page,
body.reports-body .users-page {
    min-height: calc(100dvh - 62px);
    overflow: visible;
}
body.reports-body .table-scroll,
body.reports-body .permissions-matrix,
body.reports-body .automation-field-preview,
body.reports-body .column-picker,
body.reports-body .event-text-list,
body.reports-body .users-list {
    -webkit-overflow-scrolling: touch;
}
@media (max-width: 900px) {
    body.reports-body {
        height: auto !important;
        min-height: 100dvh !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
    }
    body.reports-body .reports-page,
    body.reports-body .automations-page,
    body.reports-body .users-page {
        padding-bottom: 28px;
    }
    body.reports-body .table-scroll {
        max-height: none;
        overflow: auto;
    }
    body.reports-body .panel {
        overflow: visible;
    }
    body.reports-body .topbar {
        align-items: flex-start;
        flex-wrap: wrap;
    }
    body.reports-body .topbar-brand {
        flex-basis: 100%;
    }
    body.reports-body .topbar-nav {
        width: 100%;
        justify-content: flex-start;
        padding-bottom: 4px;
    }
}

/* v23: pagina Log comandi */
.command-logs-page {
    padding: 20px;
    display: grid;
    gap: 16px;
}
.command-logs-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 14px;
}
.command-logs-header h1 {
    margin: 0 0 6px;
}
.command-logs-filter-grid {
    display: grid;
    grid-template-columns: minmax(180px, 1.2fr) minmax(160px, .9fr) minmax(120px, .7fr) minmax(170px, .9fr) minmax(170px, .9fr) minmax(100px, .5fr) auto;
    gap: 12px;
    align-items: end;
}
.command-logs-list-full {
    display: grid;
    gap: 10px;
}
.command-log-row-full {
    border: 1px solid var(--line);
    border-radius: 16px;
    background: #f8fafc;
    padding: 13px;
}
.command-log-row-full.ok {
    border-color: rgba(22, 163, 74, .30);
    background: #f0fdf4;
}
.command-log-row-full.ko {
    border-color: rgba(220, 38, 38, .30);
    background: #fff7f7;
}
.command-log-full-head {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    align-items: flex-start;
}
.command-log-full-grid {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));
    gap: 8px;
    margin-top: 10px;
}
.command-log-full-grid > div {
    background: rgba(255,255,255,.75);
    border: 1px solid rgba(148, 163, 184, .25);
    border-radius: 12px;
    padding: 8px;
    min-width: 0;
}
.command-log-full-grid span {
    display: block;
    color: var(--muted);
    text-transform: uppercase;
    font-size: .68rem;
    font-weight: 850;
    margin-bottom: 4px;
}
.command-log-full-grid strong {
    display: block;
    font-size: .82rem;
    overflow-wrap: anywhere;
}
.command-log-details {
    margin-top: 8px;
}
.command-log-details summary {
    cursor: pointer;
    color: var(--brand);
    font-weight: 850;
}
.command-log-details pre {
    margin: 8px 0 0;
    padding: 10px;
    border-radius: 10px;
    background: #0f172a;
    color: #dbeafe;
    max-height: 280px;
    overflow: auto;
    white-space: pre-wrap;
    font-size: .78rem;
}
@media (max-width: 1100px) {
    .command-logs-filter-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    .command-logs-filter-grid .btn {
        grid-column: 1 / -1;
    }
    .command-log-full-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}
@media (max-width: 640px) {
    .command-logs-page {
        padding: 12px;
    }
    .command-logs-header {
        flex-direction: column;
    }
    .command-logs-header .btn {
        width: 100%;
    }
    .command-logs-filter-grid {
        grid-template-columns: 1fr;
    }
    .command-log-full-grid {
        grid-template-columns: 1fr;
    }
}

.command-log-logical-state {
    margin-top: 8px;
    border-radius: 10px;
    background: #eff6ff;
    color: #1d4ed8;
    padding: 8px 10px;
    font-size: .82rem;
    font-weight: 850;
}

.engine-block-state.confirmed { color: #166534; }
.engine-block-state.pending,
.engine-block-state.awaiting { color: #92400e; }
.engine-block-state.command { color: #1d4ed8; }
.engine-block-state.unknown { color: var(--muted); }
.metric small {
    display: block;
    margin-top: 4px;
    color: var(--muted);
    font-size: .76rem;
    line-height: 1.35;
}


/* v-receiver-control: menu unico, dropdown admin e pagina Receiver */
.page-body-scroll {
    height: auto;
    min-height: 100vh;
    overflow: auto;
}

.topbar {
    gap: 12px;
}

.topbar-brand {
    flex: 0 1 auto;
    min-width: 150px;
}

.topbar-nav {
    min-width: 0;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.nav-dropdown {
    position: relative;
    display: inline-flex;
    align-items: center;
}

.nav-dropdown-button {
    border: 0;
    color: #cbd5e1;
    background: transparent;
    padding: 9px 12px;
    border-radius: 10px;
    font-weight: 800;
    cursor: pointer;
    font: inherit;
}

.nav-dropdown.active .nav-dropdown-button,
.nav-dropdown:hover .nav-dropdown-button,
.nav-dropdown:focus-within .nav-dropdown-button {
    background: rgba(255,255,255,.11);
    color: white;
}

.nav-dropdown-menu {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    z-index: 2500;
    min-width: 220px;
    display: none;
    padding: 8px;
    border: 1px solid rgba(148,163,184,.35);
    border-radius: 14px;
    background: #0f172a;
    box-shadow: 0 18px 40px rgba(15,23,42,.28);
}

.nav-dropdown:hover .nav-dropdown-menu,
.nav-dropdown:focus-within .nav-dropdown-menu {
    display: grid;
    gap: 4px;
}

.nav-dropdown-menu a {
    display: block;
    white-space: nowrap;
}

.receiver-page {
    padding: 20px;
    display: grid;
    gap: 16px;
}

.receiver-hero,
.panel-head-row,
.receiver-toolbar,
.receiver-inline-form {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    flex-wrap: wrap;
}

.receiver-hero h1,
.receiver-tab-panel h2 {
    margin: 0 0 6px;
}

.receiver-toolbar {
    justify-content: flex-end;
}

.receiver-auto-refresh {
    width: auto;
    margin: 0;
    font-weight: 800;
}

.receiver-stats,
.receiver-blacklist-summary {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 12px;
}

.receiver-stat-card {
    background: white;
    border: 1px solid var(--line);
    border-radius: 16px;
    padding: 14px;
    box-shadow: 0 12px 30px rgba(15,23,42,.08);
}

.receiver-stat-card span {
    color: var(--muted);
    font-size: .78rem;
    font-weight: 850;
    text-transform: uppercase;
}

.receiver-stat-card strong {
    display: block;
    margin-top: 5px;
    font-size: 1.35rem;
}

.receiver-stat-card small {
    display: block;
    margin-top: 4px;
    color: var(--muted);
}

.receiver-tabs {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.receiver-tab {
    border: 1px solid var(--line);
    background: white;
    color: var(--dark);
    border-radius: 999px;
    padding: 9px 13px;
    font-weight: 850;
    cursor: pointer;
}

.receiver-tab.active,
.receiver-tab:hover {
    border-color: var(--brand);
    background: #eff6ff;
    color: #1d4ed8;
}

.receiver-tab-panel {
    display: none;
}

.receiver-tab-panel.active {
    display: block;
}

.receiver-table-scroll {
    max-height: 620px;
}

.receiver-inline-form {
    justify-content: flex-start;
    align-items: end;
    margin-bottom: 14px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: #f8fafc;
}

.receiver-inline-form label {
    min-width: 160px;
}

.empty-state {
    padding: 18px;
    border: 1px dashed var(--line);
    border-radius: 14px;
    color: var(--muted);
    background: #f8fafc;
}

.notice.warning {
    background: #fef3c7;
    color: #92400e;
    border: 1px solid #fde68a;
    border-radius: 14px;
    padding: 12px;
}

.automation-status.pending { color: #1d4ed8; }
.automation-status.success { color: #166534; }
.automation-status.error { color: #991b1b; }

@media (max-width: 1100px) {
    .receiver-stats,
    .receiver-blacklist-summary {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .topbar {
        height: auto;
        min-height: 62px;
        align-items: flex-start;
        padding-top: 10px;
        padding-bottom: 10px;
    }

    .topbar-nav {
        gap: 6px;
    }
}

@media (max-width: 700px) {
    .receiver-page {
        padding: 12px;
    }

    .receiver-stats,
    .receiver-blacklist-summary {
        grid-template-columns: 1fr;
    }

    .receiver-inline-form {
        display: grid;
        grid-template-columns: 1fr;
    }

    .receiver-inline-form label,
    .receiver-inline-form .btn {
        width: 100%;
    }

    .nav-dropdown {
        position: static;
    }

    .nav-dropdown-menu {
        position: absolute;
        left: 10px;
        right: 10px;
        top: auto;
        margin-top: 8px;
    }
}
