/* Componenti. Ognuno è mostrato in /admin/styleguide: se ne aggiungi uno, aggiungilo anche lì. */

/* ---------- bottoni ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2);
  min-height: 44px; padding: 0 var(--s-5); border-radius: var(--r-pill); border: 1px solid transparent;
  font-size: var(--fs-md); font-weight: 600; white-space: nowrap; user-select: none;
  transition: background .15s, border-color .15s, transform .12s var(--ease), box-shadow .15s;
}
.btn:hover { text-decoration: none; }
.btn:active { transform: scale(.97); }
.btn[disabled], .btn.is-loading { opacity: .55; pointer-events: none; }
.btn--primary { background: var(--ink); color: #fff; }
.btn--primary:hover { background: #15161b; }
.btn--accent { background: var(--accent-strong); color: #fff; }
.btn--accent:hover { background: #6a5230; }
.btn--ghost { background: var(--bg); color: var(--ink); border-color: var(--line-strong); }
.btn--ghost:hover { background: var(--surface); }
.btn--quiet { color: var(--ink-2); padding: 0 var(--s-3); }
.btn--quiet:hover { background: var(--surface-2); color: var(--ink); }
.btn--danger { background: var(--danger-soft); color: var(--danger); }
.btn--danger:hover { background: #f3d9d4; }
.btn--sm { min-height: 36px; padding: 0 var(--s-4); font-size: var(--fs-sm); }
.btn--block { width: 100%; }
.btn--icon { width: 44px; padding: 0; }
.btn--icon.btn--sm { width: 36px; }

/* ---------- card ---------- */
.card { background: var(--bg); border: 1px solid var(--line); border-radius: var(--r-lg); box-shadow: var(--shadow-1); }
.card__body { padding: var(--s-5); }
.card__head { display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); padding: var(--s-4) var(--s-5); border-bottom: 1px solid var(--line); }
.card--flat { box-shadow: none; background: var(--surface); }

/* tile di un'app in Home */
.tile {
  position: relative; display: flex; flex-direction: column; gap: var(--s-3); min-height: 168px; padding: var(--s-5);
  background: var(--bg); border: 1px solid var(--line); border-radius: var(--r-lg); color: var(--ink);
  box-shadow: var(--shadow-1); transition: transform .2s var(--ease), box-shadow .2s, border-color .2s;
}
.tile:hover { text-decoration: none; }
.tile:active { transform: scale(.985); }
.tile__icon { display: grid; place-items: center; width: 48px; height: 48px; border-radius: 15px; background: var(--accent-soft); color: var(--accent-strong); }
.tile__name { font: 600 1.5rem/1.1 var(--font-serif); }
.tile__desc { color: var(--ink-2); font-size: var(--fs-sm); }
.tile__foot { margin-top: auto; display: flex; align-items: center; justify-content: space-between; color: var(--ink-3); font-size: var(--fs-sm); }
.tile .badge { position: absolute; top: var(--s-4); right: var(--s-4); }
@media (hover: hover) {
  .tile:hover { transform: translateY(-3px); box-shadow: var(--shadow-2); border-color: var(--line-strong); }
  .tile:hover .tile__foot { color: var(--accent-strong); }
}

/* riquadro numerico */
.stat { padding: var(--s-4) var(--s-5); border: 1px solid var(--line); border-radius: var(--r-md); background: var(--bg); }
.stat__value { font: 600 2rem/1 var(--font-serif); }
.stat__label { margin-top: var(--s-1); color: var(--ink-2); font-size: var(--fs-sm); }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--s-3); }
@media (min-width: 900px) { .stats { grid-template-columns: repeat(4, 1fr); } }

/* ---------- badge e chip ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 5px; height: 22px; padding: 0 9px; border-radius: var(--r-pill);
  font-size: .6875rem; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  background: var(--surface-2); color: var(--ink-2);
}
.badge--ok { background: var(--ok-soft); color: var(--ok); }
.badge--warn { background: var(--warn-soft); color: var(--warn); }
.badge--danger { background: var(--danger-soft); color: var(--danger); }
.badge--info { background: var(--info-soft); color: var(--info); }
.badge--accent { background: var(--accent-soft); color: var(--accent-strong); }

.chips { display: flex; gap: var(--s-2); overflow-x: auto; padding: 2px var(--s-4); margin: 0 calc(var(--s-4) * -1); scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: inline-flex; align-items: center; gap: var(--s-2); height: 36px; padding: 0 var(--s-4);
  border: 1px solid var(--line-strong); border-radius: var(--r-pill); background: var(--bg); color: var(--ink-2);
  font-size: var(--fs-sm); font-weight: 600;
}
.chip:hover { text-decoration: none; background: var(--surface); }
.chip.is-active { background: var(--ink); border-color: var(--ink); color: #fff; }

/* ---------- DataList: UNA tabella semantica, due presentazioni ----------
   < 900px  ogni riga diventa una card (le celle usano data-role per disporsi)
   ≥ 900px  tabella ampia con intestazione sticky
   data-role: title · subtitle · meta · badge · amount · actions   |  .hide-mobile per nascondere una cella nella card */
