:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-2: #eceef1;
  --text: #16171a;
  --muted: #6b6f76;
  --line: #d9dce1;
  --accent: #0a6cff;
  --accent-text: #ffffff;
  --danger-bg: #fff4e5;
  --danger-line: #f0c48a;
  --danger-text: #7a4a06;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 8px 24px rgba(0,0,0,.07);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0b0f;
    --surface: #16171c;
    --surface-2: #202128;
    --text: #f2f3f5;
    --muted: #9a9ea7;
    --line: #2c2e36;
    --accent: #2f83ff;
    --danger-bg: #2e2213;
    --danger-line: #6a4e1d;
    --danger-text: #f0c48a;
    --shadow: 0 1px 2px rgba(0,0,0,.5), 0 10px 30px rgba(0,0,0,.45);
  }
}

* { box-sizing: border-box; }

/* Diverse classi qui sotto impostano display: flex/grid, che vincerebbe
   sul comportamento predefinito dell'attributo hidden. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

body {
  padding-bottom: env(safe-area-inset-bottom);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

h1 { font-size: 1.6rem; letter-spacing: -.02em; margin: 0 0 .4rem; }
h2 { font-size: 1.1rem; margin: 1.6rem 0 .6rem; }
h3 { font-size: .82rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0; font-weight: 600; }
h3 small { text-transform: none; letter-spacing: 0; font-weight: 400; opacity: .7; margin-left: .4em; }
p { margin: 0 0 .8rem; }
a { color: var(--accent); }

.lead { color: var(--muted); margin-bottom: 1.4rem; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: .85rem; }
.hint-inline { color: var(--muted); font-size: .78rem; font-weight: 400; }

/* ── topbar ──────────────────────────────────────────────────────────────── */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem;
  padding: max(.7rem, env(safe-area-inset-top)) 1rem .7rem;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 85%, transparent);
  backdrop-filter: saturate(160%) blur(14px);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 600; color: var(--text); text-decoration: none; }
.brand-mark {
  width: 22px; height: 22px; border-radius: 7px;
  background: linear-gradient(140deg, #0a6cff, #7a3cff 60%, #ff3c8a);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.25);
}

/* ── layout ──────────────────────────────────────────────────────────────── */
main { flex: 1; width: 100%; max-width: 1080px; margin: 0 auto; padding: 1.4rem 1rem 3rem; }
.step { animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.foot { padding: 1rem; text-align: center; color: var(--muted); font-size: .78rem; }

/* ── sorgenti ────────────────────────────────────────────────────────────── */
.sources { display: grid; gap: .8rem; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.source {
  display: flex; flex-direction: column; gap: .35rem; text-align: left;
  padding: 1.1rem; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--text); cursor: pointer;
  box-shadow: var(--shadow); font: inherit; transition: transform .12s ease, border-color .12s ease;
}
.source:hover { transform: translateY(-2px); border-color: var(--accent); }
.source:active { transform: none; }
.source-icon { font-size: 1.6rem; }
.source-title { font-weight: 600; font-size: 1.05rem; }
.source-desc { color: var(--muted); font-size: .87rem; }

/* ── banner / alert ──────────────────────────────────────────────────────── */
.banner, .alert {
  background: var(--danger-bg); border: 1px solid var(--danger-line); color: var(--danger-text);
  padding: .75rem .9rem; border-radius: 12px; font-size: .88rem;
  margin: 0 auto 1rem; max-width: 1080px;
}
.banner { margin: .8rem auto 0; width: calc(100% - 2rem); }
.alert { margin-bottom: 1rem; }
.alert a, .banner a { color: inherit; }

/* ── form ────────────────────────────────────────────────────────────────── */
fieldset {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface);
  padding: 1rem; margin: 0 0 1rem; box-shadow: var(--shadow);
}
legend { font-weight: 600; padding: 0 .4rem; font-size: .95rem; }
label { display: block; font-size: .82rem; color: var(--muted); font-weight: 500; margin-bottom: .7rem; }
.row { display: grid; gap: .7rem; grid-template-columns: 1fr 1fr; }
.row-3 { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 560px) { .row, .row-3 { grid-template-columns: 1fr; } }

input[type=text], input[type=number], input[type=datetime-local], select, textarea {
  width: 100%; margin-top: .25rem; padding: .55rem .65rem;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--text);
  font: inherit; font-size: 16px; /* evita lo zoom automatico di Safari iOS */
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
input[type=color] { width: 100%; height: 40px; margin-top: .25rem; padding: 2px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); }
textarea { min-height: 80px; resize: vertical; }

