/* ═══════════════════════════════════════════════════════════════════════
   MBStudio Messaggistica — Stylesheet
   Tema: Radio broadcast — professionale, chiaro, leggibile
   Font: DM Sans (display) + Inter (body)
═══════════════════════════════════════════════════════════════════════ */

/* ── Design Tokens ──────────────────────────────────────────────────── */
:root,
[data-theme='light'] {
  /* Surfaces */
  --color-bg: #f4f6f9;
  --color-surface: #ffffff;
  --color-surface-2: #f8f9fb;
  --color-border: #e2e6ec;
  --color-divider: #eaecf0;

  /* Text */
  --color-text: #1a1d23;
  --color-text-muted: #6b7280;
  --color-text-faint: #adb5bd;

  /* Primary — Radio Blu profondo */
  --color-primary: #1a56db;
  --color-primary-hover: #1447c0;
  --color-primary-active: #0e3599;
  --color-primary-highlight: #dbeafe;

  /* Danger */
  --color-danger: #dc2626;
  --color-danger-hover: #b91c1c;
  --color-danger-highlight: #fee2e2;

  /* Success */
  --color-success: #16a34a;
  --color-success-highlight: #dcfce7;

  /* Channel colors */
  --color-whatsapp: #25D366;
  --color-whatsapp-bg: #f0fdf4;
  --color-telegram: #2AABEE;
  --color-telegram-bg: #eff9ff;
  --color-sms: #7c3aed;
  --color-sms-bg: #f5f3ff;

  /* Radius */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-xl: 20px;
  --radius-full: 9999px;

  /* Shadows */
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.08), 0 2px 6px rgba(0,0,0,0.04);
  --shadow-lg: 0 12px 40px rgba(0,0,0,0.12);

  /* Transition */
  --tr: 150ms cubic-bezier(0.16, 1, 0.3, 1);

  /* Type scale */
  --text-xs: 0.75rem;
  --text-sm: 0.875rem;
  --text-base: 1rem;
  --text-lg: 1.125rem;
  --text-xl: 1.375rem;

  /* Font */
  --font-display: 'DM Sans', 'Inter', sans-serif;
  --font-body: 'Inter', 'DM Sans', sans-serif;
}

[data-theme='dark'] {
  --color-bg: #0f1117;
  --color-surface: #171923;
  --color-surface-2: #1e2130;
  --color-border: #2d3147;
  --color-divider: #252840;
  --color-text: #e2e8f0;
  --color-text-muted: #94a3b8;
  --color-text-faint: #475569;
  --color-primary: #60a5fa;
  --color-primary-hover: #93c5fd;
  --color-primary-active: #bfdbfe;
  --color-primary-highlight: #1e3a5f;
  --color-danger: #f87171;
  --color-danger-hover: #fca5a5;
  --color-danger-highlight: #450a0a;
  --color-success: #4ade80;
  --color-success-highlight: #14532d;
  --color-whatsapp-bg: #052e16;
  --color-telegram-bg: #082f49;
  --color-sms-bg: #2e1065;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.4);
  --shadow-lg: 0 12px 40px rgba(0,0,0,0.5);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --color-bg: #0f1117;
    --color-surface: #171923;
    --color-surface-2: #1e2130;
    --color-border: #2d3147;
    --color-divider: #252840;
    --color-text: #e2e8f0;
    --color-text-muted: #94a3b8;
    --color-text-faint: #475569;
    --color-primary: #60a5fa;
    --color-primary-hover: #93c5fd;
    --color-primary-highlight: #1e3a5f;
    --color-danger: #f87171;
    --color-danger-hover: #fca5a5;
    --color-danger-highlight: #450a0a;
    --color-whatsapp-bg: #052e16;
    --color-telegram-bg: #082f49;
    --color-sms-bg: #2e1065;
  }
}

/* ── Reset base ─────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-font-smoothing: antialiased; scroll-behavior: smooth; }
body {
  min-height: 100dvh;
  font-family: var(--font-body);
  font-size: var(--text-base);
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.55;
}
img, svg { display: block; max-width: 100%; }
button { cursor: pointer; border: none; background: none; font: inherit; color: inherit; }
input, select { font: inherit; color: inherit; }

/* ── Pagine ─────────────────────────────────────────────────────────── */
.page { min-height: 100dvh; }

/* ── LOGIN ──────────────────────────────────────────────────────────── */
.page-login {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1a1d23 0%, #1a3a6b 100%);
  padding: 1.5rem;
}

