/* Piramis Locator — colori del marchio: blu #0a3a8f, rosso #e6001c */
:root {
  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #f7f8fb;
  --text: #16203a;
  --muted: #667089;
  --border: #e1e5ee;
  --brand: #0a3a8f;
  --brand-2: #e6001c;
  --accent: #0a3a8f;
  --accent-text: #ffffff;
  --accent-soft: #e6ecf8;
  --on: #16883a;
  --on-bg: #def4e5;
  --off: #6b7280;
  --off-bg: #eceef2;
  --warn: #a15c00;
  --warn-bg: #fdf0d9;
  --danger: #c0151f;
  --danger-bg: #fde7e8;
  --flash: #fff6cc;
  --shadow: 0 1px 2px rgb(16 24 40 / 0.05), 0 1px 3px rgb(16 24 40 / 0.06);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141d;
    --surface: #182030;
    --surface-2: #1d2638;
    --text: #e7ebf3;
    --muted: #97a1b6;
    --border: #2a3449;
    --accent: #6d9cf0;
    --accent-text: #0b1426;
    --accent-soft: #1c2c4d;
    --on: #5fd184;
    --on-bg: #163523;
    --off: #9aa3b5;
    --off-bg: #252e40;
    --warn: #f0b451;
    --warn-bg: #3a2d12;
    --danger: #ff8a8f;
    --danger-bg: #3f1a1d;
    --flash: #3b3517;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100vh; display: flex; flex-direction: column;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text);
}
main { width: 100%; max-width: 1240px; margin: 0 auto; padding: 24px 16px 40px; flex: 1; }
h1 { font-size: 1.6rem; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 1.15rem; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
.nowrap { white-space: nowrap; }
code { font-size: 0.9em; background: var(--surface-2); padding: 1px 6px; border-radius: 4px; }
small { font-size: 0.75em; color: var(--muted); font-weight: 500; }

.icon { width: 1.1em; height: 1.1em; flex: none; vertical-align: -0.18em; }
p .icon, .small .icon { margin-right: 4px; }

/* ---------- barra superiore ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 10px 16px; background: var(--surface);
  border-bottom: 3px solid var(--brand-2); box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 12px; text-decoration: none; color: var(--text); min-width: 0; }
.logo { height: 38px; width: auto; display: block; }
.logo.client { height: 34px; }
.logo-plate { display: inline-flex; border-radius: 8px; }
.brand-sep { width: 1px; height: 30px; background: var(--border); }
.client-word { font-weight: 800; letter-spacing: 0.08em; color: var(--accent); font-size: 1.1rem; }
.topbar nav { display: flex; align-items: center; gap: 4px; }
.topbar nav a, .topbar nav .ghost {
  display: inline-flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 8px;
  color: var(--muted); text-decoration: none; font-weight: 500; border: 0; background: none;
}
.topbar nav a:hover, .topbar nav .ghost:hover { background: var(--surface-2); color: var(--text); }
.topbar nav a.active { color: var(--accent); background: var(--accent-soft); }
.who { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); padding: 0 8px; font-size: 0.9rem; }
.topbar form { margin: 0; }

/* ---------- controlli ---------- */
button, .button {
  font: inherit; font-weight: 500; padding: 8px 14px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  white-space: nowrap; min-height: 40px; transition: border-color .15s, background .15s;
}
button:hover:not(:disabled), .button:hover { border-color: var(--accent); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.big { min-height: 46px; font-size: 1rem; }
button:focus-visible, .button:focus-visible, input:focus-visible, select:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input, select {
  font: inherit; font-size: 16px; padding: 9px 12px; border-radius: 8px; min-height: 42px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); width: 100%;
}
input:disabled { background: var(--surface-2); color: var(--muted); }
label { display: flex; flex-direction: column; gap: 6px; font-size: 0.88rem; color: var(--muted); font-weight: 500; }

.search { position: relative; display: flex; align-items: center; }
.search .icon { position: absolute; left: 11px; color: var(--muted); pointer-events: none; }
.search input { padding-left: 36px; width: 260px; }

/* ---------- intestazione pagina ---------- */
.toolbar { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.toolbar p { margin: 4px 0 0; }
.toolbar .actions { display: flex; gap: 8px; }
.count { font-size: 0.95rem; color: var(--muted); font-weight: 500; }

/* ---------- riquadri riassuntivi / filtri ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat {
  display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto; column-gap: 12px;
  align-items: center; text-align: left; padding: 14px 16px; min-height: 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.stat .stat-icon { grid-row: span 2; }
.stat-n { font-size: 1.5rem; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat-l { color: var(--muted); font-size: 0.85rem; min-width: 0; overflow-wrap: anywhere; line-height: 1.2; }
.stat.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.stat-icon {
  width: 40px; height: 40px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent-soft); color: var(--accent);
}
.stat-icon .icon { width: 20px; height: 20px; }
.stat-icon.on { background: var(--on-bg); color: var(--on); }
.stat-icon.off { background: var(--off-bg); color: var(--off); }
.stat-icon.warn { background: var(--warn-bg); color: var(--warn); }
.stat-icon.dev { background: var(--danger-bg); color: var(--brand-2); }

/* ---------- tabella ---------- */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 600; background: var(--surface-2); white-space: nowrap; }
th .icon { width: 14px; height: 14px; margin-right: 2px; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--surface-2); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.num .val, td.num .sub { display: block; }
.val.big { font-size: 1.05rem; font-weight: 700; }
.sub { font-size: 0.78rem; color: var(--muted); min-height: 1em; }
tr.updated td { animation: flash 1.6s ease-out; }
tr[hidden] { display: none !important; }
@keyframes flash { from { background: var(--flash); } to { background: transparent; } }

.v-name { font-size: 0.98rem; }
.v-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.plate {
  display: inline-block; font-size: 0.75rem; font-weight: 600; letter-spacing: 0.04em;
  padding: 1px 7px; border: 1px solid var(--border); border-radius: 4px; background: var(--surface-2); color: var(--muted);
}
.tag {
  display: inline-flex; align-items: center; gap: 4px; font-size: 0.75rem; font-weight: 600;
  padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); margin-right: 4px;
}
.tag .icon { width: 12px; height: 12px; }
.tag.dev { background: var(--danger-bg); color: var(--brand-2); }

