:root {
  color-scheme: light;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

* { box-sizing: border-box; }

body { margin: 0; background: #f1f5f9; color: #0f172a; }
header { background: #0f172a; color: #fff; padding: 1.2rem; }

main {
  max-width: 960px;
  margin: 1rem auto;
  padding: 0 1rem 2rem;
  display: grid;
  gap: 1rem;
}

form, section {
  background: #fff;
  border-radius: 12px;
  padding: 1rem;
  box-shadow: 0 2px 8px rgb(15 23 42 / 10%);
}

label { display: block; margin-bottom: 0.75rem; font-weight: 600; }

input, textarea {
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.5rem;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  font: inherit;
}

#numero { font-weight: 700; background: #f8fafc; }

#causale_scelta {
  width: 100%;
  margin-top: 0.35rem;
  margin-bottom: 0.35rem;
  padding: 0.5rem;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  font: inherit;
  background: #ffffff;
}

.rows-header {
  margin-top: 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.righe-head,
.riga-row {
  display: grid;
  grid-template-columns: 1.4fr 2.2fr 1fr 0.9fr;
  gap: 0.5rem;
}

.righe-head {
  margin-top: 0.75rem;
  padding: 0.5rem;
  border: 1px solid #cbd5e1;
  border-bottom: 0;
  border-radius: 10px 10px 0 0;
  background: #f8fafc;
}

.righe-grid {
  border: 1px solid #cbd5e1;
  border-radius: 0 0 10px 10px;
  padding: 0.5rem;
  display: grid;
  gap: 0.4rem;
}

.field-with-actions { display: grid; }
.qty-wrap { grid-template-columns: 1fr auto; gap: 0.35rem; align-items: start; }
.qty-wrap .error-message { grid-column: 1 / -1; }

/* Senza un minimo, su desktop il pulsante "Rimuovi riga" schiaccia l'input
   quantita' fino a lasciare visibili solo le freccette. */
.qty-wrap input { min-width: 4.5rem; }

.actions, li { display: flex; gap: 0.5rem; align-items: center; }
.actions { margin-top: 1rem; flex-wrap: wrap; }
.item-buttons { display: flex; gap: 0.4rem; flex-wrap: wrap; }


.save-status {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: max-content;
  max-width: min(90vw, 380px);
  min-width: 220px;
  min-height: 56px;
  margin: 0;
  white-space: nowrap;
  padding: 0.85rem 1.1rem;
  border-radius: 12px;
  color: #fff;
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  box-shadow: 0 10px 24px rgb(15 23 42 / 18%);
  opacity: 0;
  pointer-events: none;
  z-index: 2000;
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.save-status.is-success,
.save-status.is-error {
  opacity: 1;
  transform: translate(-50%, -50%);
}

.save-status.is-success {
  background: #16a34a;
}

.save-status.is-error {
  background: #dc2626;
}

button {
  border: 0;
  border-radius: 8px;
  padding: 0.55rem 0.8rem;
  cursor: pointer;
  background: #2563eb;
  color: #fff;
}

button.secondary { background: #475569; }
button.danger { background: #dc2626; }
button:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.save-spinner {
  display: inline-block;
  width: 0.9em;
  height: 0.9em;
  border: 2px solid rgb(255 255 255 / 50%);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -0.12em;
  margin-right: 0.4rem;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.error-message { color: #b91c1c; display: block; min-height: 1rem; margin-top: 0.2rem; font-size: 0.8rem; }
.obbligatorio { color: #b91c1c; }
.input-error { border-color: #ef4444; box-shadow: 0 0 0 1px #ef4444 inset; }

ul { list-style: none; padding: 0; margin: 0; }
li {
  justify-content: space-between;
  padding: 0.5rem;
  border-bottom: 1px solid #e2e8f0;
  flex-wrap: wrap;
}

@media (max-width: 720px) {
  .righe-head { display: none; }
  .riga-row {
    grid-template-columns: 1fr;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    padding: 0.5rem;
  }
}

@media print {
  body > * { display: none !important; }
}


.ocr-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.35rem;
}
.ocr-document-card {
  margin-top: 0.9rem;
  padding: 0.85rem;
  border: 1px solid #93c5fd;
  border-radius: 10px;
  background: #eff6ff;
}
.ocr-title {
  margin: 0;
  font-size: 1rem;
  color: #1e3a8a;
}
.ocr-subtitle {
  margin: 0.25rem 0 0.45rem;
  font-size: 0.88rem;
  color: #1e40af;
}
.ocr-row-tools {
  margin-top: 0.4rem;
  padding: 0.45rem 0.5rem;
  border: 1px dashed #cbd5e1;
  border-radius: 8px;
  background: #f8fafc;
}
.ocr-row-title {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 700;
  color: #334155;
}
.ocr-row-subtitle {
  margin: 0.15rem 0 0.2rem;
  font-size: 0.75rem;
  color: #64748b;
}
.ocr-status { font-size: 0.9rem; color: #334155; min-height: 1.2rem; margin: 0.4rem 0; }
.ocr-preview-wrap { position: relative; display: inline-block; max-width: 100%; margin-bottom: 0.5rem; }
.ocr-preview { display: block; max-width: 100%; max-height: 220px; border-radius: 8px; border: 1px solid #cbd5e1; }
.ocr-lettura {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  border-radius: 8px;
  background: rgb(15 23 42 / 62%);
  color: #fff;
  font-weight: 600;
  font-size: 1rem;
  text-align: center;
}
.ocr-lettura[hidden] { display: none !important; }
.ocr-lettura-spinner {
  width: 2.2rem;
  height: 2.2rem;
  border: 3px solid rgb(255 255 255 / 35%);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@media (max-width: 720px) {
  .ocr-actions button { width: 100%; }
  .ocr-document-card { padding: 0.75rem; }
  .ocr-preview { max-height: 320px; }
  .ocr-lettura { font-size: 0.9rem; gap: 0.45rem; padding: 0.6rem; }
  .ocr-lettura-spinner { width: 1.8rem; height: 1.8rem; }
}

.description-wrap input { min-width: 220px; }
@media (max-width: 720px) {
  .description-wrap input { min-width: 0; }
}



.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgb(15 23 42 / 55%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 1500;
}

.modal-overlay[hidden] {
  display: none !important;
}

.modal-content {
  width: min(100%, 760px);
  max-height: 90vh;
  overflow-y: auto;
  background: #fff;
  border-radius: 14px;
  padding: 1rem;
  box-shadow: 0 12px 28px rgb(15 23 42 / 25%);
}

#firma-canvas {
  display: block;
  width: 100%;
  min-height: 240px;
  height: clamp(240px, 40vh, 320px);
  margin-top: 0.75rem;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  background: #fff;
  touch-action: none;
}

.modal-actions {
  margin-top: 0.85rem;
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  flex-wrap: wrap;
  position: sticky;
  bottom: 0;
  background: #fff;
  padding-top: 0.5rem;
}

@media (max-width: 720px) {
  .modal-content {
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    padding: 0.85rem;
  }

  #firma-canvas {
    min-height: 220px;
    height: 42vh;
  }
}


.firma-preview-wrapper {
  margin-top: 0.75rem;
  padding: 0.65rem;
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  background: #f8fafc;
}

.firma-preview-label {
  margin: 0 0 0.35rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: #334155;
}

.firma-preview {
  display: block;
  width: 100%;
  max-width: 320px;
  height: auto;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #fff;
}

@media (max-width: 720px) {
  .firma-preview {
    max-width: 100%;
  }
}

/* --- Login e sessione --- */

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: #0f172a;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

/* display esplicito nelle regole sopra: l'attributo hidden va riaffermato,
   altrimenti il foglio di stile lo sovrasta e l'overlay resta visibile. */
.login-screen[hidden],
.user-bar[hidden] { display: none; }

body.login-attivo { overflow: hidden; }

.login-box {
  background: #fff;
  border-radius: 12px;
  padding: 1.5rem;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 8px 30px rgb(0 0 0 / 35%);
}

.login-box h2 { margin: 0 0 0.25rem; }

.login-sottotitolo { margin: 0 0 1rem; color: #475569; font-size: 0.9rem; }

.login-box button {
  width: 100%;
  margin-top: 0.5rem;
  padding: 0.65rem;
  border: 0;
  border-radius: 8px;
  background: #0f172a;
  color: #fff;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.login-box button.secondary { background: #e2e8f0; color: #0f172a; font-weight: 600; }

.login-errore { margin: 0.75rem 0 0; color: #b91c1c; font-weight: 600; font-size: 0.9rem; }

#serie-opzioni { display: grid; gap: 0.6rem; }

.serie-scelta {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  align-items: flex-start;
  text-align: left;
  padding: 0.75rem !important;
}

.serie-scelta span { font-weight: 400; font-size: 0.85rem; opacity: 0.85; }

.user-bar {
  margin-top: 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  font-size: 0.9rem;
}

.user-bar #user-info { font-weight: 600; margin-right: auto; }

.user-bar select {
  padding: 0.3rem 0.5rem;
  border-radius: 6px;
  border: 1px solid #334155;
  background: #1e293b;
  color: #fff;
  font: inherit;
  font-size: 0.85rem;
}

.user-bar button {
  padding: 0.35rem 0.7rem;
  border: 1px solid #334155;
  border-radius: 6px;
  background: #1e293b;
  color: #fff;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}

.user-bar button:hover { background: #334155; }

/* --- Archivio: toolbar, chip e stato --- */

.archivio-toolbar { display: grid; gap: 0.5rem; margin-bottom: 0.75rem; }

#archivio-cerca {
  margin-top: 0;
  padding: 0.55rem 0.75rem;
  border-radius: 8px;
}

.archivio-chips { display: flex; gap: 0.4rem; flex-wrap: wrap; }

.chip {
  padding: 0.35rem 0.8rem;
  border: 1px solid #cbd5e1;
  border-radius: 999px;
  background: #fff;
  color: #0f172a;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}

.chip.attivo {
  background: #0f172a;
  border-color: #0f172a;
  color: #fff;
  font-weight: 600;
}

.archivio-stato {
  margin: 0.75rem 0 0;
  text-align: center;
  color: #475569;
  font-size: 0.85rem;
}

.archivio-stato .link {
  border: 0;
  background: none;
  padding: 0;
  color: #0f172a;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  text-decoration: underline;
  cursor: pointer;
}

/* Toast di attesa: resta visibile con lo spinner finche' l'operazione dura. */
.save-status.is-pending {
  opacity: 1;
  transform: translate(-50%, -50%);
  background: #0f172a;
}

/* --- Conferma foto documento: anteprima + rotazione manuale --- */

.ocr-conferma {
  margin-top: 0.6rem;
  padding: 0.75rem;
  border: 1px solid #93c5fd;
  border-radius: 10px;
  background: #eff6ff;
}

.ocr-conferma[hidden] { display: none; }

.ocr-conferma-testo {
  margin: 0 0 0.6rem;
  font-size: 0.88rem;
  color: #1e40af;
}

/* Rotellina dello stato OCR: variante scura per fondo chiaro. */
.ocr-spinner {
  display: inline-block;
  width: 0.95em;
  height: 0.95em;
  border: 2px solid rgb(51 65 85 / 30%);
  border-top-color: #334155;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -0.15em;
  margin-right: 0.45rem;
}
