:root {
  --bg: #f3f5f8;
  --card: #ffffff;
  --text: #14202c;
  --muted: #5d6b7a;
  --line: #e1e6ec;
  --brand: #0f2742;
  --brand-2: #1b3d63;
  --accent: #f5a800;
  --accent-ink: #1d1400;
  --ok: #17864a;
  --ok-bg: #e3f5ea;
  --warn: #b45f06;
  --warn-bg: #fff1dc;
  --err: #c62828;
  --err-bg: #fde6e6;
  --info: #1560b8;
  --info-bg: #e3eefb;
  --grey-bg: #eceff3;
  --shadow: 0 1px 2px rgba(15, 39, 66, .06), 0 4px 14px rgba(15, 39, 66, .06);
  --radius: 14px;
  --nav-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c141d;
    --card: #15212e;
    --text: #e8eef5;
    --muted: #93a3b5;
    --line: #243345;
    --brand: #0a1c30;
    --brand-2: #1c3b5e;
    --ok-bg: #12301f;
    --warn-bg: #3a2a10;
    --err-bg: #3a1616;
    --info-bg: #13283f;
    --grey-bg: #1e2b3a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--info); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 18px; }
h3 { font-size: 15px; margin: 18px 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
p { margin: 6px 0; }
.ic { width: 20px; height: 20px; flex: none; vertical-align: middle; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }

/* ---------- Shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--brand); color: #fff;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  display: flex; align-items: center; gap: 10px;
}
.topbar .logo { width: 32px; height: 32px; border-radius: 9px; background: #fff; overflow: hidden; color: var(--accent-ink); display: grid; place-items: center; flex: none; }
.topbar .logo .ic { width: 20px; height: 20px; fill: var(--accent-ink); stroke: none; }
.topbar h1 { font-size: 17px; font-weight: 650; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--brand-2); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 13px; border: 0; }
.mode-pill { font-size: 11px; background: rgba(255,255,255,.14); padding: 3px 8px; border-radius: 99px; white-space: nowrap; }

main { max-width: 980px; margin: 0 auto; padding: 14px 16px calc(var(--nav-h) + env(safe-area-inset-bottom) + 24px); }

.bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  background: var(--card); border-top: 1px solid var(--line);
  display: grid; grid-template-columns: repeat(6, 1fr);
  padding-bottom: env(safe-area-inset-bottom);
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
}
.bottomnav a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; color: var(--muted); font-size: 11px; position: relative; }
.bottomnav a.active { color: var(--brand); font-weight: 650; }
@media (prefers-color-scheme: dark) { .bottomnav a.active { color: var(--accent); } }
.bottomnav a .dot { position: absolute; top: 8px; left: calc(50% + 6px); min-width: 17px; height: 17px; border-radius: 9px; background: var(--err); color: #fff; font-size: 10px; display: grid; place-items: center; padding: 0 4px; font-weight: 700; }

/* ---------- Cards / lists ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; margin-bottom: 12px; }
.card.err { background: var(--err-bg); color: var(--err); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar .grow { flex: 1; min-width: 160px; }
.list { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; margin-bottom: 12px; }
.li { display: flex; gap: 10px; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--line); color: inherit; }
.li:last-child { border-bottom: 0; }
a.li:active, .li.tap:active { background: var(--grey-bg); }
.li-main { flex: 1; min-width: 0; }
.li-main b { font-weight: 620; }
.li-title { font-weight: 620; overflow: hidden; text-overflow: ellipsis; }
.li-side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex: none; }
.li .ic-round { width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center; background: var(--grey-bg); color: var(--brand); flex: none; }
@media (prefers-color-scheme: dark) { .li .ic-round { color: var(--accent); } }
.empty { text-align: center; color: var(--muted); padding: 28px 16px; }

.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 12px; }
@media (min-width: 700px) { .stats { grid-template-columns: repeat(4, 1fr); } }
.stat { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 14px; color: inherit; display: block; }
.stat .v { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .l { font-size: 12px; color: var(--muted); }

.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
@media (min-width: 700px) { .tiles { grid-template-columns: repeat(5, 1fr); } }
.tile { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 8px; display: flex; flex-direction: column; align-items: center; gap: 8px; color: var(--text); font-size: 13px; text-align: center; }
.tile .ic { width: 26px; height: 26px; color: var(--brand); }
@media (prefers-color-scheme: dark) { .tile .ic { color: var(--accent); } }

.tabs { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 12px; padding-bottom: 2px; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs a, .tabs button { flex: none; padding: 7px 13px; border-radius: 99px; background: var(--card); color: var(--muted); font-size: 13px; border: 1px solid var(--line); font-weight: 550; cursor: pointer; font-family: inherit; }
.tabs .on { background: var(--brand); color: #fff; border-color: var(--brand); }

.kv { display: grid; grid-template-columns: 130px 1fr; gap: 6px 12px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 11.5px; font-weight: 650; white-space: nowrap; background: var(--grey-bg); color: var(--muted); }
.b-ok { background: var(--ok-bg); color: var(--ok); }
.b-warn { background: var(--warn-bg); color: var(--warn); }
.b-err { background: var(--err-bg); color: var(--err); }
.b-info { background: var(--info-bg); color: var(--info); }

/* ---------- Buttons / forms ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 14px; border-radius: 11px; border: 1px solid var(--line); background: var(--card); color: var(--text); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; min-height: 42px; }
.btn:disabled { opacity: .5; }
.btn.primary { background: var(--brand); color: #fff; border-color: var(--brand); }
@media (prefers-color-scheme: dark) { .btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); } }
.btn.accent { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { background: var(--err); color: #fff; border-color: var(--err); }
.btn.ok { background: var(--ok); color: #fff; border-color: var(--ok); }
.btn.ghost { background: transparent; }
.btn.danger.ghost { color: var(--err); }
.btn.small { padding: 6px 10px; min-height: 34px; font-size: 13px; }
.btn.big { width: 100%; padding: 16px; font-size: 17px; border-radius: 14px; }
.btn .ic { width: 18px; height: 18px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.icon-btn { border: 0; background: transparent; color: inherit; padding: 6px; border-radius: 8px; cursor: pointer; font-size: 16px; line-height: 1; }
.fab { position: fixed; right: 18px; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px); z-index: 15; width: 56px; height: 56px; border-radius: 18px; background: var(--accent); color: var(--accent-ink); border: 0; display: grid; place-items: center; box-shadow: 0 6px 18px rgba(0,0,0,.2); cursor: pointer; }
.fab .ic { width: 26px; height: 26px; }

.field { margin-bottom: 12px; }
.field label, .label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; font-weight: 550; }
input, select, textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); color: var(--text); font: inherit; font-size: 16px; min-height: 42px;
}
textarea { min-height: 84px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type=checkbox] { width: 20px; height: 20px; min-height: 0; accent-color: var(--brand); }
input[type=range] { padding: 0; accent-color: var(--accent); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.checks { display: flex; flex-direction: column; gap: 2px; max-height: 220px; overflow: auto; border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; }
.checks label { display: flex; align-items: center; gap: 10px; padding: 6px 0; color: var(--text); font-size: 15px; margin: 0; }
.search { position: relative; }
.search .ic { position: absolute; left: 11px; top: 11px; color: var(--muted); }
.search input { padding-left: 36px; }

/* ---------- Photos ---------- */
.photos { display: flex; flex-wrap: wrap; gap: 8px; }
.ph { position: relative; width: 76px; height: 76px; border-radius: 10px; overflow: hidden; background: var(--grey-bg); }
.ph img, img.thumb { width: 100%; height: 100%; object-fit: cover; display: block; cursor: zoom-in; }
.ph .pdf, a.pdf { width: 100%; height: 100%; display: grid; place-items: center; font-weight: 700; color: var(--err); background: var(--err-bg); }
.ph-x { position: absolute; top: 3px; right: 3px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; font-size: 11px; cursor: pointer; }
.ph.add { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; border: 2px dashed var(--line); background: transparent; color: var(--muted); font-size: 12px; cursor: pointer; margin: 0; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); gap: 8px; }
.gallery > * { aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: var(--grey-bg); }
.lightbox { position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.92); display: grid; place-items: center; padding: env(safe-area-inset-top) 10px env(safe-area-inset-bottom); }
.lightbox img { max-width: 100%; max-height: 92vh; }