.login-card {
  background: var(--color-surface);
  border-radius: var(--radius-xl);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-lg);
}

.login-logo {
  display: flex;
  align-items: center;
  gap: 0.875rem;
  margin-bottom: 0.5rem;
}

.login-brand {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
}
.login-brand-main {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--color-text);
  letter-spacing: -0.02em;
}
.login-brand-sub {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  font-weight: 500;
}

.login-radio-logo {
  min-height: 8px;
  margin-bottom: 1.75rem;
}
.login-radio-logo:empty { margin-bottom: 1.75rem; }

.login-form { display: flex; flex-direction: column; gap: 1.25rem; }

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}
.form-group label {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text);
}
.form-group input {
  padding: 0.625rem 0.875rem;
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface-2);
  font-size: var(--text-base);
  transition: border-color var(--tr), box-shadow var(--tr);
  outline: none;
}
.form-group input:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-highlight);
}

.input-password-wrap { position: relative; }
.input-password-wrap input { width: 100%; padding-right: 2.75rem; }
.btn-toggle-pw {
  position: absolute;
  right: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--color-text-muted);
  padding: 0.25rem;
  line-height: 0;
  transition: color var(--tr);
}
.btn-toggle-pw:hover { color: var(--color-text); }

.login-error {
  background: var(--color-danger-highlight);
  color: var(--color-danger);
  border-radius: var(--radius-sm);
  padding: 0.625rem 0.875rem;
  font-size: var(--text-sm);
  font-weight: 500;
}

/* ── Buttons ────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  padding: 0.5rem 1rem;
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: 600;
  transition: background var(--tr), color var(--tr), border-color var(--tr), box-shadow var(--tr);
  white-space: nowrap;
}
.btn-primary {
  background: var(--color-primary);
  color: #fff;
}
.btn-primary:hover { background: var(--color-primary-hover); }
.btn-ghost {
  background: transparent;
  color: var(--color-text-muted);
  border: 1.5px solid var(--color-border);
}
.btn-ghost:hover { background: var(--color-surface-2); color: var(--color-text); }
.btn-danger { background: var(--color-danger); color: #fff; }
.btn-danger:hover { background: var(--color-danger-hover); }
.btn-full { width: 100%; padding: 0.75rem 1rem; font-size: var(--text-base); }
.btn-sm { padding: 0.375rem 0.75rem; font-size: var(--text-xs); }
.btn-icon {
  padding: 0.5rem;
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  transition: background var(--tr), color var(--tr);
  line-height: 0;
}
.btn-icon:hover { background: var(--color-surface-2); color: var(--color-text); }

/* ── TOPBAR ─────────────────────────────────────────────────────────── */
.topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.5rem;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  box-shadow: var(--shadow-sm);
  gap: 1rem;
}
.topbar-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.topbar-title {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
}
.topbar-brand {
  font-family: var(--font-display);
  font-size: var(--text-base);
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--color-text);
}
.topbar-sub {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}
.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.user-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.user-name {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text);
}
.role-badge {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.15rem 0.5rem;
  border-radius: var(--radius-full);
  background: var(--color-primary-highlight);
  color: var(--color-primary);
}
.role-badge.role-admin { background: #fef3c7; color: #92400e; }
.role-badge.role-speaker { background: var(--color-primary-highlight); color: var(--color-primary); }
.role-badge.role-user { background: var(--color-surface-2); color: var(--color-text-muted); }

/* ── TOOLBAR FILTRI ─────────────────────────────────────────────────── */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
  padding: 0.875rem 1.5rem;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-divider);
}
.toolbar-left, .toolbar-right {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.filter-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.filter-group label {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text-muted);
}

.select {
  padding: 0.375rem 0.625rem;
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface-2);
  font-size: var(--text-sm);
  color: var(--color-text);
  cursor: pointer;
  outline: none;
  transition: border-color var(--tr);
}
.select:focus { border-color: var(--color-primary); }
.select-sm { padding: 0.25rem 0.5rem; font-size: var(--text-xs); }

