/* Componenti riusabili. Nomi BEM: .blocco, .blocco__elemento, .blocco--variante */

/* ── Icone ─────────────────────────────────────────── */
.icon {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon--lg { width: 24px; height: 24px; }
.icon--xl { width: 40px; height: 40px; stroke-width: 1.5; }

/* ── Pulsanti ──────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 40px;
  padding: 0 var(--space-4);
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-size: var(--text-sm);
  font-weight: 500;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  text-decoration: none;
  transition: background-color var(--duration-fast), border-color var(--duration-fast),
    color var(--duration-fast), transform var(--duration-fast) var(--ease-out);
}
.btn:hover { text-decoration: none; }
.btn:active:not(:disabled) { transform: scale(0.98); }
/* Disattivato: stesso aspetto per ogni variante (prima un secondario bianco
   semitrasparente sembrava ancora attivo accanto a un primario spento) */
.btn:disabled,
.btn[aria-disabled="true"] {
  border-color: transparent;
  background: var(--surface-3);
  color: var(--text-subtle);
  box-shadow: none;
  cursor: not-allowed;
}
.btn--ghost:disabled { background: transparent; }

.btn--primary { background: var(--accent); color: var(--on-accent); }
.btn--primary:hover:not(:disabled) { background: var(--accent-hover); }

.btn--secondary { background: var(--surface); border-color: var(--border-strong); color: var(--text); }
.btn--secondary:hover:not(:disabled) { background: var(--surface-2); }

.btn--ghost { background: transparent; color: var(--text-muted); }
.btn--ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }

.btn--danger { color: var(--danger); }
.btn--primary.btn--danger { background: var(--danger); color: #fff; }
.btn--ghost.btn--danger:hover:not(:disabled) { background: var(--danger-soft); color: var(--danger); }

.btn--sm { min-height: 32px; padding: 0 var(--space-3); font-size: var(--text-xs); }
.btn--block { width: 100%; }

.btn[data-loading="true"] { position: relative; color: transparent !important; pointer-events: none; }
.btn[data-loading="true"]::after {
  content: "";
  position: absolute;
  width: 16px;
  height: 16px;
  border: 2px solid currentColor;
  border-color: var(--on-accent) var(--on-accent) transparent transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.btn--secondary[data-loading="true"]::after,
.btn--ghost[data-loading="true"]::after { border-color: var(--text) var(--text) transparent transparent; }

.icon-btn {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background-color var(--duration-fast), color var(--duration-fast);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.link-btn {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: var(--text-sm);
  font-weight: 500;
  cursor: pointer;
}
.link-btn:hover { text-decoration: underline; }

/* ── Chip e badge ──────────────────────────────────── */
.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  height: 32px;
  padding: 0 var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface);
  color: var(--text);
  font-size: var(--text-sm);
  font-weight: 500;
  cursor: pointer;
}
.chip { transition: background-color var(--duration-fast), border-color var(--duration-fast), color var(--duration-fast), transform var(--duration-fast) var(--ease-out); }
.chip:active { transform: scale(0.97); }
.chip:hover { background: var(--surface-2); }
.chip--live { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }

.pulse {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
  animation: pulse 1.6s ease-in-out infinite;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px var(--space-2);
  border-radius: var(--radius-full);
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 500;
  white-space: nowrap;
}
.badge--accent { background: var(--accent-soft); color: var(--accent); }
.badge--success { background: var(--success-soft); color: var(--success); }
.badge--warning { background: var(--warning-soft); color: var(--warning); }
.badge--danger { background: var(--danger-soft); color: var(--danger); }

.dot-badge {
  position: absolute;
  top: 3px;
  right: 3px;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: var(--radius-full);
  background: var(--danger);
  color: #fff;
  font-size: 10px;
  font-weight: 600;
  line-height: 16px;
  text-align: center;
}

.count {
  margin-left: var(--space-1);
  color: var(--text-subtle);
  font-variant-numeric: tabular-nums;
}

/* ── Form ──────────────────────────────────────────── */
.form { display: grid; gap: var(--space-4); }
.form__actions { display: flex; flex-wrap: wrap; gap: var(--space-2); justify-content: flex-end; }

.field { display: grid; gap: var(--space-1); }
.field__label { font-size: var(--text-sm); font-weight: 500; }
.field__hint { color: var(--text-subtle); font-size: var(--text-xs); }

.input,
.select,
.textarea {
  width: 100%;
  min-height: 40px;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  transition: border-color var(--duration-fast), box-shadow var(--duration-fast);
}
.textarea { resize: vertical; line-height: var(--leading); }
.input::placeholder,
.textarea::placeholder { color: var(--text-subtle); }
.input:focus,
.select:focus,
.textarea:focus {
  outline: none;
  border-color: var(--focus);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 22%, transparent);
}
.input[aria-invalid="true"] { border-color: var(--danger); }

