*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #0d1117;
  --bg2: #161b22;
  --bg3: #21262d;
  --bg4: #2d333b;
  --accent: #4f8ef7;
  --accent-hover: #3a7be0;
  --success: #00e676;
  --danger: #ff5252;
  --warning: #ffab40;
  --purple: #e040fb;
  --text: #e6edf3;
  --text2: #7d8590;
  --text3: #484f58;
  --border: #30363d;
  --radius: 10px;
  --radius-lg: 16px;
  --shadow: 0 8px 32px rgba(0,0,0,0.4);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  font-size: 14px;
}

/* ==================== PAGES ==================== */
.page { display: none; min-height: 100vh; }
.page.active { display: flex; }

/* ==================== LOGIN ==================== */
#loginPage {
  justify-content: center;
  align-items: center;
  background: radial-gradient(ellipse at 50% 0%, #1a2340 0%, var(--bg) 70%);
}

.login-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 48px 40px;
  width: 380px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: var(--shadow);
}

.login-logo {
  font-size: 48px;
  text-align: center;
  filter: drop-shadow(0 0 20px rgba(79,142,247,0.4));
}

.login-box h1 {
  text-align: center;
  font-size: 24px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.5px;
}

.login-sub {
  text-align: center;
  color: var(--text2);
  font-size: 13px;
  margin-top: -8px;
  margin-bottom: 8px;
}

.input-group {
  display: flex;
  align-items: center;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: border-color 0.2s;
}

.input-group:focus-within { border-color: var(--accent); }

.input-icon {
  padding: 0 12px;
  font-size: 16px;
}

.input-group input {
  border: none;
  background: transparent;
  padding: 12px 12px 12px 0;
  flex: 1;
  color: var(--text);
  font-size: 14px;
  outline: none;
}

/* ==================== SIDEBAR ==================== */
.sidebar {
  width: 220px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  height: 100vh;
  z-index: 100;
}

.sidebar-logo {
  padding: 20px;
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
  border-bottom: 1px solid var(--border);
  letter-spacing: -0.3px;
}

nav {
  padding: 12px 8px;
  flex: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text2);
  font-size: 14px;
  font-weight: 500;
  transition: all 0.15s;
  position: relative;
  text-decoration: none;
  margin-bottom: 2px;
}

.nav-item:hover { background: var(--bg3); color: var(--text); }
.nav-item.active { background: rgba(79,142,247,0.12); color: var(--accent); }

.nav-icon { font-size: 16px; }

.notif-badge {
  margin-left: auto;
  background: var(--danger);
  color: white;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 20px;
  min-width: 20px;
  text-align: center;
}

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid var(--border);
}

.logout-btn {
  width: 100%;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text2);
  font-size: 13px;
  padding: 9px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.2s;
}

.logout-btn:hover { border-color: var(--danger); color: var(--danger); background: rgba(255,82,82,0.05); }

/* ==================== MAIN ==================== */
.main-content {
  margin-left: 220px;
  padding: 32px;
  min-height: 100vh;
  max-width: 1200px;
  width: 100%;
}

.section { display: none; }
.section.active { display: block; }

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
}

.section-header h1 { font-size: 22px; font-weight: 700; }
.section-sub { color: var(--text2); font-size: 13px; margin-top: 3px; }

/* ==================== SEARCH ==================== */
.search-bar {
  display: flex;
  align-items: center;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0 14px;
  margin-bottom: 20px;
  transition: border-color 0.2s;
}

.search-bar:focus-within { border-color: var(--accent); }
.search-icon { color: var(--text2); margin-right: 8px; }

.search-bar input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  padding: 12px 0;
  color: var(--text);
  font-size: 14px;
}

.search-clear {
  color: var(--text2);
  cursor: pointer;
  padding: 4px;
  border-radius: 50%;
  transition: color 0.2s;
}

.search-clear:hover { color: var(--text); }

/* ==================== GESTORE CARD ==================== */
.gestore-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: 12px;
  overflow: hidden;
  transition: border-color 0.2s;
}

.gestore-card:hover { border-color: var(--accent); }

.gestore-header {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  cursor: pointer;
  user-select: none;
  gap: 12px;
}

.gestore-avatar {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, var(--accent), var(--purple));
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 700;
  color: white;
  flex-shrink: 0;
}

.gestore-info { flex: 1; }
.gestore-name { font-size: 15px; font-weight: 600; }
.gestore-meta { font-size: 12px; color: var(--text2); margin-top: 2px; }

.gestore-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}

.gestore-toggle {
  color: var(--text2);
  font-size: 12px;
  padding: 4px 8px;
  transition: transform 0.2s;
}

.gestore-toggle.open { transform: rotate(180deg); }

/* DISPOSITIVI DENTRO IL GESTORE */
.gestore-devices {
  display: none;
  border-top: 1px solid var(--border);
  background: var(--bg);
  padding: 12px 16px;
}

.gestore-devices.open { display: block; }

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