/* ── Channel filter buttons ─────────────────────────────────────────── */
.channel-filters {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  background: var(--color-surface-2);
  padding: 0.25rem;
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border);
}
.channel-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 34px;
  border-radius: var(--radius-md);
  transition: background var(--tr), box-shadow var(--tr);
  opacity: 0.5;
}
.channel-btn.active {
  background: var(--color-surface);
  box-shadow: var(--shadow-sm);
  opacity: 1;
}
.channel-btn:hover { opacity: 0.85; }
.channel-btn.active:hover { opacity: 1; }
.channel-all-icon {
  font-size: var(--text-xs);
  font-weight: 700;
  color: var(--color-text-muted);
}
.channel-btn.active .channel-all-icon { color: var(--color-text); }
.sms-badge {
  font-size: 0.65rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  color: #7c3aed;
  background: #ede9fe;
  padding: 0.15rem 0.35rem;
  border-radius: 4px;
}
[data-theme='dark'] .sms-badge {
  color: #c4b5fd;
  background: #3b0764;
}

/* Polling */
.polling-group {
  display: flex;
  align-items: center;
  gap: 0.375rem;
}
.polling-label {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  white-space: nowrap;
}
.polling-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-text-faint);
}
.polling-dot.active {
  background: var(--color-success);
  animation: pulse 2s infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* Contatore */
.msg-count {
  display: flex;
  align-items: baseline;
  gap: 0.25rem;
}
#msg-count-num {
  font-family: var(--font-display);
  font-size: var(--text-lg);
  font-weight: 700;
  color: var(--color-text);
}
.msg-count-label {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

/* ── MESSAGGI ───────────────────────────────────────────────────────── */
.messages-container {
  max-width: 900px;
  margin: 0 auto;
  padding: 1.25rem 1.5rem 4rem;
}

.messages-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.msg-card {
  display: flex;
  align-items: flex-start;
  gap: 0.875rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 0.875rem 1rem;
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--tr), border-color var(--tr);
  position: relative;
}
.msg-card:hover {
  box-shadow: var(--shadow-md);
  border-color: var(--color-divider);
}
.msg-card.channel-WHATSAPP { border-left: 3px solid var(--color-whatsapp); }
.msg-card.channel-TELEGRAM { border-left: 3px solid var(--color-telegram); }
.msg-card.channel-SMS { border-left: 3px solid var(--color-sms); }

.msg-channel-icon {
  flex-shrink: 0;
  width: 38px;
  height: 38px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
}
.msg-channel-icon.icon-WHATSAPP { background: var(--color-whatsapp-bg); }
.msg-channel-icon.icon-TELEGRAM { background: var(--color-telegram-bg); }
.msg-channel-icon.icon-SMS { background: var(--color-sms-bg); }

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

.msg-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.25rem;
  flex-wrap: wrap;
}
.msg-sender {
  font-weight: 700;
  font-size: var(--text-sm);
  color: var(--color-text);
  white-space: nowrap;
}
.msg-number {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  font-family: monospace;
}
.msg-time {
  font-size: var(--text-xs);
  color: var(--color-text-faint);
  margin-left: auto;
  white-space: nowrap;
}

.msg-text {
  font-size: var(--text-base);
  color: var(--color-text);
  line-height: 1.5;
  word-break: break-word;
}

.msg-actions {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  opacity: 0;
  transition: opacity var(--tr);
}
.msg-card:hover .msg-actions { opacity: 1; }

.btn-delete {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-md);
  color: var(--color-text-faint);
  transition: background var(--tr), color var(--tr);
}
.btn-delete:hover {
  background: var(--color-danger-highlight);
  color: var(--color-danger);
}

/* Skeleton loader */
.msg-skeleton {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 0.875rem 1rem;
  display: flex;
  gap: 0.875rem;
}
.sk {
  background: var(--color-divider);
  border-radius: var(--radius-sm);
  animation: shimmer 1.5s infinite;
  background-size: 200% 100%;
  background-image: linear-gradient(90deg, var(--color-divider) 25%, var(--color-border) 50%, var(--color-divider) 75%);
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
.sk-icon { width: 38px; height: 38px; border-radius: var(--radius-md); flex-shrink: 0; }
.sk-body { flex: 1; display: flex; flex-direction: column; gap: 0.5rem; }
.sk-line { height: 12px; }
.sk-line-short { width: 40%; }
.sk-line-long { width: 80%; }
.sk-line-full { width: 100%; }

/* Empty/loading states */
.messages-empty, .messages-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 4rem 1rem;
  color: var(--color-text-muted);
  text-align: center;
}
.messages-empty p, .messages-loading p {
  font-size: var(--text-base);
  color: var(--color-text-muted);
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── MODAL ──────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
  backdrop-filter: blur(2px);
}
.modal {
  background: var(--color-surface);
  border-radius: var(--radius-xl);
  padding: 1.75rem;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-lg);
  animation: modal-in 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}