.datalist { width: 100%; }
.datalist thead { display: none; }
.datalist tbody { display: grid; gap: var(--s-3); }
.datalist tr {
  position: relative; display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 2px var(--s-3);
  padding: var(--s-4); background: var(--bg); border: 1px solid var(--line); border-radius: var(--r-md); box-shadow: var(--shadow-1);
}
.datalist td { padding: 0; min-width: 0; grid-column: 1; }
.datalist td[data-role="title"] { font-weight: 600; font-size: var(--fs-lg); line-height: 1.3; }
.datalist td[data-role="subtitle"] { color: var(--ink-2); font-size: var(--fs-sm); }
.datalist td[data-role="meta"] { color: var(--ink-3); font-size: var(--fs-sm); }
.datalist td[data-role="meta"]::before { content: attr(data-label) " · "; }
.datalist td[data-role="meta"][data-label=""]::before { content: none; }
.datalist td[data-role="badge"] { grid-column: 2; grid-row: 1; justify-self: end; }
.datalist td[data-role="amount"] { grid-column: 2; grid-row: 2; justify-self: end; font-weight: 600; font-variant-numeric: tabular-nums; }
.datalist td[data-role="actions"] { grid-column: 1 / -1; display: flex; gap: var(--s-2); margin-top: var(--s-3); padding-top: var(--s-3); border-top: 1px solid var(--line); }
.datalist td[data-role="actions"]:empty { display: none; }
.datalist .hide-mobile { display: none; }
/* tutta la card cliccabile: il link del titolo si allarga a coprirla */
.datalist a.stretched::after { content: ""; position: absolute; inset: 0; border-radius: inherit; }
.datalist a.stretched { color: inherit; }
.datalist td[data-role="actions"] > * { position: relative; z-index: 1; }

@media (min-width: 900px) {
  .datalist-wrap { border: 1px solid var(--line); border-radius: var(--r-lg); background: var(--bg); box-shadow: var(--shadow-1); overflow: clip; }
  .datalist thead { display: table-header-group; }
  .datalist tbody { display: table-row-group; }
  .datalist tr { display: table-row; position: static; padding: 0; border: 0; border-radius: 0; box-shadow: none; background: none; }
  .datalist th {
    position: sticky; top: var(--topbar-h); z-index: 1; padding: var(--s-3) var(--s-4); text-align: left; white-space: nowrap;
    background: var(--surface); border-bottom: 1px solid var(--line);
    font-size: var(--fs-xs); font-weight: 600; letter-spacing: .07em; text-transform: uppercase; color: var(--ink-3);
  }
  .datalist td, .datalist td[data-role] { display: table-cell; padding: var(--s-3) var(--s-4); border-bottom: 1px solid var(--line); vertical-align: middle; font-size: var(--fs-md); }
  .datalist tbody tr:last-child td { border-bottom: 0; }
  .datalist tbody tr:hover td { background: var(--surface); }
  .datalist td[data-role="title"] { font-size: var(--fs-md); min-width: 200px; }
  .datalist td[data-role="meta"]::before { content: none; }
  .datalist td[data-role="meta"], .datalist td[data-role="subtitle"] { font-size: var(--fs-md); }
  .datalist td[data-role="amount"], .datalist th.num { text-align: right; }
  .datalist td[data-role="actions"] { display: table-cell; margin: 0; border-top: 0; text-align: right; white-space: nowrap; width: 1%; }
  .datalist td[data-role="actions"]:empty { display: table-cell; }
  .datalist .hide-mobile { display: table-cell; }
  .datalist a.stretched::after { content: none; }
  .datalist a.stretched:hover { color: var(--accent-strong); }
}

