:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --border: #e2e4e9;
  --text: #1f2430;
  --muted: #6b7280;
  --primary: #2f6fed;
  --primary-dark: #1f52c4;
  --primary-light: #eaf1ff;
  --open: #2f6fed;
  --progress: #b8860b;
  --closed: #4a4f58;
  --reopened: #d9480f;
  --danger: #c0392b;
  --success: #2b7a4b;
  --success-bg: #eaf6ee;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(20, 24, 34, 0.04), 0 1px 8px rgba(20, 24, 34, 0.04);
  --shadow-hover: 0 4px 16px rgba(20, 24, 34, 0.08);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* --- Topbar -------------------------------------------------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
}

.topbar .brand {
  font-weight: 700;
  font-size: 16px;
  white-space: nowrap;
}

.topbar .user-info {
  color: var(--muted);
  font-size: 13px;
  display: flex;
  gap: 14px;
  align-items: center;
  flex-wrap: wrap;
}

.topbar .user-info .email {
  max-width: 40vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* --- Layout ---------------------------------------------------------- */

.container {
  max-width: 880px;
  margin: 0 auto;
  padding: 20px 16px 60px;
}

.page-header {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
}

.page-header h1 { font-size: 20px; margin: 0; }

/* --- Buttons ----------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  transition: background-color .15s ease, transform .05s ease, box-shadow .15s ease;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: var(--primary-dark); text-decoration: none; }
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: .65; cursor: default; transform: none; }
.btn.secondary { background: var(--card); color: var(--text); border-color: var(--border); }
.btn.secondary:hover { background: #f0f1f3; }
.btn.danger { background: var(--danger); }
.btn.block { width: 100%; }
/* Lo spinner (.btn .spinner) e' in componenti.css, condiviso con l'area operatore. */

/* --- Cards --------------------------------------------------------- */

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}

.ticket-card {
  display: flex;
  color: inherit;
  text-decoration: none;
  transition: box-shadow .15s ease, transform .1s ease, border-color .15s ease;
}
a.ticket-card:hover {
  text-decoration: none;
  box-shadow: var(--shadow-hover);
  border-color: #d3d8e2;
}
a.ticket-card:active { transform: scale(0.995); }

.ticket-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  width: 100%;
}

.ticket-row .title { font-weight: 600; }
.ticket-row .meta { color: var(--muted); font-size: 13px; margin-top: 4px; }
.ticket-row .ref { color: var(--muted); font-size: 13px; }
.ticket-row .chevron { color: var(--muted); flex-shrink: 0; }

/* --- Badges / status ------------------------------------------------ */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 11px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
  flex-shrink: 0;
}
.badge::before {
  content: "";
  width: 6px; height: 6px;
  border-radius: 50%;
  background: currentColor;
  filter: brightness(1.6);
}
.badge.status-open { background: var(--open); }
.badge.status-progress { background: var(--progress); }
.badge.status-closed { background: var(--closed); }
.badge.status-reopened { background: var(--reopened); }

/* --- Empty state ----------------------------------------------------- */

.empty-state {
  color: var(--muted);
  text-align: center;
  padding: 56px 20px;
}
.empty-state .icon { font-size: 34px; margin-bottom: 10px; opacity: .6; }
.empty-state p { margin: 0 0 14px; }

/* --- Custom fields ---------------------------------------------------- */

.custom-fields {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 24px;
  color: var(--muted);
  font-size: 13px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
}
.custom-fields strong { color: var(--text); }

/* --- Thread / messages ------------------------------------------------- */

.thread { margin-top: 20px; display: flex; flex-direction: column; gap: 12px; }

.message {
  display: flex;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  background: var(--card);
  box-shadow: var(--shadow);
}
.message.system {
  background: transparent;
  border-style: dashed;
  box-shadow: none;
  color: var(--muted);
  font-size: 13px;
  font-style: italic;
}

.avatar {
  flex-shrink: 0;
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
}
.message.staff .avatar { background: var(--primary); }
.message.client .avatar { background: var(--closed); }

.message-body { flex: 1; min-width: 0; }

.message-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 6px;
}
.message-head .author { font-weight: 600; color: var(--text); }

.status-change-note {
  display: inline-block;
  font-size: 12px;
  color: var(--muted);
  background: var(--bg);
  border-radius: 999px;
  padding: 2px 10px;
  margin-bottom: 6px;
}

.message-text { white-space: pre-wrap; word-break: break-word; }

.attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}
.attachments button {
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  cursor: zoom-in;
  background: none;
  line-height: 0;
  transition: transform .1s ease;
}
.attachments button:hover { transform: scale(1.03); }
.attachments img {
  width: 92px;
  height: 92px;
  object-fit: cover;
  display: block;
}

/* --- Forms ----------------------------------------------------------- */

form .field { margin-bottom: 16px; }
form label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 6px; }
form input[type=text],
form input[type=email],
form input[type=search],
form input[type=password],
form textarea,
form select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-family: inherit;
  background: var(--card);
  color: var(--text);
  transition: border-color .15s ease, box-shadow .15s ease;
}
form input[type=text]:focus,
form input[type=email]:focus,
form input[type=search]:focus,
form input[type=password]:focus,
form textarea:focus,
form select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
  outline: none;
}
form textarea { resize: vertical; min-height: 78px; }
form .help-text { color: var(--muted); font-size: 12px; margin-top: 5px; }

