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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f0f2f5;
  color: #1a1a1a;
  line-height: 1.5;
  min-height: 100vh;
}

.app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 16px 40px;
}

header {
  text-align: center;
  margin-bottom: 24px;
}

header h1 {
  font-size: 1.6rem;
  color: #1a73e8;
}

.subtitle {
  color: #5f6368;
  font-size: 0.95rem;
  margin-top: 4px;
}

/* ========== GALLERY — griglia ordinata, niente scroll ========== */

.gallery-section {
  margin-bottom: 20px;
}

.gallery-section .card-title {
  margin-bottom: 0;
  border-bottom: none;
  padding-bottom: 0;
}

.gallery-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

/* Tabs */
.gallery-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.tab {
  padding: 5px 14px;
  border: 1px solid #dadce0;
  border-radius: 20px;
  background: #fff;
  font-size: 0.78rem;
  cursor: pointer;
  transition: all 0.15s;
  color: #5f6368;
  font-weight: 500;
  white-space: nowrap;
}

.tab:hover {
  border-color: #1a73e8;
  color: #1a73e8;
}

.tab.active {
  background: #1a73e8;
  color: #fff;
  border-color: #1a73e8;
}

/* Griglia preset — flex wrap, centrata, niente scroll */
.preset-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.preset-card {
  flex: 0 0 auto;
  width: 78px;
  background: #f8f9fa;
  border: 2px solid transparent;
  border-radius: 10px;
  padding: 5px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.15s, box-shadow 0.15s;
}

.preset-card:hover {
  border-color: #1a73e8;
  transform: translateY(-2px);
  box-shadow: 0 3px 10px rgba(26, 115, 232, 0.15);
}

.preset-card.active {
  border-color: #1a73e8;
  background: #e8f0fe;
  box-shadow: 0 2px 8px rgba(26, 115, 232, 0.25);
}

.preset-card.card-hidden {
  display: none;
}

.preset-qr {
  line-height: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 64px;
  border-radius: 4px;
  overflow: hidden;
}

.preset-qr canvas,
.preset-qr svg {
  width: 64px !important;
  height: 64px !important;
  display: block;
}

.preset-name {
  font-size: 0.6rem;
  font-weight: 600;
  color: #3c4043;
  margin-top: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.1;
}

/* ========== BUILDER LAYOUT ========== */

.builder {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  align-items: start;
}

.panel-options {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Cards */
.card {
  background: #fff;
  border: 1px solid #dadce0;
  border-radius: 12px;
  padding: 18px 16px;
}

.card-title {
  font-size: 1rem;
  color: #1a73e8;
  margin-bottom: 14px;
  padding-bottom: 8px;
  border-bottom: 1px solid #e8eaed;
}

/* Fields */
.field {
  margin-bottom: 14px;
}

.field:last-child {
  margin-bottom: 0;
}

.field > label {
  display: block;
  font-size: 0.85rem;
  color: #5f6368;
  margin-bottom: 4px;
  font-weight: 500;
}

.field input[type="text"],
.field input[type="email"],
.field input[type="tel"],
.field input[type="url"],
.field input[type="number"],
.field textarea,
.field select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #dadce0;
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.2s;
  background: #fff;
}

.field input:focus,
.field textarea:focus,
.field select:focus {
  border-color: #1a73e8;
}

.field textarea {
  resize: vertical;
  min-height: 60px;
}

.field-hint {
  font-size: 0.8rem;
  color: #80868b;
  margin-top: 4px;
}

/* Color row */
.color-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

input[type="color"] {
  width: 40px;
  height: 36px;
  border: 1px solid #dadce0;
  border-radius: 6px;
  padding: 2px;
  cursor: pointer;
  background: #fff;
}

.cb-label {
  display: inline-flex !important;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  color: #5f6368;
  cursor: pointer;
  user-select: none;
}

.cb-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: #1a73e8;
}

.sub-options {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  align-items: center;
}

.sub-options select {
  padding: 8px 10px;
  border: 1px solid #dadce0;
  border-radius: 6px;
  font-size: 0.85rem;
  background: #fff;
}

input[type="range"] {
  width: 100%;
  accent-color: #1a73e8;
}

input[type="file"] {
  width: 100%;
  font-size: 0.85rem;
  color: #5f6368;
}

/* ========== BUTTONS ========== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  text-decoration: none;
}

.btn:active {
  transform: scale(0.98);
}

.btn-primary {
  background: #1a73e8;
  color: #fff;
}

.btn-primary:hover {
  background: #1557b0;
}

.btn-green {
  background: #34a853;
  color: #fff;
}

.btn-green:hover {
  background: #2d8e47;
}

.btn-outline {
  background: transparent;
  border: 1px solid #1a73e8;
  color: #1a73e8;
}

.btn-outline:hover {
  background: #e8f0fe;
}

.btn-sm {
  padding: 6px 14px;
  font-size: 0.8rem;
}

/* ========== PREVIEW PANEL ========== */

.preview-sticky {
  position: sticky;
  top: 24px;
}

.preview-box {
  background: #fff;
  border: 1px solid #dadce0;
  border-radius: 12px;
  padding: 24px;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 320px;
}

#qr-frame-wrap {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  transition: border 0.2s, border-radius 0.2s;
}

#qr-container {
  line-height: 0;
}

#qr-container canvas,
#qr-container svg {
  max-width: 100%;
  height: auto;
  display: block;
}

#frame-bar {
  width: 100%;
  text-align: center;
  padding: 8px 16px;
  border-radius: 0 0 8px 8px;
  margin-top: -2px;
  font-weight: 700;
  font-size: 0.85rem;
  letter-spacing: 1px;
  color: #fff;
}

.download-row {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

.download-row .btn {
  flex: 1;
}

.preview-hint {
  text-align: center;
  color: #5f6368;
  font-size: 0.8rem;
  margin-top: 10px;
}

/* ========== FOOTER ========== */

footer {
  text-align: center;
  margin-top: 32px;
}

.back-link {
  color: #1a73e8;
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
}

.back-link:hover {
  text-decoration: underline;
}

.hidden {
  display: none !important;
}

/* ========== RESPONSIVE ========== */

@media (max-width: 768px) {
  .builder {
    grid-template-columns: 1fr;
  }

  .preview-sticky {
    position: static;
  }

  .download-row {
    flex-direction: column;
  }

  .preset-card {
    width: 68px;
  }

  .preset-qr,
  .preset-qr canvas,
  .preset-qr svg {
    width: 56px !important;
    height: 56px !important;
  }

  .preset-qr {
    height: 56px;
  }
}