.input-group { position: relative; display: flex; }
.input-group .input { padding-right: 44px; }
.input-group .icon-btn { position: absolute; right: 2px; top: 2px; }

/* ── Avvisi ────────────────────────────────────────── */
.alert {
  display: flex;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  font-size: var(--text-sm);
}
.alert--danger { background: var(--danger-soft); color: var(--danger); }
.alert--info { background: var(--info-soft); color: var(--info); }
.alert--warning { background: var(--warning-soft); color: var(--warning); }

/* ── Card ──────────────────────────────────────────── */
.card {
  padding: var(--space-5);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}
.card + .card { margin-top: var(--space-4); }
.card__title { margin-bottom: var(--space-2); font-size: var(--text-lg); font-weight: 600; }
.card__text { color: var(--text-muted); font-size: var(--text-sm); }
.card--link {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  color: inherit;
  transition: border-color var(--duration-fast), box-shadow var(--duration-fast);
}
.card--link:hover { border-color: var(--border-strong); box-shadow: var(--shadow); text-decoration: none; }

/* ── Liste ─────────────────────────────────────────── */
.list-card {
  margin: 0;
  padding: 0;
  list-style: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  overflow: hidden;
}
.list-card:empty { display: none; }

.list-item {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--border);
}
.list-item:first-child { border-top: 0; }
.list-item--link { cursor: pointer; transition: background-color var(--duration-fast); }
.list-item--link:hover { background: var(--surface-2); }
.list-item--unread { background: color-mix(in srgb, var(--accent-soft) 55%, transparent); }
.list-item__icon {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  flex: none;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text-muted);
}
.list-item__body { flex: 1; min-width: 0; }
.list-item__title {
  margin: 0;
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text);
}
.list-item__title a { color: inherit; }
.list-item__text {
  margin-top: 2px;
  color: var(--text-muted);
  font-size: var(--text-sm);
  overflow-wrap: anywhere;
}
.list-item__meta { color: var(--text-subtle); font-size: var(--text-xs); white-space: nowrap; }
.list-item__actions { display: flex; gap: var(--space-1); }

.tone-success { color: var(--success); background: var(--success-soft); }
.tone-warning { color: var(--warning); background: var(--warning-soft); }
.tone-danger { color: var(--danger); background: var(--danger-soft); }
.tone-accent { color: var(--accent); background: var(--accent-soft); }

/* ── Stati vuoti e caricamento ─────────────────────── */
.empty {
  display: grid;
  justify-items: center;
  gap: var(--space-2);
  padding: var(--space-8) var(--space-4);
  color: var(--text-muted);
  font-size: var(--text-sm);
  text-align: center;
}
.empty__title { color: var(--text); font-weight: 600; }
.empty__actions { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--space-2); margin-top: var(--space-3); }
.empty--page { padding: var(--space-12) var(--space-4); }
.empty--page h1 { color: var(--text); }
.empty--page p { max-width: 46ch; }
.empty--page .icon--xl { color: var(--text-subtle); }

