/* ============================================================
   VARIABILI E RESET
   ============================================================ */

:root {
  --primary: #0088cc;
  --primary-dark: #006699;
  --bg: #f0f2f5;
  --bg-sidebar: #ffffff;
  --bg-chat: #e5ddd5;
  --bg-message-in: #ffffff;
  --bg-message-out: #dcf8c6;
  --text: #1a1a1a;
  --text-light: #667781;
  --border: #e0e0e0;
  --danger: #dc3545;
  --success: #28a745;
  --warning: #ffc107;
  --radius: 8px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: var(--bg); color: var(--text); height: 100vh; overflow: hidden; }
input, textarea, select, button { font-family: inherit; font-size: 14px; }
button { cursor: pointer; }

/* ============================================================
   LOGIN
   ============================================================ */

.login-screen {
  display: flex; align-items: center; justify-content: center;
  height: 100vh; background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
}
.login-box {
  background: white; padding: 40px; border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15); text-align: center; width: 360px;
}
.login-box h1 { color: var(--primary); margin-bottom: 8px; }
.login-box p { color: var(--text-light); margin-bottom: 24px; }
.login-box input {
  width: 100%; padding: 12px; border: 1px solid var(--border);
  border-radius: var(--radius); margin-bottom: 16px; font-size: 15px;
}
.login-box button {
  width: 100%; padding: 12px; background: var(--primary); color: white;
  border: none; border-radius: var(--radius); font-size: 15px; font-weight: 600;
}
.login-box button:hover { background: var(--primary-dark); }
.error { color: var(--danger); margin-top: 12px; font-size: 13px; }

/* ============================================================
   DASHBOARD LAYOUT
   ============================================================ */

.dashboard {
  display: flex; height: 100vh;
}

/* ============================================================
   SIDEBAR
   ============================================================ */

