:root {
  --bg: #f3f2ee;
  --surface: #ffffff;
  --surface-2: #f8f7f4;
  --ink: #1b2130;
  --ink-2: #485064;
  --muted: #7a8192;
  --line: #e2e0d9;
  --line-2: #cfccc2;
  --accent: #dd0000;   /* rosso Artec, dal logo */
  --accent-hover: #b30000;
  --accent-soft: #fde8e8;
  --ok: #1b7a4c;
  --ok-soft: #e1f2e8;
  --warn: #9a5c00;
  --warn-soft: #fff1d1;
  --danger: #a3141c;
  --danger-soft: #fbe6e7;
  --zone: #e0a100;
  --zone-fill: rgba(255, 200, 30, 0.2);
  --field-fill: rgba(221, 0, 0, 0.06);
  --field-focus: rgba(221, 0, 0, 0.12);
  --field-line: rgba(221, 0, 0, 0.4);
  --radius: 12px;
  --radius-sm: 7px;
  --shadow: 0 1px 2px rgba(20, 24, 35, 0.06), 0 6px 20px rgba(20, 24, 35, 0.06);
  --shadow-lg: 0 10px 40px rgba(20, 24, 35, 0.18);
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { line-height: 1.25; margin: 0; letter-spacing: -0.01em; }
h1 { font-size: 22px; font-weight: 650; }
h2 { font-size: 17px; font-weight: 650; }
h3 { font-size: 15px; font-weight: 600; }
p { margin: 0; }
a { color: var(--accent); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: var(--mono); font-size: 12px; word-break: break-all; }
.grow { flex: 1; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.stack { display: grid; gap: 14px; }

/* ── bottoni e campi ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px; padding: 0 16px;
  border: 1px solid var(--line-2); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--ink);
  font: inherit; font-weight: 550; text-decoration: none; white-space: nowrap;
  cursor: pointer; transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { background: var(--surface-2); border-color: var(--muted); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.danger { color: var(--danger); border-color: transparent; background: transparent; }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: rgba(0, 0, 0, .05); }
.btn.small { min-height: 32px; padding: 0 11px; font-size: 13.5px; }
.btn.icon { width: 40px; padding: 0; }
.btn.small.icon { width: 32px; }
.btn.big { min-height: 48px; padding: 0 22px; font-size: 16px; }

label.field { display: grid; gap: 6px; font-size: 13.5px; font-weight: 550; color: var(--ink-2); }
input[type=text], input[type=email], input[type=password], input[type=search], textarea, select {
  width: 100%; min-height: 40px; padding: 8px 11px;
  border: 1px solid var(--line-2); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--ink); font: inherit;
}
textarea { min-height: 90px; resize: vertical; }
input:focus-visible, textarea:focus-visible, select:focus-visible, .btn:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 35%, transparent); outline-offset: 1px;
}
.check { display: flex; gap: 10px; align-items: flex-start; font-size: 14px; color: var(--ink-2); cursor: pointer; }
.check input { width: 18px; height: 18px; margin: 2px 0 0; accent-color: var(--accent); }

/* ── contenitori ── */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px;
}
.card > header { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.wrap { max-width: 1080px; margin: 0 auto; padding: 24px 16px 60px; }

.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 14px;
  min-height: 56px; padding: 8px 16px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(10px); -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: -0.01em; }
.brand-logo { display: flex; align-items: center; padding-right: 12px; border-right: 1px solid var(--line-2); }
.brand-logo img { display: block; height: 32px; width: auto; }
.brand-name { font-size: 19px; font-weight: 700; letter-spacing: -0.02em; color: var(--ink); }
@media (max-width: 640px) { .brand-logo img { height: 26px; } .brand-name { font-size: 17px; } }

.badge {
  display: inline-flex; align-items: center; gap: 6px; height: 24px; padding: 0 9px;
  border-radius: 99px; font-size: 12.5px; font-weight: 600; white-space: nowrap;
  background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--line);
}
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; opacity: .8; }
.badge.draft { color: var(--muted); }
.badge.open { color: var(--accent); background: var(--accent-soft); border-color: transparent; }
.badge.completed { color: var(--ok); background: var(--ok-soft); border-color: transparent; }
.badge.warn { color: var(--warn); background: var(--warn-soft); border-color: transparent; }
.badge.plain::before { display: none; }

.notice { padding: 12px 14px; border-radius: var(--radius-sm); background: var(--warn-soft); color: var(--warn); font-size: 14px; }
.notice.ok { background: var(--ok-soft); color: var(--ok); }
.notice.err { background: var(--danger-soft); color: var(--danger); }

.toast {
  position: fixed; left: 50%; bottom: 24px; z-index: 100; transform: translateX(-50%);
  max-width: min(92vw, 520px); padding: 12px 18px; border-radius: 10px;
  background: var(--ink); color: #fff; box-shadow: var(--shadow-lg); font-size: 14.5px;
  animation: toast-in .2s ease-out;
}
.toast.err { background: var(--danger); }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ── dialog ── */
dialog {
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  padding: 0; border: 0; border-radius: 16px; background: var(--surface); color: var(--ink);
  box-shadow: var(--shadow-lg);
}
dialog::backdrop { background: rgba(18, 22, 32, 0.45); }
.dialog-body { display: grid; gap: 16px; padding: 22px; }
.dialog-head { display: flex; align-items: center; gap: 10px; }
.dialog-head h2 { flex: 1; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 10px; flex-wrap: wrap; }