.pill {
  display: inline-flex; align-items: center; gap: 7px; padding: 3px 10px 3px 8px; border-radius: 999px;
  font-size: 0.82rem; font-weight: 600; background: var(--off-bg); color: var(--off);
}
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.pill.on { background: var(--on-bg); color: var(--on); }
.pill.on .dot { box-shadow: 0 0 0 0 currentColor; animation: pulse 2s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 color-mix(in srgb, currentColor 60%, transparent); } 70%, 100% { box-shadow: 0 0 0 6px transparent; } }
.state { display: flex; align-items: center; gap: 5px; color: var(--muted); margin-top: 5px; }
.state .icon { width: 14px; height: 14px; }

.row-actions { text-align: right; white-space: nowrap; }
.row-actions { display: table-cell; }
.icon-btn { padding: 6px 12px; min-height: 36px; font-size: 0.86rem; }
.icon-btn + .icon-btn { margin-left: 6px; }
.icon-btn.accent { color: var(--brand-2); border-color: color-mix(in srgb, var(--brand-2) 35%, var(--border)); }
.icon-btn.accent:hover { background: var(--danger-bg); border-color: var(--brand-2); }
.icon-btn.loading .icon { animation: spin 0.9s linear infinite; }
.icon-btn.error { color: var(--danger); border-color: var(--danger); }
@keyframes spin { to { transform: rotate(360deg); } }
.empty { text-align: center; color: var(--muted); padding: 32px 16px; margin: 0; }

/* ---------- messaggi ---------- */
.alert, .notice, .ok {
  display: flex; gap: 10px; align-items: flex-start; padding: 11px 14px; border-radius: 10px; margin: 12px 0; font-size: 0.92rem;
}
.alert .icon, .notice .icon, .ok .icon { margin-top: 2px; }
.alert { background: var(--danger-bg); color: var(--danger); }
.notice { background: var(--warn-bg); color: var(--warn); }
.ok { background: var(--on-bg); color: var(--on); }

