/* ============================================================
   Sesamo — stile
   ============================================================ */

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

:root {
  --bg: #0e1013;
  --panel: #171b21;
  --panel-2: #1f242c;
  --line: #2b313a;
  --fg: #ffffff;
  --muted: #9aa4b2;
  --qr: 70vmin;              /* ricalcolato da JS */
  --tap: 56px;
}

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

body { overflow: hidden; }

/* serve perché .sheet/.scanner impostano display: senza questa riga
   l'attributo hidden non nasconderebbe nulla */
[hidden] { display: none !important; }

button, select, input, textarea { font: inherit; color: inherit; }

/* ---------- struttura ---------- */

#app {
  position: fixed;
  inset: 0;
  height: 100dvh;                /* segue la barra degli indirizzi sui telefoni */
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
}

/* ---------- deck di card con snap orizzontale ---------- */

.deck {
  display: flex;
  min-height: 0;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  scroll-behavior: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  touch-action: pan-x;
}
.deck::-webkit-scrollbar { display: none; }

.card {
  flex: 0 0 100%;
  width: 100%;
  min-width: 0;
  min-height: 0;
  scroll-snap-align: center;
  scroll-snap-stop: always;
  /* etichetta + QR centrati insieme: lo spazio che avanza si divide
     sopra e sotto, invece di aprire un buco in mezzo */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  row-gap: clamp(6px, 1.6vh, 18px);
  overflow: hidden;
  background: var(--c, #333);
  color: var(--fgc, #fff);
  padding: calc(env(safe-area-inset-top) + clamp(8px, 1.6vh, 18px))
           clamp(10px, 3vw, 24px)
           clamp(10px, 1.6vh, 18px);
}

.card > header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(8px, 2.5vw, 18px);
  padding: 0 44px;              /* lascia liberi i due pulsanti in alto */
  max-width: 100%;
  min-width: 0;
}

.card .name {
  margin: 0;
  font-size: clamp(26px, 8.5vw, 54px);
  font-weight: 800;
  line-height: 1;
  letter-spacing: .01em;
  text-transform: uppercase;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card .mat-icon {
  width: clamp(30px, 9vw, 54px);
  height: clamp(30px, 9vw, 54px);
  flex: 0 0 auto;
}

/* il pannello ha dimensione in px calcolata da JS (fitQR) */
.qr-area {
  flex: 0 1 auto;
  width: 100%;                  /* così il 100% del pannello ha un riferimento certo */
  display: grid;
  place-items: center;
  min-height: 0;
}

.qr-panel {
  width: var(--qr);
  height: var(--qr);
  max-width: 100%;              /* rete di sicurezza: non può mai sbordare */
  max-height: 100%;
  background: #fff;
  border-radius: clamp(10px, 3vw, 20px);
  padding: clamp(8px, 2.2vw, 16px);
  box-shadow: 0 12px 34px rgba(0, 0, 0, .28);
  display: grid;
  place-items: center;
  cursor: pointer;
}

svg.qr { width: 100%; height: 100%; display: block; }

/* card senza codice */
.empty {
  color: #333;
  text-align: center;
  padding: 14px;
  display: grid;
  gap: 14px;
  place-content: center;
  font-size: clamp(14px, 3.6vw, 18px);
}
.empty strong { font-size: clamp(16px, 4.4vw, 22px); }

/* ---------- barra di scelta rapida (zona pollice) ---------- */

.switcher {
  display: flex;
  gap: 6px;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  background: var(--bg);
  overflow-x: auto;
  scrollbar-width: none;
}
.switcher::-webkit-scrollbar { display: none; }

.tab {
  flex: 1 1 0;
  min-width: 70px;
  min-height: var(--tap);
  border: 0;
  border-radius: 14px;
  padding: 9px 6px 7px;
  background: var(--panel);
  color: #cfd6e0;
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 5px;
  touch-action: manipulation;
}
.tab svg { width: 26px; height: 26px; }
.tab span {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tab[aria-selected="true"] {
  background: var(--c);
  color: var(--fgc);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .55);
}

/* ---------- pulsanti flottanti ---------- */

.fab {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 8px);
  width: 42px;
  height: 42px;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, .28);
  color: #fff;
  display: grid;
  place-items: center;
  backdrop-filter: blur(4px);
  touch-action: manipulation;
}
.fab svg { width: 22px; height: 22px; }
.fab-left { left: 8px; }
.fab-right { right: 8px; }

/* ---------- modalità ingrandita (tap sul QR) ---------- */

body.zoom .switcher,
body.zoom .fab { display: none; }

body.zoom .card { padding: calc(env(safe-area-inset-top) + 3px) 3px 3px; row-gap: 3px; }
body.zoom .card > header { padding: 0; gap: 8px; }
body.zoom .card .name { font-size: clamp(14px, 4vw, 20px); }
body.zoom .card .mat-icon { width: 20px; height: 20px; }
body.zoom .qr-panel { border-radius: 6px; padding: clamp(6px, 1.6vw, 12px); }

/* ---------- orizzontale ---------- */

