:root {
  --bg: #0f1419;
  --panel: #1a2129;
  --panel-2: #222b35;
  --line: #2e3945;
  --text: #e9eef3;
  --muted: #8b98a6;
  --accent: #3b82f6;
  --ok: #16a34a;
  --ok-soft: #0f3a22;
  --err: #dc2626;
  --err-soft: #3d1414;
  --warn: #f59e0b;
  --warn-soft: #3a2a08;
  --exit: #64748b;
  --radius: 18px;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; border: 0; }

.tnum { font-variant-numeric: tabular-nums; }

/* ---------- Postazione ---------- */
.kiosk {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  user-select: none;
}

.kiosk header, .kiosk footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 24px;
  color: var(--muted);
}

.kiosk header .clock { font-size: clamp(28px, 5vw, 44px); color: var(--text); font-weight: 600; }
.kiosk header .date { font-size: 18px; text-transform: capitalize; }
.kiosk footer { font-size: 14px; border-top: 1px solid var(--line); }

.screen { display: none; padding: 16px 24px 24px; min-height: 0; overflow-y: auto; }
.screen.active { display: flex; flex-direction: column; }

/* attesa badge */
#idle { align-items: center; justify-content: center; text-align: center; gap: 28px; }
.badge-icon {
  width: 180px; height: 180px; border-radius: 50%;
  display: grid; place-items: center;
  background: radial-gradient(circle, #1e3a5f 0%, var(--panel) 70%);
  animation: pulse 2.4s ease-in-out infinite;
}
.badge-icon svg { width: 96px; height: 96px; }
@keyframes pulse { 0%, 100% { transform: scale(1); opacity: .9; } 50% { transform: scale(1.06); opacity: 1; } }
#idle h1 { font-size: clamp(30px, 5vw, 52px); margin: 0; font-weight: 600; }
#idle p { margin: 0; color: var(--muted); font-size: 20px; }

.link-btn { background: var(--panel-2); padding: 14px 22px; border-radius: 12px; font-size: 18px; }

/* dipendente */
#dip { gap: 18px; }
.dip-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; }
.dip-head h2 { font-size: clamp(30px, 5vw, 46px); margin: 0; }
.chip { display: inline-block; padding: 6px 14px; border-radius: 999px; font-size: 16px; font-weight: 600; margin-top: 8px; }
.chip.in { background: var(--ok-soft); color: #4ade80; }
.chip.out { background: var(--panel-2); color: var(--muted); }
.chip.pausa { background: var(--warn-soft); color: var(--warn); }
.ore { text-align: right; color: var(--muted); }
.ore b { display: block; font-size: 34px; color: var(--text); }

.incorso {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px 22px; display: grid; gap: 4px;
}
.incorso .label { color: var(--muted); font-size: 15px; text-transform: uppercase; letter-spacing: .06em; }
.incorso .what { font-size: 26px; font-weight: 600; }
.incorso .sub { color: var(--muted); font-size: 18px; }

.scan-hint {
  border: 2px dashed var(--accent); border-radius: var(--radius); padding: 18px;
  text-align: center; font-size: 22px; color: #93c5fd;
}

.actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; margin-top: auto; }
.act {
  min-height: 110px; border-radius: var(--radius); font-size: 26px; font-weight: 700;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  transition: transform .08s, filter .15s;
}
.act small { font-size: 15px; font-weight: 500; opacity: .85; max-width: 90%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.act:active { transform: scale(.97); filter: brightness(1.15); }
.act.entrata { background: var(--ok); }
.act.uscita { background: var(--exit); }
.act.pausa { background: var(--warn); color: #1c1300; }
.act.riprendi { background: var(--accent); }
.act.annulla { background: var(--panel-2); color: var(--muted); font-size: 20px; min-height: 70px; }
.act.viaggio { background: #0e7490; }
.act.arrivo { background: var(--accent); }

/* tablet fuori sede: scelta del cantiere */
#cant-dlg[open] { width: min(760px, 96vw); max-height: 92dvh; display: flex; flex-direction: column; gap: 12px; }
#cant-dlg h3 { margin: 0; font-size: 26px; }
#cant-cerca { font: inherit; font-size: 20px; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
#cant-lista { overflow-y: auto; display: grid; gap: 8px; min-height: 0; }
.cant {
  text-align: left; background: var(--panel-2); border-radius: 14px; padding: 14px 16px;
  display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; align-items: center;
}
.cant:active { filter: brightness(1.2); }
.cant b { font-size: 20px; }
.cant .dove { color: var(--muted); font-size: 16px; grid-column: 1; }
.cant .km { grid-row: 1 / span 2; grid-column: 2; font-size: 18px; color: #93c5fd; white-space: nowrap; }
.cant.speciale { background: #0e7490; }
.cant.speciale .dove { color: #cffafe; }
#cant-nota { color: var(--muted); font-size: 16px; }
#gps.ok { color: #4ade80; }
#gps.no { color: var(--warn); }

/* esito */
#esito { align-items: center; justify-content: center; text-align: center; gap: 22px; border-radius: var(--radius); margin: 0 24px 24px; }
#esito.ok { background: var(--ok-soft); }
#esito.err { background: var(--err-soft); }
#esito .big { font-size: 120px; line-height: 1; }
#esito .msg { font-size: clamp(26px, 4vw, 40px); font-weight: 600; max-width: 900px; }
#esito .door { font-size: 22px; color: var(--warn); }

@media (max-width: 600px) {
  .kiosk header, .kiosk footer { padding: 10px 16px; }
  .screen { padding: 10px 16px 16px; }
  #dip { gap: 12px; }
  .ore { text-align: left; }
  .ore b { display: inline; margin-left: 8px; font-size: 24px; }
  .incorso { padding: 12px 16px; }
  .incorso .what { font-size: 22px; }
  .scan-hint { font-size: 17px; padding: 12px; }
  .actions { grid-template-columns: 1fr 1fr; gap: 10px; }
  .act { min-height: 80px; font-size: 22px; }
  .act.annulla { min-height: 56px; grid-column: 1 / -1; }
  #esito { margin: 0 16px 16px; }
  #esito .big { font-size: 80px; }
}

.timer-bar { height: 5px; background: var(--accent); width: 100%; transform-origin: left; }

/* tastierino */
dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 22px; width: min(420px, 92vw);
}
dialog::backdrop { background: rgba(0,0,0,.6); }
.pad-display { font-size: 40px; text-align: center; padding: 10px; background: var(--bg); border-radius: 12px; min-height: 66px; letter-spacing: .2em; }
.pad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 14px; }
.pad button { background: var(--panel-2); border-radius: 12px; font-size: 30px; padding: 16px 0; }
.pad button.ok { background: var(--accent); }

video#cam { width: 100%; border-radius: 12px; background: #000; }
/* il riquadro è in percentuale del video: le stesse percentuali vengono usate nel codice (MIRINO in kiosk.js) */
.cam-box { position: relative; line-height: 0; overflow: hidden; border-radius: 12px; }
.cam-box video#cam { display: block; height: auto; }
.mirino {
  position: absolute; left: 10%; top: 35%; width: 80%; height: 30%;
  border: 4px solid rgba(255,255,255,.9); border-radius: 10px;
  box-shadow: 0 0 0 9999px rgba(0,0,0,.45);   /* scurisce tutto quello che è fuori dal riquadro */
  transition: border-color .15s;
}
.mirino.ok { border-color: #22c55e; box-shadow: 0 0 0 9999px rgba(0,0,0,.45), inset 0 0 0 3px rgba(34,197,94,.5); }
.mirino span {
  position: absolute; left: 0; right: 0; top: calc(100% + 8px); text-align: center; line-height: 1.3;
  color: #fff; font-size: 18px; font-weight: 600; text-shadow: 0 1px 3px #000;
}
.mirino.ok span { color: #86efac; }

.capture { position: absolute; opacity: 0; pointer-events: none; width: 1px; height: 1px; }

/* ---------- Presenti ---------- */
.page { max-width: 1100px; margin: 0 auto; padding: 24px 16px; }
.page h1 { margin: 0 0 4px; }
.page .meta { color: var(--muted); margin-bottom: 20px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px; display: grid; gap: 6px; }
.card .n { font-size: 20px; font-weight: 600; display: flex; justify-content: space-between; gap: 8px; }
.card .w { color: var(--muted); font-size: 15px; }
.card.out { opacity: .55; }
.counts { display: flex; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.counts div { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 16px; }
.counts b { font-size: 26px; display: block; }

/* ---------- Ufficio (tema chiaro, stampabile) ---------- */
/* le pagine dell'ufficio crescono con il contenuto: senza questo, sotto la prima schermata si vedeva lo sfondo scuro dei tablet */
html:has(body.office) { background: #f4f6f8; }
body.office { height: auto; min-height: 100%; }
body.office {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --panel-2: #eef1f4;
  --line: #dde3e9;
  --text: #17202a;
  --muted: #5f6b78;
  --accent: #2563eb;
  --ok: #15803d;
  --ok-soft: #e7f6ec;
  --err: #b91c1c;
  --err-soft: #fdecec;
  --warn: #b45309;
  --warn-soft: #fff4e0;
  color-scheme: light;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}
.office a { color: var(--accent); }
.nav {
  display: flex; gap: 4px; align-items: center; flex-wrap: wrap;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.nav .brand { font-weight: 700; margin-right: 16px; }
.nav a { text-decoration: none; color: var(--muted); padding: 8px 12px; border-radius: 8px; }
.nav a.on { background: var(--panel-2); color: var(--text); font-weight: 600; }
.nav .spacer { flex: 1; }

.office .page { max-width: 1280px; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin: 4px 0 18px; }
.office input, .office select, .office textarea {
  font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; min-height: 38px;
}
.office textarea { width: 100%; min-height: 60px; }
.btn { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 14px; min-height: 38px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--err); }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: 13px; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; margin-bottom: 18px; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; }
.tile span { color: var(--muted); font-size: 13px; }
.tile b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
.tile.neg b { color: var(--err); }
.tile.pos b { color: var(--ok); }

.table-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; }
table.t { border-collapse: collapse; width: 100%; min-width: 860px; }
table.t th, table.t td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
table.t th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: var(--panel-2); position: sticky; top: 0; }
table.t td.n, table.t th.n { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.t tr.click { cursor: pointer; }
table.t tr.click:hover td { background: #f0f5ff; }
table.t tr.off td { color: var(--muted); background: #fafbfc; }
table.t tr.anom td:first-child { box-shadow: inset 4px 0 0 var(--err); }
table.t tr.oggi td { background: #f2f7ff; }
table.t tfoot td { font-weight: 700; background: var(--panel-2); }
.neg { color: var(--err); }
.pos { color: var(--ok); }

.pill { display: inline-block; padding: 1px 7px; border-radius: 6px; font-size: 12.5px; margin: 1px 2px 1px 0; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pill.e { background: var(--ok-soft); color: var(--ok); }
.pill.u { background: var(--panel-2); color: var(--muted); }
.pill.l { background: #e8efff; color: #1d4ed8; }
.pill.p { background: var(--warn-soft); color: var(--warn); }
.pill.g { background: #f3e8ff; color: #7e22ce; }
.pill.x { text-decoration: line-through; opacity: .55; }
.anom-txt { color: var(--err); font-size: 13px; }

.drawer { position: fixed; inset: 0 0 0 auto; width: min(560px, 100vw); background: var(--panel); border-left: 1px solid var(--line);
  box-shadow: -12px 0 30px rgba(0,0,0,.12); padding: 20px; overflow-y: auto; z-index: 10; }
.drawer h2 { margin: 0 0 4px; }
.drawer h3 { margin: 22px 0 8px; font-size: 15px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.drawer .close { position: absolute; right: 14px; top: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
.row label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.list-item { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; padding: 8px 0; border-bottom: 1px solid var(--line); }
.list-item .meta-s { color: var(--muted); font-size: 12.5px; }
.msg-err { color: var(--err); min-height: 1.2em; }
.empty { color: var(--muted); padding: 24px; text-align: center; }

@media print {
  .nav, .toolbar, .drawer, .no-print { display: none !important; }
  body.office { background: #fff; }
  .table-wrap { border: 0; overflow: visible; }
  table.t { min-width: 0; font-size: 11px; }
  table.t th { position: static; }
}

/* utenti in sola consultazione: niente comandi di modifica */
body.sola-lettura form, body.sola-lettura .drawer .btn.small, body.sola-lettura #costi-lav button,
body.sola-lettura #costi-dip button, body.sola-lettura #dip button, body.sola-lettura #agg button, body.sola-lettura .solo-modifica { display: none !important; }
.chiave { font-family: Consolas, "Cascadia Mono", monospace; background: var(--panel-2); padding: 10px; border-radius: 8px; word-break: break-all; user-select: all; }

/* ---------- grafici (scheda dipendente, statistiche) ---------- */
.sezione { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; margin-bottom: 16px; }
.sezione h2 { font-size: 16px; margin: 0 0 10px; }
.sezione .nota { color: var(--muted); font-size: 13px; margin: -4px 0 10px; }
.due { display: grid; grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); gap: 16px; }
.due > .sezione { margin-bottom: 0; }
.barre { display: flex; align-items: flex-end; gap: 2px; height: 170px; border-bottom: 1px solid var(--line); position: relative; }
.barre .b { flex: 1; min-width: 0; height: 100%; display: flex; align-items: flex-end; cursor: default; }
.barre .b i { display: block; width: 100%; height: var(--v); min-height: 1px; background: var(--accent); border-radius: 4px 4px 0 0; }
.barre .b.tenue i { background: #c7d2e0; }
.barre .b:hover i { filter: brightness(.85); }
.barre-x { display: flex; gap: 2px; font-size: 11px; color: var(--muted); margin-top: 4px; }
.barre-x span { flex: 1; min-width: 0; text-align: center; white-space: nowrap; overflow: visible; }
.suggerimento { position: fixed; pointer-events: none; background: var(--text); color: #fff; font-size: 12.5px; padding: 6px 9px; border-radius: 6px; z-index: 20; white-space: nowrap; }
.hbar { height: 8px; background: var(--panel-2); border-radius: 4px; min-width: 80px; }
.hbar span { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
table.t.compatta { min-width: 0; }
.tile small { display: block; color: var(--muted); font-size: 12px; }
a.card { color: inherit; text-decoration: none; cursor: pointer; }
a.card:hover { border-color: var(--accent); }
.ora-ora { font-size: 18px; font-weight: 600; margin: 4px 0 10px; }
@media print { .sezione { break-inside: avoid; } }