@keyframes modal-in {
  from { opacity: 0; transform: scale(0.95) translateY(8px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}
.modal-header h3 {
  font-family: var(--font-display);
  font-size: var(--text-lg);
  font-weight: 700;
  margin-bottom: 0.875rem;
}
.modal-body { margin-bottom: 1.5rem; }
.modal-body p { font-size: var(--text-base); color: var(--color-text-muted); line-height: 1.5; }
.modal-note {
  font-size: var(--text-sm) !important;
  margin-top: 0.5rem;
}
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
}

/* ── TOAST ──────────────────────────────────────────────────────────── */
.toast-container {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 2000;
}
.toast {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 0.75rem 1rem;
  font-size: var(--text-sm);
  font-weight: 500;
  box-shadow: var(--shadow-md);
  animation: toast-in 0.25s cubic-bezier(0.16, 1, 0.3, 1);
  min-width: 240px;
  max-width: 360px;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateX(16px); }
  to { opacity: 1; transform: translateX(0); }
}
.toast-success { border-left: 3px solid var(--color-success); }
.toast-error { border-left: 3px solid var(--color-danger); }
.toast-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.toast-success .toast-dot { background: var(--color-success); }
.toast-error .toast-dot { background: var(--color-danger); }

/* ── Responsive ─────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  .topbar { padding: 0.625rem 1rem; }
  .toolbar { padding: 0.75rem 1rem; }
  .messages-container { padding: 1rem 0.75rem 3rem; }
  .user-name { display: none; }
  .polling-label { display: none; }
  .msg-time { display: none; }
}

/* ── Logo radio ─────────────────────────────────────────────────────── */
.login-radio-logo img {
  max-height: 72px;
  max-width: 200px;
  object-fit: contain;
  margin-bottom: 0.5rem;
}
.login-radio-logo:not(:empty) {
  margin-bottom: 1.25rem;
}
.topbar-radio-logo img {
  max-height: 36px;
  max-width: 120px;
  object-fit: contain;
}
.btn-upload-logo {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
}

/* ── Pagina Utenti ──────────────────────────────────────────────────── */
.users-container {
  max-width: 760px;
  margin: 0 auto;
  padding: 2rem 1.5rem 4rem;
}

.users-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.5rem;
}

.users-title {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--color-text);
}

.users-table-wrap {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.users-table {
  width: 100%;
  border-collapse: collapse;
}

.users-table thead tr {
  background: var(--color-surface-2);
  border-bottom: 1px solid var(--color-border);
}

.users-table th {
  padding: 0.75rem 1rem;
  font-size: var(--text-xs);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
  text-align: left;
}

.users-table td {
  padding: 0.875rem 1rem;
  font-size: var(--text-sm);
  color: var(--color-text);
  border-bottom: 1px solid var(--color-divider);
  vertical-align: middle;
}

.users-table tr:last-child td {
  border-bottom: none;
}

.users-table tr:hover td {
  background: var(--color-surface-2);
}

.user-display {
  font-weight: 600;
}

.user-username {
  font-family: monospace;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  background: var(--color-surface-2);
  padding: 0.15rem 0.4rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
}

.users-table td:last-child {
  text-align: right;
  width: 90px;
}

.row-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.25rem;
}

.btn-edit-user, .btn-delete-user {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  transition: background var(--tr), color var(--tr);
}
.btn-edit-user:hover {
  background: var(--color-primary-highlight);
  color: var(--color-primary);
}
.btn-delete-user:hover {
  background: var(--color-danger-highlight);
  color: var(--color-danger);
}

/* Campi form utente */
.field-required { color: var(--color-danger); }
.field-hint {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  font-weight: 400;
}

/* Form modale utente */
#form-user .form-group {
  margin-bottom: 0.25rem;
}
#form-user input, #form-user select {
  padding: 0.625rem 0.875rem;
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface-2);
  font-size: var(--text-base);
  transition: border-color var(--tr), box-shadow var(--tr);
  outline: none;
  width: 100%;
}
#form-user input:focus, #form-user select:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-highlight);
}

/* Riga utente — evidenzia se sei tu */
.users-table tr.is-me td {
  background: var(--color-primary-highlight);
}
