*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --danger: #dc2626;
  --bg: #f8fafc;
  --card: #ffffff;
  --text: #1e293b;
  --muted: #64748b;
  --border: #e2e8f0;
  --radius: 10px;
  --shadow: 0 2px 8px rgba(0,0,0,0.10);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

/* ---- Layout ---- */
.page {
  max-width: 960px;
  margin: 0 auto;
  padding: 2rem 1rem 4rem;
  overflow-x: hidden;
}

header {
  text-align: center;
  padding: 2rem 1rem 1.5rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  margin-bottom: 2rem;
}

header h1 { font-size: 1.6rem; color: var(--primary-dark); }
header p  { color: var(--muted); margin-top: .4rem; font-size: .95rem; }

/* ---- Card ---- */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2rem;
  max-width: 460px;
  margin: 0 auto;
}

.card h2 { font-size: 1.2rem; margin-bottom: 1.2rem; }

/* ---- Form ---- */
.field { margin-bottom: 1.1rem; }
.field label { display: block; font-size: .875rem; font-weight: 600; margin-bottom: .35rem; color: var(--text); }
.field input {
  width: 100%;
  padding: .65rem .85rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 1rem;
  transition: border-color .15s;
}
.field input:focus { outline: none; border-color: var(--primary); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  padding: .7rem 1.4rem;
  border-radius: 6px;
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: background .15s, opacity .15s;
}
.btn-primary  { background: var(--primary); color: #fff; width: 100%; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-copy     { background: var(--border); color: var(--text); }
.btn-copy:hover { background: #cbd5e1; }
.btn-whatsapp { background: #25d366; color: #fff; }
.btn-whatsapp:hover { background: #1ebe5d; }
.btn-download { background: var(--success); color: #fff; }
.btn-download:hover { background: #15803d; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---- Result box (dopo registrazione) ---- */
.result-box {
  display: none;
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-radius: var(--radius);
  padding: 1.5rem;
  margin-top: 1.5rem;
}
.result-box h3 { color: var(--success); margin-bottom: .75rem; }
.link-display {
  font-size: .8rem;
  word-break: break-all;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: .6rem .8rem;
  margin-bottom: .85rem;
  color: var(--muted);
}
.btn-row { display: flex; gap: .6rem; flex-wrap: wrap; }

/* ---- Modale consenso ---- */
.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 1rem;
}
.modal {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0,0,0,.2);
  padding: 2rem;
  max-width: 520px;
  width: 100%;
}
.modal h2 { margin-bottom: 1rem; font-size: 1.1rem; }
.modal p  { font-size: .9rem; color: var(--muted); line-height: 1.6; margin-bottom: 1.2rem; }
.consent-check { display: flex; align-items: flex-start; gap: .6rem; margin-bottom: 1.4rem; }
.consent-check input[type=checkbox] { margin-top: .2rem; flex-shrink: 0; width: 18px; height: 18px; accent-color: var(--primary); }
.consent-check label { font-size: .875rem; line-height: 1.5; }

/* ---- Galleria ---- */
.toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: .75rem;
  margin-bottom: 1.2rem;
}
.toolbar-left { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; flex: 1 1 auto; min-width: 0; }
#btn-download { flex-shrink: 0; }
.count-badge {
  background: var(--primary);
  color: #fff;
  border-radius: 20px;
  padding: .2rem .65rem;
  font-size: .8rem;
  font-weight: 700;
}

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: .75rem;
}

.photo-item {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  border: 3px solid transparent;
  transition: border-color .15s, transform .1s;
  aspect-ratio: 1;
  background: var(--border);
}
.photo-item:hover { transform: scale(1.02); }
.photo-item.selected { border-color: var(--primary); }
.photo-item img {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}
.photo-item .check-icon {
  position: absolute;
  top: 6px; right: 6px;
  width: 24px; height: 24px;
  background: var(--primary);
  border-radius: 50%;
  display: none;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 13px;
}
.photo-item.selected .check-icon { display: flex; }
.photo-item img[data-src] { opacity: 0; transition: opacity .3s; }
.photo-item img.loaded { opacity: 1; }

/* ---- Lightbox ---- */
.lightbox {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.92);
  z-index: 200;
  display: none;
  align-items: center;
  justify-content: center;
}
.lightbox.open { display: flex; }
.lightbox-close {
  position: absolute; top: 1rem; right: 1.2rem;
  color: #fff; font-size: 2rem; cursor: pointer; line-height: 1;
  background: none; border: none;
}
.lightbox-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  max-width: 92vw;
  max-height: 95vh;
}
.lightbox-inner img { max-width: 100%; max-height: calc(95vh - 60px); border-radius: 6px; object-fit: contain; }
.lightbox-dl-btn {
  background: rgba(255,255,255,.15);
  backdrop-filter: blur(6px);
  color: #fff;
  border: 1px solid rgba(255,255,255,.35);
  border-radius: 20px;
  padding: .5rem 1.25rem;
  font-size: .9rem;
  font-weight: 600;
  text-decoration: none;
  transition: background .15s;
  white-space: nowrap;
  flex-shrink: 0;
}
.lightbox-dl-btn:hover { background: rgba(255,255,255,.28); }

/* ---- Messaggi ---- */
.msg { padding: .75rem 1rem; border-radius: 6px; margin-bottom: 1rem; font-size: .9rem; }
.msg-error   { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; }
.msg-success { background: #f0fdf4; color: var(--success); border: 1px solid #bbf7d0; }

.hidden { display: none !important; }

/* ---- Torna su ---- */
#btn-top {
  position: fixed;
  bottom: 1.5rem; right: 1.5rem;
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 1.2rem;
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,.2);
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s, transform .2s;
  z-index: 50;
  display: flex; align-items: center; justify-content: center;
}
#btn-top.visible { opacity: 1; pointer-events: auto; }
#btn-top:hover { transform: translateY(-3px); }

/* ---- Modale download ---- */
#dl-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 300;
  align-items: center;
  justify-content: center;
}
#dl-overlay.open { display: flex; }
#dl-box {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0,0,0,.2);
  padding: 2.2rem 2.5rem;
  min-width: 280px;
  max-width: 360px;
  width: 90%;
  text-align: center;
}
#dl-spinner {
  width: 48px; height: 48px;
  border: 5px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  margin: 0 auto 1.2rem;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#dl-label { font-size: .95rem; color: var(--text); margin-bottom: 1rem; }
#dl-bar-wrap {
  background: var(--border);
  border-radius: 6px;
  height: 10px;
  overflow: hidden;
  margin-bottom: .5rem;
}
#dl-bar {
  height: 100%;
  width: 0%;
  background: var(--primary);
  border-radius: 6px;
  transition: width .2s ease;
}
#dl-pct { font-size: .85rem; color: var(--muted); }

/* ---- Responsive ---- */
@media (max-width: 480px) {
  .gallery-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
  .card { padding: 1.25rem; }
  header h1 { font-size: 1.3rem; }
}