.tabs { display: flex; gap: 4px; padding: 4px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; }
.tabs button {
  flex: 1; min-height: 36px; border: 0; border-radius: 7px; background: transparent;
  font: inherit; font-weight: 600; color: var(--ink-2); cursor: pointer;
}
.tabs button[aria-selected=true] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.1); }

.spinner {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2.5px solid currentColor; border-right-color: transparent;
  animation: spin .7s linear infinite; display: inline-block; vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

.progress { height: 6px; border-radius: 99px; background: var(--line); overflow: hidden; }
.progress > i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: inherit; transition: width .25s; }
.progress.ok > i { background: var(--ok); }

/* ── visualizzatore PDF ── */
.pdf-pages { display: flex; flex-direction: column; align-items: center; gap: 18px; padding: 18px 0 40px; overflow-x: auto; }
.pdf-page {
  position: relative; flex: none; background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.12), 0 6px 24px rgba(0,0,0,.07);
  --pt: 1;
}
.pdf-page canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.pdf-page .page-no {
  position: absolute; left: 0; top: -17px; font-size: 11.5px; color: var(--muted); font-weight: 600;
}
.pdf-overlay { position: absolute; inset: 0; }
.viewer-status { display: grid; place-items: center; gap: 12px; min-height: 260px; color: var(--muted); text-align: center; }
.viewer-status .progress { width: 220px; }

/* zone di firma */
.zone {
  position: absolute; display: flex; align-items: center; justify-content: center;
  border: 2px dashed var(--zone); border-radius: 4px; background: var(--zone-fill);
  color: #6b4b00; font-weight: 650; font-size: clamp(9px, calc(var(--pt) * 10px), 15px);
  text-align: center; line-height: 1.15; cursor: pointer; user-select: none; -webkit-user-select: none;
  touch-action: manipulation;
}
.zone .zone-label { padding: 2px 4px; overflow: hidden; max-height: 100%; }
.zone.optional { border-color: #c9b98f; background: rgba(240, 220, 160, .18); color: #7a6a42; }
.zone.signed { border: 1.5px solid transparent; background: transparent; }
.zone.signed:hover { border-color: rgba(27, 122, 76, .5); background: rgba(27, 122, 76, .05); }
.zone img { position: absolute; left: 5%; top: 5%; width: 90%; height: 90%; object-fit: contain; pointer-events: none; }
.zone .zone-remove {
  position: absolute; top: -11px; right: -11px; width: 24px; height: 24px; border-radius: 50%;
  border: 0; background: var(--ink); color: #fff; font-size: 15px; line-height: 1; cursor: pointer;
  display: none; place-items: center; box-shadow: 0 2px 6px rgba(0,0,0,.25);
}
.zone.signed .zone-remove { display: grid; }
.zone.flash { animation: flash 1.2s ease-out 2; }
@keyframes flash { 0%, 100% { box-shadow: 0 0 0 0 rgba(224,161,0,0); } 30% { box-shadow: 0 0 0 8px rgba(224,161,0,.45); } }

/* campi compilabili (lato cliente) */
.pdf-overlay .pdf-field {
  position: absolute; margin: 0; padding: 0 calc(var(--pt) * 2px); border: 0; border-radius: 2px;
  background: var(--field-fill); color: #000; outline: none;
  font-family: Helvetica, Arial, sans-serif; font-size: calc(var(--pt) * var(--fs, 10) * 1px); line-height: 1.15;
  min-height: 0; box-shadow: inset 0 -1px 0 var(--field-line);
}
.pdf-overlay .pdf-field:hover { background: var(--field-focus); }
.pdf-overlay .pdf-field:focus { background: #fff; box-shadow: 0 0 0 2px var(--accent); z-index: 2; }
.pdf-overlay .pdf-field:disabled { background: transparent; box-shadow: none; color: #000; -webkit-text-fill-color: #000; opacity: 1; }
.pdf-overlay textarea.pdf-field { resize: none; padding-top: calc(var(--pt) * 2px); }
.pdf-overlay select.pdf-field { appearance: auto; padding: 0; }
.pdf-overlay select.pdf-field[multiple] { overflow: auto; }
.pdf-overlay .pdf-field.comb { font-family: var(--mono); }
.pdf-overlay .pdf-toggle {
  position: absolute; display: grid; place-items: center; padding: 0; border: 0; cursor: pointer;
  background: var(--field-fill); box-shadow: inset 0 0 0 1px var(--field-line); color: #000;
  font-size: calc(var(--pt) * var(--fs, 10) * 1px); line-height: 1;
}
.pdf-overlay .pdf-toggle.radio { border-radius: 50%; }
.pdf-overlay .pdf-toggle:hover { background: var(--field-focus); }
.pdf-overlay .pdf-toggle:focus-visible { box-shadow: 0 0 0 2px var(--accent); }
.pdf-overlay .pdf-toggle[aria-checked=true]::after { content: "✓"; font-weight: 700; }
.pdf-overlay .pdf-toggle.radio[aria-checked=true]::after { content: ""; width: 50%; height: 50%; border-radius: 50%; background: #000; }
.pdf-overlay .pdf-toggle:disabled { cursor: default; background: transparent; }

@media (max-width: 640px) {
  .wrap { padding: 16px 12px 40px; }
  .card { padding: 16px; }
  h1 { font-size: 19px; }
}
