:root {
  --bg: #fbfaff;
  --surface: #ffffff;
  --surface2: #eef0f8;
  --text: #1b1b21;
  --muted: #5d5e6b;
  --outline: #8f909c;
  --outline-soft: #c7c8d4;
  --primary: #3a5ba9;
  --on-primary: #ffffff;
  --primary-soft: #dbe2ff;
  --error: #ba1a1a;
  --radius: 12px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f1f23;
    --surface: #2a2a30;
    --surface2: #33343b;
    --text: #e4e2e9;
    --muted: #a9a9b6;
    --outline: #8f909c;
    --outline-soft: #46464f;
    --primary: #b1c5ff;
    --on-primary: #10295f;
    --primary-soft: #2b3f73;
    --error: #ffb4ab;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; }

/* ---------------------------------------------------------------- struttura */
.bar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 4px;
  padding: calc(env(safe-area-inset-top) + 6px) calc(env(safe-area-inset-right) + 8px) 6px calc(env(safe-area-inset-left) + 8px);
  background: var(--bg);
  border-bottom: 1px solid var(--outline-soft);
  min-height: 56px;
}
.bar h1 {
  flex: 1; margin: 0 8px; font-size: 20px; font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.content {
  max-width: 720px; margin: 0 auto;
  padding: 8px calc(env(safe-area-inset-right) + 16px) 24px calc(env(safe-area-inset-left) + 16px);
  display: flex; flex-direction: column; gap: 12px;
}
.content.with-bottom { padding-bottom: calc(env(safe-area-inset-bottom) + 100px); }
.bottom {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  padding: 12px calc(env(safe-area-inset-right) + 12px) calc(env(safe-area-inset-bottom) + 12px) calc(env(safe-area-inset-left) + 12px);
  background: var(--surface2);
  display: flex; flex-direction: column; align-items: center; gap: 4px;
}
.bottom .btn.filled { width: 100%; max-width: 720px; height: 52px; font-size: 18px; }

/* ---------------------------------------------------------------- pulsanti */
.btn {
  border: 0; background: none; cursor: pointer;
  color: var(--primary); font-weight: 500;
  padding: 10px 12px; border-radius: 20px; min-height: 40px;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn:active { background: var(--primary-soft); }
.btn.filled { background: var(--primary); color: var(--on-primary); border-radius: 26px; }
.btn.filled:disabled { opacity: .45; }
.btn.outlined { border: 1px solid var(--outline); }
.btn:disabled { opacity: .45; }
.icon-btn {
  border: 0; background: none; cursor: pointer; color: var(--text);
  width: 44px; height: 44px; border-radius: 22px;
  display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.icon-btn:active { background: var(--surface2); }
.icon-btn svg { width: 24px; height: 24px; fill: currentColor; }

/* ---------------------------------------------------------------- elenco moduli */
.section { color: var(--primary); font-size: 17px; font-weight: 600; margin: 8px 0 0; }
.card {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: 0 1px 3px rgba(0,0,0,.18);
}
button.card { display: block; width: 100%; text-align: left; border: 0; padding: 16px; cursor: pointer; }
button.card:active { background: var(--surface2); }
.card .title { font-size: 17px; font-weight: 500; }
.card .sub { font-size: 13px; color: var(--muted); margin-top: 2px; }
.outlined-card { border: 1px solid var(--outline-soft); border-radius: var(--radius); padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.outlined-card .row-title { font-weight: 600; font-size: 15px; }

/* ---------------------------------------------------------------- campi */
.field { position: relative; display: block; width: 100%; }
.field input, .field textarea {
  width: 100%; font: inherit; font-size: 17px; color: var(--text);
  background: transparent;
  border: 1px solid var(--outline); border-radius: 6px;
  padding: 22px 14px 8px; min-height: 58px;
  outline: none; appearance: none; -webkit-appearance: none;
}
.field textarea { resize: vertical; min-height: 84px; display: block; }
.field input:focus, .field textarea:focus { border: 2px solid var(--primary); padding: 21px 13px 7px; }
.field label {
  position: absolute; left: 15px; top: 7px; font-size: 12px; color: var(--muted);
  pointer-events: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: calc(100% - 30px);
}
.field input:focus + label, .field textarea:focus + label { color: var(--primary); }
.field.has-trailing input { padding-right: 52px; }
.field .trailing { position: absolute; right: 6px; top: 7px; color: var(--muted); }
.pair { display: flex; gap: 8px; }
.pair > * { flex: 1; min-width: 0; }

.switch-card { background: var(--surface2); border-radius: var(--radius); padding: 14px 16px; display: flex; align-items: center; gap: 12px; }
.switch-card .lbl { flex: 1; }
.switch-card .lbl b { font-weight: 600; display: block; }
.switch-card .lbl small { font-size: 13px; color: var(--muted); }
.toggle { position: relative; width: 52px; height: 32px; flex: none; }
.toggle input { opacity: 0; width: 100%; height: 100%; margin: 0; position: absolute; z-index: 1; cursor: pointer; }
.toggle span { position: absolute; inset: 0; border-radius: 16px; border: 2px solid var(--outline); background: var(--surface2); transition: .15s; }
.toggle span::after { content: ""; position: absolute; left: 6px; top: 6px; width: 16px; height: 16px; border-radius: 50%; background: var(--outline); transition: .15s; }
.toggle input:checked + span { background: var(--primary); border-color: var(--primary); }
.toggle input:checked + span::after { left: 22px; top: 2px; width: 24px; height: 24px; background: var(--on-primary); }

.check { display: flex; align-items: center; gap: 12px; padding: 6px 0; cursor: pointer; }
.check input, .radio input { width: 22px; height: 22px; accent-color: var(--primary); margin: 0; flex: none; }
.choice .lbl { font-size: 15px; }
.choice .opts { display: flex; flex-wrap: wrap; column-gap: 18px; }
.radio { display: flex; align-items: center; gap: 8px; padding: 8px 0; cursor: pointer; }

.info { font-size: 13px; color: var(--muted); }
.hint { font-size: 13px; color: var(--muted); }

.picklist .item { display: flex; align-items: center; border: 1px solid var(--outline-soft); border-radius: var(--radius); padding-left: 12px; margin-bottom: 4px; }
.picklist .item span { flex: 1; font-size: 15px; }
.picklist select {
  font: inherit; font-size: 16px; color: var(--primary); font-weight: 500;
  border: 1px solid var(--outline); border-radius: 20px; padding: 9px 14px; background: transparent;
  appearance: none; -webkit-appearance: none; max-width: 100%;
}

.date-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.date-btn { position: relative; }
.date-btn input[type=date] { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; }

.firma-lbl { font-weight: 600; font-size: 15px; }
.firma-box {
  height: 110px; width: 100%; border-radius: 8px; border: 1px solid var(--outline);
  background: #fff; display: flex; align-items: center; justify-content: center; cursor: pointer; color: #777;
}
.firma-box img { max-width: calc(100% - 16px); max-height: calc(100% - 16px); }

.step { display: flex; align-items: center; gap: 8px; }
.step b { width: 20px; color: var(--muted); }
.step.done b { color: var(--primary); }
.step.done span { color: var(--muted); }
.error { color: var(--error); }

/* ---------------------------------------------------------------- finestre */
.scrim { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; padding: 24px; }
.dialog {
  background: var(--surface); border-radius: 24px; padding: 22px 22px 10px;
  width: 100%; max-width: 420px; max-height: 85vh; overflow: auto;
}
.dialog h2 { margin: 0 0 12px; font-size: 22px; font-weight: 500; }
.dialog .body { white-space: pre-wrap; color: var(--muted); font-size: 15px; }
.dialog .actions { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 2px; margin-top: 16px; }

.full {
  position: fixed; inset: 0; z-index: 60; background: var(--surface);
  display: flex; flex-direction: column; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 12px) calc(env(safe-area-inset-right) + 12px) calc(env(safe-area-inset-bottom) + 8px) calc(env(safe-area-inset-left) + 12px);
}
.full h2 { margin: 0; font-size: 18px; font-weight: 500; }
.full .actions { display: flex; align-items: center; gap: 4px; }
.full .actions .grow { flex: 1; }
.pad { flex: 1; position: relative; min-height: 0; }
.pad canvas { position: absolute; inset: 0; width: 100%; height: 100%; background: #fff; border: 1px solid #999; border-radius: 8px; touch-action: none; }
.rotate-hint { font-size: 13px; color: var(--muted); }
@media (orientation: landscape) { .rotate-hint { display: none; } }

#scanner-view { flex: 1; min-height: 0; overflow: hidden; border-radius: 8px; background: #000; }
#scanner-view video { width: 100% !important; height: 100% !important; object-fit: cover; }

#toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 96px); transform: translateX(-50%);
  background: #323232; color: #fff; padding: 12px 18px; border-radius: 8px; font-size: 15px;
  max-width: calc(100% - 32px); z-index: 100; opacity: 0; pointer-events: none; transition: opacity .2s;
}
#toast.show { opacity: 1; }

.progress { height: 4px; border-radius: 2px; background: var(--surface2); overflow: hidden; }
.spinner { width: 22px; height: 22px; border: 3px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