/* ---------- login ---------- */
.login {
  max-width: 400px; margin: 8vh auto 0; background: var(--surface); padding: 32px 28px;
  border: 1px solid var(--border); border-top: 4px solid var(--brand-2); border-radius: 16px; box-shadow: var(--shadow);
}
.login-logos { display: flex; align-items: center; justify-content: center; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; padding: 12px; border-radius: 10px; background: #fff; }
.logo-lg { height: 56px; width: auto; max-width: 100%; }
.logo-lg.client { height: 48px; }
.login h1 { font-size: 1.35rem; text-align: center; }
.login > .muted { text-align: center; margin: 4px 0 0; }
.login form, dialog form { display: flex; flex-direction: column; gap: 14px; margin-top: 20px; }

/* ---------- dialog ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 16px; padding: 24px; width: min(480px, calc(100vw - 24px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 50px rgb(0 0 0 / 0.25);
}
dialog::backdrop { background: rgb(10 20 40 / 0.5); backdrop-filter: blur(2px); }
.dialog-head { display: flex; gap: 12px; align-items: center; }
.dialog-head p { margin: 2px 0 0; }
.edit-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.preview { margin: 0; font-size: 0.9rem; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ---------- admin ---------- */
.check-list td:first-child { width: 1%; }
.check-list input[type="checkbox"] { width: 22px; height: 22px; min-height: 0; accent-color: var(--brand-2); cursor: pointer; }
.v-label { flex-direction: row; flex-wrap: wrap; align-items: center; gap: 8px; color: var(--text); font-weight: 400; font-size: inherit; cursor: pointer; }
.sticky-save {
  position: sticky; bottom: 0; background: var(--bg); padding: 14px 0; margin-top: 4px;
  display: flex; gap: 12px; align-items: center; flex-wrap: wrap; border-top: 1px solid var(--border);
}
.log .details span { margin-right: 10px; word-break: break-all; }
.log .details b { color: var(--muted); font-weight: 600; }
.ev { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; font-size: 0.85rem; }
.ev.ok { color: var(--on); background: none; padding: 0; margin: 0; }
.ev.bad { color: var(--danger); }
.ev.warn { color: var(--warn); }
.ev.info { color: var(--accent); }

.foot { text-align: center; color: var(--muted); font-size: 0.78rem; padding: 16px; }

/* Loghi a colori leggibili anche in tema scuro */
@media (prefers-color-scheme: dark) {
  .logo-plate { background: #fff; padding: 3px 8px; }
}

/* ---------- tablet ---------- */
@media (max-width: 1024px) {
  .topbar nav a span, .topbar nav .ghost span { display: none; }
  .topbar nav a, .topbar nav .ghost { padding: 10px; }
  .row-actions .icon-btn span { display: none; }
  .icon-btn { padding: 8px 10px; min-height: 40px; min-width: 40px; }
}


/* ---------- tablet e telefono: le righe diventano schede ---------- */
@media (max-width: 900px) {
  .table-wrap { background: none; border: 0; box-shadow: none; overflow: visible; }
  table.units thead { display: none; }
  table.units, table.units tbody { display: block; }
  table.units tr {
    display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); min-width: 0; gap: 10px 12px; margin-bottom: 12px; padding: 14px;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  }
  table.units tbody tr:hover td { background: none; }
  table.units td { display: block; padding: 0; border: 0; text-align: left; min-width: 0; }
  table.units td.num .val { font-size: 0.95rem; overflow-wrap: anywhere; }
  table.units td.vehicle { grid-column: 1 / -1; }
  table.units td.status { grid-column: 1 / -1; display: flex; align-items: center; gap: 12px; margin-top: -4px; }
  table.units td.status .state { margin: 0; }
  table.units td.num::before {
    content: attr(data-label); display: block; font-size: 0.7rem; text-transform: uppercase;
    letter-spacing: 0.04em; color: var(--muted); font-weight: 600; margin-bottom: 2px;
  }
  table.units td.hours, table.units td.fuel { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; }
  table.units .row-actions { grid-column: 1 / -1; display: flex; gap: 8px; }
  table.units .row-actions .icon-btn { flex: 1; min-height: 44px; }
  table.units .row-actions .icon-btn span { display: inline; }
  table.units .icon-btn + .icon-btn { margin-left: 0; }
  table.units tr.updated td { animation: none; }
  table.units tr.updated { animation: flash 1.6s ease-out; }

}
@media (min-width: 561px) and (max-width: 900px) {
  table.units tbody { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  table.units tr { margin-bottom: 0; }
  table.units tr[hidden] { display: none; }
}

/* ---------- telefono ---------- */
@media (max-width: 760px) {
  main { padding: 16px 12px 32px; }
  h1 { font-size: 1.35rem; }
  .logo { height: 30px; }
  .logo.client { height: 26px; }
  .brand { gap: 8px; }
  .who { display: none; }
  .toolbar { align-items: stretch; flex-direction: column; gap: 12px; }
  .toolbar .actions { width: 100%; }
  .search { flex: 1; }
  .search input { width: 100%; }
  .stats { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .stat { padding: 10px 12px; column-gap: 10px; }
  .stat-icon { width: 34px; height: 34px; }
  .stat-n { font-size: 1.25rem; }

  .check-list th:nth-child(4), .check-list td:nth-child(4) { display: none; }
  .log th:nth-child(4), .log td:nth-child(4) { display: none; }
  .edit-grid { grid-template-columns: 1fr; }
  .dialog-actions { flex-direction: column-reverse; }
  .dialog-actions button { width: 100%; min-height: 46px; }
  dialog { padding: 20px 16px; }
  .login { margin-top: 4vh; padding: 24px 18px; }
}
@media (max-width: 380px) {
  table.units td.hours, table.units td.fuel { grid-column: 1 / -1; }
}

/* Modifica ore: attivo solo con mezzo acceso */
button.edit .i-off, button.edit:disabled .i-on { display: none; }
button.edit:disabled .i-off { display: inline-block; }
.icon-btn.accent:disabled { color: var(--muted); border-color: var(--border); background: var(--surface-2); opacity: 1; }

/* Storico modifiche ore */
.history .sub { display: block; }
.history .plate { margin-left: 6px; }
@media (max-width: 760px) {
  .log.history th:nth-child(4), .log.history td:nth-child(4) { display: table-cell; }
  .log.history th:nth-child(5), .log.history td:nth-child(5),
  .log.history th:nth-child(6), .log.history td:nth-child(6) { display: none; }
  .history .plate { display: none; }
}

/* Filtro gruppo */
#group { width: auto; max-width: 220px; cursor: pointer; }
@media (max-width: 760px) {
  .toolbar .actions { flex-wrap: wrap; }
  #group { max-width: none; flex: 1 1 100%; order: 3; }
}

/* "Acceso" con dato vecchio */
.ign-note { color: var(--warn); }
.ign-note:empty { display: none; }

/* Comandi per dispositivo (admin) */
.cmd-help code, .commands code { font-size: 0.85em; }
.commands .brand-row td { background: var(--surface-2); }
.commands td:first-child { min-width: 180px; }
.cmd-form { display: flex; gap: 8px; align-items: center; min-width: 320px; }
.cmd-form input { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 14px; min-height: 36px; }
.warn-text { color: var(--warn); font-weight: 600; }
@media (max-width: 760px) {
  .log.commands th:nth-child(2), .log.commands td:nth-child(2) { display: none; }
  .cmd-form { min-width: 0; }
}

/* Finestra storico del veicolo */
dialog.wide { width: min(860px, calc(100vw - 24px)); }
.history-link { margin: 14px 0 12px; }
#history-dialog .table-wrap { max-height: 60vh; overflow: auto; box-shadow: none; }
.unit-history .sub { display: block; }
#history-dialog .dialog-actions { margin-top: 14px; }
#history-filter select { width: auto; max-width: 280px; }
@media (max-width: 760px) {
  .unit-history th:nth-child(4), .unit-history td:nth-child(4),
  .unit-history th:nth-child(5), .unit-history td:nth-child(5) { display: none; }
  #history-filter select { max-width: none; flex: 1; }
}
.state:empty { display: none; }
