:root {
  --blue: #222d5c;
  --blue-2: #34437f;
  --blue-soft: #e8ebf5;
  --orange: #f89b0e;
  --orange-dark: #c97800;
  --orange-soft: #fff3df;
  --bg: #f4f6fa;
  --panel: #ffffff;
  --text: #1c2233;
  --muted: #5d6680;
  --line: #dde2ec;
  --red: #c62828;
  --red-soft: #fdecec;
  --green: #2e7d32;
  --green-soft: #e8f5e9;
  --amber-soft: #fff6e0;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 30, 60, .06), 0 2px 8px rgba(20, 30, 60, .05);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}
* { box-sizing: border-box; }
body { margin: 0; font-size: 15px; line-height: 1.45; background: var(--bg); }
a { color: var(--blue-2); }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; color: var(--blue); }
h2 { font-size: 1.15rem; margin: 0 0 .75rem; color: var(--blue); }
h3 { font-size: 1rem; margin: 0 0 .5rem; }
.muted { color: var(--muted); }
small, .small { font-size: .85rem; }
.lucide-icon { vertical-align: -3px; flex: none; }

.env-banner { background: var(--orange); color: #241500; text-align: center; font-weight: 700; padding: 6px 10px; font-size: .85rem; }

/* Barra in alto */
.topbar { background: var(--blue); color: #fff; display: flex; align-items: center; gap: 14px; padding: 8px 16px; position: sticky; top: 0; z-index: 20; }
.brand { display: flex; align-items: center; gap: 8px; color: #fff; text-decoration: none; flex: none; }
.brand img { height: 38px; display: block; }
.brand span { font-size: .75rem; text-transform: uppercase; letter-spacing: .08em; opacity: .75; border-left: 1px solid rgba(255,255,255,.3); padding-left: 8px; }
.mainnav { display: flex; gap: 2px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.mainnav::-webkit-scrollbar { display: none; }
.mainnav a { color: #dfe4f5; text-decoration: none; padding: 8px 10px; border-radius: 8px; white-space: nowrap; display: flex; gap: 6px; align-items: center; font-size: .92rem; }
.mainnav a:hover { background: rgba(255,255,255,.08); color: #fff; }
.mainnav a.active { background: rgba(248,155,14,.18); color: #fff; box-shadow: inset 0 -2px 0 var(--orange); }
.userbox { display: flex; align-items: center; gap: 8px; margin: 0; flex: none; }
.userbox span { display: flex; flex-direction: column; text-align: right; line-height: 1.15; font-size: .85rem; }
.userbox small { opacity: .7; font-size: .75rem; }
.userbox button { color: #fff; background: transparent; border-color: rgba(255,255,255,.35); padding: 7px 9px; }
.userbox button:hover { background: rgba(255,255,255,.12); }
.navmenu { position: relative; }
.navmenu > summary { list-style: none; cursor: pointer; color: #dfe4f5; padding: 8px 10px; border-radius: 8px; display: flex; gap: 6px; align-items: center; font-size: .92rem; white-space: nowrap; }
.navmenu > summary::-webkit-details-marker { display: none; }
.navmenu > summary:hover, .navmenu[open] > summary { background: rgba(255,255,255,.08); color: #fff; }
.navmenu > summary.active { background: rgba(248,155,14,.18); color: #fff; box-shadow: inset 0 -2px 0 var(--orange); }
.navmenu .menu { position: fixed; margin-top: 6px; background: #fff; border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.2); padding: 6px; min-width: 200px; z-index: 40; }
.navmenu .menu a { color: var(--text); display: flex; gap: 8px; padding: 8px 10px; border-radius: 6px; }
.navmenu .menu a:hover { background: var(--blue-soft); color: var(--blue); }

.page { max-width: 1280px; margin: 0 auto; padding: 20px 16px 80px; }

/* Pannelli e griglie */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 16px; }
.panel.narrow { max-width: 640px; margin-left: auto; margin-right: auto; }
.panel > h2:first-child { display: flex; align-items: center; gap: 8px; justify-content: space-between; flex-wrap: wrap; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 900px) { .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; } }
@media (min-width: 600px) and (max-width: 900px) { .grid-4 { grid-template-columns: repeat(2, 1fr); } }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head p { margin: 0; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.breadcrumb { font-size: .85rem; margin-bottom: 4px; }
.breadcrumb a { text-decoration: none; }

.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: .82rem; text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 1.6rem; font-weight: 700; color: var(--blue); }
.stat .sub { color: var(--muted); font-size: .85rem; }
.stat.alert-stat .value { color: var(--red); }

/* Pulsanti */
button, .button {
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
  background: var(--blue); color: #fff; border: 1px solid var(--blue); border-radius: 8px;
  padding: 8px 14px; font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; line-height: 1.2;
}
button:hover, .button:hover { background: var(--blue-2); }
.button.orange, button.orange { background: var(--orange); border-color: var(--orange); color: #241500; }
.button.orange:hover, button.orange:hover { background: var(--orange-dark); color: #fff; }
.button.ghost, button.ghost { background: transparent; color: var(--blue); border-color: var(--line); }
.button.ghost:hover, button.ghost:hover { background: var(--blue-soft); }
.button.danger, button.danger { background: #fff; color: var(--red); border-color: #f2b8b8; }
.button.danger:hover, button.danger:hover { background: var(--red-soft); }
.button.small, button.small { padding: 4px 9px; font-size: .85rem; }
button:disabled { opacity: .55; cursor: not-allowed; }
.inline-form { display: inline; margin: 0; }

/* Pulsanti grandi per l'operatore */
.big-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.big-action { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; min-height: 110px; border-radius: 14px; font-size: 1.1rem; font-weight: 700; text-decoration: none; color: #fff; background: var(--blue); box-shadow: var(--shadow); }
.big-action.orange { background: var(--orange); color: #241500; }
.big-action .lucide-icon { width: 34px; height: 34px; }

/* Moduli */
form.stack, .form-stack { display: grid; gap: 12px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px 14px; }
.form-grid .wide { grid-column: 1 / -1; }
label { display: grid; gap: 4px; font-size: .88rem; font-weight: 600; color: var(--muted); }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 500; color: var(--text); }
input, select, textarea {
  font: inherit; color: var(--text); background: #fff; border: 1px solid #c9d0de; border-radius: 8px; padding: 8px 10px; width: 100%; min-height: 40px;
}
input[type=checkbox], input[type=radio] { width: auto; min-height: 0; transform: scale(1.2); }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(248,155,14,.5); border-color: var(--orange); }
textarea { min-height: 70px; resize: vertical; }
.hint { font-weight: 400; color: var(--muted); font-size: .8rem; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 4px; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin: 0; }
legend { font-weight: 700; color: var(--blue); padding: 0 6px; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 12px; }
.filters label { min-width: 150px; }

/* Tabelle */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; background: #f8f9fc; white-space: nowrap; }
td.num, th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
tr.row-alert td { background: var(--red-soft); }
tr.row-warn td { background: var(--amber-soft); }
tr.total td { font-weight: 700; background: #f8f9fc; }
tbody tr:hover td { background: #fafbfe; }
.empty { color: var(--muted); padding: 18px; text-align: center; }

/* Etichette */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; font-size: .78rem; font-weight: 600; background: var(--blue-soft); color: var(--blue); white-space: nowrap; }
.badge.red { background: var(--red-soft); color: var(--red); }
.badge.green { background: var(--green-soft); color: var(--green); }
.badge.orange { background: var(--orange-soft); color: var(--orange-dark); }
.badge.grey { background: #eef0f4; color: var(--muted); }

/* Avvisi */
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; font-weight: 600; }
.flash-ok { background: var(--green-soft); color: var(--green); }
.flash-error { background: var(--red-soft); color: var(--red); }
.flash-warn { background: var(--amber-soft); color: #8a5a00; }
.alert { background: var(--red-soft); color: var(--red); padding: 10px 12px; border-radius: 8px; }
.alert-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.alert-item { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border-radius: 8px; border-left: 4px solid var(--orange); background: var(--amber-soft); text-decoration: none; color: inherit; }
.alert-item.critical { border-left-color: var(--red); background: var(--red-soft); }
.alert-item .lucide-icon { margin-top: 2px; }
.alert-item.critical .lucide-icon { color: var(--red); }
.alert-item .what { font-weight: 700; }
.alert-item .meta { font-size: .8rem; color: var(--muted); }

/* Schede */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; overflow-x: auto; }
.tabs a { padding: 8px 12px; text-decoration: none; color: var(--muted); border-bottom: 3px solid transparent; white-space: nowrap; font-weight: 600; }
.tabs a.active { color: var(--blue); border-bottom-color: var(--orange); }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
.kv dt { color: var(--muted); font-size: .88rem; }
.kv dd { margin: 0; font-weight: 600; }
.photo { width: 120px; height: 120px; object-fit: cover; border-radius: 12px; border: 1px solid var(--line); background: #eef0f4; }
.photo.placeholder { display: flex; align-items: center; justify-content: center; color: var(--muted); }
.head-card { display: flex; gap: 16px; align-items: flex-start; flex-wrap: wrap; }
.head-card .info { flex: 1; min-width: 240px; }
details.drawer { border: 1px dashed #c9d0de; border-radius: var(--radius); padding: 8px 12px; margin-bottom: 12px; background: #fbfcfe; }
details.drawer > summary { cursor: pointer; font-weight: 700; color: var(--blue); list-style: none; display: flex; align-items: center; gap: 6px; }
details.drawer[open] > summary { margin-bottom: 10px; }
.files { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.files li { display: flex; gap: 8px; align-items: center; justify-content: space-between; border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; }
.timeline { list-style: none; margin: 0; padding: 0; font-size: .88rem; }
.timeline li { padding: 6px 0; border-bottom: 1px dashed var(--line); }

/* Calendario */
.cal-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.calendar { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); border-top: 1px solid var(--line); border-left: 1px solid var(--line); }
.calendar .dow { background: #f8f9fc; font-size: .75rem; text-transform: uppercase; color: var(--muted); padding: 6px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); text-align: center; font-weight: 600; }
.calendar .day { min-height: 104px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 4px; background: #fff; overflow: hidden; }
.calendar .day.other { background: #f7f8fb; color: #a2a9bb; }
.calendar .day.today { box-shadow: inset 0 0 0 2px var(--orange); }
.calendar .num { font-size: .8rem; font-weight: 700; }
.cal-item { display: block; font-size: .74rem; padding: 2px 5px; border-radius: 5px; margin-top: 3px; text-decoration: none; color: var(--blue); background: var(--blue-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cal-item.gara { background: var(--orange-soft); color: var(--orange-dark); }
.cal-item.person { background: #ede7f6; color: #5e35b1; }
.cal-item.vehicle { background: #e0f2f1; color: #00695c; }
.cal-item.expired { background: var(--red-soft); color: var(--red); }
.cal-item.done { text-decoration: line-through; opacity: .6; }
.cal-list { display: none; }
@media (max-width: 700px) {
  .calendar { display: none; }
  .cal-list { display: block; }
}
.legend { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }

/* Griglia ingressi */
.intake-grid td { padding: 4px 6px; }
.intake-grid input, .intake-grid select { min-height: 36px; padding: 6px 8px; }
.intake-grid .tons { max-width: 120px; text-align: right; }

/* Login */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; background: linear-gradient(160deg, var(--blue) 0%, #1a2348 55%, #2b3a75 100%); }
.login-card { width: 100%; max-width: 400px; background: #fff; border-radius: 16px; padding: 28px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.login-card img { width: 100%; max-width: 300px; height: auto; margin-bottom: 12px; }
.login-card h1 { font-size: 1.25rem; }
.auth-password-field { position: relative; display: block; }
.auth-password-toggle { position: absolute; right: 4px; top: 4px; bottom: 4px; padding: 0 8px; background: transparent; color: var(--muted); border: 0; }
.auth-password-toggle:hover { background: var(--blue-soft); }

/* PWA */
.pwa-install-banner { position: fixed; left: 12px; right: 12px; bottom: 12px; z-index: 50; display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 12px; background: #fff; border: 1px solid var(--line); box-shadow: 0 8px 24px rgba(0,0,0,.15); max-width: 520px; margin: 0 auto; }
.pwa-install-banner[hidden] { display: none; }
.pwa-install-copy { flex: 1; display: grid; font-size: .85rem; }
.pwa-install-dismiss { background: transparent; color: var(--muted); border: 0; padding: 4px; }

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; padding: 8px 10px; gap: 8px; }
  .mainnav { order: 3; flex-basis: 100%; }
  .userbox { margin-left: auto; }
  .page { padding: 14px 10px 90px; }
  h1 { font-size: 1.3rem; }
  .panel { padding: 12px; }
  .kv { grid-template-columns: 1fr; gap: 2px; }
  .kv dd { margin-bottom: 6px; }
  .hide-mobile { display: none; }
}
@media print {
  .topbar, .no-print, .pwa-install-banner { display: none !important; }
  .page { padding: 0; }
  .panel { box-shadow: none; }
}
.auth .page { max-width: none; padding: 0; }
@media (max-width: 1400px) and (min-width: 761px) {
  .brand span { display: none; }
  .mainnav a, .navmenu > summary { padding: 8px 7px; gap: 4px; font-size: .88rem; }
  .userbox small { display: none; }
}
@media (max-width: 760px) {
  .brand span, .userbox span { display: none; }
  .brand img { height: 34px; }
}