/* ---------- form ---------- */
.field { display: block; }
.field + .field { margin-top: var(--s-4); }
.field__label { display: block; margin-bottom: 6px; font-size: var(--fs-sm); font-weight: 600; color: var(--ink); }
.field__hint { display: block; margin-top: 6px; font-size: var(--fs-sm); color: var(--ink-3); }
.input, .select, .textarea {
  display: block; width: 100%; min-height: 48px; padding: 0 var(--s-4);
  border: 1px solid var(--line-strong); border-radius: var(--r-md); background: var(--bg);
  font-size: 1rem;                     /* 16px: sotto, iOS fa lo zoom al focus */
  transition: border-color .15s, box-shadow .15s; appearance: none;
}
.textarea { padding: var(--s-3) var(--s-4); min-height: 120px; resize: vertical; line-height: 1.5; }
.select {
  padding-right: 40px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='none' stroke='%238c8a8d' stroke-width='2' stroke-linecap='round' stroke-linejoin='round' viewBox='0 0 24 24'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 14px center;
}
.input::placeholder, .textarea::placeholder { color: var(--ink-3); }
.input:focus, .select:focus, .textarea:focus { outline: 0; border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.input[type="color"] { padding: 6px; width: 64px; }
.input[type="file"] { padding: 11px var(--s-4); font-size: var(--fs-sm); }

.check { display: flex; align-items: flex-start; gap: var(--s-3); padding: 10px 0; cursor: pointer; }
.check input { flex: none; width: 22px; height: 22px; margin: 1px 0 0; accent-color: var(--accent-strong); }
.check__text { font-weight: 500; }
.check__hint { display: block; font-size: var(--fs-sm); color: var(--ink-3); font-weight: 400; }

/* segmented control (radio travestiti) */
.segmented { display: inline-flex; padding: 3px; border-radius: var(--r-pill); background: var(--surface-2); }
.segmented label { position: relative; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; inset: 0; }
.segmented span { display: block; padding: 8px var(--s-4); border-radius: var(--r-pill); font-size: var(--fs-sm); font-weight: 600; color: var(--ink-2); }
.segmented input:checked + span { background: var(--bg); color: var(--ink); box-shadow: var(--shadow-1); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* barra azioni: su mobile resta attaccata sopra la tab bar */
.form-actions {
  position: sticky; bottom: calc(var(--tabbar-h) + var(--safe-b)); z-index: 5;
  display: flex; gap: var(--s-3); margin: var(--s-6) calc(var(--s-4) * -1) 0; padding: var(--s-3) var(--s-4);
  background: rgba(255, 255, 255, .94); -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px); border-top: 1px solid var(--line);
}
.form-actions .btn { flex: 1; }
@media (min-width: 900px) {
  .form-actions { position: static; margin: var(--s-6) 0 0; padding: 0; background: none; border: 0; backdrop-filter: none; }
  .form-actions .btn { flex: none; }
}

/* ---------- sheet ⇄ dialog ----------
   Un <dialog> nativo (focus trap ed Esc gratis): bottom-sheet su mobile, modal centrato su desktop. */
dialog.sheet {
  width: 100%; max-width: 100%; max-height: 90dvh; margin: auto 0 0; padding: 0; border: 0;
  border-radius: var(--r-sheet) var(--r-sheet) 0 0; background: var(--bg); color: var(--ink); box-shadow: var(--shadow-3);
  overflow: hidden;
}
dialog.sheet[open] { display: flex; flex-direction: column; animation: sheet-up .32s var(--ease); }
dialog.sheet::backdrop { background: rgba(38, 39, 47, .42); -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px); animation: fade-in .25s; }
.sheet__grip { flex: none; padding: 10px 0 2px; touch-action: none; cursor: grab; }
.sheet__grip::before { content: ""; display: block; width: 40px; height: 5px; margin: 0 auto; border-radius: 3px; background: var(--line-strong); }
.sheet__head { flex: none; display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); padding: var(--s-2) var(--s-5) var(--s-3); }
.sheet__title { font: 600 1.5rem/1.15 var(--font-serif); }
.sheet__body { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: var(--s-2) var(--s-5) var(--s-5); }
.sheet__foot { flex: none; display: flex; gap: var(--s-3); padding: var(--s-3) var(--s-5) calc(var(--s-4) + var(--safe-b)); border-top: 1px solid var(--line); background: var(--bg); }
.sheet__foot .btn { flex: 1; }
@keyframes sheet-up { from { transform: translateY(100%); } }
@keyframes fade-in { from { opacity: 0; } }
@keyframes pop-in { from { opacity: 0; transform: translateY(12px) scale(.98); } }