.sidebar {
  width: 340px; min-width: 340px; background: var(--bg-sidebar);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
}
.sidebar-header {
  padding: 16px 20px; display: flex; align-items: center; justify-content: space-between;
  border-bottom: 1px solid var(--border);
}
.sidebar-header h2 { font-size: 20px; }
.badge {
  background: var(--primary); color: white; border-radius: 20px;
  padding: 2px 10px; font-size: 13px; font-weight: 600;
}
.search-box {
  padding: 8px 12px; border-bottom: 1px solid var(--border);
}
.search-box input {
  width: 100%; padding: 8px 12px; border: 1px solid var(--border);
  border-radius: 20px; background: var(--bg); font-size: 13px;
}
.client-list {
  flex: 1; overflow-y: auto;
}
.client-item {
  display: flex; align-items: center; padding: 12px 20px; cursor: pointer;
  border-bottom: 1px solid #f0f0f0; transition: background 0.15s;
}
.client-item:hover { background: var(--bg); }
.client-item.active { background: #e8f4fd; }
.client-item .avatar {
  width: 44px; height: 44px; border-radius: 50%; background: var(--primary);
  color: white; display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 16px; margin-right: 12px; flex-shrink: 0;
}
.client-item .client-info { flex: 1; min-width: 0; }
.client-item .client-name {
  font-weight: 600; font-size: 15px; display: flex; align-items: center; gap: 6px;
}
.client-item .client-preview {
  font-size: 13px; color: var(--text-light); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.client-item .client-meta {
  text-align: right; flex-shrink: 0; margin-left: 8px;
}
.client-item .client-time { font-size: 11px; color: var(--text-light); }
.client-item .unread-dot {
  background: var(--primary); color: white; border-radius: 50%;
  width: 22px; height: 22px; display: flex; align-items: center;
  justify-content: center; font-size: 11px; font-weight: 600; margin-top: 4px;
  margin-left: auto;
}
.mode-indicator {
  font-size: 10px; padding: 1px 6px; border-radius: 10px;
  font-weight: 500; white-space: nowrap;
}
.mode-indicator.open { background: #e8f5e9; color: #2e7d32; }
.mode-indicator.request_only { background: #fff3e0; color: #e65100; }
.mode-indicator.request-active { background: #e3f2fd; color: #1565c0; }

/* ============================================================
   AREA CHAT
   ============================================================ */

.chat-area {
  flex: 1; display: flex; flex-direction: column; background: var(--bg-chat);
  min-width: 0;
}
.no-chat {
  flex: 1; display: flex; align-items: center; justify-content: center;
  background: var(--bg);
}
.no-chat-content { text-align: center; color: var(--text-light); }
.no-chat-content h2 { margin-bottom: 8px; color: var(--text); }
.hint { font-size: 12px; color: var(--text-light); margin-top: 4px; }
.hint code { background: #eee; padding: 2px 6px; border-radius: 4px; font-size: 12px; }

.chat-header {
  padding: 12px 20px; background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.chat-header-info { display: flex; align-items: center; gap: 10px; }
.chat-header-info h3 { font-size: 17px; }
.mode-badge {
  font-size: 11px; padding: 2px 8px; border-radius: 10px; font-weight: 500;
}
.mode-badge.open { background: #e8f5e9; color: #2e7d32; }
.mode-badge.request_only { background: #fff3e0; color: #e65100; }
.request-badge {
  font-size: 11px; padding: 2px 8px; border-radius: 10px;
  background: #e3f2fd; color: #1565c0; font-weight: 500;
}

.chat-messages {
  flex: 1; overflow-y: auto; padding: 16px 60px;
  display: flex; flex-direction: column; gap: 4px;
}
.message {
  max-width: 65%; padding: 8px 12px; border-radius: 8px;
  font-size: 14px; line-height: 1.4; position: relative;
  word-wrap: break-word;
}
.message.in {
  background: var(--bg-message-in); align-self: flex-start;
  border-top-left-radius: 0;
}
.message.out {
  background: var(--bg-message-out); align-self: flex-end;
  border-top-right-radius: 0;
}
.message .msg-text { white-space: pre-wrap; }
.message .msg-file {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px; background: rgba(0,0,0,0.05); border-radius: 6px;
  margin-top: 4px; font-size: 13px;
}
.message .msg-file a { color: var(--primary); text-decoration: none; }
.message .msg-file a:hover { text-decoration: underline; }
.message .msg-time {
  font-size: 11px; color: var(--text-light); text-align: right; margin-top: 4px;
}
.message .msg-photo { max-width: 300px; border-radius: 6px; margin-bottom: 4px; }

.system-message {
  text-align: center; font-size: 12px; color: var(--text-light);
  padding: 8px 16px; background: rgba(255,255,255,0.7);
  border-radius: 20px; align-self: center; margin: 8px 0;
}

/* ============================================================
   INPUT AREA
   ============================================================ */

.chat-input-area {
  padding: 10px 16px; background: var(--bg-sidebar);
  border-top: 1px solid var(--border);
}
.chat-input-controls {
  display: flex; align-items: flex-end; gap: 8px;
}
.btn-icon {
  background: none; border: none; font-size: 20px; padding: 6px;
  cursor: pointer; border-radius: 50%; transition: background 0.15s;
}
.btn-icon:hover { background: var(--bg); }
.btn-attach { cursor: pointer; }
#message-input {
  flex: 1; border: 1px solid var(--border); border-radius: 20px;
  padding: 10px 16px; resize: none; max-height: 120px; font-size: 14px;
  line-height: 1.4;
}
#message-input:focus { outline: none; border-color: var(--primary); }
.btn-send {
  background: var(--primary); color: white; border: none;
  width: 40px; height: 40px; border-radius: 50%; font-size: 18px;
  display: flex; align-items: center; justify-content: center;
}
.btn-send:hover { background: var(--primary-dark); }
.file-preview {
  display: flex; align-items: center; gap: 8px; padding: 6px 12px;
  background: #e3f2fd; border-radius: 6px; margin-top: 6px; font-size: 13px;
}

/* ============================================================
   PANNELLO IMPOSTAZIONI
   ============================================================ */

.client-settings {
  width: 340px; min-width: 340px; background: var(--bg-sidebar);
  border-left: 1px solid var(--border); display: flex; flex-direction: column;
  overflow-y: auto;
}
.settings-header {
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.settings-body { padding: 16px 20px; }
.setting-group {
  margin-bottom: 20px; padding-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
}
.setting-group label {
  display: block; font-weight: 600; font-size: 13px;
  color: var(--text-light); margin-bottom: 6px; text-transform: uppercase;
}
.setting-group input[type="text"],
.setting-group textarea,
.setting-group select {
  width: 100%; padding: 8px 12px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: 14px;
}
.setting-group textarea { resize: vertical; }
.btn-small {
  margin-top: 6px; padding: 6px 16px; border: none; border-radius: var(--radius);
  background: var(--primary); color: white; font-size: 13px;
}
.btn-small:hover { background: var(--primary-dark); }
.btn-action {
  width: 100%; padding: 10px; border: none; border-radius: var(--radius);
  font-size: 14px; font-weight: 600; margin-top: 8px;
}
.btn-open { background: var(--primary); color: white; }
.btn-open:hover { background: var(--primary-dark); }
.btn-close { background: var(--danger); color: white; }
.btn-close:hover { background: #c82333; }
.request-active {
  color: var(--success); font-weight: 600; font-size: 14px;
}
.request-topic { font-size: 13px; color: var(--text-light); margin-top: 2px; }
.info-text { font-size: 13px; color: var(--text-light); line-height: 1.5; }
.request-history { max-height: 200px; overflow-y: auto; }
.request-history-item {
  font-size: 12px; padding: 6px 0; border-bottom: 1px solid #f5f5f5;
  color: var(--text-light);
}

/* ============================================================
   MODAL
   ============================================================ */

.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center; z-index: 1000;
}
.modal-content {
  background: white; border-radius: 12px; width: 480px; max-height: 80vh;
  overflow-y: auto; box-shadow: 0 4px 30px rgba(0,0,0,0.2);
}
.modal-header {
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.template-list { padding: 8px; }
.template-item {
  padding: 12px 16px; border-radius: var(--radius); cursor: pointer;
  transition: background 0.15s;
}
.template-item:hover { background: var(--bg); }
.template-item .template-name { font-weight: 600; font-size: 14px; }
.template-item .template-preview { font-size: 13px; color: var(--text-light); margin-top: 2px; }

/* ============================================================
   PULSANTE AGGIUNGI CLIENTE
   ============================================================ */

.btn-add {
  background: var(--primary); color: white; border: none;
  width: 32px; height: 32px; border-radius: 50%; font-size: 20px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; line-height: 1;
}
.btn-add:hover { background: var(--primary-dark); }

/* ============================================================
   SEZIONE INVITI
   ============================================================ */

.invite-link-box {
  display: flex; gap: 6px; margin: 8px 0;
}
.invite-link-box input {
  flex: 1; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: 12px; background: var(--bg);
  color: var(--text);
}
.invite-qr {
  text-align: center; margin: 12px 0;
}
.invite-qr img {
  max-width: 200px; border-radius: var(--radius); border: 1px solid var(--border);
}
.invite-status {
  font-weight: 600; font-size: 14px; padding: 4px 0;
}
.invite-status.pending { color: #e65100; }
.invite-status.connected { color: var(--success); }

/* ============================================================
   MODAL BODY (form nuovo cliente)
   ============================================================ */

.modal-body {
  padding: 20px;
}
.form-group {
  margin-bottom: 16px;
}
.form-group label {
  display: block; font-weight: 600; font-size: 13px;
  color: var(--text-light); margin-bottom: 6px;
}
.form-group input[type="text"],
.form-group textarea,
.form-group select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: 14px;
}
.form-group textarea { resize: vertical; }

/* Status nella lista clienti */
.status-indicator {
  font-size: 10px; padding: 1px 6px; border-radius: 10px;
  font-weight: 500; white-space: nowrap;
}
.status-indicator.pending { background: #fff3e0; color: #e65100; }

/* ============================================================
   SCROLLBAR
   ============================================================ */

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #c1c1c1; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #a1a1a1; }

/* ============================================================
   MENU UTENTE E GESTIONE UTENTI
   ============================================================ */

.sidebar-header-right {
  display: flex; align-items: center; gap: 6px;
}

.sidebar-footer {
  padding: 10px 16px; border-top: 1px solid var(--border);
  background: #fafafa; font-size: 12px; color: var(--text-light);
}
.current-user-label {
  display: flex; align-items: center; gap: 6px;
}
.current-user-label::before {
  content: "●"; color: var(--success); font-size: 10px;
}

.user-menu {
  position: absolute; top: 60px; left: 220px;
  background: white; border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  min-width: 220px; padding: 8px 0; z-index: 500;
}
.user-menu hr {
  border: none; border-top: 1px solid #f0f0f0; margin: 4px 0;
}
.user-menu-item {
  display: block; width: 100%; padding: 10px 16px; text-align: left;
  background: none; border: none; font-size: 14px; color: var(--text);
  cursor: pointer;
}
.user-menu-item:hover { background: var(--bg); }
#user-menu-info { cursor: default; font-size: 13px; color: var(--text-light); }
#user-menu-info:hover { background: transparent; }
#user-menu-info strong { color: var(--text); font-size: 14px; }

.users-list {
  max-height: 260px; overflow-y: auto;
  border: 1px solid var(--border); border-radius: var(--radius);
}
.user-row {
  display: flex; align-items: center; padding: 10px 12px;
  border-bottom: 1px solid #f0f0f0;
}
.user-row:last-child { border-bottom: none; }
.user-row .user-info { flex: 1; }
.user-row .user-name { font-weight: 600; font-size: 14px; }
.user-row .user-username { font-size: 12px; color: var(--text-light); }
.user-row .user-badge {
  background: var(--primary); color: white; border-radius: 10px;
  padding: 1px 8px; font-size: 11px; font-weight: 600; margin-left: 6px;
}
.user-row .user-actions { display: flex; gap: 4px; }
.user-row button {
  padding: 4px 10px; font-size: 12px; border: 1px solid var(--border);
  background: white; border-radius: 4px; cursor: pointer;
}
.user-row button:hover { background: var(--bg); }
.user-row button.danger { color: var(--danger); border-color: #f5c6cb; }
.user-row button.danger:hover { background: #f8d7da; }

/* Mittente del messaggio in uscita (nome segretaria) */
.msg-sender {
  font-size: 11px; font-weight: 600; color: var(--primary-dark);
  margin-bottom: 2px;
}

/* Login con due campi */
.login-box input + input { margin-top: 0; }