.skeleton,
.skeleton-card,
.nav__skeleton {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 50%, var(--surface-2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}
.skeleton-card { min-height: 148px; border: 0; box-shadow: none; }
.skeleton-card--tall { min-height: 210px; }
.skeleton-line { height: 12px; border-radius: var(--radius-sm); }

/* ── Tab ───────────────────────────────────────────── */
.tabs {
  display: flex;
  gap: var(--space-1);
  margin-bottom: var(--space-4);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
.tab {
  padding: var(--space-2) var(--space-3);
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
}
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { border-bottom-color: var(--accent); color: var(--text); }
.tabs { position: relative; }
.tab { transition: color var(--duration) var(--ease-out); }
.tabs:has(.tabs__indicator) .tab[aria-selected="true"] { border-bottom-color: transparent; }
.tabs__indicator {
  position: absolute;
  left: 0;
  bottom: -1px;
  height: 2px;
  border-radius: 2px;
  background: var(--accent);
  transition: transform var(--duration-slow) var(--ease-out), width var(--duration-slow) var(--ease-out);
}

/* ── Menu e popover ────────────────────────────────── */
.menu { position: relative; }
.menu__panel {
  position: absolute;
  right: 0;
  top: calc(100% + var(--space-2));
  z-index: var(--z-popover);
  min-width: 220px;
  padding: var(--space-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  transform-origin: top right;
  transition: opacity var(--duration) var(--ease-out), transform var(--duration) var(--ease-out), display var(--duration) allow-discrete;
}
.menu__panel[hidden] { display: none !important; opacity: 0; transform: scale(0.96) translateY(-4px); }
@starting-style {
  .menu__panel:not([hidden]) { opacity: 0; transform: scale(0.96) translateY(-4px); }
}
.menu__panel--up { top: auto; bottom: calc(100% + var(--space-2)); left: 0; right: 0; transform-origin: bottom center; }
.menu__item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--text);
  font-size: var(--text-sm);
  text-align: left;
  cursor: pointer;
}
.menu__item:hover { background: var(--surface-2); text-decoration: none; }
.menu__item--danger { color: var(--danger); }
.menu__sep { margin: var(--space-1) 0; border: 0; border-top: 1px solid var(--border); }

.popover { width: min(340px, calc(100vw - 32px)); padding: 0; }
.popover--wide { width: min(400px, calc(100vw - 32px)); }
.popover__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-3) var(--space-4) var(--space-2);
}
.popover__title {
  padding: var(--space-3) var(--space-4) var(--space-2);
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.popover__head .popover__title { padding: 0; }
.popover__list { max-height: 360px; margin: 0; padding: 0; list-style: none; overflow-y: auto; }
.popover__list .list-item { padding: var(--space-3) var(--space-4); }
.popover__list:empty::after {
  content: attr(data-empty);
  display: block;
  padding: var(--space-6) var(--space-4);
  color: var(--text-subtle);
  font-size: var(--text-sm);
  text-align: center;
}
.popover__foot {
  display: block;
  padding: var(--space-3);
  border-top: 1px solid var(--border);
  font-size: var(--text-sm);
  font-weight: 500;
  text-align: center;
}

/* ── Dialog ────────────────────────────────────────── */
.dialog {
  width: min(460px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
}
/* dialog: entrata e uscita animate, anche per il velo */
.dialog {
  opacity: 0;
  transform: scale(0.96) translateY(8px);
  transition: opacity var(--duration) var(--ease-out), transform var(--duration-slow) var(--ease-out),
    overlay var(--duration) allow-discrete, display var(--duration) allow-discrete;
}
.dialog[open] { opacity: 1; transform: none; }
@starting-style { .dialog[open] { opacity: 0; transform: scale(0.96) translateY(8px); } }
.dialog::backdrop {
  background: rgb(10 12 16 / 0);
  transition: background-color var(--duration) var(--ease-out), overlay var(--duration) allow-discrete, display var(--duration) allow-discrete;
}
.dialog[open]::backdrop { background: rgb(10 12 16 / 0.45); }
@starting-style { .dialog[open]::backdrop { background: rgb(10 12 16 / 0); } }
.dialog__body { display: grid; gap: var(--space-3); max-height: calc(100dvh - 160px); overflow-y: auto; padding: var(--space-6); }
.dialog__title { font-size: var(--text-lg); font-weight: 600; }
.dialog__text { color: var(--text-muted); font-size: var(--text-sm); }
.dialog__actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

/* ── Toast ─────────────────────────────────────────── */
.toasts {
  position: fixed;
  right: var(--space-4);
  bottom: var(--space-4);
  z-index: var(--z-toast);
  display: grid;
  gap: var(--space-2);
  width: min(380px, calc(100vw - 32px));
  pointer-events: none;
}
.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-sm);
  pointer-events: auto;
  animation: toast-in var(--duration) var(--ease-out);
}
.toast[data-leaving] { animation: toast-out var(--duration-fast) ease-in forwards; }
.toast--success .icon { color: var(--success); }
.toast--error .icon { color: var(--danger); }
.toast--info .icon { color: var(--info); }
.toast__body { flex: 1; }
.toast__action { margin-top: var(--space-1); }