@media (min-width: 900px) {
  dialog.sheet { width: 560px; margin: auto; border-radius: var(--r-lg); max-height: 84dvh; }
  dialog.sheet[open] { animation: pop-in .22s var(--ease); }
  dialog.sheet--wide { width: 720px; }
  .sheet__grip { display: none; }
  .sheet__head { padding-top: var(--s-5); }
  .sheet__foot { justify-content: flex-end; padding-bottom: var(--s-4); }
  .sheet__foot .btn { flex: none; }
}

/* lista di scelte dentro uno sheet (selettore struttura, menu "Altro") */
.optlist > li + li { border-top: 1px solid var(--line); }
.opt {
  display: flex; align-items: center; gap: var(--s-3); width: 100%; min-height: 56px; padding: var(--s-2) 0;
  text-align: left; color: var(--ink); font-weight: 500;
}
.opt:hover { text-decoration: none; }
.opt__check { margin-left: auto; color: var(--accent-strong); }
.opt .subtle { font-weight: 400; }

/* ---------- command palette ---------- */
dialog.palette {
  width: 100%; max-width: 100%; height: 100dvh; max-height: 100dvh; margin: 0; padding: 0; border: 0; background: var(--bg); color: var(--ink);
}
dialog.palette[open] { display: flex; flex-direction: column; animation: fade-in .15s; }
dialog.palette::backdrop { background: rgba(38, 39, 47, .42); -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px); }
.palette__input-wrap { flex: none; display: flex; align-items: center; gap: var(--s-3); padding: calc(var(--safe-t) + var(--s-3)) var(--s-4) var(--s-3); border-bottom: 1px solid var(--line); color: var(--ink-3); }
.palette__input { flex: 1; min-width: 0; height: 44px; border: 0; outline: 0; background: none; font-size: 1.0625rem; }
.palette__list { flex: 1; overflow-y: auto; padding: var(--s-2); }
.palette__item { display: flex; align-items: center; gap: var(--s-3); padding: var(--s-3); border-radius: var(--r-md); color: var(--ink); }
.palette__item:hover, .palette__item.is-selected { background: var(--surface-2); text-decoration: none; }
.palette__item-icon { display: grid; place-items: center; width: 38px; height: 38px; flex: none; border-radius: 11px; background: var(--accent-soft); color: var(--accent-strong); }
.palette__empty { padding: var(--s-7) var(--s-4); text-align: center; color: var(--ink-3); }
@media (min-width: 900px) {
  dialog.palette { width: 620px; height: auto; max-height: 70dvh; margin: 12dvh auto auto; border-radius: var(--r-lg); box-shadow: var(--shadow-3); overflow: hidden; }
  .palette__input-wrap { padding-top: var(--s-3); }
}

/* ---------- toast ---------- */
.toasts {
  position: fixed; z-index: var(--z-toast); left: var(--s-4); right: var(--s-4);
  bottom: calc(var(--tabbar-h) + var(--safe-b) + var(--s-3));
  display: flex; flex-direction: column; gap: var(--s-2); align-items: center; pointer-events: none;
}
.toast {
  pointer-events: auto; display: flex; align-items: center; gap: var(--s-3); max-width: 480px; width: 100%;
  padding: var(--s-3) var(--s-4); border-radius: var(--r-md); background: var(--ink); color: #fff;
  font-size: var(--fs-md); font-weight: 500; box-shadow: var(--shadow-3); animation: pop-in .25s var(--ease);
}
.toast.is-leaving { opacity: 0; transform: translateY(8px); transition: opacity .25s, transform .25s; }
.toast--success .ic { color: #8fd3a8; }
.toast--error .ic { color: #f2a596; }
.toast--info .ic { color: var(--gold); }
@media (min-width: 900px) { .toasts { top: var(--s-5); bottom: auto; left: auto; right: var(--s-6); align-items: flex-end; } }

/* ---------- stati vuoti, skeleton, avvisi ---------- */
.empty { display: flex; flex-direction: column; align-items: center; gap: var(--s-3); padding: var(--s-8) var(--s-4); text-align: center; border: 1px dashed var(--line-strong); border-radius: var(--r-lg); background: var(--surface); }
.empty__icon { display: grid; place-items: center; width: 64px; height: 64px; border-radius: 50%; background: var(--bg); color: var(--accent); box-shadow: var(--shadow-1); }
.empty__title { font: 600 1.5rem/1.15 var(--font-serif); }
.empty__text { max-width: 42ch; color: var(--ink-2); }

.skeleton { border-radius: var(--r-sm); background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface) 50%, var(--surface-2) 75%); background-size: 200% 100%; animation: shimmer 1.4s infinite linear; }
.skeleton--line { height: 14px; } .skeleton--title { height: 22px; width: 55%; } .skeleton--block { height: 96px; border-radius: var(--r-md); }
@keyframes shimmer { to { background-position: -200% 0; } }