.devices-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.device-row {
  display: flex;
  align-items: center;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 8px;
  gap: 12px;
  transition: border-color 0.2s;
}

.device-row:hover { border-color: var(--accent); }

.device-status {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.device-status.online { background: var(--success); box-shadow: 0 0 6px var(--success); }
.device-status.offline { background: var(--text3); }

.device-info { flex: 1; }
.device-name { font-size: 14px; font-weight: 500; }
.device-meta { font-size: 11px; color: var(--text2); margin-top: 2px; }

.device-actions { display: flex; gap: 6px; }

.no-devices {
  text-align: center;
  color: var(--text2);
  font-size: 13px;
  padding: 16px;
}

/* ==================== BUTTONS ==================== */
button {
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  border: none;
  border-radius: var(--radius);
  transition: all 0.15s;
  padding: 9px 16px;
}

button:hover { opacity: 0.88; transform: translateY(-1px); }
button:active { transform: translateY(0); }

.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-hover); opacity: 1; }
.btn-secondary { background: var(--bg3); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: rgba(255,82,82,0.12); color: var(--danger); border: 1px solid rgba(255,82,82,0.2); }
.btn-danger:hover { background: rgba(255,82,82,0.2); opacity: 1; }
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-full { width: 100%; margin-top: 8px; }
.btn-icon { padding: 7px 10px; }

/* ==================== INPUTS ==================== */
input, select, textarea {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
}

input:focus, select:focus { border-color: var(--accent); }
select option { background: var(--bg3); }

.form-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text2);
  margin-bottom: 6px;
  margin-top: 4px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.form-group { display: flex; flex-direction: column; gap: 4px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-section-title { font-size: 14px; font-weight: 600; margin-bottom: 12px; color: var(--text2); }

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  padding: 8px 0;
}

.checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

.error { color: var(--danger); font-size: 13px; text-align: center; min-height: 18px; }

/* ==================== MODALS ==================== */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  z-index: 1000;
  justify-content: center;
  align-items: center;
  backdrop-filter: blur(4px);
}

.modal.active { display: flex; }

.modal-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px;
  width: 460px;
  max-width: 95vw;
  max-height: 90vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: var(--shadow);
  animation: modalIn 0.2s ease;
}

.modal-box.wide { width: 680px; }

@keyframes modalIn {
  from { opacity: 0; transform: scale(0.95) translateY(-10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h2 { font-size: 18px; font-weight: 700; }

.modal-close {
  background: var(--bg3);
  color: var(--text2);
  border: 1px solid var(--border);
  width: 30px;
  height: 30px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  border-radius: 50%;
}

.modal-close:hover { color: var(--danger); border-color: var(--danger); opacity: 1; transform: none; }
.modal-buttons { display: flex; gap: 10px; justify-content: flex-end; margin-top: 4px; }

/* ==================== QR ==================== */
.qr-container {
  display: flex;
  justify-content: center;
  padding: 16px;
  background: white;
  border-radius: var(--radius);
}

#qrImage { width: 200px; height: 200px; display: block; }
.qr-subtitle { color: var(--text2); font-size: 13px; text-align: center; }

/* ==================== WIDGETS ==================== */
.widgets-editor { display: flex; flex-direction: column; gap: 8px; }

.widget-row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg3);
  border: 1px solid var(--border);
  padding: 12px 14px;
  border-radius: var(--radius);
  font-size: 13px;
}

.widget-type-badge {
  background: rgba(79,142,247,0.12);
  color: var(--accent);
  padding: 3px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  white-space: nowrap;
}

.widget-info { flex: 1; }
.widget-label { font-weight: 600; }
.widget-pin { color: var(--text2); font-size: 11px; margin-top: 2px; }

.widget-form { display: flex; flex-direction: column; gap: 12px; }
.divider { border: none; border-top: 1px solid var(--border); margin: 4px 0; }

/* COLOR PICKER */
.color-picker-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 4px; }

.color-btn {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid transparent;
  padding: 0;
  cursor: pointer;
  transition: all 0.15s;
  flex-shrink: 0;
}

.color-btn:hover { transform: scale(1.2); border-color: rgba(255,255,255,0.5); opacity: 1; }
.color-btn.selected { border-color: white; transform: scale(1.15); box-shadow: 0 0 8px rgba(255,255,255,0.3); }

/* ==================== NOTIFICHE ==================== */
.notif-row {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  transition: border-color 0.2s;
}

.notif-row.unread { border-left: 3px solid var(--warning); }
.notif-device { font-weight: 600; }
.notif-message { color: var(--text2); font-size: 13px; margin-top: 2px; }
.notif-time { font-size: 11px; color: var(--text3); margin-top: 4px; }
.notif-new { background: rgba(255,171,64,0.12); color: var(--warning); font-size: 11px; font-weight: 700; padding: 3px 8px; border-radius: 6px; white-space: nowrap; }

/* ==================== RESPONSIVE ==================== */
@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); }
  .main-content { margin-left: 0; padding: 16px; }
  .form-row { grid-template-columns: 1fr; }
}