/* --- Campo password con toggle mostra/nascondi -------------------------- */

.password-field { position: relative; }
.password-field input[type=password],
.password-field input[type=text] { padding-right: 42px; }
.password-toggle {
  position: absolute;
  top: 50%;
  right: 4px;
  transform: translateY(-50%);
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  border-radius: 6px;
  color: var(--muted);
  cursor: pointer;
}
.password-toggle:hover { background: var(--bg); color: var(--text); }
.password-toggle svg { width: 18px; height: 18px; }
.password-toggle .icon-eye-off { display: none; }
.password-toggle.is-visible .icon-eye { display: none; }
.password-toggle.is-visible .icon-eye-off { display: block; }
form .errorlist { color: var(--danger); font-size: 13px; padding-left: 0; list-style: none; margin: 6px 0 0; }
form .errorlist li::before { content: "⚠ "; }

/* --- Barra filtri (lista ticket) --------------------------------------- */

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-bottom: 16px;
}
.filter-bar input[type=search] { flex: 1 1 220px; margin-bottom: 0; }
.filter-bar select { flex: 0 1 170px; margin-bottom: 0; width: auto; }
.filter-bar .btn { flex-shrink: 0; }
.filter-clear {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
}

@media (max-width: 560px) {
  .filter-bar { flex-direction: column; align-items: stretch; }
  /* Senza questo reset, "flex: 1 1 220px" del layout a riga (sopra) si
     applica all'asse verticale una volta che .filter-bar diventa una
     colonna: il 220px diventa un'altezza, non una larghezza minima, e la
     casella di ricerca cresce fino a riempire lo spazio disponibile. */
  .filter-bar input[type=search] { flex: 0 0 auto; width: 100%; }
  .filter-bar select { flex: 1 1 auto; width: 100%; }
  .filter-bar .btn { width: 100%; }
  .filter-clear { text-align: center; }
}

.actions-row { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px; }

/* --- Riapertura di un ticket chiuso/risolto -------------------------- */

.reopen-toggle summary {
  cursor: pointer;
  list-style: none;
  width: fit-content;
}
.reopen-toggle summary::-webkit-details-marker { display: none; }
.reopen-toggle summary::marker { content: ""; }
.reopen-toggle[open] summary { margin-bottom: 16px; }
.reopen-body form { margin-top: 0; }

/* Dropzone e anteprime (.dropzone*, .preview-*): in componenti.css,
   condivise con il form "Nuovo Ticket" dell'area operatore. */

/* --- Toasts / messages ------------------------------------------------- */

.messages { list-style: none; padding: 0; margin: 0 0 16px; display: flex; flex-direction: column; gap: 8px; }
.messages li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  background: var(--success-bg);
  color: var(--success);
  font-size: 14px;
  transition: opacity .3s ease, transform .3s ease;
}
.messages li button {
  background: none; border: none; cursor: pointer;
  color: inherit; opacity: .6; font-size: 16px; line-height: 1; padding: 0 2px;
}
.messages li button:hover { opacity: 1; }
.messages li.is-hiding { opacity: 0; transform: translateY(-4px); }

/* Lightbox (.lightbox*): in componenti.css, condivisa con l'area operatore. */

/* --- Confirm dialog ------------------------------------------------ */

dialog.confirm-dialog {
  border: none;
  border-radius: var(--radius);
  padding: 22px;
  max-width: 340px;
  width: calc(100% - 40px);
  box-shadow: var(--shadow-hover);
}
dialog.confirm-dialog::backdrop { background: rgba(15, 17, 23, 0.45); }
dialog.confirm-dialog p { margin: 0 0 18px; }
dialog.confirm-dialog .actions-row { justify-content: flex-end; margin-top: 0; }

/* --- Responsive -------------------------------------------------------- */

@media (max-width: 560px) {
  .container { padding: 16px 12px 48px; }
  .card { padding: 15px; }
  .topbar { padding: 10px 12px; }
  .topbar .user-info .email { display: none; }
  .page-header { align-items: flex-start; }
  .ticket-row { flex-direction: column; align-items: flex-start; gap: 8px; }
  .ticket-row .chevron { display: none; }
  .badge { align-self: flex-start; }
  .actions-row { flex-direction: column; }
  .actions-row .btn { width: 100%; }

  /* Testo e pulsanti pensati per il desktop risultano piccoli al tocco
     su un telefono: qui, non nelle regole base, per non cambiare nulla
     su desktop. form input/textarea/select a 16px evita anche lo zoom
     automatico che Safari su iOS attiva quando il font di un campo e'
     sotto quella soglia. */
  body { font-size: 16px; }
  form input[type=text],
  form input[type=email],
  form input[type=search],
  form input[type=password],
  form textarea,
  form select { font-size: 16px; padding: 12px 14px; }
  .btn { padding: 12px 18px; font-size: 15px; }
  .page-header h1 { font-size: 22px; }
  .ticket-row .title { font-size: 16px; }
  .badge { font-size: 13px; padding: 5px 12px; }
}