.notice { display: flex; gap: var(--s-3); padding: var(--s-4); border-radius: var(--r-md); background: var(--info-soft); color: var(--ink); font-size: var(--fs-md); }
.notice .ic { color: var(--info); margin-top: 2px; }
.notice--warn { background: var(--warn-soft); } .notice--warn .ic { color: var(--warn); }
.notice--danger { background: var(--danger-soft); } .notice--danger .ic { color: var(--danger); }
.notice--ok { background: var(--ok-soft); } .notice--ok .ic { color: var(--ok); }

/* elenco chiave/valore (schede, stato sistema) */
.kv { display: grid; grid-template-columns: 1fr; gap: 0; }
.kv > div { display: flex; justify-content: space-between; gap: var(--s-4); padding: var(--s-3) 0; border-bottom: 1px solid var(--line); }
.kv > div:last-child { border-bottom: 0; }
.kv dt { color: var(--ink-2); flex: none; }
.kv dd { text-align: right; font-weight: 500; min-width: 0; overflow-wrap: anywhere; }

/* ---------- matrice dei permessi ---------- */
.cellbtn {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%; min-height: 36px; padding: 0 var(--s-3);
  border: 1px dashed var(--line-strong); border-radius: var(--r-pill); color: var(--ink-3); font-size: var(--fs-sm); font-weight: 600;
}
.cellbtn:hover { border-color: var(--accent); color: var(--accent-strong); }
.cellbtn.is-set { border-style: solid; border-color: transparent; background: var(--accent-soft); color: var(--accent-strong); }
.cellbtn__dot { width: 6px; height: 6px; border-radius: 50%; background: var(--warn); }
.matrix-mobile > li { border: 1px solid var(--line); border-radius: var(--r-md); background: var(--bg); box-shadow: var(--shadow-1); }
.matrix-mobile > li + li { margin-top: var(--s-3); }
.matrix-mobile__head { display: flex; align-items: center; gap: var(--s-3); padding: var(--s-4); font-weight: 600; font-size: var(--fs-lg); }
.matrix-mobile__row { display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); padding: var(--s-3) var(--s-4); border-top: 1px solid var(--line); }
.matrix-desktop { display: none; }
@media (min-width: 900px) {
  .matrix-mobile { display: none; }
  .matrix-mobile.only-all { display: block; max-width: 560px; }   /* le app di gruppo: stessa lista anche su desktop */
  .matrix-desktop { display: block; }
  .matrix th, .matrix td { padding: var(--s-3) var(--s-4); border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
  .matrix thead th { background: var(--surface); font-size: var(--fs-sm); font-weight: 600; color: var(--ink-2); white-space: nowrap; }
  .matrix tbody th { font-weight: 600; white-space: nowrap; }
  .matrix tbody tr:last-child > * { border-bottom: 0; }
}
.azioni-group + .azioni-group { margin-top: var(--s-4); }
.azioni-group__title { margin-bottom: var(--s-1); }
.is-critica .check__text::after { content: " · delicata"; color: var(--danger); font-size: var(--fs-sm); font-weight: 600; }
.is-changed .check__text { color: var(--warn); }

/* paginazione */
.pager { display: flex; justify-content: center; gap: var(--s-3); margin-top: var(--s-6); }

/* styleguide */
.sg-swatches { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: var(--s-3); }
.sg-swatch { border: 1px solid var(--line); border-radius: var(--r-md); overflow: hidden; font-size: var(--fs-sm); }
.sg-swatch__color { height: 56px; background: var(--c); }
.sg-swatch__name { padding: var(--s-2) var(--s-3); }

/* utilità minime */
.mt-0 { margin-top: 0 !important; } .mt-2 { margin-top: var(--s-2); } .mt-4 { margin-top: var(--s-4); } .mt-6 { margin-top: var(--s-6); }
.text-center { text-align: center; } .text-right { text-align: right; }
.text-danger { color: var(--danger); } .text-ok { color: var(--ok); } .text-warn { color: var(--warn); }
.only-desktop { display: none; }
@media (min-width: 900px) { .only-desktop { display: initial; } .only-mobile { display: none !important; } }
