  :root{
    /* palette dal logo: rosso #e40000 e nero */
    --brand: #e40000;
    --brand-light: #fde9e9;
    --ink: #1a1a1a;
    --paper: #ffffff;
    --line: #d9d9d9;
    --muted: #757575;
  }
  *{box-sizing:border-box;}
  body{
    margin:0;
    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
    background: #ededed;
    color: var(--ink);
    padding: 24px 12px calc(100px + env(safe-area-inset-bottom));
  }
  /* tocco più reattivo, niente zoom da doppio tap sui controlli */
  button, .chip, input, select, textarea{ touch-action: manipulation; }
  .sheet{
    max-width: 900px;
    margin: 0 auto;
    background: var(--paper);
    border: 1px solid var(--line);
    padding: 28px 28px 40px;
  }
  header.brand{
    display:flex;
    justify-content: space-between;
    align-items: flex-start;
    border-bottom: 3px solid var(--brand);
    padding-bottom: 14px;
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 12px;
  }
  .brand img.logo{
    max-width: 280px;
    width: 100%;
    height: auto;
    display: block;
  }
  .brand .sub{
    font-family: inherit;
    font-size: 11px;
    color: var(--muted);
    margin-top: 8px;
    line-height: 1.5;
  }
  .num-box{
    text-align:right;
    font-family: inherit;
  }
  .num-box .lbl{
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--muted);
  }
  .num-box input{
    font-family: inherit;
    font-size: 20px;
    font-weight: bold;
    color: var(--brand);
    border: none;
    border-bottom: 1px solid var(--line);
    background: transparent;
    text-align: right;
    width: 140px;
    padding: 2px 0;
  }
  h2.title{
    text-align:center;
    font-size: 18px;
    letter-spacing: 2px;
    margin: 0 0 22px;
  }
  .row{
    display:flex;
    gap: 16px;
    margin-bottom: 14px;
    flex-wrap: wrap;
  }
  .field{
    flex: 1;
    min-width: 160px;
  }
  .field label{
    display:block;
    font-family: inherit;
    font-size: 10.5px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--muted);
    margin-bottom: 4px;
  }
  .field input, .field textarea, .field select{
    width: 100%;
    font-family: inherit;
    font-size: 16px; /* sotto i 16px iOS zooma la pagina al focus */
    padding: 10px 8px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--ink);
  }
  .field input:focus, .field textarea:focus, .field select:focus{
    outline: none;
    border-color: var(--brand);
  }
  /* Campi ricavati dall'anagrafica, non compilati dal tecnico (il dealer del
     cliente e il suo referente): si leggono e basta. Restano nel linguaggio
     dei normali campi del modulo — il rosso qui direbbe "errore". */
  .field input.derivato{
    background: #f4f4f4;
    color: #3d3d3d;
    font-weight: 600;
    cursor: default;
  }
  /* toccandoli non devono accendersi come un campo in compilazione */
  .field input.derivato:focus{ border-color: var(--line); }
  /* ==== avvisi e conferme in pagina (al posto di alert/confirm) ==== */
  .avvisi{
    position: fixed;
    z-index: 200;
    top: 0;
    left: 0;
    right: 0;
    padding: calc(8px + env(safe-area-inset-top)) 10px 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    /* la striscia non deve rubare i tocchi al modulo sotto */
    pointer-events: none;
  }
  .avviso{
    pointer-events: auto;
    width: 100%;
    max-width: 640px;
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 12px 12px 12px 14px;
    border-radius: 8px;
    border: 1px solid;
    box-shadow: 0 8px 24px rgba(0,0,0,.18);
    animation: avviso-entra .18s ease-out;
  }
  @keyframes avviso-entra{
    from{ transform: translateY(-8px); opacity: 0; }
    to{ transform: none; opacity: 1; }
  }
  @media (prefers-reduced-motion: reduce){
    .avviso{ animation: none; }
  }
  .avviso-errore{ background: var(--brand-light); border-color: var(--brand); color: #7a0000; }
  .avviso-ok{ background: #e8f6ec; border-color: #2e7d32; color: #1b5e20; }
  .avviso-testo{
    margin: 0;
    flex: 1;
    font-size: 15px;
    line-height: 1.35;
    font-weight: 600;
  }
  .avviso-chiudi{
    flex: none;
    /* area di tocco piena anche se il segno è piccolo */
    min-width: 32px;
    min-height: 32px;
    padding: 0;
    border: none;
    background: none;
    color: inherit;
    font-size: 16px;
    line-height: 1;
    opacity: .7;
  }
  .dlg-fondale{
    position: fixed;
    inset: 0;
    z-index: 210;
    background: rgba(0,0,0,.45);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
  }
  .dlg{
    width: 100%;
    max-width: 420px;
    background: var(--paper);
    border-radius: 10px;
    padding: 18px;
    box-shadow: 0 16px 48px rgba(0,0,0,.3);
  }
  /* riceve il focus solo per portarci il lettore di schermo: l'alone attorno
     al riquadro non serve, il pannello è già evidente da sé */
  .dlg:focus{ outline: none; }
  .dlg-titolo{ margin: 0 0 8px; font-size: 17px; }
  .dlg-testo{ margin: 0 0 8px; font-size: 15px; line-height: 1.4; color: var(--ink); }
  .dlg-avvertenza{
    margin: 0 0 8px;
    font-size: 14px;
    line-height: 1.4;
    font-weight: 600;
    color: var(--brand);
  }
  .dlg-azioni{
    display: flex;
    gap: 10px;
    margin-top: 16px;
  }
  /* pulsanti larghi e affiancati: si preme col pollice, senza mirare */
  .dlg-azioni button{
    flex: 1;
    min-height: 48px;
    font-size: 15px;
  }
  /* campo che ha causato l'errore, dopo lo scorrimento */
  .campo-errore{
    border-color: var(--brand) !important;
    /* !important anche qui: gli stili degli input sono più specifici */
    background: var(--brand-light) !important;
    animation: campo-pulsa 1.2s ease-out 1;
  }
  @keyframes campo-pulsa{
    0%, 100%{ box-shadow: none; }
    30%{ box-shadow: 0 0 0 4px rgba(228,0,0,.22); }
  }
  @media (prefers-reduced-motion: reduce){
    .campo-errore{ animation: none; }
  }

  /* autocomplete clienti/commesse (al posto del datalist, inutilizzabile su mobile) */
  .ac-wrap{ position: relative; }
  /* campo a scelta obbligata: il testo digitato non corrispondeva a nessuna voce */
  input.ac-invalid{ border-color: var(--brand); background: var(--brand-light); }
  .ac-avviso{
    margin: 4px 0 0;
    font-size: 12px;
    line-height: 1.3;
    color: var(--brand);
  }
  .ac-list{
    position: absolute;
    z-index: 50;
    left: 0;
    right: 0;
    top: calc(100% + 2px);
    margin: 0;
    padding: 4px 0;
    list-style: none;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 4px;
    box-shadow: 0 6px 20px rgba(0,0,0,.12);
    max-height: 220px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .ac-item{
    padding: 12px 10px;
    font-size: 16px;
    line-height: 1.3;
    cursor: pointer;
    border-bottom: 1px solid #f0f0f0;
  }
  .ac-item:last-child{ border-bottom: none; }
  .ac-item.active, .ac-item:active{ background: var(--brand-light); }
  fieldset{
    border: 1px solid var(--line);
    margin: 0 0 18px;
    padding: 12px 14px 16px;
  }
  legend{
    font-family: inherit;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--brand);
    padding: 0 6px;
  }
  .int-card{
    border: 1px solid var(--line);
    background: #fff;
    padding: 12px 14px 14px;
    margin-bottom: 12px;
  }
  .int-head{
    display:flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
  }
  .int-head .int-num{
    font-family: inherit;
    font-size: 11px;
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--brand);
  }
  .int-head .row-del{
    font-family: inherit;
    font-size: 13px;
    background: none;
    border: none;
    color: var(--muted);
    text-decoration: underline;
    cursor: pointer;
    padding: 10px;
    margin: -10px -10px -10px 0;
  }
  .int-head .row-del:hover{ color: var(--brand); }
  .chips{
    display:flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 12px;
  }
  .chip{
    display:inline-flex;
    align-items: center;
    gap: 7px;
    font-family: inherit;
    font-size: 13.5px;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 10px 16px 10px 12px;
    background: #fff;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
  }
  .chip input{
    width: 18px;
    height: 18px;
    margin: 0;
    accent-color: var(--brand);
    cursor: pointer;
  }
  .chip:has(input:checked){
    background: var(--brand);
    border-color: var(--brand);
    color: #fff;
  }
  .int-fields{
    display:flex;
    flex-wrap: wrap;
    gap: 14px 18px;
    align-items: flex-end;
  }
  .field.ore-viaggio{ flex: 0 1 100px; min-width: 84px; }
  .field.ore-viaggio input{
    background: #f2f2f2;
    font-weight: bold;
    text-align: center;
    pointer-events: none;
  }
  .int-fields .field.addebito{ flex: 0 1 170px; min-width: 150px; }
  .int-fields .field.matricola{ flex: 1 1 180px; min-width: 160px; }
  .tgroup .tlbl{
    display:block;
    font-family: inherit;
    font-size: 10.5px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--muted);
    margin-bottom: 4px;
  }
  .tgroup .trow{
    display:flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 5px;
    font-family: inherit;
    font-size: 11px;
    color: var(--muted);
  }
  .tgroup input{
    font-family: inherit;
    font-size: 16px;
    padding: 9px 4px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--ink);
  }
  .tgroup input:focus{ outline: none; border-color: var(--brand); }
  .tgroup input[type="time"]{ width: 104px; }
  .tgroup input.ore{
    width: 60px;
    text-align: center;
    background: #f2f2f2;
    font-weight: bold;
    pointer-events: none;
  }
  .add-row-btn{
    margin-top: 8px;
    font-family: inherit;
    font-size: 13px;
    background: none;
    border: 1px dashed var(--muted);
    color: var(--muted);
    padding: 12px 18px;
    cursor: pointer;
  }
  .add-row-btn:hover{ border-color: var(--brand); color: var(--brand); }
  textarea{
    display: block;
    width: 100%;
    min-height: 110px;
    resize: vertical;
    font-family: inherit;
    font-size: 16px;
    line-height: 1.5;
    padding: 10px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--ink);
  }
  textarea:focus{ outline: none; border-color: var(--brand); }
  .sig-block{
    display:flex;
    gap: 20px;
    flex-wrap: wrap;
  }
  .sig-unit{
    flex: 1;
    min-width: 260px;
  }
  .sig-unit .name{
    font-family: inherit;
    font-size: 12px;
    margin-bottom: 6px;
  }
  .sig-box{ position: relative; }
  canvas.sigpad{
    width: 100%;
    height: 150px;
    border: 1px solid var(--line);
    background: #fff;
    display:block;
    cursor: pointer;
  }
  .sig-hint{
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    color: var(--muted);
    pointer-events: none;
  }
  .sig-actions{
    margin-top: 6px;
    display:flex;
    justify-content: flex-end;
  }
  .sig-actions button{
    font-family: inherit;
    font-size: 13px;
    background: none;
    border: none;
    color: var(--muted);
    text-decoration: underline;
    cursor: pointer;
    padding: 12px 10px;
  }
  .sig-actions button:hover{ color: var(--brand); }
  /* lavagna firma a tutto schermo */
  .sig-modal{
    position: fixed;
    inset: 0;
    background: #fff;
    z-index: 100;
    display: none;
  }
  .sig-modal.open{ display: block; }
  .sig-modal-inner{
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: calc(14px + env(safe-area-inset-top)) calc(14px + env(safe-area-inset-right))
             calc(14px + env(safe-area-inset-bottom)) calc(14px + env(safe-area-inset-left));
  }
  /* col telefono in verticale la lavagna si ruota in orizzontale:
     rotazione solo visiva (CSS), le coordinate del tocco sono
     rimappate in signatures.js (modalPos) */
  @media (orientation: portrait){
    .sig-modal-inner{
      inset: auto;
      top: 50%;
      left: 50%;
      width: 100vh;
      height: 100vw;
      width: 100dvh;
      height: 100dvw;
      transform: translate(-50%, -50%) rotate(90deg);
    }
  }
  .sig-modal-head{
    display: flex;
    align-items: baseline;
    gap: 12px;
  }
  #sigModalTitle{
    font-weight: bold;
    font-size: 16px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--brand);
  }
  .sig-modal-sub{ font-size: 12px; color: var(--muted); }
  #sigModalCanvas{
    flex: 1;
    min-height: 0;
    width: 100%;
    display: block;
    border: 2px dashed var(--line);
    background: #fff;
    touch-action: none;
  }
  .sig-modal-actions{
    display: flex;
    justify-content: flex-end;
    gap: 10px;
  }
  .sig-modal-actions button{
    font-size: 14px;
    padding: 12px 22px;
    cursor: pointer;
  }
  .sig-modal-actions .btn-primary{ border: none; }
  .toolbar{
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(253,252,250,0.97);
    border-top: 1px solid var(--line);
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
    display:flex;
    justify-content: flex-end;
    align-items: center;
    gap: 10px;
    z-index: 10;
  }
  .toolbar .autosave-note{
    margin-right: auto;
    font-family: inherit;
    font-size: 11px;
    color: var(--muted);
  }
  .toolbar button{
    font-family: inherit;
    font-size: 14px;
    padding: 12px 20px;
    border: none;
    cursor: pointer;
  }
  .btn-primary{
    background: var(--brand);
    color: #fff;
  }
  .btn-secondary{
    background: transparent;
    border: 1px solid var(--muted);
    color: var(--ink);
  }
  .sync-bar{
    max-width: 900px;
    margin: 0 auto 8px;
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 12px;
    color: var(--muted);
  }
  .sync-bar .sync-user{ font-weight: 600; color: var(--ink); }
  .sync-bar .sync-stato.ok{ color: #2e7d32; }
  .sync-bar .sync-stato.attesa{ color: var(--muted); }
  .sync-bar .sync-stato.errore{ color: var(--brand); font-weight: 600; }
  .sync-bar button{
    margin-left: auto;
    background: transparent;
    border: 1px solid var(--line);
    color: var(--muted);
    font-family: inherit;
    font-size: 11px;
    padding: 4px 10px;
    cursor: pointer;
  }
  .login-inner{
    background: var(--paper);
    width: 100%;
    max-width: 360px;
    padding: 28px 26px;
    border-top: 4px solid var(--brand);
    display: flex;
    flex-direction: column;
    gap: 10px;
  }
  .login-inner img{ max-width: 200px; margin: 0 auto 4px; }
  .login-inner h3{ margin: 0; font-size: 17px; text-align: center; }
  .login-inner p{ margin: 0 0 6px; font-size: 12px; color: var(--muted); text-align: center; }
  .login-inner input{
    font-family: inherit;
    font-size: 15px;
    padding: 10px 12px;
    border: 1px solid var(--line);
  }
  .login-inner .login-err{
    min-height: 16px;
    font-size: 12px;
    color: var(--brand);
    text-align: center;
  }
  .login-inner button{
    font-family: inherit;
    font-size: 14px;
    padding: 12px 20px;
    border: none;
    cursor: pointer;
  }
  .login-inner .btn-secondary{ border: 1px solid var(--muted); }
  /* L'accesso e' una pagina a se, non piu' un velo sopra il rapportino:
     la stessa scatola, centrata su una pagina vuota. */
  .login-page{
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
  }
  .print-text{ display:none; }
  @page{ size: A4 portrait; margin: 10mm 12mm; }
  @media print{
    /* larghezza fissa pari all'area utile del foglio (A4 meno i margini):
       così telefono e PC impaginano allo stesso modo, senza rimpicciolire */
    html, body{ width: 186mm; }
    body{ background: #fff; padding: 0; }
    .sheet{ border: none; width: 100%; max-width: none; margin: 0; padding: 0; }
    .toolbar, .add-row-btn, .sig-actions, .row-del, .sig-hint, .sig-modal, .sync-bar{ display:none !important; }
    fieldset{
      break-inside: avoid;
      page-break-inside: avoid;
      margin-bottom: 8px;
      padding: 6px 12px 8px;
    }
    .row{ margin-bottom: 6px; }
    /* orari del viaggio tutti sulla stessa riga */
    #viaggiBody .row{ flex-wrap: nowrap; gap: 8px; }
    #viaggiBody .field{ min-width: 0; }
    .field label{ font-size: 9px; letter-spacing: 0.3px; white-space: nowrap; margin-bottom: 2px; }
    h2.title{ font-size: 14px; letter-spacing: 1.5px; margin-bottom: 8px; }
    .brand img.logo{ max-width: 180px; }
    .brand .sub{ font-size: 9px; margin-top: 4px; }
    header.brand{ padding-bottom: 8px; margin-bottom: 10px; }
    .num-box input{ font-size: 15px; }
    legend{ font-size: 10px; }
    .field input, .field select{ border-color: #bbb; padding: 5px 6px; font-size: 12px; }
    .field select{ -webkit-appearance: none; appearance: none; }
    /* niente freccette/icone dei picker nativi (Android le stampa enormi) */
    input[type="date"], input[type="time"]{ -webkit-appearance: none; appearance: none; }
    input[type="date"]::-webkit-calendar-picker-indicator,
    input[type="time"]::-webkit-calendar-picker-indicator{
      display: none;
      -webkit-appearance: none;
    }
    .int-card{
      break-inside: avoid;
      page-break-inside: avoid;
      padding: 8px 10px;
      margin-bottom: 8px;
    }
    .print-empty{ display:none !important; }
    .chips{ gap: 5px; margin-bottom: 8px; }
    .chip{ padding: 3px 9px 3px 7px; font-size: 10px; gap: 5px; border-radius: 999px; }
    .chip input{ width: 11px; height: 11px; }
    .chip:has(input:checked){
      background: var(--brand-light);
      border: 2px solid var(--brand);
      color: var(--ink);
      font-weight: bold;
      -webkit-print-color-adjust: exact;
      print-color-adjust: exact;
    }
    .tgroup input{ border-color: #bbb; padding: 4px; font-size: 12px; }
    .tgroup input[type="time"]{ width: 72px; }
    /* le textarea a stampa vengono sostituite dal div .print-text,
       che si espande sempre a tutto il contenuto senza tagliarlo */
    textarea{ display:none; }
    .print-text{
      display:block;
      font-family: inherit;
      font-size: 12px;
      white-space: pre-wrap;
      overflow-wrap: break-word;
      border: 1px solid #bbb;
      padding: 6px 8px;
      min-height: 40px;
      background: #fff;
    }
    canvas.sigpad{ border: 1px solid #999; height: 85px; }
    .sig-unit{ min-width: 200px; }
    .sig-unit .name{ font-size: 10px; margin-bottom: 3px; }
    .print-text{ min-height: 30px; }
  }
  @media screen and (max-width: 600px){
    body{ padding: 8px 4px calc(110px + env(safe-area-inset-bottom)); }
    .sheet{ padding: 16px 12px 28px; }
    .brand img.logo{ max-width: 210px; }
    .int-card{ padding: 10px 10px 12px; }
    .int-fields{ gap: 10px 14px; }
    .tgroup input[type="time"]{ width: 88px; }
    /* il totale ore viaggio va su una riga tutta sua, non accanto agli orari */
    .field.ore-viaggio{ flex: 1 1 100%; min-width: 100%; }
    .field.ore-viaggio input{ width: 96px; }
    canvas.sigpad{ height: 130px; }
  }
  @media screen and (max-width: 480px){
    .toolbar{ flex-wrap: wrap; }
    .toolbar .autosave-note{ flex: 1 1 100%; margin: 0 0 4px; text-align: center; }
    .toolbar button{ flex: 1; }
  }