/* ---------- Modal ---------- */
.modal-wrap { position: fixed; inset: 0; z-index: 50; background: rgba(8, 18, 30, .55); display: flex; align-items: flex-end; justify-content: center; }
@media (min-width: 700px) { .modal-wrap { align-items: center; } }
.modal { background: var(--bg); width: 100%; max-width: 560px; max-height: 94vh; border-radius: 18px 18px 0 0; display: flex; flex-direction: column; margin: 0; }
.modal.wide { max-width: 820px; }
@media (min-width: 700px) { .modal { border-radius: 18px; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.modal-body { padding: 14px 16px; overflow: auto; flex: 1; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); flex-wrap: wrap; }
.modal-foot .spacer { flex: 1; }

.toast-wrap, #toast { position: fixed; left: 50%; transform: translateX(-50%); top: calc(env(safe-area-inset-top) + 64px); z-index: 200; display: flex; flex-direction: column; gap: 6px; width: min(92vw, 420px); pointer-events: none; }
.toast { background: var(--ok); color: #fff; padding: 11px 14px; border-radius: 12px; box-shadow: var(--shadow); font-weight: 550; animation: tin .2s ease-out; }
.toast.warn { background: var(--warn); }
.toast.err { background: var(--err); }
@keyframes tin { from { opacity: 0; transform: translateY(-6px); } }

.loading { display: grid; place-items: center; padding: 60px 0; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Presenze ---------- */
.punch { text-align: center; padding: 18px 14px; }
.punch .state { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 650; }
.punch .big-time { font-size: 34px; font-weight: 750; font-variant-numeric: tabular-nums; margin: 4px 0; }
.punch.on { border: 2px solid var(--ok); }
.gps-box { display: flex; gap: 8px; align-items: flex-start; padding: 10px 12px; border-radius: 10px; background: var(--info-bg); color: var(--info); margin-bottom: 12px; font-size: 14px; }
.gps-box.ok { background: var(--ok-bg); color: var(--ok); }
.gps-box.warn { background: var(--warn-bg); color: var(--warn); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.t { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.t th, table.t td { padding: 8px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
table.t th { color: var(--muted); font-weight: 600; font-size: 12px; white-space: nowrap; }
table.t td.n, table.t th.n { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.t tfoot td { font-weight: 700; border-bottom: 0; }

/* ---------- Righe articoli ---------- */
.riga { display: grid; grid-template-columns: 1fr 1fr 1fr 34px; gap: 6px; padding: 8px; border: 1px solid var(--line); border-radius: 10px; margin-bottom: 6px; background: var(--card); }
.riga .r-desc { grid-column: 1 / 4; }
.riga .r-del { grid-column: 4; grid-row: 1; align-self: center; }
.riga input { min-height: 38px; padding: 7px 9px; }
.righe[data-prezzi="0"] .riga { grid-template-columns: 1fr 1fr 1fr 34px; }
@media (min-width: 700px) {
  .riga { grid-template-columns: 3fr 1.2fr .8fr .7fr 1fr 34px; }
  .riga .r-desc { grid-column: auto; }
  .riga .r-del { grid-column: auto; grid-row: auto; }
  .righe[data-prezzi="0"] .riga { grid-template-columns: 3fr 1.2fr .8fr .7fr 34px; }
}
.righe-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 4px; }

/* ---------- Gantt ---------- */
.gantt { font-size: 13px; }
.g-scale { position: relative; height: 20px; margin-left: 0; border-bottom: 1px solid var(--line); margin-bottom: 6px; }
.g-scale span { position: absolute; top: 0; font-size: 11px; color: var(--muted); transform: translateX(2px); white-space: nowrap; }
.g-scale span::before { content: ""; position: absolute; left: -2px; top: 0; height: 20px; border-left: 1px solid var(--line); }
.g-row { padding: 6px 0; border-bottom: 1px dashed var(--line); cursor: pointer; }
.g-label { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.g-label b { font-weight: 600; }
.g-track { position: relative; height: 16px; background: var(--grey-bg); border-radius: 6px; }
.g-bar { position: absolute; top: 0; height: 16px; border-radius: 6px; background: #9bb6d6; overflow: hidden; min-width: 4px; }
.g-prog { height: 100%; background: var(--brand-2); }
.g-bar.done .g-prog { background: var(--ok); }
.g-bar.late { outline: 2px solid var(--err); }
.g-today { position: absolute; top: -4px; bottom: -4px; width: 2px; background: var(--accent); z-index: 2; }
.progress { height: 6px; background: var(--grey-bg); border-radius: 3px; overflow: hidden; margin-top: 6px; }
.progress > div { height: 100%; background: var(--accent); }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: var(--brand); }
.login .box { width: 100%; max-width: 400px; background: var(--card); border-radius: 20px; padding: 22px; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.login .brand { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.login .brand .logo { width: 46px; height: 46px; border-radius: 13px; background: #fff; overflow: hidden; box-shadow: 0 0 0 1px var(--line); display: grid; place-items: center; }
.login .brand .logo .ic { width: 28px; height: 28px; fill: var(--accent-ink); stroke: none; }
.user-pick { display: flex; flex-direction: column; gap: 8px; }
.user-pick button { text-align: left; justify-content: flex-start; }
.boot-err { padding: 30px; color: var(--err); }

@media print {
  .topbar, .bottomnav, .fab, .no-print, .toolbar button, .tabs { display: none !important; }
  main { padding: 0; max-width: none; }
  body { background: #fff; color: #000; }
  .card, .list { box-shadow: none; border: 1px solid #ccc; break-inside: avoid; }
}
.logo img { width: 100%; height: 100%; object-fit: contain; display: block; }
