/* ============================================================================
   COMPONENTS — componenti condivisi da tutte le viste.
   Piatto, hairline, senza gradienti né glow. Colore solo semantico.
   ========================================================================== */
@layer components {
  /* ── Bottoni ──────────────────────────────────────────────────── */
  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: 9px 16px;
    font-size: var(--text-sm);
    font-weight: 600;
    line-height: 1;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    white-space: nowrap;
    user-select: none;
  }
  .btn:hover { background: var(--surface-hover); }
  .btn:active { background: var(--surface-sunken); }
  .btn:disabled { opacity: 0.55; cursor: not-allowed; }

  .btn--primary {
    background: var(--brand);
    border-color: var(--brand);
    color: var(--brand-contrast);
  }
  .btn--primary:hover { background: var(--brand-hover); border-color: var(--brand-hover); }
  .btn--primary:active { background: var(--brand-hover); }

  .btn--ghost {
    background: transparent;
    border-color: transparent;
  }
  .btn--ghost:hover { background: var(--surface-hover); border-color: var(--border); }

  .btn--danger {
    background: transparent;
    border-color: var(--danger);
    color: var(--danger);
  }
  .btn--danger:hover { background: var(--danger-soft); }

  .btn--sm {
    padding: 5px 10px;
    font-size: var(--text-xs);
  }

  .btn__icon {
    width: 16px;
    height: 16px;
  }

  /* Spinner funzionale (non decorativo): indica lavoro in corso. */
  .spinner {
    width: 14px;
    height: 14px;
    border: 2px solid currentColor;
    border-top-color: transparent;
    border-radius: 50%;
    display: inline-block;
    animation: fr-spin 0.7s linear infinite;
  }
  @keyframes fr-spin { to { transform: rotate(360deg); } }

  /* ── Card ─────────────────────────────────────────────────────── */
  .card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: var(--space-4);
  }
  .card--flush { padding: 0; }
  .card__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding-bottom: var(--space-3);
    margin-bottom: var(--space-3);
    border-bottom: 1px solid var(--border);
  }
  .card__title {
    font-size: var(--text-md);
    font-weight: 600;
  }
  .card__sub {
    font-size: var(--text-xs);
    color: var(--text-muted);
  }

  /* ── Badge di stato (il colore VIVE qui, solo semantico) ─────── */
  .badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 2px 8px;
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.02em;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface-sunken);
    color: var(--text-muted);
    white-space: nowrap;
  }
  .badge__dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: currentColor;
    flex: none;
  }
  .badge--success { background: var(--success-soft); border-color: transparent; color: var(--success); }
  .badge--warning { background: var(--warning-soft); border-color: transparent; color: var(--warning); }
  .badge--danger  { background: var(--danger-soft);  border-color: transparent; color: var(--danger); }
  .badge--info    { background: var(--info-soft);    border-color: transparent; color: var(--info); }
  .badge--brand   { background: var(--brand-soft);   border-color: transparent; color: var(--brand); }

  /* ── Form ─────────────────────────────────────────────────────── */
  .field {
    display: flex;
    flex-direction: column;
    gap: 5px;
  }
  .field__label {
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
  }
  .input,
  .select,
  .textarea {
    width: 100%;
    padding: 9px 12px;
    font-size: var(--text-md);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--text);
  }
  .input::placeholder,
  .textarea::placeholder { color: var(--text-faint); }
  .input:focus,
  .select:focus,
  .textarea:focus {
    outline: none;
    border-color: var(--focus);
    box-shadow: 0 0 0 3px var(--info-soft);
  }
  .field__error {
    font-size: var(--text-xs);
    color: var(--danger);
  }
  .field--invalid .input,
  .field--invalid .select {
    border-color: var(--danger);
  }

  .form-row {
    display: grid;
    gap: var(--space-3);
    grid-template-columns: 1fr;
  }
  @media (min-width: 640px) {
    .form-row--search {
      grid-template-columns: 1.2fr 1.2fr 0.9fr 0.7fr auto;
      align-items: end;
    }
  }

  /* ── Tabella responsive: tabella su desktop, lista su mobile ─── */
  .table-wrap {
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
  }
  .table {
    width: 100%;
    font-size: var(--text-sm);
  }
  .table th {
    text-align: left;
    font-size: var(--text-xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    background: var(--surface-sunken);
    white-space: nowrap;
  }
  .table td {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
  }
  .table tr:last-child td { border-bottom: none; }
  .table tbody tr:hover { background: var(--surface-hover); }
  .table .num { text-align: right; font-variant-numeric: tabular-nums; }

  /* Su mobile la tabella diventa una lista di card: ogni riga è un blocco
     con etichette, nessun min-width fisso, zero scroll orizzontale. */
  @media (max-width: 767px) {
    .table-wrap { border: none; background: transparent; overflow: visible; }
    .table thead { display: none; }
    .table, .table tbody, .table tr, .table td {
      display: block;
      width: 100%;
    }
    .table tr {
      border: 1px solid var(--border);
      border-radius: var(--radius-md);
      background: var(--surface);
      margin-bottom: var(--space-3);
      padding: var(--space-2) var(--space-3);
    }
    .table td {
      border: none;
      padding: 4px 0;
      display: flex;
      justify-content: space-between;
      align-items: baseline;
      gap: var(--space-3);
      text-align: right;
    }
    .table td::before {
      content: attr(data-label);
      font-size: var(--text-xs);
      font-weight: 600;
      text-transform: uppercase;
      letter-spacing: 0.04em;
      color: var(--text-muted);
      text-align: left;
      flex: none;
    }
  }

  /* ── Stati vuoto / caricamento / errore (uniformi) ───────────── */
  .state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-3);
    padding: var(--space-7) var(--space-4);
    text-align: center;
    color: var(--text-muted);
  }
  .state__icon {
    width: 40px;
    height: 40px;
    color: var(--text-faint);
  }
  /* Le icone SVG degli stati non hanno width/height: senza questo riempirebbero la card. */
  .state > svg {
    width: 40px;
    height: 40px;
    color: var(--text-faint);
  }
  .state__title {
    font-size: var(--text-lg);
    font-weight: 600;
    color: var(--text);
  }
  .state__text {
    font-size: var(--text-sm);
    max-width: 420px;
  }
  .state--error .state__title { color: var(--danger); }
  .state--error .state__icon { color: var(--danger); }

  .skeleton {
    border-radius: var(--radius-sm);
    background: var(--surface-sunken);
    position: relative;
    overflow: hidden;
  }
  .skeleton::after {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg, transparent, var(--surface-hover), transparent);
    animation: fr-shimmer 1.4s ease-in-out infinite;
  }
  @keyframes fr-shimmer {
    from { transform: translateX(-100%); }
    to { transform: translateX(100%); }
  }

  /* ── Callout / avviso ─────────────────────────────────────────── */
  .callout {
    display: flex;
    gap: var(--space-3);
    align-items: flex-start;
    padding: var(--space-3) var(--space-4);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
    font-size: var(--text-sm);
  }
  .callout__icon { flex: none; width: 18px; height: 18px; margin-top: 1px; }
  /* Logo dell'area auth: icona train dentro un box 44px (1em sarebbe troppo piccola). */
  .auth-mark svg { width: 24px; height: 24px; }
  .callout--info { background: var(--info-soft); border-color: transparent; color: var(--info); }
  .callout--warning { background: var(--warning-soft); border-color: transparent; color: var(--warning); }
  .callout--danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }

  /* ── Toast ────────────────────────────────────────────────────── */
  .toast-region {
    position: fixed;
    bottom: var(--space-4);
    left: 50%;
    transform: translateX(-50%);
    z-index: 200;
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    width: min(480px, calc(100vw - 32px));
  }
  .toast {
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-md);
    font-size: var(--text-sm);
  }
  .toast--success { border-color: var(--success); }
  .toast--danger { border-color: var(--danger); }

  /* ── Segmented control (sostituisce i tab senza tabpanel) ────── */
  .segmented {
    display: inline-flex;
    background: var(--surface-sunken);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 3px;
    gap: 2px;
  }
  .segmented__btn {
    padding: 6px 14px;
    font-size: var(--text-sm);
    font-weight: 600;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
  }
  .segmented__btn[aria-current="true"],
  .segmented__btn.is-active {
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow-xs);
  }

  /* ── Accordion / sezione espandibile ──────────────────────────── */
  .accordion {
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
  }
  .accordion + .accordion { margin-top: var(--space-3); }
  .accordion__head {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    font-size: var(--text-md);
    font-weight: 600;
    text-align: left;
  }
  .accordion__head:hover { background: var(--surface-hover); }
  .accordion__chev {
    width: 16px;
    height: 16px;
    color: var(--text-faint);
    transition: transform 0.15s ease;
  }
  .accordion[aria-expanded="true"] .accordion__chev { transform: rotate(180deg); }
  .accordion__body {
    padding: 0 var(--space-4) var(--space-4);
    border-top: 1px solid var(--border);
    padding-top: var(--space-3);
  }
  .accordion[aria-expanded="false"] .accordion__body { display: none; }

  /* ── Modale ───────────────────────────────────────────────────── */
  .modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(10, 11, 13, 0.55);
    z-index: 150;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-4);
  }
  .modal {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    width: min(560px, 100%);
    max-height: 85vh;
    overflow-y: auto;
    padding: var(--space-5);
  }
  .modal__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--space-4);
  }
  .modal__title { font-size: var(--text-lg); font-weight: 600; }
  .modal__close {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
    color: var(--text-muted);
  }
  .modal__close:hover { background: var(--surface-hover); }

  /* ── Elenco metadati (chiave/valore) ──────────────────────────── */
  .kv {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: var(--space-3) var(--space-4);
  }
  .kv__item { min-width: 0; }
  .kv__k {
    font-size: var(--text-xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
  }
  .kv__v {
    font-size: var(--text-md);
    font-weight: 500;
    margin-top: 2px;
    overflow-wrap: anywhere;
  }

  /* ── Chip (filtri) ────────────────────────────────────────────── */
  .chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 12px;
    font-size: var(--text-sm);
    font-weight: 500;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    color: var(--text-muted);
  }
  .chip:hover { background: var(--surface-hover); }
  .chip.is-active {
    background: var(--brand-soft);
    border-color: transparent;
    color: var(--brand);
    font-weight: 600;
  }

  /* ── Link ─────────────────────────────────────────────────────── */
  .link {
    color: var(--info);
    font-weight: 500;
    text-decoration: underline;
    text-underline-offset: 2px;
  }
  .link:hover { text-decoration-thickness: 2px; }

  /* ── Avatar ───────────────────────────────────────────────────── */
  .avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--surface-sunken);
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--text-muted);
    flex: none;
  }
}
