/* ============ Tema ============ */
:root {
  --primary: #2196F3;
  --primary-dark: #1976D2;
  --secondary: #7C4DFF;
  --error: #E53935;
  --success: #43A047;
  --bg: #F5F7FA;
  --surface: #FFFFFF;
  --on-surface: #1A1C1E;
  --on-surface-muted: rgba(26, 28, 30, 0.65);
  --outline: #C7CBD1;
  --error-container: #FDECEA;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  --radius-card: 16px;
  --radius-btn: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121417;
    --surface: #1E2126;
    --on-surface: #E3E5E8;
    --on-surface-muted: rgba(227, 229, 232, 0.65);
    --outline: #44484E;
    --error-container: #4A1F1D;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

/* L'attributo hidden deve sempre vincere sulle regole display */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--on-surface);
  -webkit-tap-highlight-color: transparent;
}

/* ============ Layout viste ============ */
.view {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.view[hidden] { display: none; }

.view-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 24px;
  overflow: hidden;
  min-height: 0;
}

.view-center {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 12px;
  padding: 32px;
}

/* ============ Topbar ============ */
.topbar {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  height: 56px;
  padding: 0 8px;
  flex-shrink: 0;
}

.topbar-title {
  font-size: 1.15rem;
  font-weight: 600;
  margin: 0;
}

.topbar .icon-btn:first-child { position: absolute; left: 8px; }
.topbar .icon-btn:last-child:not(:first-child) { position: absolute; right: 8px; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: none;
  background: transparent;
  color: var(--on-surface);
  border-radius: 50%;
  cursor: pointer;
}

.icon-btn:hover { background: rgba(128, 128, 128, 0.15); }
.icon-btn:disabled { opacity: 0.35; cursor: default; }
.icon-btn svg { width: 22px; height: 22px; }

/* ============ Card ============ */
.card {
  background: var(--surface);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow);
  padding: 16px;
}

/* ============ Bottoni ============ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 32px;
  border-radius: var(--radius-btn);
  border: none;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}

.btn svg { width: 20px; height: 20px; }

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-primary:disabled { opacity: 0.45; cursor: default; }

.btn-outline {
  background: transparent;
  color: var(--primary);
  border: 1.5px solid var(--primary);
}

.btn-text {
  background: transparent;
  color: var(--on-surface);
  padding: 10px 16px;
}
.btn-text-primary { color: var(--primary); }

.btn-block { width: 100%; }

/* ============ Spinner ============ */
.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(128, 128, 128, 0.25);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.spinner-sm { width: 18px; height: 18px; border-width: 2.5px; }

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

/* ============ Vista codice ============ */
.code-form {
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  text-align: center;
}

.hero-icon {
  color: var(--primary);
}
.hero-icon svg { width: 80px; height: 80px; }

.code-form h2 { margin: 0; font-size: 1.4rem; }

.field {
  width: 100%;
  text-align: left;
}

.field label {
  display: block;
  font-size: 0.85rem;
  margin-bottom: 6px;
  color: var(--on-surface-muted);
}

.field input {
  width: 100%;
  padding: 14px 16px;
  font-size: 1rem;
  border: 1.5px solid var(--outline);
  border-radius: var(--radius-btn);
  background: var(--surface);
  color: var(--on-surface);
  font-family: inherit;
}

.field input:focus {
  outline: none;
  border-color: var(--primary);
}

.field-error {
  color: var(--error);
  font-size: 0.8rem;
  margin: 6px 0 0;
}

/* ============ Home ============ */
.client-card {
  display: flex;
  align-items: center;
  gap: 12px;
}

.client-card-icon {
  color: var(--primary);
  flex-shrink: 0;
}
.client-card-icon svg { width: 28px; height: 28px; }

.client-code {
  font-size: 1.1rem;
  font-weight: 700;
}

.home-actions {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 16px;
}

.action-tile {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px;
  border: none;
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  color: var(--on-surface);
  width: 100%;
}

.action-icon {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.action-icon svg { width: 32px; height: 32px; }
.action-icon-primary { background: rgba(33, 150, 243, 0.12); color: var(--primary); }
.action-icon-secondary { background: rgba(124, 77, 255, 0.12); color: var(--secondary); }

.action-text {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.action-title { font-size: 1.15rem; font-weight: 700; }
.action-subtitle { font-size: 0.9rem; color: var(--on-surface-muted); }

.chevron { width: 22px; height: 22px; color: var(--on-surface-muted); flex-shrink: 0; }

/* Bandierina cambio lingua in basso al centro */
.home-footer {
  display: flex;
  justify-content: center;
  padding: 8px 24px 20px;
  flex-shrink: 0;
}

.lang-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  padding: 0;
  overflow: hidden;
  background: var(--surface);
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 0.15s ease;
}

.lang-btn:hover { transform: scale(1.08); }
.lang-btn svg { width: 100%; height: 100%; display: block; }

/* ============ Firma ============ */
.signature-body { padding-top: 8px; }

.signature-hint {
  text-align: center;
  font-size: 1rem;
  margin: 8px 0 16px;
}

.signature-pad-wrap {
  flex: 1;
  position: relative;
  background: #fff;
  border: 2px solid var(--outline);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
  min-height: 0;
}

#signature-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

.signature-line {
  position: absolute;
  bottom: 24px;
  left: 16px;
  right: 16px;
  height: 1px;
  background: rgba(128, 128, 128, 0.35);
  pointer-events: none;
}

.signature-placeholder {
  position: absolute;
  bottom: 8px;
  right: 16px;
  color: rgba(128, 128, 128, 0.55);
  font-size: 12px;
  font-style: italic;
  pointer-events: none;
}

.privacy-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 12px 0;
  font-size: 0.9rem;
}