.chips { display: flex; flex-wrap: wrap; gap: .45rem; margin-bottom: .8rem; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip span {
  display: inline-block; padding: .4rem .75rem; border-radius: 999px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
  font-size: .85rem; cursor: pointer;
}
.chip input:checked + span { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ── gruppi di campi ─────────────────────────────────────────────────────── */
.fieldgroup { border-top: 1px solid var(--line); padding: .8rem 0; }
.fieldgroup:first-of-type { border-top: 0; padding-top: 0; }
.fieldgroup-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-bottom: .5rem; }
.fieldrow { display: grid; grid-template-columns: 1fr 1.6fr auto; gap: .5rem; align-items: start; margin-bottom: .5rem; }
.fieldrow input { margin-top: 0; }
.fieldrow textarea { margin-top: 0; }
@media (max-width: 560px) { .fieldrow { grid-template-columns: 1fr auto; } .fieldrow .f-label { grid-column: 1 / -1; } }
.iconbtn {
  border: 1px solid var(--line); background: var(--bg); color: var(--muted);
  border-radius: 10px; width: 38px; height: 38px; cursor: pointer; font-size: 1.1rem; line-height: 1;
}
.iconbtn:hover { color: var(--text); border-color: var(--accent); }

.suggestions { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .3rem; }
.suggestions button {
  font: inherit; font-size: .78rem; padding: .25rem .6rem; border-radius: 999px;
  border: 1px dashed var(--line); background: transparent; color: var(--muted); cursor: pointer;
}
.suggestions button:hover { color: var(--text); border-color: var(--accent); }

/* ── slot immagini ───────────────────────────────────────────────────────── */
.imgslot { border: 1px solid var(--line); border-radius: 12px; padding: .7rem; background: var(--bg); }
.imgslot-label { font-size: .82rem; color: var(--muted); font-weight: 500; }
.imgslot-body {
  margin: .5rem 0; min-height: 84px; border-radius: 8px; background: var(--surface-2);
  display: flex; align-items: center; justify-content: center; overflow: hidden;
}
.imgslot-body img { max-width: 100%; max-height: 140px; display: block; }
.imgslot-body:empty::after { content: "nessuna immagine"; color: var(--muted); font-size: .8rem; }
.imgslot-actions { display: flex; gap: .4rem; }

/* ── bottoni ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: inherit; font-weight: 600; padding: .62rem 1.05rem; border-radius: 11px;
  border: 1px solid transparent; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn-sm { padding: .32rem .7rem; font-size: .82rem; font-weight: 500; }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-primary:disabled { opacity: .5; cursor: progress; }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--text); }
.btn-ghost:hover { border-color: var(--accent); color: var(--accent); }
.btn-wallet {
  background: #000; color: #fff; border-radius: 12px; padding: .85rem 1.4rem;
  font-size: 1.02rem; width: 100%; max-width: 340px;
}
@media (prefers-color-scheme: dark) { .btn-wallet { background: #fff; color: #000; } }

.editor-actions, .done-actions { display: flex; gap: .6rem; flex-wrap: wrap; margin-top: 1rem; }

/* ── editor ──────────────────────────────────────────────────────────────── */
.editor-grid { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 1.6rem; align-items: start; }
@media (max-width: 900px) { .editor-grid { grid-template-columns: 1fr; } .editor-preview { order: -1; } }
.preview-sticky { position: sticky; top: 5rem; display: flex; flex-direction: column; align-items: center; gap: .7rem; }

/* ── anteprima pass (replica del layout Wallet) ──────────────────────────── */
.phone {
  width: 320px; max-width: 100%; padding: 14px; border-radius: 28px;
  background: var(--surface-2); border: 1px solid var(--line);
}
.pass {
  border-radius: 12px; overflow: hidden; box-shadow: 0 6px 20px rgba(0,0,0,.25);
  font-family: var(--font); min-height: 380px; display: flex; flex-direction: column;
  background: #3c414c; color: #fff; position: relative;
}
.pass.flipped .pass-front { display: none; }
.pass:not(.flipped) .pass-back { display: none; }
.pass-front, .pass-back { display: flex; flex-direction: column; flex: 1; }

.p-head { display: flex; align-items: center; gap: .5rem; padding: 10px 12px 6px; }
.p-logo { height: 26px; max-width: 110px; object-fit: contain; }
.p-logotext { font-size: .8rem; font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.p-headfields { display: flex; gap: 10px; }
.p-headfields .p-field { text-align: right; }

.p-strip { width: 100%; display: block; object-fit: cover; }
.p-thumb { width: 60px; height: 60px; border-radius: 6px; object-fit: cover; flex: none; }

.p-primary { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; padding: 8px 12px; }
.p-primary .p-value { font-size: 1.35rem; font-weight: 500; line-height: 1.15; word-break: break-word; }
.p-row { display: flex; gap: 14px; padding: 6px 12px; flex-wrap: wrap; }
.p-field { min-width: 0; }
.p-label { font-size: .6rem; font-weight: 600; letter-spacing: .045em; text-transform: uppercase; opacity: .95; }
.p-value { font-size: .82rem; word-break: break-word; }

.p-barcode { margin-top: auto; background: #fff; padding: 10px; display: flex; flex-direction: column; align-items: center; gap: 5px; }
.p-barcode img, .p-barcode .p-barcode-box {
  width: 118px; height: 118px; background: #fff; display: block;
  object-fit: contain; image-rendering: pixelated;
}
/* I codici monodimensionali Wallet li allunga in verticale. */
.p-barcode.wide img, .p-barcode.wide .p-barcode-box { width: 92%; height: 54px; object-fit: fill; }
.p-barcode-alt { color: #000; font-size: .62rem; letter-spacing: .04em; text-align: center; word-break: break-all; }
.p-barcode-box { border: 1px dashed #bbb; color: #999; font-size: .6rem; display: flex; align-items: center; justify-content: center; text-align: center; }

.pass-back { padding: 12px; gap: 10px; overflow-y: auto; max-height: 380px; }
.pass-back .p-backfield { border-bottom: 1px solid rgba(255,255,255,.18); padding-bottom: 8px; }
.pass-back .p-value { white-space: pre-wrap; font-size: .76rem; }

/* ── risultato ───────────────────────────────────────────────────────────── */
.done-grid { display: grid; grid-template-columns: 1fr auto; gap: 1.6rem; align-items: start; }
@media (max-width: 700px) { .done-grid { grid-template-columns: 1fr; } }
.qr-wrap { background: #fff; padding: 12px; border-radius: 14px; box-shadow: var(--shadow); line-height: 0; }
.qr-wrap img { width: 220px; height: 220px; display: block; }
.linkbox {
  display: flex; align-items: center; gap: .5rem; margin: 1rem 0;
  background: var(--surface); border: 1px solid var(--line); border-radius: 11px; padding: .5rem .7rem;
}
.linkbox code { flex: 1; font-size: .8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── elenchi ─────────────────────────────────────────────────────────────── */
.recent-list { list-style: none; margin: 0; padding: 0; display: grid; gap: .5rem; }
.recent-list li {
  display: flex; align-items: center; gap: .8rem; padding: .7rem .9rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
}
.recent-list .r-main { flex: 1; min-width: 0; }
.recent-list .r-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent-list .r-sub { color: var(--muted); font-size: .78rem; }

/* ── spinner ─────────────────────────────────────────────────────────────── */
#step-loading { text-align: center; padding: 4rem 0; color: var(--muted); }
.spinner {
  width: 34px; height: 34px; margin: 0 auto 1rem; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2s; } .step { animation: none; } }

/* ── landing del pass ────────────────────────────────────────────────────── */
body.landing { align-items: center; justify-content: center; }
.landing-wrap { max-width: 460px; margin: 0 auto; padding: 2rem 1.2rem 3rem; text-align: center; }
.landing-wrap .btn-wallet { margin: 0 auto; }
.card-preview {
  border-radius: 14px; padding: 16px; margin: 1.2rem 0 1.6rem; text-align: left;
  box-shadow: 0 8px 26px rgba(0,0,0,.22); min-height: 130px;
}
.card-preview .logo-text { font-size: .8rem; font-weight: 600; opacity: .95; }
.card-preview .card-primary { font-size: 1.4rem; margin: .5rem 0 .8rem; }
.card-preview .card-secondary { display: flex; gap: 16px; font-size: .78rem; flex-wrap: wrap; }
.share { margin-top: 2rem; text-align: center; }
.share summary { cursor: pointer; color: var(--muted); font-size: .88rem; }
.share .qr { width: 200px; height: 200px; margin: 1rem auto; display: block; background: #fff; padding: 8px; border-radius: 10px; }
.share .link code { font-size: .75rem; word-break: break-all; }