@media (orientation: landscape) and (max-height: 560px) {
  .card { row-gap: 4px; padding: calc(env(safe-area-inset-top) + 6px) 10px 6px; }
  .card > header { padding: 0 48px; }
  .card .name { font-size: clamp(20px, 5vh, 40px); }
  .card .mat-icon { width: clamp(22px, 5vh, 40px); height: clamp(22px, 5vh, 40px); }
  .tab { min-height: 46px; padding: 5px 6px; }
  .tab svg { width: 20px; height: 20px; }
}

/* ---------- hint iniziale ---------- */

.hint {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tap) + 26px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: rgba(0, 0, 0, .72);
  color: #fff;
  padding: 10px 14px;
  border-radius: 999px;
  font-size: 13px;
  text-align: center;
  max-width: 92vw;
  pointer-events: none;
  animation: fade .35s ease;
}

@keyframes fade { from { opacity: 0 } to { opacity: 1 } }

/* ---------- pannelli (impostazioni / editor / import) ---------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: var(--bg);
  display: grid;
  grid-template-rows: auto 1fr;
}

.sheet-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) 12px 10px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.sheet-bar h1 { margin: 0; font-size: 18px; font-weight: 700; }

.btn-icon {
  width: 42px;
  height: 42px;
  border: 0;
  border-radius: 12px;
  background: var(--panel-2);
  color: #fff;
  font-size: 18px;
  touch-action: manipulation;
}

.sheet-body {
  overflow-y: auto;
  padding: 14px 12px calc(28px + env(safe-area-inset-bottom));
  -webkit-overflow-scrolling: touch;
}

.sheet-body h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
  margin: 22px 2px 8px;
}
.sheet-body h2:first-child { margin-top: 4px; }

.muted { color: var(--muted); font-size: 13px; line-height: 1.45; margin: 6px 2px 10px; }
.muted small { display: block; }

/* lista materiali */
.item-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }

.item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px;
}
.dot {
  width: 38px; height: 38px;
  border-radius: 11px;
  display: grid; place-items: center;
  flex: 0 0 auto;
}
.dot svg { width: 22px; height: 22px; }
.item-txt { min-width: 0; }
.item-txt b { display: block; font-size: 15px; }
.item-txt small { color: var(--muted); font-size: 12px; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-txt small.ko { color: #ff9a8a; }
.item-actions { display: flex; gap: 4px; }
.mini {
  width: 36px; height: 36px;
  border: 0; border-radius: 10px;
  background: var(--panel-2); color: #fff;
  font-size: 15px;
  touch-action: manipulation;
}

/* bottoni */
.btn {
  min-height: 48px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel-2);
  color: #fff;
  font-weight: 600;
  touch-action: manipulation;
}
.btn:active { filter: brightness(1.25); }
.btn-block { width: 100%; margin-top: 10px; }
.btn-primary { background: #2f6fed; border-color: #2f6fed; flex: 1 1 auto; }
.btn-danger { background: #3a1f20; border-color: #5a2a2c; color: #ff9a8a; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }
.btn-row .btn { flex: 1 1 auto; }

/* righe opzioni */
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
  margin-bottom: 8px;
  font-size: 15px;
}
.row > span { min-width: 0; }
.row select {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  min-height: 44px;
}
.row input[type="checkbox"] { width: 26px; height: 26px; flex: 0 0 auto; accent-color: #2f6fed; }

/* campi editor */
.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 13px; color: var(--muted); }
.field input[type="text"], .field textarea, #fImport {
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  font-size: 16px;
  min-height: 48px;
}
.field textarea, #fImport { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 14px; resize: vertical; }

.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.sw {
  width: 44px; height: 44px;
  border: 2px solid transparent;
  border-radius: 12px;
  touch-action: manipulation;
}
.sw[aria-pressed="true"] { border-color: #fff; box-shadow: 0 0 0 2px #000 inset; }

.icon-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.ic {
  width: 52px; height: 52px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: #fff;
  display: grid; place-items: center;
  touch-action: manipulation;
}
.ic svg { width: 26px; height: 26px; }
.ic[aria-pressed="true"] { background: #2f6fed; border-color: #2f6fed; }

.preview {
  display: flex;
  align-items: center;
  gap: 10px;
  border-radius: 14px;
  padding: 14px;
  font-weight: 800;
  font-size: 22px;
  text-transform: uppercase;
}
.preview svg { width: 30px; height: 30px; }

/* ---------- scanner ---------- */

.scanner {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: #000;
  display: grid;
  grid-template-rows: 1fr auto auto;
  align-items: center;
  justify-items: center;
}
.scanner video {
  grid-row: 1 / 2;
  grid-column: 1;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.scan-frame {
  grid-row: 1 / 2;
  grid-column: 1;
  width: min(68vw, 56vh);
  aspect-ratio: 1;
  border: 3px solid rgba(255, 255, 255, .9);
  border-radius: 18px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .45);
}
.scan-msg { color: #fff; margin: 10px 14px; text-align: center; }
.scanner .btn { margin: 0 14px calc(18px + env(safe-area-inset-bottom)); width: calc(100% - 28px); }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  z-index: 60;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #fff;
  color: #111;
  font-size: 14px;
  font-weight: 600;
  padding: 12px 16px;
  border-radius: 999px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .4);
  max-width: 92vw;
  text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; }
}