.privacy-row input {
  width: 20px;
  height: 20px;
  margin: 0;
  accent-color: var(--primary);
  flex-shrink: 0;
}

.privacy-row a { color: var(--primary); }

.controls-row {
  display: flex;
  justify-content: space-evenly;
  gap: 16px;
  padding: 8px 0;
}

/* ============ Scansione ============ */
.scan-body { padding: 0 0 24px; }

.scan-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.camera-frame {
  flex: 1;
  position: relative;
  margin: 8px 16px 0;
  border-radius: 12px;
  overflow: hidden;
  background: #000;
  min-height: 0;
}

#scan-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.camera-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.preview-frame {
  flex: 1;
  margin: 8px 16px 0;
  border-radius: 12px;
  overflow: hidden;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

#scan-preview-img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.controls-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 20px 24px 0;
  text-align: center;
}

.controls-col p { margin: 0; font-size: 1rem; }

.shutter-btn {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: 4px solid var(--primary);
  background: transparent;
  padding: 4px;
  cursor: pointer;
}

.shutter-inner {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
}

.shutter-inner svg { width: 30px; height: 30px; }
.shutter-btn:disabled .shutter-inner { background: #9e9e9e; }
.shutter-btn:disabled { border-color: #9e9e9e; cursor: default; }

.state-icon svg { width: 80px; height: 80px; }
.muted-icon { color: #9e9e9e; }
.error-icon { color: var(--error); }

/* Landscape: fotocamera a sinistra, controlli/shutter a destra */
@media (orientation: landscape) {
  .scan-body { padding-bottom: 8px; }

  .scan-stage {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: stretch;
  }

  .camera-frame,
  .preview-frame {
    margin: 8px 0 8px 16px;
    min-width: 0;
  }

  .scan-stage .controls-col {
    flex-shrink: 0;
    justify-content: center;
    gap: 16px;
    padding: 8px 20px 8px 8px;
    max-width: 220px;
  }

  .scan-stage .controls-row {
    flex-direction: column;
    align-items: stretch;
    justify-content: center;
    padding: 0;
  }

  /* Il banner d'errore occupa una riga intera sotto camera e controlli */
  .scan-stage .error-banner {
    flex-basis: 100%;
    order: 3;
    margin: 4px 16px 8px;
  }

  /* Firma: pad a sinistra, hint in alto e controlli in colonna a destra */
  .signature-body {
    flex-direction: row;
    flex-wrap: wrap;
    column-gap: 16px;
    padding: 4px 16px 12px;
  }

  .signature-hint {
    flex-basis: 100%;
    margin: 0 0 4px;
    font-size: 0.85rem;
    text-align: left;
  }

  .signature-pad-wrap {
    flex: 1;
    min-width: 0;
  }

  .signature-side {
    flex: 0 0 240px;
    min-width: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 8px;
    overflow-y: auto;
  }

  .signature-side .error-banner { margin: 0; }

  .signature-side .privacy-row {
    padding: 4px 0;
    font-size: 0.8rem;
  }

  .signature-side .controls-row {
    flex-direction: column;
    align-items: stretch;
    justify-content: center;
    padding: 0;
    gap: 8px;
  }

  .signature-side .controls-row .btn { padding: 12px 16px; }
}

/* ============ Error banner ============ */
.error-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 16px;
  padding: 12px;
  background: var(--error-container);
  color: var(--error);
  border-radius: 8px;
  font-size: 0.9rem;
}

.error-banner svg { width: 20px; height: 20px; flex-shrink: 0; }

/* ============ Overlay caricamento ============ */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.overlay[hidden] { display: none; }

.overlay-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 28px 36px;
  margin: 32px;
}

.overlay-card p { margin: 0; }

/* ============ Dialog ============ */
.dialog {
  border: none;
  border-radius: var(--radius-card);
  background: var(--surface);
  color: var(--on-surface);
  padding: 24px;
  max-width: 340px;
  width: calc(100% - 64px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.dialog::backdrop { background: rgba(0, 0, 0, 0.5); }

.dialog h3 { margin: 0 0 8px; font-size: 1.15rem; }
.dialog p { margin: 0 0 20px; }

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* ============ Snackbar ============ */
.snackbar {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--success);
  color: #fff;
  padding: 14px 24px;
  border-radius: 10px;
  font-size: 0.95rem;
  box-shadow: var(--shadow);
  z-index: 200;
  animation: snackbar-in 0.25s ease-out;
}

.snackbar[hidden] { display: none; }

@keyframes snackbar-in {
  from { opacity: 0; transform: translate(-50%, 12px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* ============ Gate installazione ============ */
.install-gate {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
  overflow-y: auto;
}

.install-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  width: 100%;
  max-width: 420px;
  padding: 32px 24px;
  text-align: center;
}

.install-card h2 { margin: 0; font-size: 1.4rem; }
.install-card p { margin: 0; }

.install-icon {
  width: 88px;
  height: 88px;
  border-radius: 20px;
  box-shadow: var(--shadow);
}

.install-steps {
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: install-step;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: left;
}

.install-steps li {
  display: flex;
  align-items: center;
  gap: 12px;
  counter-increment: install-step;
}

.install-steps li::before {
  content: counter(install-step);
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(33, 150, 243, 0.12);
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
}

.step-icon {
  width: 22px;
  height: 22px;
  color: var(--primary);
  flex-shrink: 0;
}

/* ============ Utility ============ */
.muted { color: var(--on-surface-muted); }
.small { font-size: 0.85rem; }