/* ── Statistiche e metadati ────────────────────────── */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: var(--space-4); }
.stat { display: grid; gap: var(--space-1); }
.stat__value { font-family: var(--font-mono); font-size: var(--text-xl); font-weight: 500; font-variant-numeric: tabular-nums; }
.stat__label { color: var(--text-muted); font-size: var(--text-xs); }

.meta-list { display: grid; gap: var(--space-3); margin: 0; }
.meta-list div { display: grid; grid-template-columns: 160px 1fr; gap: var(--space-3); }
.meta-list dt { color: var(--text-muted); font-size: var(--text-sm); }
.meta-list dd { margin: 0; font-weight: 500; }

.eyebrow {
  margin-bottom: var(--space-2);
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* ── Marchio ───────────────────────────────────────── */
.brand-mark {
  display: inline-flex;
  width: 22px;
  height: 22px;
  flex: none;
  overflow: hidden;
  border-radius: 5px;
  box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.08);
}
.brand-mark i { flex: 1; }
.brand-mark i:nth-child(1) { background: var(--flag-green); }
.brand-mark i:nth-child(2) { background: var(--flag-white); }
.brand-mark i:nth-child(3) { background: var(--flag-red); }
.brand-name { font-weight: 600; letter-spacing: -0.01em; }

.avatar {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  flex: none;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: var(--text-sm);
  font-weight: 600;
}

/* ── Animazioni ────────────────────────────────────── */
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
@keyframes pop-in { from { opacity: 0; transform: scale(0.97); } to { opacity: 1; transform: scale(1); } }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes toast-out { to { opacity: 0; transform: translateY(4px); } }

/* ── Telefono e touch ─────────────────────────────── */
/* 16px nei campi: sotto questa soglia Safari iOS ingrandisce la pagina al tocco */
@media (max-width: 900px), (pointer: coarse) {
  .input, .select, .textarea, .composer__input { font-size: 16px; }
}

/* Bersagli comodi per il dito (almeno ~36px) */
@media (pointer: coarse) {
  .btn--sm { min-height: 36px; }
  .icon-btn { width: 40px; height: 40px; }
  .chip { height: 36px; }
  .tab { padding-top: var(--space-3); padding-bottom: var(--space-3); }
  .menu__item { min-height: 44px; }
  .pill__remove { width: 32px; height: 32px; }
}

@media (max-width: 560px) {
  /* i toast in alto: in basso coprirebbero la casella di scrittura */
  .toasts { top: calc(var(--topbar-height) + var(--space-2)); bottom: auto; right: var(--space-3); left: var(--space-3); width: auto; }
  .dialog__body { padding: var(--space-5) var(--space-4); }
  .dialog__actions { flex-wrap: wrap; padding: var(--space-3) var(--space-4); }
  .dialog__actions .btn { flex: 1; }
  .dialog__left { flex-basis: 100%; order: 3; }
}

/* ── Dettagli di movimento ─────────────────────────── */
/* Segnaposto che cambia (es. cambio modalità) */
.placeholder-in::placeholder { animation: placeholder-in 360ms var(--ease-out); }
@keyframes placeholder-in { from { opacity: 0; transform: translateX(8px); } to { opacity: 1; transform: none; } }

/* Contenuto che sostituisce uno scheletro di caricamento (solo la prima volta:
   gli aggiornamenti automatici successivi non devono lampeggiare) */
.just-loaded > * { animation: content-in 320ms var(--ease-out) both; }
@keyframes content-in { from { opacity: 0; } to { opacity: 1; } }

/* Comparsa della pagina */
.page, .chat__main { animation: page-in 360ms var(--ease-out) both; }
@keyframes page-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* Hover delle card: leggero sollevamento */
.card--link, .agent-card, .province-card, .gallery__item {
  transition: border-color var(--duration-fast), box-shadow var(--duration), transform var(--duration) var(--ease-out);
}
@media (hover: hover) {
  .card--link:hover, .agent-card:hover, .province-card:hover, .gallery__item:hover { transform: translateY(-2px); }
}

/* Icone che ruotano con lo stato */
.icon { transition: transform var(--duration) var(--ease-out); }
[aria-expanded="true"] > .icon:last-child[class*="chevron"],
.user-btn[aria-expanded="true"] > .icon { transform: rotate(180deg); }
